1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
22 
23 class FormatTest : public ::testing::Test {
24 protected:
25   enum IncompleteCheck {
26     IC_ExpectComplete,
27     IC_ExpectIncomplete,
28     IC_DoNotCheck
29   };
30 
31   std::string format(llvm::StringRef Code,
32                      const FormatStyle &Style = getLLVMStyle(),
33                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
34     DEBUG(llvm::errs() << "---\n");
35     DEBUG(llvm::errs() << Code << "\n\n");
36     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
37     bool IncompleteFormat = false;
38     tooling::Replacements Replaces =
39         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
40     if (CheckIncomplete != IC_DoNotCheck) {
41       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
42       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
43     }
44     ReplacementCount = Replaces.size();
45     std::string Result = applyAllReplacements(Code, Replaces);
46     EXPECT_NE("", Result);
47     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
48     return Result;
49   }
50 
51   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
52     FormatStyle Style = getLLVMStyle();
53     Style.ColumnLimit = ColumnLimit;
54     return Style;
55   }
56 
57   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
58     FormatStyle Style = getGoogleStyle();
59     Style.ColumnLimit = ColumnLimit;
60     return Style;
61   }
62 
63   void verifyFormat(llvm::StringRef Code,
64                     const FormatStyle &Style = getLLVMStyle()) {
65     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
66   }
67 
68   void verifyIncompleteFormat(llvm::StringRef Code,
69                               const FormatStyle &Style = getLLVMStyle()) {
70     EXPECT_EQ(Code.str(),
71               format(test::messUp(Code), Style, IC_ExpectIncomplete));
72   }
73 
74   void verifyGoogleFormat(llvm::StringRef Code) {
75     verifyFormat(Code, getGoogleStyle());
76   }
77 
78   void verifyIndependentOfContext(llvm::StringRef text) {
79     verifyFormat(text);
80     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
81   }
82 
83   /// \brief Verify that clang-format does not crash on the given input.
84   void verifyNoCrash(llvm::StringRef Code,
85                      const FormatStyle &Style = getLLVMStyle()) {
86     format(Code, Style, IC_DoNotCheck);
87   }
88 
89   int ReplacementCount;
90 };
91 
92 TEST_F(FormatTest, MessUp) {
93   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
94   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
95   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
96   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
97   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
98 }
99 
100 //===----------------------------------------------------------------------===//
101 // Basic function tests.
102 //===----------------------------------------------------------------------===//
103 
104 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
105   EXPECT_EQ(";", format(";"));
106 }
107 
108 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
109   EXPECT_EQ("int i;", format("  int i;"));
110   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
111   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
112   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
113 }
114 
115 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
116   EXPECT_EQ("int i;", format("int\ni;"));
117 }
118 
119 TEST_F(FormatTest, FormatsNestedBlockStatements) {
120   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
121 }
122 
123 TEST_F(FormatTest, FormatsNestedCall) {
124   verifyFormat("Method(f1, f2(f3));");
125   verifyFormat("Method(f1(f2, f3()));");
126   verifyFormat("Method(f1(f2, (f3())));");
127 }
128 
129 TEST_F(FormatTest, NestedNameSpecifiers) {
130   verifyFormat("vector<::Type> v;");
131   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
132   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
133   verifyFormat("bool a = 2 < ::SomeFunction();");
134 }
135 
136 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
137   EXPECT_EQ("if (a) {\n"
138             "  f();\n"
139             "}",
140             format("if(a){f();}"));
141   EXPECT_EQ(4, ReplacementCount);
142   EXPECT_EQ("if (a) {\n"
143             "  f();\n"
144             "}",
145             format("if (a) {\n"
146                    "  f();\n"
147                    "}"));
148   EXPECT_EQ(0, ReplacementCount);
149   EXPECT_EQ("/*\r\n"
150             "\r\n"
151             "*/\r\n",
152             format("/*\r\n"
153                    "\r\n"
154                    "*/\r\n"));
155   EXPECT_EQ(0, ReplacementCount);
156 }
157 
158 TEST_F(FormatTest, RemovesEmptyLines) {
159   EXPECT_EQ("class C {\n"
160             "  int i;\n"
161             "};",
162             format("class C {\n"
163                    " int i;\n"
164                    "\n"
165                    "};"));
166 
167   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
168   EXPECT_EQ("namespace N {\n"
169             "\n"
170             "int i;\n"
171             "}",
172             format("namespace N {\n"
173                    "\n"
174                    "int    i;\n"
175                    "}",
176                    getGoogleStyle()));
177   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
178             "\n"
179             "int i;\n"
180             "}",
181             format("extern /**/ \"C\" /**/ {\n"
182                    "\n"
183                    "int    i;\n"
184                    "}",
185                    getGoogleStyle()));
186 
187   // ...but do keep inlining and removing empty lines for non-block extern "C"
188   // functions.
189   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
190   EXPECT_EQ("extern \"C\" int f() {\n"
191             "  int i = 42;\n"
192             "  return i;\n"
193             "}",
194             format("extern \"C\" int f() {\n"
195                    "\n"
196                    "  int i = 42;\n"
197                    "  return i;\n"
198                    "}",
199                    getGoogleStyle()));
200 
201   // Remove empty lines at the beginning and end of blocks.
202   EXPECT_EQ("void f() {\n"
203             "\n"
204             "  if (a) {\n"
205             "\n"
206             "    f();\n"
207             "  }\n"
208             "}",
209             format("void f() {\n"
210                    "\n"
211                    "  if (a) {\n"
212                    "\n"
213                    "    f();\n"
214                    "\n"
215                    "  }\n"
216                    "\n"
217                    "}",
218                    getLLVMStyle()));
219   EXPECT_EQ("void f() {\n"
220             "  if (a) {\n"
221             "    f();\n"
222             "  }\n"
223             "}",
224             format("void f() {\n"
225                    "\n"
226                    "  if (a) {\n"
227                    "\n"
228                    "    f();\n"
229                    "\n"
230                    "  }\n"
231                    "\n"
232                    "}",
233                    getGoogleStyle()));
234 
235   // Don't remove empty lines in more complex control statements.
236   EXPECT_EQ("void f() {\n"
237             "  if (a) {\n"
238             "    f();\n"
239             "\n"
240             "  } else if (b) {\n"
241             "    f();\n"
242             "  }\n"
243             "}",
244             format("void f() {\n"
245                    "  if (a) {\n"
246                    "    f();\n"
247                    "\n"
248                    "  } else if (b) {\n"
249                    "    f();\n"
250                    "\n"
251                    "  }\n"
252                    "\n"
253                    "}"));
254 
255   // FIXME: This is slightly inconsistent.
256   EXPECT_EQ("namespace {\n"
257             "int i;\n"
258             "}",
259             format("namespace {\n"
260                    "int i;\n"
261                    "\n"
262                    "}"));
263   EXPECT_EQ("namespace {\n"
264             "int i;\n"
265             "\n"
266             "} // namespace",
267             format("namespace {\n"
268                    "int i;\n"
269                    "\n"
270                    "}  // namespace"));
271 }
272 
273 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
274   verifyFormat("x = (a) and (b);");
275   verifyFormat("x = (a) or (b);");
276   verifyFormat("x = (a) bitand (b);");
277   verifyFormat("x = (a) bitor (b);");
278   verifyFormat("x = (a) not_eq (b);");
279   verifyFormat("x = (a) and_eq (b);");
280   verifyFormat("x = (a) or_eq (b);");
281   verifyFormat("x = (a) xor (b);");
282 }
283 
284 //===----------------------------------------------------------------------===//
285 // Tests for control statements.
286 //===----------------------------------------------------------------------===//
287 
288 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
289   verifyFormat("if (true)\n  f();\ng();");
290   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
291   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
292 
293   FormatStyle AllowsMergedIf = getLLVMStyle();
294   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
295   verifyFormat("if (a)\n"
296                "  // comment\n"
297                "  f();",
298                AllowsMergedIf);
299   verifyFormat("if (a)\n"
300                "  ;",
301                AllowsMergedIf);
302   verifyFormat("if (a)\n"
303                "  if (b) return;",
304                AllowsMergedIf);
305 
306   verifyFormat("if (a) // Can't merge this\n"
307                "  f();\n",
308                AllowsMergedIf);
309   verifyFormat("if (a) /* still don't merge */\n"
310                "  f();",
311                AllowsMergedIf);
312   verifyFormat("if (a) { // Never merge this\n"
313                "  f();\n"
314                "}",
315                AllowsMergedIf);
316   verifyFormat("if (a) {/* Never merge this */\n"
317                "  f();\n"
318                "}",
319                AllowsMergedIf);
320 
321   AllowsMergedIf.ColumnLimit = 14;
322   verifyFormat("if (a) return;", AllowsMergedIf);
323   verifyFormat("if (aaaaaaaaa)\n"
324                "  return;",
325                AllowsMergedIf);
326 
327   AllowsMergedIf.ColumnLimit = 13;
328   verifyFormat("if (a)\n  return;", AllowsMergedIf);
329 }
330 
331 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
332   FormatStyle AllowsMergedLoops = getLLVMStyle();
333   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
334   verifyFormat("while (true) continue;", AllowsMergedLoops);
335   verifyFormat("for (;;) continue;", AllowsMergedLoops);
336   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
337   verifyFormat("while (true)\n"
338                "  ;",
339                AllowsMergedLoops);
340   verifyFormat("for (;;)\n"
341                "  ;",
342                AllowsMergedLoops);
343   verifyFormat("for (;;)\n"
344                "  for (;;) continue;",
345                AllowsMergedLoops);
346   verifyFormat("for (;;) // Can't merge this\n"
347                "  continue;",
348                AllowsMergedLoops);
349   verifyFormat("for (;;) /* still don't merge */\n"
350                "  continue;",
351                AllowsMergedLoops);
352 }
353 
354 TEST_F(FormatTest, FormatShortBracedStatements) {
355   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
356   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
357 
358   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
359   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
360 
361   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
362   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
363   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
364   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
365   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
366   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
367   verifyFormat("if (true) { //\n"
368                "  f();\n"
369                "}",
370                AllowSimpleBracedStatements);
371   verifyFormat("if (true) {\n"
372                "  f();\n"
373                "  f();\n"
374                "}",
375                AllowSimpleBracedStatements);
376   verifyFormat("if (true) {\n"
377                "  f();\n"
378                "} else {\n"
379                "  f();\n"
380                "}",
381                AllowSimpleBracedStatements);
382 
383   verifyFormat("template <int> struct A2 {\n"
384                "  struct B {};\n"
385                "};",
386                AllowSimpleBracedStatements);
387 
388   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
389   verifyFormat("if (true) {\n"
390                "  f();\n"
391                "}",
392                AllowSimpleBracedStatements);
393   verifyFormat("if (true) {\n"
394                "  f();\n"
395                "} else {\n"
396                "  f();\n"
397                "}",
398                AllowSimpleBracedStatements);
399 
400   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
401   verifyFormat("while (true) {\n"
402                "  f();\n"
403                "}",
404                AllowSimpleBracedStatements);
405   verifyFormat("for (;;) {\n"
406                "  f();\n"
407                "}",
408                AllowSimpleBracedStatements);
409 }
410 
411 TEST_F(FormatTest, ParseIfElse) {
412   verifyFormat("if (true)\n"
413                "  if (true)\n"
414                "    if (true)\n"
415                "      f();\n"
416                "    else\n"
417                "      g();\n"
418                "  else\n"
419                "    h();\n"
420                "else\n"
421                "  i();");
422   verifyFormat("if (true)\n"
423                "  if (true)\n"
424                "    if (true) {\n"
425                "      if (true)\n"
426                "        f();\n"
427                "    } else {\n"
428                "      g();\n"
429                "    }\n"
430                "  else\n"
431                "    h();\n"
432                "else {\n"
433                "  i();\n"
434                "}");
435   verifyFormat("void f() {\n"
436                "  if (a) {\n"
437                "  } else {\n"
438                "  }\n"
439                "}");
440 }
441 
442 TEST_F(FormatTest, ElseIf) {
443   verifyFormat("if (a) {\n} else if (b) {\n}");
444   verifyFormat("if (a)\n"
445                "  f();\n"
446                "else if (b)\n"
447                "  g();\n"
448                "else\n"
449                "  h();");
450   verifyFormat("if (a) {\n"
451                "  f();\n"
452                "}\n"
453                "// or else ..\n"
454                "else {\n"
455                "  g()\n"
456                "}");
457 
458   verifyFormat("if (a) {\n"
459                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
460                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
461                "}");
462   verifyFormat("if (a) {\n"
463                "} else if (\n"
464                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
465                "}",
466                getLLVMStyleWithColumns(62));
467 }
468 
469 TEST_F(FormatTest, FormatsForLoop) {
470   verifyFormat(
471       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
472       "     ++VeryVeryLongLoopVariable)\n"
473       "  ;");
474   verifyFormat("for (;;)\n"
475                "  f();");
476   verifyFormat("for (;;) {\n}");
477   verifyFormat("for (;;) {\n"
478                "  f();\n"
479                "}");
480   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
481 
482   verifyFormat(
483       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
484       "                                          E = UnwrappedLines.end();\n"
485       "     I != E; ++I) {\n}");
486 
487   verifyFormat(
488       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
489       "     ++IIIII) {\n}");
490   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
491                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
492                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
493   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
494                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
495                "         E = FD->getDeclsInPrototypeScope().end();\n"
496                "     I != E; ++I) {\n}");
497   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
498                "         I = Container.begin(),\n"
499                "         E = Container.end();\n"
500                "     I != E; ++I) {\n}",
501                getLLVMStyleWithColumns(76));
502 
503   verifyFormat(
504       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
505       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
506       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
507       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
508       "     ++aaaaaaaaaaa) {\n}");
509   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
510                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
511                "     ++i) {\n}");
512   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
513                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
514                "}");
515   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
516                "         aaaaaaaaaa);\n"
517                "     iter; ++iter) {\n"
518                "}");
519   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
520                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
521                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
522                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
523 
524   FormatStyle NoBinPacking = getLLVMStyle();
525   NoBinPacking.BinPackParameters = false;
526   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
527                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
528                "                                           aaaaaaaaaaaaaaaa,\n"
529                "                                           aaaaaaaaaaaaaaaa,\n"
530                "                                           aaaaaaaaaaaaaaaa);\n"
531                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
532                "}",
533                NoBinPacking);
534   verifyFormat(
535       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
536       "                                          E = UnwrappedLines.end();\n"
537       "     I != E;\n"
538       "     ++I) {\n}",
539       NoBinPacking);
540 }
541 
542 TEST_F(FormatTest, RangeBasedForLoops) {
543   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
544                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
545   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
546                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
547   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
548                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
549   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
550                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
551 }
552 
553 TEST_F(FormatTest, ForEachLoops) {
554   verifyFormat("void f() {\n"
555                "  foreach (Item *item, itemlist) {}\n"
556                "  Q_FOREACH (Item *item, itemlist) {}\n"
557                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
558                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
559                "}");
560 
561   // As function-like macros.
562   verifyFormat("#define foreach(x, y)\n"
563                "#define Q_FOREACH(x, y)\n"
564                "#define BOOST_FOREACH(x, y)\n"
565                "#define UNKNOWN_FOREACH(x, y)\n");
566 
567   // Not as function-like macros.
568   verifyFormat("#define foreach (x, y)\n"
569                "#define Q_FOREACH (x, y)\n"
570                "#define BOOST_FOREACH (x, y)\n"
571                "#define UNKNOWN_FOREACH (x, y)\n");
572 }
573 
574 TEST_F(FormatTest, FormatsWhileLoop) {
575   verifyFormat("while (true) {\n}");
576   verifyFormat("while (true)\n"
577                "  f();");
578   verifyFormat("while () {\n}");
579   verifyFormat("while () {\n"
580                "  f();\n"
581                "}");
582 }
583 
584 TEST_F(FormatTest, FormatsDoWhile) {
585   verifyFormat("do {\n"
586                "  do_something();\n"
587                "} while (something());");
588   verifyFormat("do\n"
589                "  do_something();\n"
590                "while (something());");
591 }
592 
593 TEST_F(FormatTest, FormatsSwitchStatement) {
594   verifyFormat("switch (x) {\n"
595                "case 1:\n"
596                "  f();\n"
597                "  break;\n"
598                "case kFoo:\n"
599                "case ns::kBar:\n"
600                "case kBaz:\n"
601                "  break;\n"
602                "default:\n"
603                "  g();\n"
604                "  break;\n"
605                "}");
606   verifyFormat("switch (x) {\n"
607                "case 1: {\n"
608                "  f();\n"
609                "  break;\n"
610                "}\n"
611                "case 2: {\n"
612                "  break;\n"
613                "}\n"
614                "}");
615   verifyFormat("switch (x) {\n"
616                "case 1: {\n"
617                "  f();\n"
618                "  {\n"
619                "    g();\n"
620                "    h();\n"
621                "  }\n"
622                "  break;\n"
623                "}\n"
624                "}");
625   verifyFormat("switch (x) {\n"
626                "case 1: {\n"
627                "  f();\n"
628                "  if (foo) {\n"
629                "    g();\n"
630                "    h();\n"
631                "  }\n"
632                "  break;\n"
633                "}\n"
634                "}");
635   verifyFormat("switch (x) {\n"
636                "case 1: {\n"
637                "  f();\n"
638                "  g();\n"
639                "} break;\n"
640                "}");
641   verifyFormat("switch (test)\n"
642                "  ;");
643   verifyFormat("switch (x) {\n"
644                "default: {\n"
645                "  // Do nothing.\n"
646                "}\n"
647                "}");
648   verifyFormat("switch (x) {\n"
649                "// comment\n"
650                "// if 1, do f()\n"
651                "case 1:\n"
652                "  f();\n"
653                "}");
654   verifyFormat("switch (x) {\n"
655                "case 1:\n"
656                "  // Do amazing stuff\n"
657                "  {\n"
658                "    f();\n"
659                "    g();\n"
660                "  }\n"
661                "  break;\n"
662                "}");
663   verifyFormat("#define A          \\\n"
664                "  switch (x) {     \\\n"
665                "  case a:          \\\n"
666                "    foo = b;       \\\n"
667                "  }",
668                getLLVMStyleWithColumns(20));
669   verifyFormat("#define OPERATION_CASE(name)           \\\n"
670                "  case OP_name:                        \\\n"
671                "    return operations::Operation##name\n",
672                getLLVMStyleWithColumns(40));
673   verifyFormat("switch (x) {\n"
674                "case 1:;\n"
675                "default:;\n"
676                "  int i;\n"
677                "}");
678 
679   verifyGoogleFormat("switch (x) {\n"
680                      "  case 1:\n"
681                      "    f();\n"
682                      "    break;\n"
683                      "  case kFoo:\n"
684                      "  case ns::kBar:\n"
685                      "  case kBaz:\n"
686                      "    break;\n"
687                      "  default:\n"
688                      "    g();\n"
689                      "    break;\n"
690                      "}");
691   verifyGoogleFormat("switch (x) {\n"
692                      "  case 1: {\n"
693                      "    f();\n"
694                      "    break;\n"
695                      "  }\n"
696                      "}");
697   verifyGoogleFormat("switch (test)\n"
698                      "  ;");
699 
700   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
701                      "  case OP_name:              \\\n"
702                      "    return operations::Operation##name\n");
703   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
704                      "  // Get the correction operation class.\n"
705                      "  switch (OpCode) {\n"
706                      "    CASE(Add);\n"
707                      "    CASE(Subtract);\n"
708                      "    default:\n"
709                      "      return operations::Unknown;\n"
710                      "  }\n"
711                      "#undef OPERATION_CASE\n"
712                      "}");
713   verifyFormat("DEBUG({\n"
714                "  switch (x) {\n"
715                "  case A:\n"
716                "    f();\n"
717                "    break;\n"
718                "  // On B:\n"
719                "  case B:\n"
720                "    g();\n"
721                "    break;\n"
722                "  }\n"
723                "});");
724   verifyFormat("switch (a) {\n"
725                "case (b):\n"
726                "  return;\n"
727                "}");
728 
729   verifyFormat("switch (a) {\n"
730                "case some_namespace::\n"
731                "    some_constant:\n"
732                "  return;\n"
733                "}",
734                getLLVMStyleWithColumns(34));
735 }
736 
737 TEST_F(FormatTest, CaseRanges) {
738   verifyFormat("switch (x) {\n"
739                "case 'A' ... 'Z':\n"
740                "case 1 ... 5:\n"
741                "  break;\n"
742                "}");
743 }
744 
745 TEST_F(FormatTest, ShortCaseLabels) {
746   FormatStyle Style = getLLVMStyle();
747   Style.AllowShortCaseLabelsOnASingleLine = true;
748   verifyFormat("switch (a) {\n"
749                "case 1: x = 1; break;\n"
750                "case 2: return;\n"
751                "case 3:\n"
752                "case 4:\n"
753                "case 5: return;\n"
754                "case 6: // comment\n"
755                "  return;\n"
756                "case 7:\n"
757                "  // comment\n"
758                "  return;\n"
759                "case 8:\n"
760                "  x = 8; // comment\n"
761                "  break;\n"
762                "default: y = 1; break;\n"
763                "}",
764                Style);
765   verifyFormat("switch (a) {\n"
766                "#if FOO\n"
767                "case 0: return 0;\n"
768                "#endif\n"
769                "}",
770                Style);
771   verifyFormat("switch (a) {\n"
772                "case 1: {\n"
773                "}\n"
774                "case 2: {\n"
775                "  return;\n"
776                "}\n"
777                "case 3: {\n"
778                "  x = 1;\n"
779                "  return;\n"
780                "}\n"
781                "case 4:\n"
782                "  if (x)\n"
783                "    return;\n"
784                "}",
785                Style);
786   Style.ColumnLimit = 21;
787   verifyFormat("switch (a) {\n"
788                "case 1: x = 1; break;\n"
789                "case 2: return;\n"
790                "case 3:\n"
791                "case 4:\n"
792                "case 5: return;\n"
793                "default:\n"
794                "  y = 1;\n"
795                "  break;\n"
796                "}",
797                Style);
798 }
799 
800 TEST_F(FormatTest, FormatsLabels) {
801   verifyFormat("void f() {\n"
802                "  some_code();\n"
803                "test_label:\n"
804                "  some_other_code();\n"
805                "  {\n"
806                "    some_more_code();\n"
807                "  another_label:\n"
808                "    some_more_code();\n"
809                "  }\n"
810                "}");
811   verifyFormat("{\n"
812                "  some_code();\n"
813                "test_label:\n"
814                "  some_other_code();\n"
815                "}");
816   verifyFormat("{\n"
817                "  some_code();\n"
818                "test_label:;\n"
819                "  int i = 0;\n"
820                "}");
821 }
822 
823 //===----------------------------------------------------------------------===//
824 // Tests for comments.
825 //===----------------------------------------------------------------------===//
826 
827 TEST_F(FormatTest, UnderstandsSingleLineComments) {
828   verifyFormat("//* */");
829   verifyFormat("// line 1\n"
830                "// line 2\n"
831                "void f() {}\n");
832 
833   verifyFormat("void f() {\n"
834                "  // Doesn't do anything\n"
835                "}");
836   verifyFormat("SomeObject\n"
837                "    // Calling someFunction on SomeObject\n"
838                "    .someFunction();");
839   verifyFormat("auto result = SomeObject\n"
840                "                  // Calling someFunction on SomeObject\n"
841                "                  .someFunction();");
842   verifyFormat("void f(int i,  // some comment (probably for i)\n"
843                "       int j,  // some comment (probably for j)\n"
844                "       int k); // some comment (probably for k)");
845   verifyFormat("void f(int i,\n"
846                "       // some comment (probably for j)\n"
847                "       int j,\n"
848                "       // some comment (probably for k)\n"
849                "       int k);");
850 
851   verifyFormat("int i    // This is a fancy variable\n"
852                "    = 5; // with nicely aligned comment.");
853 
854   verifyFormat("// Leading comment.\n"
855                "int a; // Trailing comment.");
856   verifyFormat("int a; // Trailing comment\n"
857                "       // on 2\n"
858                "       // or 3 lines.\n"
859                "int b;");
860   verifyFormat("int a; // Trailing comment\n"
861                "\n"
862                "// Leading comment.\n"
863                "int b;");
864   verifyFormat("int a;    // Comment.\n"
865                "          // More details.\n"
866                "int bbbb; // Another comment.");
867   verifyFormat(
868       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
869       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
870       "int cccccccccccccccccccccccccccccc;       // comment\n"
871       "int ddd;                     // looooooooooooooooooooooooong comment\n"
872       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
873       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
874       "int ccccccccccccccccccc;     // comment");
875 
876   verifyFormat("#include \"a\"     // comment\n"
877                "#include \"a/b/c\" // comment");
878   verifyFormat("#include <a>     // comment\n"
879                "#include <a/b/c> // comment");
880   EXPECT_EQ("#include \"a\"     // comment\n"
881             "#include \"a/b/c\" // comment",
882             format("#include \\\n"
883                    "  \"a\" // comment\n"
884                    "#include \"a/b/c\" // comment"));
885 
886   verifyFormat("enum E {\n"
887                "  // comment\n"
888                "  VAL_A, // comment\n"
889                "  VAL_B\n"
890                "};");
891 
892   verifyFormat(
893       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
894       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
895   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
896                "    // Comment inside a statement.\n"
897                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
898   verifyFormat("SomeFunction(a,\n"
899                "             // comment\n"
900                "             b + x);");
901   verifyFormat("SomeFunction(a, a,\n"
902                "             // comment\n"
903                "             b + x);");
904   verifyFormat(
905       "bool aaaaaaaaaaaaa = // comment\n"
906       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
907       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
908 
909   verifyFormat("int aaaa; // aaaaa\n"
910                "int aa;   // aaaaaaa",
911                getLLVMStyleWithColumns(20));
912 
913   EXPECT_EQ("void f() { // This does something ..\n"
914             "}\n"
915             "int a; // This is unrelated",
916             format("void f()    {     // This does something ..\n"
917                    "  }\n"
918                    "int   a;     // This is unrelated"));
919   EXPECT_EQ("class C {\n"
920             "  void f() { // This does something ..\n"
921             "  }          // awesome..\n"
922             "\n"
923             "  int a; // This is unrelated\n"
924             "};",
925             format("class C{void f()    { // This does something ..\n"
926                    "      } // awesome..\n"
927                    " \n"
928                    "int a;    // This is unrelated\n"
929                    "};"));
930 
931   EXPECT_EQ("int i; // single line trailing comment",
932             format("int i;\\\n// single line trailing comment"));
933 
934   verifyGoogleFormat("int a;  // Trailing comment.");
935 
936   verifyFormat("someFunction(anotherFunction( // Force break.\n"
937                "    parameter));");
938 
939   verifyGoogleFormat("#endif  // HEADER_GUARD");
940 
941   verifyFormat("const char *test[] = {\n"
942                "    // A\n"
943                "    \"aaaa\",\n"
944                "    // B\n"
945                "    \"aaaaa\"};");
946   verifyGoogleFormat(
947       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
948       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
949   EXPECT_EQ("D(a, {\n"
950             "  // test\n"
951             "  int a;\n"
952             "});",
953             format("D(a, {\n"
954                    "// test\n"
955                    "int a;\n"
956                    "});"));
957 
958   EXPECT_EQ("lineWith(); // comment\n"
959             "// at start\n"
960             "otherLine();",
961             format("lineWith();   // comment\n"
962                    "// at start\n"
963                    "otherLine();"));
964   EXPECT_EQ("lineWith(); // comment\n"
965             "            // at start\n"
966             "otherLine();",
967             format("lineWith();   // comment\n"
968                    " // at start\n"
969                    "otherLine();"));
970 
971   EXPECT_EQ("lineWith(); // comment\n"
972             "// at start\n"
973             "otherLine(); // comment",
974             format("lineWith();   // comment\n"
975                    "// at start\n"
976                    "otherLine();   // comment"));
977   EXPECT_EQ("lineWith();\n"
978             "// at start\n"
979             "otherLine(); // comment",
980             format("lineWith();\n"
981                    " // at start\n"
982                    "otherLine();   // comment"));
983   EXPECT_EQ("// first\n"
984             "// at start\n"
985             "otherLine(); // comment",
986             format("// first\n"
987                    " // at start\n"
988                    "otherLine();   // comment"));
989   EXPECT_EQ("f();\n"
990             "// first\n"
991             "// at start\n"
992             "otherLine(); // comment",
993             format("f();\n"
994                    "// first\n"
995                    " // at start\n"
996                    "otherLine();   // comment"));
997   verifyFormat("f(); // comment\n"
998                "// first\n"
999                "// at start\n"
1000                "otherLine();");
1001   EXPECT_EQ("f(); // comment\n"
1002             "// first\n"
1003             "// at start\n"
1004             "otherLine();",
1005             format("f();   // comment\n"
1006                    "// first\n"
1007                    " // at start\n"
1008                    "otherLine();"));
1009   EXPECT_EQ("f(); // comment\n"
1010             "     // first\n"
1011             "// at start\n"
1012             "otherLine();",
1013             format("f();   // comment\n"
1014                    " // first\n"
1015                    "// at start\n"
1016                    "otherLine();"));
1017   EXPECT_EQ("void f() {\n"
1018             "  lineWith(); // comment\n"
1019             "  // at start\n"
1020             "}",
1021             format("void              f() {\n"
1022                    "  lineWith(); // comment\n"
1023                    "  // at start\n"
1024                    "}"));
1025 
1026   verifyFormat("#define A                                                  \\\n"
1027                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1028                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1029                getLLVMStyleWithColumns(60));
1030   verifyFormat(
1031       "#define A                                                   \\\n"
1032       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1033       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1034       getLLVMStyleWithColumns(61));
1035 
1036   verifyFormat("if ( // This is some comment\n"
1037                "    x + 3) {\n"
1038                "}");
1039   EXPECT_EQ("if ( // This is some comment\n"
1040             "     // spanning two lines\n"
1041             "    x + 3) {\n"
1042             "}",
1043             format("if( // This is some comment\n"
1044                    "     // spanning two lines\n"
1045                    " x + 3) {\n"
1046                    "}"));
1047 
1048   verifyNoCrash("/\\\n/");
1049   verifyNoCrash("/\\\n* */");
1050   // The 0-character somehow makes the lexer return a proper comment.
1051   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1052 }
1053 
1054 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1055   EXPECT_EQ("SomeFunction(a,\n"
1056             "             b, // comment\n"
1057             "             c);",
1058             format("SomeFunction(a,\n"
1059                    "          b, // comment\n"
1060                    "      c);"));
1061   EXPECT_EQ("SomeFunction(a, b,\n"
1062             "             // comment\n"
1063             "             c);",
1064             format("SomeFunction(a,\n"
1065                    "          b,\n"
1066                    "  // comment\n"
1067                    "      c);"));
1068   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1069             "             c);",
1070             format("SomeFunction(a, b, // comment (unclear relation)\n"
1071                    "      c);"));
1072   EXPECT_EQ("SomeFunction(a, // comment\n"
1073             "             b,\n"
1074             "             c); // comment",
1075             format("SomeFunction(a,     // comment\n"
1076                    "          b,\n"
1077                    "      c); // comment"));
1078 }
1079 
1080 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1081   EXPECT_EQ("// comment", format("// comment  "));
1082   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1083             format("int aaaaaaa, bbbbbbb; // comment                   ",
1084                    getLLVMStyleWithColumns(33)));
1085   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1086   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1087 }
1088 
1089 TEST_F(FormatTest, UnderstandsBlockComments) {
1090   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1091   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1092   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1093             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1094             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1095                    "/* Trailing comment for aa... */\n"
1096                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1097   EXPECT_EQ(
1098       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1099       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1100       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1101              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1102   EXPECT_EQ(
1103       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1104       "    aaaaaaaaaaaaaaaaaa,\n"
1105       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1106       "}",
1107       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1108              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1109              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1110              "}"));
1111 
1112   FormatStyle NoBinPacking = getLLVMStyle();
1113   NoBinPacking.BinPackParameters = false;
1114   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1115                "         /* parameter 2 */ aaaaaa,\n"
1116                "         /* parameter 3 */ aaaaaa,\n"
1117                "         /* parameter 4 */ aaaaaa);",
1118                NoBinPacking);
1119 
1120   // Aligning block comments in macros.
1121   verifyGoogleFormat("#define A        \\\n"
1122                      "  int i;   /*a*/ \\\n"
1123                      "  int jjj; /*b*/");
1124 }
1125 
1126 TEST_F(FormatTest, AlignsBlockComments) {
1127   EXPECT_EQ("/*\n"
1128             " * Really multi-line\n"
1129             " * comment.\n"
1130             " */\n"
1131             "void f() {}",
1132             format("  /*\n"
1133                    "   * Really multi-line\n"
1134                    "   * comment.\n"
1135                    "   */\n"
1136                    "  void f() {}"));
1137   EXPECT_EQ("class C {\n"
1138             "  /*\n"
1139             "   * Another multi-line\n"
1140             "   * comment.\n"
1141             "   */\n"
1142             "  void f() {}\n"
1143             "};",
1144             format("class C {\n"
1145                    "/*\n"
1146                    " * Another multi-line\n"
1147                    " * comment.\n"
1148                    " */\n"
1149                    "void f() {}\n"
1150                    "};"));
1151   EXPECT_EQ("/*\n"
1152             "  1. This is a comment with non-trivial formatting.\n"
1153             "     1.1. We have to indent/outdent all lines equally\n"
1154             "         1.1.1. to keep the formatting.\n"
1155             " */",
1156             format("  /*\n"
1157                    "    1. This is a comment with non-trivial formatting.\n"
1158                    "       1.1. We have to indent/outdent all lines equally\n"
1159                    "           1.1.1. to keep the formatting.\n"
1160                    "   */"));
1161   EXPECT_EQ("/*\n"
1162             "Don't try to outdent if there's not enough indentation.\n"
1163             "*/",
1164             format("  /*\n"
1165                    " Don't try to outdent if there's not enough indentation.\n"
1166                    " */"));
1167 
1168   EXPECT_EQ("int i; /* Comment with empty...\n"
1169             "        *\n"
1170             "        * line. */",
1171             format("int i; /* Comment with empty...\n"
1172                    "        *\n"
1173                    "        * line. */"));
1174   EXPECT_EQ("int foobar = 0; /* comment */\n"
1175             "int bar = 0;    /* multiline\n"
1176             "                   comment 1 */\n"
1177             "int baz = 0;    /* multiline\n"
1178             "                   comment 2 */\n"
1179             "int bzz = 0;    /* multiline\n"
1180             "                   comment 3 */",
1181             format("int foobar = 0; /* comment */\n"
1182                    "int bar = 0;    /* multiline\n"
1183                    "                   comment 1 */\n"
1184                    "int baz = 0; /* multiline\n"
1185                    "                comment 2 */\n"
1186                    "int bzz = 0;         /* multiline\n"
1187                    "                        comment 3 */"));
1188   EXPECT_EQ("int foobar = 0; /* comment */\n"
1189             "int bar = 0;    /* multiline\n"
1190             "   comment */\n"
1191             "int baz = 0;    /* multiline\n"
1192             "comment */",
1193             format("int foobar = 0; /* comment */\n"
1194                    "int bar = 0; /* multiline\n"
1195                    "comment */\n"
1196                    "int baz = 0;        /* multiline\n"
1197                    "comment */"));
1198 }
1199 
1200 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1201   FormatStyle Style = getLLVMStyleWithColumns(20);
1202   Style.ReflowComments = false;
1203   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1204   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1205 }
1206 
1207 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1208   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1209             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1210             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1211                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1212   EXPECT_EQ(
1213       "void ffffffffffff(\n"
1214       "    int aaaaaaaa, int bbbbbbbb,\n"
1215       "    int cccccccccccc) { /*\n"
1216       "                           aaaaaaaaaa\n"
1217       "                           aaaaaaaaaaaaa\n"
1218       "                           bbbbbbbbbbbbbb\n"
1219       "                           bbbbbbbbbb\n"
1220       "                         */\n"
1221       "}",
1222       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1223              "{ /*\n"
1224              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1225              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1226              "   */\n"
1227              "}",
1228              getLLVMStyleWithColumns(40)));
1229 }
1230 
1231 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1232   EXPECT_EQ("void ffffffffff(\n"
1233             "    int aaaaa /* test */);",
1234             format("void ffffffffff(int aaaaa /* test */);",
1235                    getLLVMStyleWithColumns(35)));
1236 }
1237 
1238 TEST_F(FormatTest, SplitsLongCxxComments) {
1239   EXPECT_EQ("// A comment that\n"
1240             "// doesn't fit on\n"
1241             "// one line",
1242             format("// A comment that doesn't fit on one line",
1243                    getLLVMStyleWithColumns(20)));
1244   EXPECT_EQ("/// A comment that\n"
1245             "/// doesn't fit on\n"
1246             "/// one line",
1247             format("/// A comment that doesn't fit on one line",
1248                    getLLVMStyleWithColumns(20)));
1249   EXPECT_EQ("//! A comment that\n"
1250             "//! doesn't fit on\n"
1251             "//! one line",
1252             format("//! A comment that doesn't fit on one line",
1253                    getLLVMStyleWithColumns(20)));
1254   EXPECT_EQ("// a b c d\n"
1255             "// e f  g\n"
1256             "// h i j k",
1257             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1258   EXPECT_EQ(
1259       "// a b c d\n"
1260       "// e f  g\n"
1261       "// h i j k",
1262       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1263   EXPECT_EQ("if (true) // A comment that\n"
1264             "          // doesn't fit on\n"
1265             "          // one line",
1266             format("if (true) // A comment that doesn't fit on one line   ",
1267                    getLLVMStyleWithColumns(30)));
1268   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1269             format("//    Don't_touch_leading_whitespace",
1270                    getLLVMStyleWithColumns(20)));
1271   EXPECT_EQ("// Add leading\n"
1272             "// whitespace",
1273             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1274   EXPECT_EQ("/// Add leading\n"
1275             "/// whitespace",
1276             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1277   EXPECT_EQ("//! Add leading\n"
1278             "//! whitespace",
1279             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1280   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1281   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1282             "// limit",
1283             format("//Even if it makes the line exceed the column limit",
1284                    getLLVMStyleWithColumns(51)));
1285   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1286 
1287   EXPECT_EQ("// aa bb cc dd",
1288             format("// aa bb             cc dd                   ",
1289                    getLLVMStyleWithColumns(15)));
1290 
1291   EXPECT_EQ("// A comment before\n"
1292             "// a macro\n"
1293             "// definition\n"
1294             "#define a b",
1295             format("// A comment before a macro definition\n"
1296                    "#define a b",
1297                    getLLVMStyleWithColumns(20)));
1298   EXPECT_EQ("void ffffff(\n"
1299             "    int aaaaaaaaa,  // wwww\n"
1300             "    int bbbbbbbbbb, // xxxxxxx\n"
1301             "                    // yyyyyyyyyy\n"
1302             "    int c, int d, int e) {}",
1303             format("void ffffff(\n"
1304                    "    int aaaaaaaaa, // wwww\n"
1305                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1306                    "    int c, int d, int e) {}",
1307                    getLLVMStyleWithColumns(40)));
1308   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1309             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1310                    getLLVMStyleWithColumns(20)));
1311   EXPECT_EQ(
1312       "#define XXX // a b c d\n"
1313       "            // e f g h",
1314       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1315   EXPECT_EQ(
1316       "#define XXX // q w e r\n"
1317       "            // t y u i",
1318       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1319 }
1320 
1321 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1322   EXPECT_EQ("//     A comment\n"
1323             "//     that doesn't\n"
1324             "//     fit on one\n"
1325             "//     line",
1326             format("//     A comment that doesn't fit on one line",
1327                    getLLVMStyleWithColumns(20)));
1328   EXPECT_EQ("///     A comment\n"
1329             "///     that doesn't\n"
1330             "///     fit on one\n"
1331             "///     line",
1332             format("///     A comment that doesn't fit on one line",
1333                    getLLVMStyleWithColumns(20)));
1334 }
1335 
1336 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1337   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1338             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1339             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1340             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1341                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1342                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1343   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1344             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1345             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1346             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1347                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1348                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1349                    getLLVMStyleWithColumns(50)));
1350   // FIXME: One day we might want to implement adjustment of leading whitespace
1351   // of the consecutive lines in this kind of comment:
1352   EXPECT_EQ("double\n"
1353             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1354             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1355             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1356             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1357                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1358                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1359                    getLLVMStyleWithColumns(49)));
1360 }
1361 
1362 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1363   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1364   Pragmas.CommentPragmas = "^ IWYU pragma:";
1365   EXPECT_EQ(
1366       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1367       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1368   EXPECT_EQ(
1369       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1370       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1371 }
1372 
1373 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1374   EXPECT_EQ("if (xxx ==\n"
1375             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1376             "    zzz)\n"
1377             "  q();",
1378             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1379                    "    zzz) q();",
1380                    getLLVMStyleWithColumns(40)));
1381   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1382             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1383             "    zzz)\n"
1384             "  q();",
1385             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1386                    "    zzz) q();",
1387                    getLLVMStyleWithColumns(40)));
1388   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1389             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1390             "    zzz)\n"
1391             "  q();",
1392             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1393                    "    zzz) q();",
1394                    getLLVMStyleWithColumns(40)));
1395   EXPECT_EQ("fffffffff(\n"
1396             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1397             "    zzz);",
1398             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1399                    " zzz);",
1400                    getLLVMStyleWithColumns(40)));
1401 }
1402 
1403 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1404   EXPECT_EQ("#define A(x) /* \\\n"
1405             "  a comment     \\\n"
1406             "  inside */     \\\n"
1407             "  f();",
1408             format("#define A(x) /* \\\n"
1409                    "  a comment     \\\n"
1410                    "  inside */     \\\n"
1411                    "  f();",
1412                    getLLVMStyleWithColumns(17)));
1413   EXPECT_EQ("#define A(      \\\n"
1414             "    x) /*       \\\n"
1415             "  a comment     \\\n"
1416             "  inside */     \\\n"
1417             "  f();",
1418             format("#define A(      \\\n"
1419                    "    x) /*       \\\n"
1420                    "  a comment     \\\n"
1421                    "  inside */     \\\n"
1422                    "  f();",
1423                    getLLVMStyleWithColumns(17)));
1424 }
1425 
1426 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1427   EXPECT_EQ("namespace {}\n// Test\n#define A",
1428             format("namespace {}\n   // Test\n#define A"));
1429   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1430             format("namespace {}\n   /* Test */\n#define A"));
1431   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1432             format("namespace {}\n   /* Test */    #define A"));
1433 }
1434 
1435 TEST_F(FormatTest, SplitsLongLinesInComments) {
1436   EXPECT_EQ("/* This is a long\n"
1437             " * comment that\n"
1438             " * doesn't\n"
1439             " * fit on one line.\n"
1440             " */",
1441             format("/* "
1442                    "This is a long                                         "
1443                    "comment that "
1444                    "doesn't                                    "
1445                    "fit on one line.  */",
1446                    getLLVMStyleWithColumns(20)));
1447   EXPECT_EQ(
1448       "/* a b c d\n"
1449       " * e f  g\n"
1450       " * h i j k\n"
1451       " */",
1452       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1453   EXPECT_EQ(
1454       "/* a b c d\n"
1455       " * e f  g\n"
1456       " * h i j k\n"
1457       " */",
1458       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1459   EXPECT_EQ("/*\n"
1460             "This is a long\n"
1461             "comment that doesn't\n"
1462             "fit on one line.\n"
1463             "*/",
1464             format("/*\n"
1465                    "This is a long                                         "
1466                    "comment that doesn't                                    "
1467                    "fit on one line.                                      \n"
1468                    "*/",
1469                    getLLVMStyleWithColumns(20)));
1470   EXPECT_EQ("/*\n"
1471             " * This is a long\n"
1472             " * comment that\n"
1473             " * doesn't fit on\n"
1474             " * one line.\n"
1475             " */",
1476             format("/*      \n"
1477                    " * This is a long "
1478                    "   comment that     "
1479                    "   doesn't fit on   "
1480                    "   one line.                                            \n"
1481                    " */",
1482                    getLLVMStyleWithColumns(20)));
1483   EXPECT_EQ("/*\n"
1484             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1485             " * so_it_should_be_broken\n"
1486             " * wherever_a_space_occurs\n"
1487             " */",
1488             format("/*\n"
1489                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1490                    "   so_it_should_be_broken "
1491                    "   wherever_a_space_occurs                             \n"
1492                    " */",
1493                    getLLVMStyleWithColumns(20)));
1494   EXPECT_EQ("/*\n"
1495             " *    This_comment_can_not_be_broken_into_lines\n"
1496             " */",
1497             format("/*\n"
1498                    " *    This_comment_can_not_be_broken_into_lines\n"
1499                    " */",
1500                    getLLVMStyleWithColumns(20)));
1501   EXPECT_EQ("{\n"
1502             "  /*\n"
1503             "  This is another\n"
1504             "  long comment that\n"
1505             "  doesn't fit on one\n"
1506             "  line    1234567890\n"
1507             "  */\n"
1508             "}",
1509             format("{\n"
1510                    "/*\n"
1511                    "This is another     "
1512                    "  long comment that "
1513                    "  doesn't fit on one"
1514                    "  line    1234567890\n"
1515                    "*/\n"
1516                    "}",
1517                    getLLVMStyleWithColumns(20)));
1518   EXPECT_EQ("{\n"
1519             "  /*\n"
1520             "   * This        i s\n"
1521             "   * another comment\n"
1522             "   * t hat  doesn' t\n"
1523             "   * fit on one l i\n"
1524             "   * n e\n"
1525             "   */\n"
1526             "}",
1527             format("{\n"
1528                    "/*\n"
1529                    " * This        i s"
1530                    "   another comment"
1531                    "   t hat  doesn' t"
1532                    "   fit on one l i"
1533                    "   n e\n"
1534                    " */\n"
1535                    "}",
1536                    getLLVMStyleWithColumns(20)));
1537   EXPECT_EQ("/*\n"
1538             " * This is a long\n"
1539             " * comment that\n"
1540             " * doesn't fit on\n"
1541             " * one line\n"
1542             " */",
1543             format("   /*\n"
1544                    "    * This is a long comment that doesn't fit on one line\n"
1545                    "    */",
1546                    getLLVMStyleWithColumns(20)));
1547   EXPECT_EQ("{\n"
1548             "  if (something) /* This is a\n"
1549             "                    long\n"
1550             "                    comment */\n"
1551             "    ;\n"
1552             "}",
1553             format("{\n"
1554                    "  if (something) /* This is a long comment */\n"
1555                    "    ;\n"
1556                    "}",
1557                    getLLVMStyleWithColumns(30)));
1558 
1559   EXPECT_EQ("/* A comment before\n"
1560             " * a macro\n"
1561             " * definition */\n"
1562             "#define a b",
1563             format("/* A comment before a macro definition */\n"
1564                    "#define a b",
1565                    getLLVMStyleWithColumns(20)));
1566 
1567   EXPECT_EQ("/* some comment\n"
1568             "     *   a comment\n"
1569             "* that we break\n"
1570             " * another comment\n"
1571             "* we have to break\n"
1572             "* a left comment\n"
1573             " */",
1574             format("  /* some comment\n"
1575                    "       *   a comment that we break\n"
1576                    "   * another comment we have to break\n"
1577                    "* a left comment\n"
1578                    "   */",
1579                    getLLVMStyleWithColumns(20)));
1580 
1581   EXPECT_EQ("/**\n"
1582             " * multiline block\n"
1583             " * comment\n"
1584             " *\n"
1585             " */",
1586             format("/**\n"
1587                    " * multiline block comment\n"
1588                    " *\n"
1589                    " */",
1590                    getLLVMStyleWithColumns(20)));
1591 
1592   EXPECT_EQ("/*\n"
1593             "\n"
1594             "\n"
1595             "    */\n",
1596             format("  /*       \n"
1597                    "      \n"
1598                    "               \n"
1599                    "      */\n"));
1600 
1601   EXPECT_EQ("/* a a */",
1602             format("/* a a            */", getLLVMStyleWithColumns(15)));
1603   EXPECT_EQ("/* a a bc  */",
1604             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1605   EXPECT_EQ("/* aaa aaa\n"
1606             " * aaaaa */",
1607             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1608   EXPECT_EQ("/* aaa aaa\n"
1609             " * aaaaa     */",
1610             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1611 }
1612 
1613 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1614   EXPECT_EQ("#define X          \\\n"
1615             "  /*               \\\n"
1616             "   Test            \\\n"
1617             "   Macro comment   \\\n"
1618             "   with a long     \\\n"
1619             "   line            \\\n"
1620             "   */              \\\n"
1621             "  A + B",
1622             format("#define X \\\n"
1623                    "  /*\n"
1624                    "   Test\n"
1625                    "   Macro comment with a long  line\n"
1626                    "   */ \\\n"
1627                    "  A + B",
1628                    getLLVMStyleWithColumns(20)));
1629   EXPECT_EQ("#define X          \\\n"
1630             "  /* Macro comment \\\n"
1631             "     with a long   \\\n"
1632             "     line */       \\\n"
1633             "  A + B",
1634             format("#define X \\\n"
1635                    "  /* Macro comment with a long\n"
1636                    "     line */ \\\n"
1637                    "  A + B",
1638                    getLLVMStyleWithColumns(20)));
1639   EXPECT_EQ("#define X          \\\n"
1640             "  /* Macro comment \\\n"
1641             "   * with a long   \\\n"
1642             "   * line */       \\\n"
1643             "  A + B",
1644             format("#define X \\\n"
1645                    "  /* Macro comment with a long  line */ \\\n"
1646                    "  A + B",
1647                    getLLVMStyleWithColumns(20)));
1648 }
1649 
1650 TEST_F(FormatTest, CommentsInStaticInitializers) {
1651   EXPECT_EQ(
1652       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1653       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1654       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1655       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1656       "                        aaaaaaaaaaaaaaaaaaaa};",
1657       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1658              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1659              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1660              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1661              "                  aaaaaaaaaaaaaaaaaaaa };"));
1662   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1663                "                        bbbbbbbbbbb, ccccccccccc};");
1664   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1665                "                        // comment for bb....\n"
1666                "                        bbbbbbbbbbb, ccccccccccc};");
1667   verifyGoogleFormat(
1668       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1669       "                        bbbbbbbbbbb, ccccccccccc};");
1670   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1671                      "                        // comment for bb....\n"
1672                      "                        bbbbbbbbbbb, ccccccccccc};");
1673 
1674   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1675                "       {d, e, f},  // Group #2\n"
1676                "       {g, h, i}}; // Group #3");
1677   verifyFormat("S s = {{// Group #1\n"
1678                "        a, b, c},\n"
1679                "       {// Group #2\n"
1680                "        d, e, f},\n"
1681                "       {// Group #3\n"
1682                "        g, h, i}};");
1683 
1684   EXPECT_EQ("S s = {\n"
1685             "    // Some comment\n"
1686             "    a,\n"
1687             "\n"
1688             "    // Comment after empty line\n"
1689             "    b}",
1690             format("S s =    {\n"
1691                    "      // Some comment\n"
1692                    "  a,\n"
1693                    "  \n"
1694                    "     // Comment after empty line\n"
1695                    "      b\n"
1696                    "}"));
1697   EXPECT_EQ("S s = {\n"
1698             "    /* Some comment */\n"
1699             "    a,\n"
1700             "\n"
1701             "    /* Comment after empty line */\n"
1702             "    b}",
1703             format("S s =    {\n"
1704                    "      /* Some comment */\n"
1705                    "  a,\n"
1706                    "  \n"
1707                    "     /* Comment after empty line */\n"
1708                    "      b\n"
1709                    "}"));
1710   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1711                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1712                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1713                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1714 }
1715 
1716 TEST_F(FormatTest, IgnoresIf0Contents) {
1717   EXPECT_EQ("#if 0\n"
1718             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1719             "#endif\n"
1720             "void f() {}",
1721             format("#if 0\n"
1722                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1723                    "#endif\n"
1724                    "void f(  ) {  }"));
1725   EXPECT_EQ("#if false\n"
1726             "void f(  ) {  }\n"
1727             "#endif\n"
1728             "void g() {}\n",
1729             format("#if false\n"
1730                    "void f(  ) {  }\n"
1731                    "#endif\n"
1732                    "void g(  ) {  }\n"));
1733   EXPECT_EQ("enum E {\n"
1734             "  One,\n"
1735             "  Two,\n"
1736             "#if 0\n"
1737             "Three,\n"
1738             "      Four,\n"
1739             "#endif\n"
1740             "  Five\n"
1741             "};",
1742             format("enum E {\n"
1743                    "  One,Two,\n"
1744                    "#if 0\n"
1745                    "Three,\n"
1746                    "      Four,\n"
1747                    "#endif\n"
1748                    "  Five};"));
1749   EXPECT_EQ("enum F {\n"
1750             "  One,\n"
1751             "#if 1\n"
1752             "  Two,\n"
1753             "#if 0\n"
1754             "Three,\n"
1755             "      Four,\n"
1756             "#endif\n"
1757             "  Five\n"
1758             "#endif\n"
1759             "};",
1760             format("enum F {\n"
1761                    "One,\n"
1762                    "#if 1\n"
1763                    "Two,\n"
1764                    "#if 0\n"
1765                    "Three,\n"
1766                    "      Four,\n"
1767                    "#endif\n"
1768                    "Five\n"
1769                    "#endif\n"
1770                    "};"));
1771   EXPECT_EQ("enum G {\n"
1772             "  One,\n"
1773             "#if 0\n"
1774             "Two,\n"
1775             "#else\n"
1776             "  Three,\n"
1777             "#endif\n"
1778             "  Four\n"
1779             "};",
1780             format("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   EXPECT_EQ("enum H {\n"
1790             "  One,\n"
1791             "#if 0\n"
1792             "#ifdef Q\n"
1793             "Two,\n"
1794             "#else\n"
1795             "Three,\n"
1796             "#endif\n"
1797             "#endif\n"
1798             "  Four\n"
1799             "};",
1800             format("enum H {\n"
1801                    "One,\n"
1802                    "#if 0\n"
1803                    "#ifdef Q\n"
1804                    "Two,\n"
1805                    "#else\n"
1806                    "Three,\n"
1807                    "#endif\n"
1808                    "#endif\n"
1809                    "Four\n"
1810                    "};"));
1811   EXPECT_EQ("enum I {\n"
1812             "  One,\n"
1813             "#if /* test */ 0 || 1\n"
1814             "Two,\n"
1815             "Three,\n"
1816             "#endif\n"
1817             "  Four\n"
1818             "};",
1819             format("enum I {\n"
1820                    "One,\n"
1821                    "#if /* test */ 0 || 1\n"
1822                    "Two,\n"
1823                    "Three,\n"
1824                    "#endif\n"
1825                    "Four\n"
1826                    "};"));
1827   EXPECT_EQ("enum J {\n"
1828             "  One,\n"
1829             "#if 0\n"
1830             "#if 0\n"
1831             "Two,\n"
1832             "#else\n"
1833             "Three,\n"
1834             "#endif\n"
1835             "Four,\n"
1836             "#endif\n"
1837             "  Five\n"
1838             "};",
1839             format("enum J {\n"
1840                    "One,\n"
1841                    "#if 0\n"
1842                    "#if 0\n"
1843                    "Two,\n"
1844                    "#else\n"
1845                    "Three,\n"
1846                    "#endif\n"
1847                    "Four,\n"
1848                    "#endif\n"
1849                    "Five\n"
1850                    "};"));
1851 }
1852 
1853 //===----------------------------------------------------------------------===//
1854 // Tests for classes, namespaces, etc.
1855 //===----------------------------------------------------------------------===//
1856 
1857 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1858   verifyFormat("class A {};");
1859 }
1860 
1861 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1862   verifyFormat("class A {\n"
1863                "public:\n"
1864                "public: // comment\n"
1865                "protected:\n"
1866                "private:\n"
1867                "  void f() {}\n"
1868                "};");
1869   verifyGoogleFormat("class A {\n"
1870                      " public:\n"
1871                      " protected:\n"
1872                      " private:\n"
1873                      "  void f() {}\n"
1874                      "};");
1875   verifyFormat("class A {\n"
1876                "public slots:\n"
1877                "  void f1() {}\n"
1878                "public Q_SLOTS:\n"
1879                "  void f2() {}\n"
1880                "protected slots:\n"
1881                "  void f3() {}\n"
1882                "protected Q_SLOTS:\n"
1883                "  void f4() {}\n"
1884                "private slots:\n"
1885                "  void f5() {}\n"
1886                "private Q_SLOTS:\n"
1887                "  void f6() {}\n"
1888                "signals:\n"
1889                "  void g1();\n"
1890                "Q_SIGNALS:\n"
1891                "  void g2();\n"
1892                "};");
1893 
1894   // Don't interpret 'signals' the wrong way.
1895   verifyFormat("signals.set();");
1896   verifyFormat("for (Signals signals : f()) {\n}");
1897   verifyFormat("{\n"
1898                "  signals.set(); // This needs indentation.\n"
1899                "}");
1900 }
1901 
1902 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1903   EXPECT_EQ("class A {\n"
1904             "public:\n"
1905             "  void f();\n"
1906             "\n"
1907             "private:\n"
1908             "  void g() {}\n"
1909             "  // test\n"
1910             "protected:\n"
1911             "  int h;\n"
1912             "};",
1913             format("class A {\n"
1914                    "public:\n"
1915                    "void f();\n"
1916                    "private:\n"
1917                    "void g() {}\n"
1918                    "// test\n"
1919                    "protected:\n"
1920                    "int h;\n"
1921                    "};"));
1922   EXPECT_EQ("class A {\n"
1923             "protected:\n"
1924             "public:\n"
1925             "  void f();\n"
1926             "};",
1927             format("class A {\n"
1928                    "protected:\n"
1929                    "\n"
1930                    "public:\n"
1931                    "\n"
1932                    "  void f();\n"
1933                    "};"));
1934 
1935   // Even ensure proper spacing inside macros.
1936   EXPECT_EQ("#define B     \\\n"
1937             "  class A {   \\\n"
1938             "   protected: \\\n"
1939             "   public:    \\\n"
1940             "    void f(); \\\n"
1941             "  };",
1942             format("#define B     \\\n"
1943                    "  class A {   \\\n"
1944                    "   protected: \\\n"
1945                    "              \\\n"
1946                    "   public:    \\\n"
1947                    "              \\\n"
1948                    "    void f(); \\\n"
1949                    "  };",
1950                    getGoogleStyle()));
1951   // But don't remove empty lines after macros ending in access specifiers.
1952   EXPECT_EQ("#define A private:\n"
1953             "\n"
1954             "int i;",
1955             format("#define A         private:\n"
1956                    "\n"
1957                    "int              i;"));
1958 }
1959 
1960 TEST_F(FormatTest, FormatsClasses) {
1961   verifyFormat("class A : public B {};");
1962   verifyFormat("class A : public ::B {};");
1963 
1964   verifyFormat(
1965       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1966       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1967   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1968                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1969                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1970   verifyFormat(
1971       "class A : public B, public C, public D, public E, public F {};");
1972   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1973                "                     public C,\n"
1974                "                     public D,\n"
1975                "                     public E,\n"
1976                "                     public F,\n"
1977                "                     public G {};");
1978 
1979   verifyFormat("class\n"
1980                "    ReallyReallyLongClassName {\n"
1981                "  int i;\n"
1982                "};",
1983                getLLVMStyleWithColumns(32));
1984   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1985                "                           aaaaaaaaaaaaaaaa> {};");
1986   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1987                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1988                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1989   verifyFormat("template <class R, class C>\n"
1990                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1991                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1992   verifyFormat("class ::A::B {};");
1993 }
1994 
1995 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1996   verifyFormat("class A {\n} a, b;");
1997   verifyFormat("struct A {\n} a, b;");
1998   verifyFormat("union A {\n} a;");
1999 }
2000 
2001 TEST_F(FormatTest, FormatsEnum) {
2002   verifyFormat("enum {\n"
2003                "  Zero,\n"
2004                "  One = 1,\n"
2005                "  Two = One + 1,\n"
2006                "  Three = (One + Two),\n"
2007                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2008                "  Five = (One, Two, Three, Four, 5)\n"
2009                "};");
2010   verifyGoogleFormat("enum {\n"
2011                      "  Zero,\n"
2012                      "  One = 1,\n"
2013                      "  Two = One + 1,\n"
2014                      "  Three = (One + Two),\n"
2015                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2016                      "  Five = (One, Two, Three, Four, 5)\n"
2017                      "};");
2018   verifyFormat("enum Enum {};");
2019   verifyFormat("enum {};");
2020   verifyFormat("enum X E {} d;");
2021   verifyFormat("enum __attribute__((...)) E {} d;");
2022   verifyFormat("enum __declspec__((...)) E {} d;");
2023   verifyFormat("enum {\n"
2024                "  Bar = Foo<int, int>::value\n"
2025                "};",
2026                getLLVMStyleWithColumns(30));
2027 
2028   verifyFormat("enum ShortEnum { A, B, C };");
2029   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2030 
2031   EXPECT_EQ("enum KeepEmptyLines {\n"
2032             "  ONE,\n"
2033             "\n"
2034             "  TWO,\n"
2035             "\n"
2036             "  THREE\n"
2037             "}",
2038             format("enum KeepEmptyLines {\n"
2039                    "  ONE,\n"
2040                    "\n"
2041                    "  TWO,\n"
2042                    "\n"
2043                    "\n"
2044                    "  THREE\n"
2045                    "}"));
2046   verifyFormat("enum E { // comment\n"
2047                "  ONE,\n"
2048                "  TWO\n"
2049                "};\n"
2050                "int i;");
2051   // Not enums.
2052   verifyFormat("enum X f() {\n"
2053                "  a();\n"
2054                "  return 42;\n"
2055                "}");
2056   verifyFormat("enum X Type::f() {\n"
2057                "  a();\n"
2058                "  return 42;\n"
2059                "}");
2060   verifyFormat("enum ::X f() {\n"
2061                "  a();\n"
2062                "  return 42;\n"
2063                "}");
2064   verifyFormat("enum ns::X f() {\n"
2065                "  a();\n"
2066                "  return 42;\n"
2067                "}");
2068 }
2069 
2070 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2071   verifyFormat("enum Type {\n"
2072                "  One = 0; // These semicolons should be commas.\n"
2073                "  Two = 1;\n"
2074                "};");
2075   verifyFormat("namespace n {\n"
2076                "enum Type {\n"
2077                "  One,\n"
2078                "  Two, // missing };\n"
2079                "  int i;\n"
2080                "}\n"
2081                "void g() {}");
2082 }
2083 
2084 TEST_F(FormatTest, FormatsEnumStruct) {
2085   verifyFormat("enum struct {\n"
2086                "  Zero,\n"
2087                "  One = 1,\n"
2088                "  Two = One + 1,\n"
2089                "  Three = (One + Two),\n"
2090                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2091                "  Five = (One, Two, Three, Four, 5)\n"
2092                "};");
2093   verifyFormat("enum struct Enum {};");
2094   verifyFormat("enum struct {};");
2095   verifyFormat("enum struct X E {} d;");
2096   verifyFormat("enum struct __attribute__((...)) E {} d;");
2097   verifyFormat("enum struct __declspec__((...)) E {} d;");
2098   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2099 }
2100 
2101 TEST_F(FormatTest, FormatsEnumClass) {
2102   verifyFormat("enum class {\n"
2103                "  Zero,\n"
2104                "  One = 1,\n"
2105                "  Two = One + 1,\n"
2106                "  Three = (One + Two),\n"
2107                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2108                "  Five = (One, Two, Three, Four, 5)\n"
2109                "};");
2110   verifyFormat("enum class Enum {};");
2111   verifyFormat("enum class {};");
2112   verifyFormat("enum class X E {} d;");
2113   verifyFormat("enum class __attribute__((...)) E {} d;");
2114   verifyFormat("enum class __declspec__((...)) E {} d;");
2115   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2116 }
2117 
2118 TEST_F(FormatTest, FormatsEnumTypes) {
2119   verifyFormat("enum X : int {\n"
2120                "  A, // Force multiple lines.\n"
2121                "  B\n"
2122                "};");
2123   verifyFormat("enum X : int { A, B };");
2124   verifyFormat("enum X : std::uint32_t { A, B };");
2125 }
2126 
2127 TEST_F(FormatTest, FormatsNSEnums) {
2128   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2129   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2130                      "  // Information about someDecentlyLongValue.\n"
2131                      "  someDecentlyLongValue,\n"
2132                      "  // Information about anotherDecentlyLongValue.\n"
2133                      "  anotherDecentlyLongValue,\n"
2134                      "  // Information about aThirdDecentlyLongValue.\n"
2135                      "  aThirdDecentlyLongValue\n"
2136                      "};");
2137   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2138                      "  a = 1,\n"
2139                      "  b = 2,\n"
2140                      "  c = 3,\n"
2141                      "};");
2142   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2143                      "  a = 1,\n"
2144                      "  b = 2,\n"
2145                      "  c = 3,\n"
2146                      "};");
2147   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2148                      "  a = 1,\n"
2149                      "  b = 2,\n"
2150                      "  c = 3,\n"
2151                      "};");
2152 }
2153 
2154 TEST_F(FormatTest, FormatsBitfields) {
2155   verifyFormat("struct Bitfields {\n"
2156                "  unsigned sClass : 8;\n"
2157                "  unsigned ValueKind : 2;\n"
2158                "};");
2159   verifyFormat("struct A {\n"
2160                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2161                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2162                "};");
2163   verifyFormat("struct MyStruct {\n"
2164                "  uchar data;\n"
2165                "  uchar : 8;\n"
2166                "  uchar : 8;\n"
2167                "  uchar other;\n"
2168                "};");
2169 }
2170 
2171 TEST_F(FormatTest, FormatsNamespaces) {
2172   verifyFormat("namespace some_namespace {\n"
2173                "class A {};\n"
2174                "void f() { f(); }\n"
2175                "}");
2176   verifyFormat("namespace {\n"
2177                "class A {};\n"
2178                "void f() { f(); }\n"
2179                "}");
2180   verifyFormat("inline namespace X {\n"
2181                "class A {};\n"
2182                "void f() { f(); }\n"
2183                "}");
2184   verifyFormat("using namespace some_namespace;\n"
2185                "class A {};\n"
2186                "void f() { f(); }");
2187 
2188   // This code is more common than we thought; if we
2189   // layout this correctly the semicolon will go into
2190   // its own line, which is undesirable.
2191   verifyFormat("namespace {};");
2192   verifyFormat("namespace {\n"
2193                "class A {};\n"
2194                "};");
2195 
2196   verifyFormat("namespace {\n"
2197                "int SomeVariable = 0; // comment\n"
2198                "} // namespace");
2199   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2200             "#define HEADER_GUARD\n"
2201             "namespace my_namespace {\n"
2202             "int i;\n"
2203             "} // my_namespace\n"
2204             "#endif // HEADER_GUARD",
2205             format("#ifndef HEADER_GUARD\n"
2206                    " #define HEADER_GUARD\n"
2207                    "   namespace my_namespace {\n"
2208                    "int i;\n"
2209                    "}    // my_namespace\n"
2210                    "#endif    // HEADER_GUARD"));
2211 
2212   EXPECT_EQ("namespace A::B {\n"
2213             "class C {};\n"
2214             "}",
2215             format("namespace A::B {\n"
2216                    "class C {};\n"
2217                    "}"));
2218 
2219   FormatStyle Style = getLLVMStyle();
2220   Style.NamespaceIndentation = FormatStyle::NI_All;
2221   EXPECT_EQ("namespace out {\n"
2222             "  int i;\n"
2223             "  namespace in {\n"
2224             "    int i;\n"
2225             "  } // namespace\n"
2226             "} // namespace",
2227             format("namespace out {\n"
2228                    "int i;\n"
2229                    "namespace in {\n"
2230                    "int i;\n"
2231                    "} // namespace\n"
2232                    "} // namespace",
2233                    Style));
2234 
2235   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2236   EXPECT_EQ("namespace out {\n"
2237             "int i;\n"
2238             "namespace in {\n"
2239             "  int i;\n"
2240             "} // namespace\n"
2241             "} // namespace",
2242             format("namespace out {\n"
2243                    "int i;\n"
2244                    "namespace in {\n"
2245                    "int i;\n"
2246                    "} // namespace\n"
2247                    "} // namespace",
2248                    Style));
2249 }
2250 
2251 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2252 
2253 TEST_F(FormatTest, FormatsInlineASM) {
2254   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2255   verifyFormat("asm(\"nop\" ::: \"memory\");");
2256   verifyFormat(
2257       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2258       "    \"cpuid\\n\\t\"\n"
2259       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2260       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2261       "    : \"a\"(value));");
2262   EXPECT_EQ(
2263       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2264       "  __asm {\n"
2265       "        mov     edx,[that] // vtable in edx\n"
2266       "        mov     eax,methodIndex\n"
2267       "        call    [edx][eax*4] // stdcall\n"
2268       "  }\n"
2269       "}",
2270       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2271              "    __asm {\n"
2272              "        mov     edx,[that] // vtable in edx\n"
2273              "        mov     eax,methodIndex\n"
2274              "        call    [edx][eax*4] // stdcall\n"
2275              "    }\n"
2276              "}"));
2277   EXPECT_EQ("_asm {\n"
2278             "  xor eax, eax;\n"
2279             "  cpuid;\n"
2280             "}",
2281             format("_asm {\n"
2282                    "  xor eax, eax;\n"
2283                    "  cpuid;\n"
2284                    "}"));
2285   verifyFormat("void function() {\n"
2286                "  // comment\n"
2287                "  asm(\"\");\n"
2288                "}");
2289   EXPECT_EQ("__asm {\n"
2290             "}\n"
2291             "int i;",
2292             format("__asm   {\n"
2293                    "}\n"
2294                    "int   i;"));
2295 }
2296 
2297 TEST_F(FormatTest, FormatTryCatch) {
2298   verifyFormat("try {\n"
2299                "  throw a * b;\n"
2300                "} catch (int a) {\n"
2301                "  // Do nothing.\n"
2302                "} catch (...) {\n"
2303                "  exit(42);\n"
2304                "}");
2305 
2306   // Function-level try statements.
2307   verifyFormat("int f() try { return 4; } catch (...) {\n"
2308                "  return 5;\n"
2309                "}");
2310   verifyFormat("class A {\n"
2311                "  int a;\n"
2312                "  A() try : a(0) {\n"
2313                "  } catch (...) {\n"
2314                "    throw;\n"
2315                "  }\n"
2316                "};\n");
2317 
2318   // Incomplete try-catch blocks.
2319   verifyIncompleteFormat("try {} catch (");
2320 }
2321 
2322 TEST_F(FormatTest, FormatSEHTryCatch) {
2323   verifyFormat("__try {\n"
2324                "  int a = b * c;\n"
2325                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2326                "  // Do nothing.\n"
2327                "}");
2328 
2329   verifyFormat("__try {\n"
2330                "  int a = b * c;\n"
2331                "} __finally {\n"
2332                "  // Do nothing.\n"
2333                "}");
2334 
2335   verifyFormat("DEBUG({\n"
2336                "  __try {\n"
2337                "  } __finally {\n"
2338                "  }\n"
2339                "});\n");
2340 }
2341 
2342 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2343   verifyFormat("try {\n"
2344                "  f();\n"
2345                "} catch {\n"
2346                "  g();\n"
2347                "}");
2348   verifyFormat("try {\n"
2349                "  f();\n"
2350                "} catch (A a) MACRO(x) {\n"
2351                "  g();\n"
2352                "} catch (B b) MACRO(x) {\n"
2353                "  g();\n"
2354                "}");
2355 }
2356 
2357 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2358   FormatStyle Style = getLLVMStyle();
2359   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2360                           FormatStyle::BS_WebKit}) {
2361     Style.BreakBeforeBraces = BraceStyle;
2362     verifyFormat("try {\n"
2363                  "  // something\n"
2364                  "} catch (...) {\n"
2365                  "  // something\n"
2366                  "}",
2367                  Style);
2368   }
2369   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2370   verifyFormat("try {\n"
2371                "  // something\n"
2372                "}\n"
2373                "catch (...) {\n"
2374                "  // something\n"
2375                "}",
2376                Style);
2377   verifyFormat("__try {\n"
2378                "  // something\n"
2379                "}\n"
2380                "__finally {\n"
2381                "  // something\n"
2382                "}",
2383                Style);
2384   verifyFormat("@try {\n"
2385                "  // something\n"
2386                "}\n"
2387                "@finally {\n"
2388                "  // something\n"
2389                "}",
2390                Style);
2391   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2392   verifyFormat("try\n"
2393                "{\n"
2394                "  // something\n"
2395                "}\n"
2396                "catch (...)\n"
2397                "{\n"
2398                "  // something\n"
2399                "}",
2400                Style);
2401   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2402   verifyFormat("try\n"
2403                "  {\n"
2404                "    // something\n"
2405                "  }\n"
2406                "catch (...)\n"
2407                "  {\n"
2408                "    // something\n"
2409                "  }",
2410                Style);
2411   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2412   Style.BraceWrapping.BeforeCatch = true;
2413   verifyFormat("try {\n"
2414                "  // something\n"
2415                "}\n"
2416                "catch (...) {\n"
2417                "  // something\n"
2418                "}",
2419                Style);
2420 }
2421 
2422 TEST_F(FormatTest, FormatObjCTryCatch) {
2423   verifyFormat("@try {\n"
2424                "  f();\n"
2425                "} @catch (NSException e) {\n"
2426                "  @throw;\n"
2427                "} @finally {\n"
2428                "  exit(42);\n"
2429                "}");
2430   verifyFormat("DEBUG({\n"
2431                "  @try {\n"
2432                "  } @finally {\n"
2433                "  }\n"
2434                "});\n");
2435 }
2436 
2437 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2438   FormatStyle Style = getLLVMStyle();
2439   verifyFormat("@autoreleasepool {\n"
2440                "  f();\n"
2441                "}\n"
2442                "@autoreleasepool {\n"
2443                "  f();\n"
2444                "}\n",
2445                Style);
2446   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2447   verifyFormat("@autoreleasepool\n"
2448                "{\n"
2449                "  f();\n"
2450                "}\n"
2451                "@autoreleasepool\n"
2452                "{\n"
2453                "  f();\n"
2454                "}\n",
2455                Style);
2456 }
2457 
2458 TEST_F(FormatTest, StaticInitializers) {
2459   verifyFormat("static SomeClass SC = {1, 'a'};");
2460 
2461   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2462                "    100000000, "
2463                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2464 
2465   // Here, everything other than the "}" would fit on a line.
2466   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2467                "    10000000000000000000000000};");
2468   EXPECT_EQ("S s = {a,\n"
2469             "\n"
2470             "       b};",
2471             format("S s = {\n"
2472                    "  a,\n"
2473                    "\n"
2474                    "  b\n"
2475                    "};"));
2476 
2477   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2478   // line. However, the formatting looks a bit off and this probably doesn't
2479   // happen often in practice.
2480   verifyFormat("static int Variable[1] = {\n"
2481                "    {1000000000000000000000000000000000000}};",
2482                getLLVMStyleWithColumns(40));
2483 }
2484 
2485 TEST_F(FormatTest, DesignatedInitializers) {
2486   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2487   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2488                "                    .bbbbbbbbbb = 2,\n"
2489                "                    .cccccccccc = 3,\n"
2490                "                    .dddddddddd = 4,\n"
2491                "                    .eeeeeeeeee = 5};");
2492   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2493                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2494                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2495                "    .ccccccccccccccccccccccccccc = 3,\n"
2496                "    .ddddddddddddddddddddddddddd = 4,\n"
2497                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2498 
2499   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2500 }
2501 
2502 TEST_F(FormatTest, NestedStaticInitializers) {
2503   verifyFormat("static A x = {{{}}};\n");
2504   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2505                "               {init1, init2, init3, init4}}};",
2506                getLLVMStyleWithColumns(50));
2507 
2508   verifyFormat("somes Status::global_reps[3] = {\n"
2509                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2510                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2511                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2512                getLLVMStyleWithColumns(60));
2513   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2514                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2515                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2516                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2517   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2518                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2519                "rect.fTop}};");
2520 
2521   verifyFormat(
2522       "SomeArrayOfSomeType a = {\n"
2523       "    {{1, 2, 3},\n"
2524       "     {1, 2, 3},\n"
2525       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2526       "      333333333333333333333333333333},\n"
2527       "     {1, 2, 3},\n"
2528       "     {1, 2, 3}}};");
2529   verifyFormat(
2530       "SomeArrayOfSomeType a = {\n"
2531       "    {{1, 2, 3}},\n"
2532       "    {{1, 2, 3}},\n"
2533       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2534       "      333333333333333333333333333333}},\n"
2535       "    {{1, 2, 3}},\n"
2536       "    {{1, 2, 3}}};");
2537 
2538   verifyFormat("struct {\n"
2539                "  unsigned bit;\n"
2540                "  const char *const name;\n"
2541                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2542                "                 {kOsWin, \"Windows\"},\n"
2543                "                 {kOsLinux, \"Linux\"},\n"
2544                "                 {kOsCrOS, \"Chrome OS\"}};");
2545   verifyFormat("struct {\n"
2546                "  unsigned bit;\n"
2547                "  const char *const name;\n"
2548                "} kBitsToOs[] = {\n"
2549                "    {kOsMac, \"Mac\"},\n"
2550                "    {kOsWin, \"Windows\"},\n"
2551                "    {kOsLinux, \"Linux\"},\n"
2552                "    {kOsCrOS, \"Chrome OS\"},\n"
2553                "};");
2554 }
2555 
2556 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2557   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2558                "                      \\\n"
2559                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2560 }
2561 
2562 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2563   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2564                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2565 
2566   // Do break defaulted and deleted functions.
2567   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2568                "    default;",
2569                getLLVMStyleWithColumns(40));
2570   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2571                "    delete;",
2572                getLLVMStyleWithColumns(40));
2573 }
2574 
2575 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2576   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2577                getLLVMStyleWithColumns(40));
2578   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2579                getLLVMStyleWithColumns(40));
2580   EXPECT_EQ("#define Q                              \\\n"
2581             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2582             "  \"aaaaaaaa.cpp\"",
2583             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2584                    getLLVMStyleWithColumns(40)));
2585 }
2586 
2587 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2588   EXPECT_EQ("# 123 \"A string literal\"",
2589             format("   #     123    \"A string literal\""));
2590 }
2591 
2592 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2593   EXPECT_EQ("#;", format("#;"));
2594   verifyFormat("#\n;\n;\n;");
2595 }
2596 
2597 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2598   EXPECT_EQ("#line 42 \"test\"\n",
2599             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2600   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2601                                     getLLVMStyleWithColumns(12)));
2602 }
2603 
2604 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2605   EXPECT_EQ("#line 42 \"test\"",
2606             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2607   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2608 }
2609 
2610 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2611   verifyFormat("#define A \\x20");
2612   verifyFormat("#define A \\ x20");
2613   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2614   verifyFormat("#define A ''");
2615   verifyFormat("#define A ''qqq");
2616   verifyFormat("#define A `qqq");
2617   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2618   EXPECT_EQ("const char *c = STRINGIFY(\n"
2619             "\\na : b);",
2620             format("const char * c = STRINGIFY(\n"
2621                    "\\na : b);"));
2622 
2623   verifyFormat("a\r\\");
2624   verifyFormat("a\v\\");
2625   verifyFormat("a\f\\");
2626 }
2627 
2628 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2629   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2630   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2631   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2632   // FIXME: We never break before the macro name.
2633   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2634 
2635   verifyFormat("#define A A\n#define A A");
2636   verifyFormat("#define A(X) A\n#define A A");
2637 
2638   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2639   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2640 }
2641 
2642 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2643   EXPECT_EQ("// somecomment\n"
2644             "#include \"a.h\"\n"
2645             "#define A(  \\\n"
2646             "    A, B)\n"
2647             "#include \"b.h\"\n"
2648             "// somecomment\n",
2649             format("  // somecomment\n"
2650                    "  #include \"a.h\"\n"
2651                    "#define A(A,\\\n"
2652                    "    B)\n"
2653                    "    #include \"b.h\"\n"
2654                    " // somecomment\n",
2655                    getLLVMStyleWithColumns(13)));
2656 }
2657 
2658 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2659 
2660 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2661   EXPECT_EQ("#define A    \\\n"
2662             "  c;         \\\n"
2663             "  e;\n"
2664             "f;",
2665             format("#define A c; e;\n"
2666                    "f;",
2667                    getLLVMStyleWithColumns(14)));
2668 }
2669 
2670 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2671 
2672 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2673   EXPECT_EQ("int x,\n"
2674             "#define A\n"
2675             "    y;",
2676             format("int x,\n#define A\ny;"));
2677 }
2678 
2679 TEST_F(FormatTest, HashInMacroDefinition) {
2680   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2681   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2682   verifyFormat("#define A  \\\n"
2683                "  {        \\\n"
2684                "    f(#c); \\\n"
2685                "  }",
2686                getLLVMStyleWithColumns(11));
2687 
2688   verifyFormat("#define A(X)         \\\n"
2689                "  void function##X()",
2690                getLLVMStyleWithColumns(22));
2691 
2692   verifyFormat("#define A(a, b, c)   \\\n"
2693                "  void a##b##c()",
2694                getLLVMStyleWithColumns(22));
2695 
2696   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2697 }
2698 
2699 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2700   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2701   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2702 }
2703 
2704 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2705   EXPECT_EQ("#define A b;", format("#define A \\\n"
2706                                    "          \\\n"
2707                                    "  b;",
2708                                    getLLVMStyleWithColumns(25)));
2709   EXPECT_EQ("#define A \\\n"
2710             "          \\\n"
2711             "  a;      \\\n"
2712             "  b;",
2713             format("#define A \\\n"
2714                    "          \\\n"
2715                    "  a;      \\\n"
2716                    "  b;",
2717                    getLLVMStyleWithColumns(11)));
2718   EXPECT_EQ("#define A \\\n"
2719             "  a;      \\\n"
2720             "          \\\n"
2721             "  b;",
2722             format("#define A \\\n"
2723                    "  a;      \\\n"
2724                    "          \\\n"
2725                    "  b;",
2726                    getLLVMStyleWithColumns(11)));
2727 }
2728 
2729 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2730   verifyIncompleteFormat("#define A :");
2731   verifyFormat("#define SOMECASES  \\\n"
2732                "  case 1:          \\\n"
2733                "  case 2\n",
2734                getLLVMStyleWithColumns(20));
2735   verifyFormat("#define A template <typename T>");
2736   verifyIncompleteFormat("#define STR(x) #x\n"
2737                          "f(STR(this_is_a_string_literal{));");
2738   verifyFormat("#pragma omp threadprivate( \\\n"
2739                "    y)), // expected-warning",
2740                getLLVMStyleWithColumns(28));
2741   verifyFormat("#d, = };");
2742   verifyFormat("#if \"a");
2743   verifyIncompleteFormat("({\n"
2744                          "#define b     \\\n"
2745                          "  }           \\\n"
2746                          "  a\n"
2747                          "a",
2748                          getLLVMStyleWithColumns(15));
2749   verifyFormat("#define A     \\\n"
2750                "  {           \\\n"
2751                "    {\n"
2752                "#define B     \\\n"
2753                "  }           \\\n"
2754                "  }",
2755                getLLVMStyleWithColumns(15));
2756   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2757   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2758   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2759   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2760 }
2761 
2762 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2763   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2764   EXPECT_EQ("class A : public QObject {\n"
2765             "  Q_OBJECT\n"
2766             "\n"
2767             "  A() {}\n"
2768             "};",
2769             format("class A  :  public QObject {\n"
2770                    "     Q_OBJECT\n"
2771                    "\n"
2772                    "  A() {\n}\n"
2773                    "}  ;"));
2774   EXPECT_EQ("MACRO\n"
2775             "/*static*/ int i;",
2776             format("MACRO\n"
2777                    " /*static*/ int   i;"));
2778   EXPECT_EQ("SOME_MACRO\n"
2779             "namespace {\n"
2780             "void f();\n"
2781             "}",
2782             format("SOME_MACRO\n"
2783                    "  namespace    {\n"
2784                    "void   f(  );\n"
2785                    "}"));
2786   // Only if the identifier contains at least 5 characters.
2787   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2788   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2789   // Only if everything is upper case.
2790   EXPECT_EQ("class A : public QObject {\n"
2791             "  Q_Object A() {}\n"
2792             "};",
2793             format("class A  :  public QObject {\n"
2794                    "     Q_Object\n"
2795                    "  A() {\n}\n"
2796                    "}  ;"));
2797 
2798   // Only if the next line can actually start an unwrapped line.
2799   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2800             format("SOME_WEIRD_LOG_MACRO\n"
2801                    "<< SomeThing;"));
2802 
2803   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2804                "(n, buffers))\n",
2805                getChromiumStyle(FormatStyle::LK_Cpp));
2806 }
2807 
2808 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2809   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2810             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2811             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2812             "class X {};\n"
2813             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2814             "int *createScopDetectionPass() { return 0; }",
2815             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2816                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2817                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2818                    "  class X {};\n"
2819                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2820                    "  int *createScopDetectionPass() { return 0; }"));
2821   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2822   // braces, so that inner block is indented one level more.
2823   EXPECT_EQ("int q() {\n"
2824             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2825             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2826             "  IPC_END_MESSAGE_MAP()\n"
2827             "}",
2828             format("int q() {\n"
2829                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2830                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2831                    "  IPC_END_MESSAGE_MAP()\n"
2832                    "}"));
2833 
2834   // Same inside macros.
2835   EXPECT_EQ("#define LIST(L) \\\n"
2836             "  L(A)          \\\n"
2837             "  L(B)          \\\n"
2838             "  L(C)",
2839             format("#define LIST(L) \\\n"
2840                    "  L(A) \\\n"
2841                    "  L(B) \\\n"
2842                    "  L(C)",
2843                    getGoogleStyle()));
2844 
2845   // These must not be recognized as macros.
2846   EXPECT_EQ("int q() {\n"
2847             "  f(x);\n"
2848             "  f(x) {}\n"
2849             "  f(x)->g();\n"
2850             "  f(x)->*g();\n"
2851             "  f(x).g();\n"
2852             "  f(x) = x;\n"
2853             "  f(x) += x;\n"
2854             "  f(x) -= x;\n"
2855             "  f(x) *= x;\n"
2856             "  f(x) /= x;\n"
2857             "  f(x) %= x;\n"
2858             "  f(x) &= x;\n"
2859             "  f(x) |= x;\n"
2860             "  f(x) ^= x;\n"
2861             "  f(x) >>= x;\n"
2862             "  f(x) <<= x;\n"
2863             "  f(x)[y].z();\n"
2864             "  LOG(INFO) << x;\n"
2865             "  ifstream(x) >> x;\n"
2866             "}\n",
2867             format("int q() {\n"
2868                    "  f(x)\n;\n"
2869                    "  f(x)\n {}\n"
2870                    "  f(x)\n->g();\n"
2871                    "  f(x)\n->*g();\n"
2872                    "  f(x)\n.g();\n"
2873                    "  f(x)\n = x;\n"
2874                    "  f(x)\n += x;\n"
2875                    "  f(x)\n -= x;\n"
2876                    "  f(x)\n *= x;\n"
2877                    "  f(x)\n /= x;\n"
2878                    "  f(x)\n %= x;\n"
2879                    "  f(x)\n &= x;\n"
2880                    "  f(x)\n |= x;\n"
2881                    "  f(x)\n ^= x;\n"
2882                    "  f(x)\n >>= x;\n"
2883                    "  f(x)\n <<= x;\n"
2884                    "  f(x)\n[y].z();\n"
2885                    "  LOG(INFO)\n << x;\n"
2886                    "  ifstream(x)\n >> x;\n"
2887                    "}\n"));
2888   EXPECT_EQ("int q() {\n"
2889             "  F(x)\n"
2890             "  if (1) {\n"
2891             "  }\n"
2892             "  F(x)\n"
2893             "  while (1) {\n"
2894             "  }\n"
2895             "  F(x)\n"
2896             "  G(x);\n"
2897             "  F(x)\n"
2898             "  try {\n"
2899             "    Q();\n"
2900             "  } catch (...) {\n"
2901             "  }\n"
2902             "}\n",
2903             format("int q() {\n"
2904                    "F(x)\n"
2905                    "if (1) {}\n"
2906                    "F(x)\n"
2907                    "while (1) {}\n"
2908                    "F(x)\n"
2909                    "G(x);\n"
2910                    "F(x)\n"
2911                    "try { Q(); } catch (...) {}\n"
2912                    "}\n"));
2913   EXPECT_EQ("class A {\n"
2914             "  A() : t(0) {}\n"
2915             "  A(int i) noexcept() : {}\n"
2916             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2917             "  try : t(0) {\n"
2918             "  } catch (...) {\n"
2919             "  }\n"
2920             "};",
2921             format("class A {\n"
2922                    "  A()\n : t(0) {}\n"
2923                    "  A(int i)\n noexcept() : {}\n"
2924                    "  A(X x)\n"
2925                    "  try : t(0) {} catch (...) {}\n"
2926                    "};"));
2927   EXPECT_EQ("class SomeClass {\n"
2928             "public:\n"
2929             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2930             "};",
2931             format("class SomeClass {\n"
2932                    "public:\n"
2933                    "  SomeClass()\n"
2934                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2935                    "};"));
2936   EXPECT_EQ("class SomeClass {\n"
2937             "public:\n"
2938             "  SomeClass()\n"
2939             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2940             "};",
2941             format("class SomeClass {\n"
2942                    "public:\n"
2943                    "  SomeClass()\n"
2944                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2945                    "};",
2946                    getLLVMStyleWithColumns(40)));
2947 
2948   verifyFormat("MACRO(>)");
2949 }
2950 
2951 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2952   verifyFormat("#define A \\\n"
2953                "  f({     \\\n"
2954                "    g();  \\\n"
2955                "  });",
2956                getLLVMStyleWithColumns(11));
2957 }
2958 
2959 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2960   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2961 }
2962 
2963 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2964   verifyFormat("{\n  { a #c; }\n}");
2965 }
2966 
2967 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2968   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2969             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2970   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2971             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2972 }
2973 
2974 TEST_F(FormatTest, EscapedNewlines) {
2975   EXPECT_EQ(
2976       "#define A \\\n  int i;  \\\n  int j;",
2977       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2978   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2979   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2980   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2981   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2982 }
2983 
2984 TEST_F(FormatTest, DontCrashOnBlockComments) {
2985   EXPECT_EQ(
2986       "int xxxxxxxxx; /* "
2987       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2988       "zzzzzz\n"
2989       "0*/",
2990       format("int xxxxxxxxx;                          /* "
2991              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2992              "0*/"));
2993 }
2994 
2995 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2996   verifyFormat("#define A \\\n"
2997                "  int v(  \\\n"
2998                "      a); \\\n"
2999                "  int i;",
3000                getLLVMStyleWithColumns(11));
3001 }
3002 
3003 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3004   EXPECT_EQ(
3005       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3006       "                      \\\n"
3007       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3008       "\n"
3009       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3010       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3011       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3012              "\\\n"
3013              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3014              "  \n"
3015              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3016              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3017 }
3018 
3019 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3020   EXPECT_EQ("int\n"
3021             "#define A\n"
3022             "    a;",
3023             format("int\n#define A\na;"));
3024   verifyFormat("functionCallTo(\n"
3025                "    someOtherFunction(\n"
3026                "        withSomeParameters, whichInSequence,\n"
3027                "        areLongerThanALine(andAnotherCall,\n"
3028                "#define A B\n"
3029                "                           withMoreParamters,\n"
3030                "                           whichStronglyInfluenceTheLayout),\n"
3031                "        andMoreParameters),\n"
3032                "    trailing);",
3033                getLLVMStyleWithColumns(69));
3034   verifyFormat("Foo::Foo()\n"
3035                "#ifdef BAR\n"
3036                "    : baz(0)\n"
3037                "#endif\n"
3038                "{\n"
3039                "}");
3040   verifyFormat("void f() {\n"
3041                "  if (true)\n"
3042                "#ifdef A\n"
3043                "    f(42);\n"
3044                "  x();\n"
3045                "#else\n"
3046                "    g();\n"
3047                "  x();\n"
3048                "#endif\n"
3049                "}");
3050   verifyFormat("void f(param1, param2,\n"
3051                "       param3,\n"
3052                "#ifdef A\n"
3053                "       param4(param5,\n"
3054                "#ifdef A1\n"
3055                "              param6,\n"
3056                "#ifdef A2\n"
3057                "              param7),\n"
3058                "#else\n"
3059                "              param8),\n"
3060                "       param9,\n"
3061                "#endif\n"
3062                "       param10,\n"
3063                "#endif\n"
3064                "       param11)\n"
3065                "#else\n"
3066                "       param12)\n"
3067                "#endif\n"
3068                "{\n"
3069                "  x();\n"
3070                "}",
3071                getLLVMStyleWithColumns(28));
3072   verifyFormat("#if 1\n"
3073                "int i;");
3074   verifyFormat("#if 1\n"
3075                "#endif\n"
3076                "#if 1\n"
3077                "#else\n"
3078                "#endif\n");
3079   verifyFormat("DEBUG({\n"
3080                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3081                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3082                "});\n"
3083                "#if a\n"
3084                "#else\n"
3085                "#endif");
3086 
3087   verifyIncompleteFormat("void f(\n"
3088                          "#if A\n"
3089                          "    );\n"
3090                          "#else\n"
3091                          "#endif");
3092 }
3093 
3094 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3095   verifyFormat("#endif\n"
3096                "#if B");
3097 }
3098 
3099 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3100   FormatStyle SingleLine = getLLVMStyle();
3101   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3102   verifyFormat("#if 0\n"
3103                "#elif 1\n"
3104                "#endif\n"
3105                "void foo() {\n"
3106                "  if (test) foo2();\n"
3107                "}",
3108                SingleLine);
3109 }
3110 
3111 TEST_F(FormatTest, LayoutBlockInsideParens) {
3112   verifyFormat("functionCall({ int i; });");
3113   verifyFormat("functionCall({\n"
3114                "  int i;\n"
3115                "  int j;\n"
3116                "});");
3117   verifyFormat("functionCall(\n"
3118                "    {\n"
3119                "      int i;\n"
3120                "      int j;\n"
3121                "    },\n"
3122                "    aaaa, bbbb, cccc);");
3123   verifyFormat("functionA(functionB({\n"
3124                "            int i;\n"
3125                "            int j;\n"
3126                "          }),\n"
3127                "          aaaa, bbbb, cccc);");
3128   verifyFormat("functionCall(\n"
3129                "    {\n"
3130                "      int i;\n"
3131                "      int j;\n"
3132                "    },\n"
3133                "    aaaa, bbbb, // comment\n"
3134                "    cccc);");
3135   verifyFormat("functionA(functionB({\n"
3136                "            int i;\n"
3137                "            int j;\n"
3138                "          }),\n"
3139                "          aaaa, bbbb, // comment\n"
3140                "          cccc);");
3141   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3142   verifyFormat("functionCall(aaaa, bbbb, {\n"
3143                "  int i;\n"
3144                "  int j;\n"
3145                "});");
3146   verifyFormat(
3147       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3148       "    {\n"
3149       "      int i; // break\n"
3150       "    },\n"
3151       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3152       "                                     ccccccccccccccccc));");
3153   verifyFormat("DEBUG({\n"
3154                "  if (a)\n"
3155                "    f();\n"
3156                "});");
3157 }
3158 
3159 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3160   EXPECT_EQ("SOME_MACRO { int i; }\n"
3161             "int i;",
3162             format("  SOME_MACRO  {int i;}  int i;"));
3163 }
3164 
3165 TEST_F(FormatTest, LayoutNestedBlocks) {
3166   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3167                "  struct s {\n"
3168                "    int i;\n"
3169                "  };\n"
3170                "  s kBitsToOs[] = {{10}};\n"
3171                "  for (int i = 0; i < 10; ++i)\n"
3172                "    return;\n"
3173                "}");
3174   verifyFormat("call(parameter, {\n"
3175                "  something();\n"
3176                "  // Comment using all columns.\n"
3177                "  somethingelse();\n"
3178                "});",
3179                getLLVMStyleWithColumns(40));
3180   verifyFormat("DEBUG( //\n"
3181                "    { f(); }, a);");
3182   verifyFormat("DEBUG( //\n"
3183                "    {\n"
3184                "      f(); //\n"
3185                "    },\n"
3186                "    a);");
3187 
3188   EXPECT_EQ("call(parameter, {\n"
3189             "  something();\n"
3190             "  // Comment too\n"
3191             "  // looooooooooong.\n"
3192             "  somethingElse();\n"
3193             "});",
3194             format("call(parameter, {\n"
3195                    "  something();\n"
3196                    "  // Comment too looooooooooong.\n"
3197                    "  somethingElse();\n"
3198                    "});",
3199                    getLLVMStyleWithColumns(29)));
3200   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3201   EXPECT_EQ("DEBUG({ // comment\n"
3202             "  int i;\n"
3203             "});",
3204             format("DEBUG({ // comment\n"
3205                    "int  i;\n"
3206                    "});"));
3207   EXPECT_EQ("DEBUG({\n"
3208             "  int i;\n"
3209             "\n"
3210             "  // comment\n"
3211             "  int j;\n"
3212             "});",
3213             format("DEBUG({\n"
3214                    "  int  i;\n"
3215                    "\n"
3216                    "  // comment\n"
3217                    "  int  j;\n"
3218                    "});"));
3219 
3220   verifyFormat("DEBUG({\n"
3221                "  if (a)\n"
3222                "    return;\n"
3223                "});");
3224   verifyGoogleFormat("DEBUG({\n"
3225                      "  if (a) return;\n"
3226                      "});");
3227   FormatStyle Style = getGoogleStyle();
3228   Style.ColumnLimit = 45;
3229   verifyFormat("Debug(aaaaa,\n"
3230                "      {\n"
3231                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3232                "      },\n"
3233                "      a);",
3234                Style);
3235 
3236   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3237 
3238   verifyNoCrash("^{v^{a}}");
3239 }
3240 
3241 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3242   EXPECT_EQ("#define MACRO()                     \\\n"
3243             "  Debug(aaa, /* force line break */ \\\n"
3244             "        {                           \\\n"
3245             "          int i;                    \\\n"
3246             "          int j;                    \\\n"
3247             "        })",
3248             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3249                    "          {  int   i;  int  j;   })",
3250                    getGoogleStyle()));
3251 
3252   EXPECT_EQ("#define A                                       \\\n"
3253             "  [] {                                          \\\n"
3254             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3255             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3256             "  }",
3257             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3258                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3259                    getGoogleStyle()));
3260 }
3261 
3262 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3263   EXPECT_EQ("{}", format("{}"));
3264   verifyFormat("enum E {};");
3265   verifyFormat("enum E {}");
3266 }
3267 
3268 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3269   FormatStyle Style = getLLVMStyle();
3270   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3271   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3272   verifyFormat("FOO_BEGIN\n"
3273                "  FOO_ENTRY\n"
3274                "FOO_END", Style);
3275   verifyFormat("FOO_BEGIN\n"
3276                "  NESTED_FOO_BEGIN\n"
3277                "    NESTED_FOO_ENTRY\n"
3278                "  NESTED_FOO_END\n"
3279                "FOO_END", Style);
3280   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3281                "  int x;\n"
3282                "  x = 1;\n"
3283                "FOO_END(Baz)", Style);
3284 }
3285 
3286 //===----------------------------------------------------------------------===//
3287 // Line break tests.
3288 //===----------------------------------------------------------------------===//
3289 
3290 TEST_F(FormatTest, PreventConfusingIndents) {
3291   verifyFormat(
3292       "void f() {\n"
3293       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3294       "                         parameter, parameter, parameter)),\n"
3295       "                     SecondLongCall(parameter));\n"
3296       "}");
3297   verifyFormat(
3298       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3299       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3300       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3301       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3302   verifyFormat(
3303       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3304       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3305       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3306       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3307   verifyFormat(
3308       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3309       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3310       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3311       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3312   verifyFormat("int a = bbbb && ccc && fffff(\n"
3313                "#define A Just forcing a new line\n"
3314                "                           ddd);");
3315 }
3316 
3317 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3318   verifyFormat(
3319       "bool aaaaaaa =\n"
3320       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3321       "    bbbbbbbb();");
3322   verifyFormat(
3323       "bool aaaaaaa =\n"
3324       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3325       "    bbbbbbbb();");
3326 
3327   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3328                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3329                "    ccccccccc == ddddddddddd;");
3330   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3331                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3332                "    ccccccccc == ddddddddddd;");
3333   verifyFormat(
3334       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3335       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3336       "    ccccccccc == ddddddddddd;");
3337 
3338   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3339                "                 aaaaaa) &&\n"
3340                "         bbbbbb && cccccc;");
3341   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3342                "                 aaaaaa) >>\n"
3343                "         bbbbbb;");
3344   verifyFormat("Whitespaces.addUntouchableComment(\n"
3345                "    SourceMgr.getSpellingColumnNumber(\n"
3346                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3347                "    1);");
3348 
3349   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3350                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3351                "    cccccc) {\n}");
3352   verifyFormat("b = a &&\n"
3353                "    // Comment\n"
3354                "    b.c && d;");
3355 
3356   // If the LHS of a comparison is not a binary expression itself, the
3357   // additional linebreak confuses many people.
3358   verifyFormat(
3359       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3360       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3361       "}");
3362   verifyFormat(
3363       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3364       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3365       "}");
3366   verifyFormat(
3367       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3368       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3369       "}");
3370   // Even explicit parentheses stress the precedence enough to make the
3371   // additional break unnecessary.
3372   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3373                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3374                "}");
3375   // This cases is borderline, but with the indentation it is still readable.
3376   verifyFormat(
3377       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3378       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3379       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3380       "}",
3381       getLLVMStyleWithColumns(75));
3382 
3383   // If the LHS is a binary expression, we should still use the additional break
3384   // as otherwise the formatting hides the operator precedence.
3385   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3386                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3387                "    5) {\n"
3388                "}");
3389 
3390   FormatStyle OnePerLine = getLLVMStyle();
3391   OnePerLine.BinPackParameters = false;
3392   verifyFormat(
3393       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3394       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3395       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3396       OnePerLine);
3397 }
3398 
3399 TEST_F(FormatTest, ExpressionIndentation) {
3400   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3401                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3402                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3403                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3404                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3405                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3406                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3407                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3408                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3409   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3410                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3411                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3412                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3413   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3414                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3415                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3416                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3417   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3418                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3419                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3420                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3421   verifyFormat("if () {\n"
3422                "} else if (aaaaa &&\n"
3423                "           bbbbb > // break\n"
3424                "               ccccc) {\n"
3425                "}");
3426 
3427   // Presence of a trailing comment used to change indentation of b.
3428   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3429                "       b;\n"
3430                "return aaaaaaaaaaaaaaaaaaa +\n"
3431                "       b; //",
3432                getLLVMStyleWithColumns(30));
3433 }
3434 
3435 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3436   // Not sure what the best system is here. Like this, the LHS can be found
3437   // immediately above an operator (everything with the same or a higher
3438   // indent). The RHS is aligned right of the operator and so compasses
3439   // everything until something with the same indent as the operator is found.
3440   // FIXME: Is this a good system?
3441   FormatStyle Style = getLLVMStyle();
3442   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3443   verifyFormat(
3444       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3445       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3446       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3447       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3448       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3449       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3450       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3451       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3452       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3453       Style);
3454   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3455                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3456                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3457                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3458                Style);
3459   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3460                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3461                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3462                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3463                Style);
3464   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3465                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3466                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3467                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3468                Style);
3469   verifyFormat("if () {\n"
3470                "} else if (aaaaa\n"
3471                "           && bbbbb // break\n"
3472                "                  > ccccc) {\n"
3473                "}",
3474                Style);
3475   verifyFormat("return (a)\n"
3476                "       // comment\n"
3477                "       + b;",
3478                Style);
3479   verifyFormat(
3480       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3481       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3482       "             + cc;",
3483       Style);
3484 
3485   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3486                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3487                Style);
3488 
3489   // Forced by comments.
3490   verifyFormat(
3491       "unsigned ContentSize =\n"
3492       "    sizeof(int16_t)   // DWARF ARange version number\n"
3493       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3494       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3495       "    + sizeof(int8_t); // Segment Size (in bytes)");
3496 
3497   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3498                "       == boost::fusion::at_c<1>(iiii).second;",
3499                Style);
3500 
3501   Style.ColumnLimit = 60;
3502   verifyFormat("zzzzzzzzzz\n"
3503                "    = bbbbbbbbbbbbbbbbb\n"
3504                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3505                Style);
3506 }
3507 
3508 TEST_F(FormatTest, NoOperandAlignment) {
3509   FormatStyle Style = getLLVMStyle();
3510   Style.AlignOperands = false;
3511   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3512   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3513                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3514                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3515                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3516                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3517                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3518                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3519                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3520                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3521                Style);
3522 
3523   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3524                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3525                "    + cc;",
3526                Style);
3527   verifyFormat("int a = aa\n"
3528                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3529                "        * cccccccccccccccccccccccccccccccccccc;",
3530                Style);
3531 
3532   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3533   verifyFormat("return (a > b\n"
3534                "    // comment1\n"
3535                "    // comment2\n"
3536                "    || c);",
3537                Style);
3538 }
3539 
3540 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3541   FormatStyle Style = getLLVMStyle();
3542   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3543   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3544                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3545                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3546                Style);
3547 }
3548 
3549 TEST_F(FormatTest, ConstructorInitializers) {
3550   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3551   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3552                getLLVMStyleWithColumns(45));
3553   verifyFormat("Constructor()\n"
3554                "    : Inttializer(FitsOnTheLine) {}",
3555                getLLVMStyleWithColumns(44));
3556   verifyFormat("Constructor()\n"
3557                "    : Inttializer(FitsOnTheLine) {}",
3558                getLLVMStyleWithColumns(43));
3559 
3560   verifyFormat("template <typename T>\n"
3561                "Constructor() : Initializer(FitsOnTheLine) {}",
3562                getLLVMStyleWithColumns(45));
3563 
3564   verifyFormat(
3565       "SomeClass::Constructor()\n"
3566       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3567 
3568   verifyFormat(
3569       "SomeClass::Constructor()\n"
3570       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3571       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3572   verifyFormat(
3573       "SomeClass::Constructor()\n"
3574       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3575       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3576   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3577                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3578                "    : aaaaaaaaaa(aaaaaa) {}");
3579 
3580   verifyFormat("Constructor()\n"
3581                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3582                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3583                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3584                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3585 
3586   verifyFormat("Constructor()\n"
3587                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3588                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3589 
3590   verifyFormat("Constructor(int Parameter = 0)\n"
3591                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3592                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3593   verifyFormat("Constructor()\n"
3594                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3595                "}",
3596                getLLVMStyleWithColumns(60));
3597   verifyFormat("Constructor()\n"
3598                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3599                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3600 
3601   // Here a line could be saved by splitting the second initializer onto two
3602   // lines, but that is not desirable.
3603   verifyFormat("Constructor()\n"
3604                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3605                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3606                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3607 
3608   FormatStyle OnePerLine = getLLVMStyle();
3609   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3610   verifyFormat("SomeClass::Constructor()\n"
3611                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3612                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3613                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3614                OnePerLine);
3615   verifyFormat("SomeClass::Constructor()\n"
3616                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3617                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3618                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3619                OnePerLine);
3620   verifyFormat("MyClass::MyClass(int var)\n"
3621                "    : some_var_(var),            // 4 space indent\n"
3622                "      some_other_var_(var + 1) { // lined up\n"
3623                "}",
3624                OnePerLine);
3625   verifyFormat("Constructor()\n"
3626                "    : aaaaa(aaaaaa),\n"
3627                "      aaaaa(aaaaaa),\n"
3628                "      aaaaa(aaaaaa),\n"
3629                "      aaaaa(aaaaaa),\n"
3630                "      aaaaa(aaaaaa) {}",
3631                OnePerLine);
3632   verifyFormat("Constructor()\n"
3633                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3634                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3635                OnePerLine);
3636   OnePerLine.ColumnLimit = 60;
3637   verifyFormat("Constructor()\n"
3638                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3639                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3640                OnePerLine);
3641 
3642   EXPECT_EQ("Constructor()\n"
3643             "    : // Comment forcing unwanted break.\n"
3644             "      aaaa(aaaa) {}",
3645             format("Constructor() :\n"
3646                    "    // Comment forcing unwanted break.\n"
3647                    "    aaaa(aaaa) {}"));
3648 }
3649 
3650 TEST_F(FormatTest, MemoizationTests) {
3651   // This breaks if the memoization lookup does not take \c Indent and
3652   // \c LastSpace into account.
3653   verifyFormat(
3654       "extern CFRunLoopTimerRef\n"
3655       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3656       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3657       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3658       "                     CFRunLoopTimerContext *context) {}");
3659 
3660   // Deep nesting somewhat works around our memoization.
3661   verifyFormat(
3662       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3663       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3664       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3665       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3666       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3667       getLLVMStyleWithColumns(65));
3668   verifyFormat(
3669       "aaaaa(\n"
3670       "    aaaaa,\n"
3671       "    aaaaa(\n"
3672       "        aaaaa,\n"
3673       "        aaaaa(\n"
3674       "            aaaaa,\n"
3675       "            aaaaa(\n"
3676       "                aaaaa,\n"
3677       "                aaaaa(\n"
3678       "                    aaaaa,\n"
3679       "                    aaaaa(\n"
3680       "                        aaaaa,\n"
3681       "                        aaaaa(\n"
3682       "                            aaaaa,\n"
3683       "                            aaaaa(\n"
3684       "                                aaaaa,\n"
3685       "                                aaaaa(\n"
3686       "                                    aaaaa,\n"
3687       "                                    aaaaa(\n"
3688       "                                        aaaaa,\n"
3689       "                                        aaaaa(\n"
3690       "                                            aaaaa,\n"
3691       "                                            aaaaa(\n"
3692       "                                                aaaaa,\n"
3693       "                                                aaaaa))))))))))));",
3694       getLLVMStyleWithColumns(65));
3695   verifyFormat(
3696       "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"
3697       "                                  a),\n"
3698       "                                a),\n"
3699       "                              a),\n"
3700       "                            a),\n"
3701       "                          a),\n"
3702       "                        a),\n"
3703       "                      a),\n"
3704       "                    a),\n"
3705       "                  a),\n"
3706       "                a),\n"
3707       "              a),\n"
3708       "            a),\n"
3709       "          a),\n"
3710       "        a),\n"
3711       "      a),\n"
3712       "    a),\n"
3713       "  a)",
3714       getLLVMStyleWithColumns(65));
3715 
3716   // This test takes VERY long when memoization is broken.
3717   FormatStyle OnePerLine = getLLVMStyle();
3718   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3719   OnePerLine.BinPackParameters = false;
3720   std::string input = "Constructor()\n"
3721                       "    : aaaa(a,\n";
3722   for (unsigned i = 0, e = 80; i != e; ++i) {
3723     input += "           a,\n";
3724   }
3725   input += "           a) {}";
3726   verifyFormat(input, OnePerLine);
3727 }
3728 
3729 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3730   verifyFormat(
3731       "void f() {\n"
3732       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3733       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3734       "    f();\n"
3735       "}");
3736   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3737                "    Intervals[i - 1].getRange().getLast()) {\n}");
3738 }
3739 
3740 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3741   // Principially, we break function declarations in a certain order:
3742   // 1) break amongst arguments.
3743   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3744                "                              Cccccccccccccc cccccccccccccc);");
3745   verifyFormat("template <class TemplateIt>\n"
3746                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3747                "                            TemplateIt *stop) {}");
3748 
3749   // 2) break after return type.
3750   verifyFormat(
3751       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3752       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3753       getGoogleStyle());
3754 
3755   // 3) break after (.
3756   verifyFormat(
3757       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3758       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3759       getGoogleStyle());
3760 
3761   // 4) break before after nested name specifiers.
3762   verifyFormat(
3763       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3764       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3765       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3766       getGoogleStyle());
3767 
3768   // However, there are exceptions, if a sufficient amount of lines can be
3769   // saved.
3770   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3771   // more adjusting.
3772   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3773                "                                  Cccccccccccccc cccccccccc,\n"
3774                "                                  Cccccccccccccc cccccccccc,\n"
3775                "                                  Cccccccccccccc cccccccccc,\n"
3776                "                                  Cccccccccccccc cccccccccc);");
3777   verifyFormat(
3778       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3779       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3780       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3781       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3782       getGoogleStyle());
3783   verifyFormat(
3784       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3785       "                                          Cccccccccccccc cccccccccc,\n"
3786       "                                          Cccccccccccccc cccccccccc,\n"
3787       "                                          Cccccccccccccc cccccccccc,\n"
3788       "                                          Cccccccccccccc cccccccccc,\n"
3789       "                                          Cccccccccccccc cccccccccc,\n"
3790       "                                          Cccccccccccccc cccccccccc);");
3791   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3792                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3793                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3794                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3795                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3796 
3797   // Break after multi-line parameters.
3798   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3799                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3800                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3801                "    bbbb bbbb);");
3802   verifyFormat("void SomeLoooooooooooongFunction(\n"
3803                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3804                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3805                "    int bbbbbbbbbbbbb);");
3806 
3807   // Treat overloaded operators like other functions.
3808   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3809                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3810   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3811                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3812   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3813                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3814   verifyGoogleFormat(
3815       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3816       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3817   verifyGoogleFormat(
3818       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3819       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3820   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3821                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3822   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3823                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3824   verifyGoogleFormat(
3825       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3826       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3827       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3828 
3829   FormatStyle Style = getLLVMStyle();
3830   Style.PointerAlignment = FormatStyle::PAS_Left;
3831   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3832                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3833                Style);
3834   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3835                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3836                Style);
3837 }
3838 
3839 TEST_F(FormatTest, TrailingReturnType) {
3840   verifyFormat("auto foo() -> int;\n");
3841   verifyFormat("struct S {\n"
3842                "  auto bar() const -> int;\n"
3843                "};");
3844   verifyFormat("template <size_t Order, typename T>\n"
3845                "auto load_img(const std::string &filename)\n"
3846                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3847   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3848                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3849   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3850   verifyFormat("template <typename T>\n"
3851                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3852                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3853 
3854   // Not trailing return types.
3855   verifyFormat("void f() { auto a = b->c(); }");
3856 }
3857 
3858 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3859   // Avoid breaking before trailing 'const' or other trailing annotations, if
3860   // they are not function-like.
3861   FormatStyle Style = getGoogleStyle();
3862   Style.ColumnLimit = 47;
3863   verifyFormat("void someLongFunction(\n"
3864                "    int someLoooooooooooooongParameter) const {\n}",
3865                getLLVMStyleWithColumns(47));
3866   verifyFormat("LoooooongReturnType\n"
3867                "someLoooooooongFunction() const {}",
3868                getLLVMStyleWithColumns(47));
3869   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3870                "    const {}",
3871                Style);
3872   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3873                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3874   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3875                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3876   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3877                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3878   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3879                "                   aaaaaaaaaaa aaaaa) const override;");
3880   verifyGoogleFormat(
3881       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3882       "    const override;");
3883 
3884   // Even if the first parameter has to be wrapped.
3885   verifyFormat("void someLongFunction(\n"
3886                "    int someLongParameter) const {}",
3887                getLLVMStyleWithColumns(46));
3888   verifyFormat("void someLongFunction(\n"
3889                "    int someLongParameter) const {}",
3890                Style);
3891   verifyFormat("void someLongFunction(\n"
3892                "    int someLongParameter) override {}",
3893                Style);
3894   verifyFormat("void someLongFunction(\n"
3895                "    int someLongParameter) OVERRIDE {}",
3896                Style);
3897   verifyFormat("void someLongFunction(\n"
3898                "    int someLongParameter) final {}",
3899                Style);
3900   verifyFormat("void someLongFunction(\n"
3901                "    int someLongParameter) FINAL {}",
3902                Style);
3903   verifyFormat("void someLongFunction(\n"
3904                "    int parameter) const override {}",
3905                Style);
3906 
3907   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3908   verifyFormat("void someLongFunction(\n"
3909                "    int someLongParameter) const\n"
3910                "{\n"
3911                "}",
3912                Style);
3913 
3914   // Unless these are unknown annotations.
3915   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3916                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3917                "    LONG_AND_UGLY_ANNOTATION;");
3918 
3919   // Breaking before function-like trailing annotations is fine to keep them
3920   // close to their arguments.
3921   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3922                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3923   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3924                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3925   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3926                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3927   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3928                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3929   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3930 
3931   verifyFormat(
3932       "void aaaaaaaaaaaaaaaaaa()\n"
3933       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3934       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3935   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3936                "    __attribute__((unused));");
3937   verifyGoogleFormat(
3938       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3939       "    GUARDED_BY(aaaaaaaaaaaa);");
3940   verifyGoogleFormat(
3941       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3942       "    GUARDED_BY(aaaaaaaaaaaa);");
3943   verifyGoogleFormat(
3944       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3945       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3946   verifyGoogleFormat(
3947       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3948       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3949 }
3950 
3951 TEST_F(FormatTest, FunctionAnnotations) {
3952   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3953                "int OldFunction(const string &parameter) {}");
3954   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3955                "string OldFunction(const string &parameter) {}");
3956   verifyFormat("template <typename T>\n"
3957                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3958                "string OldFunction(const string &parameter) {}");
3959 
3960   // Not function annotations.
3961   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3962                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3963   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3964                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3965 }
3966 
3967 TEST_F(FormatTest, BreaksDesireably) {
3968   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3969                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3970                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3971   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3972                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3973                "}");
3974 
3975   verifyFormat(
3976       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3977       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3978 
3979   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3980                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3981                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3982 
3983   verifyFormat(
3984       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3985       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3986       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3987       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3988 
3989   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3990                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3991 
3992   verifyFormat(
3993       "void f() {\n"
3994       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3995       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3996       "}");
3997   verifyFormat(
3998       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3999       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4000   verifyFormat(
4001       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4002       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4003   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4004                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4005                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4006 
4007   // Indent consistently independent of call expression and unary operator.
4008   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4009                "    dddddddddddddddddddddddddddddd));");
4010   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4011                "    dddddddddddddddddddddddddddddd));");
4012   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4013                "    dddddddddddddddddddddddddddddd));");
4014 
4015   // This test case breaks on an incorrect memoization, i.e. an optimization not
4016   // taking into account the StopAt value.
4017   verifyFormat(
4018       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4019       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4020       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4021       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4022 
4023   verifyFormat("{\n  {\n    {\n"
4024                "      Annotation.SpaceRequiredBefore =\n"
4025                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4026                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4027                "    }\n  }\n}");
4028 
4029   // Break on an outer level if there was a break on an inner level.
4030   EXPECT_EQ("f(g(h(a, // comment\n"
4031             "      b, c),\n"
4032             "    d, e),\n"
4033             "  x, y);",
4034             format("f(g(h(a, // comment\n"
4035                    "    b, c), d, e), x, y);"));
4036 
4037   // Prefer breaking similar line breaks.
4038   verifyFormat(
4039       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4040       "                             NSTrackingMouseEnteredAndExited |\n"
4041       "                             NSTrackingActiveAlways;");
4042 }
4043 
4044 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4045   FormatStyle NoBinPacking = getGoogleStyle();
4046   NoBinPacking.BinPackParameters = false;
4047   NoBinPacking.BinPackArguments = true;
4048   verifyFormat("void f() {\n"
4049                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4050                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4051                "}",
4052                NoBinPacking);
4053   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4054                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4055                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4056                NoBinPacking);
4057 }
4058 
4059 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4060   FormatStyle NoBinPacking = getGoogleStyle();
4061   NoBinPacking.BinPackParameters = false;
4062   NoBinPacking.BinPackArguments = false;
4063   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4064                "  aaaaaaaaaaaaaaaaaaaa,\n"
4065                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4066                NoBinPacking);
4067   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4068                "        aaaaaaaaaaaaa,\n"
4069                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4070                NoBinPacking);
4071   verifyFormat(
4072       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4073       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4074       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4075       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4076       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4077       NoBinPacking);
4078   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4079                "    .aaaaaaaaaaaaaaaaaa();",
4080                NoBinPacking);
4081   verifyFormat("void f() {\n"
4082                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4083                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4084                "}",
4085                NoBinPacking);
4086 
4087   verifyFormat(
4088       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4089       "             aaaaaaaaaaaa,\n"
4090       "             aaaaaaaaaaaa);",
4091       NoBinPacking);
4092   verifyFormat(
4093       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4094       "                               ddddddddddddddddddddddddddddd),\n"
4095       "             test);",
4096       NoBinPacking);
4097 
4098   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4099                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4100                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4101                "    aaaaaaaaaaaaaaaaaa;",
4102                NoBinPacking);
4103   verifyFormat("a(\"a\"\n"
4104                "  \"a\",\n"
4105                "  a);");
4106 
4107   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4108   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4109                "                aaaaaaaaa,\n"
4110                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4111                NoBinPacking);
4112   verifyFormat(
4113       "void f() {\n"
4114       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4115       "      .aaaaaaa();\n"
4116       "}",
4117       NoBinPacking);
4118   verifyFormat(
4119       "template <class SomeType, class SomeOtherType>\n"
4120       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4121       NoBinPacking);
4122 }
4123 
4124 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4125   FormatStyle Style = getLLVMStyleWithColumns(15);
4126   Style.ExperimentalAutoDetectBinPacking = true;
4127   EXPECT_EQ("aaa(aaaa,\n"
4128             "    aaaa,\n"
4129             "    aaaa);\n"
4130             "aaa(aaaa,\n"
4131             "    aaaa,\n"
4132             "    aaaa);",
4133             format("aaa(aaaa,\n" // one-per-line
4134                    "  aaaa,\n"
4135                    "    aaaa  );\n"
4136                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4137                    Style));
4138   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4139             "    aaaa);\n"
4140             "aaa(aaaa, aaaa,\n"
4141             "    aaaa);",
4142             format("aaa(aaaa,  aaaa,\n" // bin-packed
4143                    "    aaaa  );\n"
4144                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4145                    Style));
4146 }
4147 
4148 TEST_F(FormatTest, FormatsBuilderPattern) {
4149   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4150                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4151                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4152                "    .StartsWith(\".init\", ORDER_INIT)\n"
4153                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4154                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4155                "    .Default(ORDER_TEXT);\n");
4156 
4157   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4158                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4159   verifyFormat(
4160       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4161       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4162       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4163   verifyFormat(
4164       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4165       "    aaaaaaaaaaaaaa);");
4166   verifyFormat(
4167       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4168       "    aaaaaa->aaaaaaaaaaaa()\n"
4169       "        ->aaaaaaaaaaaaaaaa(\n"
4170       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4171       "        ->aaaaaaaaaaaaaaaaa();");
4172   verifyGoogleFormat(
4173       "void f() {\n"
4174       "  someo->Add((new util::filetools::Handler(dir))\n"
4175       "                 ->OnEvent1(NewPermanentCallback(\n"
4176       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4177       "                 ->OnEvent2(NewPermanentCallback(\n"
4178       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4179       "                 ->OnEvent3(NewPermanentCallback(\n"
4180       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4181       "                 ->OnEvent5(NewPermanentCallback(\n"
4182       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4183       "                 ->OnEvent6(NewPermanentCallback(\n"
4184       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4185       "}");
4186 
4187   verifyFormat(
4188       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4189   verifyFormat("aaaaaaaaaaaaaaa()\n"
4190                "    .aaaaaaaaaaaaaaa()\n"
4191                "    .aaaaaaaaaaaaaaa()\n"
4192                "    .aaaaaaaaaaaaaaa()\n"
4193                "    .aaaaaaaaaaaaaaa();");
4194   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4195                "    .aaaaaaaaaaaaaaa()\n"
4196                "    .aaaaaaaaaaaaaaa()\n"
4197                "    .aaaaaaaaaaaaaaa();");
4198   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4199                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4200                "    .aaaaaaaaaaaaaaa();");
4201   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4202                "    ->aaaaaaaaaaaaaae(0)\n"
4203                "    ->aaaaaaaaaaaaaaa();");
4204 
4205   // Don't linewrap after very short segments.
4206   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4207                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4208                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4209   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4210                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4211                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4212   verifyFormat("aaa()\n"
4213                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4214                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4215                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4216 
4217   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4218                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4219                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4220   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4221                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4222                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4223 
4224   // Prefer not to break after empty parentheses.
4225   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4226                "    First->LastNewlineOffset);");
4227 }
4228 
4229 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4230   verifyFormat(
4231       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4232       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4233   verifyFormat(
4234       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4235       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4236 
4237   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4238                "    ccccccccccccccccccccccccc) {\n}");
4239   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4240                "    ccccccccccccccccccccccccc) {\n}");
4241 
4242   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4243                "    ccccccccccccccccccccccccc) {\n}");
4244   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4245                "    ccccccccccccccccccccccccc) {\n}");
4246 
4247   verifyFormat(
4248       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4249       "    ccccccccccccccccccccccccc) {\n}");
4250   verifyFormat(
4251       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4252       "    ccccccccccccccccccccccccc) {\n}");
4253 
4254   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4255                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4256                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4257                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4258   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4259                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4260                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4261                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4262 
4263   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4264                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4265                "    aaaaaaaaaaaaaaa != aa) {\n}");
4266   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4267                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4268                "    aaaaaaaaaaaaaaa != aa) {\n}");
4269 }
4270 
4271 TEST_F(FormatTest, BreaksAfterAssignments) {
4272   verifyFormat(
4273       "unsigned Cost =\n"
4274       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4275       "                        SI->getPointerAddressSpaceee());\n");
4276   verifyFormat(
4277       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4278       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4279 
4280   verifyFormat(
4281       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4282       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4283   verifyFormat("unsigned OriginalStartColumn =\n"
4284                "    SourceMgr.getSpellingColumnNumber(\n"
4285                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4286                "    1;");
4287 }
4288 
4289 TEST_F(FormatTest, AlignsAfterAssignments) {
4290   verifyFormat(
4291       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4292       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4293   verifyFormat(
4294       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4295       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4296   verifyFormat(
4297       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4298       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4299   verifyFormat(
4300       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4301       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4302   verifyFormat(
4303       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4304       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4305       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4306 }
4307 
4308 TEST_F(FormatTest, AlignsAfterReturn) {
4309   verifyFormat(
4310       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4311       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4312   verifyFormat(
4313       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4314       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4315   verifyFormat(
4316       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4317       "       aaaaaaaaaaaaaaaaaaaaaa();");
4318   verifyFormat(
4319       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4320       "        aaaaaaaaaaaaaaaaaaaaaa());");
4321   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4322                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4323   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4324                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4325                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4326   verifyFormat("return\n"
4327                "    // true if code is one of a or b.\n"
4328                "    code == a || code == b;");
4329 }
4330 
4331 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4332   verifyFormat(
4333       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4334       "                                                aaaaaaaaa aaaaaaa) {}");
4335   verifyFormat(
4336       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4337       "                                               aaaaaaaaaaa aaaaaaaaa);");
4338   verifyFormat(
4339       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4340       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4341   FormatStyle Style = getLLVMStyle();
4342   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4343   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4344                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4345                Style);
4346   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4347                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4348                Style);
4349   verifyFormat("SomeLongVariableName->someFunction(\n"
4350                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4351                Style);
4352   verifyFormat(
4353       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4354       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4355       Style);
4356   verifyFormat(
4357       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4358       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4359       Style);
4360   verifyFormat(
4361       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4362       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4363       Style);
4364 }
4365 
4366 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4367   FormatStyle Style = getLLVMStyleWithColumns(40);
4368   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4369                "          bbbbbbbbbbbbbbbbbbbbbb);",
4370                Style);
4371   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4372   Style.AlignOperands = false;
4373   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4374                "          bbbbbbbbbbbbbbbbbbbbbb);",
4375                Style);
4376   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4377   Style.AlignOperands = true;
4378   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4379                "          bbbbbbbbbbbbbbbbbbbbbb);",
4380                Style);
4381   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4382   Style.AlignOperands = false;
4383   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4384                "    bbbbbbbbbbbbbbbbbbbbbb);",
4385                Style);
4386 }
4387 
4388 TEST_F(FormatTest, BreaksConditionalExpressions) {
4389   verifyFormat(
4390       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4391       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4392       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4393   verifyFormat(
4394       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4395       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4396   verifyFormat(
4397       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4398       "                                                    : aaaaaaaaaaaaa);");
4399   verifyFormat(
4400       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4401       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4402       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4403       "                   aaaaaaaaaaaaa);");
4404   verifyFormat(
4405       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4406       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4407       "                   aaaaaaaaaaaaa);");
4408   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4409                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4410                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4411                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4412                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4413   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4414                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4415                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4416                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4417                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4418                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4419                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4420   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4421                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4422                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4423                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4424                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4425   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4426                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4427                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4428   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4429                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4430                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4431                "        : aaaaaaaaaaaaaaaa;");
4432   verifyFormat(
4433       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4434       "    ? aaaaaaaaaaaaaaa\n"
4435       "    : aaaaaaaaaaaaaaa;");
4436   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4437                "          aaaaaaaaa\n"
4438                "      ? b\n"
4439                "      : c);");
4440   verifyFormat("return aaaa == bbbb\n"
4441                "           // comment\n"
4442                "           ? aaaa\n"
4443                "           : bbbb;");
4444   verifyFormat("unsigned Indent =\n"
4445                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4446                "                              ? IndentForLevel[TheLine.Level]\n"
4447                "                              : TheLine * 2,\n"
4448                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4449                getLLVMStyleWithColumns(70));
4450   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4451                "                  ? aaaaaaaaaaaaaaa\n"
4452                "                  : bbbbbbbbbbbbbbb //\n"
4453                "                        ? ccccccccccccccc\n"
4454                "                        : ddddddddddddddd;");
4455   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4456                "                  ? aaaaaaaaaaaaaaa\n"
4457                "                  : (bbbbbbbbbbbbbbb //\n"
4458                "                         ? ccccccccccccccc\n"
4459                "                         : ddddddddddddddd);");
4460   verifyFormat(
4461       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4462       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4463       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4464       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4465       "                                      : aaaaaaaaaa;");
4466   verifyFormat(
4467       "aaaaaa = aaaaaaaaaaaa\n"
4468       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4469       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4470       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4471 
4472   FormatStyle NoBinPacking = getLLVMStyle();
4473   NoBinPacking.BinPackArguments = false;
4474   verifyFormat(
4475       "void f() {\n"
4476       "  g(aaa,\n"
4477       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4478       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4479       "        ? aaaaaaaaaaaaaaa\n"
4480       "        : aaaaaaaaaaaaaaa);\n"
4481       "}",
4482       NoBinPacking);
4483   verifyFormat(
4484       "void f() {\n"
4485       "  g(aaa,\n"
4486       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4487       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4488       "        ?: aaaaaaaaaaaaaaa);\n"
4489       "}",
4490       NoBinPacking);
4491 
4492   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4493                "             // comment.\n"
4494                "             ccccccccccccccccccccccccccccccccccccccc\n"
4495                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4496                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4497 
4498   // Assignments in conditional expressions. Apparently not uncommon :-(.
4499   verifyFormat("return a != b\n"
4500                "           // comment\n"
4501                "           ? a = b\n"
4502                "           : a = b;");
4503   verifyFormat("return a != b\n"
4504                "           // comment\n"
4505                "           ? a = a != b\n"
4506                "                     // comment\n"
4507                "                     ? a = b\n"
4508                "                     : a\n"
4509                "           : a;\n");
4510   verifyFormat("return a != b\n"
4511                "           // comment\n"
4512                "           ? a\n"
4513                "           : a = a != b\n"
4514                "                     // comment\n"
4515                "                     ? a = b\n"
4516                "                     : a;");
4517 }
4518 
4519 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4520   FormatStyle Style = getLLVMStyle();
4521   Style.BreakBeforeTernaryOperators = false;
4522   Style.ColumnLimit = 70;
4523   verifyFormat(
4524       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4525       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4526       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4527       Style);
4528   verifyFormat(
4529       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4530       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4531       Style);
4532   verifyFormat(
4533       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4534       "                                                      aaaaaaaaaaaaa);",
4535       Style);
4536   verifyFormat(
4537       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4538       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4539       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4540       "                   aaaaaaaaaaaaa);",
4541       Style);
4542   verifyFormat(
4543       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4544       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4545       "                   aaaaaaaaaaaaa);",
4546       Style);
4547   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4548                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4549                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4550                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4551                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4552                Style);
4553   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4554                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4555                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4556                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4557                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4558                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4559                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4560                Style);
4561   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4562                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4563                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4564                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4565                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4566                Style);
4567   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4568                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4569                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4570                Style);
4571   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4572                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4573                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4574                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4575                Style);
4576   verifyFormat(
4577       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4578       "    aaaaaaaaaaaaaaa :\n"
4579       "    aaaaaaaaaaaaaaa;",
4580       Style);
4581   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4582                "          aaaaaaaaa ?\n"
4583                "      b :\n"
4584                "      c);",
4585                Style);
4586   verifyFormat(
4587       "unsigned Indent =\n"
4588       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4589       "                              IndentForLevel[TheLine.Level] :\n"
4590       "                              TheLine * 2,\n"
4591       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4592       Style);
4593   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4594                "                  aaaaaaaaaaaaaaa :\n"
4595                "                  bbbbbbbbbbbbbbb ? //\n"
4596                "                      ccccccccccccccc :\n"
4597                "                      ddddddddddddddd;",
4598                Style);
4599   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4600                "                  aaaaaaaaaaaaaaa :\n"
4601                "                  (bbbbbbbbbbbbbbb ? //\n"
4602                "                       ccccccccccccccc :\n"
4603                "                       ddddddddddddddd);",
4604                Style);
4605 }
4606 
4607 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4608   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4609                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4610   verifyFormat("bool a = true, b = false;");
4611 
4612   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4613                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4614                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4615                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4616   verifyFormat(
4617       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4618       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4619       "     d = e && f;");
4620   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4621                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4622   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4623                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4624   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4625                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4626 
4627   FormatStyle Style = getGoogleStyle();
4628   Style.PointerAlignment = FormatStyle::PAS_Left;
4629   Style.DerivePointerAlignment = false;
4630   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4631                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4632                "    *b = bbbbbbbbbbbbbbbbbbb;",
4633                Style);
4634   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4635                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4636                Style);
4637 }
4638 
4639 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4640   verifyFormat("arr[foo ? bar : baz];");
4641   verifyFormat("f()[foo ? bar : baz];");
4642   verifyFormat("(a + b)[foo ? bar : baz];");
4643   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4644 }
4645 
4646 TEST_F(FormatTest, AlignsStringLiterals) {
4647   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4648                "                                      \"short literal\");");
4649   verifyFormat(
4650       "looooooooooooooooooooooooongFunction(\n"
4651       "    \"short literal\"\n"
4652       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4653   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4654                "             \" string literals\",\n"
4655                "             and, other, parameters);");
4656   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4657             "      \"5678\";",
4658             format("fun + \"1243\" /* comment */\n"
4659                    "      \"5678\";",
4660                    getLLVMStyleWithColumns(28)));
4661   EXPECT_EQ(
4662       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4663       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4664       "         \"aaaaaaaaaaaaaaaa\";",
4665       format("aaaaaa ="
4666              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4667              "aaaaaaaaaaaaaaaaaaaaa\" "
4668              "\"aaaaaaaaaaaaaaaa\";"));
4669   verifyFormat("a = a + \"a\"\n"
4670                "        \"a\"\n"
4671                "        \"a\";");
4672   verifyFormat("f(\"a\", \"b\"\n"
4673                "       \"c\");");
4674 
4675   verifyFormat(
4676       "#define LL_FORMAT \"ll\"\n"
4677       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4678       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4679 
4680   verifyFormat("#define A(X)          \\\n"
4681                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4682                "  \"ccccc\"",
4683                getLLVMStyleWithColumns(23));
4684   verifyFormat("#define A \"def\"\n"
4685                "f(\"abc\" A \"ghi\"\n"
4686                "  \"jkl\");");
4687 
4688   verifyFormat("f(L\"a\"\n"
4689                "  L\"b\");");
4690   verifyFormat("#define A(X)            \\\n"
4691                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4692                "  L\"ccccc\"",
4693                getLLVMStyleWithColumns(25));
4694 
4695   verifyFormat("f(@\"a\"\n"
4696                "  @\"b\");");
4697   verifyFormat("NSString s = @\"a\"\n"
4698                "             @\"b\"\n"
4699                "             @\"c\";");
4700   verifyFormat("NSString s = @\"a\"\n"
4701                "              \"b\"\n"
4702                "              \"c\";");
4703 }
4704 
4705 TEST_F(FormatTest, DefinitionReturnTypeBreakingStyle) {
4706   FormatStyle Style = getLLVMStyle();
4707   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_TopLevel;
4708   verifyFormat("class C {\n"
4709                "  int f() { return 1; }\n"
4710                "};\n"
4711                "int\n"
4712                "f() {\n"
4713                "  return 1;\n"
4714                "}",
4715                Style);
4716   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
4717   verifyFormat("class C {\n"
4718                "  int\n"
4719                "  f() {\n"
4720                "    return 1;\n"
4721                "  }\n"
4722                "};\n"
4723                "int\n"
4724                "f() {\n"
4725                "  return 1;\n"
4726                "}",
4727                Style);
4728   verifyFormat("const char *\n"
4729                "f(void) {\n" // Break here.
4730                "  return \"\";\n"
4731                "}\n"
4732                "const char *bar(void);\n", // No break here.
4733                Style);
4734   verifyFormat("template <class T>\n"
4735                "T *\n"
4736                "f(T &c) {\n" // Break here.
4737                "  return NULL;\n"
4738                "}\n"
4739                "template <class T> T *f(T &c);\n", // No break here.
4740                Style);
4741   verifyFormat("class C {\n"
4742                "  int\n"
4743                "  operator+() {\n"
4744                "    return 1;\n"
4745                "  }\n"
4746                "  int\n"
4747                "  operator()() {\n"
4748                "    return 1;\n"
4749                "  }\n"
4750                "};\n",
4751                Style);
4752   verifyFormat("void\n"
4753                "A::operator()() {}\n"
4754                "void\n"
4755                "A::operator>>() {}\n"
4756                "void\n"
4757                "A::operator+() {}\n",
4758                Style);
4759   verifyFormat("void *operator new(std::size_t s);", // No break here.
4760                Style);
4761   verifyFormat("void *\n"
4762                "operator new(std::size_t s) {}",
4763                Style);
4764   verifyFormat("void *\n"
4765                "operator delete[](void *ptr) {}",
4766                Style);
4767   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4768   verifyFormat("const char *\n"
4769                "f(void)\n" // Break here.
4770                "{\n"
4771                "  return \"\";\n"
4772                "}\n"
4773                "const char *bar(void);\n", // No break here.
4774                Style);
4775   verifyFormat("template <class T>\n"
4776                "T *\n"     // Problem here: no line break
4777                "f(T &c)\n" // Break here.
4778                "{\n"
4779                "  return NULL;\n"
4780                "}\n"
4781                "template <class T> T *f(T &c);\n", // No break here.
4782                Style);
4783 }
4784 
4785 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4786   FormatStyle NoBreak = getLLVMStyle();
4787   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4788   FormatStyle Break = getLLVMStyle();
4789   Break.AlwaysBreakBeforeMultilineStrings = true;
4790   verifyFormat("aaaa = \"bbbb\"\n"
4791                "       \"cccc\";",
4792                NoBreak);
4793   verifyFormat("aaaa =\n"
4794                "    \"bbbb\"\n"
4795                "    \"cccc\";",
4796                Break);
4797   verifyFormat("aaaa(\"bbbb\"\n"
4798                "     \"cccc\");",
4799                NoBreak);
4800   verifyFormat("aaaa(\n"
4801                "    \"bbbb\"\n"
4802                "    \"cccc\");",
4803                Break);
4804   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4805                "          \"cccc\");",
4806                NoBreak);
4807   verifyFormat("aaaa(qqq,\n"
4808                "     \"bbbb\"\n"
4809                "     \"cccc\");",
4810                Break);
4811   verifyFormat("aaaa(qqq,\n"
4812                "     L\"bbbb\"\n"
4813                "     L\"cccc\");",
4814                Break);
4815   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4816                "                      \"bbbb\"));",
4817                Break);
4818   verifyFormat("string s = someFunction(\n"
4819                "    \"abc\"\n"
4820                "    \"abc\");",
4821                Break);
4822 
4823   // As we break before unary operators, breaking right after them is bad.
4824   verifyFormat("string foo = abc ? \"x\"\n"
4825                "                   \"blah blah blah blah blah blah\"\n"
4826                "                 : \"y\";",
4827                Break);
4828 
4829   // Don't break if there is no column gain.
4830   verifyFormat("f(\"aaaa\"\n"
4831                "  \"bbbb\");",
4832                Break);
4833 
4834   // Treat literals with escaped newlines like multi-line string literals.
4835   EXPECT_EQ("x = \"a\\\n"
4836             "b\\\n"
4837             "c\";",
4838             format("x = \"a\\\n"
4839                    "b\\\n"
4840                    "c\";",
4841                    NoBreak));
4842   EXPECT_EQ("xxxx =\n"
4843             "    \"a\\\n"
4844             "b\\\n"
4845             "c\";",
4846             format("xxxx = \"a\\\n"
4847                    "b\\\n"
4848                    "c\";",
4849                    Break));
4850 
4851   // Exempt ObjC strings for now.
4852   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4853             "                          @\"bbbb\";",
4854             format("NSString *const kString = @\"aaaa\"\n"
4855                    "@\"bbbb\";",
4856                    Break));
4857 
4858   Break.ColumnLimit = 0;
4859   verifyFormat("const char *hello = \"hello llvm\";", Break);
4860 }
4861 
4862 TEST_F(FormatTest, AlignsPipes) {
4863   verifyFormat(
4864       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4865       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4866       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4867   verifyFormat(
4868       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4869       "                     << aaaaaaaaaaaaaaaaaaaa;");
4870   verifyFormat(
4871       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4872       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4873   verifyFormat(
4874       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4875       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4876       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4877   verifyFormat(
4878       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4879       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4880       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4881   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4882                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4883                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4884                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4885   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4886                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4887   verifyFormat(
4888       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4889       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4890 
4891   verifyFormat("return out << \"somepacket = {\\n\"\n"
4892                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4893                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4894                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4895                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4896                "           << \"}\";");
4897 
4898   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4899                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4900                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4901   verifyFormat(
4902       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4903       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4904       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4905       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4906       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4907   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4908                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4909   verifyFormat(
4910       "void f() {\n"
4911       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4912       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4913       "}");
4914   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4915                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4916   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4917                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4918                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4919                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4920   verifyFormat("LOG_IF(aaa == //\n"
4921                "       bbb)\n"
4922                "    << a << b;");
4923 
4924   // Breaking before the first "<<" is generally not desirable.
4925   verifyFormat(
4926       "llvm::errs()\n"
4927       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4928       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4929       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4930       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4931       getLLVMStyleWithColumns(70));
4932   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4933                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4934                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4935                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4936                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4937                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4938                getLLVMStyleWithColumns(70));
4939 
4940   // But sometimes, breaking before the first "<<" is desirable.
4941   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4942                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4943   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4944                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4945                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4946   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4947                "    << BEF << IsTemplate << Description << E->getType();");
4948 
4949   verifyFormat(
4950       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4951       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4952 
4953   // Incomplete string literal.
4954   EXPECT_EQ("llvm::errs() << \"\n"
4955             "             << a;",
4956             format("llvm::errs() << \"\n<<a;"));
4957 
4958   verifyFormat("void f() {\n"
4959                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4960                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4961                "}");
4962 
4963   // Handle 'endl'.
4964   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4965                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4966   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4967 }
4968 
4969 TEST_F(FormatTest, UnderstandsEquals) {
4970   verifyFormat(
4971       "aaaaaaaaaaaaaaaaa =\n"
4972       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4973   verifyFormat(
4974       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4975       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4976   verifyFormat(
4977       "if (a) {\n"
4978       "  f();\n"
4979       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4980       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4981       "}");
4982 
4983   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4984                "        100000000 + 10000000) {\n}");
4985 }
4986 
4987 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4988   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4989                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4990 
4991   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4992                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4993 
4994   verifyFormat(
4995       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4996       "                                                          Parameter2);");
4997 
4998   verifyFormat(
4999       "ShortObject->shortFunction(\n"
5000       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5001       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5002 
5003   verifyFormat("loooooooooooooongFunction(\n"
5004                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5005 
5006   verifyFormat(
5007       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5008       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5009 
5010   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5011                "    .WillRepeatedly(Return(SomeValue));");
5012   verifyFormat("void f() {\n"
5013                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5014                "      .Times(2)\n"
5015                "      .WillRepeatedly(Return(SomeValue));\n"
5016                "}");
5017   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5018                "    ccccccccccccccccccccccc);");
5019   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5020                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5021                "          .aaaaa(aaaaa),\n"
5022                "      aaaaaaaaaaaaaaaaaaaaa);");
5023   verifyFormat("void f() {\n"
5024                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5025                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5026                "}");
5027   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5028                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5029                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5030                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5031                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5032   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5033                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5034                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5035                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5036                "}");
5037 
5038   // Here, it is not necessary to wrap at "." or "->".
5039   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5040                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5041   verifyFormat(
5042       "aaaaaaaaaaa->aaaaaaaaa(\n"
5043       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5044       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5045 
5046   verifyFormat(
5047       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5048       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5049   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5050                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5051   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5052                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5053 
5054   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5055                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5056                "    .a();");
5057 
5058   FormatStyle NoBinPacking = getLLVMStyle();
5059   NoBinPacking.BinPackParameters = false;
5060   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5061                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5062                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5063                "                         aaaaaaaaaaaaaaaaaaa,\n"
5064                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5065                NoBinPacking);
5066 
5067   // If there is a subsequent call, change to hanging indentation.
5068   verifyFormat(
5069       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5070       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5071       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5072   verifyFormat(
5073       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5074       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5075   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5076                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5077                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5078   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5079                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5080                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5081 }
5082 
5083 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5084   verifyFormat("template <typename T>\n"
5085                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5086   verifyFormat("template <typename T>\n"
5087                "// T should be one of {A, B}.\n"
5088                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5089   verifyFormat(
5090       "template <typename T>\n"
5091       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5092   verifyFormat("template <typename T>\n"
5093                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5094                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5095   verifyFormat(
5096       "template <typename T>\n"
5097       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5098       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5099   verifyFormat(
5100       "template <typename T>\n"
5101       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5102       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5103       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5104   verifyFormat("template <typename T>\n"
5105                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5106                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5107   verifyFormat(
5108       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5109       "          typename T4 = char>\n"
5110       "void f();");
5111   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5112                "          template <typename> class cccccccccccccccccccccc,\n"
5113                "          typename ddddddddddddd>\n"
5114                "class C {};");
5115   verifyFormat(
5116       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5117       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5118 
5119   verifyFormat("void f() {\n"
5120                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5121                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5122                "}");
5123 
5124   verifyFormat("template <typename T> class C {};");
5125   verifyFormat("template <typename T> void f();");
5126   verifyFormat("template <typename T> void f() {}");
5127   verifyFormat(
5128       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5129       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5130       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5131       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5132       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5133       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5134       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5135       getLLVMStyleWithColumns(72));
5136   EXPECT_EQ("static_cast<A< //\n"
5137             "    B> *>(\n"
5138             "\n"
5139             "    );",
5140             format("static_cast<A<//\n"
5141                    "    B>*>(\n"
5142                    "\n"
5143                    "    );"));
5144   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5145                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5146 
5147   FormatStyle AlwaysBreak = getLLVMStyle();
5148   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5149   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5150   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5151   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5152   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5153                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5154                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5155   verifyFormat("template <template <typename> class Fooooooo,\n"
5156                "          template <typename> class Baaaaaaar>\n"
5157                "struct C {};",
5158                AlwaysBreak);
5159   verifyFormat("template <typename T> // T can be A, B or C.\n"
5160                "struct C {};",
5161                AlwaysBreak);
5162 }
5163 
5164 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5165   verifyFormat(
5166       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5167       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5168   verifyFormat(
5169       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5170       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5171       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5172 
5173   // FIXME: Should we have the extra indent after the second break?
5174   verifyFormat(
5175       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5176       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5177       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5178 
5179   verifyFormat(
5180       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5181       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5182 
5183   // Breaking at nested name specifiers is generally not desirable.
5184   verifyFormat(
5185       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5186       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5187 
5188   verifyFormat(
5189       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5190       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5191       "                   aaaaaaaaaaaaaaaaaaaaa);",
5192       getLLVMStyleWithColumns(74));
5193 
5194   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5195                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5196                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5197 }
5198 
5199 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5200   verifyFormat("A<int> a;");
5201   verifyFormat("A<A<A<int>>> a;");
5202   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5203   verifyFormat("bool x = a < 1 || 2 > a;");
5204   verifyFormat("bool x = 5 < f<int>();");
5205   verifyFormat("bool x = f<int>() > 5;");
5206   verifyFormat("bool x = 5 < a<int>::x;");
5207   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5208   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5209 
5210   verifyGoogleFormat("A<A<int>> a;");
5211   verifyGoogleFormat("A<A<A<int>>> a;");
5212   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5213   verifyGoogleFormat("A<A<int> > a;");
5214   verifyGoogleFormat("A<A<A<int> > > a;");
5215   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5216   verifyGoogleFormat("A<::A<int>> a;");
5217   verifyGoogleFormat("A<::A> a;");
5218   verifyGoogleFormat("A< ::A> a;");
5219   verifyGoogleFormat("A< ::A<int> > a;");
5220   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5221   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5222   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5223   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5224   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5225             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5226 
5227   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5228 
5229   verifyFormat("test >> a >> b;");
5230   verifyFormat("test << a >> b;");
5231 
5232   verifyFormat("f<int>();");
5233   verifyFormat("template <typename T> void f() {}");
5234   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5235   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5236                "sizeof(char)>::type>;");
5237   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5238 
5239   // Not template parameters.
5240   verifyFormat("return a < b && c > d;");
5241   verifyFormat("void f() {\n"
5242                "  while (a < b && c > d) {\n"
5243                "  }\n"
5244                "}");
5245   verifyFormat("template <typename... Types>\n"
5246                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5247 
5248   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5249                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5250                getLLVMStyleWithColumns(60));
5251   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5252   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5253   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5254 }
5255 
5256 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5257   verifyFormat("COMPARE(a, ==, b);");
5258 }
5259 
5260 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5261   verifyFormat("int A::*x;");
5262   verifyFormat("int (S::*func)(void *);");
5263   verifyFormat("void f() { int (S::*func)(void *); }");
5264   verifyFormat("typedef bool *(Class::*Member)() const;");
5265   verifyFormat("void f() {\n"
5266                "  (a->*f)();\n"
5267                "  a->*x;\n"
5268                "  (a.*f)();\n"
5269                "  ((*a).*f)();\n"
5270                "  a.*x;\n"
5271                "}");
5272   verifyFormat("void f() {\n"
5273                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5274                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5275                "}");
5276   verifyFormat(
5277       "(aaaaaaaaaa->*bbbbbbb)(\n"
5278       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5279   FormatStyle Style = getLLVMStyle();
5280   Style.PointerAlignment = FormatStyle::PAS_Left;
5281   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5282 }
5283 
5284 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5285   verifyFormat("int a = -2;");
5286   verifyFormat("f(-1, -2, -3);");
5287   verifyFormat("a[-1] = 5;");
5288   verifyFormat("int a = 5 + -2;");
5289   verifyFormat("if (i == -1) {\n}");
5290   verifyFormat("if (i != -1) {\n}");
5291   verifyFormat("if (i > -1) {\n}");
5292   verifyFormat("if (i < -1) {\n}");
5293   verifyFormat("++(a->f());");
5294   verifyFormat("--(a->f());");
5295   verifyFormat("(a->f())++;");
5296   verifyFormat("a[42]++;");
5297   verifyFormat("if (!(a->f())) {\n}");
5298 
5299   verifyFormat("a-- > b;");
5300   verifyFormat("b ? -a : c;");
5301   verifyFormat("n * sizeof char16;");
5302   verifyFormat("n * alignof char16;", getGoogleStyle());
5303   verifyFormat("sizeof(char);");
5304   verifyFormat("alignof(char);", getGoogleStyle());
5305 
5306   verifyFormat("return -1;");
5307   verifyFormat("switch (a) {\n"
5308                "case -1:\n"
5309                "  break;\n"
5310                "}");
5311   verifyFormat("#define X -1");
5312   verifyFormat("#define X -kConstant");
5313 
5314   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5315   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5316 
5317   verifyFormat("int a = /* confusing comment */ -1;");
5318   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5319   verifyFormat("int a = i /* confusing comment */++;");
5320 }
5321 
5322 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5323   verifyFormat("if (!aaaaaaaaaa( // break\n"
5324                "        aaaaa)) {\n"
5325                "}");
5326   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5327                "    aaaaa));");
5328   verifyFormat("*aaa = aaaaaaa( // break\n"
5329                "    bbbbbb);");
5330 }
5331 
5332 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5333   verifyFormat("bool operator<();");
5334   verifyFormat("bool operator>();");
5335   verifyFormat("bool operator=();");
5336   verifyFormat("bool operator==();");
5337   verifyFormat("bool operator!=();");
5338   verifyFormat("int operator+();");
5339   verifyFormat("int operator++();");
5340   verifyFormat("bool operator();");
5341   verifyFormat("bool operator()();");
5342   verifyFormat("bool operator[]();");
5343   verifyFormat("operator bool();");
5344   verifyFormat("operator int();");
5345   verifyFormat("operator void *();");
5346   verifyFormat("operator SomeType<int>();");
5347   verifyFormat("operator SomeType<int, int>();");
5348   verifyFormat("operator SomeType<SomeType<int>>();");
5349   verifyFormat("void *operator new(std::size_t size);");
5350   verifyFormat("void *operator new[](std::size_t size);");
5351   verifyFormat("void operator delete(void *ptr);");
5352   verifyFormat("void operator delete[](void *ptr);");
5353   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5354                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5355 
5356   verifyFormat(
5357       "ostream &operator<<(ostream &OutputStream,\n"
5358       "                    SomeReallyLongType WithSomeReallyLongValue);");
5359   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5360                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5361                "  return left.group < right.group;\n"
5362                "}");
5363   verifyFormat("SomeType &operator=(const SomeType &S);");
5364   verifyFormat("f.template operator()<int>();");
5365 
5366   verifyGoogleFormat("operator void*();");
5367   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5368   verifyGoogleFormat("operator ::A();");
5369 
5370   verifyFormat("using A::operator+;");
5371   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5372                "int i;");
5373 }
5374 
5375 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5376   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5377   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5378   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5379   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5380   verifyFormat("Deleted &operator=(const Deleted &) &;");
5381   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5382   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5383   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5384   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5385   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5386   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5387 
5388   FormatStyle AlignLeft = getLLVMStyle();
5389   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5390   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5391   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5392                AlignLeft);
5393   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5394   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5395 
5396   FormatStyle Spaces = getLLVMStyle();
5397   Spaces.SpacesInCStyleCastParentheses = true;
5398   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5399   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5400   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5401   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5402 
5403   Spaces.SpacesInCStyleCastParentheses = false;
5404   Spaces.SpacesInParentheses = true;
5405   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5406   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5407   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5408   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5409 }
5410 
5411 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5412   verifyFormat("void f() {\n"
5413                "  A *a = new A;\n"
5414                "  A *a = new (placement) A;\n"
5415                "  delete a;\n"
5416                "  delete (A *)a;\n"
5417                "}");
5418   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5419                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5420   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5421                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5422                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5423   verifyFormat("delete[] h->p;");
5424 }
5425 
5426 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5427   verifyFormat("int *f(int *a) {}");
5428   verifyFormat("int main(int argc, char **argv) {}");
5429   verifyFormat("Test::Test(int b) : a(b * b) {}");
5430   verifyIndependentOfContext("f(a, *a);");
5431   verifyFormat("void g() { f(*a); }");
5432   verifyIndependentOfContext("int a = b * 10;");
5433   verifyIndependentOfContext("int a = 10 * b;");
5434   verifyIndependentOfContext("int a = b * c;");
5435   verifyIndependentOfContext("int a += b * c;");
5436   verifyIndependentOfContext("int a -= b * c;");
5437   verifyIndependentOfContext("int a *= b * c;");
5438   verifyIndependentOfContext("int a /= b * c;");
5439   verifyIndependentOfContext("int a = *b;");
5440   verifyIndependentOfContext("int a = *b * c;");
5441   verifyIndependentOfContext("int a = b * *c;");
5442   verifyIndependentOfContext("int a = b * (10);");
5443   verifyIndependentOfContext("S << b * (10);");
5444   verifyIndependentOfContext("return 10 * b;");
5445   verifyIndependentOfContext("return *b * *c;");
5446   verifyIndependentOfContext("return a & ~b;");
5447   verifyIndependentOfContext("f(b ? *c : *d);");
5448   verifyIndependentOfContext("int a = b ? *c : *d;");
5449   verifyIndependentOfContext("*b = a;");
5450   verifyIndependentOfContext("a * ~b;");
5451   verifyIndependentOfContext("a * !b;");
5452   verifyIndependentOfContext("a * +b;");
5453   verifyIndependentOfContext("a * -b;");
5454   verifyIndependentOfContext("a * ++b;");
5455   verifyIndependentOfContext("a * --b;");
5456   verifyIndependentOfContext("a[4] * b;");
5457   verifyIndependentOfContext("a[a * a] = 1;");
5458   verifyIndependentOfContext("f() * b;");
5459   verifyIndependentOfContext("a * [self dostuff];");
5460   verifyIndependentOfContext("int x = a * (a + b);");
5461   verifyIndependentOfContext("(a *)(a + b);");
5462   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5463   verifyIndependentOfContext("int *pa = (int *)&a;");
5464   verifyIndependentOfContext("return sizeof(int **);");
5465   verifyIndependentOfContext("return sizeof(int ******);");
5466   verifyIndependentOfContext("return (int **&)a;");
5467   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5468   verifyFormat("void f(Type (*parameter)[10]) {}");
5469   verifyFormat("void f(Type (&parameter)[10]) {}");
5470   verifyGoogleFormat("return sizeof(int**);");
5471   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5472   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5473   verifyFormat("auto a = [](int **&, int ***) {};");
5474   verifyFormat("auto PointerBinding = [](const char *S) {};");
5475   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5476   verifyFormat("[](const decltype(*a) &value) {}");
5477   verifyFormat("decltype(a * b) F();");
5478   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5479   verifyIndependentOfContext("typedef void (*f)(int *a);");
5480   verifyIndependentOfContext("int i{a * b};");
5481   verifyIndependentOfContext("aaa && aaa->f();");
5482   verifyIndependentOfContext("int x = ~*p;");
5483   verifyFormat("Constructor() : a(a), area(width * height) {}");
5484   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5485   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5486   verifyFormat("void f() { f(a, c * d); }");
5487   verifyFormat("void f() { f(new a(), c * d); }");
5488 
5489   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5490 
5491   verifyIndependentOfContext("A<int *> a;");
5492   verifyIndependentOfContext("A<int **> a;");
5493   verifyIndependentOfContext("A<int *, int *> a;");
5494   verifyIndependentOfContext("A<int *[]> a;");
5495   verifyIndependentOfContext(
5496       "const char *const p = reinterpret_cast<const char *const>(q);");
5497   verifyIndependentOfContext("A<int **, int **> a;");
5498   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5499   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5500   verifyFormat("for (; a && b;) {\n}");
5501   verifyFormat("bool foo = true && [] { return false; }();");
5502 
5503   verifyFormat(
5504       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5505       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5506 
5507   verifyGoogleFormat("**outparam = 1;");
5508   verifyGoogleFormat("*outparam = a * b;");
5509   verifyGoogleFormat("int main(int argc, char** argv) {}");
5510   verifyGoogleFormat("A<int*> a;");
5511   verifyGoogleFormat("A<int**> a;");
5512   verifyGoogleFormat("A<int*, int*> a;");
5513   verifyGoogleFormat("A<int**, int**> a;");
5514   verifyGoogleFormat("f(b ? *c : *d);");
5515   verifyGoogleFormat("int a = b ? *c : *d;");
5516   verifyGoogleFormat("Type* t = **x;");
5517   verifyGoogleFormat("Type* t = *++*x;");
5518   verifyGoogleFormat("*++*x;");
5519   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5520   verifyGoogleFormat("Type* t = x++ * y;");
5521   verifyGoogleFormat(
5522       "const char* const p = reinterpret_cast<const char* const>(q);");
5523   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5524   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5525   verifyGoogleFormat("template <typename T>\n"
5526                      "void f(int i = 0, SomeType** temps = NULL);");
5527 
5528   FormatStyle Left = getLLVMStyle();
5529   Left.PointerAlignment = FormatStyle::PAS_Left;
5530   verifyFormat("x = *a(x) = *a(y);", Left);
5531   verifyFormat("for (;; * = b) {\n}", Left);
5532 
5533   verifyIndependentOfContext("a = *(x + y);");
5534   verifyIndependentOfContext("a = &(x + y);");
5535   verifyIndependentOfContext("*(x + y).call();");
5536   verifyIndependentOfContext("&(x + y)->call();");
5537   verifyFormat("void f() { &(*I).first; }");
5538 
5539   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5540   verifyFormat(
5541       "int *MyValues = {\n"
5542       "    *A, // Operator detection might be confused by the '{'\n"
5543       "    *BB // Operator detection might be confused by previous comment\n"
5544       "};");
5545 
5546   verifyIndependentOfContext("if (int *a = &b)");
5547   verifyIndependentOfContext("if (int &a = *b)");
5548   verifyIndependentOfContext("if (a & b[i])");
5549   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5550   verifyIndependentOfContext("if (*b[i])");
5551   verifyIndependentOfContext("if (int *a = (&b))");
5552   verifyIndependentOfContext("while (int *a = &b)");
5553   verifyIndependentOfContext("size = sizeof *a;");
5554   verifyIndependentOfContext("if (a && (b = c))");
5555   verifyFormat("void f() {\n"
5556                "  for (const int &v : Values) {\n"
5557                "  }\n"
5558                "}");
5559   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5560   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5561   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5562 
5563   verifyFormat("#define A (!a * b)");
5564   verifyFormat("#define MACRO     \\\n"
5565                "  int *i = a * b; \\\n"
5566                "  void f(a *b);",
5567                getLLVMStyleWithColumns(19));
5568 
5569   verifyIndependentOfContext("A = new SomeType *[Length];");
5570   verifyIndependentOfContext("A = new SomeType *[Length]();");
5571   verifyIndependentOfContext("T **t = new T *;");
5572   verifyIndependentOfContext("T **t = new T *();");
5573   verifyGoogleFormat("A = new SomeType*[Length]();");
5574   verifyGoogleFormat("A = new SomeType*[Length];");
5575   verifyGoogleFormat("T** t = new T*;");
5576   verifyGoogleFormat("T** t = new T*();");
5577 
5578   FormatStyle PointerLeft = getLLVMStyle();
5579   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5580   verifyFormat("delete *x;", PointerLeft);
5581   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5582   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5583   verifyFormat("template <bool a, bool b> "
5584                "typename t::if<x && y>::type f() {}");
5585   verifyFormat("template <int *y> f() {}");
5586   verifyFormat("vector<int *> v;");
5587   verifyFormat("vector<int *const> v;");
5588   verifyFormat("vector<int *const **const *> v;");
5589   verifyFormat("vector<int *volatile> v;");
5590   verifyFormat("vector<a * b> v;");
5591   verifyFormat("foo<b && false>();");
5592   verifyFormat("foo<b & 1>();");
5593   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5594   verifyFormat(
5595       "template <class T, class = typename std::enable_if<\n"
5596       "                       std::is_integral<T>::value &&\n"
5597       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5598       "void F();",
5599       getLLVMStyleWithColumns(76));
5600   verifyFormat(
5601       "template <class T,\n"
5602       "          class = typename ::std::enable_if<\n"
5603       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5604       "void F();",
5605       getGoogleStyleWithColumns(68));
5606 
5607   verifyIndependentOfContext("MACRO(int *i);");
5608   verifyIndependentOfContext("MACRO(auto *a);");
5609   verifyIndependentOfContext("MACRO(const A *a);");
5610   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5611   // FIXME: Is there a way to make this work?
5612   // verifyIndependentOfContext("MACRO(A *a);");
5613 
5614   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5615   verifyFormat("return options != nullptr && operator==(*options);");
5616 
5617   EXPECT_EQ("#define OP(x)                                    \\\n"
5618             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5619             "    return s << a.DebugString();                 \\\n"
5620             "  }",
5621             format("#define OP(x) \\\n"
5622                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5623                    "    return s << a.DebugString(); \\\n"
5624                    "  }",
5625                    getLLVMStyleWithColumns(50)));
5626 
5627   // FIXME: We cannot handle this case yet; we might be able to figure out that
5628   // foo<x> d > v; doesn't make sense.
5629   verifyFormat("foo<a<b && c> d> v;");
5630 
5631   FormatStyle PointerMiddle = getLLVMStyle();
5632   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5633   verifyFormat("delete *x;", PointerMiddle);
5634   verifyFormat("int * x;", PointerMiddle);
5635   verifyFormat("template <int * y> f() {}", PointerMiddle);
5636   verifyFormat("int * f(int * a) {}", PointerMiddle);
5637   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5638   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5639   verifyFormat("A<int *> a;", PointerMiddle);
5640   verifyFormat("A<int **> a;", PointerMiddle);
5641   verifyFormat("A<int *, int *> a;", PointerMiddle);
5642   verifyFormat("A<int * []> a;", PointerMiddle);
5643   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5644   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5645   verifyFormat("T ** t = new T *;", PointerMiddle);
5646 
5647   // Member function reference qualifiers aren't binary operators.
5648   verifyFormat("string // break\n"
5649                "operator()() & {}");
5650   verifyFormat("string // break\n"
5651                "operator()() && {}");
5652   verifyGoogleFormat("template <typename T>\n"
5653                      "auto x() & -> int {}");
5654 }
5655 
5656 TEST_F(FormatTest, UnderstandsAttributes) {
5657   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5658   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5659                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5660   FormatStyle AfterType = getLLVMStyle();
5661   AfterType.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
5662   verifyFormat("__attribute__((nodebug)) void\n"
5663                "foo() {}\n",
5664                AfterType);
5665 }
5666 
5667 TEST_F(FormatTest, UnderstandsEllipsis) {
5668   verifyFormat("int printf(const char *fmt, ...);");
5669   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5670   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5671 
5672   FormatStyle PointersLeft = getLLVMStyle();
5673   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5674   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5675 }
5676 
5677 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5678   EXPECT_EQ("int *a;\n"
5679             "int *a;\n"
5680             "int *a;",
5681             format("int *a;\n"
5682                    "int* a;\n"
5683                    "int *a;",
5684                    getGoogleStyle()));
5685   EXPECT_EQ("int* a;\n"
5686             "int* a;\n"
5687             "int* a;",
5688             format("int* a;\n"
5689                    "int* a;\n"
5690                    "int *a;",
5691                    getGoogleStyle()));
5692   EXPECT_EQ("int *a;\n"
5693             "int *a;\n"
5694             "int *a;",
5695             format("int *a;\n"
5696                    "int * a;\n"
5697                    "int *  a;",
5698                    getGoogleStyle()));
5699   EXPECT_EQ("auto x = [] {\n"
5700             "  int *a;\n"
5701             "  int *a;\n"
5702             "  int *a;\n"
5703             "};",
5704             format("auto x=[]{int *a;\n"
5705                    "int * a;\n"
5706                    "int *  a;};",
5707                    getGoogleStyle()));
5708 }
5709 
5710 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5711   verifyFormat("int f(int &&a) {}");
5712   verifyFormat("int f(int a, char &&b) {}");
5713   verifyFormat("void f() { int &&a = b; }");
5714   verifyGoogleFormat("int f(int a, char&& b) {}");
5715   verifyGoogleFormat("void f() { int&& a = b; }");
5716 
5717   verifyIndependentOfContext("A<int &&> a;");
5718   verifyIndependentOfContext("A<int &&, int &&> a;");
5719   verifyGoogleFormat("A<int&&> a;");
5720   verifyGoogleFormat("A<int&&, int&&> a;");
5721 
5722   // Not rvalue references:
5723   verifyFormat("template <bool B, bool C> class A {\n"
5724                "  static_assert(B && C, \"Something is wrong\");\n"
5725                "};");
5726   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5727   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5728   verifyFormat("#define A(a, b) (a && b)");
5729 }
5730 
5731 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5732   verifyFormat("void f() {\n"
5733                "  x[aaaaaaaaa -\n"
5734                "    b] = 23;\n"
5735                "}",
5736                getLLVMStyleWithColumns(15));
5737 }
5738 
5739 TEST_F(FormatTest, FormatsCasts) {
5740   verifyFormat("Type *A = static_cast<Type *>(P);");
5741   verifyFormat("Type *A = (Type *)P;");
5742   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5743   verifyFormat("int a = (int)(2.0f);");
5744   verifyFormat("int a = (int)2.0f;");
5745   verifyFormat("x[(int32)y];");
5746   verifyFormat("x = (int32)y;");
5747   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5748   verifyFormat("int a = (int)*b;");
5749   verifyFormat("int a = (int)2.0f;");
5750   verifyFormat("int a = (int)~0;");
5751   verifyFormat("int a = (int)++a;");
5752   verifyFormat("int a = (int)sizeof(int);");
5753   verifyFormat("int a = (int)+2;");
5754   verifyFormat("my_int a = (my_int)2.0f;");
5755   verifyFormat("my_int a = (my_int)sizeof(int);");
5756   verifyFormat("return (my_int)aaa;");
5757   verifyFormat("#define x ((int)-1)");
5758   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5759   verifyFormat("#define p(q) ((int *)&q)");
5760   verifyFormat("fn(a)(b) + 1;");
5761 
5762   verifyFormat("void f() { my_int a = (my_int)*b; }");
5763   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5764   verifyFormat("my_int a = (my_int)~0;");
5765   verifyFormat("my_int a = (my_int)++a;");
5766   verifyFormat("my_int a = (my_int)-2;");
5767   verifyFormat("my_int a = (my_int)1;");
5768   verifyFormat("my_int a = (my_int *)1;");
5769   verifyFormat("my_int a = (const my_int)-1;");
5770   verifyFormat("my_int a = (const my_int *)-1;");
5771   verifyFormat("my_int a = (my_int)(my_int)-1;");
5772   verifyFormat("my_int a = (ns::my_int)-2;");
5773   verifyFormat("case (my_int)ONE:");
5774 
5775   // FIXME: single value wrapped with paren will be treated as cast.
5776   verifyFormat("void f(int i = (kValue)*kMask) {}");
5777 
5778   verifyFormat("{ (void)F; }");
5779 
5780   // Don't break after a cast's
5781   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5782                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5783                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5784 
5785   // These are not casts.
5786   verifyFormat("void f(int *) {}");
5787   verifyFormat("f(foo)->b;");
5788   verifyFormat("f(foo).b;");
5789   verifyFormat("f(foo)(b);");
5790   verifyFormat("f(foo)[b];");
5791   verifyFormat("[](foo) { return 4; }(bar);");
5792   verifyFormat("(*funptr)(foo)[4];");
5793   verifyFormat("funptrs[4](foo)[4];");
5794   verifyFormat("void f(int *);");
5795   verifyFormat("void f(int *) = 0;");
5796   verifyFormat("void f(SmallVector<int>) {}");
5797   verifyFormat("void f(SmallVector<int>);");
5798   verifyFormat("void f(SmallVector<int>) = 0;");
5799   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5800   verifyFormat("int a = sizeof(int) * b;");
5801   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5802   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5803   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5804   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5805 
5806   // These are not casts, but at some point were confused with casts.
5807   verifyFormat("virtual void foo(int *) override;");
5808   verifyFormat("virtual void foo(char &) const;");
5809   verifyFormat("virtual void foo(int *a, char *) const;");
5810   verifyFormat("int a = sizeof(int *) + b;");
5811   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5812   verifyFormat("bool b = f(g<int>) && c;");
5813   verifyFormat("typedef void (*f)(int i) func;");
5814 
5815   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5816                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5817   // FIXME: The indentation here is not ideal.
5818   verifyFormat(
5819       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5820       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5821       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5822 }
5823 
5824 TEST_F(FormatTest, FormatsFunctionTypes) {
5825   verifyFormat("A<bool()> a;");
5826   verifyFormat("A<SomeType()> a;");
5827   verifyFormat("A<void (*)(int, std::string)> a;");
5828   verifyFormat("A<void *(int)>;");
5829   verifyFormat("void *(*a)(int *, SomeType *);");
5830   verifyFormat("int (*func)(void *);");
5831   verifyFormat("void f() { int (*func)(void *); }");
5832   verifyFormat("template <class CallbackClass>\n"
5833                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5834 
5835   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5836   verifyGoogleFormat("void* (*a)(int);");
5837   verifyGoogleFormat(
5838       "template <class CallbackClass>\n"
5839       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5840 
5841   // Other constructs can look somewhat like function types:
5842   verifyFormat("A<sizeof(*x)> a;");
5843   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5844   verifyFormat("some_var = function(*some_pointer_var)[0];");
5845   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5846 }
5847 
5848 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5849   verifyFormat("A (*foo_)[6];");
5850   verifyFormat("vector<int> (*foo_)[6];");
5851 }
5852 
5853 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5854   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5855                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5856   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5857                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5858   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5859                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5860 
5861   // Different ways of ()-initializiation.
5862   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5863                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5864   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5865                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5866   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5867                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5868   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5869                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5870 }
5871 
5872 TEST_F(FormatTest, BreaksLongDeclarations) {
5873   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5874                "    AnotherNameForTheLongType;");
5875   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5876                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5877   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5878                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5879   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5880                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5881   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5882                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5883   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5884                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5885   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5886                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5887   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5888                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5889   FormatStyle Indented = getLLVMStyle();
5890   Indented.IndentWrappedFunctionNames = true;
5891   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5892                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5893                Indented);
5894   verifyFormat(
5895       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5896       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5897       Indented);
5898   verifyFormat(
5899       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5900       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5901       Indented);
5902   verifyFormat(
5903       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5904       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5905       Indented);
5906 
5907   // FIXME: Without the comment, this breaks after "(".
5908   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5909                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5910                getGoogleStyle());
5911 
5912   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5913                "                  int LoooooooooooooooooooongParam2) {}");
5914   verifyFormat(
5915       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5916       "                                   SourceLocation L, IdentifierIn *II,\n"
5917       "                                   Type *T) {}");
5918   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5919                "ReallyReaaallyLongFunctionName(\n"
5920                "    const std::string &SomeParameter,\n"
5921                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5922                "        &ReallyReallyLongParameterName,\n"
5923                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5924                "        &AnotherLongParameterName) {}");
5925   verifyFormat("template <typename A>\n"
5926                "SomeLoooooooooooooooooooooongType<\n"
5927                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5928                "Function() {}");
5929 
5930   verifyGoogleFormat(
5931       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5932       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5933   verifyGoogleFormat(
5934       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5935       "                                   SourceLocation L) {}");
5936   verifyGoogleFormat(
5937       "some_namespace::LongReturnType\n"
5938       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5939       "    int first_long_parameter, int second_parameter) {}");
5940 
5941   verifyGoogleFormat("template <typename T>\n"
5942                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5943                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5944   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5945                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5946 
5947   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5948                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5949                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5950   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5951                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5952                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5953   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5954                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5955                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5956                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5957 }
5958 
5959 TEST_F(FormatTest, FormatsArrays) {
5960   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5961                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5962   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5963                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5964   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5965                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5966   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5967                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5968                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5969   verifyFormat(
5970       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5971       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5972       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5973 
5974   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5975                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5976   verifyFormat(
5977       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5978       "                                  .aaaaaaa[0]\n"
5979       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5980 
5981   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5982 }
5983 
5984 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5985   verifyFormat("(a)->b();");
5986   verifyFormat("--a;");
5987 }
5988 
5989 TEST_F(FormatTest, HandlesIncludeDirectives) {
5990   verifyFormat("#include <string>\n"
5991                "#include <a/b/c.h>\n"
5992                "#include \"a/b/string\"\n"
5993                "#include \"string.h\"\n"
5994                "#include \"string.h\"\n"
5995                "#include <a-a>\n"
5996                "#include < path with space >\n"
5997                "#include_next <test.h>"
5998                "#include \"abc.h\" // this is included for ABC\n"
5999                "#include \"some long include\" // with a comment\n"
6000                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6001                getLLVMStyleWithColumns(35));
6002   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6003   EXPECT_EQ("#include <a>", format("#include<a>"));
6004 
6005   verifyFormat("#import <string>");
6006   verifyFormat("#import <a/b/c.h>");
6007   verifyFormat("#import \"a/b/string\"");
6008   verifyFormat("#import \"string.h\"");
6009   verifyFormat("#import \"string.h\"");
6010   verifyFormat("#if __has_include(<strstream>)\n"
6011                "#include <strstream>\n"
6012                "#endif");
6013 
6014   verifyFormat("#define MY_IMPORT <a/b>");
6015 
6016   // Protocol buffer definition or missing "#".
6017   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6018                getLLVMStyleWithColumns(30));
6019 
6020   FormatStyle Style = getLLVMStyle();
6021   Style.AlwaysBreakBeforeMultilineStrings = true;
6022   Style.ColumnLimit = 0;
6023   verifyFormat("#import \"abc.h\"", Style);
6024 
6025   // But 'import' might also be a regular C++ namespace.
6026   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6027                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6028 }
6029 
6030 //===----------------------------------------------------------------------===//
6031 // Error recovery tests.
6032 //===----------------------------------------------------------------------===//
6033 
6034 TEST_F(FormatTest, IncompleteParameterLists) {
6035   FormatStyle NoBinPacking = getLLVMStyle();
6036   NoBinPacking.BinPackParameters = false;
6037   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6038                "                        double *min_x,\n"
6039                "                        double *max_x,\n"
6040                "                        double *min_y,\n"
6041                "                        double *max_y,\n"
6042                "                        double *min_z,\n"
6043                "                        double *max_z, ) {}",
6044                NoBinPacking);
6045 }
6046 
6047 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6048   verifyFormat("void f() { return; }\n42");
6049   verifyFormat("void f() {\n"
6050                "  if (0)\n"
6051                "    return;\n"
6052                "}\n"
6053                "42");
6054   verifyFormat("void f() { return }\n42");
6055   verifyFormat("void f() {\n"
6056                "  if (0)\n"
6057                "    return\n"
6058                "}\n"
6059                "42");
6060 }
6061 
6062 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6063   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6064   EXPECT_EQ("void f() {\n"
6065             "  if (a)\n"
6066             "    return\n"
6067             "}",
6068             format("void  f  (  )  {  if  ( a )  return  }"));
6069   EXPECT_EQ("namespace N {\n"
6070             "void f()\n"
6071             "}",
6072             format("namespace  N  {  void f()  }"));
6073   EXPECT_EQ("namespace N {\n"
6074             "void f() {}\n"
6075             "void g()\n"
6076             "}",
6077             format("namespace N  { void f( ) { } void g( ) }"));
6078 }
6079 
6080 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6081   verifyFormat("int aaaaaaaa =\n"
6082                "    // Overlylongcomment\n"
6083                "    b;",
6084                getLLVMStyleWithColumns(20));
6085   verifyFormat("function(\n"
6086                "    ShortArgument,\n"
6087                "    LoooooooooooongArgument);\n",
6088                getLLVMStyleWithColumns(20));
6089 }
6090 
6091 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6092   verifyFormat("public:");
6093   verifyFormat("class A {\n"
6094                "public\n"
6095                "  void f() {}\n"
6096                "};");
6097   verifyFormat("public\n"
6098                "int qwerty;");
6099   verifyFormat("public\n"
6100                "B {}");
6101   verifyFormat("public\n"
6102                "{}");
6103   verifyFormat("public\n"
6104                "B { int x; }");
6105 }
6106 
6107 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6108   verifyFormat("{");
6109   verifyFormat("#})");
6110   verifyNoCrash("(/**/[:!] ?[).");
6111 }
6112 
6113 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6114   verifyFormat("do {\n}");
6115   verifyFormat("do {\n}\n"
6116                "f();");
6117   verifyFormat("do {\n}\n"
6118                "wheeee(fun);");
6119   verifyFormat("do {\n"
6120                "  f();\n"
6121                "}");
6122 }
6123 
6124 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6125   verifyFormat("if {\n  foo;\n  foo();\n}");
6126   verifyFormat("switch {\n  foo;\n  foo();\n}");
6127   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6128   verifyFormat("while {\n  foo;\n  foo();\n}");
6129   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6130 }
6131 
6132 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6133   verifyIncompleteFormat("namespace {\n"
6134                          "class Foo { Foo (\n"
6135                          "};\n"
6136                          "} // comment");
6137 }
6138 
6139 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6140   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6141   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6142   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6143   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6144 
6145   EXPECT_EQ("{\n"
6146             "  {\n"
6147             "    breakme(\n"
6148             "        qwe);\n"
6149             "  }\n",
6150             format("{\n"
6151                    "    {\n"
6152                    " breakme(qwe);\n"
6153                    "}\n",
6154                    getLLVMStyleWithColumns(10)));
6155 }
6156 
6157 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6158   verifyFormat("int x = {\n"
6159                "    avariable,\n"
6160                "    b(alongervariable)};",
6161                getLLVMStyleWithColumns(25));
6162 }
6163 
6164 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6165   verifyFormat("return (a)(b){1, 2, 3};");
6166 }
6167 
6168 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6169   verifyFormat("vector<int> x{1, 2, 3, 4};");
6170   verifyFormat("vector<int> x{\n"
6171                "    1, 2, 3, 4,\n"
6172                "};");
6173   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6174   verifyFormat("f({1, 2});");
6175   verifyFormat("auto v = Foo{-1};");
6176   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6177   verifyFormat("Class::Class : member{1, 2, 3} {}");
6178   verifyFormat("new vector<int>{1, 2, 3};");
6179   verifyFormat("new int[3]{1, 2, 3};");
6180   verifyFormat("new int{1};");
6181   verifyFormat("return {arg1, arg2};");
6182   verifyFormat("return {arg1, SomeType{parameter}};");
6183   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6184   verifyFormat("new T{arg1, arg2};");
6185   verifyFormat("f(MyMap[{composite, key}]);");
6186   verifyFormat("class Class {\n"
6187                "  T member = {arg1, arg2};\n"
6188                "};");
6189   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6190   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6191   verifyFormat("int a = std::is_integral<int>{} + 0;");
6192 
6193   verifyFormat("int foo(int i) { return fo1{}(i); }");
6194   verifyFormat("int foo(int i) { return fo1{}(i); }");
6195   verifyFormat("auto i = decltype(x){};");
6196   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6197   verifyFormat("Node n{1, Node{1000}, //\n"
6198                "       2};");
6199   verifyFormat("Aaaa aaaaaaa{\n"
6200                "    {\n"
6201                "        aaaa,\n"
6202                "    },\n"
6203                "};");
6204   verifyFormat("class C : public D {\n"
6205                "  SomeClass SC{2};\n"
6206                "};");
6207   verifyFormat("class C : public A {\n"
6208                "  class D : public B {\n"
6209                "    void f() { int i{2}; }\n"
6210                "  };\n"
6211                "};");
6212   verifyFormat("#define A {a, a},");
6213 
6214   // In combination with BinPackArguments = false.
6215   FormatStyle NoBinPacking = getLLVMStyle();
6216   NoBinPacking.BinPackArguments = false;
6217   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6218                "                      bbbbb,\n"
6219                "                      ccccc,\n"
6220                "                      ddddd,\n"
6221                "                      eeeee,\n"
6222                "                      ffffff,\n"
6223                "                      ggggg,\n"
6224                "                      hhhhhh,\n"
6225                "                      iiiiii,\n"
6226                "                      jjjjjj,\n"
6227                "                      kkkkkk};",
6228                NoBinPacking);
6229   verifyFormat("const Aaaaaa aaaaa = {\n"
6230                "    aaaaa,\n"
6231                "    bbbbb,\n"
6232                "    ccccc,\n"
6233                "    ddddd,\n"
6234                "    eeeee,\n"
6235                "    ffffff,\n"
6236                "    ggggg,\n"
6237                "    hhhhhh,\n"
6238                "    iiiiii,\n"
6239                "    jjjjjj,\n"
6240                "    kkkkkk,\n"
6241                "};",
6242                NoBinPacking);
6243   verifyFormat(
6244       "const Aaaaaa aaaaa = {\n"
6245       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6246       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6247       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6248       "};",
6249       NoBinPacking);
6250 
6251   // FIXME: The alignment of these trailing comments might be bad. Then again,
6252   // this might be utterly useless in real code.
6253   verifyFormat("Constructor::Constructor()\n"
6254                "    : some_value{         //\n"
6255                "                 aaaaaaa, //\n"
6256                "                 bbbbbbb} {}");
6257 
6258   // In braced lists, the first comment is always assumed to belong to the
6259   // first element. Thus, it can be moved to the next or previous line as
6260   // appropriate.
6261   EXPECT_EQ("function({// First element:\n"
6262             "          1,\n"
6263             "          // Second element:\n"
6264             "          2});",
6265             format("function({\n"
6266                    "    // First element:\n"
6267                    "    1,\n"
6268                    "    // Second element:\n"
6269                    "    2});"));
6270   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6271             "    // First element:\n"
6272             "    1,\n"
6273             "    // Second element:\n"
6274             "    2};",
6275             format("std::vector<int> MyNumbers{// First element:\n"
6276                    "                           1,\n"
6277                    "                           // Second element:\n"
6278                    "                           2};",
6279                    getLLVMStyleWithColumns(30)));
6280   // A trailing comma should still lead to an enforced line break.
6281   EXPECT_EQ("vector<int> SomeVector = {\n"
6282             "    // aaa\n"
6283             "    1, 2,\n"
6284             "};",
6285             format("vector<int> SomeVector = { // aaa\n"
6286                    "    1, 2, };"));
6287 
6288   FormatStyle ExtraSpaces = getLLVMStyle();
6289   ExtraSpaces.Cpp11BracedListStyle = false;
6290   ExtraSpaces.ColumnLimit = 75;
6291   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6292   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6293   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6294   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6295   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6296   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6297   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6298   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6299   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6300   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6301   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6302   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6303   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6304   verifyFormat("class Class {\n"
6305                "  T member = { arg1, arg2 };\n"
6306                "};",
6307                ExtraSpaces);
6308   verifyFormat(
6309       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6310       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6311       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6312       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6313       ExtraSpaces);
6314   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6315   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6316                ExtraSpaces);
6317   verifyFormat(
6318       "someFunction(OtherParam,\n"
6319       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6320       "                         param1, param2,\n"
6321       "                         // comment 2\n"
6322       "                         param3, param4 });",
6323       ExtraSpaces);
6324   verifyFormat(
6325       "std::this_thread::sleep_for(\n"
6326       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6327       ExtraSpaces);
6328   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6329                "    aaaaaaa,\n"
6330                "    aaaaaaaaaa,\n"
6331                "    aaaaa,\n"
6332                "    aaaaaaaaaaaaaaa,\n"
6333                "    aaa,\n"
6334                "    aaaaaaaaaa,\n"
6335                "    a,\n"
6336                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6337                "    aaaaaaaaaaaa,\n"
6338                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6339                "    aaaaaaa,\n"
6340                "    a};");
6341   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6342 }
6343 
6344 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6345   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6346                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6347                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6348                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6349                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6350                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6351   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6352                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6353                "                 1, 22, 333, 4444, 55555, //\n"
6354                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6355                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6356   verifyFormat(
6357       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6358       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6359       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6360       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6361       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6362       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6363       "                 7777777};");
6364   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6365                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6366                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6367   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6368                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6369                "    // Separating comment.\n"
6370                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6371   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6372                "    // Leading comment\n"
6373                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6374                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6375   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6376                "                 1, 1, 1, 1};",
6377                getLLVMStyleWithColumns(39));
6378   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6379                "                 1, 1, 1, 1};",
6380                getLLVMStyleWithColumns(38));
6381   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6382                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6383                getLLVMStyleWithColumns(43));
6384   verifyFormat(
6385       "static unsigned SomeValues[10][3] = {\n"
6386       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6387       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6388   verifyFormat("static auto fields = new vector<string>{\n"
6389                "    \"aaaaaaaaaaaaa\",\n"
6390                "    \"aaaaaaaaaaaaa\",\n"
6391                "    \"aaaaaaaaaaaa\",\n"
6392                "    \"aaaaaaaaaaaaaa\",\n"
6393                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6394                "    \"aaaaaaaaaaaa\",\n"
6395                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6396                "};");
6397   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6398   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6399                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6400                "                 3, cccccccccccccccccccccc};",
6401                getLLVMStyleWithColumns(60));
6402 
6403   // Trailing commas.
6404   verifyFormat("vector<int> x = {\n"
6405                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6406                "};",
6407                getLLVMStyleWithColumns(39));
6408   verifyFormat("vector<int> x = {\n"
6409                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6410                "};",
6411                getLLVMStyleWithColumns(39));
6412   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6413                "                 1, 1, 1, 1,\n"
6414                "                 /**/ /**/};",
6415                getLLVMStyleWithColumns(39));
6416 
6417   // Trailing comment in the first line.
6418   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6419                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6420                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6421                "    11111111,   22222222,   333333333,   44444444};");
6422   // Trailing comment in the last line.
6423   verifyFormat("int aaaaa[] = {\n"
6424                "    1, 2, 3, // comment\n"
6425                "    4, 5, 6  // comment\n"
6426                "};");
6427 
6428   // With nested lists, we should either format one item per line or all nested
6429   // lists one on line.
6430   // FIXME: For some nested lists, we can do better.
6431   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6432                "        {aaaaaaaaaaaaaaaaaaa},\n"
6433                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6434                "        {aaaaaaaaaaaaaaaaa}};",
6435                getLLVMStyleWithColumns(60));
6436   verifyFormat(
6437       "SomeStruct my_struct_array = {\n"
6438       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6439       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6440       "    {aaa, aaa},\n"
6441       "    {aaa, aaa},\n"
6442       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6443       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6444       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6445 
6446   // No column layout should be used here.
6447   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6448                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6449 
6450   verifyNoCrash("a<,");
6451 }
6452 
6453 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6454   FormatStyle DoNotMerge = getLLVMStyle();
6455   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6456 
6457   verifyFormat("void f() { return 42; }");
6458   verifyFormat("void f() {\n"
6459                "  return 42;\n"
6460                "}",
6461                DoNotMerge);
6462   verifyFormat("void f() {\n"
6463                "  // Comment\n"
6464                "}");
6465   verifyFormat("{\n"
6466                "#error {\n"
6467                "  int a;\n"
6468                "}");
6469   verifyFormat("{\n"
6470                "  int a;\n"
6471                "#error {\n"
6472                "}");
6473   verifyFormat("void f() {} // comment");
6474   verifyFormat("void f() { int a; } // comment");
6475   verifyFormat("void f() {\n"
6476                "} // comment",
6477                DoNotMerge);
6478   verifyFormat("void f() {\n"
6479                "  int a;\n"
6480                "} // comment",
6481                DoNotMerge);
6482   verifyFormat("void f() {\n"
6483                "} // comment",
6484                getLLVMStyleWithColumns(15));
6485 
6486   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6487   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6488 
6489   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6490   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6491   verifyFormat("class C {\n"
6492                "  C()\n"
6493                "      : iiiiiiii(nullptr),\n"
6494                "        kkkkkkk(nullptr),\n"
6495                "        mmmmmmm(nullptr),\n"
6496                "        nnnnnnn(nullptr) {}\n"
6497                "};",
6498                getGoogleStyle());
6499 
6500   FormatStyle NoColumnLimit = getLLVMStyle();
6501   NoColumnLimit.ColumnLimit = 0;
6502   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6503   EXPECT_EQ("class C {\n"
6504             "  A() : b(0) {}\n"
6505             "};",
6506             format("class C{A():b(0){}};", NoColumnLimit));
6507   EXPECT_EQ("A()\n"
6508             "    : b(0) {\n"
6509             "}",
6510             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6511 
6512   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6513   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6514       FormatStyle::SFS_None;
6515   EXPECT_EQ("A()\n"
6516             "    : b(0) {\n"
6517             "}",
6518             format("A():b(0){}", DoNotMergeNoColumnLimit));
6519   EXPECT_EQ("A()\n"
6520             "    : b(0) {\n"
6521             "}",
6522             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6523 
6524   verifyFormat("#define A          \\\n"
6525                "  void f() {       \\\n"
6526                "    int i;         \\\n"
6527                "  }",
6528                getLLVMStyleWithColumns(20));
6529   verifyFormat("#define A           \\\n"
6530                "  void f() { int i; }",
6531                getLLVMStyleWithColumns(21));
6532   verifyFormat("#define A            \\\n"
6533                "  void f() {         \\\n"
6534                "    int i;           \\\n"
6535                "  }                  \\\n"
6536                "  int j;",
6537                getLLVMStyleWithColumns(22));
6538   verifyFormat("#define A             \\\n"
6539                "  void f() { int i; } \\\n"
6540                "  int j;",
6541                getLLVMStyleWithColumns(23));
6542 }
6543 
6544 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6545   FormatStyle MergeInlineOnly = getLLVMStyle();
6546   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6547   verifyFormat("class C {\n"
6548                "  int f() { return 42; }\n"
6549                "};",
6550                MergeInlineOnly);
6551   verifyFormat("int f() {\n"
6552                "  return 42;\n"
6553                "}",
6554                MergeInlineOnly);
6555 }
6556 
6557 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6558   // Elaborate type variable declarations.
6559   verifyFormat("struct foo a = {bar};\nint n;");
6560   verifyFormat("class foo a = {bar};\nint n;");
6561   verifyFormat("union foo a = {bar};\nint n;");
6562 
6563   // Elaborate types inside function definitions.
6564   verifyFormat("struct foo f() {}\nint n;");
6565   verifyFormat("class foo f() {}\nint n;");
6566   verifyFormat("union foo f() {}\nint n;");
6567 
6568   // Templates.
6569   verifyFormat("template <class X> void f() {}\nint n;");
6570   verifyFormat("template <struct X> void f() {}\nint n;");
6571   verifyFormat("template <union X> void f() {}\nint n;");
6572 
6573   // Actual definitions...
6574   verifyFormat("struct {\n} n;");
6575   verifyFormat(
6576       "template <template <class T, class Y>, class Z> class X {\n} n;");
6577   verifyFormat("union Z {\n  int n;\n} x;");
6578   verifyFormat("class MACRO Z {\n} n;");
6579   verifyFormat("class MACRO(X) Z {\n} n;");
6580   verifyFormat("class __attribute__(X) Z {\n} n;");
6581   verifyFormat("class __declspec(X) Z {\n} n;");
6582   verifyFormat("class A##B##C {\n} n;");
6583   verifyFormat("class alignas(16) Z {\n} n;");
6584   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6585   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6586 
6587   // Redefinition from nested context:
6588   verifyFormat("class A::B::C {\n} n;");
6589 
6590   // Template definitions.
6591   verifyFormat(
6592       "template <typename F>\n"
6593       "Matcher(const Matcher<F> &Other,\n"
6594       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6595       "                             !is_same<F, T>::value>::type * = 0)\n"
6596       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6597 
6598   // FIXME: This is still incorrectly handled at the formatter side.
6599   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6600   verifyFormat("int i = SomeFunction(a<b, a> b);");
6601 
6602   // FIXME:
6603   // This now gets parsed incorrectly as class definition.
6604   // verifyFormat("class A<int> f() {\n}\nint n;");
6605 
6606   // Elaborate types where incorrectly parsing the structural element would
6607   // break the indent.
6608   verifyFormat("if (true)\n"
6609                "  class X x;\n"
6610                "else\n"
6611                "  f();\n");
6612 
6613   // This is simply incomplete. Formatting is not important, but must not crash.
6614   verifyFormat("class A:");
6615 }
6616 
6617 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6618   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6619             format("#error Leave     all         white!!!!! space* alone!\n"));
6620   EXPECT_EQ(
6621       "#warning Leave     all         white!!!!! space* alone!\n",
6622       format("#warning Leave     all         white!!!!! space* alone!\n"));
6623   EXPECT_EQ("#error 1", format("  #  error   1"));
6624   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6625 }
6626 
6627 TEST_F(FormatTest, FormatHashIfExpressions) {
6628   verifyFormat("#if AAAA && BBBB");
6629   verifyFormat("#if (AAAA && BBBB)");
6630   verifyFormat("#elif (AAAA && BBBB)");
6631   // FIXME: Come up with a better indentation for #elif.
6632   verifyFormat(
6633       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6634       "    defined(BBBBBBBB)\n"
6635       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6636       "    defined(BBBBBBBB)\n"
6637       "#endif",
6638       getLLVMStyleWithColumns(65));
6639 }
6640 
6641 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6642   FormatStyle AllowsMergedIf = getGoogleStyle();
6643   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6644   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6645   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6646   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6647   EXPECT_EQ("if (true) return 42;",
6648             format("if (true)\nreturn 42;", AllowsMergedIf));
6649   FormatStyle ShortMergedIf = AllowsMergedIf;
6650   ShortMergedIf.ColumnLimit = 25;
6651   verifyFormat("#define A \\\n"
6652                "  if (true) return 42;",
6653                ShortMergedIf);
6654   verifyFormat("#define A \\\n"
6655                "  f();    \\\n"
6656                "  if (true)\n"
6657                "#define B",
6658                ShortMergedIf);
6659   verifyFormat("#define A \\\n"
6660                "  f();    \\\n"
6661                "  if (true)\n"
6662                "g();",
6663                ShortMergedIf);
6664   verifyFormat("{\n"
6665                "#ifdef A\n"
6666                "  // Comment\n"
6667                "  if (true) continue;\n"
6668                "#endif\n"
6669                "  // Comment\n"
6670                "  if (true) continue;\n"
6671                "}",
6672                ShortMergedIf);
6673   ShortMergedIf.ColumnLimit = 29;
6674   verifyFormat("#define A                   \\\n"
6675                "  if (aaaaaaaaaa) return 1; \\\n"
6676                "  return 2;",
6677                ShortMergedIf);
6678   ShortMergedIf.ColumnLimit = 28;
6679   verifyFormat("#define A         \\\n"
6680                "  if (aaaaaaaaaa) \\\n"
6681                "    return 1;     \\\n"
6682                "  return 2;",
6683                ShortMergedIf);
6684 }
6685 
6686 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6687   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6688                "  f();\n"
6689                "}");
6690   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6691                "  f();\n"
6692                "} /* another comment */ else /* comment #3 */ {\n"
6693                "  g();\n"
6694                "}");
6695   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6696                "  f();\n"
6697                "}");
6698   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6699                "  f();\n"
6700                "}");
6701   verifyFormat("do /* a comment in a strange place */ {\n"
6702                "  f();\n"
6703                "} /* another comment */ while (0);");
6704 }
6705 
6706 TEST_F(FormatTest, BlockComments) {
6707   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6708             format("/* *//* */  /* */\n/* *//* */  /* */"));
6709   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6710   EXPECT_EQ("#define A /*123*/ \\\n"
6711             "  b\n"
6712             "/* */\n"
6713             "someCall(\n"
6714             "    parameter);",
6715             format("#define A /*123*/ b\n"
6716                    "/* */\n"
6717                    "someCall(parameter);",
6718                    getLLVMStyleWithColumns(15)));
6719 
6720   EXPECT_EQ("#define A\n"
6721             "/* */ someCall(\n"
6722             "    parameter);",
6723             format("#define A\n"
6724                    "/* */someCall(parameter);",
6725                    getLLVMStyleWithColumns(15)));
6726   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6727   EXPECT_EQ("/*\n"
6728             "*\n"
6729             " * aaaaaa\n"
6730             " * aaaaaa\n"
6731             "*/",
6732             format("/*\n"
6733                    "*\n"
6734                    " * aaaaaa aaaaaa\n"
6735                    "*/",
6736                    getLLVMStyleWithColumns(10)));
6737   EXPECT_EQ("/*\n"
6738             "**\n"
6739             "* aaaaaa\n"
6740             "*aaaaaa\n"
6741             "*/",
6742             format("/*\n"
6743                    "**\n"
6744                    "* aaaaaa aaaaaa\n"
6745                    "*/",
6746                    getLLVMStyleWithColumns(10)));
6747 
6748   FormatStyle NoBinPacking = getLLVMStyle();
6749   NoBinPacking.BinPackParameters = false;
6750   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6751             "             2, /* comment 2 */\n"
6752             "             3, /* comment 3 */\n"
6753             "             aaaa,\n"
6754             "             bbbb);",
6755             format("someFunction (1,   /* comment 1 */\n"
6756                    "                2, /* comment 2 */  \n"
6757                    "               3,   /* comment 3 */\n"
6758                    "aaaa, bbbb );",
6759                    NoBinPacking));
6760   verifyFormat(
6761       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6762       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6763   EXPECT_EQ(
6764       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6765       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6766       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6767       format(
6768           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6769           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6770           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6771   EXPECT_EQ(
6772       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6773       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6774       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6775       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6776              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6777              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6778 
6779   verifyFormat("void f(int * /* unused */) {}");
6780 
6781   EXPECT_EQ("/*\n"
6782             " **\n"
6783             " */",
6784             format("/*\n"
6785                    " **\n"
6786                    " */"));
6787   EXPECT_EQ("/*\n"
6788             " *q\n"
6789             " */",
6790             format("/*\n"
6791                    " *q\n"
6792                    " */"));
6793   EXPECT_EQ("/*\n"
6794             " * q\n"
6795             " */",
6796             format("/*\n"
6797                    " * q\n"
6798                    " */"));
6799   EXPECT_EQ("/*\n"
6800             " **/",
6801             format("/*\n"
6802                    " **/"));
6803   EXPECT_EQ("/*\n"
6804             " ***/",
6805             format("/*\n"
6806                    " ***/"));
6807 }
6808 
6809 TEST_F(FormatTest, BlockCommentsInMacros) {
6810   EXPECT_EQ("#define A          \\\n"
6811             "  {                \\\n"
6812             "    /* one line */ \\\n"
6813             "    someCall();",
6814             format("#define A {        \\\n"
6815                    "  /* one line */   \\\n"
6816                    "  someCall();",
6817                    getLLVMStyleWithColumns(20)));
6818   EXPECT_EQ("#define A          \\\n"
6819             "  {                \\\n"
6820             "    /* previous */ \\\n"
6821             "    /* one line */ \\\n"
6822             "    someCall();",
6823             format("#define A {        \\\n"
6824                    "  /* previous */   \\\n"
6825                    "  /* one line */   \\\n"
6826                    "  someCall();",
6827                    getLLVMStyleWithColumns(20)));
6828 }
6829 
6830 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6831   EXPECT_EQ("a = {\n"
6832             "    1111 /*    */\n"
6833             "};",
6834             format("a = {1111 /*    */\n"
6835                    "};",
6836                    getLLVMStyleWithColumns(15)));
6837   EXPECT_EQ("a = {\n"
6838             "    1111 /*      */\n"
6839             "};",
6840             format("a = {1111 /*      */\n"
6841                    "};",
6842                    getLLVMStyleWithColumns(15)));
6843 
6844   // FIXME: The formatting is still wrong here.
6845   EXPECT_EQ("a = {\n"
6846             "    1111 /*      a\n"
6847             "            */\n"
6848             "};",
6849             format("a = {1111 /*      a */\n"
6850                    "};",
6851                    getLLVMStyleWithColumns(15)));
6852 }
6853 
6854 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6855   // FIXME: This is not what we want...
6856   verifyFormat("{\n"
6857                "// a"
6858                "// b");
6859 }
6860 
6861 TEST_F(FormatTest, FormatStarDependingOnContext) {
6862   verifyFormat("void f(int *a);");
6863   verifyFormat("void f() { f(fint * b); }");
6864   verifyFormat("class A {\n  void f(int *a);\n};");
6865   verifyFormat("class A {\n  int *a;\n};");
6866   verifyFormat("namespace a {\n"
6867                "namespace b {\n"
6868                "class A {\n"
6869                "  void f() {}\n"
6870                "  int *a;\n"
6871                "};\n"
6872                "}\n"
6873                "}");
6874 }
6875 
6876 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6877   verifyFormat("while");
6878   verifyFormat("operator");
6879 }
6880 
6881 //===----------------------------------------------------------------------===//
6882 // Objective-C tests.
6883 //===----------------------------------------------------------------------===//
6884 
6885 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6886   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6887   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6888             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6889   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6890   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6891   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6892             format("-(NSInteger)Method3:(id)anObject;"));
6893   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6894             format("-(NSInteger)Method4:(id)anObject;"));
6895   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6896             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6897   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6898             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6899   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6900             "forAllCells:(BOOL)flag;",
6901             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6902                    "forAllCells:(BOOL)flag;"));
6903 
6904   // Very long objectiveC method declaration.
6905   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6906                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6907   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6908                "                    inRange:(NSRange)range\n"
6909                "                   outRange:(NSRange)out_range\n"
6910                "                  outRange1:(NSRange)out_range1\n"
6911                "                  outRange2:(NSRange)out_range2\n"
6912                "                  outRange3:(NSRange)out_range3\n"
6913                "                  outRange4:(NSRange)out_range4\n"
6914                "                  outRange5:(NSRange)out_range5\n"
6915                "                  outRange6:(NSRange)out_range6\n"
6916                "                  outRange7:(NSRange)out_range7\n"
6917                "                  outRange8:(NSRange)out_range8\n"
6918                "                  outRange9:(NSRange)out_range9;");
6919 
6920   // When the function name has to be wrapped.
6921   FormatStyle Style = getLLVMStyle();
6922   Style.IndentWrappedFunctionNames = false;
6923   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6924                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6925                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6926                "}",
6927                Style);
6928   Style.IndentWrappedFunctionNames = true;
6929   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6930                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6931                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6932                "}",
6933                Style);
6934 
6935   verifyFormat("- (int)sum:(vector<int>)numbers;");
6936   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6937   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6938   // protocol lists (but not for template classes):
6939   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6940 
6941   verifyFormat("- (int (*)())foo:(int (*)())f;");
6942   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6943 
6944   // If there's no return type (very rare in practice!), LLVM and Google style
6945   // agree.
6946   verifyFormat("- foo;");
6947   verifyFormat("- foo:(int)f;");
6948   verifyGoogleFormat("- foo:(int)foo;");
6949 }
6950 
6951 TEST_F(FormatTest, FormatObjCInterface) {
6952   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6953                "@public\n"
6954                "  int field1;\n"
6955                "@protected\n"
6956                "  int field2;\n"
6957                "@private\n"
6958                "  int field3;\n"
6959                "@package\n"
6960                "  int field4;\n"
6961                "}\n"
6962                "+ (id)init;\n"
6963                "@end");
6964 
6965   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6966                      " @public\n"
6967                      "  int field1;\n"
6968                      " @protected\n"
6969                      "  int field2;\n"
6970                      " @private\n"
6971                      "  int field3;\n"
6972                      " @package\n"
6973                      "  int field4;\n"
6974                      "}\n"
6975                      "+ (id)init;\n"
6976                      "@end");
6977 
6978   verifyFormat("@interface /* wait for it */ Foo\n"
6979                "+ (id)init;\n"
6980                "// Look, a comment!\n"
6981                "- (int)answerWith:(int)i;\n"
6982                "@end");
6983 
6984   verifyFormat("@interface Foo\n"
6985                "@end\n"
6986                "@interface Bar\n"
6987                "@end");
6988 
6989   verifyFormat("@interface Foo : Bar\n"
6990                "+ (id)init;\n"
6991                "@end");
6992 
6993   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6994                "+ (id)init;\n"
6995                "@end");
6996 
6997   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6998                      "+ (id)init;\n"
6999                      "@end");
7000 
7001   verifyFormat("@interface Foo (HackStuff)\n"
7002                "+ (id)init;\n"
7003                "@end");
7004 
7005   verifyFormat("@interface Foo ()\n"
7006                "+ (id)init;\n"
7007                "@end");
7008 
7009   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7010                "+ (id)init;\n"
7011                "@end");
7012 
7013   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7014                      "+ (id)init;\n"
7015                      "@end");
7016 
7017   verifyFormat("@interface Foo {\n"
7018                "  int _i;\n"
7019                "}\n"
7020                "+ (id)init;\n"
7021                "@end");
7022 
7023   verifyFormat("@interface Foo : Bar {\n"
7024                "  int _i;\n"
7025                "}\n"
7026                "+ (id)init;\n"
7027                "@end");
7028 
7029   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7030                "  int _i;\n"
7031                "}\n"
7032                "+ (id)init;\n"
7033                "@end");
7034 
7035   verifyFormat("@interface Foo (HackStuff) {\n"
7036                "  int _i;\n"
7037                "}\n"
7038                "+ (id)init;\n"
7039                "@end");
7040 
7041   verifyFormat("@interface Foo () {\n"
7042                "  int _i;\n"
7043                "}\n"
7044                "+ (id)init;\n"
7045                "@end");
7046 
7047   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7048                "  int _i;\n"
7049                "}\n"
7050                "+ (id)init;\n"
7051                "@end");
7052 
7053   FormatStyle OnePerLine = getGoogleStyle();
7054   OnePerLine.BinPackParameters = false;
7055   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7056                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7057                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7058                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7059                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7060                "}",
7061                OnePerLine);
7062 }
7063 
7064 TEST_F(FormatTest, FormatObjCImplementation) {
7065   verifyFormat("@implementation Foo : NSObject {\n"
7066                "@public\n"
7067                "  int field1;\n"
7068                "@protected\n"
7069                "  int field2;\n"
7070                "@private\n"
7071                "  int field3;\n"
7072                "@package\n"
7073                "  int field4;\n"
7074                "}\n"
7075                "+ (id)init {\n}\n"
7076                "@end");
7077 
7078   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7079                      " @public\n"
7080                      "  int field1;\n"
7081                      " @protected\n"
7082                      "  int field2;\n"
7083                      " @private\n"
7084                      "  int field3;\n"
7085                      " @package\n"
7086                      "  int field4;\n"
7087                      "}\n"
7088                      "+ (id)init {\n}\n"
7089                      "@end");
7090 
7091   verifyFormat("@implementation Foo\n"
7092                "+ (id)init {\n"
7093                "  if (true)\n"
7094                "    return nil;\n"
7095                "}\n"
7096                "// Look, a comment!\n"
7097                "- (int)answerWith:(int)i {\n"
7098                "  return i;\n"
7099                "}\n"
7100                "+ (int)answerWith:(int)i {\n"
7101                "  return i;\n"
7102                "}\n"
7103                "@end");
7104 
7105   verifyFormat("@implementation Foo\n"
7106                "@end\n"
7107                "@implementation Bar\n"
7108                "@end");
7109 
7110   EXPECT_EQ("@implementation Foo : Bar\n"
7111             "+ (id)init {\n}\n"
7112             "- (void)foo {\n}\n"
7113             "@end",
7114             format("@implementation Foo : Bar\n"
7115                    "+(id)init{}\n"
7116                    "-(void)foo{}\n"
7117                    "@end"));
7118 
7119   verifyFormat("@implementation Foo {\n"
7120                "  int _i;\n"
7121                "}\n"
7122                "+ (id)init {\n}\n"
7123                "@end");
7124 
7125   verifyFormat("@implementation Foo : Bar {\n"
7126                "  int _i;\n"
7127                "}\n"
7128                "+ (id)init {\n}\n"
7129                "@end");
7130 
7131   verifyFormat("@implementation Foo (HackStuff)\n"
7132                "+ (id)init {\n}\n"
7133                "@end");
7134   verifyFormat("@implementation ObjcClass\n"
7135                "- (void)method;\n"
7136                "{}\n"
7137                "@end");
7138 }
7139 
7140 TEST_F(FormatTest, FormatObjCProtocol) {
7141   verifyFormat("@protocol Foo\n"
7142                "@property(weak) id delegate;\n"
7143                "- (NSUInteger)numberOfThings;\n"
7144                "@end");
7145 
7146   verifyFormat("@protocol MyProtocol <NSObject>\n"
7147                "- (NSUInteger)numberOfThings;\n"
7148                "@end");
7149 
7150   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7151                      "- (NSUInteger)numberOfThings;\n"
7152                      "@end");
7153 
7154   verifyFormat("@protocol Foo;\n"
7155                "@protocol Bar;\n");
7156 
7157   verifyFormat("@protocol Foo\n"
7158                "@end\n"
7159                "@protocol Bar\n"
7160                "@end");
7161 
7162   verifyFormat("@protocol myProtocol\n"
7163                "- (void)mandatoryWithInt:(int)i;\n"
7164                "@optional\n"
7165                "- (void)optional;\n"
7166                "@required\n"
7167                "- (void)required;\n"
7168                "@optional\n"
7169                "@property(assign) int madProp;\n"
7170                "@end\n");
7171 
7172   verifyFormat("@property(nonatomic, assign, readonly)\n"
7173                "    int *looooooooooooooooooooooooooooongNumber;\n"
7174                "@property(nonatomic, assign, readonly)\n"
7175                "    NSString *looooooooooooooooooooooooooooongName;");
7176 
7177   verifyFormat("@implementation PR18406\n"
7178                "}\n"
7179                "@end");
7180 }
7181 
7182 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7183   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7184                "                   rect:(NSRect)theRect\n"
7185                "               interval:(float)theInterval {\n"
7186                "}");
7187   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7188                "          longKeyword:(NSRect)theRect\n"
7189                "    evenLongerKeyword:(float)theInterval\n"
7190                "                error:(NSError **)theError {\n"
7191                "}");
7192   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7193                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7194                "    NS_DESIGNATED_INITIALIZER;",
7195                getLLVMStyleWithColumns(60));
7196 
7197   // Continuation indent width should win over aligning colons if the function
7198   // name is long.
7199   FormatStyle continuationStyle = getGoogleStyle();
7200   continuationStyle.ColumnLimit = 40;
7201   continuationStyle.IndentWrappedFunctionNames = true;
7202   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7203                "    dontAlignNamef:(NSRect)theRect {\n"
7204                "}",
7205                continuationStyle);
7206 
7207   // Make sure we don't break aligning for short parameter names.
7208   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7209                "       aShortf:(NSRect)theRect {\n"
7210                "}",
7211                continuationStyle);
7212 }
7213 
7214 TEST_F(FormatTest, FormatObjCMethodExpr) {
7215   verifyFormat("[foo bar:baz];");
7216   verifyFormat("return [foo bar:baz];");
7217   verifyFormat("return (a)[foo bar:baz];");
7218   verifyFormat("f([foo bar:baz]);");
7219   verifyFormat("f(2, [foo bar:baz]);");
7220   verifyFormat("f(2, a ? b : c);");
7221   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7222 
7223   // Unary operators.
7224   verifyFormat("int a = +[foo bar:baz];");
7225   verifyFormat("int a = -[foo bar:baz];");
7226   verifyFormat("int a = ![foo bar:baz];");
7227   verifyFormat("int a = ~[foo bar:baz];");
7228   verifyFormat("int a = ++[foo bar:baz];");
7229   verifyFormat("int a = --[foo bar:baz];");
7230   verifyFormat("int a = sizeof [foo bar:baz];");
7231   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7232   verifyFormat("int a = &[foo bar:baz];");
7233   verifyFormat("int a = *[foo bar:baz];");
7234   // FIXME: Make casts work, without breaking f()[4].
7235   // verifyFormat("int a = (int)[foo bar:baz];");
7236   // verifyFormat("return (int)[foo bar:baz];");
7237   // verifyFormat("(void)[foo bar:baz];");
7238   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7239 
7240   // Binary operators.
7241   verifyFormat("[foo bar:baz], [foo bar:baz];");
7242   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7243   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7244   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7245   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7246   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7247   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7248   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7249   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7250   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7251   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7252   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7253   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7254   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7255   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7256   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7257   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7258   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7259   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7260   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7261   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7262   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7263   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7264   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7265   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7266   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7267   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7268   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7269   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7270   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7271   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7272   // Whew!
7273 
7274   verifyFormat("return in[42];");
7275   verifyFormat("for (auto v : in[1]) {\n}");
7276   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7277   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7278   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7279   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7280   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7281   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7282                "}");
7283   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7284   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7285   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7286 
7287   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7288   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7289   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7290   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7291   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7292   verifyFormat("[button setAction:@selector(zoomOut:)];");
7293   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7294 
7295   verifyFormat("arr[[self indexForFoo:a]];");
7296   verifyFormat("throw [self errorFor:a];");
7297   verifyFormat("@throw [self errorFor:a];");
7298 
7299   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7300   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7301   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7302 
7303   // This tests that the formatter doesn't break after "backing" but before ":",
7304   // which would be at 80 columns.
7305   verifyFormat(
7306       "void f() {\n"
7307       "  if ((self = [super initWithContentRect:contentRect\n"
7308       "                               styleMask:styleMask ?: otherMask\n"
7309       "                                 backing:NSBackingStoreBuffered\n"
7310       "                                   defer:YES]))");
7311 
7312   verifyFormat(
7313       "[foo checkThatBreakingAfterColonWorksOk:\n"
7314       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7315 
7316   verifyFormat("[myObj short:arg1 // Force line break\n"
7317                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7318                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7319                "                error:arg4];");
7320   verifyFormat(
7321       "void f() {\n"
7322       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7323       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7324       "                                     pos.width(), pos.height())\n"
7325       "                styleMask:NSBorderlessWindowMask\n"
7326       "                  backing:NSBackingStoreBuffered\n"
7327       "                    defer:NO]);\n"
7328       "}");
7329   verifyFormat(
7330       "void f() {\n"
7331       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7332       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7333       "                                 pos.width(), pos.height())\n"
7334       "                syeMask:NSBorderlessWindowMask\n"
7335       "                  bking:NSBackingStoreBuffered\n"
7336       "                    der:NO]);\n"
7337       "}",
7338       getLLVMStyleWithColumns(70));
7339   verifyFormat(
7340       "void f() {\n"
7341       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7342       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7343       "                                     pos.width(), pos.height())\n"
7344       "                styleMask:NSBorderlessWindowMask\n"
7345       "                  backing:NSBackingStoreBuffered\n"
7346       "                    defer:NO]);\n"
7347       "}",
7348       getChromiumStyle(FormatStyle::LK_Cpp));
7349   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7350                "                             with:contentsNativeView];");
7351 
7352   verifyFormat(
7353       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7354       "           owner:nillllll];");
7355 
7356   verifyFormat(
7357       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7358       "        forType:kBookmarkButtonDragType];");
7359 
7360   verifyFormat("[defaultCenter addObserver:self\n"
7361                "                  selector:@selector(willEnterFullscreen)\n"
7362                "                      name:kWillEnterFullscreenNotification\n"
7363                "                    object:nil];");
7364   verifyFormat("[image_rep drawInRect:drawRect\n"
7365                "             fromRect:NSZeroRect\n"
7366                "            operation:NSCompositeCopy\n"
7367                "             fraction:1.0\n"
7368                "       respectFlipped:NO\n"
7369                "                hints:nil];");
7370   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7371                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7372   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7373                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7374   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7375                "    aaaaaaaaaaaaaaaaaaaaaa];");
7376   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7377                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7378                getLLVMStyleWithColumns(60));
7379 
7380   verifyFormat(
7381       "scoped_nsobject<NSTextField> message(\n"
7382       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7383       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7384   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7385                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7386                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7387                "          aaaa:bbb];");
7388   verifyFormat("[self param:function( //\n"
7389                "                parameter)]");
7390   verifyFormat(
7391       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7392       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7393       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7394 
7395   // FIXME: This violates the column limit.
7396   verifyFormat(
7397       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7398       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7399       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7400       getLLVMStyleWithColumns(60));
7401 
7402   // Variadic parameters.
7403   verifyFormat(
7404       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7405   verifyFormat(
7406       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7407       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7408       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7409   verifyFormat("[self // break\n"
7410                "      a:a\n"
7411                "    aaa:aaa];");
7412   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7413                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7414 }
7415 
7416 TEST_F(FormatTest, ObjCAt) {
7417   verifyFormat("@autoreleasepool");
7418   verifyFormat("@catch");
7419   verifyFormat("@class");
7420   verifyFormat("@compatibility_alias");
7421   verifyFormat("@defs");
7422   verifyFormat("@dynamic");
7423   verifyFormat("@encode");
7424   verifyFormat("@end");
7425   verifyFormat("@finally");
7426   verifyFormat("@implementation");
7427   verifyFormat("@import");
7428   verifyFormat("@interface");
7429   verifyFormat("@optional");
7430   verifyFormat("@package");
7431   verifyFormat("@private");
7432   verifyFormat("@property");
7433   verifyFormat("@protected");
7434   verifyFormat("@protocol");
7435   verifyFormat("@public");
7436   verifyFormat("@required");
7437   verifyFormat("@selector");
7438   verifyFormat("@synchronized");
7439   verifyFormat("@synthesize");
7440   verifyFormat("@throw");
7441   verifyFormat("@try");
7442 
7443   EXPECT_EQ("@interface", format("@ interface"));
7444 
7445   // The precise formatting of this doesn't matter, nobody writes code like
7446   // this.
7447   verifyFormat("@ /*foo*/ interface");
7448 }
7449 
7450 TEST_F(FormatTest, ObjCSnippets) {
7451   verifyFormat("@autoreleasepool {\n"
7452                "  foo();\n"
7453                "}");
7454   verifyFormat("@class Foo, Bar;");
7455   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7456   verifyFormat("@dynamic textColor;");
7457   verifyFormat("char *buf1 = @encode(int *);");
7458   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7459   verifyFormat("char *buf1 = @encode(int **);");
7460   verifyFormat("Protocol *proto = @protocol(p1);");
7461   verifyFormat("SEL s = @selector(foo:);");
7462   verifyFormat("@synchronized(self) {\n"
7463                "  f();\n"
7464                "}");
7465 
7466   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7467   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7468 
7469   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7470   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7471   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7472   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7473                getMozillaStyle());
7474   verifyFormat("@property BOOL editable;", getMozillaStyle());
7475   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7476                getWebKitStyle());
7477   verifyFormat("@property BOOL editable;", getWebKitStyle());
7478 
7479   verifyFormat("@import foo.bar;\n"
7480                "@import baz;");
7481 }
7482 
7483 TEST_F(FormatTest, ObjCForIn) {
7484   verifyFormat("- (void)test {\n"
7485                "  for (NSString *n in arrayOfStrings) {\n"
7486                "    foo(n);\n"
7487                "  }\n"
7488                "}");
7489   verifyFormat("- (void)test {\n"
7490                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7491                "    foo(n);\n"
7492                "  }\n"
7493                "}");
7494 }
7495 
7496 TEST_F(FormatTest, ObjCLiterals) {
7497   verifyFormat("@\"String\"");
7498   verifyFormat("@1");
7499   verifyFormat("@+4.8");
7500   verifyFormat("@-4");
7501   verifyFormat("@1LL");
7502   verifyFormat("@.5");
7503   verifyFormat("@'c'");
7504   verifyFormat("@true");
7505 
7506   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7507   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7508   verifyFormat("NSNumber *favoriteColor = @(Green);");
7509   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7510 
7511   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7512 }
7513 
7514 TEST_F(FormatTest, ObjCDictLiterals) {
7515   verifyFormat("@{");
7516   verifyFormat("@{}");
7517   verifyFormat("@{@\"one\" : @1}");
7518   verifyFormat("return @{@\"one\" : @1;");
7519   verifyFormat("@{@\"one\" : @1}");
7520 
7521   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7522   verifyFormat("@{\n"
7523                "  @\"one\" : @{@2 : @1},\n"
7524                "}");
7525 
7526   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7527   verifyIncompleteFormat("[self setDict:@{}");
7528   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7529   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7530   verifyFormat(
7531       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7532   verifyFormat(
7533       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7534 
7535   verifyFormat("NSDictionary *d = @{\n"
7536                "  @\"nam\" : NSUserNam(),\n"
7537                "  @\"dte\" : [NSDate date],\n"
7538                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7539                "};");
7540   verifyFormat(
7541       "@{\n"
7542       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7543       "regularFont,\n"
7544       "};");
7545   verifyGoogleFormat(
7546       "@{\n"
7547       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7548       "regularFont,\n"
7549       "};");
7550   verifyFormat(
7551       "@{\n"
7552       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7553       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7554       "};");
7555 
7556   // We should try to be robust in case someone forgets the "@".
7557   verifyFormat("NSDictionary *d = {\n"
7558                "  @\"nam\" : NSUserNam(),\n"
7559                "  @\"dte\" : [NSDate date],\n"
7560                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7561                "};");
7562   verifyFormat("NSMutableDictionary *dictionary =\n"
7563                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7564                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7565                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7566                "      cccccccccccccccc : ccccccccccccccc\n"
7567                "    }];");
7568 
7569   // Ensure that casts before the key are kept on the same line as the key.
7570   verifyFormat(
7571       "NSDictionary *d = @{\n"
7572       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7573       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7574       "};");
7575 }
7576 
7577 TEST_F(FormatTest, ObjCArrayLiterals) {
7578   verifyIncompleteFormat("@[");
7579   verifyFormat("@[]");
7580   verifyFormat(
7581       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7582   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7583   verifyFormat("NSArray *array = @[ [foo description] ];");
7584 
7585   verifyFormat(
7586       "NSArray *some_variable = @[\n"
7587       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7588       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7589       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7590       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7591       "];");
7592   verifyFormat("NSArray *some_variable = @[\n"
7593                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7594                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7595                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7596                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7597                "];");
7598   verifyGoogleFormat("NSArray *some_variable = @[\n"
7599                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7600                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7601                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7602                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7603                      "];");
7604   verifyFormat("NSArray *array = @[\n"
7605                "  @\"a\",\n"
7606                "  @\"a\",\n" // Trailing comma -> one per line.
7607                "];");
7608 
7609   // We should try to be robust in case someone forgets the "@".
7610   verifyFormat("NSArray *some_variable = [\n"
7611                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7612                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7613                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7614                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7615                "];");
7616   verifyFormat(
7617       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7618       "                                             index:(NSUInteger)index\n"
7619       "                                nonDigitAttributes:\n"
7620       "                                    (NSDictionary *)noDigitAttributes;");
7621   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7622                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7623                "]];");
7624 }
7625 
7626 TEST_F(FormatTest, BreaksStringLiterals) {
7627   EXPECT_EQ("\"some text \"\n"
7628             "\"other\";",
7629             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7630   EXPECT_EQ("\"some text \"\n"
7631             "\"other\";",
7632             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7633   EXPECT_EQ(
7634       "#define A  \\\n"
7635       "  \"some \"  \\\n"
7636       "  \"text \"  \\\n"
7637       "  \"other\";",
7638       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7639   EXPECT_EQ(
7640       "#define A  \\\n"
7641       "  \"so \"    \\\n"
7642       "  \"text \"  \\\n"
7643       "  \"other\";",
7644       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7645 
7646   EXPECT_EQ("\"some text\"",
7647             format("\"some text\"", getLLVMStyleWithColumns(1)));
7648   EXPECT_EQ("\"some text\"",
7649             format("\"some text\"", getLLVMStyleWithColumns(11)));
7650   EXPECT_EQ("\"some \"\n"
7651             "\"text\"",
7652             format("\"some text\"", getLLVMStyleWithColumns(10)));
7653   EXPECT_EQ("\"some \"\n"
7654             "\"text\"",
7655             format("\"some text\"", getLLVMStyleWithColumns(7)));
7656   EXPECT_EQ("\"some\"\n"
7657             "\" tex\"\n"
7658             "\"t\"",
7659             format("\"some text\"", getLLVMStyleWithColumns(6)));
7660   EXPECT_EQ("\"some\"\n"
7661             "\" tex\"\n"
7662             "\" and\"",
7663             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7664   EXPECT_EQ("\"some\"\n"
7665             "\"/tex\"\n"
7666             "\"/and\"",
7667             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7668 
7669   EXPECT_EQ("variable =\n"
7670             "    \"long string \"\n"
7671             "    \"literal\";",
7672             format("variable = \"long string literal\";",
7673                    getLLVMStyleWithColumns(20)));
7674 
7675   EXPECT_EQ("variable = f(\n"
7676             "    \"long string \"\n"
7677             "    \"literal\",\n"
7678             "    short,\n"
7679             "    loooooooooooooooooooong);",
7680             format("variable = f(\"long string literal\", short, "
7681                    "loooooooooooooooooooong);",
7682                    getLLVMStyleWithColumns(20)));
7683 
7684   EXPECT_EQ(
7685       "f(g(\"long string \"\n"
7686       "    \"literal\"),\n"
7687       "  b);",
7688       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7689   EXPECT_EQ("f(g(\"long string \"\n"
7690             "    \"literal\",\n"
7691             "    a),\n"
7692             "  b);",
7693             format("f(g(\"long string literal\", a), b);",
7694                    getLLVMStyleWithColumns(20)));
7695   EXPECT_EQ(
7696       "f(\"one two\".split(\n"
7697       "    variable));",
7698       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7699   EXPECT_EQ("f(\"one two three four five six \"\n"
7700             "  \"seven\".split(\n"
7701             "      really_looooong_variable));",
7702             format("f(\"one two three four five six seven\"."
7703                    "split(really_looooong_variable));",
7704                    getLLVMStyleWithColumns(33)));
7705 
7706   EXPECT_EQ("f(\"some \"\n"
7707             "  \"text\",\n"
7708             "  other);",
7709             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7710 
7711   // Only break as a last resort.
7712   verifyFormat(
7713       "aaaaaaaaaaaaaaaaaaaa(\n"
7714       "    aaaaaaaaaaaaaaaaaaaa,\n"
7715       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7716 
7717   EXPECT_EQ("\"splitmea\"\n"
7718             "\"trandomp\"\n"
7719             "\"oint\"",
7720             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7721 
7722   EXPECT_EQ("\"split/\"\n"
7723             "\"pathat/\"\n"
7724             "\"slashes\"",
7725             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7726 
7727   EXPECT_EQ("\"split/\"\n"
7728             "\"pathat/\"\n"
7729             "\"slashes\"",
7730             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7731   EXPECT_EQ("\"split at \"\n"
7732             "\"spaces/at/\"\n"
7733             "\"slashes.at.any$\"\n"
7734             "\"non-alphanumeric%\"\n"
7735             "\"1111111111characte\"\n"
7736             "\"rs\"",
7737             format("\"split at "
7738                    "spaces/at/"
7739                    "slashes.at."
7740                    "any$non-"
7741                    "alphanumeric%"
7742                    "1111111111characte"
7743                    "rs\"",
7744                    getLLVMStyleWithColumns(20)));
7745 
7746   // Verify that splitting the strings understands
7747   // Style::AlwaysBreakBeforeMultilineStrings.
7748   EXPECT_EQ(
7749       "aaaaaaaaaaaa(\n"
7750       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7751       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7752       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7753              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7754              "aaaaaaaaaaaaaaaaaaaaaa\");",
7755              getGoogleStyle()));
7756   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7757             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7758             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7759                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7760                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7761                    getGoogleStyle()));
7762   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7763             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7764             format("llvm::outs() << "
7765                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7766                    "aaaaaaaaaaaaaaaaaaa\";"));
7767   EXPECT_EQ("ffff(\n"
7768             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7769             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7770             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7771                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7772                    getGoogleStyle()));
7773 
7774   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7775   AlignLeft.AlignEscapedNewlinesLeft = true;
7776   EXPECT_EQ("#define A \\\n"
7777             "  \"some \" \\\n"
7778             "  \"text \" \\\n"
7779             "  \"other\";",
7780             format("#define A \"some text other\";", AlignLeft));
7781 }
7782 
7783 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7784   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7785   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7786   EXPECT_EQ("int i = a(b());",
7787             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7788 }
7789 
7790 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7791   EXPECT_EQ(
7792       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7793       "(\n"
7794       "    \"x\t\");",
7795       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7796              "aaaaaaa("
7797              "\"x\t\");"));
7798 }
7799 
7800 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7801   EXPECT_EQ(
7802       "u8\"utf8 string \"\n"
7803       "u8\"literal\";",
7804       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7805   EXPECT_EQ(
7806       "u\"utf16 string \"\n"
7807       "u\"literal\";",
7808       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7809   EXPECT_EQ(
7810       "U\"utf32 string \"\n"
7811       "U\"literal\";",
7812       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7813   EXPECT_EQ("L\"wide string \"\n"
7814             "L\"literal\";",
7815             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7816   EXPECT_EQ("@\"NSString \"\n"
7817             "@\"literal\";",
7818             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7819 
7820   // This input makes clang-format try to split the incomplete unicode escape
7821   // sequence, which used to lead to a crasher.
7822   verifyNoCrash(
7823       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7824       getLLVMStyleWithColumns(60));
7825 }
7826 
7827 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7828   FormatStyle Style = getGoogleStyleWithColumns(15);
7829   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7830   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7831   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7832   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7833   EXPECT_EQ("u8R\"x(raw literal)x\";",
7834             format("u8R\"x(raw literal)x\";", Style));
7835 }
7836 
7837 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7838   FormatStyle Style = getLLVMStyleWithColumns(20);
7839   EXPECT_EQ(
7840       "_T(\"aaaaaaaaaaaaaa\")\n"
7841       "_T(\"aaaaaaaaaaaaaa\")\n"
7842       "_T(\"aaaaaaaaaaaa\")",
7843       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7844   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7845             "     _T(\"aaaaaa\"),\n"
7846             "  z);",
7847             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7848 
7849   // FIXME: Handle embedded spaces in one iteration.
7850   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7851   //            "_T(\"aaaaaaaaaaaaa\")\n"
7852   //            "_T(\"aaaaaaaaaaaaa\")\n"
7853   //            "_T(\"a\")",
7854   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7855   //                   getLLVMStyleWithColumns(20)));
7856   EXPECT_EQ(
7857       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7858       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7859   EXPECT_EQ("f(\n"
7860             "#if !TEST\n"
7861             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7862             "#endif\n"
7863             "    );",
7864             format("f(\n"
7865                    "#if !TEST\n"
7866                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7867                    "#endif\n"
7868                    ");"));
7869   EXPECT_EQ("f(\n"
7870             "\n"
7871             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7872             format("f(\n"
7873                    "\n"
7874                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7875 }
7876 
7877 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7878   EXPECT_EQ(
7879       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7880       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7881       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7882       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7883              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7884              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7885 }
7886 
7887 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7888   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7889             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7890   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7891             "multiline raw string literal xxxxxxxxxxxxxx\n"
7892             ")x\",\n"
7893             "              a),\n"
7894             "            b);",
7895             format("fffffffffff(g(R\"x(\n"
7896                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7897                    ")x\", a), b);",
7898                    getGoogleStyleWithColumns(20)));
7899   EXPECT_EQ("fffffffffff(\n"
7900             "    g(R\"x(qqq\n"
7901             "multiline raw string literal xxxxxxxxxxxxxx\n"
7902             ")x\",\n"
7903             "      a),\n"
7904             "    b);",
7905             format("fffffffffff(g(R\"x(qqq\n"
7906                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7907                    ")x\", a), b);",
7908                    getGoogleStyleWithColumns(20)));
7909 
7910   EXPECT_EQ("fffffffffff(R\"x(\n"
7911             "multiline raw string literal xxxxxxxxxxxxxx\n"
7912             ")x\");",
7913             format("fffffffffff(R\"x(\n"
7914                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7915                    ")x\");",
7916                    getGoogleStyleWithColumns(20)));
7917   EXPECT_EQ("fffffffffff(R\"x(\n"
7918             "multiline raw string literal xxxxxxxxxxxxxx\n"
7919             ")x\" + bbbbbb);",
7920             format("fffffffffff(R\"x(\n"
7921                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7922                    ")x\" +   bbbbbb);",
7923                    getGoogleStyleWithColumns(20)));
7924   EXPECT_EQ("fffffffffff(\n"
7925             "    R\"x(\n"
7926             "multiline raw string literal xxxxxxxxxxxxxx\n"
7927             ")x\" +\n"
7928             "    bbbbbb);",
7929             format("fffffffffff(\n"
7930                    " R\"x(\n"
7931                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7932                    ")x\" + bbbbbb);",
7933                    getGoogleStyleWithColumns(20)));
7934 }
7935 
7936 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7937   verifyFormat("string a = \"unterminated;");
7938   EXPECT_EQ("function(\"unterminated,\n"
7939             "         OtherParameter);",
7940             format("function(  \"unterminated,\n"
7941                    "    OtherParameter);"));
7942 }
7943 
7944 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7945   FormatStyle Style = getLLVMStyle();
7946   Style.Standard = FormatStyle::LS_Cpp03;
7947   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7948             format("#define x(_a) printf(\"foo\"_a);", Style));
7949 }
7950 
7951 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7952 
7953 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7954   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7955             "             \"ddeeefff\");",
7956             format("someFunction(\"aaabbbcccdddeeefff\");",
7957                    getLLVMStyleWithColumns(25)));
7958   EXPECT_EQ("someFunction1234567890(\n"
7959             "    \"aaabbbcccdddeeefff\");",
7960             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7961                    getLLVMStyleWithColumns(26)));
7962   EXPECT_EQ("someFunction1234567890(\n"
7963             "    \"aaabbbcccdddeeeff\"\n"
7964             "    \"f\");",
7965             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7966                    getLLVMStyleWithColumns(25)));
7967   EXPECT_EQ("someFunction1234567890(\n"
7968             "    \"aaabbbcccdddeeeff\"\n"
7969             "    \"f\");",
7970             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7971                    getLLVMStyleWithColumns(24)));
7972   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7973             "             \"ddde \"\n"
7974             "             \"efff\");",
7975             format("someFunction(\"aaabbbcc ddde efff\");",
7976                    getLLVMStyleWithColumns(25)));
7977   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7978             "             \"ddeeefff\");",
7979             format("someFunction(\"aaabbbccc ddeeefff\");",
7980                    getLLVMStyleWithColumns(25)));
7981   EXPECT_EQ("someFunction1234567890(\n"
7982             "    \"aaabb \"\n"
7983             "    \"cccdddeeefff\");",
7984             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7985                    getLLVMStyleWithColumns(25)));
7986   EXPECT_EQ("#define A          \\\n"
7987             "  string s =       \\\n"
7988             "      \"123456789\"  \\\n"
7989             "      \"0\";         \\\n"
7990             "  int i;",
7991             format("#define A string s = \"1234567890\"; int i;",
7992                    getLLVMStyleWithColumns(20)));
7993   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7994   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7995             "             \"dddeeeff\"\n"
7996             "             \"f\");",
7997             format("someFunction(\"aaabbbcc dddeeefff\");",
7998                    getLLVMStyleWithColumns(25)));
7999 }
8000 
8001 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8002   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8003   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8004   EXPECT_EQ("\"test\"\n"
8005             "\"\\n\"",
8006             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8007   EXPECT_EQ("\"tes\\\\\"\n"
8008             "\"n\"",
8009             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8010   EXPECT_EQ("\"\\\\\\\\\"\n"
8011             "\"\\n\"",
8012             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8013   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8014   EXPECT_EQ("\"\\uff01\"\n"
8015             "\"test\"",
8016             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8017   EXPECT_EQ("\"\\Uff01ff02\"",
8018             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8019   EXPECT_EQ("\"\\x000000000001\"\n"
8020             "\"next\"",
8021             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8022   EXPECT_EQ("\"\\x000000000001next\"",
8023             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8024   EXPECT_EQ("\"\\x000000000001\"",
8025             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8026   EXPECT_EQ("\"test\"\n"
8027             "\"\\000000\"\n"
8028             "\"000001\"",
8029             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8030   EXPECT_EQ("\"test\\000\"\n"
8031             "\"00000000\"\n"
8032             "\"1\"",
8033             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8034 }
8035 
8036 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8037   verifyFormat("void f() {\n"
8038                "  return g() {}\n"
8039                "  void h() {}");
8040   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8041                "g();\n"
8042                "}");
8043 }
8044 
8045 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8046   verifyFormat(
8047       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8048 }
8049 
8050 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8051   verifyFormat("class X {\n"
8052                "  void f() {\n"
8053                "  }\n"
8054                "};",
8055                getLLVMStyleWithColumns(12));
8056 }
8057 
8058 TEST_F(FormatTest, ConfigurableIndentWidth) {
8059   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8060   EightIndent.IndentWidth = 8;
8061   EightIndent.ContinuationIndentWidth = 8;
8062   verifyFormat("void f() {\n"
8063                "        someFunction();\n"
8064                "        if (true) {\n"
8065                "                f();\n"
8066                "        }\n"
8067                "}",
8068                EightIndent);
8069   verifyFormat("class X {\n"
8070                "        void f() {\n"
8071                "        }\n"
8072                "};",
8073                EightIndent);
8074   verifyFormat("int x[] = {\n"
8075                "        call(),\n"
8076                "        call()};",
8077                EightIndent);
8078 }
8079 
8080 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8081   verifyFormat("double\n"
8082                "f();",
8083                getLLVMStyleWithColumns(8));
8084 }
8085 
8086 TEST_F(FormatTest, ConfigurableUseOfTab) {
8087   FormatStyle Tab = getLLVMStyleWithColumns(42);
8088   Tab.IndentWidth = 8;
8089   Tab.UseTab = FormatStyle::UT_Always;
8090   Tab.AlignEscapedNewlinesLeft = true;
8091 
8092   EXPECT_EQ("if (aaaaaaaa && // q\n"
8093             "    bb)\t\t// w\n"
8094             "\t;",
8095             format("if (aaaaaaaa &&// q\n"
8096                    "bb)// w\n"
8097                    ";",
8098                    Tab));
8099   EXPECT_EQ("if (aaa && bbb) // w\n"
8100             "\t;",
8101             format("if(aaa&&bbb)// w\n"
8102                    ";",
8103                    Tab));
8104 
8105   verifyFormat("class X {\n"
8106                "\tvoid f() {\n"
8107                "\t\tsomeFunction(parameter1,\n"
8108                "\t\t\t     parameter2);\n"
8109                "\t}\n"
8110                "};",
8111                Tab);
8112   verifyFormat("#define A                        \\\n"
8113                "\tvoid f() {               \\\n"
8114                "\t\tsomeFunction(    \\\n"
8115                "\t\t    parameter1,  \\\n"
8116                "\t\t    parameter2); \\\n"
8117                "\t}",
8118                Tab);
8119 
8120   Tab.TabWidth = 4;
8121   Tab.IndentWidth = 8;
8122   verifyFormat("class TabWidth4Indent8 {\n"
8123                "\t\tvoid f() {\n"
8124                "\t\t\t\tsomeFunction(parameter1,\n"
8125                "\t\t\t\t\t\t\t parameter2);\n"
8126                "\t\t}\n"
8127                "};",
8128                Tab);
8129 
8130   Tab.TabWidth = 4;
8131   Tab.IndentWidth = 4;
8132   verifyFormat("class TabWidth4Indent4 {\n"
8133                "\tvoid f() {\n"
8134                "\t\tsomeFunction(parameter1,\n"
8135                "\t\t\t\t\t parameter2);\n"
8136                "\t}\n"
8137                "};",
8138                Tab);
8139 
8140   Tab.TabWidth = 8;
8141   Tab.IndentWidth = 4;
8142   verifyFormat("class TabWidth8Indent4 {\n"
8143                "    void f() {\n"
8144                "\tsomeFunction(parameter1,\n"
8145                "\t\t     parameter2);\n"
8146                "    }\n"
8147                "};",
8148                Tab);
8149 
8150   Tab.TabWidth = 8;
8151   Tab.IndentWidth = 8;
8152   EXPECT_EQ("/*\n"
8153             "\t      a\t\tcomment\n"
8154             "\t      in multiple lines\n"
8155             "       */",
8156             format("   /*\t \t \n"
8157                    " \t \t a\t\tcomment\t \t\n"
8158                    " \t \t in multiple lines\t\n"
8159                    " \t  */",
8160                    Tab));
8161 
8162   Tab.UseTab = FormatStyle::UT_ForIndentation;
8163   verifyFormat("{\n"
8164                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8165                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8166                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8167                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8168                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8169                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8170                "};",
8171                Tab);
8172   verifyFormat("enum A {\n"
8173                "\ta1, // Force multiple lines\n"
8174                "\ta2,\n"
8175                "\ta3\n"
8176                "};",
8177                Tab);
8178   EXPECT_EQ("if (aaaaaaaa && // q\n"
8179             "    bb)         // w\n"
8180             "\t;",
8181             format("if (aaaaaaaa &&// q\n"
8182                    "bb)// w\n"
8183                    ";",
8184                    Tab));
8185   verifyFormat("class X {\n"
8186                "\tvoid f() {\n"
8187                "\t\tsomeFunction(parameter1,\n"
8188                "\t\t             parameter2);\n"
8189                "\t}\n"
8190                "};",
8191                Tab);
8192   verifyFormat("{\n"
8193                "\tQ(\n"
8194                "\t    {\n"
8195                "\t\t    int a;\n"
8196                "\t\t    someFunction(aaaaaaaa,\n"
8197                "\t\t                 bbbbbbb);\n"
8198                "\t    },\n"
8199                "\t    p);\n"
8200                "}",
8201                Tab);
8202   EXPECT_EQ("{\n"
8203             "\t/* aaaa\n"
8204             "\t   bbbb */\n"
8205             "}",
8206             format("{\n"
8207                    "/* aaaa\n"
8208                    "   bbbb */\n"
8209                    "}",
8210                    Tab));
8211   EXPECT_EQ("{\n"
8212             "\t/*\n"
8213             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8214             "\t  bbbbbbbbbbbbb\n"
8215             "\t*/\n"
8216             "}",
8217             format("{\n"
8218                    "/*\n"
8219                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8220                    "*/\n"
8221                    "}",
8222                    Tab));
8223   EXPECT_EQ("{\n"
8224             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8225             "\t// bbbbbbbbbbbbb\n"
8226             "}",
8227             format("{\n"
8228                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8229                    "}",
8230                    Tab));
8231   EXPECT_EQ("{\n"
8232             "\t/*\n"
8233             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8234             "\t  bbbbbbbbbbbbb\n"
8235             "\t*/\n"
8236             "}",
8237             format("{\n"
8238                    "\t/*\n"
8239                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8240                    "\t*/\n"
8241                    "}",
8242                    Tab));
8243   EXPECT_EQ("{\n"
8244             "\t/*\n"
8245             "\n"
8246             "\t*/\n"
8247             "}",
8248             format("{\n"
8249                    "\t/*\n"
8250                    "\n"
8251                    "\t*/\n"
8252                    "}",
8253                    Tab));
8254   EXPECT_EQ("{\n"
8255             "\t/*\n"
8256             " asdf\n"
8257             "\t*/\n"
8258             "}",
8259             format("{\n"
8260                    "\t/*\n"
8261                    " asdf\n"
8262                    "\t*/\n"
8263                    "}",
8264                    Tab));
8265 
8266   Tab.UseTab = FormatStyle::UT_Never;
8267   EXPECT_EQ("/*\n"
8268             "              a\t\tcomment\n"
8269             "              in multiple lines\n"
8270             "       */",
8271             format("   /*\t \t \n"
8272                    " \t \t a\t\tcomment\t \t\n"
8273                    " \t \t in multiple lines\t\n"
8274                    " \t  */",
8275                    Tab));
8276   EXPECT_EQ("/* some\n"
8277             "   comment */",
8278             format(" \t \t /* some\n"
8279                    " \t \t    comment */",
8280                    Tab));
8281   EXPECT_EQ("int a; /* some\n"
8282             "   comment */",
8283             format(" \t \t int a; /* some\n"
8284                    " \t \t    comment */",
8285                    Tab));
8286 
8287   EXPECT_EQ("int a; /* some\n"
8288             "comment */",
8289             format(" \t \t int\ta; /* some\n"
8290                    " \t \t    comment */",
8291                    Tab));
8292   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8293             "    comment */",
8294             format(" \t \t f(\"\t\t\"); /* some\n"
8295                    " \t \t    comment */",
8296                    Tab));
8297   EXPECT_EQ("{\n"
8298             "  /*\n"
8299             "   * Comment\n"
8300             "   */\n"
8301             "  int i;\n"
8302             "}",
8303             format("{\n"
8304                    "\t/*\n"
8305                    "\t * Comment\n"
8306                    "\t */\n"
8307                    "\t int i;\n"
8308                    "}"));
8309 }
8310 
8311 TEST_F(FormatTest, CalculatesOriginalColumn) {
8312   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8313             "q\"; /* some\n"
8314             "       comment */",
8315             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8316                    "q\"; /* some\n"
8317                    "       comment */",
8318                    getLLVMStyle()));
8319   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8320             "/* some\n"
8321             "   comment */",
8322             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8323                    " /* some\n"
8324                    "    comment */",
8325                    getLLVMStyle()));
8326   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8327             "qqq\n"
8328             "/* some\n"
8329             "   comment */",
8330             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8331                    "qqq\n"
8332                    " /* some\n"
8333                    "    comment */",
8334                    getLLVMStyle()));
8335   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8336             "wwww; /* some\n"
8337             "         comment */",
8338             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8339                    "wwww; /* some\n"
8340                    "         comment */",
8341                    getLLVMStyle()));
8342 }
8343 
8344 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8345   FormatStyle NoSpace = getLLVMStyle();
8346   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8347 
8348   verifyFormat("while(true)\n"
8349                "  continue;",
8350                NoSpace);
8351   verifyFormat("for(;;)\n"
8352                "  continue;",
8353                NoSpace);
8354   verifyFormat("if(true)\n"
8355                "  f();\n"
8356                "else if(true)\n"
8357                "  f();",
8358                NoSpace);
8359   verifyFormat("do {\n"
8360                "  do_something();\n"
8361                "} while(something());",
8362                NoSpace);
8363   verifyFormat("switch(x) {\n"
8364                "default:\n"
8365                "  break;\n"
8366                "}",
8367                NoSpace);
8368   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8369   verifyFormat("size_t x = sizeof(x);", NoSpace);
8370   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8371   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8372   verifyFormat("alignas(128) char a[128];", NoSpace);
8373   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8374   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8375   verifyFormat("int f() throw(Deprecated);", NoSpace);
8376   verifyFormat("typedef void (*cb)(int);", NoSpace);
8377   verifyFormat("T A::operator()();", NoSpace);
8378   verifyFormat("X A::operator++(T);", NoSpace);
8379 
8380   FormatStyle Space = getLLVMStyle();
8381   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8382 
8383   verifyFormat("int f ();", Space);
8384   verifyFormat("void f (int a, T b) {\n"
8385                "  while (true)\n"
8386                "    continue;\n"
8387                "}",
8388                Space);
8389   verifyFormat("if (true)\n"
8390                "  f ();\n"
8391                "else if (true)\n"
8392                "  f ();",
8393                Space);
8394   verifyFormat("do {\n"
8395                "  do_something ();\n"
8396                "} while (something ());",
8397                Space);
8398   verifyFormat("switch (x) {\n"
8399                "default:\n"
8400                "  break;\n"
8401                "}",
8402                Space);
8403   verifyFormat("A::A () : a (1) {}", Space);
8404   verifyFormat("void f () __attribute__ ((asdf));", Space);
8405   verifyFormat("*(&a + 1);\n"
8406                "&((&a)[1]);\n"
8407                "a[(b + c) * d];\n"
8408                "(((a + 1) * 2) + 3) * 4;",
8409                Space);
8410   verifyFormat("#define A(x) x", Space);
8411   verifyFormat("#define A (x) x", Space);
8412   verifyFormat("#if defined(x)\n"
8413                "#endif",
8414                Space);
8415   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8416   verifyFormat("size_t x = sizeof (x);", Space);
8417   verifyFormat("auto f (int x) -> decltype (x);", Space);
8418   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8419   verifyFormat("alignas (128) char a[128];", Space);
8420   verifyFormat("size_t x = alignof (MyType);", Space);
8421   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8422   verifyFormat("int f () throw (Deprecated);", Space);
8423   verifyFormat("typedef void (*cb) (int);", Space);
8424   verifyFormat("T A::operator() ();", Space);
8425   verifyFormat("X A::operator++ (T);", Space);
8426 }
8427 
8428 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8429   FormatStyle Spaces = getLLVMStyle();
8430 
8431   Spaces.SpacesInParentheses = true;
8432   verifyFormat("call( x, y, z );", Spaces);
8433   verifyFormat("call();", Spaces);
8434   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8435   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8436                Spaces);
8437   verifyFormat("while ( (bool)1 )\n"
8438                "  continue;",
8439                Spaces);
8440   verifyFormat("for ( ;; )\n"
8441                "  continue;",
8442                Spaces);
8443   verifyFormat("if ( true )\n"
8444                "  f();\n"
8445                "else if ( true )\n"
8446                "  f();",
8447                Spaces);
8448   verifyFormat("do {\n"
8449                "  do_something( (int)i );\n"
8450                "} while ( something() );",
8451                Spaces);
8452   verifyFormat("switch ( x ) {\n"
8453                "default:\n"
8454                "  break;\n"
8455                "}",
8456                Spaces);
8457 
8458   Spaces.SpacesInParentheses = false;
8459   Spaces.SpacesInCStyleCastParentheses = true;
8460   verifyFormat("Type *A = ( Type * )P;", Spaces);
8461   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8462   verifyFormat("x = ( int32 )y;", Spaces);
8463   verifyFormat("int a = ( int )(2.0f);", Spaces);
8464   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8465   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8466   verifyFormat("#define x (( int )-1)", Spaces);
8467 
8468   // Run the first set of tests again with:
8469   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8470   Spaces.SpacesInCStyleCastParentheses = true;
8471   verifyFormat("call(x, y, z);", Spaces);
8472   verifyFormat("call( );", Spaces);
8473   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8474   verifyFormat("while (( bool )1)\n"
8475                "  continue;",
8476                Spaces);
8477   verifyFormat("for (;;)\n"
8478                "  continue;",
8479                Spaces);
8480   verifyFormat("if (true)\n"
8481                "  f( );\n"
8482                "else if (true)\n"
8483                "  f( );",
8484                Spaces);
8485   verifyFormat("do {\n"
8486                "  do_something(( int )i);\n"
8487                "} while (something( ));",
8488                Spaces);
8489   verifyFormat("switch (x) {\n"
8490                "default:\n"
8491                "  break;\n"
8492                "}",
8493                Spaces);
8494 
8495   // Run the first set of tests again with:
8496   Spaces.SpaceAfterCStyleCast = true;
8497   verifyFormat("call(x, y, z);", Spaces);
8498   verifyFormat("call( );", Spaces);
8499   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8500   verifyFormat("while (( bool ) 1)\n"
8501                "  continue;",
8502                Spaces);
8503   verifyFormat("for (;;)\n"
8504                "  continue;",
8505                Spaces);
8506   verifyFormat("if (true)\n"
8507                "  f( );\n"
8508                "else if (true)\n"
8509                "  f( );",
8510                Spaces);
8511   verifyFormat("do {\n"
8512                "  do_something(( int ) i);\n"
8513                "} while (something( ));",
8514                Spaces);
8515   verifyFormat("switch (x) {\n"
8516                "default:\n"
8517                "  break;\n"
8518                "}",
8519                Spaces);
8520 
8521   // Run subset of tests again with:
8522   Spaces.SpacesInCStyleCastParentheses = false;
8523   Spaces.SpaceAfterCStyleCast = true;
8524   verifyFormat("while ((bool) 1)\n"
8525                "  continue;",
8526                Spaces);
8527   verifyFormat("do {\n"
8528                "  do_something((int) i);\n"
8529                "} while (something( ));",
8530                Spaces);
8531 }
8532 
8533 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8534   verifyFormat("int a[5];");
8535   verifyFormat("a[3] += 42;");
8536 
8537   FormatStyle Spaces = getLLVMStyle();
8538   Spaces.SpacesInSquareBrackets = true;
8539   // Lambdas unchanged.
8540   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8541   verifyFormat("return [i, args...] {};", Spaces);
8542 
8543   // Not lambdas.
8544   verifyFormat("int a[ 5 ];", Spaces);
8545   verifyFormat("a[ 3 ] += 42;", Spaces);
8546   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8547   verifyFormat("double &operator[](int i) { return 0; }\n"
8548                "int i;",
8549                Spaces);
8550   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8551   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8552   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8553 }
8554 
8555 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8556   verifyFormat("int a = 5;");
8557   verifyFormat("a += 42;");
8558   verifyFormat("a or_eq 8;");
8559 
8560   FormatStyle Spaces = getLLVMStyle();
8561   Spaces.SpaceBeforeAssignmentOperators = false;
8562   verifyFormat("int a= 5;", Spaces);
8563   verifyFormat("a+= 42;", Spaces);
8564   verifyFormat("a or_eq 8;", Spaces);
8565 }
8566 
8567 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8568   FormatStyle Alignment = getLLVMStyle();
8569   Alignment.AlignConsecutiveAssignments = false;
8570   verifyFormat("int a = 5;\n"
8571                "int oneTwoThree = 123;",
8572                Alignment);
8573   verifyFormat("int a = 5;\n"
8574                "int oneTwoThree = 123;",
8575                Alignment);
8576 
8577   Alignment.AlignConsecutiveAssignments = true;
8578   verifyFormat("int a           = 5;\n"
8579                "int oneTwoThree = 123;",
8580                Alignment);
8581   verifyFormat("int a           = method();\n"
8582                "int oneTwoThree = 133;",
8583                Alignment);
8584   verifyFormat("a &= 5;\n"
8585                "bcd *= 5;\n"
8586                "ghtyf += 5;\n"
8587                "dvfvdb -= 5;\n"
8588                "a /= 5;\n"
8589                "vdsvsv %= 5;\n"
8590                "sfdbddfbdfbb ^= 5;\n"
8591                "dvsdsv |= 5;\n"
8592                "int dsvvdvsdvvv = 123;",
8593                Alignment);
8594   verifyFormat("int i = 1, j = 10;\n"
8595                "something = 2000;",
8596                Alignment);
8597   verifyFormat("something = 2000;\n"
8598                "int i = 1, j = 10;\n",
8599                Alignment);
8600   verifyFormat("something = 2000;\n"
8601                "another   = 911;\n"
8602                "int i = 1, j = 10;\n"
8603                "oneMore = 1;\n"
8604                "i       = 2;",
8605                Alignment);
8606   verifyFormat("int a   = 5;\n"
8607                "int one = 1;\n"
8608                "method();\n"
8609                "int oneTwoThree = 123;\n"
8610                "int oneTwo      = 12;",
8611                Alignment);
8612   verifyFormat("int oneTwoThree = 123;\n"
8613                "int oneTwo      = 12;\n"
8614                "method();\n",
8615                Alignment);
8616   verifyFormat("int oneTwoThree = 123; // comment\n"
8617                "int oneTwo      = 12;  // comment",
8618                Alignment);
8619   EXPECT_EQ("int a = 5;\n"
8620             "\n"
8621             "int oneTwoThree = 123;",
8622             format("int a       = 5;\n"
8623                    "\n"
8624                    "int oneTwoThree= 123;",
8625                    Alignment));
8626   EXPECT_EQ("int a   = 5;\n"
8627             "int one = 1;\n"
8628             "\n"
8629             "int oneTwoThree = 123;",
8630             format("int a = 5;\n"
8631                    "int one = 1;\n"
8632                    "\n"
8633                    "int oneTwoThree = 123;",
8634                    Alignment));
8635   EXPECT_EQ("int a   = 5;\n"
8636             "int one = 1;\n"
8637             "\n"
8638             "int oneTwoThree = 123;\n"
8639             "int oneTwo      = 12;",
8640             format("int a = 5;\n"
8641                    "int one = 1;\n"
8642                    "\n"
8643                    "int oneTwoThree = 123;\n"
8644                    "int oneTwo = 12;",
8645                    Alignment));
8646   Alignment.AlignEscapedNewlinesLeft = true;
8647   verifyFormat("#define A               \\\n"
8648                "  int aaaa       = 12;  \\\n"
8649                "  int b          = 23;  \\\n"
8650                "  int ccc        = 234; \\\n"
8651                "  int dddddddddd = 2345;",
8652                Alignment);
8653   Alignment.AlignEscapedNewlinesLeft = false;
8654   verifyFormat("#define A                                                      "
8655                "                \\\n"
8656                "  int aaaa       = 12;                                         "
8657                "                \\\n"
8658                "  int b          = 23;                                         "
8659                "                \\\n"
8660                "  int ccc        = 234;                                        "
8661                "                \\\n"
8662                "  int dddddddddd = 2345;",
8663                Alignment);
8664   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8665                "k = 4, int l = 5,\n"
8666                "                  int m = 6) {\n"
8667                "  int j      = 10;\n"
8668                "  otherThing = 1;\n"
8669                "}",
8670                Alignment);
8671   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8672                "  int i   = 1;\n"
8673                "  int j   = 2;\n"
8674                "  int big = 10000;\n"
8675                "}",
8676                Alignment);
8677   verifyFormat("class C {\n"
8678                "public:\n"
8679                "  int i            = 1;\n"
8680                "  virtual void f() = 0;\n"
8681                "};",
8682                Alignment);
8683   verifyFormat("int i = 1;\n"
8684                "if (SomeType t = getSomething()) {\n"
8685                "}\n"
8686                "int j   = 2;\n"
8687                "int big = 10000;",
8688                Alignment);
8689   verifyFormat("int j = 7;\n"
8690                "for (int k = 0; k < N; ++k) {\n"
8691                "}\n"
8692                "int j   = 2;\n"
8693                "int big = 10000;\n"
8694                "}",
8695                Alignment);
8696   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8697   verifyFormat("int i = 1;\n"
8698                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8699                "    = someLooooooooooooooooongFunction();\n"
8700                "int j = 2;",
8701                Alignment);
8702   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8703   verifyFormat("int i = 1;\n"
8704                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8705                "    someLooooooooooooooooongFunction();\n"
8706                "int j = 2;",
8707                Alignment);
8708 
8709   verifyFormat("auto lambda = []() {\n"
8710                "  auto i = 0;\n"
8711                "  return 0;\n"
8712                "};\n"
8713                "int i  = 0;\n"
8714                "auto v = type{\n"
8715                "    i = 1,   //\n"
8716                "    (i = 2), //\n"
8717                "    i = 3    //\n"
8718                "};",
8719                Alignment);
8720 
8721   // FIXME: Should align all three assignments
8722   verifyFormat(
8723       "int i      = 1;\n"
8724       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8725       "                          loooooooooooooooooooooongParameterB);\n"
8726       "int j = 2;",
8727       Alignment);
8728 
8729   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8730                "          typename B   = very_long_type_name_1,\n"
8731                "          typename T_2 = very_long_type_name_2>\n"
8732                "auto foo() {}\n",
8733                Alignment);
8734   verifyFormat("int a, b = 1;\n"
8735                "int c  = 2;\n"
8736                "int dd = 3;\n",
8737                Alignment);
8738   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8739                "float b[1][] = {{3.f}};\n",
8740                Alignment);
8741 }
8742 
8743 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8744   FormatStyle Alignment = getLLVMStyle();
8745   Alignment.AlignConsecutiveDeclarations = false;
8746   verifyFormat("float const a = 5;\n"
8747                "int oneTwoThree = 123;",
8748                Alignment);
8749   verifyFormat("int a = 5;\n"
8750                "float const oneTwoThree = 123;",
8751                Alignment);
8752 
8753   Alignment.AlignConsecutiveDeclarations = true;
8754   verifyFormat("float const a = 5;\n"
8755                "int         oneTwoThree = 123;",
8756                Alignment);
8757   verifyFormat("int         a = method();\n"
8758                "float const oneTwoThree = 133;",
8759                Alignment);
8760   verifyFormat("int i = 1, j = 10;\n"
8761                "something = 2000;",
8762                Alignment);
8763   verifyFormat("something = 2000;\n"
8764                "int i = 1, j = 10;\n",
8765                Alignment);
8766   verifyFormat("float      something = 2000;\n"
8767                "double     another = 911;\n"
8768                "int        i = 1, j = 10;\n"
8769                "const int *oneMore = 1;\n"
8770                "unsigned   i = 2;",
8771                Alignment);
8772   verifyFormat("float a = 5;\n"
8773                "int   one = 1;\n"
8774                "method();\n"
8775                "const double       oneTwoThree = 123;\n"
8776                "const unsigned int oneTwo = 12;",
8777                Alignment);
8778   verifyFormat("int      oneTwoThree{0}; // comment\n"
8779                "unsigned oneTwo;         // comment",
8780                Alignment);
8781   EXPECT_EQ("float const a = 5;\n"
8782             "\n"
8783             "int oneTwoThree = 123;",
8784             format("float const   a = 5;\n"
8785                    "\n"
8786                    "int           oneTwoThree= 123;",
8787                    Alignment));
8788   EXPECT_EQ("float a = 5;\n"
8789             "int   one = 1;\n"
8790             "\n"
8791             "unsigned oneTwoThree = 123;",
8792             format("float    a = 5;\n"
8793                    "int      one = 1;\n"
8794                    "\n"
8795                    "unsigned oneTwoThree = 123;",
8796                    Alignment));
8797   EXPECT_EQ("float a = 5;\n"
8798             "int   one = 1;\n"
8799             "\n"
8800             "unsigned oneTwoThree = 123;\n"
8801             "int      oneTwo = 12;",
8802             format("float    a = 5;\n"
8803                    "int one = 1;\n"
8804                    "\n"
8805                    "unsigned oneTwoThree = 123;\n"
8806                    "int oneTwo = 12;",
8807                    Alignment));
8808   Alignment.AlignConsecutiveAssignments = true;
8809   verifyFormat("float      something = 2000;\n"
8810                "double     another   = 911;\n"
8811                "int        i = 1, j = 10;\n"
8812                "const int *oneMore = 1;\n"
8813                "unsigned   i       = 2;",
8814                Alignment);
8815   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8816                "unsigned oneTwo      = 0;   // comment",
8817                Alignment);
8818   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8819             "  int const i   = 1;\n"
8820             "  int *     j   = 2;\n"
8821             "  int       big = 10000;\n"
8822             "\n"
8823             "  unsigned oneTwoThree = 123;\n"
8824             "  int      oneTwo      = 12;\n"
8825             "  method();\n"
8826             "  float k  = 2;\n"
8827             "  int   ll = 10000;\n"
8828             "}",
8829             format("void SomeFunction(int parameter= 0) {\n"
8830                    " int const  i= 1;\n"
8831                    "  int *j=2;\n"
8832                    " int big  =  10000;\n"
8833                    "\n"
8834                    "unsigned oneTwoThree  =123;\n"
8835                    "int oneTwo = 12;\n"
8836                    "  method();\n"
8837                    "float k= 2;\n"
8838                    "int ll=10000;\n"
8839                    "}",
8840                    Alignment));
8841   Alignment.AlignConsecutiveAssignments = false;
8842   Alignment.AlignEscapedNewlinesLeft = true;
8843   verifyFormat("#define A              \\\n"
8844                "  int       aaaa = 12; \\\n"
8845                "  float     b = 23;    \\\n"
8846                "  const int ccc = 234; \\\n"
8847                "  unsigned  dddddddddd = 2345;",
8848                Alignment);
8849   Alignment.AlignEscapedNewlinesLeft = false;
8850   Alignment.ColumnLimit = 30;
8851   verifyFormat("#define A                    \\\n"
8852                "  int       aaaa = 12;       \\\n"
8853                "  float     b = 23;          \\\n"
8854                "  const int ccc = 234;       \\\n"
8855                "  int       dddddddddd = 2345;",
8856                Alignment);
8857   Alignment.ColumnLimit = 80;
8858   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8859                "k = 4, int l = 5,\n"
8860                "                  int m = 6) {\n"
8861                "  const int j = 10;\n"
8862                "  otherThing = 1;\n"
8863                "}",
8864                Alignment);
8865   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8866                "  int const i = 1;\n"
8867                "  int *     j = 2;\n"
8868                "  int       big = 10000;\n"
8869                "}",
8870                Alignment);
8871   verifyFormat("class C {\n"
8872                "public:\n"
8873                "  int          i = 1;\n"
8874                "  virtual void f() = 0;\n"
8875                "};",
8876                Alignment);
8877   verifyFormat("float i = 1;\n"
8878                "if (SomeType t = getSomething()) {\n"
8879                "}\n"
8880                "const unsigned j = 2;\n"
8881                "int            big = 10000;",
8882                Alignment);
8883   verifyFormat("float j = 7;\n"
8884                "for (int k = 0; k < N; ++k) {\n"
8885                "}\n"
8886                "unsigned j = 2;\n"
8887                "int      big = 10000;\n"
8888                "}",
8889                Alignment);
8890   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8891   verifyFormat("float              i = 1;\n"
8892                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8893                "    = someLooooooooooooooooongFunction();\n"
8894                "int j = 2;",
8895                Alignment);
8896   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8897   verifyFormat("int                i = 1;\n"
8898                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8899                "    someLooooooooooooooooongFunction();\n"
8900                "int j = 2;",
8901                Alignment);
8902 
8903   Alignment.AlignConsecutiveAssignments = true;
8904   verifyFormat("auto lambda = []() {\n"
8905                "  auto  ii = 0;\n"
8906                "  float j  = 0;\n"
8907                "  return 0;\n"
8908                "};\n"
8909                "int   i  = 0;\n"
8910                "float i2 = 0;\n"
8911                "auto  v  = type{\n"
8912                "    i = 1,   //\n"
8913                "    (i = 2), //\n"
8914                "    i = 3    //\n"
8915                "};",
8916                Alignment);
8917   Alignment.AlignConsecutiveAssignments = false;
8918 
8919   // FIXME: Should align all three declarations
8920   verifyFormat(
8921       "int      i = 1;\n"
8922       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8923       "                          loooooooooooooooooooooongParameterB);\n"
8924       "int j = 2;",
8925       Alignment);
8926 
8927   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8928   // We expect declarations and assignments to align, as long as it doesn't
8929   // exceed the column limit, starting a new alignemnt sequence whenever it
8930   // happens.
8931   Alignment.AlignConsecutiveAssignments = true;
8932   Alignment.ColumnLimit = 30;
8933   verifyFormat("float    ii              = 1;\n"
8934                "unsigned j               = 2;\n"
8935                "int someVerylongVariable = 1;\n"
8936                "AnotherLongType  ll = 123456;\n"
8937                "VeryVeryLongType k  = 2;\n"
8938                "int              myvar = 1;",
8939                Alignment);
8940   Alignment.ColumnLimit = 80;
8941   Alignment.AlignConsecutiveAssignments = false;
8942 
8943   verifyFormat(
8944       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8945       "          typename LongType, typename B>\n"
8946       "auto foo() {}\n",
8947       Alignment);
8948   verifyFormat("float a, b = 1;\n"
8949                "int   c = 2;\n"
8950                "int   dd = 3;\n",
8951                Alignment);
8952   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8953                "float b[1][] = {{3.f}};\n",
8954                Alignment);
8955   Alignment.AlignConsecutiveAssignments = true;
8956   verifyFormat("float a, b = 1;\n"
8957                "int   c  = 2;\n"
8958                "int   dd = 3;\n",
8959                Alignment);
8960   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8961                "float b[1][] = {{3.f}};\n",
8962                Alignment);
8963   Alignment.AlignConsecutiveAssignments = false;
8964 
8965   Alignment.ColumnLimit = 30;
8966   Alignment.BinPackParameters = false;
8967   verifyFormat("void foo(float     a,\n"
8968                "         float     b,\n"
8969                "         int       c,\n"
8970                "         uint32_t *d) {\n"
8971                "  int *  e = 0;\n"
8972                "  float  f = 0;\n"
8973                "  double g = 0;\n"
8974                "}\n"
8975                "void bar(ino_t     a,\n"
8976                "         int       b,\n"
8977                "         uint32_t *c,\n"
8978                "         bool      d) {}\n",
8979                Alignment);
8980   Alignment.BinPackParameters = true;
8981   Alignment.ColumnLimit = 80;
8982 }
8983 
8984 TEST_F(FormatTest, LinuxBraceBreaking) {
8985   FormatStyle LinuxBraceStyle = getLLVMStyle();
8986   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8987   verifyFormat("namespace a\n"
8988                "{\n"
8989                "class A\n"
8990                "{\n"
8991                "  void f()\n"
8992                "  {\n"
8993                "    if (true) {\n"
8994                "      a();\n"
8995                "      b();\n"
8996                "    }\n"
8997                "  }\n"
8998                "  void g() { return; }\n"
8999                "};\n"
9000                "struct B {\n"
9001                "  int x;\n"
9002                "};\n"
9003                "}\n",
9004                LinuxBraceStyle);
9005   verifyFormat("enum X {\n"
9006                "  Y = 0,\n"
9007                "}\n",
9008                LinuxBraceStyle);
9009   verifyFormat("struct S {\n"
9010                "  int Type;\n"
9011                "  union {\n"
9012                "    int x;\n"
9013                "    double y;\n"
9014                "  } Value;\n"
9015                "  class C\n"
9016                "  {\n"
9017                "    MyFavoriteType Value;\n"
9018                "  } Class;\n"
9019                "}\n",
9020                LinuxBraceStyle);
9021 }
9022 
9023 TEST_F(FormatTest, MozillaBraceBreaking) {
9024   FormatStyle MozillaBraceStyle = getLLVMStyle();
9025   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9026   verifyFormat("namespace a {\n"
9027                "class A\n"
9028                "{\n"
9029                "  void f()\n"
9030                "  {\n"
9031                "    if (true) {\n"
9032                "      a();\n"
9033                "      b();\n"
9034                "    }\n"
9035                "  }\n"
9036                "  void g() { return; }\n"
9037                "};\n"
9038                "enum E\n"
9039                "{\n"
9040                "  A,\n"
9041                "  // foo\n"
9042                "  B,\n"
9043                "  C\n"
9044                "};\n"
9045                "struct B\n"
9046                "{\n"
9047                "  int x;\n"
9048                "};\n"
9049                "}\n",
9050                MozillaBraceStyle);
9051   verifyFormat("struct S\n"
9052                "{\n"
9053                "  int Type;\n"
9054                "  union\n"
9055                "  {\n"
9056                "    int x;\n"
9057                "    double y;\n"
9058                "  } Value;\n"
9059                "  class C\n"
9060                "  {\n"
9061                "    MyFavoriteType Value;\n"
9062                "  } Class;\n"
9063                "}\n",
9064                MozillaBraceStyle);
9065 }
9066 
9067 TEST_F(FormatTest, StroustrupBraceBreaking) {
9068   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9069   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9070   verifyFormat("namespace a {\n"
9071                "class A {\n"
9072                "  void f()\n"
9073                "  {\n"
9074                "    if (true) {\n"
9075                "      a();\n"
9076                "      b();\n"
9077                "    }\n"
9078                "  }\n"
9079                "  void g() { return; }\n"
9080                "};\n"
9081                "struct B {\n"
9082                "  int x;\n"
9083                "};\n"
9084                "}\n",
9085                StroustrupBraceStyle);
9086 
9087   verifyFormat("void foo()\n"
9088                "{\n"
9089                "  if (a) {\n"
9090                "    a();\n"
9091                "  }\n"
9092                "  else {\n"
9093                "    b();\n"
9094                "  }\n"
9095                "}\n",
9096                StroustrupBraceStyle);
9097 
9098   verifyFormat("#ifdef _DEBUG\n"
9099                "int foo(int i = 0)\n"
9100                "#else\n"
9101                "int foo(int i = 5)\n"
9102                "#endif\n"
9103                "{\n"
9104                "  return i;\n"
9105                "}",
9106                StroustrupBraceStyle);
9107 
9108   verifyFormat("void foo() {}\n"
9109                "void bar()\n"
9110                "#ifdef _DEBUG\n"
9111                "{\n"
9112                "  foo();\n"
9113                "}\n"
9114                "#else\n"
9115                "{\n"
9116                "}\n"
9117                "#endif",
9118                StroustrupBraceStyle);
9119 
9120   verifyFormat("void foobar() { int i = 5; }\n"
9121                "#ifdef _DEBUG\n"
9122                "void bar() {}\n"
9123                "#else\n"
9124                "void bar() { foobar(); }\n"
9125                "#endif",
9126                StroustrupBraceStyle);
9127 }
9128 
9129 TEST_F(FormatTest, AllmanBraceBreaking) {
9130   FormatStyle AllmanBraceStyle = getLLVMStyle();
9131   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9132   verifyFormat("namespace a\n"
9133                "{\n"
9134                "class A\n"
9135                "{\n"
9136                "  void f()\n"
9137                "  {\n"
9138                "    if (true)\n"
9139                "    {\n"
9140                "      a();\n"
9141                "      b();\n"
9142                "    }\n"
9143                "  }\n"
9144                "  void g() { return; }\n"
9145                "};\n"
9146                "struct B\n"
9147                "{\n"
9148                "  int x;\n"
9149                "};\n"
9150                "}",
9151                AllmanBraceStyle);
9152 
9153   verifyFormat("void f()\n"
9154                "{\n"
9155                "  if (true)\n"
9156                "  {\n"
9157                "    a();\n"
9158                "  }\n"
9159                "  else if (false)\n"
9160                "  {\n"
9161                "    b();\n"
9162                "  }\n"
9163                "  else\n"
9164                "  {\n"
9165                "    c();\n"
9166                "  }\n"
9167                "}\n",
9168                AllmanBraceStyle);
9169 
9170   verifyFormat("void f()\n"
9171                "{\n"
9172                "  for (int i = 0; i < 10; ++i)\n"
9173                "  {\n"
9174                "    a();\n"
9175                "  }\n"
9176                "  while (false)\n"
9177                "  {\n"
9178                "    b();\n"
9179                "  }\n"
9180                "  do\n"
9181                "  {\n"
9182                "    c();\n"
9183                "  } while (false)\n"
9184                "}\n",
9185                AllmanBraceStyle);
9186 
9187   verifyFormat("void f(int a)\n"
9188                "{\n"
9189                "  switch (a)\n"
9190                "  {\n"
9191                "  case 0:\n"
9192                "    break;\n"
9193                "  case 1:\n"
9194                "  {\n"
9195                "    break;\n"
9196                "  }\n"
9197                "  case 2:\n"
9198                "  {\n"
9199                "  }\n"
9200                "  break;\n"
9201                "  default:\n"
9202                "    break;\n"
9203                "  }\n"
9204                "}\n",
9205                AllmanBraceStyle);
9206 
9207   verifyFormat("enum X\n"
9208                "{\n"
9209                "  Y = 0,\n"
9210                "}\n",
9211                AllmanBraceStyle);
9212   verifyFormat("enum X\n"
9213                "{\n"
9214                "  Y = 0\n"
9215                "}\n",
9216                AllmanBraceStyle);
9217 
9218   verifyFormat("@interface BSApplicationController ()\n"
9219                "{\n"
9220                "@private\n"
9221                "  id _extraIvar;\n"
9222                "}\n"
9223                "@end\n",
9224                AllmanBraceStyle);
9225 
9226   verifyFormat("#ifdef _DEBUG\n"
9227                "int foo(int i = 0)\n"
9228                "#else\n"
9229                "int foo(int i = 5)\n"
9230                "#endif\n"
9231                "{\n"
9232                "  return i;\n"
9233                "}",
9234                AllmanBraceStyle);
9235 
9236   verifyFormat("void foo() {}\n"
9237                "void bar()\n"
9238                "#ifdef _DEBUG\n"
9239                "{\n"
9240                "  foo();\n"
9241                "}\n"
9242                "#else\n"
9243                "{\n"
9244                "}\n"
9245                "#endif",
9246                AllmanBraceStyle);
9247 
9248   verifyFormat("void foobar() { int i = 5; }\n"
9249                "#ifdef _DEBUG\n"
9250                "void bar() {}\n"
9251                "#else\n"
9252                "void bar() { foobar(); }\n"
9253                "#endif",
9254                AllmanBraceStyle);
9255 
9256   // This shouldn't affect ObjC blocks..
9257   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9258                "  // ...\n"
9259                "  int i;\n"
9260                "}];",
9261                AllmanBraceStyle);
9262   verifyFormat("void (^block)(void) = ^{\n"
9263                "  // ...\n"
9264                "  int i;\n"
9265                "};",
9266                AllmanBraceStyle);
9267   // .. or dict literals.
9268   verifyFormat("void f()\n"
9269                "{\n"
9270                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9271                "}",
9272                AllmanBraceStyle);
9273   verifyFormat("int f()\n"
9274                "{ // comment\n"
9275                "  return 42;\n"
9276                "}",
9277                AllmanBraceStyle);
9278 
9279   AllmanBraceStyle.ColumnLimit = 19;
9280   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9281   AllmanBraceStyle.ColumnLimit = 18;
9282   verifyFormat("void f()\n"
9283                "{\n"
9284                "  int i;\n"
9285                "}",
9286                AllmanBraceStyle);
9287   AllmanBraceStyle.ColumnLimit = 80;
9288 
9289   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9290   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9291   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9292   verifyFormat("void f(bool b)\n"
9293                "{\n"
9294                "  if (b)\n"
9295                "  {\n"
9296                "    return;\n"
9297                "  }\n"
9298                "}\n",
9299                BreakBeforeBraceShortIfs);
9300   verifyFormat("void f(bool b)\n"
9301                "{\n"
9302                "  if (b) return;\n"
9303                "}\n",
9304                BreakBeforeBraceShortIfs);
9305   verifyFormat("void f(bool b)\n"
9306                "{\n"
9307                "  while (b)\n"
9308                "  {\n"
9309                "    return;\n"
9310                "  }\n"
9311                "}\n",
9312                BreakBeforeBraceShortIfs);
9313 }
9314 
9315 TEST_F(FormatTest, GNUBraceBreaking) {
9316   FormatStyle GNUBraceStyle = getLLVMStyle();
9317   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9318   verifyFormat("namespace a\n"
9319                "{\n"
9320                "class A\n"
9321                "{\n"
9322                "  void f()\n"
9323                "  {\n"
9324                "    int a;\n"
9325                "    {\n"
9326                "      int b;\n"
9327                "    }\n"
9328                "    if (true)\n"
9329                "      {\n"
9330                "        a();\n"
9331                "        b();\n"
9332                "      }\n"
9333                "  }\n"
9334                "  void g() { return; }\n"
9335                "}\n"
9336                "}",
9337                GNUBraceStyle);
9338 
9339   verifyFormat("void f()\n"
9340                "{\n"
9341                "  if (true)\n"
9342                "    {\n"
9343                "      a();\n"
9344                "    }\n"
9345                "  else if (false)\n"
9346                "    {\n"
9347                "      b();\n"
9348                "    }\n"
9349                "  else\n"
9350                "    {\n"
9351                "      c();\n"
9352                "    }\n"
9353                "}\n",
9354                GNUBraceStyle);
9355 
9356   verifyFormat("void f()\n"
9357                "{\n"
9358                "  for (int i = 0; i < 10; ++i)\n"
9359                "    {\n"
9360                "      a();\n"
9361                "    }\n"
9362                "  while (false)\n"
9363                "    {\n"
9364                "      b();\n"
9365                "    }\n"
9366                "  do\n"
9367                "    {\n"
9368                "      c();\n"
9369                "    }\n"
9370                "  while (false);\n"
9371                "}\n",
9372                GNUBraceStyle);
9373 
9374   verifyFormat("void f(int a)\n"
9375                "{\n"
9376                "  switch (a)\n"
9377                "    {\n"
9378                "    case 0:\n"
9379                "      break;\n"
9380                "    case 1:\n"
9381                "      {\n"
9382                "        break;\n"
9383                "      }\n"
9384                "    case 2:\n"
9385                "      {\n"
9386                "      }\n"
9387                "      break;\n"
9388                "    default:\n"
9389                "      break;\n"
9390                "    }\n"
9391                "}\n",
9392                GNUBraceStyle);
9393 
9394   verifyFormat("enum X\n"
9395                "{\n"
9396                "  Y = 0,\n"
9397                "}\n",
9398                GNUBraceStyle);
9399 
9400   verifyFormat("@interface BSApplicationController ()\n"
9401                "{\n"
9402                "@private\n"
9403                "  id _extraIvar;\n"
9404                "}\n"
9405                "@end\n",
9406                GNUBraceStyle);
9407 
9408   verifyFormat("#ifdef _DEBUG\n"
9409                "int foo(int i = 0)\n"
9410                "#else\n"
9411                "int foo(int i = 5)\n"
9412                "#endif\n"
9413                "{\n"
9414                "  return i;\n"
9415                "}",
9416                GNUBraceStyle);
9417 
9418   verifyFormat("void foo() {}\n"
9419                "void bar()\n"
9420                "#ifdef _DEBUG\n"
9421                "{\n"
9422                "  foo();\n"
9423                "}\n"
9424                "#else\n"
9425                "{\n"
9426                "}\n"
9427                "#endif",
9428                GNUBraceStyle);
9429 
9430   verifyFormat("void foobar() { int i = 5; }\n"
9431                "#ifdef _DEBUG\n"
9432                "void bar() {}\n"
9433                "#else\n"
9434                "void bar() { foobar(); }\n"
9435                "#endif",
9436                GNUBraceStyle);
9437 }
9438 
9439 TEST_F(FormatTest, WebKitBraceBreaking) {
9440   FormatStyle WebKitBraceStyle = getLLVMStyle();
9441   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9442   verifyFormat("namespace a {\n"
9443                "class A {\n"
9444                "  void f()\n"
9445                "  {\n"
9446                "    if (true) {\n"
9447                "      a();\n"
9448                "      b();\n"
9449                "    }\n"
9450                "  }\n"
9451                "  void g() { return; }\n"
9452                "};\n"
9453                "enum E {\n"
9454                "  A,\n"
9455                "  // foo\n"
9456                "  B,\n"
9457                "  C\n"
9458                "};\n"
9459                "struct B {\n"
9460                "  int x;\n"
9461                "};\n"
9462                "}\n",
9463                WebKitBraceStyle);
9464   verifyFormat("struct S {\n"
9465                "  int Type;\n"
9466                "  union {\n"
9467                "    int x;\n"
9468                "    double y;\n"
9469                "  } Value;\n"
9470                "  class C {\n"
9471                "    MyFavoriteType Value;\n"
9472                "  } Class;\n"
9473                "};\n",
9474                WebKitBraceStyle);
9475 }
9476 
9477 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9478   verifyFormat("void f() {\n"
9479                "  try {\n"
9480                "  } catch (const Exception &e) {\n"
9481                "  }\n"
9482                "}\n",
9483                getLLVMStyle());
9484 }
9485 
9486 TEST_F(FormatTest, UnderstandsPragmas) {
9487   verifyFormat("#pragma omp reduction(| : var)");
9488   verifyFormat("#pragma omp reduction(+ : var)");
9489 
9490   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9491             "(including parentheses).",
9492             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9493                    "(including parentheses)."));
9494 }
9495 
9496 TEST_F(FormatTest, UnderstandPragmaOption) {
9497   verifyFormat("#pragma option -C -A");
9498 
9499   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9500 }
9501 
9502 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9503   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9504   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9505                                   << " differs from Style #0"
9506 
9507 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9508   SmallVector<FormatStyle, 3> Styles;
9509   Styles.resize(3);
9510 
9511   Styles[0] = getLLVMStyle();
9512   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9513   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9514   EXPECT_ALL_STYLES_EQUAL(Styles);
9515 
9516   Styles[0] = getGoogleStyle();
9517   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9518   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9519   EXPECT_ALL_STYLES_EQUAL(Styles);
9520 
9521   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9522   EXPECT_TRUE(
9523       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9524   EXPECT_TRUE(
9525       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9526   EXPECT_ALL_STYLES_EQUAL(Styles);
9527 
9528   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9529   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9530   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9531   EXPECT_ALL_STYLES_EQUAL(Styles);
9532 
9533   Styles[0] = getMozillaStyle();
9534   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9535   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9536   EXPECT_ALL_STYLES_EQUAL(Styles);
9537 
9538   Styles[0] = getWebKitStyle();
9539   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9540   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9541   EXPECT_ALL_STYLES_EQUAL(Styles);
9542 
9543   Styles[0] = getGNUStyle();
9544   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9545   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9546   EXPECT_ALL_STYLES_EQUAL(Styles);
9547 
9548   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9549 }
9550 
9551 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9552   SmallVector<FormatStyle, 8> Styles;
9553   Styles.resize(2);
9554 
9555   Styles[0] = getGoogleStyle();
9556   Styles[1] = getLLVMStyle();
9557   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9558   EXPECT_ALL_STYLES_EQUAL(Styles);
9559 
9560   Styles.resize(5);
9561   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9562   Styles[1] = getLLVMStyle();
9563   Styles[1].Language = FormatStyle::LK_JavaScript;
9564   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9565 
9566   Styles[2] = getLLVMStyle();
9567   Styles[2].Language = FormatStyle::LK_JavaScript;
9568   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9569                                   "BasedOnStyle: Google",
9570                                   &Styles[2])
9571                    .value());
9572 
9573   Styles[3] = getLLVMStyle();
9574   Styles[3].Language = FormatStyle::LK_JavaScript;
9575   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9576                                   "Language: JavaScript",
9577                                   &Styles[3])
9578                    .value());
9579 
9580   Styles[4] = getLLVMStyle();
9581   Styles[4].Language = FormatStyle::LK_JavaScript;
9582   EXPECT_EQ(0, parseConfiguration("---\n"
9583                                   "BasedOnStyle: LLVM\n"
9584                                   "IndentWidth: 123\n"
9585                                   "---\n"
9586                                   "BasedOnStyle: Google\n"
9587                                   "Language: JavaScript",
9588                                   &Styles[4])
9589                    .value());
9590   EXPECT_ALL_STYLES_EQUAL(Styles);
9591 }
9592 
9593 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9594   Style.FIELD = false;                                                         \
9595   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9596   EXPECT_TRUE(Style.FIELD);                                                    \
9597   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9598   EXPECT_FALSE(Style.FIELD);
9599 
9600 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9601 
9602 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9603   Style.STRUCT.FIELD = false;                                                  \
9604   EXPECT_EQ(0,                                                                 \
9605             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9606                 .value());                                                     \
9607   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9608   EXPECT_EQ(0,                                                                 \
9609             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9610                 .value());                                                     \
9611   EXPECT_FALSE(Style.STRUCT.FIELD);
9612 
9613 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9614   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9615 
9616 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9617   EXPECT_NE(VALUE, Style.FIELD);                                               \
9618   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9619   EXPECT_EQ(VALUE, Style.FIELD)
9620 
9621 TEST_F(FormatTest, ParsesConfigurationBools) {
9622   FormatStyle Style = {};
9623   Style.Language = FormatStyle::LK_Cpp;
9624   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9625   CHECK_PARSE_BOOL(AlignOperands);
9626   CHECK_PARSE_BOOL(AlignTrailingComments);
9627   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9628   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9629   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9630   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9631   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9632   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9633   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9634   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9635   CHECK_PARSE_BOOL(BinPackArguments);
9636   CHECK_PARSE_BOOL(BinPackParameters);
9637   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9638   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9639   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9640   CHECK_PARSE_BOOL(DerivePointerAlignment);
9641   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9642   CHECK_PARSE_BOOL(DisableFormat);
9643   CHECK_PARSE_BOOL(IndentCaseLabels);
9644   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9645   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9646   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9647   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9648   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9649   CHECK_PARSE_BOOL(ReflowComments);
9650   CHECK_PARSE_BOOL(SortIncludes);
9651   CHECK_PARSE_BOOL(SpacesInParentheses);
9652   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9653   CHECK_PARSE_BOOL(SpacesInAngles);
9654   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9655   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9656   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9657   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9658   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9659 
9660   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9661   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9662   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9663   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9664   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9665   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9666   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9667   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9668   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9669   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9670   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9671 }
9672 
9673 #undef CHECK_PARSE_BOOL
9674 
9675 TEST_F(FormatTest, ParsesConfiguration) {
9676   FormatStyle Style = {};
9677   Style.Language = FormatStyle::LK_Cpp;
9678   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9679   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9680               ConstructorInitializerIndentWidth, 1234u);
9681   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9682   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9683   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9684   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9685               PenaltyBreakBeforeFirstCallParameter, 1234u);
9686   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9687   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9688               PenaltyReturnTypeOnItsOwnLine, 1234u);
9689   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9690               SpacesBeforeTrailingComments, 1234u);
9691   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9692   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9693 
9694   Style.PointerAlignment = FormatStyle::PAS_Middle;
9695   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9696               FormatStyle::PAS_Left);
9697   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9698               FormatStyle::PAS_Right);
9699   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9700               FormatStyle::PAS_Middle);
9701   // For backward compatibility:
9702   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9703               FormatStyle::PAS_Left);
9704   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9705               FormatStyle::PAS_Right);
9706   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9707               FormatStyle::PAS_Middle);
9708 
9709   Style.Standard = FormatStyle::LS_Auto;
9710   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9711   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9712   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9713   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9714   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9715 
9716   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9717   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9718               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9719   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9720               FormatStyle::BOS_None);
9721   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9722               FormatStyle::BOS_All);
9723   // For backward compatibility:
9724   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9725               FormatStyle::BOS_None);
9726   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9727               FormatStyle::BOS_All);
9728 
9729   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9730   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9731               FormatStyle::BAS_Align);
9732   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9733               FormatStyle::BAS_DontAlign);
9734   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9735               FormatStyle::BAS_AlwaysBreak);
9736   // For backward compatibility:
9737   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9738               FormatStyle::BAS_DontAlign);
9739   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9740               FormatStyle::BAS_Align);
9741 
9742   Style.UseTab = FormatStyle::UT_ForIndentation;
9743   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9744   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9745   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9746   // For backward compatibility:
9747   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9748   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9749 
9750   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9751   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9752               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9753   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9754               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9755   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9756               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9757   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9758               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9759   // For backward compatibility:
9760   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9761               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9762   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9763               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9764 
9765   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9766   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9767               FormatStyle::SBPO_Never);
9768   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9769               FormatStyle::SBPO_Always);
9770   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9771               FormatStyle::SBPO_ControlStatements);
9772   // For backward compatibility:
9773   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9774               FormatStyle::SBPO_Never);
9775   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9776               FormatStyle::SBPO_ControlStatements);
9777 
9778   Style.ColumnLimit = 123;
9779   FormatStyle BaseStyle = getLLVMStyle();
9780   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9781   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9782 
9783   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9784   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9785               FormatStyle::BS_Attach);
9786   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9787               FormatStyle::BS_Linux);
9788   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9789               FormatStyle::BS_Mozilla);
9790   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9791               FormatStyle::BS_Stroustrup);
9792   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9793               FormatStyle::BS_Allman);
9794   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9795   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9796               FormatStyle::BS_WebKit);
9797   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9798               FormatStyle::BS_Custom);
9799 
9800   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9801   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9802               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9803   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9804               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9805   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9806               AlwaysBreakAfterDefinitionReturnType,
9807               FormatStyle::DRTBS_TopLevel);
9808 
9809   Style.NamespaceIndentation = FormatStyle::NI_All;
9810   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9811               FormatStyle::NI_None);
9812   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9813               FormatStyle::NI_Inner);
9814   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9815               FormatStyle::NI_All);
9816 
9817   // FIXME: This is required because parsing a configuration simply overwrites
9818   // the first N elements of the list instead of resetting it.
9819   Style.ForEachMacros.clear();
9820   std::vector<std::string> BoostForeach;
9821   BoostForeach.push_back("BOOST_FOREACH");
9822   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9823   std::vector<std::string> BoostAndQForeach;
9824   BoostAndQForeach.push_back("BOOST_FOREACH");
9825   BoostAndQForeach.push_back("Q_FOREACH");
9826   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9827               BoostAndQForeach);
9828 
9829   Style.IncludeCategories.clear();
9830   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9831                                                                   {".*", 1}};
9832   CHECK_PARSE("IncludeCategories:\n"
9833               "  - Regex: abc/.*\n"
9834               "    Priority: 2\n"
9835               "  - Regex: .*\n"
9836               "    Priority: 1",
9837               IncludeCategories, ExpectedCategories);
9838 }
9839 
9840 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9841   FormatStyle Style = {};
9842   Style.Language = FormatStyle::LK_Cpp;
9843   CHECK_PARSE("Language: Cpp\n"
9844               "IndentWidth: 12",
9845               IndentWidth, 12u);
9846   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9847                                "IndentWidth: 34",
9848                                &Style),
9849             ParseError::Unsuitable);
9850   EXPECT_EQ(12u, Style.IndentWidth);
9851   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9852   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9853 
9854   Style.Language = FormatStyle::LK_JavaScript;
9855   CHECK_PARSE("Language: JavaScript\n"
9856               "IndentWidth: 12",
9857               IndentWidth, 12u);
9858   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9859   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9860                                "IndentWidth: 34",
9861                                &Style),
9862             ParseError::Unsuitable);
9863   EXPECT_EQ(23u, Style.IndentWidth);
9864   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9865   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9866 
9867   CHECK_PARSE("BasedOnStyle: LLVM\n"
9868               "IndentWidth: 67",
9869               IndentWidth, 67u);
9870 
9871   CHECK_PARSE("---\n"
9872               "Language: JavaScript\n"
9873               "IndentWidth: 12\n"
9874               "---\n"
9875               "Language: Cpp\n"
9876               "IndentWidth: 34\n"
9877               "...\n",
9878               IndentWidth, 12u);
9879 
9880   Style.Language = FormatStyle::LK_Cpp;
9881   CHECK_PARSE("---\n"
9882               "Language: JavaScript\n"
9883               "IndentWidth: 12\n"
9884               "---\n"
9885               "Language: Cpp\n"
9886               "IndentWidth: 34\n"
9887               "...\n",
9888               IndentWidth, 34u);
9889   CHECK_PARSE("---\n"
9890               "IndentWidth: 78\n"
9891               "---\n"
9892               "Language: JavaScript\n"
9893               "IndentWidth: 56\n"
9894               "...\n",
9895               IndentWidth, 78u);
9896 
9897   Style.ColumnLimit = 123;
9898   Style.IndentWidth = 234;
9899   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9900   Style.TabWidth = 345;
9901   EXPECT_FALSE(parseConfiguration("---\n"
9902                                   "IndentWidth: 456\n"
9903                                   "BreakBeforeBraces: Allman\n"
9904                                   "---\n"
9905                                   "Language: JavaScript\n"
9906                                   "IndentWidth: 111\n"
9907                                   "TabWidth: 111\n"
9908                                   "---\n"
9909                                   "Language: Cpp\n"
9910                                   "BreakBeforeBraces: Stroustrup\n"
9911                                   "TabWidth: 789\n"
9912                                   "...\n",
9913                                   &Style));
9914   EXPECT_EQ(123u, Style.ColumnLimit);
9915   EXPECT_EQ(456u, Style.IndentWidth);
9916   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9917   EXPECT_EQ(789u, Style.TabWidth);
9918 
9919   EXPECT_EQ(parseConfiguration("---\n"
9920                                "Language: JavaScript\n"
9921                                "IndentWidth: 56\n"
9922                                "---\n"
9923                                "IndentWidth: 78\n"
9924                                "...\n",
9925                                &Style),
9926             ParseError::Error);
9927   EXPECT_EQ(parseConfiguration("---\n"
9928                                "Language: JavaScript\n"
9929                                "IndentWidth: 56\n"
9930                                "---\n"
9931                                "Language: JavaScript\n"
9932                                "IndentWidth: 78\n"
9933                                "...\n",
9934                                &Style),
9935             ParseError::Error);
9936 
9937   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9938 }
9939 
9940 #undef CHECK_PARSE
9941 
9942 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9943   FormatStyle Style = {};
9944   Style.Language = FormatStyle::LK_JavaScript;
9945   Style.BreakBeforeTernaryOperators = true;
9946   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9947   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9948 
9949   Style.BreakBeforeTernaryOperators = true;
9950   EXPECT_EQ(0, parseConfiguration("---\n"
9951                                   "BasedOnStyle: Google\n"
9952                                   "---\n"
9953                                   "Language: JavaScript\n"
9954                                   "IndentWidth: 76\n"
9955                                   "...\n",
9956                                   &Style)
9957                    .value());
9958   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9959   EXPECT_EQ(76u, Style.IndentWidth);
9960   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9961 }
9962 
9963 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9964   FormatStyle Style = getLLVMStyle();
9965   std::string YAML = configurationAsText(Style);
9966   FormatStyle ParsedStyle = {};
9967   ParsedStyle.Language = FormatStyle::LK_Cpp;
9968   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9969   EXPECT_EQ(Style, ParsedStyle);
9970 }
9971 
9972 TEST_F(FormatTest, WorksFor8bitEncodings) {
9973   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9974             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9975             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9976             "\"\xef\xee\xf0\xf3...\"",
9977             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9978                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9979                    "\xef\xee\xf0\xf3...\"",
9980                    getLLVMStyleWithColumns(12)));
9981 }
9982 
9983 TEST_F(FormatTest, HandlesUTF8BOM) {
9984   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9985   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9986             format("\xef\xbb\xbf#include <iostream>"));
9987   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9988             format("\xef\xbb\xbf\n#include <iostream>"));
9989 }
9990 
9991 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9992 #if !defined(_MSC_VER)
9993 
9994 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9995   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9996                getLLVMStyleWithColumns(35));
9997   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9998                getLLVMStyleWithColumns(31));
9999   verifyFormat("// Однажды в студёную зимнюю пору...",
10000                getLLVMStyleWithColumns(36));
10001   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10002   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10003                getLLVMStyleWithColumns(39));
10004   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10005                getLLVMStyleWithColumns(35));
10006 }
10007 
10008 TEST_F(FormatTest, SplitsUTF8Strings) {
10009   // Non-printable characters' width is currently considered to be the length in
10010   // bytes in UTF8. The characters can be displayed in very different manner
10011   // (zero-width, single width with a substitution glyph, expanded to their code
10012   // (e.g. "<8d>"), so there's no single correct way to handle them.
10013   EXPECT_EQ("\"aaaaÄ\"\n"
10014             "\"\xc2\x8d\";",
10015             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10016   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10017             "\"\xc2\x8d\";",
10018             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10019   EXPECT_EQ("\"Однажды, в \"\n"
10020             "\"студёную \"\n"
10021             "\"зимнюю \"\n"
10022             "\"пору,\"",
10023             format("\"Однажды, в студёную зимнюю пору,\"",
10024                    getLLVMStyleWithColumns(13)));
10025   EXPECT_EQ(
10026       "\"一 二 三 \"\n"
10027       "\"四 五六 \"\n"
10028       "\"七 八 九 \"\n"
10029       "\"十\"",
10030       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10031   EXPECT_EQ("\"一\t二 \"\n"
10032             "\"\t三 \"\n"
10033             "\"四 五\t六 \"\n"
10034             "\"\t七 \"\n"
10035             "\"八九十\tqq\"",
10036             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10037                    getLLVMStyleWithColumns(11)));
10038 
10039   // UTF8 character in an escape sequence.
10040   EXPECT_EQ("\"aaaaaa\"\n"
10041             "\"\\\xC2\x8D\"",
10042             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10043 }
10044 
10045 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10046   EXPECT_EQ("const char *sssss =\n"
10047             "    \"一二三四五六七八\\\n"
10048             " 九 十\";",
10049             format("const char *sssss = \"一二三四五六七八\\\n"
10050                    " 九 十\";",
10051                    getLLVMStyleWithColumns(30)));
10052 }
10053 
10054 TEST_F(FormatTest, SplitsUTF8LineComments) {
10055   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10056             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10057   EXPECT_EQ("// Я из лесу\n"
10058             "// вышел; был\n"
10059             "// сильный\n"
10060             "// мороз.",
10061             format("// Я из лесу вышел; был сильный мороз.",
10062                    getLLVMStyleWithColumns(13)));
10063   EXPECT_EQ("// 一二三\n"
10064             "// 四五六七\n"
10065             "// 八  九\n"
10066             "// 十",
10067             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10068 }
10069 
10070 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10071   EXPECT_EQ("/* Гляжу,\n"
10072             " * поднимается\n"
10073             " * медленно в\n"
10074             " * гору\n"
10075             " * Лошадка,\n"
10076             " * везущая\n"
10077             " * хворосту\n"
10078             " * воз. */",
10079             format("/* Гляжу, поднимается медленно в гору\n"
10080                    " * Лошадка, везущая хворосту воз. */",
10081                    getLLVMStyleWithColumns(13)));
10082   EXPECT_EQ(
10083       "/* 一二三\n"
10084       " * 四五六七\n"
10085       " * 八  九\n"
10086       " * 十  */",
10087       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10088   EXPECT_EQ("/* �������� ��������\n"
10089             " * ��������\n"
10090             " * ������-�� */",
10091             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10092 }
10093 
10094 #endif // _MSC_VER
10095 
10096 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10097   FormatStyle Style = getLLVMStyle();
10098 
10099   Style.ConstructorInitializerIndentWidth = 4;
10100   verifyFormat(
10101       "SomeClass::Constructor()\n"
10102       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10103       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10104       Style);
10105 
10106   Style.ConstructorInitializerIndentWidth = 2;
10107   verifyFormat(
10108       "SomeClass::Constructor()\n"
10109       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10110       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10111       Style);
10112 
10113   Style.ConstructorInitializerIndentWidth = 0;
10114   verifyFormat(
10115       "SomeClass::Constructor()\n"
10116       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10117       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10118       Style);
10119 }
10120 
10121 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10122   FormatStyle Style = getLLVMStyle();
10123   Style.BreakConstructorInitializersBeforeComma = true;
10124   Style.ConstructorInitializerIndentWidth = 4;
10125   verifyFormat("SomeClass::Constructor()\n"
10126                "    : a(a)\n"
10127                "    , b(b)\n"
10128                "    , c(c) {}",
10129                Style);
10130   verifyFormat("SomeClass::Constructor()\n"
10131                "    : a(a) {}",
10132                Style);
10133 
10134   Style.ColumnLimit = 0;
10135   verifyFormat("SomeClass::Constructor()\n"
10136                "    : a(a) {}",
10137                Style);
10138   verifyFormat("SomeClass::Constructor()\n"
10139                "    : a(a)\n"
10140                "    , b(b)\n"
10141                "    , c(c) {}",
10142                Style);
10143   verifyFormat("SomeClass::Constructor()\n"
10144                "    : a(a) {\n"
10145                "  foo();\n"
10146                "  bar();\n"
10147                "}",
10148                Style);
10149 
10150   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10151   verifyFormat("SomeClass::Constructor()\n"
10152                "    : a(a)\n"
10153                "    , b(b)\n"
10154                "    , c(c) {\n}",
10155                Style);
10156   verifyFormat("SomeClass::Constructor()\n"
10157                "    : a(a) {\n}",
10158                Style);
10159 
10160   Style.ColumnLimit = 80;
10161   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10162   Style.ConstructorInitializerIndentWidth = 2;
10163   verifyFormat("SomeClass::Constructor()\n"
10164                "  : a(a)\n"
10165                "  , b(b)\n"
10166                "  , c(c) {}",
10167                Style);
10168 
10169   Style.ConstructorInitializerIndentWidth = 0;
10170   verifyFormat("SomeClass::Constructor()\n"
10171                ": a(a)\n"
10172                ", b(b)\n"
10173                ", c(c) {}",
10174                Style);
10175 
10176   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10177   Style.ConstructorInitializerIndentWidth = 4;
10178   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10179   verifyFormat(
10180       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10181       Style);
10182   verifyFormat(
10183       "SomeClass::Constructor()\n"
10184       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10185       Style);
10186   Style.ConstructorInitializerIndentWidth = 4;
10187   Style.ColumnLimit = 60;
10188   verifyFormat("SomeClass::Constructor()\n"
10189                "    : aaaaaaaa(aaaaaaaa)\n"
10190                "    , aaaaaaaa(aaaaaaaa)\n"
10191                "    , aaaaaaaa(aaaaaaaa) {}",
10192                Style);
10193 }
10194 
10195 TEST_F(FormatTest, Destructors) {
10196   verifyFormat("void F(int &i) { i.~int(); }");
10197   verifyFormat("void F(int &i) { i->~int(); }");
10198 }
10199 
10200 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10201   FormatStyle Style = getWebKitStyle();
10202 
10203   // Don't indent in outer namespaces.
10204   verifyFormat("namespace outer {\n"
10205                "int i;\n"
10206                "namespace inner {\n"
10207                "    int i;\n"
10208                "} // namespace inner\n"
10209                "} // namespace outer\n"
10210                "namespace other_outer {\n"
10211                "int i;\n"
10212                "}",
10213                Style);
10214 
10215   // Don't indent case labels.
10216   verifyFormat("switch (variable) {\n"
10217                "case 1:\n"
10218                "case 2:\n"
10219                "    doSomething();\n"
10220                "    break;\n"
10221                "default:\n"
10222                "    ++variable;\n"
10223                "}",
10224                Style);
10225 
10226   // Wrap before binary operators.
10227   EXPECT_EQ("void f()\n"
10228             "{\n"
10229             "    if (aaaaaaaaaaaaaaaa\n"
10230             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10231             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10232             "        return;\n"
10233             "}",
10234             format("void f() {\n"
10235                    "if (aaaaaaaaaaaaaaaa\n"
10236                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10237                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10238                    "return;\n"
10239                    "}",
10240                    Style));
10241 
10242   // Allow functions on a single line.
10243   verifyFormat("void f() { return; }", Style);
10244 
10245   // Constructor initializers are formatted one per line with the "," on the
10246   // new line.
10247   verifyFormat("Constructor()\n"
10248                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10249                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10250                "          aaaaaaaaaaaaaa)\n"
10251                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10252                "{\n"
10253                "}",
10254                Style);
10255   verifyFormat("SomeClass::Constructor()\n"
10256                "    : a(a)\n"
10257                "{\n"
10258                "}",
10259                Style);
10260   EXPECT_EQ("SomeClass::Constructor()\n"
10261             "    : a(a)\n"
10262             "{\n"
10263             "}",
10264             format("SomeClass::Constructor():a(a){}", Style));
10265   verifyFormat("SomeClass::Constructor()\n"
10266                "    : a(a)\n"
10267                "    , b(b)\n"
10268                "    , c(c)\n"
10269                "{\n"
10270                "}",
10271                Style);
10272   verifyFormat("SomeClass::Constructor()\n"
10273                "    : a(a)\n"
10274                "{\n"
10275                "    foo();\n"
10276                "    bar();\n"
10277                "}",
10278                Style);
10279 
10280   // Access specifiers should be aligned left.
10281   verifyFormat("class C {\n"
10282                "public:\n"
10283                "    int i;\n"
10284                "};",
10285                Style);
10286 
10287   // Do not align comments.
10288   verifyFormat("int a; // Do not\n"
10289                "double b; // align comments.",
10290                Style);
10291 
10292   // Do not align operands.
10293   EXPECT_EQ("ASSERT(aaaa\n"
10294             "    || bbbb);",
10295             format("ASSERT ( aaaa\n||bbbb);", Style));
10296 
10297   // Accept input's line breaks.
10298   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10299             "    || bbbbbbbbbbbbbbb) {\n"
10300             "    i++;\n"
10301             "}",
10302             format("if (aaaaaaaaaaaaaaa\n"
10303                    "|| bbbbbbbbbbbbbbb) { i++; }",
10304                    Style));
10305   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10306             "    i++;\n"
10307             "}",
10308             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10309 
10310   // Don't automatically break all macro definitions (llvm.org/PR17842).
10311   verifyFormat("#define aNumber 10", Style);
10312   // However, generally keep the line breaks that the user authored.
10313   EXPECT_EQ("#define aNumber \\\n"
10314             "    10",
10315             format("#define aNumber \\\n"
10316                    " 10",
10317                    Style));
10318 
10319   // Keep empty and one-element array literals on a single line.
10320   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10321             "                                  copyItems:YES];",
10322             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10323                    "copyItems:YES];",
10324                    Style));
10325   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10326             "                                  copyItems:YES];",
10327             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10328                    "             copyItems:YES];",
10329                    Style));
10330   // FIXME: This does not seem right, there should be more indentation before
10331   // the array literal's entries. Nested blocks have the same problem.
10332   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10333             "    @\"a\",\n"
10334             "    @\"a\"\n"
10335             "]\n"
10336             "                                  copyItems:YES];",
10337             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10338                    "     @\"a\",\n"
10339                    "     @\"a\"\n"
10340                    "     ]\n"
10341                    "       copyItems:YES];",
10342                    Style));
10343   EXPECT_EQ(
10344       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10345       "                                  copyItems:YES];",
10346       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10347              "   copyItems:YES];",
10348              Style));
10349 
10350   verifyFormat("[self.a b:c c:d];", Style);
10351   EXPECT_EQ("[self.a b:c\n"
10352             "        c:d];",
10353             format("[self.a b:c\n"
10354                    "c:d];",
10355                    Style));
10356 }
10357 
10358 TEST_F(FormatTest, FormatsLambdas) {
10359   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10360   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10361   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10362   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10363   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10364   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10365   verifyFormat("void f() {\n"
10366                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10367                "}\n");
10368   verifyFormat("void f() {\n"
10369                "  other(x.begin(), //\n"
10370                "        x.end(),   //\n"
10371                "        [&](int, int) { return 1; });\n"
10372                "}\n");
10373   verifyFormat("SomeFunction([]() { // A cool function...\n"
10374                "  return 43;\n"
10375                "});");
10376   EXPECT_EQ("SomeFunction([]() {\n"
10377             "#define A a\n"
10378             "  return 43;\n"
10379             "});",
10380             format("SomeFunction([](){\n"
10381                    "#define A a\n"
10382                    "return 43;\n"
10383                    "});"));
10384   verifyFormat("void f() {\n"
10385                "  SomeFunction([](decltype(x), A *a) {});\n"
10386                "}");
10387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10388                "    [](const aaaaaaaaaa &a) { return a; });");
10389   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10390                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10391                "});");
10392   verifyFormat("Constructor()\n"
10393                "    : Field([] { // comment\n"
10394                "        int i;\n"
10395                "      }) {}");
10396   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10397                "  return some_parameter.size();\n"
10398                "};");
10399   verifyFormat("int i = aaaaaa ? 1 //\n"
10400                "               : [] {\n"
10401                "                   return 2; //\n"
10402                "                 }();");
10403   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10404                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10405                "                  return x == 2; // force break\n"
10406                "                });");
10407   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10408                "    int iiiiiiiiiiii) {\n"
10409                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10410                "});",
10411                getLLVMStyleWithColumns(60));
10412   verifyFormat("SomeFunction({[&] {\n"
10413                "                // comment\n"
10414                "              },\n"
10415                "              [&] {\n"
10416                "                // comment\n"
10417                "              }});");
10418   verifyFormat("SomeFunction({[&] {\n"
10419                "  // comment\n"
10420                "}});");
10421   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10422                "                             [&]() { return true; },\n"
10423                "                         aaaaa aaaaaaaaa);");
10424 
10425   // Lambdas with return types.
10426   verifyFormat("int c = []() -> int { return 2; }();\n");
10427   verifyFormat("int c = []() -> int * { return 2; }();\n");
10428   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10429   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10430   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10431   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10432   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10433   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10434   verifyFormat("[a, a]() -> a<1> {};");
10435   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10436                "                   int j) -> int {\n"
10437                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10438                "};");
10439   verifyFormat(
10440       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10441       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10442       "      return aaaaaaaaaaaaaaaaa;\n"
10443       "    });",
10444       getLLVMStyleWithColumns(70));
10445 
10446   // Multiple lambdas in the same parentheses change indentation rules.
10447   verifyFormat("SomeFunction(\n"
10448                "    []() {\n"
10449                "      int i = 42;\n"
10450                "      return i;\n"
10451                "    },\n"
10452                "    []() {\n"
10453                "      int j = 43;\n"
10454                "      return j;\n"
10455                "    });");
10456 
10457   // More complex introducers.
10458   verifyFormat("return [i, args...] {};");
10459 
10460   // Not lambdas.
10461   verifyFormat("constexpr char hello[]{\"hello\"};");
10462   verifyFormat("double &operator[](int i) { return 0; }\n"
10463                "int i;");
10464   verifyFormat("std::unique_ptr<int[]> foo() {}");
10465   verifyFormat("int i = a[a][a]->f();");
10466   verifyFormat("int i = (*b)[a]->f();");
10467 
10468   // Other corner cases.
10469   verifyFormat("void f() {\n"
10470                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10471                "      );\n"
10472                "}");
10473 
10474   // Lambdas created through weird macros.
10475   verifyFormat("void f() {\n"
10476                "  MACRO((const AA &a) { return 1; });\n"
10477                "}");
10478 
10479   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10480                "      doo_dah();\n"
10481                "      doo_dah();\n"
10482                "    })) {\n"
10483                "}");
10484   verifyFormat("auto lambda = []() {\n"
10485                "  int a = 2\n"
10486                "#if A\n"
10487                "          + 2\n"
10488                "#endif\n"
10489                "      ;\n"
10490                "};");
10491 }
10492 
10493 TEST_F(FormatTest, FormatsBlocks) {
10494   FormatStyle ShortBlocks = getLLVMStyle();
10495   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10496   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10497   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10498   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10499   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10500   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10501   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10502 
10503   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10504   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10505   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10506 
10507   verifyFormat("[operation setCompletionBlock:^{\n"
10508                "  [self onOperationDone];\n"
10509                "}];");
10510   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10511                "  [self onOperationDone];\n"
10512                "}]};");
10513   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10514                "  f();\n"
10515                "}];");
10516   verifyFormat("int a = [operation block:^int(int *i) {\n"
10517                "  return 1;\n"
10518                "}];");
10519   verifyFormat("[myObject doSomethingWith:arg1\n"
10520                "                      aaa:^int(int *a) {\n"
10521                "                        return 1;\n"
10522                "                      }\n"
10523                "                      bbb:f(a * bbbbbbbb)];");
10524 
10525   verifyFormat("[operation setCompletionBlock:^{\n"
10526                "  [self.delegate newDataAvailable];\n"
10527                "}];",
10528                getLLVMStyleWithColumns(60));
10529   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10530                "  NSString *path = [self sessionFilePath];\n"
10531                "  if (path) {\n"
10532                "    // ...\n"
10533                "  }\n"
10534                "});");
10535   verifyFormat("[[SessionService sharedService]\n"
10536                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10537                "      if (window) {\n"
10538                "        [self windowDidLoad:window];\n"
10539                "      } else {\n"
10540                "        [self errorLoadingWindow];\n"
10541                "      }\n"
10542                "    }];");
10543   verifyFormat("void (^largeBlock)(void) = ^{\n"
10544                "  // ...\n"
10545                "};\n",
10546                getLLVMStyleWithColumns(40));
10547   verifyFormat("[[SessionService sharedService]\n"
10548                "    loadWindowWithCompletionBlock: //\n"
10549                "        ^(SessionWindow *window) {\n"
10550                "          if (window) {\n"
10551                "            [self windowDidLoad:window];\n"
10552                "          } else {\n"
10553                "            [self errorLoadingWindow];\n"
10554                "          }\n"
10555                "        }];",
10556                getLLVMStyleWithColumns(60));
10557   verifyFormat("[myObject doSomethingWith:arg1\n"
10558                "    firstBlock:^(Foo *a) {\n"
10559                "      // ...\n"
10560                "      int i;\n"
10561                "    }\n"
10562                "    secondBlock:^(Bar *b) {\n"
10563                "      // ...\n"
10564                "      int i;\n"
10565                "    }\n"
10566                "    thirdBlock:^Foo(Bar *b) {\n"
10567                "      // ...\n"
10568                "      int i;\n"
10569                "    }];");
10570   verifyFormat("[myObject doSomethingWith:arg1\n"
10571                "               firstBlock:-1\n"
10572                "              secondBlock:^(Bar *b) {\n"
10573                "                // ...\n"
10574                "                int i;\n"
10575                "              }];");
10576 
10577   verifyFormat("f(^{\n"
10578                "  @autoreleasepool {\n"
10579                "    if (a) {\n"
10580                "      g();\n"
10581                "    }\n"
10582                "  }\n"
10583                "});");
10584   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10585 
10586   FormatStyle FourIndent = getLLVMStyle();
10587   FourIndent.ObjCBlockIndentWidth = 4;
10588   verifyFormat("[operation setCompletionBlock:^{\n"
10589                "    [self onOperationDone];\n"
10590                "}];",
10591                FourIndent);
10592 }
10593 
10594 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10595   FormatStyle ZeroColumn = getLLVMStyle();
10596   ZeroColumn.ColumnLimit = 0;
10597 
10598   verifyFormat("[[SessionService sharedService] "
10599                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10600                "  if (window) {\n"
10601                "    [self windowDidLoad:window];\n"
10602                "  } else {\n"
10603                "    [self errorLoadingWindow];\n"
10604                "  }\n"
10605                "}];",
10606                ZeroColumn);
10607   EXPECT_EQ("[[SessionService sharedService]\n"
10608             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10609             "      if (window) {\n"
10610             "        [self windowDidLoad:window];\n"
10611             "      } else {\n"
10612             "        [self errorLoadingWindow];\n"
10613             "      }\n"
10614             "    }];",
10615             format("[[SessionService sharedService]\n"
10616                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10617                    "                if (window) {\n"
10618                    "    [self windowDidLoad:window];\n"
10619                    "  } else {\n"
10620                    "    [self errorLoadingWindow];\n"
10621                    "  }\n"
10622                    "}];",
10623                    ZeroColumn));
10624   verifyFormat("[myObject doSomethingWith:arg1\n"
10625                "    firstBlock:^(Foo *a) {\n"
10626                "      // ...\n"
10627                "      int i;\n"
10628                "    }\n"
10629                "    secondBlock:^(Bar *b) {\n"
10630                "      // ...\n"
10631                "      int i;\n"
10632                "    }\n"
10633                "    thirdBlock:^Foo(Bar *b) {\n"
10634                "      // ...\n"
10635                "      int i;\n"
10636                "    }];",
10637                ZeroColumn);
10638   verifyFormat("f(^{\n"
10639                "  @autoreleasepool {\n"
10640                "    if (a) {\n"
10641                "      g();\n"
10642                "    }\n"
10643                "  }\n"
10644                "});",
10645                ZeroColumn);
10646   verifyFormat("void (^largeBlock)(void) = ^{\n"
10647                "  // ...\n"
10648                "};",
10649                ZeroColumn);
10650 
10651   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10652   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10653             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10654   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10655   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10656             "  int i;\n"
10657             "};",
10658             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10659 }
10660 
10661 TEST_F(FormatTest, SupportsCRLF) {
10662   EXPECT_EQ("int a;\r\n"
10663             "int b;\r\n"
10664             "int c;\r\n",
10665             format("int a;\r\n"
10666                    "  int b;\r\n"
10667                    "    int c;\r\n",
10668                    getLLVMStyle()));
10669   EXPECT_EQ("int a;\r\n"
10670             "int b;\r\n"
10671             "int c;\r\n",
10672             format("int a;\r\n"
10673                    "  int b;\n"
10674                    "    int c;\r\n",
10675                    getLLVMStyle()));
10676   EXPECT_EQ("int a;\n"
10677             "int b;\n"
10678             "int c;\n",
10679             format("int a;\r\n"
10680                    "  int b;\n"
10681                    "    int c;\n",
10682                    getLLVMStyle()));
10683   EXPECT_EQ("\"aaaaaaa \"\r\n"
10684             "\"bbbbbbb\";\r\n",
10685             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10686   EXPECT_EQ("#define A \\\r\n"
10687             "  b;      \\\r\n"
10688             "  c;      \\\r\n"
10689             "  d;\r\n",
10690             format("#define A \\\r\n"
10691                    "  b; \\\r\n"
10692                    "  c; d; \r\n",
10693                    getGoogleStyle()));
10694 
10695   EXPECT_EQ("/*\r\n"
10696             "multi line block comments\r\n"
10697             "should not introduce\r\n"
10698             "an extra carriage return\r\n"
10699             "*/\r\n",
10700             format("/*\r\n"
10701                    "multi line block comments\r\n"
10702                    "should not introduce\r\n"
10703                    "an extra carriage return\r\n"
10704                    "*/\r\n"));
10705 }
10706 
10707 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10708   verifyFormat("MY_CLASS(C) {\n"
10709                "  int i;\n"
10710                "  int j;\n"
10711                "};");
10712 }
10713 
10714 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10715   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10716   TwoIndent.ContinuationIndentWidth = 2;
10717 
10718   EXPECT_EQ("int i =\n"
10719             "  longFunction(\n"
10720             "    arg);",
10721             format("int i = longFunction(arg);", TwoIndent));
10722 
10723   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10724   SixIndent.ContinuationIndentWidth = 6;
10725 
10726   EXPECT_EQ("int i =\n"
10727             "      longFunction(\n"
10728             "            arg);",
10729             format("int i = longFunction(arg);", SixIndent));
10730 }
10731 
10732 TEST_F(FormatTest, SpacesInAngles) {
10733   FormatStyle Spaces = getLLVMStyle();
10734   Spaces.SpacesInAngles = true;
10735 
10736   verifyFormat("static_cast< int >(arg);", Spaces);
10737   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10738   verifyFormat("f< int, float >();", Spaces);
10739   verifyFormat("template <> g() {}", Spaces);
10740   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10741   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10742   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10743                Spaces);
10744 
10745   Spaces.Standard = FormatStyle::LS_Cpp03;
10746   Spaces.SpacesInAngles = true;
10747   verifyFormat("A< A< int > >();", Spaces);
10748 
10749   Spaces.SpacesInAngles = false;
10750   verifyFormat("A<A<int> >();", Spaces);
10751 
10752   Spaces.Standard = FormatStyle::LS_Cpp11;
10753   Spaces.SpacesInAngles = true;
10754   verifyFormat("A< A< int > >();", Spaces);
10755 
10756   Spaces.SpacesInAngles = false;
10757   verifyFormat("A<A<int>>();", Spaces);
10758 }
10759 
10760 TEST_F(FormatTest, TripleAngleBrackets) {
10761   verifyFormat("f<<<1, 1>>>();");
10762   verifyFormat("f<<<1, 1, 1, s>>>();");
10763   verifyFormat("f<<<a, b, c, d>>>();");
10764   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10765   verifyFormat("f<param><<<1, 1>>>();");
10766   verifyFormat("f<1><<<1, 1>>>();");
10767   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10768   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10769                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10770 }
10771 
10772 TEST_F(FormatTest, MergeLessLessAtEnd) {
10773   verifyFormat("<<");
10774   EXPECT_EQ("< < <", format("\\\n<<<"));
10775   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10776                "aaallvm::outs() <<");
10777   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10778                "aaaallvm::outs()\n    <<");
10779 }
10780 
10781 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10782   std::string code = "#if A\n"
10783                      "#if B\n"
10784                      "a.\n"
10785                      "#endif\n"
10786                      "    a = 1;\n"
10787                      "#else\n"
10788                      "#endif\n"
10789                      "#if C\n"
10790                      "#else\n"
10791                      "#endif\n";
10792   EXPECT_EQ(code, format(code));
10793 }
10794 
10795 TEST_F(FormatTest, HandleConflictMarkers) {
10796   // Git/SVN conflict markers.
10797   EXPECT_EQ("int a;\n"
10798             "void f() {\n"
10799             "  callme(some(parameter1,\n"
10800             "<<<<<<< text by the vcs\n"
10801             "              parameter2),\n"
10802             "||||||| text by the vcs\n"
10803             "              parameter2),\n"
10804             "         parameter3,\n"
10805             "======= text by the vcs\n"
10806             "              parameter2, parameter3),\n"
10807             ">>>>>>> text by the vcs\n"
10808             "         otherparameter);\n",
10809             format("int a;\n"
10810                    "void f() {\n"
10811                    "  callme(some(parameter1,\n"
10812                    "<<<<<<< text by the vcs\n"
10813                    "  parameter2),\n"
10814                    "||||||| text by the vcs\n"
10815                    "  parameter2),\n"
10816                    "  parameter3,\n"
10817                    "======= text by the vcs\n"
10818                    "  parameter2,\n"
10819                    "  parameter3),\n"
10820                    ">>>>>>> text by the vcs\n"
10821                    "  otherparameter);\n"));
10822 
10823   // Perforce markers.
10824   EXPECT_EQ("void f() {\n"
10825             "  function(\n"
10826             ">>>> text by the vcs\n"
10827             "      parameter,\n"
10828             "==== text by the vcs\n"
10829             "      parameter,\n"
10830             "==== text by the vcs\n"
10831             "      parameter,\n"
10832             "<<<< text by the vcs\n"
10833             "      parameter);\n",
10834             format("void f() {\n"
10835                    "  function(\n"
10836                    ">>>> text by the vcs\n"
10837                    "  parameter,\n"
10838                    "==== text by the vcs\n"
10839                    "  parameter,\n"
10840                    "==== text by the vcs\n"
10841                    "  parameter,\n"
10842                    "<<<< text by the vcs\n"
10843                    "  parameter);\n"));
10844 
10845   EXPECT_EQ("<<<<<<<\n"
10846             "|||||||\n"
10847             "=======\n"
10848             ">>>>>>>",
10849             format("<<<<<<<\n"
10850                    "|||||||\n"
10851                    "=======\n"
10852                    ">>>>>>>"));
10853 
10854   EXPECT_EQ("<<<<<<<\n"
10855             "|||||||\n"
10856             "int i;\n"
10857             "=======\n"
10858             ">>>>>>>",
10859             format("<<<<<<<\n"
10860                    "|||||||\n"
10861                    "int i;\n"
10862                    "=======\n"
10863                    ">>>>>>>"));
10864 
10865   // FIXME: Handle parsing of macros around conflict markers correctly:
10866   EXPECT_EQ("#define Macro \\\n"
10867             "<<<<<<<\n"
10868             "Something \\\n"
10869             "|||||||\n"
10870             "Else \\\n"
10871             "=======\n"
10872             "Other \\\n"
10873             ">>>>>>>\n"
10874             "    End int i;\n",
10875             format("#define Macro \\\n"
10876                    "<<<<<<<\n"
10877                    "  Something \\\n"
10878                    "|||||||\n"
10879                    "  Else \\\n"
10880                    "=======\n"
10881                    "  Other \\\n"
10882                    ">>>>>>>\n"
10883                    "  End\n"
10884                    "int i;\n"));
10885 }
10886 
10887 TEST_F(FormatTest, DisableRegions) {
10888   EXPECT_EQ("int i;\n"
10889             "// clang-format off\n"
10890             "  int j;\n"
10891             "// clang-format on\n"
10892             "int k;",
10893             format(" int  i;\n"
10894                    "   // clang-format off\n"
10895                    "  int j;\n"
10896                    " // clang-format on\n"
10897                    "   int   k;"));
10898   EXPECT_EQ("int i;\n"
10899             "/* clang-format off */\n"
10900             "  int j;\n"
10901             "/* clang-format on */\n"
10902             "int k;",
10903             format(" int  i;\n"
10904                    "   /* clang-format off */\n"
10905                    "  int j;\n"
10906                    " /* clang-format on */\n"
10907                    "   int   k;"));
10908 }
10909 
10910 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10911   format("? ) =");
10912   verifyNoCrash("#define a\\\n /**/}");
10913 }
10914 
10915 } // end namespace
10916 } // end namespace format
10917 } // end namespace clang
10918