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 "clang/Format/Format.h"
11 
12 #include "../Tooling/RewriterTestContext.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 namespace clang {
23 namespace format {
24 namespace {
25 
26 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
27 
28 class FormatTest : public ::testing::Test {
29 protected:
30   enum IncompleteCheck {
31     IC_ExpectComplete,
32     IC_ExpectIncomplete,
33     IC_DoNotCheck
34   };
35 
36   std::string format(llvm::StringRef Code,
37                      const FormatStyle &Style = getLLVMStyle(),
38                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
39     DEBUG(llvm::errs() << "---\n");
40     DEBUG(llvm::errs() << Code << "\n\n");
41     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
42     bool IncompleteFormat = false;
43     tooling::Replacements Replaces =
44         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
45     if (CheckIncomplete != IC_DoNotCheck) {
46       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
47       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
48     }
49     ReplacementCount = Replaces.size();
50     std::string Result = applyAllReplacements(Code, Replaces);
51     EXPECT_NE("", Result);
52     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
53     return Result;
54   }
55 
56   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
57     FormatStyle Style = getLLVMStyle();
58     Style.ColumnLimit = ColumnLimit;
59     return Style;
60   }
61 
62   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
63     FormatStyle Style = getGoogleStyle();
64     Style.ColumnLimit = ColumnLimit;
65     return Style;
66   }
67 
68   void verifyFormat(llvm::StringRef Code,
69                     const FormatStyle &Style = getLLVMStyle()) {
70     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
71   }
72 
73   void verifyIncompleteFormat(llvm::StringRef Code,
74                               const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Code.str(),
76               format(test::messUp(Code), Style, IC_ExpectIncomplete));
77   }
78 
79   void verifyGoogleFormat(llvm::StringRef Code) {
80     verifyFormat(Code, getGoogleStyle());
81   }
82 
83   void verifyIndependentOfContext(llvm::StringRef text) {
84     verifyFormat(text);
85     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
86   }
87 
88   /// \brief Verify that clang-format does not crash on the given input.
89   void verifyNoCrash(llvm::StringRef Code,
90                      const FormatStyle &Style = getLLVMStyle()) {
91     format(Code, Style, IC_DoNotCheck);
92   }
93 
94   int ReplacementCount;
95 };
96 
97 TEST_F(FormatTest, MessUp) {
98   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
99   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
100   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
101   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
102   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
103 }
104 
105 //===----------------------------------------------------------------------===//
106 // Basic function tests.
107 //===----------------------------------------------------------------------===//
108 
109 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
110   EXPECT_EQ(";", format(";"));
111 }
112 
113 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
114   EXPECT_EQ("int i;", format("  int i;"));
115   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
116   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
117   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
118 }
119 
120 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
121   EXPECT_EQ("int i;", format("int\ni;"));
122 }
123 
124 TEST_F(FormatTest, FormatsNestedBlockStatements) {
125   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
126 }
127 
128 TEST_F(FormatTest, FormatsNestedCall) {
129   verifyFormat("Method(f1, f2(f3));");
130   verifyFormat("Method(f1(f2, f3()));");
131   verifyFormat("Method(f1(f2, (f3())));");
132 }
133 
134 TEST_F(FormatTest, NestedNameSpecifiers) {
135   verifyFormat("vector<::Type> v;");
136   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
137   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
138   verifyFormat("bool a = 2 < ::SomeFunction();");
139 }
140 
141 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
142   EXPECT_EQ("if (a) {\n"
143             "  f();\n"
144             "}",
145             format("if(a){f();}"));
146   EXPECT_EQ(4, ReplacementCount);
147   EXPECT_EQ("if (a) {\n"
148             "  f();\n"
149             "}",
150             format("if (a) {\n"
151                    "  f();\n"
152                    "}"));
153   EXPECT_EQ(0, ReplacementCount);
154   EXPECT_EQ("/*\r\n"
155             "\r\n"
156             "*/\r\n",
157             format("/*\r\n"
158                    "\r\n"
159                    "*/\r\n"));
160   EXPECT_EQ(0, ReplacementCount);
161 }
162 
163 TEST_F(FormatTest, RemovesEmptyLines) {
164   EXPECT_EQ("class C {\n"
165             "  int i;\n"
166             "};",
167             format("class C {\n"
168                    " int i;\n"
169                    "\n"
170                    "};"));
171 
172   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
173   EXPECT_EQ("namespace N {\n"
174             "\n"
175             "int i;\n"
176             "}",
177             format("namespace N {\n"
178                    "\n"
179                    "int    i;\n"
180                    "}",
181                    getGoogleStyle()));
182   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
183             "\n"
184             "int i;\n"
185             "}",
186             format("extern /**/ \"C\" /**/ {\n"
187                    "\n"
188                    "int    i;\n"
189                    "}",
190                    getGoogleStyle()));
191 
192   // ...but do keep inlining and removing empty lines for non-block extern "C"
193   // functions.
194   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
195   EXPECT_EQ("extern \"C\" int f() {\n"
196             "  int i = 42;\n"
197             "  return i;\n"
198             "}",
199             format("extern \"C\" int f() {\n"
200                    "\n"
201                    "  int i = 42;\n"
202                    "  return i;\n"
203                    "}",
204                    getGoogleStyle()));
205 
206   // Remove empty lines at the beginning and end of blocks.
207   EXPECT_EQ("void f() {\n"
208             "\n"
209             "  if (a) {\n"
210             "\n"
211             "    f();\n"
212             "  }\n"
213             "}",
214             format("void f() {\n"
215                    "\n"
216                    "  if (a) {\n"
217                    "\n"
218                    "    f();\n"
219                    "\n"
220                    "  }\n"
221                    "\n"
222                    "}",
223                    getLLVMStyle()));
224   EXPECT_EQ("void f() {\n"
225             "  if (a) {\n"
226             "    f();\n"
227             "  }\n"
228             "}",
229             format("void f() {\n"
230                    "\n"
231                    "  if (a) {\n"
232                    "\n"
233                    "    f();\n"
234                    "\n"
235                    "  }\n"
236                    "\n"
237                    "}",
238                    getGoogleStyle()));
239 
240   // Don't remove empty lines in more complex control statements.
241   EXPECT_EQ("void f() {\n"
242             "  if (a) {\n"
243             "    f();\n"
244             "\n"
245             "  } else if (b) {\n"
246             "    f();\n"
247             "  }\n"
248             "}",
249             format("void f() {\n"
250                    "  if (a) {\n"
251                    "    f();\n"
252                    "\n"
253                    "  } else if (b) {\n"
254                    "    f();\n"
255                    "\n"
256                    "  }\n"
257                    "\n"
258                    "}"));
259 
260   // FIXME: This is slightly inconsistent.
261   EXPECT_EQ("namespace {\n"
262             "int i;\n"
263             "}",
264             format("namespace {\n"
265                    "int i;\n"
266                    "\n"
267                    "}"));
268   EXPECT_EQ("namespace {\n"
269             "int i;\n"
270             "\n"
271             "} // namespace",
272             format("namespace {\n"
273                    "int i;\n"
274                    "\n"
275                    "}  // namespace"));
276 }
277 
278 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
279   verifyFormat("x = (a) and (b);");
280   verifyFormat("x = (a) or (b);");
281   verifyFormat("x = (a) bitand (b);");
282   verifyFormat("x = (a) bitor (b);");
283   verifyFormat("x = (a) not_eq (b);");
284   verifyFormat("x = (a) and_eq (b);");
285   verifyFormat("x = (a) or_eq (b);");
286   verifyFormat("x = (a) xor (b);");
287 }
288 
289 //===----------------------------------------------------------------------===//
290 // Tests for control statements.
291 //===----------------------------------------------------------------------===//
292 
293 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
294   verifyFormat("if (true)\n  f();\ng();");
295   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
296   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
297 
298   FormatStyle AllowsMergedIf = getLLVMStyle();
299   AllowsMergedIf.AlignEscapedNewlinesLeft = true;
300   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
301   verifyFormat("if (a)\n"
302                "  // comment\n"
303                "  f();",
304                AllowsMergedIf);
305   verifyFormat("{\n"
306                "  if (a)\n"
307                "  label:\n"
308                "    f();\n"
309                "}",
310                AllowsMergedIf);
311   verifyFormat("#define A \\\n"
312                "  if (a)  \\\n"
313                "  label:  \\\n"
314                "    f()",
315                AllowsMergedIf);
316   verifyFormat("if (a)\n"
317                "  ;",
318                AllowsMergedIf);
319   verifyFormat("if (a)\n"
320                "  if (b) return;",
321                AllowsMergedIf);
322 
323   verifyFormat("if (a) // Can't merge this\n"
324                "  f();\n",
325                AllowsMergedIf);
326   verifyFormat("if (a) /* still don't merge */\n"
327                "  f();",
328                AllowsMergedIf);
329   verifyFormat("if (a) { // Never merge this\n"
330                "  f();\n"
331                "}",
332                AllowsMergedIf);
333   verifyFormat("if (a) { /* Never merge this */\n"
334                "  f();\n"
335                "}",
336                AllowsMergedIf);
337 
338   AllowsMergedIf.ColumnLimit = 14;
339   verifyFormat("if (a) return;", AllowsMergedIf);
340   verifyFormat("if (aaaaaaaaa)\n"
341                "  return;",
342                AllowsMergedIf);
343 
344   AllowsMergedIf.ColumnLimit = 13;
345   verifyFormat("if (a)\n  return;", AllowsMergedIf);
346 }
347 
348 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
349   FormatStyle AllowsMergedLoops = getLLVMStyle();
350   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
351   verifyFormat("while (true) continue;", AllowsMergedLoops);
352   verifyFormat("for (;;) continue;", AllowsMergedLoops);
353   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
354   verifyFormat("while (true)\n"
355                "  ;",
356                AllowsMergedLoops);
357   verifyFormat("for (;;)\n"
358                "  ;",
359                AllowsMergedLoops);
360   verifyFormat("for (;;)\n"
361                "  for (;;) continue;",
362                AllowsMergedLoops);
363   verifyFormat("for (;;) // Can't merge this\n"
364                "  continue;",
365                AllowsMergedLoops);
366   verifyFormat("for (;;) /* still don't merge */\n"
367                "  continue;",
368                AllowsMergedLoops);
369 }
370 
371 TEST_F(FormatTest, FormatShortBracedStatements) {
372   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
373   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
374 
375   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
376   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
377 
378   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
379   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
380   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
381   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
382   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
383   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
384   verifyFormat("if (true) { //\n"
385                "  f();\n"
386                "}",
387                AllowSimpleBracedStatements);
388   verifyFormat("if (true) {\n"
389                "  f();\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   verifyFormat("template <int> struct A2 {\n"
401                "  struct B {};\n"
402                "};",
403                AllowSimpleBracedStatements);
404 
405   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
406   verifyFormat("if (true) {\n"
407                "  f();\n"
408                "}",
409                AllowSimpleBracedStatements);
410   verifyFormat("if (true) {\n"
411                "  f();\n"
412                "} else {\n"
413                "  f();\n"
414                "}",
415                AllowSimpleBracedStatements);
416 
417   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
418   verifyFormat("while (true) {\n"
419                "  f();\n"
420                "}",
421                AllowSimpleBracedStatements);
422   verifyFormat("for (;;) {\n"
423                "  f();\n"
424                "}",
425                AllowSimpleBracedStatements);
426 }
427 
428 TEST_F(FormatTest, ParseIfElse) {
429   verifyFormat("if (true)\n"
430                "  if (true)\n"
431                "    if (true)\n"
432                "      f();\n"
433                "    else\n"
434                "      g();\n"
435                "  else\n"
436                "    h();\n"
437                "else\n"
438                "  i();");
439   verifyFormat("if (true)\n"
440                "  if (true)\n"
441                "    if (true) {\n"
442                "      if (true)\n"
443                "        f();\n"
444                "    } else {\n"
445                "      g();\n"
446                "    }\n"
447                "  else\n"
448                "    h();\n"
449                "else {\n"
450                "  i();\n"
451                "}");
452   verifyFormat("void f() {\n"
453                "  if (a) {\n"
454                "  } else {\n"
455                "  }\n"
456                "}");
457 }
458 
459 TEST_F(FormatTest, ElseIf) {
460   verifyFormat("if (a) {\n} else if (b) {\n}");
461   verifyFormat("if (a)\n"
462                "  f();\n"
463                "else if (b)\n"
464                "  g();\n"
465                "else\n"
466                "  h();");
467   verifyFormat("if (a) {\n"
468                "  f();\n"
469                "}\n"
470                "// or else ..\n"
471                "else {\n"
472                "  g()\n"
473                "}");
474 
475   verifyFormat("if (a) {\n"
476                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
477                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
478                "}");
479   verifyFormat("if (a) {\n"
480                "} else if (\n"
481                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
482                "}",
483                getLLVMStyleWithColumns(62));
484 }
485 
486 TEST_F(FormatTest, FormatsForLoop) {
487   verifyFormat(
488       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
489       "     ++VeryVeryLongLoopVariable)\n"
490       "  ;");
491   verifyFormat("for (;;)\n"
492                "  f();");
493   verifyFormat("for (;;) {\n}");
494   verifyFormat("for (;;) {\n"
495                "  f();\n"
496                "}");
497   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
498 
499   verifyFormat(
500       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
501       "                                          E = UnwrappedLines.end();\n"
502       "     I != E; ++I) {\n}");
503 
504   verifyFormat(
505       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
506       "     ++IIIII) {\n}");
507   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
508                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
509                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
510   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
511                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
512                "         E = FD->getDeclsInPrototypeScope().end();\n"
513                "     I != E; ++I) {\n}");
514   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
515                "         I = Container.begin(),\n"
516                "         E = Container.end();\n"
517                "     I != E; ++I) {\n}",
518                getLLVMStyleWithColumns(76));
519 
520   verifyFormat(
521       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
522       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
523       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
524       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
525       "     ++aaaaaaaaaaa) {\n}");
526   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
527                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
528                "     ++i) {\n}");
529   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
530                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
531                "}");
532   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
533                "         aaaaaaaaaa);\n"
534                "     iter; ++iter) {\n"
535                "}");
536   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
537                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
538                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
539                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
540 
541   FormatStyle NoBinPacking = getLLVMStyle();
542   NoBinPacking.BinPackParameters = false;
543   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
544                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
545                "                                           aaaaaaaaaaaaaaaa,\n"
546                "                                           aaaaaaaaaaaaaaaa,\n"
547                "                                           aaaaaaaaaaaaaaaa);\n"
548                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
549                "}",
550                NoBinPacking);
551   verifyFormat(
552       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
553       "                                          E = UnwrappedLines.end();\n"
554       "     I != E;\n"
555       "     ++I) {\n}",
556       NoBinPacking);
557 }
558 
559 TEST_F(FormatTest, RangeBasedForLoops) {
560   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
561                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
562   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
563                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
564   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
565                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
566   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
567                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
568 }
569 
570 TEST_F(FormatTest, ForEachLoops) {
571   verifyFormat("void f() {\n"
572                "  foreach (Item *item, itemlist) {}\n"
573                "  Q_FOREACH (Item *item, itemlist) {}\n"
574                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
575                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
576                "}");
577 
578   // As function-like macros.
579   verifyFormat("#define foreach(x, y)\n"
580                "#define Q_FOREACH(x, y)\n"
581                "#define BOOST_FOREACH(x, y)\n"
582                "#define UNKNOWN_FOREACH(x, y)\n");
583 
584   // Not as function-like macros.
585   verifyFormat("#define foreach (x, y)\n"
586                "#define Q_FOREACH (x, y)\n"
587                "#define BOOST_FOREACH (x, y)\n"
588                "#define UNKNOWN_FOREACH (x, y)\n");
589 }
590 
591 TEST_F(FormatTest, FormatsWhileLoop) {
592   verifyFormat("while (true) {\n}");
593   verifyFormat("while (true)\n"
594                "  f();");
595   verifyFormat("while () {\n}");
596   verifyFormat("while () {\n"
597                "  f();\n"
598                "}");
599 }
600 
601 TEST_F(FormatTest, FormatsDoWhile) {
602   verifyFormat("do {\n"
603                "  do_something();\n"
604                "} while (something());");
605   verifyFormat("do\n"
606                "  do_something();\n"
607                "while (something());");
608 }
609 
610 TEST_F(FormatTest, FormatsSwitchStatement) {
611   verifyFormat("switch (x) {\n"
612                "case 1:\n"
613                "  f();\n"
614                "  break;\n"
615                "case kFoo:\n"
616                "case ns::kBar:\n"
617                "case kBaz:\n"
618                "  break;\n"
619                "default:\n"
620                "  g();\n"
621                "  break;\n"
622                "}");
623   verifyFormat("switch (x) {\n"
624                "case 1: {\n"
625                "  f();\n"
626                "  break;\n"
627                "}\n"
628                "case 2: {\n"
629                "  break;\n"
630                "}\n"
631                "}");
632   verifyFormat("switch (x) {\n"
633                "case 1: {\n"
634                "  f();\n"
635                "  {\n"
636                "    g();\n"
637                "    h();\n"
638                "  }\n"
639                "  break;\n"
640                "}\n"
641                "}");
642   verifyFormat("switch (x) {\n"
643                "case 1: {\n"
644                "  f();\n"
645                "  if (foo) {\n"
646                "    g();\n"
647                "    h();\n"
648                "  }\n"
649                "  break;\n"
650                "}\n"
651                "}");
652   verifyFormat("switch (x) {\n"
653                "case 1: {\n"
654                "  f();\n"
655                "  g();\n"
656                "} break;\n"
657                "}");
658   verifyFormat("switch (test)\n"
659                "  ;");
660   verifyFormat("switch (x) {\n"
661                "default: {\n"
662                "  // Do nothing.\n"
663                "}\n"
664                "}");
665   verifyFormat("switch (x) {\n"
666                "// comment\n"
667                "// if 1, do f()\n"
668                "case 1:\n"
669                "  f();\n"
670                "}");
671   verifyFormat("switch (x) {\n"
672                "case 1:\n"
673                "  // Do amazing stuff\n"
674                "  {\n"
675                "    f();\n"
676                "    g();\n"
677                "  }\n"
678                "  break;\n"
679                "}");
680   verifyFormat("#define A          \\\n"
681                "  switch (x) {     \\\n"
682                "  case a:          \\\n"
683                "    foo = b;       \\\n"
684                "  }",
685                getLLVMStyleWithColumns(20));
686   verifyFormat("#define OPERATION_CASE(name)           \\\n"
687                "  case OP_name:                        \\\n"
688                "    return operations::Operation##name\n",
689                getLLVMStyleWithColumns(40));
690   verifyFormat("switch (x) {\n"
691                "case 1:;\n"
692                "default:;\n"
693                "  int i;\n"
694                "}");
695 
696   verifyGoogleFormat("switch (x) {\n"
697                      "  case 1:\n"
698                      "    f();\n"
699                      "    break;\n"
700                      "  case kFoo:\n"
701                      "  case ns::kBar:\n"
702                      "  case kBaz:\n"
703                      "    break;\n"
704                      "  default:\n"
705                      "    g();\n"
706                      "    break;\n"
707                      "}");
708   verifyGoogleFormat("switch (x) {\n"
709                      "  case 1: {\n"
710                      "    f();\n"
711                      "    break;\n"
712                      "  }\n"
713                      "}");
714   verifyGoogleFormat("switch (test)\n"
715                      "  ;");
716 
717   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
718                      "  case OP_name:              \\\n"
719                      "    return operations::Operation##name\n");
720   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
721                      "  // Get the correction operation class.\n"
722                      "  switch (OpCode) {\n"
723                      "    CASE(Add);\n"
724                      "    CASE(Subtract);\n"
725                      "    default:\n"
726                      "      return operations::Unknown;\n"
727                      "  }\n"
728                      "#undef OPERATION_CASE\n"
729                      "}");
730   verifyFormat("DEBUG({\n"
731                "  switch (x) {\n"
732                "  case A:\n"
733                "    f();\n"
734                "    break;\n"
735                "  // On B:\n"
736                "  case B:\n"
737                "    g();\n"
738                "    break;\n"
739                "  }\n"
740                "});");
741   verifyFormat("switch (a) {\n"
742                "case (b):\n"
743                "  return;\n"
744                "}");
745 
746   verifyFormat("switch (a) {\n"
747                "case some_namespace::\n"
748                "    some_constant:\n"
749                "  return;\n"
750                "}",
751                getLLVMStyleWithColumns(34));
752 }
753 
754 TEST_F(FormatTest, CaseRanges) {
755   verifyFormat("switch (x) {\n"
756                "case 'A' ... 'Z':\n"
757                "case 1 ... 5:\n"
758                "case a ... b:\n"
759                "  break;\n"
760                "}");
761 }
762 
763 TEST_F(FormatTest, ShortCaseLabels) {
764   FormatStyle Style = getLLVMStyle();
765   Style.AllowShortCaseLabelsOnASingleLine = true;
766   verifyFormat("switch (a) {\n"
767                "case 1: x = 1; break;\n"
768                "case 2: return;\n"
769                "case 3:\n"
770                "case 4:\n"
771                "case 5: return;\n"
772                "case 6: // comment\n"
773                "  return;\n"
774                "case 7:\n"
775                "  // comment\n"
776                "  return;\n"
777                "case 8:\n"
778                "  x = 8; // comment\n"
779                "  break;\n"
780                "default: y = 1; break;\n"
781                "}",
782                Style);
783   verifyFormat("switch (a) {\n"
784                "#if FOO\n"
785                "case 0: return 0;\n"
786                "#endif\n"
787                "}",
788                Style);
789   verifyFormat("switch (a) {\n"
790                "case 1: {\n"
791                "}\n"
792                "case 2: {\n"
793                "  return;\n"
794                "}\n"
795                "case 3: {\n"
796                "  x = 1;\n"
797                "  return;\n"
798                "}\n"
799                "case 4:\n"
800                "  if (x)\n"
801                "    return;\n"
802                "}",
803                Style);
804   Style.ColumnLimit = 21;
805   verifyFormat("switch (a) {\n"
806                "case 1: x = 1; break;\n"
807                "case 2: return;\n"
808                "case 3:\n"
809                "case 4:\n"
810                "case 5: return;\n"
811                "default:\n"
812                "  y = 1;\n"
813                "  break;\n"
814                "}",
815                Style);
816 }
817 
818 TEST_F(FormatTest, FormatsLabels) {
819   verifyFormat("void f() {\n"
820                "  some_code();\n"
821                "test_label:\n"
822                "  some_other_code();\n"
823                "  {\n"
824                "    some_more_code();\n"
825                "  another_label:\n"
826                "    some_more_code();\n"
827                "  }\n"
828                "}");
829   verifyFormat("{\n"
830                "  some_code();\n"
831                "test_label:\n"
832                "  some_other_code();\n"
833                "}");
834   verifyFormat("{\n"
835                "  some_code();\n"
836                "test_label:;\n"
837                "  int i = 0;\n"
838                "}");
839 }
840 
841 //===----------------------------------------------------------------------===//
842 // Tests for comments.
843 //===----------------------------------------------------------------------===//
844 
845 TEST_F(FormatTest, UnderstandsSingleLineComments) {
846   verifyFormat("//* */");
847   verifyFormat("// line 1\n"
848                "// line 2\n"
849                "void f() {}\n");
850 
851   verifyFormat("void f() {\n"
852                "  // Doesn't do anything\n"
853                "}");
854   verifyFormat("SomeObject\n"
855                "    // Calling someFunction on SomeObject\n"
856                "    .someFunction();");
857   verifyFormat("auto result = SomeObject\n"
858                "                  // Calling someFunction on SomeObject\n"
859                "                  .someFunction();");
860   verifyFormat("void f(int i,  // some comment (probably for i)\n"
861                "       int j,  // some comment (probably for j)\n"
862                "       int k); // some comment (probably for k)");
863   verifyFormat("void f(int i,\n"
864                "       // some comment (probably for j)\n"
865                "       int j,\n"
866                "       // some comment (probably for k)\n"
867                "       int k);");
868 
869   verifyFormat("int i    // This is a fancy variable\n"
870                "    = 5; // with nicely aligned comment.");
871 
872   verifyFormat("// Leading comment.\n"
873                "int a; // Trailing comment.");
874   verifyFormat("int a; // Trailing comment\n"
875                "       // on 2\n"
876                "       // or 3 lines.\n"
877                "int b;");
878   verifyFormat("int a; // Trailing comment\n"
879                "\n"
880                "// Leading comment.\n"
881                "int b;");
882   verifyFormat("int a;    // Comment.\n"
883                "          // More details.\n"
884                "int bbbb; // Another comment.");
885   verifyFormat(
886       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
887       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
888       "int cccccccccccccccccccccccccccccc;       // comment\n"
889       "int ddd;                     // looooooooooooooooooooooooong comment\n"
890       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
891       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
892       "int ccccccccccccccccccc;     // comment");
893 
894   verifyFormat("#include \"a\"     // comment\n"
895                "#include \"a/b/c\" // comment");
896   verifyFormat("#include <a>     // comment\n"
897                "#include <a/b/c> // comment");
898   EXPECT_EQ("#include \"a\"     // comment\n"
899             "#include \"a/b/c\" // comment",
900             format("#include \\\n"
901                    "  \"a\" // comment\n"
902                    "#include \"a/b/c\" // comment"));
903 
904   verifyFormat("enum E {\n"
905                "  // comment\n"
906                "  VAL_A, // comment\n"
907                "  VAL_B\n"
908                "};");
909 
910   verifyFormat(
911       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
912       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
913   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
914                "    // Comment inside a statement.\n"
915                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
916   verifyFormat("SomeFunction(a,\n"
917                "             // comment\n"
918                "             b + x);");
919   verifyFormat("SomeFunction(a, a,\n"
920                "             // comment\n"
921                "             b + x);");
922   verifyFormat(
923       "bool aaaaaaaaaaaaa = // comment\n"
924       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
925       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
926 
927   verifyFormat("int aaaa; // aaaaa\n"
928                "int aa;   // aaaaaaa",
929                getLLVMStyleWithColumns(20));
930 
931   EXPECT_EQ("void f() { // This does something ..\n"
932             "}\n"
933             "int a; // This is unrelated",
934             format("void f()    {     // This does something ..\n"
935                    "  }\n"
936                    "int   a;     // This is unrelated"));
937   EXPECT_EQ("class C {\n"
938             "  void f() { // This does something ..\n"
939             "  }          // awesome..\n"
940             "\n"
941             "  int a; // This is unrelated\n"
942             "};",
943             format("class C{void f()    { // This does something ..\n"
944                    "      } // awesome..\n"
945                    " \n"
946                    "int a;    // This is unrelated\n"
947                    "};"));
948 
949   EXPECT_EQ("int i; // single line trailing comment",
950             format("int i;\\\n// single line trailing comment"));
951 
952   verifyGoogleFormat("int a;  // Trailing comment.");
953 
954   verifyFormat("someFunction(anotherFunction( // Force break.\n"
955                "    parameter));");
956 
957   verifyGoogleFormat("#endif  // HEADER_GUARD");
958 
959   verifyFormat("const char *test[] = {\n"
960                "    // A\n"
961                "    \"aaaa\",\n"
962                "    // B\n"
963                "    \"aaaaa\"};");
964   verifyGoogleFormat(
965       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
966       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
967   EXPECT_EQ("D(a, {\n"
968             "  // test\n"
969             "  int a;\n"
970             "});",
971             format("D(a, {\n"
972                    "// test\n"
973                    "int a;\n"
974                    "});"));
975 
976   EXPECT_EQ("lineWith(); // comment\n"
977             "// at start\n"
978             "otherLine();",
979             format("lineWith();   // comment\n"
980                    "// at start\n"
981                    "otherLine();"));
982   EXPECT_EQ("lineWith(); // comment\n"
983             "/*\n"
984             " * at start */\n"
985             "otherLine();",
986             format("lineWith();   // comment\n"
987                    "/*\n"
988                    " * at start */\n"
989                    "otherLine();"));
990   EXPECT_EQ("lineWith(); // comment\n"
991             "            // at start\n"
992             "otherLine();",
993             format("lineWith();   // comment\n"
994                    " // at start\n"
995                    "otherLine();"));
996 
997   EXPECT_EQ("lineWith(); // comment\n"
998             "// at start\n"
999             "otherLine(); // comment",
1000             format("lineWith();   // comment\n"
1001                    "// at start\n"
1002                    "otherLine();   // comment"));
1003   EXPECT_EQ("lineWith();\n"
1004             "// at start\n"
1005             "otherLine(); // comment",
1006             format("lineWith();\n"
1007                    " // at start\n"
1008                    "otherLine();   // comment"));
1009   EXPECT_EQ("// first\n"
1010             "// at start\n"
1011             "otherLine(); // comment",
1012             format("// first\n"
1013                    " // at start\n"
1014                    "otherLine();   // comment"));
1015   EXPECT_EQ("f();\n"
1016             "// first\n"
1017             "// at start\n"
1018             "otherLine(); // comment",
1019             format("f();\n"
1020                    "// first\n"
1021                    " // at start\n"
1022                    "otherLine();   // comment"));
1023   verifyFormat("f(); // comment\n"
1024                "// first\n"
1025                "// at start\n"
1026                "otherLine();");
1027   EXPECT_EQ("f(); // comment\n"
1028             "// first\n"
1029             "// at start\n"
1030             "otherLine();",
1031             format("f();   // comment\n"
1032                    "// first\n"
1033                    " // at start\n"
1034                    "otherLine();"));
1035   EXPECT_EQ("f(); // comment\n"
1036             "     // first\n"
1037             "// at start\n"
1038             "otherLine();",
1039             format("f();   // comment\n"
1040                    " // first\n"
1041                    "// at start\n"
1042                    "otherLine();"));
1043   EXPECT_EQ("void f() {\n"
1044             "  lineWith(); // comment\n"
1045             "  // at start\n"
1046             "}",
1047             format("void              f() {\n"
1048                    "  lineWith(); // comment\n"
1049                    "  // at start\n"
1050                    "}"));
1051   EXPECT_EQ("int xy; // a\n"
1052             "int z;  // b",
1053             format("int xy;    // a\n"
1054                    "int z;    //b"));
1055   EXPECT_EQ("int xy; // a\n"
1056             "int z; // bb",
1057             format("int xy;    // a\n"
1058                    "int z;    //bb",
1059                    getLLVMStyleWithColumns(12)));
1060 
1061   verifyFormat("#define A                                                  \\\n"
1062                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1063                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1064                getLLVMStyleWithColumns(60));
1065   verifyFormat(
1066       "#define A                                                   \\\n"
1067       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1068       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1069       getLLVMStyleWithColumns(61));
1070 
1071   verifyFormat("if ( // This is some comment\n"
1072                "    x + 3) {\n"
1073                "}");
1074   EXPECT_EQ("if ( // This is some comment\n"
1075             "     // spanning two lines\n"
1076             "    x + 3) {\n"
1077             "}",
1078             format("if( // This is some comment\n"
1079                    "     // spanning two lines\n"
1080                    " x + 3) {\n"
1081                    "}"));
1082 
1083   verifyNoCrash("/\\\n/");
1084   verifyNoCrash("/\\\n* */");
1085   // The 0-character somehow makes the lexer return a proper comment.
1086   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1087 }
1088 
1089 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1090   EXPECT_EQ("SomeFunction(a,\n"
1091             "             b, // comment\n"
1092             "             c);",
1093             format("SomeFunction(a,\n"
1094                    "          b, // comment\n"
1095                    "      c);"));
1096   EXPECT_EQ("SomeFunction(a, b,\n"
1097             "             // comment\n"
1098             "             c);",
1099             format("SomeFunction(a,\n"
1100                    "          b,\n"
1101                    "  // comment\n"
1102                    "      c);"));
1103   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1104             "             c);",
1105             format("SomeFunction(a, b, // comment (unclear relation)\n"
1106                    "      c);"));
1107   EXPECT_EQ("SomeFunction(a, // comment\n"
1108             "             b,\n"
1109             "             c); // comment",
1110             format("SomeFunction(a,     // comment\n"
1111                    "          b,\n"
1112                    "      c); // comment"));
1113 }
1114 
1115 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1116   EXPECT_EQ("// comment", format("// comment  "));
1117   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1118             format("int aaaaaaa, bbbbbbb; // comment                   ",
1119                    getLLVMStyleWithColumns(33)));
1120   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1121   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1122 }
1123 
1124 TEST_F(FormatTest, UnderstandsBlockComments) {
1125   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1126   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
1127   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1128             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1129             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1130                    "/* Trailing comment for aa... */\n"
1131                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1132   EXPECT_EQ(
1133       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1134       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1135       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1136              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1137   EXPECT_EQ(
1138       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1139       "    aaaaaaaaaaaaaaaaaa,\n"
1140       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1141       "}",
1142       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1143              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1144              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1145              "}"));
1146 
1147   FormatStyle NoBinPacking = getLLVMStyle();
1148   NoBinPacking.BinPackParameters = false;
1149   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1150                "         /* parameter 2 */ aaaaaa,\n"
1151                "         /* parameter 3 */ aaaaaa,\n"
1152                "         /* parameter 4 */ aaaaaa);",
1153                NoBinPacking);
1154 
1155   // Aligning block comments in macros.
1156   verifyGoogleFormat("#define A        \\\n"
1157                      "  int i;   /*a*/ \\\n"
1158                      "  int jjj; /*b*/");
1159 }
1160 
1161 TEST_F(FormatTest, AlignsBlockComments) {
1162   EXPECT_EQ("/*\n"
1163             " * Really multi-line\n"
1164             " * comment.\n"
1165             " */\n"
1166             "void f() {}",
1167             format("  /*\n"
1168                    "   * Really multi-line\n"
1169                    "   * comment.\n"
1170                    "   */\n"
1171                    "  void f() {}"));
1172   EXPECT_EQ("class C {\n"
1173             "  /*\n"
1174             "   * Another multi-line\n"
1175             "   * comment.\n"
1176             "   */\n"
1177             "  void f() {}\n"
1178             "};",
1179             format("class C {\n"
1180                    "/*\n"
1181                    " * Another multi-line\n"
1182                    " * comment.\n"
1183                    " */\n"
1184                    "void f() {}\n"
1185                    "};"));
1186   EXPECT_EQ("/*\n"
1187             "  1. This is a comment with non-trivial formatting.\n"
1188             "     1.1. We have to indent/outdent all lines equally\n"
1189             "         1.1.1. to keep the formatting.\n"
1190             " */",
1191             format("  /*\n"
1192                    "    1. This is a comment with non-trivial formatting.\n"
1193                    "       1.1. We have to indent/outdent all lines equally\n"
1194                    "           1.1.1. to keep the formatting.\n"
1195                    "   */"));
1196   EXPECT_EQ("/*\n"
1197             "Don't try to outdent if there's not enough indentation.\n"
1198             "*/",
1199             format("  /*\n"
1200                    " Don't try to outdent if there's not enough indentation.\n"
1201                    " */"));
1202 
1203   EXPECT_EQ("int i; /* Comment with empty...\n"
1204             "        *\n"
1205             "        * line. */",
1206             format("int i; /* Comment with empty...\n"
1207                    "        *\n"
1208                    "        * line. */"));
1209   EXPECT_EQ("int foobar = 0; /* comment */\n"
1210             "int bar = 0;    /* multiline\n"
1211             "                   comment 1 */\n"
1212             "int baz = 0;    /* multiline\n"
1213             "                   comment 2 */\n"
1214             "int bzz = 0;    /* multiline\n"
1215             "                   comment 3 */",
1216             format("int foobar = 0; /* comment */\n"
1217                    "int bar = 0;    /* multiline\n"
1218                    "                   comment 1 */\n"
1219                    "int baz = 0; /* multiline\n"
1220                    "                comment 2 */\n"
1221                    "int bzz = 0;         /* multiline\n"
1222                    "                        comment 3 */"));
1223   EXPECT_EQ("int foobar = 0; /* comment */\n"
1224             "int bar = 0;    /* multiline\n"
1225             "   comment */\n"
1226             "int baz = 0;    /* multiline\n"
1227             "comment */",
1228             format("int foobar = 0; /* comment */\n"
1229                    "int bar = 0; /* multiline\n"
1230                    "comment */\n"
1231                    "int baz = 0;        /* multiline\n"
1232                    "comment */"));
1233 }
1234 
1235 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1236   FormatStyle Style = getLLVMStyleWithColumns(20);
1237   Style.ReflowComments = false;
1238   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1239   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1240 }
1241 
1242 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1243   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1244             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1245             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1246                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1247   EXPECT_EQ(
1248       "void ffffffffffff(\n"
1249       "    int aaaaaaaa, int bbbbbbbb,\n"
1250       "    int cccccccccccc) { /*\n"
1251       "                           aaaaaaaaaa\n"
1252       "                           aaaaaaaaaaaaa\n"
1253       "                           bbbbbbbbbbbbbb\n"
1254       "                           bbbbbbbbbb\n"
1255       "                         */\n"
1256       "}",
1257       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1258              "{ /*\n"
1259              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1260              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1261              "   */\n"
1262              "}",
1263              getLLVMStyleWithColumns(40)));
1264 }
1265 
1266 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1267   EXPECT_EQ("void ffffffffff(\n"
1268             "    int aaaaa /* test */);",
1269             format("void ffffffffff(int aaaaa /* test */);",
1270                    getLLVMStyleWithColumns(35)));
1271 }
1272 
1273 TEST_F(FormatTest, SplitsLongCxxComments) {
1274   EXPECT_EQ("// A comment that\n"
1275             "// doesn't fit on\n"
1276             "// one line",
1277             format("// A comment that doesn't fit on one line",
1278                    getLLVMStyleWithColumns(20)));
1279   EXPECT_EQ("/// A comment that\n"
1280             "/// doesn't fit on\n"
1281             "/// one line",
1282             format("/// A comment that doesn't fit on one line",
1283                    getLLVMStyleWithColumns(20)));
1284   EXPECT_EQ("//! A comment that\n"
1285             "//! doesn't fit on\n"
1286             "//! one line",
1287             format("//! A comment that doesn't fit on one line",
1288                    getLLVMStyleWithColumns(20)));
1289   EXPECT_EQ("// a b c d\n"
1290             "// e f  g\n"
1291             "// h i j k",
1292             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1293   EXPECT_EQ(
1294       "// a b c d\n"
1295       "// e f  g\n"
1296       "// h i j k",
1297       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1298   EXPECT_EQ("if (true) // A comment that\n"
1299             "          // doesn't fit on\n"
1300             "          // one line",
1301             format("if (true) // A comment that doesn't fit on one line   ",
1302                    getLLVMStyleWithColumns(30)));
1303   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1304             format("//    Don't_touch_leading_whitespace",
1305                    getLLVMStyleWithColumns(20)));
1306   EXPECT_EQ("// Add leading\n"
1307             "// whitespace",
1308             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1309   EXPECT_EQ("/// Add leading\n"
1310             "/// whitespace",
1311             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1312   EXPECT_EQ("//! Add leading\n"
1313             "//! whitespace",
1314             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1315   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1316   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1317             "// limit",
1318             format("//Even if it makes the line exceed the column limit",
1319                    getLLVMStyleWithColumns(51)));
1320   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1321 
1322   EXPECT_EQ("// aa bb cc dd",
1323             format("// aa bb             cc dd                   ",
1324                    getLLVMStyleWithColumns(15)));
1325 
1326   EXPECT_EQ("// A comment before\n"
1327             "// a macro\n"
1328             "// definition\n"
1329             "#define a b",
1330             format("// A comment before a macro definition\n"
1331                    "#define a b",
1332                    getLLVMStyleWithColumns(20)));
1333   EXPECT_EQ("void ffffff(\n"
1334             "    int aaaaaaaaa,  // wwww\n"
1335             "    int bbbbbbbbbb, // xxxxxxx\n"
1336             "                    // yyyyyyyyyy\n"
1337             "    int c, int d, int e) {}",
1338             format("void ffffff(\n"
1339                    "    int aaaaaaaaa, // wwww\n"
1340                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1341                    "    int c, int d, int e) {}",
1342                    getLLVMStyleWithColumns(40)));
1343   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1344             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1345                    getLLVMStyleWithColumns(20)));
1346   EXPECT_EQ(
1347       "#define XXX // a b c d\n"
1348       "            // e f g h",
1349       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1350   EXPECT_EQ(
1351       "#define XXX // q w e r\n"
1352       "            // t y u i",
1353       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1354 }
1355 
1356 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1357   EXPECT_EQ("//     A comment\n"
1358             "//     that doesn't\n"
1359             "//     fit on one\n"
1360             "//     line",
1361             format("//     A comment that doesn't fit on one line",
1362                    getLLVMStyleWithColumns(20)));
1363   EXPECT_EQ("///     A comment\n"
1364             "///     that doesn't\n"
1365             "///     fit on one\n"
1366             "///     line",
1367             format("///     A comment that doesn't fit on one line",
1368                    getLLVMStyleWithColumns(20)));
1369 }
1370 
1371 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1372   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1373             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1374             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1375             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1376                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1377                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1378   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1379             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1380             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1381             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1382                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1383                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1384                    getLLVMStyleWithColumns(50)));
1385   // FIXME: One day we might want to implement adjustment of leading whitespace
1386   // of the consecutive lines in this kind of comment:
1387   EXPECT_EQ("double\n"
1388             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1389             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1390             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1391             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1392                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1393                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1394                    getLLVMStyleWithColumns(49)));
1395 }
1396 
1397 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1398   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1399   Pragmas.CommentPragmas = "^ IWYU pragma:";
1400   EXPECT_EQ(
1401       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1402       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1403   EXPECT_EQ(
1404       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1405       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1406 }
1407 
1408 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1409   EXPECT_EQ("if (xxx ==\n"
1410             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1411             "    zzz)\n"
1412             "  q();",
1413             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1414                    "    zzz) q();",
1415                    getLLVMStyleWithColumns(40)));
1416   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1417             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1418             "    zzz)\n"
1419             "  q();",
1420             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1421                    "    zzz) q();",
1422                    getLLVMStyleWithColumns(40)));
1423   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1424             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1425             "    zzz)\n"
1426             "  q();",
1427             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1428                    "    zzz) q();",
1429                    getLLVMStyleWithColumns(40)));
1430   EXPECT_EQ("fffffffff(\n"
1431             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1432             "    zzz);",
1433             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1434                    " zzz);",
1435                    getLLVMStyleWithColumns(40)));
1436 }
1437 
1438 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1439   EXPECT_EQ("#define A(x) /* \\\n"
1440             "  a comment     \\\n"
1441             "  inside */     \\\n"
1442             "  f();",
1443             format("#define A(x) /* \\\n"
1444                    "  a comment     \\\n"
1445                    "  inside */     \\\n"
1446                    "  f();",
1447                    getLLVMStyleWithColumns(17)));
1448   EXPECT_EQ("#define A(      \\\n"
1449             "    x) /*       \\\n"
1450             "  a comment     \\\n"
1451             "  inside */     \\\n"
1452             "  f();",
1453             format("#define A(      \\\n"
1454                    "    x) /*       \\\n"
1455                    "  a comment     \\\n"
1456                    "  inside */     \\\n"
1457                    "  f();",
1458                    getLLVMStyleWithColumns(17)));
1459 }
1460 
1461 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1462   EXPECT_EQ("namespace {}\n// Test\n#define A",
1463             format("namespace {}\n   // Test\n#define A"));
1464   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1465             format("namespace {}\n   /* Test */\n#define A"));
1466   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1467             format("namespace {}\n   /* Test */    #define A"));
1468 }
1469 
1470 TEST_F(FormatTest, SplitsLongLinesInComments) {
1471   EXPECT_EQ("/* This is a long\n"
1472             " * comment that\n"
1473             " * doesn't\n"
1474             " * fit on one line.\n"
1475             " */",
1476             format("/* "
1477                    "This is a long                                         "
1478                    "comment that "
1479                    "doesn't                                    "
1480                    "fit on one line.  */",
1481                    getLLVMStyleWithColumns(20)));
1482   EXPECT_EQ(
1483       "/* a b c d\n"
1484       " * e f  g\n"
1485       " * h i j k\n"
1486       " */",
1487       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1488   EXPECT_EQ(
1489       "/* a b c d\n"
1490       " * e f  g\n"
1491       " * h i j k\n"
1492       " */",
1493       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1494   EXPECT_EQ("/*\n"
1495             "This is a long\n"
1496             "comment that doesn't\n"
1497             "fit on one line.\n"
1498             "*/",
1499             format("/*\n"
1500                    "This is a long                                         "
1501                    "comment that doesn't                                    "
1502                    "fit on one line.                                      \n"
1503                    "*/",
1504                    getLLVMStyleWithColumns(20)));
1505   EXPECT_EQ("/*\n"
1506             " * This is a long\n"
1507             " * comment that\n"
1508             " * doesn't fit on\n"
1509             " * one line.\n"
1510             " */",
1511             format("/*      \n"
1512                    " * This is a long "
1513                    "   comment that     "
1514                    "   doesn't fit on   "
1515                    "   one line.                                            \n"
1516                    " */",
1517                    getLLVMStyleWithColumns(20)));
1518   EXPECT_EQ("/*\n"
1519             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1520             " * so_it_should_be_broken\n"
1521             " * wherever_a_space_occurs\n"
1522             " */",
1523             format("/*\n"
1524                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1525                    "   so_it_should_be_broken "
1526                    "   wherever_a_space_occurs                             \n"
1527                    " */",
1528                    getLLVMStyleWithColumns(20)));
1529   EXPECT_EQ("/*\n"
1530             " *    This_comment_can_not_be_broken_into_lines\n"
1531             " */",
1532             format("/*\n"
1533                    " *    This_comment_can_not_be_broken_into_lines\n"
1534                    " */",
1535                    getLLVMStyleWithColumns(20)));
1536   EXPECT_EQ("{\n"
1537             "  /*\n"
1538             "  This is another\n"
1539             "  long comment that\n"
1540             "  doesn't fit on one\n"
1541             "  line    1234567890\n"
1542             "  */\n"
1543             "}",
1544             format("{\n"
1545                    "/*\n"
1546                    "This is another     "
1547                    "  long comment that "
1548                    "  doesn't fit on one"
1549                    "  line    1234567890\n"
1550                    "*/\n"
1551                    "}",
1552                    getLLVMStyleWithColumns(20)));
1553   EXPECT_EQ("{\n"
1554             "  /*\n"
1555             "   * This        i s\n"
1556             "   * another comment\n"
1557             "   * t hat  doesn' t\n"
1558             "   * fit on one l i\n"
1559             "   * n e\n"
1560             "   */\n"
1561             "}",
1562             format("{\n"
1563                    "/*\n"
1564                    " * This        i s"
1565                    "   another comment"
1566                    "   t hat  doesn' t"
1567                    "   fit on one l i"
1568                    "   n e\n"
1569                    " */\n"
1570                    "}",
1571                    getLLVMStyleWithColumns(20)));
1572   EXPECT_EQ("/*\n"
1573             " * This is a long\n"
1574             " * comment that\n"
1575             " * doesn't fit on\n"
1576             " * one line\n"
1577             " */",
1578             format("   /*\n"
1579                    "    * This is a long comment that doesn't fit on one line\n"
1580                    "    */",
1581                    getLLVMStyleWithColumns(20)));
1582   EXPECT_EQ("{\n"
1583             "  if (something) /* This is a\n"
1584             "                    long\n"
1585             "                    comment */\n"
1586             "    ;\n"
1587             "}",
1588             format("{\n"
1589                    "  if (something) /* This is a long comment */\n"
1590                    "    ;\n"
1591                    "}",
1592                    getLLVMStyleWithColumns(30)));
1593 
1594   EXPECT_EQ("/* A comment before\n"
1595             " * a macro\n"
1596             " * definition */\n"
1597             "#define a b",
1598             format("/* A comment before a macro definition */\n"
1599                    "#define a b",
1600                    getLLVMStyleWithColumns(20)));
1601 
1602   EXPECT_EQ("/* some comment\n"
1603             "     *   a comment\n"
1604             "* that we break\n"
1605             " * another comment\n"
1606             "* we have to break\n"
1607             "* a left comment\n"
1608             " */",
1609             format("  /* some comment\n"
1610                    "       *   a comment that we break\n"
1611                    "   * another comment we have to break\n"
1612                    "* a left comment\n"
1613                    "   */",
1614                    getLLVMStyleWithColumns(20)));
1615 
1616   EXPECT_EQ("/**\n"
1617             " * multiline block\n"
1618             " * comment\n"
1619             " *\n"
1620             " */",
1621             format("/**\n"
1622                    " * multiline block comment\n"
1623                    " *\n"
1624                    " */",
1625                    getLLVMStyleWithColumns(20)));
1626 
1627   EXPECT_EQ("/*\n"
1628             "\n"
1629             "\n"
1630             "    */\n",
1631             format("  /*       \n"
1632                    "      \n"
1633                    "               \n"
1634                    "      */\n"));
1635 
1636   EXPECT_EQ("/* a a */",
1637             format("/* a a            */", getLLVMStyleWithColumns(15)));
1638   EXPECT_EQ("/* a a bc  */",
1639             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1640   EXPECT_EQ("/* aaa aaa\n"
1641             " * aaaaa */",
1642             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1643   EXPECT_EQ("/* aaa aaa\n"
1644             " * aaaaa     */",
1645             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1646 }
1647 
1648 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1649   EXPECT_EQ("#define X          \\\n"
1650             "  /*               \\\n"
1651             "   Test            \\\n"
1652             "   Macro comment   \\\n"
1653             "   with a long     \\\n"
1654             "   line            \\\n"
1655             "   */              \\\n"
1656             "  A + B",
1657             format("#define X \\\n"
1658                    "  /*\n"
1659                    "   Test\n"
1660                    "   Macro comment with a long  line\n"
1661                    "   */ \\\n"
1662                    "  A + B",
1663                    getLLVMStyleWithColumns(20)));
1664   EXPECT_EQ("#define X          \\\n"
1665             "  /* Macro comment \\\n"
1666             "     with a long   \\\n"
1667             "     line */       \\\n"
1668             "  A + B",
1669             format("#define X \\\n"
1670                    "  /* Macro comment with a long\n"
1671                    "     line */ \\\n"
1672                    "  A + B",
1673                    getLLVMStyleWithColumns(20)));
1674   EXPECT_EQ("#define X          \\\n"
1675             "  /* Macro comment \\\n"
1676             "   * with a long   \\\n"
1677             "   * line */       \\\n"
1678             "  A + B",
1679             format("#define X \\\n"
1680                    "  /* Macro comment with a long  line */ \\\n"
1681                    "  A + B",
1682                    getLLVMStyleWithColumns(20)));
1683 }
1684 
1685 TEST_F(FormatTest, CommentsInStaticInitializers) {
1686   EXPECT_EQ(
1687       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1688       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1689       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1690       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1691       "                        aaaaaaaaaaaaaaaaaaaa};",
1692       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1693              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1694              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1695              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1696              "                  aaaaaaaaaaaaaaaaaaaa };"));
1697   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1698                "                        bbbbbbbbbbb, ccccccccccc};");
1699   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1700                "                        // comment for bb....\n"
1701                "                        bbbbbbbbbbb, ccccccccccc};");
1702   verifyGoogleFormat(
1703       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1704       "                        bbbbbbbbbbb, ccccccccccc};");
1705   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1706                      "                        // comment for bb....\n"
1707                      "                        bbbbbbbbbbb, ccccccccccc};");
1708 
1709   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1710                "       {d, e, f},  // Group #2\n"
1711                "       {g, h, i}}; // Group #3");
1712   verifyFormat("S s = {{// Group #1\n"
1713                "        a, b, c},\n"
1714                "       {// Group #2\n"
1715                "        d, e, f},\n"
1716                "       {// Group #3\n"
1717                "        g, h, i}};");
1718 
1719   EXPECT_EQ("S s = {\n"
1720             "    // Some comment\n"
1721             "    a,\n"
1722             "\n"
1723             "    // Comment after empty line\n"
1724             "    b}",
1725             format("S s =    {\n"
1726                    "      // Some comment\n"
1727                    "  a,\n"
1728                    "  \n"
1729                    "     // Comment after empty line\n"
1730                    "      b\n"
1731                    "}"));
1732   EXPECT_EQ("S s = {\n"
1733             "    /* Some comment */\n"
1734             "    a,\n"
1735             "\n"
1736             "    /* Comment after empty line */\n"
1737             "    b}",
1738             format("S s =    {\n"
1739                    "      /* Some comment */\n"
1740                    "  a,\n"
1741                    "  \n"
1742                    "     /* Comment after empty line */\n"
1743                    "      b\n"
1744                    "}"));
1745   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1746                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1747                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1748                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1749 }
1750 
1751 TEST_F(FormatTest, IgnoresIf0Contents) {
1752   EXPECT_EQ("#if 0\n"
1753             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1754             "#endif\n"
1755             "void f() {}",
1756             format("#if 0\n"
1757                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1758                    "#endif\n"
1759                    "void f(  ) {  }"));
1760   EXPECT_EQ("#if false\n"
1761             "void f(  ) {  }\n"
1762             "#endif\n"
1763             "void g() {}\n",
1764             format("#if false\n"
1765                    "void f(  ) {  }\n"
1766                    "#endif\n"
1767                    "void g(  ) {  }\n"));
1768   EXPECT_EQ("enum E {\n"
1769             "  One,\n"
1770             "  Two,\n"
1771             "#if 0\n"
1772             "Three,\n"
1773             "      Four,\n"
1774             "#endif\n"
1775             "  Five\n"
1776             "};",
1777             format("enum E {\n"
1778                    "  One,Two,\n"
1779                    "#if 0\n"
1780                    "Three,\n"
1781                    "      Four,\n"
1782                    "#endif\n"
1783                    "  Five};"));
1784   EXPECT_EQ("enum F {\n"
1785             "  One,\n"
1786             "#if 1\n"
1787             "  Two,\n"
1788             "#if 0\n"
1789             "Three,\n"
1790             "      Four,\n"
1791             "#endif\n"
1792             "  Five\n"
1793             "#endif\n"
1794             "};",
1795             format("enum F {\n"
1796                    "One,\n"
1797                    "#if 1\n"
1798                    "Two,\n"
1799                    "#if 0\n"
1800                    "Three,\n"
1801                    "      Four,\n"
1802                    "#endif\n"
1803                    "Five\n"
1804                    "#endif\n"
1805                    "};"));
1806   EXPECT_EQ("enum G {\n"
1807             "  One,\n"
1808             "#if 0\n"
1809             "Two,\n"
1810             "#else\n"
1811             "  Three,\n"
1812             "#endif\n"
1813             "  Four\n"
1814             "};",
1815             format("enum G {\n"
1816                    "One,\n"
1817                    "#if 0\n"
1818                    "Two,\n"
1819                    "#else\n"
1820                    "Three,\n"
1821                    "#endif\n"
1822                    "Four\n"
1823                    "};"));
1824   EXPECT_EQ("enum H {\n"
1825             "  One,\n"
1826             "#if 0\n"
1827             "#ifdef Q\n"
1828             "Two,\n"
1829             "#else\n"
1830             "Three,\n"
1831             "#endif\n"
1832             "#endif\n"
1833             "  Four\n"
1834             "};",
1835             format("enum H {\n"
1836                    "One,\n"
1837                    "#if 0\n"
1838                    "#ifdef Q\n"
1839                    "Two,\n"
1840                    "#else\n"
1841                    "Three,\n"
1842                    "#endif\n"
1843                    "#endif\n"
1844                    "Four\n"
1845                    "};"));
1846   EXPECT_EQ("enum I {\n"
1847             "  One,\n"
1848             "#if /* test */ 0 || 1\n"
1849             "Two,\n"
1850             "Three,\n"
1851             "#endif\n"
1852             "  Four\n"
1853             "};",
1854             format("enum I {\n"
1855                    "One,\n"
1856                    "#if /* test */ 0 || 1\n"
1857                    "Two,\n"
1858                    "Three,\n"
1859                    "#endif\n"
1860                    "Four\n"
1861                    "};"));
1862   EXPECT_EQ("enum J {\n"
1863             "  One,\n"
1864             "#if 0\n"
1865             "#if 0\n"
1866             "Two,\n"
1867             "#else\n"
1868             "Three,\n"
1869             "#endif\n"
1870             "Four,\n"
1871             "#endif\n"
1872             "  Five\n"
1873             "};",
1874             format("enum J {\n"
1875                    "One,\n"
1876                    "#if 0\n"
1877                    "#if 0\n"
1878                    "Two,\n"
1879                    "#else\n"
1880                    "Three,\n"
1881                    "#endif\n"
1882                    "Four,\n"
1883                    "#endif\n"
1884                    "Five\n"
1885                    "};"));
1886 }
1887 
1888 //===----------------------------------------------------------------------===//
1889 // Tests for classes, namespaces, etc.
1890 //===----------------------------------------------------------------------===//
1891 
1892 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1893   verifyFormat("class A {};");
1894 }
1895 
1896 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1897   verifyFormat("class A {\n"
1898                "public:\n"
1899                "public: // comment\n"
1900                "protected:\n"
1901                "private:\n"
1902                "  void f() {}\n"
1903                "};");
1904   verifyGoogleFormat("class A {\n"
1905                      " public:\n"
1906                      " protected:\n"
1907                      " private:\n"
1908                      "  void f() {}\n"
1909                      "};");
1910   verifyFormat("class A {\n"
1911                "public slots:\n"
1912                "  void f1() {}\n"
1913                "public Q_SLOTS:\n"
1914                "  void f2() {}\n"
1915                "protected slots:\n"
1916                "  void f3() {}\n"
1917                "protected Q_SLOTS:\n"
1918                "  void f4() {}\n"
1919                "private slots:\n"
1920                "  void f5() {}\n"
1921                "private Q_SLOTS:\n"
1922                "  void f6() {}\n"
1923                "signals:\n"
1924                "  void g1();\n"
1925                "Q_SIGNALS:\n"
1926                "  void g2();\n"
1927                "};");
1928 
1929   // Don't interpret 'signals' the wrong way.
1930   verifyFormat("signals.set();");
1931   verifyFormat("for (Signals signals : f()) {\n}");
1932   verifyFormat("{\n"
1933                "  signals.set(); // This needs indentation.\n"
1934                "}");
1935 }
1936 
1937 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1938   EXPECT_EQ("class A {\n"
1939             "public:\n"
1940             "  void f();\n"
1941             "\n"
1942             "private:\n"
1943             "  void g() {}\n"
1944             "  // test\n"
1945             "protected:\n"
1946             "  int h;\n"
1947             "};",
1948             format("class A {\n"
1949                    "public:\n"
1950                    "void f();\n"
1951                    "private:\n"
1952                    "void g() {}\n"
1953                    "// test\n"
1954                    "protected:\n"
1955                    "int h;\n"
1956                    "};"));
1957   EXPECT_EQ("class A {\n"
1958             "protected:\n"
1959             "public:\n"
1960             "  void f();\n"
1961             "};",
1962             format("class A {\n"
1963                    "protected:\n"
1964                    "\n"
1965                    "public:\n"
1966                    "\n"
1967                    "  void f();\n"
1968                    "};"));
1969 
1970   // Even ensure proper spacing inside macros.
1971   EXPECT_EQ("#define B     \\\n"
1972             "  class A {   \\\n"
1973             "   protected: \\\n"
1974             "   public:    \\\n"
1975             "    void f(); \\\n"
1976             "  };",
1977             format("#define B     \\\n"
1978                    "  class A {   \\\n"
1979                    "   protected: \\\n"
1980                    "              \\\n"
1981                    "   public:    \\\n"
1982                    "              \\\n"
1983                    "    void f(); \\\n"
1984                    "  };",
1985                    getGoogleStyle()));
1986   // But don't remove empty lines after macros ending in access specifiers.
1987   EXPECT_EQ("#define A private:\n"
1988             "\n"
1989             "int i;",
1990             format("#define A         private:\n"
1991                    "\n"
1992                    "int              i;"));
1993 }
1994 
1995 TEST_F(FormatTest, FormatsClasses) {
1996   verifyFormat("class A : public B {};");
1997   verifyFormat("class A : public ::B {};");
1998 
1999   verifyFormat(
2000       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2001       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2002   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2003                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2004                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2005   verifyFormat(
2006       "class A : public B, public C, public D, public E, public F {};");
2007   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2008                "                     public C,\n"
2009                "                     public D,\n"
2010                "                     public E,\n"
2011                "                     public F,\n"
2012                "                     public G {};");
2013 
2014   verifyFormat("class\n"
2015                "    ReallyReallyLongClassName {\n"
2016                "  int i;\n"
2017                "};",
2018                getLLVMStyleWithColumns(32));
2019   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2020                "                           aaaaaaaaaaaaaaaa> {};");
2021   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2022                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2023                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2024   verifyFormat("template <class R, class C>\n"
2025                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2026                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2027   verifyFormat("class ::A::B {};");
2028 }
2029 
2030 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2031   verifyFormat("class A {\n} a, b;");
2032   verifyFormat("struct A {\n} a, b;");
2033   verifyFormat("union A {\n} a;");
2034 }
2035 
2036 TEST_F(FormatTest, FormatsEnum) {
2037   verifyFormat("enum {\n"
2038                "  Zero,\n"
2039                "  One = 1,\n"
2040                "  Two = One + 1,\n"
2041                "  Three = (One + Two),\n"
2042                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2043                "  Five = (One, Two, Three, Four, 5)\n"
2044                "};");
2045   verifyGoogleFormat("enum {\n"
2046                      "  Zero,\n"
2047                      "  One = 1,\n"
2048                      "  Two = One + 1,\n"
2049                      "  Three = (One + Two),\n"
2050                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2051                      "  Five = (One, Two, Three, Four, 5)\n"
2052                      "};");
2053   verifyFormat("enum Enum {};");
2054   verifyFormat("enum {};");
2055   verifyFormat("enum X E {} d;");
2056   verifyFormat("enum __attribute__((...)) E {} d;");
2057   verifyFormat("enum __declspec__((...)) E {} d;");
2058   verifyFormat("enum {\n"
2059                "  Bar = Foo<int, int>::value\n"
2060                "};",
2061                getLLVMStyleWithColumns(30));
2062 
2063   verifyFormat("enum ShortEnum { A, B, C };");
2064   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2065 
2066   EXPECT_EQ("enum KeepEmptyLines {\n"
2067             "  ONE,\n"
2068             "\n"
2069             "  TWO,\n"
2070             "\n"
2071             "  THREE\n"
2072             "}",
2073             format("enum KeepEmptyLines {\n"
2074                    "  ONE,\n"
2075                    "\n"
2076                    "  TWO,\n"
2077                    "\n"
2078                    "\n"
2079                    "  THREE\n"
2080                    "}"));
2081   verifyFormat("enum E { // comment\n"
2082                "  ONE,\n"
2083                "  TWO\n"
2084                "};\n"
2085                "int i;");
2086   // Not enums.
2087   verifyFormat("enum X f() {\n"
2088                "  a();\n"
2089                "  return 42;\n"
2090                "}");
2091   verifyFormat("enum X Type::f() {\n"
2092                "  a();\n"
2093                "  return 42;\n"
2094                "}");
2095   verifyFormat("enum ::X f() {\n"
2096                "  a();\n"
2097                "  return 42;\n"
2098                "}");
2099   verifyFormat("enum ns::X f() {\n"
2100                "  a();\n"
2101                "  return 42;\n"
2102                "}");
2103 }
2104 
2105 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2106   verifyFormat("enum Type {\n"
2107                "  One = 0; // These semicolons should be commas.\n"
2108                "  Two = 1;\n"
2109                "};");
2110   verifyFormat("namespace n {\n"
2111                "enum Type {\n"
2112                "  One,\n"
2113                "  Two, // missing };\n"
2114                "  int i;\n"
2115                "}\n"
2116                "void g() {}");
2117 }
2118 
2119 TEST_F(FormatTest, FormatsEnumStruct) {
2120   verifyFormat("enum struct {\n"
2121                "  Zero,\n"
2122                "  One = 1,\n"
2123                "  Two = One + 1,\n"
2124                "  Three = (One + Two),\n"
2125                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2126                "  Five = (One, Two, Three, Four, 5)\n"
2127                "};");
2128   verifyFormat("enum struct Enum {};");
2129   verifyFormat("enum struct {};");
2130   verifyFormat("enum struct X E {} d;");
2131   verifyFormat("enum struct __attribute__((...)) E {} d;");
2132   verifyFormat("enum struct __declspec__((...)) E {} d;");
2133   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2134 }
2135 
2136 TEST_F(FormatTest, FormatsEnumClass) {
2137   verifyFormat("enum class {\n"
2138                "  Zero,\n"
2139                "  One = 1,\n"
2140                "  Two = One + 1,\n"
2141                "  Three = (One + Two),\n"
2142                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2143                "  Five = (One, Two, Three, Four, 5)\n"
2144                "};");
2145   verifyFormat("enum class Enum {};");
2146   verifyFormat("enum class {};");
2147   verifyFormat("enum class X E {} d;");
2148   verifyFormat("enum class __attribute__((...)) E {} d;");
2149   verifyFormat("enum class __declspec__((...)) E {} d;");
2150   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2151 }
2152 
2153 TEST_F(FormatTest, FormatsEnumTypes) {
2154   verifyFormat("enum X : int {\n"
2155                "  A, // Force multiple lines.\n"
2156                "  B\n"
2157                "};");
2158   verifyFormat("enum X : int { A, B };");
2159   verifyFormat("enum X : std::uint32_t { A, B };");
2160 }
2161 
2162 TEST_F(FormatTest, FormatsNSEnums) {
2163   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2164   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2165                      "  // Information about someDecentlyLongValue.\n"
2166                      "  someDecentlyLongValue,\n"
2167                      "  // Information about anotherDecentlyLongValue.\n"
2168                      "  anotherDecentlyLongValue,\n"
2169                      "  // Information about aThirdDecentlyLongValue.\n"
2170                      "  aThirdDecentlyLongValue\n"
2171                      "};");
2172   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2173                      "  a = 1,\n"
2174                      "  b = 2,\n"
2175                      "  c = 3,\n"
2176                      "};");
2177   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2178                      "  a = 1,\n"
2179                      "  b = 2,\n"
2180                      "  c = 3,\n"
2181                      "};");
2182   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2183                      "  a = 1,\n"
2184                      "  b = 2,\n"
2185                      "  c = 3,\n"
2186                      "};");
2187 }
2188 
2189 TEST_F(FormatTest, FormatsBitfields) {
2190   verifyFormat("struct Bitfields {\n"
2191                "  unsigned sClass : 8;\n"
2192                "  unsigned ValueKind : 2;\n"
2193                "};");
2194   verifyFormat("struct A {\n"
2195                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2196                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2197                "};");
2198   verifyFormat("struct MyStruct {\n"
2199                "  uchar data;\n"
2200                "  uchar : 8;\n"
2201                "  uchar : 8;\n"
2202                "  uchar other;\n"
2203                "};");
2204 }
2205 
2206 TEST_F(FormatTest, FormatsNamespaces) {
2207   verifyFormat("namespace some_namespace {\n"
2208                "class A {};\n"
2209                "void f() { f(); }\n"
2210                "}");
2211   verifyFormat("namespace {\n"
2212                "class A {};\n"
2213                "void f() { f(); }\n"
2214                "}");
2215   verifyFormat("inline namespace X {\n"
2216                "class A {};\n"
2217                "void f() { f(); }\n"
2218                "}");
2219   verifyFormat("using namespace some_namespace;\n"
2220                "class A {};\n"
2221                "void f() { f(); }");
2222 
2223   // This code is more common than we thought; if we
2224   // layout this correctly the semicolon will go into
2225   // its own line, which is undesirable.
2226   verifyFormat("namespace {};");
2227   verifyFormat("namespace {\n"
2228                "class A {};\n"
2229                "};");
2230 
2231   verifyFormat("namespace {\n"
2232                "int SomeVariable = 0; // comment\n"
2233                "} // namespace");
2234   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2235             "#define HEADER_GUARD\n"
2236             "namespace my_namespace {\n"
2237             "int i;\n"
2238             "} // my_namespace\n"
2239             "#endif // HEADER_GUARD",
2240             format("#ifndef HEADER_GUARD\n"
2241                    " #define HEADER_GUARD\n"
2242                    "   namespace my_namespace {\n"
2243                    "int i;\n"
2244                    "}    // my_namespace\n"
2245                    "#endif    // HEADER_GUARD"));
2246 
2247   EXPECT_EQ("namespace A::B {\n"
2248             "class C {};\n"
2249             "}",
2250             format("namespace A::B {\n"
2251                    "class C {};\n"
2252                    "}"));
2253 
2254   FormatStyle Style = getLLVMStyle();
2255   Style.NamespaceIndentation = FormatStyle::NI_All;
2256   EXPECT_EQ("namespace out {\n"
2257             "  int i;\n"
2258             "  namespace in {\n"
2259             "    int i;\n"
2260             "  } // namespace\n"
2261             "} // namespace",
2262             format("namespace out {\n"
2263                    "int i;\n"
2264                    "namespace in {\n"
2265                    "int i;\n"
2266                    "} // namespace\n"
2267                    "} // namespace",
2268                    Style));
2269 
2270   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2271   EXPECT_EQ("namespace out {\n"
2272             "int i;\n"
2273             "namespace in {\n"
2274             "  int i;\n"
2275             "} // namespace\n"
2276             "} // namespace",
2277             format("namespace out {\n"
2278                    "int i;\n"
2279                    "namespace in {\n"
2280                    "int i;\n"
2281                    "} // namespace\n"
2282                    "} // namespace",
2283                    Style));
2284 }
2285 
2286 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2287 
2288 TEST_F(FormatTest, FormatsInlineASM) {
2289   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2290   verifyFormat("asm(\"nop\" ::: \"memory\");");
2291   verifyFormat(
2292       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2293       "    \"cpuid\\n\\t\"\n"
2294       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2295       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2296       "    : \"a\"(value));");
2297   EXPECT_EQ(
2298       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2299       "  __asm {\n"
2300       "        mov     edx,[that] // vtable in edx\n"
2301       "        mov     eax,methodIndex\n"
2302       "        call    [edx][eax*4] // stdcall\n"
2303       "  }\n"
2304       "}",
2305       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2306              "    __asm {\n"
2307              "        mov     edx,[that] // vtable in edx\n"
2308              "        mov     eax,methodIndex\n"
2309              "        call    [edx][eax*4] // stdcall\n"
2310              "    }\n"
2311              "}"));
2312   EXPECT_EQ("_asm {\n"
2313             "  xor eax, eax;\n"
2314             "  cpuid;\n"
2315             "}",
2316             format("_asm {\n"
2317                    "  xor eax, eax;\n"
2318                    "  cpuid;\n"
2319                    "}"));
2320   verifyFormat("void function() {\n"
2321                "  // comment\n"
2322                "  asm(\"\");\n"
2323                "}");
2324   EXPECT_EQ("__asm {\n"
2325             "}\n"
2326             "int i;",
2327             format("__asm   {\n"
2328                    "}\n"
2329                    "int   i;"));
2330 }
2331 
2332 TEST_F(FormatTest, FormatTryCatch) {
2333   verifyFormat("try {\n"
2334                "  throw a * b;\n"
2335                "} catch (int a) {\n"
2336                "  // Do nothing.\n"
2337                "} catch (...) {\n"
2338                "  exit(42);\n"
2339                "}");
2340 
2341   // Function-level try statements.
2342   verifyFormat("int f() try { return 4; } catch (...) {\n"
2343                "  return 5;\n"
2344                "}");
2345   verifyFormat("class A {\n"
2346                "  int a;\n"
2347                "  A() try : a(0) {\n"
2348                "  } catch (...) {\n"
2349                "    throw;\n"
2350                "  }\n"
2351                "};\n");
2352 
2353   // Incomplete try-catch blocks.
2354   verifyIncompleteFormat("try {} catch (");
2355 }
2356 
2357 TEST_F(FormatTest, FormatSEHTryCatch) {
2358   verifyFormat("__try {\n"
2359                "  int a = b * c;\n"
2360                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2361                "  // Do nothing.\n"
2362                "}");
2363 
2364   verifyFormat("__try {\n"
2365                "  int a = b * c;\n"
2366                "} __finally {\n"
2367                "  // Do nothing.\n"
2368                "}");
2369 
2370   verifyFormat("DEBUG({\n"
2371                "  __try {\n"
2372                "  } __finally {\n"
2373                "  }\n"
2374                "});\n");
2375 }
2376 
2377 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2378   verifyFormat("try {\n"
2379                "  f();\n"
2380                "} catch {\n"
2381                "  g();\n"
2382                "}");
2383   verifyFormat("try {\n"
2384                "  f();\n"
2385                "} catch (A a) MACRO(x) {\n"
2386                "  g();\n"
2387                "} catch (B b) MACRO(x) {\n"
2388                "  g();\n"
2389                "}");
2390 }
2391 
2392 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2393   FormatStyle Style = getLLVMStyle();
2394   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2395                           FormatStyle::BS_WebKit}) {
2396     Style.BreakBeforeBraces = BraceStyle;
2397     verifyFormat("try {\n"
2398                  "  // something\n"
2399                  "} catch (...) {\n"
2400                  "  // something\n"
2401                  "}",
2402                  Style);
2403   }
2404   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2405   verifyFormat("try {\n"
2406                "  // something\n"
2407                "}\n"
2408                "catch (...) {\n"
2409                "  // something\n"
2410                "}",
2411                Style);
2412   verifyFormat("__try {\n"
2413                "  // something\n"
2414                "}\n"
2415                "__finally {\n"
2416                "  // something\n"
2417                "}",
2418                Style);
2419   verifyFormat("@try {\n"
2420                "  // something\n"
2421                "}\n"
2422                "@finally {\n"
2423                "  // something\n"
2424                "}",
2425                Style);
2426   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2427   verifyFormat("try\n"
2428                "{\n"
2429                "  // something\n"
2430                "}\n"
2431                "catch (...)\n"
2432                "{\n"
2433                "  // something\n"
2434                "}",
2435                Style);
2436   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2437   verifyFormat("try\n"
2438                "  {\n"
2439                "    // something\n"
2440                "  }\n"
2441                "catch (...)\n"
2442                "  {\n"
2443                "    // something\n"
2444                "  }",
2445                Style);
2446   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2447   Style.BraceWrapping.BeforeCatch = true;
2448   verifyFormat("try {\n"
2449                "  // something\n"
2450                "}\n"
2451                "catch (...) {\n"
2452                "  // something\n"
2453                "}",
2454                Style);
2455 }
2456 
2457 TEST_F(FormatTest, FormatObjCTryCatch) {
2458   verifyFormat("@try {\n"
2459                "  f();\n"
2460                "} @catch (NSException e) {\n"
2461                "  @throw;\n"
2462                "} @finally {\n"
2463                "  exit(42);\n"
2464                "}");
2465   verifyFormat("DEBUG({\n"
2466                "  @try {\n"
2467                "  } @finally {\n"
2468                "  }\n"
2469                "});\n");
2470 }
2471 
2472 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2473   FormatStyle Style = getLLVMStyle();
2474   verifyFormat("@autoreleasepool {\n"
2475                "  f();\n"
2476                "}\n"
2477                "@autoreleasepool {\n"
2478                "  f();\n"
2479                "}\n",
2480                Style);
2481   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2482   verifyFormat("@autoreleasepool\n"
2483                "{\n"
2484                "  f();\n"
2485                "}\n"
2486                "@autoreleasepool\n"
2487                "{\n"
2488                "  f();\n"
2489                "}\n",
2490                Style);
2491 }
2492 
2493 TEST_F(FormatTest, StaticInitializers) {
2494   verifyFormat("static SomeClass SC = {1, 'a'};");
2495 
2496   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2497                "    100000000, "
2498                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2499 
2500   // Here, everything other than the "}" would fit on a line.
2501   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2502                "    10000000000000000000000000};");
2503   EXPECT_EQ("S s = {a,\n"
2504             "\n"
2505             "       b};",
2506             format("S s = {\n"
2507                    "  a,\n"
2508                    "\n"
2509                    "  b\n"
2510                    "};"));
2511 
2512   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2513   // line. However, the formatting looks a bit off and this probably doesn't
2514   // happen often in practice.
2515   verifyFormat("static int Variable[1] = {\n"
2516                "    {1000000000000000000000000000000000000}};",
2517                getLLVMStyleWithColumns(40));
2518 }
2519 
2520 TEST_F(FormatTest, DesignatedInitializers) {
2521   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2522   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2523                "                    .bbbbbbbbbb = 2,\n"
2524                "                    .cccccccccc = 3,\n"
2525                "                    .dddddddddd = 4,\n"
2526                "                    .eeeeeeeeee = 5};");
2527   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2528                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2529                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2530                "    .ccccccccccccccccccccccccccc = 3,\n"
2531                "    .ddddddddddddddddddddddddddd = 4,\n"
2532                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2533 
2534   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2535 }
2536 
2537 TEST_F(FormatTest, NestedStaticInitializers) {
2538   verifyFormat("static A x = {{{}}};\n");
2539   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2540                "               {init1, init2, init3, init4}}};",
2541                getLLVMStyleWithColumns(50));
2542 
2543   verifyFormat("somes Status::global_reps[3] = {\n"
2544                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2545                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2546                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2547                getLLVMStyleWithColumns(60));
2548   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2549                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2550                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2551                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2552   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2553                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2554                "rect.fTop}};");
2555 
2556   verifyFormat(
2557       "SomeArrayOfSomeType a = {\n"
2558       "    {{1, 2, 3},\n"
2559       "     {1, 2, 3},\n"
2560       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2561       "      333333333333333333333333333333},\n"
2562       "     {1, 2, 3},\n"
2563       "     {1, 2, 3}}};");
2564   verifyFormat(
2565       "SomeArrayOfSomeType a = {\n"
2566       "    {{1, 2, 3}},\n"
2567       "    {{1, 2, 3}},\n"
2568       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2569       "      333333333333333333333333333333}},\n"
2570       "    {{1, 2, 3}},\n"
2571       "    {{1, 2, 3}}};");
2572 
2573   verifyFormat("struct {\n"
2574                "  unsigned bit;\n"
2575                "  const char *const name;\n"
2576                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2577                "                 {kOsWin, \"Windows\"},\n"
2578                "                 {kOsLinux, \"Linux\"},\n"
2579                "                 {kOsCrOS, \"Chrome OS\"}};");
2580   verifyFormat("struct {\n"
2581                "  unsigned bit;\n"
2582                "  const char *const name;\n"
2583                "} kBitsToOs[] = {\n"
2584                "    {kOsMac, \"Mac\"},\n"
2585                "    {kOsWin, \"Windows\"},\n"
2586                "    {kOsLinux, \"Linux\"},\n"
2587                "    {kOsCrOS, \"Chrome OS\"},\n"
2588                "};");
2589 }
2590 
2591 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2592   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2593                "                      \\\n"
2594                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2595 }
2596 
2597 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2598   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2599                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2600 
2601   // Do break defaulted and deleted functions.
2602   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2603                "    default;",
2604                getLLVMStyleWithColumns(40));
2605   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2606                "    delete;",
2607                getLLVMStyleWithColumns(40));
2608 }
2609 
2610 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2611   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2612                getLLVMStyleWithColumns(40));
2613   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2614                getLLVMStyleWithColumns(40));
2615   EXPECT_EQ("#define Q                              \\\n"
2616             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2617             "  \"aaaaaaaa.cpp\"",
2618             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2619                    getLLVMStyleWithColumns(40)));
2620 }
2621 
2622 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2623   EXPECT_EQ("# 123 \"A string literal\"",
2624             format("   #     123    \"A string literal\""));
2625 }
2626 
2627 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2628   EXPECT_EQ("#;", format("#;"));
2629   verifyFormat("#\n;\n;\n;");
2630 }
2631 
2632 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2633   EXPECT_EQ("#line 42 \"test\"\n",
2634             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2635   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2636                                     getLLVMStyleWithColumns(12)));
2637 }
2638 
2639 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2640   EXPECT_EQ("#line 42 \"test\"",
2641             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2642   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2643 }
2644 
2645 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2646   verifyFormat("#define A \\x20");
2647   verifyFormat("#define A \\ x20");
2648   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2649   verifyFormat("#define A ''");
2650   verifyFormat("#define A ''qqq");
2651   verifyFormat("#define A `qqq");
2652   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2653   EXPECT_EQ("const char *c = STRINGIFY(\n"
2654             "\\na : b);",
2655             format("const char * c = STRINGIFY(\n"
2656                    "\\na : b);"));
2657 
2658   verifyFormat("a\r\\");
2659   verifyFormat("a\v\\");
2660   verifyFormat("a\f\\");
2661 }
2662 
2663 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2664   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2665   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2666   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2667   // FIXME: We never break before the macro name.
2668   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2669 
2670   verifyFormat("#define A A\n#define A A");
2671   verifyFormat("#define A(X) A\n#define A A");
2672 
2673   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2674   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2675 }
2676 
2677 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2678   EXPECT_EQ("// somecomment\n"
2679             "#include \"a.h\"\n"
2680             "#define A(  \\\n"
2681             "    A, B)\n"
2682             "#include \"b.h\"\n"
2683             "// somecomment\n",
2684             format("  // somecomment\n"
2685                    "  #include \"a.h\"\n"
2686                    "#define A(A,\\\n"
2687                    "    B)\n"
2688                    "    #include \"b.h\"\n"
2689                    " // somecomment\n",
2690                    getLLVMStyleWithColumns(13)));
2691 }
2692 
2693 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2694 
2695 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2696   EXPECT_EQ("#define A    \\\n"
2697             "  c;         \\\n"
2698             "  e;\n"
2699             "f;",
2700             format("#define A c; e;\n"
2701                    "f;",
2702                    getLLVMStyleWithColumns(14)));
2703 }
2704 
2705 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2706 
2707 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2708   EXPECT_EQ("int x,\n"
2709             "#define A\n"
2710             "    y;",
2711             format("int x,\n#define A\ny;"));
2712 }
2713 
2714 TEST_F(FormatTest, HashInMacroDefinition) {
2715   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2716   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2717   verifyFormat("#define A  \\\n"
2718                "  {        \\\n"
2719                "    f(#c); \\\n"
2720                "  }",
2721                getLLVMStyleWithColumns(11));
2722 
2723   verifyFormat("#define A(X)         \\\n"
2724                "  void function##X()",
2725                getLLVMStyleWithColumns(22));
2726 
2727   verifyFormat("#define A(a, b, c)   \\\n"
2728                "  void a##b##c()",
2729                getLLVMStyleWithColumns(22));
2730 
2731   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2732 }
2733 
2734 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2735   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2736   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2737 }
2738 
2739 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2740   EXPECT_EQ("#define A b;", format("#define A \\\n"
2741                                    "          \\\n"
2742                                    "  b;",
2743                                    getLLVMStyleWithColumns(25)));
2744   EXPECT_EQ("#define A \\\n"
2745             "          \\\n"
2746             "  a;      \\\n"
2747             "  b;",
2748             format("#define A \\\n"
2749                    "          \\\n"
2750                    "  a;      \\\n"
2751                    "  b;",
2752                    getLLVMStyleWithColumns(11)));
2753   EXPECT_EQ("#define A \\\n"
2754             "  a;      \\\n"
2755             "          \\\n"
2756             "  b;",
2757             format("#define A \\\n"
2758                    "  a;      \\\n"
2759                    "          \\\n"
2760                    "  b;",
2761                    getLLVMStyleWithColumns(11)));
2762 }
2763 
2764 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2765   verifyIncompleteFormat("#define A :");
2766   verifyFormat("#define SOMECASES  \\\n"
2767                "  case 1:          \\\n"
2768                "  case 2\n",
2769                getLLVMStyleWithColumns(20));
2770   verifyFormat("#define MACRO(a) \\\n"
2771                "  if (a)         \\\n"
2772                "    f();         \\\n"
2773                "  else           \\\n"
2774                "    g()",
2775                getLLVMStyleWithColumns(18));
2776   verifyFormat("#define A template <typename T>");
2777   verifyIncompleteFormat("#define STR(x) #x\n"
2778                          "f(STR(this_is_a_string_literal{));");
2779   verifyFormat("#pragma omp threadprivate( \\\n"
2780                "    y)), // expected-warning",
2781                getLLVMStyleWithColumns(28));
2782   verifyFormat("#d, = };");
2783   verifyFormat("#if \"a");
2784   verifyIncompleteFormat("({\n"
2785                          "#define b     \\\n"
2786                          "  }           \\\n"
2787                          "  a\n"
2788                          "a",
2789                          getLLVMStyleWithColumns(15));
2790   verifyFormat("#define A     \\\n"
2791                "  {           \\\n"
2792                "    {\n"
2793                "#define B     \\\n"
2794                "  }           \\\n"
2795                "  }",
2796                getLLVMStyleWithColumns(15));
2797   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2798   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2799   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2800   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2801 }
2802 
2803 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2804   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2805   EXPECT_EQ("class A : public QObject {\n"
2806             "  Q_OBJECT\n"
2807             "\n"
2808             "  A() {}\n"
2809             "};",
2810             format("class A  :  public QObject {\n"
2811                    "     Q_OBJECT\n"
2812                    "\n"
2813                    "  A() {\n}\n"
2814                    "}  ;"));
2815   EXPECT_EQ("MACRO\n"
2816             "/*static*/ int i;",
2817             format("MACRO\n"
2818                    " /*static*/ int   i;"));
2819   EXPECT_EQ("SOME_MACRO\n"
2820             "namespace {\n"
2821             "void f();\n"
2822             "}",
2823             format("SOME_MACRO\n"
2824                    "  namespace    {\n"
2825                    "void   f(  );\n"
2826                    "}"));
2827   // Only if the identifier contains at least 5 characters.
2828   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2829   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2830   // Only if everything is upper case.
2831   EXPECT_EQ("class A : public QObject {\n"
2832             "  Q_Object A() {}\n"
2833             "};",
2834             format("class A  :  public QObject {\n"
2835                    "     Q_Object\n"
2836                    "  A() {\n}\n"
2837                    "}  ;"));
2838 
2839   // Only if the next line can actually start an unwrapped line.
2840   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2841             format("SOME_WEIRD_LOG_MACRO\n"
2842                    "<< SomeThing;"));
2843 
2844   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2845                "(n, buffers))\n",
2846                getChromiumStyle(FormatStyle::LK_Cpp));
2847 }
2848 
2849 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2850   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2851             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2852             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2853             "class X {};\n"
2854             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2855             "int *createScopDetectionPass() { return 0; }",
2856             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2857                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2858                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2859                    "  class X {};\n"
2860                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2861                    "  int *createScopDetectionPass() { return 0; }"));
2862   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2863   // braces, so that inner block is indented one level more.
2864   EXPECT_EQ("int q() {\n"
2865             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2866             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2867             "  IPC_END_MESSAGE_MAP()\n"
2868             "}",
2869             format("int q() {\n"
2870                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2871                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2872                    "  IPC_END_MESSAGE_MAP()\n"
2873                    "}"));
2874 
2875   // Same inside macros.
2876   EXPECT_EQ("#define LIST(L) \\\n"
2877             "  L(A)          \\\n"
2878             "  L(B)          \\\n"
2879             "  L(C)",
2880             format("#define LIST(L) \\\n"
2881                    "  L(A) \\\n"
2882                    "  L(B) \\\n"
2883                    "  L(C)",
2884                    getGoogleStyle()));
2885 
2886   // These must not be recognized as macros.
2887   EXPECT_EQ("int q() {\n"
2888             "  f(x);\n"
2889             "  f(x) {}\n"
2890             "  f(x)->g();\n"
2891             "  f(x)->*g();\n"
2892             "  f(x).g();\n"
2893             "  f(x) = x;\n"
2894             "  f(x) += x;\n"
2895             "  f(x) -= x;\n"
2896             "  f(x) *= x;\n"
2897             "  f(x) /= x;\n"
2898             "  f(x) %= x;\n"
2899             "  f(x) &= x;\n"
2900             "  f(x) |= x;\n"
2901             "  f(x) ^= x;\n"
2902             "  f(x) >>= x;\n"
2903             "  f(x) <<= x;\n"
2904             "  f(x)[y].z();\n"
2905             "  LOG(INFO) << x;\n"
2906             "  ifstream(x) >> x;\n"
2907             "}\n",
2908             format("int q() {\n"
2909                    "  f(x)\n;\n"
2910                    "  f(x)\n {}\n"
2911                    "  f(x)\n->g();\n"
2912                    "  f(x)\n->*g();\n"
2913                    "  f(x)\n.g();\n"
2914                    "  f(x)\n = x;\n"
2915                    "  f(x)\n += x;\n"
2916                    "  f(x)\n -= x;\n"
2917                    "  f(x)\n *= x;\n"
2918                    "  f(x)\n /= x;\n"
2919                    "  f(x)\n %= x;\n"
2920                    "  f(x)\n &= x;\n"
2921                    "  f(x)\n |= x;\n"
2922                    "  f(x)\n ^= x;\n"
2923                    "  f(x)\n >>= x;\n"
2924                    "  f(x)\n <<= x;\n"
2925                    "  f(x)\n[y].z();\n"
2926                    "  LOG(INFO)\n << x;\n"
2927                    "  ifstream(x)\n >> x;\n"
2928                    "}\n"));
2929   EXPECT_EQ("int q() {\n"
2930             "  F(x)\n"
2931             "  if (1) {\n"
2932             "  }\n"
2933             "  F(x)\n"
2934             "  while (1) {\n"
2935             "  }\n"
2936             "  F(x)\n"
2937             "  G(x);\n"
2938             "  F(x)\n"
2939             "  try {\n"
2940             "    Q();\n"
2941             "  } catch (...) {\n"
2942             "  }\n"
2943             "}\n",
2944             format("int q() {\n"
2945                    "F(x)\n"
2946                    "if (1) {}\n"
2947                    "F(x)\n"
2948                    "while (1) {}\n"
2949                    "F(x)\n"
2950                    "G(x);\n"
2951                    "F(x)\n"
2952                    "try { Q(); } catch (...) {}\n"
2953                    "}\n"));
2954   EXPECT_EQ("class A {\n"
2955             "  A() : t(0) {}\n"
2956             "  A(int i) noexcept() : {}\n"
2957             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2958             "  try : t(0) {\n"
2959             "  } catch (...) {\n"
2960             "  }\n"
2961             "};",
2962             format("class A {\n"
2963                    "  A()\n : t(0) {}\n"
2964                    "  A(int i)\n noexcept() : {}\n"
2965                    "  A(X x)\n"
2966                    "  try : t(0) {} catch (...) {}\n"
2967                    "};"));
2968   EXPECT_EQ("class SomeClass {\n"
2969             "public:\n"
2970             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2971             "};",
2972             format("class SomeClass {\n"
2973                    "public:\n"
2974                    "  SomeClass()\n"
2975                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2976                    "};"));
2977   EXPECT_EQ("class SomeClass {\n"
2978             "public:\n"
2979             "  SomeClass()\n"
2980             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2981             "};",
2982             format("class SomeClass {\n"
2983                    "public:\n"
2984                    "  SomeClass()\n"
2985                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2986                    "};",
2987                    getLLVMStyleWithColumns(40)));
2988 
2989   verifyFormat("MACRO(>)");
2990 }
2991 
2992 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2993   verifyFormat("#define A \\\n"
2994                "  f({     \\\n"
2995                "    g();  \\\n"
2996                "  });",
2997                getLLVMStyleWithColumns(11));
2998 }
2999 
3000 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
3001   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
3002 }
3003 
3004 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3005   verifyFormat("{\n  { a #c; }\n}");
3006 }
3007 
3008 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3009   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3010             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3011   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3012             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3013 }
3014 
3015 TEST_F(FormatTest, EscapedNewlines) {
3016   EXPECT_EQ(
3017       "#define A \\\n  int i;  \\\n  int j;",
3018       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
3019   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3020   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3021   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
3022   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3023 }
3024 
3025 TEST_F(FormatTest, DontCrashOnBlockComments) {
3026   EXPECT_EQ(
3027       "int xxxxxxxxx; /* "
3028       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
3029       "zzzzzz\n"
3030       "0*/",
3031       format("int xxxxxxxxx;                          /* "
3032              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
3033              "0*/"));
3034 }
3035 
3036 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3037   verifyFormat("#define A \\\n"
3038                "  int v(  \\\n"
3039                "      a); \\\n"
3040                "  int i;",
3041                getLLVMStyleWithColumns(11));
3042 }
3043 
3044 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3045   EXPECT_EQ(
3046       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3047       "                      \\\n"
3048       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3049       "\n"
3050       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3051       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3052       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3053              "\\\n"
3054              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3055              "  \n"
3056              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3057              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3058 }
3059 
3060 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3061   EXPECT_EQ("int\n"
3062             "#define A\n"
3063             "    a;",
3064             format("int\n#define A\na;"));
3065   verifyFormat("functionCallTo(\n"
3066                "    someOtherFunction(\n"
3067                "        withSomeParameters, whichInSequence,\n"
3068                "        areLongerThanALine(andAnotherCall,\n"
3069                "#define A B\n"
3070                "                           withMoreParamters,\n"
3071                "                           whichStronglyInfluenceTheLayout),\n"
3072                "        andMoreParameters),\n"
3073                "    trailing);",
3074                getLLVMStyleWithColumns(69));
3075   verifyFormat("Foo::Foo()\n"
3076                "#ifdef BAR\n"
3077                "    : baz(0)\n"
3078                "#endif\n"
3079                "{\n"
3080                "}");
3081   verifyFormat("void f() {\n"
3082                "  if (true)\n"
3083                "#ifdef A\n"
3084                "    f(42);\n"
3085                "  x();\n"
3086                "#else\n"
3087                "    g();\n"
3088                "  x();\n"
3089                "#endif\n"
3090                "}");
3091   verifyFormat("void f(param1, param2,\n"
3092                "       param3,\n"
3093                "#ifdef A\n"
3094                "       param4(param5,\n"
3095                "#ifdef A1\n"
3096                "              param6,\n"
3097                "#ifdef A2\n"
3098                "              param7),\n"
3099                "#else\n"
3100                "              param8),\n"
3101                "       param9,\n"
3102                "#endif\n"
3103                "       param10,\n"
3104                "#endif\n"
3105                "       param11)\n"
3106                "#else\n"
3107                "       param12)\n"
3108                "#endif\n"
3109                "{\n"
3110                "  x();\n"
3111                "}",
3112                getLLVMStyleWithColumns(28));
3113   verifyFormat("#if 1\n"
3114                "int i;");
3115   verifyFormat("#if 1\n"
3116                "#endif\n"
3117                "#if 1\n"
3118                "#else\n"
3119                "#endif\n");
3120   verifyFormat("DEBUG({\n"
3121                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3122                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3123                "});\n"
3124                "#if a\n"
3125                "#else\n"
3126                "#endif");
3127 
3128   verifyIncompleteFormat("void f(\n"
3129                          "#if A\n"
3130                          "    );\n"
3131                          "#else\n"
3132                          "#endif");
3133 }
3134 
3135 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3136   verifyFormat("#endif\n"
3137                "#if B");
3138 }
3139 
3140 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3141   FormatStyle SingleLine = getLLVMStyle();
3142   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3143   verifyFormat("#if 0\n"
3144                "#elif 1\n"
3145                "#endif\n"
3146                "void foo() {\n"
3147                "  if (test) foo2();\n"
3148                "}",
3149                SingleLine);
3150 }
3151 
3152 TEST_F(FormatTest, LayoutBlockInsideParens) {
3153   verifyFormat("functionCall({ int i; });");
3154   verifyFormat("functionCall({\n"
3155                "  int i;\n"
3156                "  int j;\n"
3157                "});");
3158   verifyFormat("functionCall(\n"
3159                "    {\n"
3160                "      int i;\n"
3161                "      int j;\n"
3162                "    },\n"
3163                "    aaaa, bbbb, cccc);");
3164   verifyFormat("functionA(functionB({\n"
3165                "            int i;\n"
3166                "            int j;\n"
3167                "          }),\n"
3168                "          aaaa, bbbb, cccc);");
3169   verifyFormat("functionCall(\n"
3170                "    {\n"
3171                "      int i;\n"
3172                "      int j;\n"
3173                "    },\n"
3174                "    aaaa, bbbb, // comment\n"
3175                "    cccc);");
3176   verifyFormat("functionA(functionB({\n"
3177                "            int i;\n"
3178                "            int j;\n"
3179                "          }),\n"
3180                "          aaaa, bbbb, // comment\n"
3181                "          cccc);");
3182   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3183   verifyFormat("functionCall(aaaa, bbbb, {\n"
3184                "  int i;\n"
3185                "  int j;\n"
3186                "});");
3187   verifyFormat(
3188       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3189       "    {\n"
3190       "      int i; // break\n"
3191       "    },\n"
3192       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3193       "                                     ccccccccccccccccc));");
3194   verifyFormat("DEBUG({\n"
3195                "  if (a)\n"
3196                "    f();\n"
3197                "});");
3198 }
3199 
3200 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3201   EXPECT_EQ("SOME_MACRO { int i; }\n"
3202             "int i;",
3203             format("  SOME_MACRO  {int i;}  int i;"));
3204 }
3205 
3206 TEST_F(FormatTest, LayoutNestedBlocks) {
3207   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3208                "  struct s {\n"
3209                "    int i;\n"
3210                "  };\n"
3211                "  s kBitsToOs[] = {{10}};\n"
3212                "  for (int i = 0; i < 10; ++i)\n"
3213                "    return;\n"
3214                "}");
3215   verifyFormat("call(parameter, {\n"
3216                "  something();\n"
3217                "  // Comment using all columns.\n"
3218                "  somethingelse();\n"
3219                "});",
3220                getLLVMStyleWithColumns(40));
3221   verifyFormat("DEBUG( //\n"
3222                "    { f(); }, a);");
3223   verifyFormat("DEBUG( //\n"
3224                "    {\n"
3225                "      f(); //\n"
3226                "    },\n"
3227                "    a);");
3228 
3229   EXPECT_EQ("call(parameter, {\n"
3230             "  something();\n"
3231             "  // Comment too\n"
3232             "  // looooooooooong.\n"
3233             "  somethingElse();\n"
3234             "});",
3235             format("call(parameter, {\n"
3236                    "  something();\n"
3237                    "  // Comment too looooooooooong.\n"
3238                    "  somethingElse();\n"
3239                    "});",
3240                    getLLVMStyleWithColumns(29)));
3241   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3242   EXPECT_EQ("DEBUG({ // comment\n"
3243             "  int i;\n"
3244             "});",
3245             format("DEBUG({ // comment\n"
3246                    "int  i;\n"
3247                    "});"));
3248   EXPECT_EQ("DEBUG({\n"
3249             "  int i;\n"
3250             "\n"
3251             "  // comment\n"
3252             "  int j;\n"
3253             "});",
3254             format("DEBUG({\n"
3255                    "  int  i;\n"
3256                    "\n"
3257                    "  // comment\n"
3258                    "  int  j;\n"
3259                    "});"));
3260 
3261   verifyFormat("DEBUG({\n"
3262                "  if (a)\n"
3263                "    return;\n"
3264                "});");
3265   verifyGoogleFormat("DEBUG({\n"
3266                      "  if (a) return;\n"
3267                      "});");
3268   FormatStyle Style = getGoogleStyle();
3269   Style.ColumnLimit = 45;
3270   verifyFormat("Debug(aaaaa,\n"
3271                "      {\n"
3272                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3273                "      },\n"
3274                "      a);",
3275                Style);
3276 
3277   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3278 
3279   verifyNoCrash("^{v^{a}}");
3280 }
3281 
3282 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3283   EXPECT_EQ("#define MACRO()                     \\\n"
3284             "  Debug(aaa, /* force line break */ \\\n"
3285             "        {                           \\\n"
3286             "          int i;                    \\\n"
3287             "          int j;                    \\\n"
3288             "        })",
3289             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3290                    "          {  int   i;  int  j;   })",
3291                    getGoogleStyle()));
3292 
3293   EXPECT_EQ("#define A                                       \\\n"
3294             "  [] {                                          \\\n"
3295             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3296             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3297             "  }",
3298             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3299                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3300                    getGoogleStyle()));
3301 }
3302 
3303 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3304   EXPECT_EQ("{}", format("{}"));
3305   verifyFormat("enum E {};");
3306   verifyFormat("enum E {}");
3307 }
3308 
3309 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3310   FormatStyle Style = getLLVMStyle();
3311   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3312   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3313   verifyFormat("FOO_BEGIN\n"
3314                "  FOO_ENTRY\n"
3315                "FOO_END", Style);
3316   verifyFormat("FOO_BEGIN\n"
3317                "  NESTED_FOO_BEGIN\n"
3318                "    NESTED_FOO_ENTRY\n"
3319                "  NESTED_FOO_END\n"
3320                "FOO_END", Style);
3321   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3322                "  int x;\n"
3323                "  x = 1;\n"
3324                "FOO_END(Baz)", Style);
3325 }
3326 
3327 //===----------------------------------------------------------------------===//
3328 // Line break tests.
3329 //===----------------------------------------------------------------------===//
3330 
3331 TEST_F(FormatTest, PreventConfusingIndents) {
3332   verifyFormat(
3333       "void f() {\n"
3334       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3335       "                         parameter, parameter, parameter)),\n"
3336       "                     SecondLongCall(parameter));\n"
3337       "}");
3338   verifyFormat(
3339       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3340       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3341       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3342       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3343   verifyFormat(
3344       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3345       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3346       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3347       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3348   verifyFormat(
3349       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3350       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3351       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3352       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3353   verifyFormat("int a = bbbb && ccc && fffff(\n"
3354                "#define A Just forcing a new line\n"
3355                "                           ddd);");
3356 }
3357 
3358 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3359   verifyFormat(
3360       "bool aaaaaaa =\n"
3361       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3362       "    bbbbbbbb();");
3363   verifyFormat(
3364       "bool aaaaaaa =\n"
3365       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3366       "    bbbbbbbb();");
3367 
3368   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3369                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3370                "    ccccccccc == ddddddddddd;");
3371   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3372                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3373                "    ccccccccc == ddddddddddd;");
3374   verifyFormat(
3375       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3376       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3377       "    ccccccccc == ddddddddddd;");
3378 
3379   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3380                "                 aaaaaa) &&\n"
3381                "         bbbbbb && cccccc;");
3382   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3383                "                 aaaaaa) >>\n"
3384                "         bbbbbb;");
3385   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3386                "    SourceMgr.getSpellingColumnNumber(\n"
3387                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3388                "    1);");
3389 
3390   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3391                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3392                "    cccccc) {\n}");
3393   verifyFormat("b = a &&\n"
3394                "    // Comment\n"
3395                "    b.c && d;");
3396 
3397   // If the LHS of a comparison is not a binary expression itself, the
3398   // additional linebreak confuses many people.
3399   verifyFormat(
3400       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3401       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3402       "}");
3403   verifyFormat(
3404       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3405       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3406       "}");
3407   verifyFormat(
3408       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3409       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3410       "}");
3411   // Even explicit parentheses stress the precedence enough to make the
3412   // additional break unnecessary.
3413   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3414                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3415                "}");
3416   // This cases is borderline, but with the indentation it is still readable.
3417   verifyFormat(
3418       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3419       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3420       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3421       "}",
3422       getLLVMStyleWithColumns(75));
3423 
3424   // If the LHS is a binary expression, we should still use the additional break
3425   // as otherwise the formatting hides the operator precedence.
3426   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3427                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3428                "    5) {\n"
3429                "}");
3430 
3431   FormatStyle OnePerLine = getLLVMStyle();
3432   OnePerLine.BinPackParameters = false;
3433   verifyFormat(
3434       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3435       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3436       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3437       OnePerLine);
3438 }
3439 
3440 TEST_F(FormatTest, ExpressionIndentation) {
3441   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3442                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3443                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3444                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3445                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3446                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3447                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3448                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3449                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3450   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3451                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3452                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3453                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3454   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3455                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3456                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3457                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3458   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3459                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3460                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3461                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3462   verifyFormat("if () {\n"
3463                "} else if (aaaaa &&\n"
3464                "           bbbbb > // break\n"
3465                "               ccccc) {\n"
3466                "}");
3467 
3468   // Presence of a trailing comment used to change indentation of b.
3469   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3470                "       b;\n"
3471                "return aaaaaaaaaaaaaaaaaaa +\n"
3472                "       b; //",
3473                getLLVMStyleWithColumns(30));
3474 }
3475 
3476 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3477   // Not sure what the best system is here. Like this, the LHS can be found
3478   // immediately above an operator (everything with the same or a higher
3479   // indent). The RHS is aligned right of the operator and so compasses
3480   // everything until something with the same indent as the operator is found.
3481   // FIXME: Is this a good system?
3482   FormatStyle Style = getLLVMStyle();
3483   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3484   verifyFormat(
3485       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3486       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3487       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3488       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3489       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3490       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3491       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3492       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3493       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3494       Style);
3495   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3496                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3497                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3498                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3499                Style);
3500   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3501                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3502                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3503                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3504                Style);
3505   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3506                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3507                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3508                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3509                Style);
3510   verifyFormat("if () {\n"
3511                "} else if (aaaaa\n"
3512                "           && bbbbb // break\n"
3513                "                  > ccccc) {\n"
3514                "}",
3515                Style);
3516   verifyFormat("return (a)\n"
3517                "       // comment\n"
3518                "       + b;",
3519                Style);
3520   verifyFormat(
3521       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3522       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3523       "             + cc;",
3524       Style);
3525 
3526   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3527                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3528                Style);
3529 
3530   // Forced by comments.
3531   verifyFormat(
3532       "unsigned ContentSize =\n"
3533       "    sizeof(int16_t)   // DWARF ARange version number\n"
3534       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3535       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3536       "    + sizeof(int8_t); // Segment Size (in bytes)");
3537 
3538   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3539                "       == boost::fusion::at_c<1>(iiii).second;",
3540                Style);
3541 
3542   Style.ColumnLimit = 60;
3543   verifyFormat("zzzzzzzzzz\n"
3544                "    = bbbbbbbbbbbbbbbbb\n"
3545                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3546                Style);
3547 }
3548 
3549 TEST_F(FormatTest, NoOperandAlignment) {
3550   FormatStyle Style = getLLVMStyle();
3551   Style.AlignOperands = false;
3552   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3553   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3554                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3555                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3556                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3557                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3558                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3559                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3560                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3561                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3562                Style);
3563 
3564   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3565                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3566                "    + cc;",
3567                Style);
3568   verifyFormat("int a = aa\n"
3569                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3570                "        * cccccccccccccccccccccccccccccccccccc;",
3571                Style);
3572 
3573   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3574   verifyFormat("return (a > b\n"
3575                "    // comment1\n"
3576                "    // comment2\n"
3577                "    || c);",
3578                Style);
3579 }
3580 
3581 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3582   FormatStyle Style = getLLVMStyle();
3583   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3584   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3585                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3586                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3587                Style);
3588 }
3589 
3590 TEST_F(FormatTest, ConstructorInitializers) {
3591   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3592   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3593                getLLVMStyleWithColumns(45));
3594   verifyFormat("Constructor()\n"
3595                "    : Inttializer(FitsOnTheLine) {}",
3596                getLLVMStyleWithColumns(44));
3597   verifyFormat("Constructor()\n"
3598                "    : Inttializer(FitsOnTheLine) {}",
3599                getLLVMStyleWithColumns(43));
3600 
3601   verifyFormat("template <typename T>\n"
3602                "Constructor() : Initializer(FitsOnTheLine) {}",
3603                getLLVMStyleWithColumns(45));
3604 
3605   verifyFormat(
3606       "SomeClass::Constructor()\n"
3607       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3608 
3609   verifyFormat(
3610       "SomeClass::Constructor()\n"
3611       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3612       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3613   verifyFormat(
3614       "SomeClass::Constructor()\n"
3615       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3616       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3617   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3618                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3619                "    : aaaaaaaaaa(aaaaaa) {}");
3620 
3621   verifyFormat("Constructor()\n"
3622                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3623                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3624                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3625                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3626 
3627   verifyFormat("Constructor()\n"
3628                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3629                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3630 
3631   verifyFormat("Constructor(int Parameter = 0)\n"
3632                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3633                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3634   verifyFormat("Constructor()\n"
3635                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3636                "}",
3637                getLLVMStyleWithColumns(60));
3638   verifyFormat("Constructor()\n"
3639                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3640                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3641 
3642   // Here a line could be saved by splitting the second initializer onto two
3643   // lines, but that is not desirable.
3644   verifyFormat("Constructor()\n"
3645                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3646                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3647                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3648 
3649   FormatStyle OnePerLine = getLLVMStyle();
3650   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3651   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3652   verifyFormat("SomeClass::Constructor()\n"
3653                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3654                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3655                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3656                OnePerLine);
3657   verifyFormat("SomeClass::Constructor()\n"
3658                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3659                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3660                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3661                OnePerLine);
3662   verifyFormat("MyClass::MyClass(int var)\n"
3663                "    : some_var_(var),            // 4 space indent\n"
3664                "      some_other_var_(var + 1) { // lined up\n"
3665                "}",
3666                OnePerLine);
3667   verifyFormat("Constructor()\n"
3668                "    : aaaaa(aaaaaa),\n"
3669                "      aaaaa(aaaaaa),\n"
3670                "      aaaaa(aaaaaa),\n"
3671                "      aaaaa(aaaaaa),\n"
3672                "      aaaaa(aaaaaa) {}",
3673                OnePerLine);
3674   verifyFormat("Constructor()\n"
3675                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3676                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3677                OnePerLine);
3678   OnePerLine.BinPackParameters = false;
3679   verifyFormat(
3680       "Constructor()\n"
3681       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3682       "          aaaaaaaaaaa().aaa(),\n"
3683       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3684       OnePerLine);
3685   OnePerLine.ColumnLimit = 60;
3686   verifyFormat("Constructor()\n"
3687                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3688                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3689                OnePerLine);
3690 
3691   EXPECT_EQ("Constructor()\n"
3692             "    : // Comment forcing unwanted break.\n"
3693             "      aaaa(aaaa) {}",
3694             format("Constructor() :\n"
3695                    "    // Comment forcing unwanted break.\n"
3696                    "    aaaa(aaaa) {}"));
3697 }
3698 
3699 TEST_F(FormatTest, MemoizationTests) {
3700   // This breaks if the memoization lookup does not take \c Indent and
3701   // \c LastSpace into account.
3702   verifyFormat(
3703       "extern CFRunLoopTimerRef\n"
3704       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3705       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3706       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3707       "                     CFRunLoopTimerContext *context) {}");
3708 
3709   // Deep nesting somewhat works around our memoization.
3710   verifyFormat(
3711       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3712       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3713       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3714       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3715       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3716       getLLVMStyleWithColumns(65));
3717   verifyFormat(
3718       "aaaaa(\n"
3719       "    aaaaa,\n"
3720       "    aaaaa(\n"
3721       "        aaaaa,\n"
3722       "        aaaaa(\n"
3723       "            aaaaa,\n"
3724       "            aaaaa(\n"
3725       "                aaaaa,\n"
3726       "                aaaaa(\n"
3727       "                    aaaaa,\n"
3728       "                    aaaaa(\n"
3729       "                        aaaaa,\n"
3730       "                        aaaaa(\n"
3731       "                            aaaaa,\n"
3732       "                            aaaaa(\n"
3733       "                                aaaaa,\n"
3734       "                                aaaaa(\n"
3735       "                                    aaaaa,\n"
3736       "                                    aaaaa(\n"
3737       "                                        aaaaa,\n"
3738       "                                        aaaaa(\n"
3739       "                                            aaaaa,\n"
3740       "                                            aaaaa(\n"
3741       "                                                aaaaa,\n"
3742       "                                                aaaaa))))))))))));",
3743       getLLVMStyleWithColumns(65));
3744   verifyFormat(
3745       "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"
3746       "                                  a),\n"
3747       "                                a),\n"
3748       "                              a),\n"
3749       "                            a),\n"
3750       "                          a),\n"
3751       "                        a),\n"
3752       "                      a),\n"
3753       "                    a),\n"
3754       "                  a),\n"
3755       "                a),\n"
3756       "              a),\n"
3757       "            a),\n"
3758       "          a),\n"
3759       "        a),\n"
3760       "      a),\n"
3761       "    a),\n"
3762       "  a)",
3763       getLLVMStyleWithColumns(65));
3764 
3765   // This test takes VERY long when memoization is broken.
3766   FormatStyle OnePerLine = getLLVMStyle();
3767   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3768   OnePerLine.BinPackParameters = false;
3769   std::string input = "Constructor()\n"
3770                       "    : aaaa(a,\n";
3771   for (unsigned i = 0, e = 80; i != e; ++i) {
3772     input += "           a,\n";
3773   }
3774   input += "           a) {}";
3775   verifyFormat(input, OnePerLine);
3776 }
3777 
3778 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3779   verifyFormat(
3780       "void f() {\n"
3781       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3782       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3783       "    f();\n"
3784       "}");
3785   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3786                "    Intervals[i - 1].getRange().getLast()) {\n}");
3787 }
3788 
3789 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3790   // Principially, we break function declarations in a certain order:
3791   // 1) break amongst arguments.
3792   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3793                "                              Cccccccccccccc cccccccccccccc);");
3794   verifyFormat("template <class TemplateIt>\n"
3795                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3796                "                            TemplateIt *stop) {}");
3797 
3798   // 2) break after return type.
3799   verifyFormat(
3800       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3801       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3802       getGoogleStyle());
3803 
3804   // 3) break after (.
3805   verifyFormat(
3806       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3807       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3808       getGoogleStyle());
3809 
3810   // 4) break before after nested name specifiers.
3811   verifyFormat(
3812       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3813       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3814       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3815       getGoogleStyle());
3816 
3817   // However, there are exceptions, if a sufficient amount of lines can be
3818   // saved.
3819   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3820   // more adjusting.
3821   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3822                "                                  Cccccccccccccc cccccccccc,\n"
3823                "                                  Cccccccccccccc cccccccccc,\n"
3824                "                                  Cccccccccccccc cccccccccc,\n"
3825                "                                  Cccccccccccccc cccccccccc);");
3826   verifyFormat(
3827       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3828       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3829       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3830       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3831       getGoogleStyle());
3832   verifyFormat(
3833       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3834       "                                          Cccccccccccccc cccccccccc,\n"
3835       "                                          Cccccccccccccc cccccccccc,\n"
3836       "                                          Cccccccccccccc cccccccccc,\n"
3837       "                                          Cccccccccccccc cccccccccc,\n"
3838       "                                          Cccccccccccccc cccccccccc,\n"
3839       "                                          Cccccccccccccc cccccccccc);");
3840   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3841                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3842                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3843                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3844                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3845 
3846   // Break after multi-line parameters.
3847   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3848                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3849                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3850                "    bbbb bbbb);");
3851   verifyFormat("void SomeLoooooooooooongFunction(\n"
3852                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3853                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3854                "    int bbbbbbbbbbbbb);");
3855 
3856   // Treat overloaded operators like other functions.
3857   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3858                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3859   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3860                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3861   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3862                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3863   verifyGoogleFormat(
3864       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3865       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3866   verifyGoogleFormat(
3867       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3868       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3869   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3870                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3871   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3872                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3873   verifyGoogleFormat(
3874       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3875       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3876       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3877   verifyGoogleFormat(
3878       "template <typename T>\n"
3879       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3880       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3881       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3882 
3883   FormatStyle Style = getLLVMStyle();
3884   Style.PointerAlignment = FormatStyle::PAS_Left;
3885   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3886                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3887                Style);
3888   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3889                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3890                Style);
3891 }
3892 
3893 TEST_F(FormatTest, TrailingReturnType) {
3894   verifyFormat("auto foo() -> int;\n");
3895   verifyFormat("struct S {\n"
3896                "  auto bar() const -> int;\n"
3897                "};");
3898   verifyFormat("template <size_t Order, typename T>\n"
3899                "auto load_img(const std::string &filename)\n"
3900                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3901   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3902                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3903   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3904   verifyFormat("template <typename T>\n"
3905                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3906                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3907 
3908   // Not trailing return types.
3909   verifyFormat("void f() { auto a = b->c(); }");
3910 }
3911 
3912 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3913   // Avoid breaking before trailing 'const' or other trailing annotations, if
3914   // they are not function-like.
3915   FormatStyle Style = getGoogleStyle();
3916   Style.ColumnLimit = 47;
3917   verifyFormat("void someLongFunction(\n"
3918                "    int someLoooooooooooooongParameter) const {\n}",
3919                getLLVMStyleWithColumns(47));
3920   verifyFormat("LoooooongReturnType\n"
3921                "someLoooooooongFunction() const {}",
3922                getLLVMStyleWithColumns(47));
3923   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3924                "    const {}",
3925                Style);
3926   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3927                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3928   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3929                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3930   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3931                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3932   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3933                "                   aaaaaaaaaaa aaaaa) const override;");
3934   verifyGoogleFormat(
3935       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3936       "    const override;");
3937 
3938   // Even if the first parameter has to be wrapped.
3939   verifyFormat("void someLongFunction(\n"
3940                "    int someLongParameter) const {}",
3941                getLLVMStyleWithColumns(46));
3942   verifyFormat("void someLongFunction(\n"
3943                "    int someLongParameter) const {}",
3944                Style);
3945   verifyFormat("void someLongFunction(\n"
3946                "    int someLongParameter) override {}",
3947                Style);
3948   verifyFormat("void someLongFunction(\n"
3949                "    int someLongParameter) OVERRIDE {}",
3950                Style);
3951   verifyFormat("void someLongFunction(\n"
3952                "    int someLongParameter) final {}",
3953                Style);
3954   verifyFormat("void someLongFunction(\n"
3955                "    int someLongParameter) FINAL {}",
3956                Style);
3957   verifyFormat("void someLongFunction(\n"
3958                "    int parameter) const override {}",
3959                Style);
3960 
3961   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3962   verifyFormat("void someLongFunction(\n"
3963                "    int someLongParameter) const\n"
3964                "{\n"
3965                "}",
3966                Style);
3967 
3968   // Unless these are unknown annotations.
3969   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3970                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3971                "    LONG_AND_UGLY_ANNOTATION;");
3972 
3973   // Breaking before function-like trailing annotations is fine to keep them
3974   // close to their arguments.
3975   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3976                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3977   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3978                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3979   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3980                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3981   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3982                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3983   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3984 
3985   verifyFormat(
3986       "void aaaaaaaaaaaaaaaaaa()\n"
3987       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3988       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3989   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3990                "    __attribute__((unused));");
3991   verifyGoogleFormat(
3992       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3993       "    GUARDED_BY(aaaaaaaaaaaa);");
3994   verifyGoogleFormat(
3995       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3996       "    GUARDED_BY(aaaaaaaaaaaa);");
3997   verifyGoogleFormat(
3998       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3999       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4000   verifyGoogleFormat(
4001       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4002       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4003 }
4004 
4005 TEST_F(FormatTest, FunctionAnnotations) {
4006   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4007                "int OldFunction(const string &parameter) {}");
4008   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4009                "string OldFunction(const string &parameter) {}");
4010   verifyFormat("template <typename T>\n"
4011                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4012                "string OldFunction(const string &parameter) {}");
4013 
4014   // Not function annotations.
4015   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4016                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4017   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4018                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4019   verifyFormat("MACRO(abc).function() // wrap\n"
4020                "    << abc;");
4021   verifyFormat("MACRO(abc)->function() // wrap\n"
4022                "    << abc;");
4023   verifyFormat("MACRO(abc)::function() // wrap\n"
4024                "    << abc;");
4025 }
4026 
4027 TEST_F(FormatTest, BreaksDesireably) {
4028   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4029                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4030                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4031   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4032                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4033                "}");
4034 
4035   verifyFormat(
4036       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4037       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4038 
4039   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4040                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4041                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4042 
4043   verifyFormat(
4044       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4045       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4046       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4047       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4048 
4049   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4050                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4051 
4052   verifyFormat(
4053       "void f() {\n"
4054       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4055       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4056       "}");
4057   verifyFormat(
4058       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4059       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4060   verifyFormat(
4061       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4062       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4063   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4064                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4065                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4066 
4067   // Indent consistently independent of call expression and unary operator.
4068   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4069                "    dddddddddddddddddddddddddddddd));");
4070   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4071                "    dddddddddddddddddddddddddddddd));");
4072   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4073                "    dddddddddddddddddddddddddddddd));");
4074 
4075   // This test case breaks on an incorrect memoization, i.e. an optimization not
4076   // taking into account the StopAt value.
4077   verifyFormat(
4078       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4079       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4080       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4081       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4082 
4083   verifyFormat("{\n  {\n    {\n"
4084                "      Annotation.SpaceRequiredBefore =\n"
4085                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4086                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4087                "    }\n  }\n}");
4088 
4089   // Break on an outer level if there was a break on an inner level.
4090   EXPECT_EQ("f(g(h(a, // comment\n"
4091             "      b, c),\n"
4092             "    d, e),\n"
4093             "  x, y);",
4094             format("f(g(h(a, // comment\n"
4095                    "    b, c), d, e), x, y);"));
4096 
4097   // Prefer breaking similar line breaks.
4098   verifyFormat(
4099       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4100       "                             NSTrackingMouseEnteredAndExited |\n"
4101       "                             NSTrackingActiveAlways;");
4102 }
4103 
4104 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4105   FormatStyle NoBinPacking = getGoogleStyle();
4106   NoBinPacking.BinPackParameters = false;
4107   NoBinPacking.BinPackArguments = true;
4108   verifyFormat("void f() {\n"
4109                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4110                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4111                "}",
4112                NoBinPacking);
4113   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4114                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4115                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4116                NoBinPacking);
4117 
4118   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4119   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4120                "                        vector<int> bbbbbbbbbbbbbbb);",
4121                NoBinPacking);
4122   // FIXME: This behavior difference is probably not wanted. However, currently
4123   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4124   // template arguments from BreakBeforeParameter being set because of the
4125   // one-per-line formatting.
4126   verifyFormat(
4127       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4128       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4129       NoBinPacking);
4130   verifyFormat(
4131       "void fffffffffff(\n"
4132       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4133       "        aaaaaaaaaa);");
4134 }
4135 
4136 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4137   FormatStyle NoBinPacking = getGoogleStyle();
4138   NoBinPacking.BinPackParameters = false;
4139   NoBinPacking.BinPackArguments = false;
4140   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4141                "  aaaaaaaaaaaaaaaaaaaa,\n"
4142                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4143                NoBinPacking);
4144   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4145                "        aaaaaaaaaaaaa,\n"
4146                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4147                NoBinPacking);
4148   verifyFormat(
4149       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4150       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4151       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4152       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4153       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4154       NoBinPacking);
4155   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4156                "    .aaaaaaaaaaaaaaaaaa();",
4157                NoBinPacking);
4158   verifyFormat("void f() {\n"
4159                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4160                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4161                "}",
4162                NoBinPacking);
4163 
4164   verifyFormat(
4165       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4166       "             aaaaaaaaaaaa,\n"
4167       "             aaaaaaaaaaaa);",
4168       NoBinPacking);
4169   verifyFormat(
4170       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4171       "                               ddddddddddddddddddddddddddddd),\n"
4172       "             test);",
4173       NoBinPacking);
4174 
4175   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4176                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4177                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4178                "    aaaaaaaaaaaaaaaaaa;",
4179                NoBinPacking);
4180   verifyFormat("a(\"a\"\n"
4181                "  \"a\",\n"
4182                "  a);");
4183 
4184   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4185   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4186                "                aaaaaaaaa,\n"
4187                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4188                NoBinPacking);
4189   verifyFormat(
4190       "void f() {\n"
4191       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4192       "      .aaaaaaa();\n"
4193       "}",
4194       NoBinPacking);
4195   verifyFormat(
4196       "template <class SomeType, class SomeOtherType>\n"
4197       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4198       NoBinPacking);
4199 }
4200 
4201 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4202   FormatStyle Style = getLLVMStyleWithColumns(15);
4203   Style.ExperimentalAutoDetectBinPacking = true;
4204   EXPECT_EQ("aaa(aaaa,\n"
4205             "    aaaa,\n"
4206             "    aaaa);\n"
4207             "aaa(aaaa,\n"
4208             "    aaaa,\n"
4209             "    aaaa);",
4210             format("aaa(aaaa,\n" // one-per-line
4211                    "  aaaa,\n"
4212                    "    aaaa  );\n"
4213                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4214                    Style));
4215   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4216             "    aaaa);\n"
4217             "aaa(aaaa, aaaa,\n"
4218             "    aaaa);",
4219             format("aaa(aaaa,  aaaa,\n" // bin-packed
4220                    "    aaaa  );\n"
4221                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4222                    Style));
4223 }
4224 
4225 TEST_F(FormatTest, FormatsBuilderPattern) {
4226   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4227                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4228                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4229                "    .StartsWith(\".init\", ORDER_INIT)\n"
4230                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4231                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4232                "    .Default(ORDER_TEXT);\n");
4233 
4234   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4235                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4236   verifyFormat(
4237       "aaaaaaa->aaaaaaa\n"
4238       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4239       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4240       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4241   verifyFormat(
4242       "aaaaaaa->aaaaaaa\n"
4243       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4244       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4245   verifyFormat(
4246       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4247       "    aaaaaaaaaaaaaa);");
4248   verifyFormat(
4249       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4250       "    aaaaaa->aaaaaaaaaaaa()\n"
4251       "        ->aaaaaaaaaaaaaaaa(\n"
4252       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4253       "        ->aaaaaaaaaaaaaaaaa();");
4254   verifyGoogleFormat(
4255       "void f() {\n"
4256       "  someo->Add((new util::filetools::Handler(dir))\n"
4257       "                 ->OnEvent1(NewPermanentCallback(\n"
4258       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4259       "                 ->OnEvent2(NewPermanentCallback(\n"
4260       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4261       "                 ->OnEvent3(NewPermanentCallback(\n"
4262       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4263       "                 ->OnEvent5(NewPermanentCallback(\n"
4264       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4265       "                 ->OnEvent6(NewPermanentCallback(\n"
4266       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4267       "}");
4268 
4269   verifyFormat(
4270       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4271   verifyFormat("aaaaaaaaaaaaaaa()\n"
4272                "    .aaaaaaaaaaaaaaa()\n"
4273                "    .aaaaaaaaaaaaaaa()\n"
4274                "    .aaaaaaaaaaaaaaa()\n"
4275                "    .aaaaaaaaaaaaaaa();");
4276   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4277                "    .aaaaaaaaaaaaaaa()\n"
4278                "    .aaaaaaaaaaaaaaa()\n"
4279                "    .aaaaaaaaaaaaaaa();");
4280   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4281                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4282                "    .aaaaaaaaaaaaaaa();");
4283   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4284                "    ->aaaaaaaaaaaaaae(0)\n"
4285                "    ->aaaaaaaaaaaaaaa();");
4286 
4287   // Don't linewrap after very short segments.
4288   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4289                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4290                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4291   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4292                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4293                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4294   verifyFormat("aaa()\n"
4295                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4296                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4297                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4298 
4299   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4300                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4301                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4302   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4303                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4304                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4305 
4306   // Prefer not to break after empty parentheses.
4307   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4308                "    First->LastNewlineOffset);");
4309 
4310   // Prefer not to create "hanging" indents.
4311   verifyFormat(
4312       "return !soooooooooooooome_map\n"
4313       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4314       "            .second;");
4315   verifyFormat(
4316       "return aaaaaaaaaaaaaaaa\n"
4317       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4318       "    .aaaa(aaaaaaaaaaaaaa);");
4319   // No hanging indent here.
4320   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4321                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4322   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4323                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4324   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4325                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4326                getLLVMStyleWithColumns(60));
4327   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4328                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4329                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4330                getLLVMStyleWithColumns(59));
4331   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4332                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4333                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4334 }
4335 
4336 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4337   verifyFormat(
4338       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4339       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4340   verifyFormat(
4341       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4342       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4343 
4344   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4345                "    ccccccccccccccccccccccccc) {\n}");
4346   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4347                "    ccccccccccccccccccccccccc) {\n}");
4348 
4349   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4350                "    ccccccccccccccccccccccccc) {\n}");
4351   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4352                "    ccccccccccccccccccccccccc) {\n}");
4353 
4354   verifyFormat(
4355       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4356       "    ccccccccccccccccccccccccc) {\n}");
4357   verifyFormat(
4358       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4359       "    ccccccccccccccccccccccccc) {\n}");
4360 
4361   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4362                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4363                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4364                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4365   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4366                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4367                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4368                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4369 
4370   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4371                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4372                "    aaaaaaaaaaaaaaa != aa) {\n}");
4373   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4374                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4375                "    aaaaaaaaaaaaaaa != aa) {\n}");
4376 }
4377 
4378 TEST_F(FormatTest, BreaksAfterAssignments) {
4379   verifyFormat(
4380       "unsigned Cost =\n"
4381       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4382       "                        SI->getPointerAddressSpaceee());\n");
4383   verifyFormat(
4384       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4385       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4386 
4387   verifyFormat(
4388       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4389       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4390   verifyFormat("unsigned OriginalStartColumn =\n"
4391                "    SourceMgr.getSpellingColumnNumber(\n"
4392                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4393                "    1;");
4394 }
4395 
4396 TEST_F(FormatTest, AlignsAfterAssignments) {
4397   verifyFormat(
4398       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4399       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4400   verifyFormat(
4401       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4402       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4403   verifyFormat(
4404       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4405       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4406   verifyFormat(
4407       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4408       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4409   verifyFormat(
4410       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4411       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4412       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4413 }
4414 
4415 TEST_F(FormatTest, AlignsAfterReturn) {
4416   verifyFormat(
4417       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4418       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4419   verifyFormat(
4420       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4421       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4422   verifyFormat(
4423       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4424       "       aaaaaaaaaaaaaaaaaaaaaa();");
4425   verifyFormat(
4426       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4427       "        aaaaaaaaaaaaaaaaaaaaaa());");
4428   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4429                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4430   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4431                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4432                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4433   verifyFormat("return\n"
4434                "    // true if code is one of a or b.\n"
4435                "    code == a || code == b;");
4436 }
4437 
4438 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4439   verifyFormat(
4440       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4441       "                                                aaaaaaaaa aaaaaaa) {}");
4442   verifyFormat(
4443       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4444       "                                               aaaaaaaaaaa aaaaaaaaa);");
4445   verifyFormat(
4446       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4447       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4448   FormatStyle Style = getLLVMStyle();
4449   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4450   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4451                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4452                Style);
4453   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4454                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4455                Style);
4456   verifyFormat("SomeLongVariableName->someFunction(\n"
4457                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4458                Style);
4459   verifyFormat(
4460       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4461       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4462       Style);
4463   verifyFormat(
4464       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4465       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4466       Style);
4467   verifyFormat(
4468       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4469       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4470       Style);
4471 
4472   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4473                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4474                "        b));",
4475                Style);
4476 
4477   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4478   Style.BinPackArguments = false;
4479   Style.BinPackParameters = false;
4480   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4481                "    aaaaaaaaaaa aaaaaaaa,\n"
4482                "    aaaaaaaaa aaaaaaa,\n"
4483                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4484                Style);
4485   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4486                "    aaaaaaaaaaa aaaaaaaaa,\n"
4487                "    aaaaaaaaaaa aaaaaaaaa,\n"
4488                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4489                Style);
4490   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4491                "    aaaaaaaaaaaaaaa,\n"
4492                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4493                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4494                Style);
4495   verifyFormat(
4496       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4497       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4498       Style);
4499   verifyFormat(
4500       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4501       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4502       Style);
4503   verifyFormat(
4504       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4505       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4506       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4507       "    aaaaaaaaaaaaaaaa);",
4508       Style);
4509   verifyFormat(
4510       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4511       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4512       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4513       "    aaaaaaaaaaaaaaaa);",
4514       Style);
4515 }
4516 
4517 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4518   FormatStyle Style = getLLVMStyleWithColumns(40);
4519   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4520                "          bbbbbbbbbbbbbbbbbbbbbb);",
4521                Style);
4522   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4523   Style.AlignOperands = false;
4524   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4525                "          bbbbbbbbbbbbbbbbbbbbbb);",
4526                Style);
4527   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4528   Style.AlignOperands = true;
4529   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4530                "          bbbbbbbbbbbbbbbbbbbbbb);",
4531                Style);
4532   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4533   Style.AlignOperands = false;
4534   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4535                "    bbbbbbbbbbbbbbbbbbbbbb);",
4536                Style);
4537 }
4538 
4539 TEST_F(FormatTest, BreaksConditionalExpressions) {
4540   verifyFormat(
4541       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4542       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4543       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4544   verifyFormat(
4545       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4546       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4547   verifyFormat(
4548       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4549       "                                                    : aaaaaaaaaaaaa);");
4550   verifyFormat(
4551       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4552       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4553       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4554       "                   aaaaaaaaaaaaa);");
4555   verifyFormat(
4556       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4557       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4558       "                   aaaaaaaaaaaaa);");
4559   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4560                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4561                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4562                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4563                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4564   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4565                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4566                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4567                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4568                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4569                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4570                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4571   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4572                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4573                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4574                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4575                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4576   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4577                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4578                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4579   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4580                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4581                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4582                "        : aaaaaaaaaaaaaaaa;");
4583   verifyFormat(
4584       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4585       "    ? aaaaaaaaaaaaaaa\n"
4586       "    : aaaaaaaaaaaaaaa;");
4587   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4588                "          aaaaaaaaa\n"
4589                "      ? b\n"
4590                "      : c);");
4591   verifyFormat("return aaaa == bbbb\n"
4592                "           // comment\n"
4593                "           ? aaaa\n"
4594                "           : bbbb;");
4595   verifyFormat("unsigned Indent =\n"
4596                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4597                "                              ? IndentForLevel[TheLine.Level]\n"
4598                "                              : TheLine * 2,\n"
4599                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4600                getLLVMStyleWithColumns(70));
4601   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4602                "                  ? aaaaaaaaaaaaaaa\n"
4603                "                  : bbbbbbbbbbbbbbb //\n"
4604                "                        ? ccccccccccccccc\n"
4605                "                        : ddddddddddddddd;");
4606   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4607                "                  ? aaaaaaaaaaaaaaa\n"
4608                "                  : (bbbbbbbbbbbbbbb //\n"
4609                "                         ? ccccccccccccccc\n"
4610                "                         : ddddddddddddddd);");
4611   verifyFormat(
4612       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4613       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4614       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4615       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4616       "                                      : aaaaaaaaaa;");
4617   verifyFormat(
4618       "aaaaaa = aaaaaaaaaaaa\n"
4619       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4620       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4621       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4622 
4623   FormatStyle NoBinPacking = getLLVMStyle();
4624   NoBinPacking.BinPackArguments = false;
4625   verifyFormat(
4626       "void f() {\n"
4627       "  g(aaa,\n"
4628       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4629       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4630       "        ? aaaaaaaaaaaaaaa\n"
4631       "        : aaaaaaaaaaaaaaa);\n"
4632       "}",
4633       NoBinPacking);
4634   verifyFormat(
4635       "void f() {\n"
4636       "  g(aaa,\n"
4637       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4638       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4639       "        ?: aaaaaaaaaaaaaaa);\n"
4640       "}",
4641       NoBinPacking);
4642 
4643   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4644                "             // comment.\n"
4645                "             ccccccccccccccccccccccccccccccccccccccc\n"
4646                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4647                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4648 
4649   // Assignments in conditional expressions. Apparently not uncommon :-(.
4650   verifyFormat("return a != b\n"
4651                "           // comment\n"
4652                "           ? a = b\n"
4653                "           : a = b;");
4654   verifyFormat("return a != b\n"
4655                "           // comment\n"
4656                "           ? a = a != b\n"
4657                "                     // comment\n"
4658                "                     ? a = b\n"
4659                "                     : a\n"
4660                "           : a;\n");
4661   verifyFormat("return a != b\n"
4662                "           // comment\n"
4663                "           ? a\n"
4664                "           : a = a != b\n"
4665                "                     // comment\n"
4666                "                     ? a = b\n"
4667                "                     : a;");
4668 }
4669 
4670 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4671   FormatStyle Style = getLLVMStyle();
4672   Style.BreakBeforeTernaryOperators = false;
4673   Style.ColumnLimit = 70;
4674   verifyFormat(
4675       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4676       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4677       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4678       Style);
4679   verifyFormat(
4680       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4681       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4682       Style);
4683   verifyFormat(
4684       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4685       "                                                      aaaaaaaaaaaaa);",
4686       Style);
4687   verifyFormat(
4688       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4689       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4690       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4691       "                   aaaaaaaaaaaaa);",
4692       Style);
4693   verifyFormat(
4694       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4695       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4696       "                   aaaaaaaaaaaaa);",
4697       Style);
4698   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4699                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4700                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4701                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4702                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4703                Style);
4704   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4705                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4706                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4707                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4708                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4709                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4710                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4711                Style);
4712   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4713                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4714                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4715                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4716                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4717                Style);
4718   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4719                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4720                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4721                Style);
4722   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4723                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4724                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4725                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4726                Style);
4727   verifyFormat(
4728       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4729       "    aaaaaaaaaaaaaaa :\n"
4730       "    aaaaaaaaaaaaaaa;",
4731       Style);
4732   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4733                "          aaaaaaaaa ?\n"
4734                "      b :\n"
4735                "      c);",
4736                Style);
4737   verifyFormat(
4738       "unsigned Indent =\n"
4739       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4740       "                              IndentForLevel[TheLine.Level] :\n"
4741       "                              TheLine * 2,\n"
4742       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4743       Style);
4744   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4745                "                  aaaaaaaaaaaaaaa :\n"
4746                "                  bbbbbbbbbbbbbbb ? //\n"
4747                "                      ccccccccccccccc :\n"
4748                "                      ddddddddddddddd;",
4749                Style);
4750   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4751                "                  aaaaaaaaaaaaaaa :\n"
4752                "                  (bbbbbbbbbbbbbbb ? //\n"
4753                "                       ccccccccccccccc :\n"
4754                "                       ddddddddddddddd);",
4755                Style);
4756   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4757                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4758                "            ccccccccccccccccccccccccccc;",
4759                Style);
4760   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4761                "           aaaaa :\n"
4762                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4763                Style);
4764 }
4765 
4766 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4767   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4768                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4769   verifyFormat("bool a = true, b = false;");
4770 
4771   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4772                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4773                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4774                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4775   verifyFormat(
4776       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4777       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4778       "     d = e && f;");
4779   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4780                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4781   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4782                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4783   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4784                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4785 
4786   FormatStyle Style = getGoogleStyle();
4787   Style.PointerAlignment = FormatStyle::PAS_Left;
4788   Style.DerivePointerAlignment = false;
4789   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4790                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4791                "    *b = bbbbbbbbbbbbbbbbbbb;",
4792                Style);
4793   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4794                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4795                Style);
4796 }
4797 
4798 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4799   verifyFormat("arr[foo ? bar : baz];");
4800   verifyFormat("f()[foo ? bar : baz];");
4801   verifyFormat("(a + b)[foo ? bar : baz];");
4802   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4803 }
4804 
4805 TEST_F(FormatTest, AlignsStringLiterals) {
4806   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4807                "                                      \"short literal\");");
4808   verifyFormat(
4809       "looooooooooooooooooooooooongFunction(\n"
4810       "    \"short literal\"\n"
4811       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4812   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4813                "             \" string literals\",\n"
4814                "             and, other, parameters);");
4815   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4816             "      \"5678\";",
4817             format("fun + \"1243\" /* comment */\n"
4818                    "      \"5678\";",
4819                    getLLVMStyleWithColumns(28)));
4820   EXPECT_EQ(
4821       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4822       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4823       "         \"aaaaaaaaaaaaaaaa\";",
4824       format("aaaaaa ="
4825              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4826              "aaaaaaaaaaaaaaaaaaaaa\" "
4827              "\"aaaaaaaaaaaaaaaa\";"));
4828   verifyFormat("a = a + \"a\"\n"
4829                "        \"a\"\n"
4830                "        \"a\";");
4831   verifyFormat("f(\"a\", \"b\"\n"
4832                "       \"c\");");
4833 
4834   verifyFormat(
4835       "#define LL_FORMAT \"ll\"\n"
4836       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4837       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4838 
4839   verifyFormat("#define A(X)          \\\n"
4840                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4841                "  \"ccccc\"",
4842                getLLVMStyleWithColumns(23));
4843   verifyFormat("#define A \"def\"\n"
4844                "f(\"abc\" A \"ghi\"\n"
4845                "  \"jkl\");");
4846 
4847   verifyFormat("f(L\"a\"\n"
4848                "  L\"b\");");
4849   verifyFormat("#define A(X)            \\\n"
4850                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4851                "  L\"ccccc\"",
4852                getLLVMStyleWithColumns(25));
4853 
4854   verifyFormat("f(@\"a\"\n"
4855                "  @\"b\");");
4856   verifyFormat("NSString s = @\"a\"\n"
4857                "             @\"b\"\n"
4858                "             @\"c\";");
4859   verifyFormat("NSString s = @\"a\"\n"
4860                "              \"b\"\n"
4861                "              \"c\";");
4862 }
4863 
4864 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4865   FormatStyle Style = getLLVMStyle();
4866   // No declarations or definitions should be moved to own line.
4867   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4868   verifyFormat("class A {\n"
4869                "  int f() { return 1; }\n"
4870                "  int g();\n"
4871                "};\n"
4872                "int f() { return 1; }\n"
4873                "int g();\n",
4874                Style);
4875 
4876   // All declarations and definitions should have the return type moved to its
4877   // own
4878   // line.
4879   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4880   verifyFormat("class E {\n"
4881                "  int\n"
4882                "  f() {\n"
4883                "    return 1;\n"
4884                "  }\n"
4885                "  int\n"
4886                "  g();\n"
4887                "};\n"
4888                "int\n"
4889                "f() {\n"
4890                "  return 1;\n"
4891                "}\n"
4892                "int\n"
4893                "g();\n",
4894                Style);
4895 
4896   // Top-level definitions, and no kinds of declarations should have the
4897   // return type moved to its own line.
4898   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4899   verifyFormat("class B {\n"
4900                "  int f() { return 1; }\n"
4901                "  int g();\n"
4902                "};\n"
4903                "int\n"
4904                "f() {\n"
4905                "  return 1;\n"
4906                "}\n"
4907                "int g();\n",
4908                Style);
4909 
4910   // Top-level definitions and declarations should have the return type moved
4911   // to its own line.
4912   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4913   verifyFormat("class C {\n"
4914                "  int f() { return 1; }\n"
4915                "  int g();\n"
4916                "};\n"
4917                "int\n"
4918                "f() {\n"
4919                "  return 1;\n"
4920                "}\n"
4921                "int\n"
4922                "g();\n",
4923                Style);
4924 
4925   // All definitions should have the return type moved to its own line, but no
4926   // kinds of declarations.
4927   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4928   verifyFormat("class D {\n"
4929                "  int\n"
4930                "  f() {\n"
4931                "    return 1;\n"
4932                "  }\n"
4933                "  int g();\n"
4934                "};\n"
4935                "int\n"
4936                "f() {\n"
4937                "  return 1;\n"
4938                "}\n"
4939                "int g();\n",
4940                Style);
4941   verifyFormat("const char *\n"
4942                "f(void) {\n" // Break here.
4943                "  return \"\";\n"
4944                "}\n"
4945                "const char *bar(void);\n", // No break here.
4946                Style);
4947   verifyFormat("template <class T>\n"
4948                "T *\n"
4949                "f(T &c) {\n" // Break here.
4950                "  return NULL;\n"
4951                "}\n"
4952                "template <class T> T *f(T &c);\n", // No break here.
4953                Style);
4954   verifyFormat("class C {\n"
4955                "  int\n"
4956                "  operator+() {\n"
4957                "    return 1;\n"
4958                "  }\n"
4959                "  int\n"
4960                "  operator()() {\n"
4961                "    return 1;\n"
4962                "  }\n"
4963                "};\n",
4964                Style);
4965   verifyFormat("void\n"
4966                "A::operator()() {}\n"
4967                "void\n"
4968                "A::operator>>() {}\n"
4969                "void\n"
4970                "A::operator+() {}\n",
4971                Style);
4972   verifyFormat("void *operator new(std::size_t s);", // No break here.
4973                Style);
4974   verifyFormat("void *\n"
4975                "operator new(std::size_t s) {}",
4976                Style);
4977   verifyFormat("void *\n"
4978                "operator delete[](void *ptr) {}",
4979                Style);
4980   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4981   verifyFormat("const char *\n"
4982                "f(void)\n" // Break here.
4983                "{\n"
4984                "  return \"\";\n"
4985                "}\n"
4986                "const char *bar(void);\n", // No break here.
4987                Style);
4988   verifyFormat("template <class T>\n"
4989                "T *\n"     // Problem here: no line break
4990                "f(T &c)\n" // Break here.
4991                "{\n"
4992                "  return NULL;\n"
4993                "}\n"
4994                "template <class T> T *f(T &c);\n", // No break here.
4995                Style);
4996 }
4997 
4998 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4999   FormatStyle NoBreak = getLLVMStyle();
5000   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5001   FormatStyle Break = getLLVMStyle();
5002   Break.AlwaysBreakBeforeMultilineStrings = true;
5003   verifyFormat("aaaa = \"bbbb\"\n"
5004                "       \"cccc\";",
5005                NoBreak);
5006   verifyFormat("aaaa =\n"
5007                "    \"bbbb\"\n"
5008                "    \"cccc\";",
5009                Break);
5010   verifyFormat("aaaa(\"bbbb\"\n"
5011                "     \"cccc\");",
5012                NoBreak);
5013   verifyFormat("aaaa(\n"
5014                "    \"bbbb\"\n"
5015                "    \"cccc\");",
5016                Break);
5017   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5018                "          \"cccc\");",
5019                NoBreak);
5020   verifyFormat("aaaa(qqq,\n"
5021                "     \"bbbb\"\n"
5022                "     \"cccc\");",
5023                Break);
5024   verifyFormat("aaaa(qqq,\n"
5025                "     L\"bbbb\"\n"
5026                "     L\"cccc\");",
5027                Break);
5028   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5029                "                      \"bbbb\"));",
5030                Break);
5031   verifyFormat("string s = someFunction(\n"
5032                "    \"abc\"\n"
5033                "    \"abc\");",
5034                Break);
5035 
5036   // As we break before unary operators, breaking right after them is bad.
5037   verifyFormat("string foo = abc ? \"x\"\n"
5038                "                   \"blah blah blah blah blah blah\"\n"
5039                "                 : \"y\";",
5040                Break);
5041 
5042   // Don't break if there is no column gain.
5043   verifyFormat("f(\"aaaa\"\n"
5044                "  \"bbbb\");",
5045                Break);
5046 
5047   // Treat literals with escaped newlines like multi-line string literals.
5048   EXPECT_EQ("x = \"a\\\n"
5049             "b\\\n"
5050             "c\";",
5051             format("x = \"a\\\n"
5052                    "b\\\n"
5053                    "c\";",
5054                    NoBreak));
5055   EXPECT_EQ("xxxx =\n"
5056             "    \"a\\\n"
5057             "b\\\n"
5058             "c\";",
5059             format("xxxx = \"a\\\n"
5060                    "b\\\n"
5061                    "c\";",
5062                    Break));
5063 
5064   // Exempt ObjC strings for now.
5065   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
5066             "                          @\"bbbb\";",
5067             format("NSString *const kString = @\"aaaa\"\n"
5068                    "@\"bbbb\";",
5069                    Break));
5070 
5071   Break.ColumnLimit = 0;
5072   verifyFormat("const char *hello = \"hello llvm\";", Break);
5073 }
5074 
5075 TEST_F(FormatTest, AlignsPipes) {
5076   verifyFormat(
5077       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5078       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5079       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5080   verifyFormat(
5081       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5082       "                     << aaaaaaaaaaaaaaaaaaaa;");
5083   verifyFormat(
5084       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5085       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5086   verifyFormat(
5087       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5088       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5089       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5090   verifyFormat(
5091       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5092       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5093       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5094   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5095                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5096                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5097                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5098   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5099                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5100   verifyFormat(
5101       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5102       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5103 
5104   verifyFormat("return out << \"somepacket = {\\n\"\n"
5105                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5106                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5107                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5108                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5109                "           << \"}\";");
5110 
5111   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5112                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5113                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5114   verifyFormat(
5115       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5116       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5117       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5118       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5119       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5120   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5121                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5122   verifyFormat(
5123       "void f() {\n"
5124       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5125       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5126       "}");
5127   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5128                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5129   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5130                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5131                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5132                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5133   verifyFormat("LOG_IF(aaa == //\n"
5134                "       bbb)\n"
5135                "    << a << b;");
5136 
5137   // Breaking before the first "<<" is generally not desirable.
5138   verifyFormat(
5139       "llvm::errs()\n"
5140       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5141       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5142       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5143       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5144       getLLVMStyleWithColumns(70));
5145   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5146                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5147                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5148                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5149                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5150                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5151                getLLVMStyleWithColumns(70));
5152 
5153   // But sometimes, breaking before the first "<<" is desirable.
5154   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5155                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5156   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5157                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5158                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5159   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5160                "    << BEF << IsTemplate << Description << E->getType();");
5161   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5162                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5163                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5164   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5165                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5166                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5167                "    << aaa;");
5168 
5169   verifyFormat(
5170       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5171       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5172 
5173   // Incomplete string literal.
5174   EXPECT_EQ("llvm::errs() << \"\n"
5175             "             << a;",
5176             format("llvm::errs() << \"\n<<a;"));
5177 
5178   verifyFormat("void f() {\n"
5179                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5180                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5181                "}");
5182 
5183   // Handle 'endl'.
5184   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5185                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5186   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5187 
5188   // Handle '\n'.
5189   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5190                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5191   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5192                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5193   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5194                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5195   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5196 }
5197 
5198 TEST_F(FormatTest, UnderstandsEquals) {
5199   verifyFormat(
5200       "aaaaaaaaaaaaaaaaa =\n"
5201       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5202   verifyFormat(
5203       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5204       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5205   verifyFormat(
5206       "if (a) {\n"
5207       "  f();\n"
5208       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5209       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5210       "}");
5211 
5212   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5213                "        100000000 + 10000000) {\n}");
5214 }
5215 
5216 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5217   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5218                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5219 
5220   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5221                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5222 
5223   verifyFormat(
5224       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5225       "                                                          Parameter2);");
5226 
5227   verifyFormat(
5228       "ShortObject->shortFunction(\n"
5229       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5230       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5231 
5232   verifyFormat("loooooooooooooongFunction(\n"
5233                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5234 
5235   verifyFormat(
5236       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5237       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5238 
5239   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5240                "    .WillRepeatedly(Return(SomeValue));");
5241   verifyFormat("void f() {\n"
5242                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5243                "      .Times(2)\n"
5244                "      .WillRepeatedly(Return(SomeValue));\n"
5245                "}");
5246   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5247                "    ccccccccccccccccccccccc);");
5248   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5249                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5250                "          .aaaaa(aaaaa),\n"
5251                "      aaaaaaaaaaaaaaaaaaaaa);");
5252   verifyFormat("void f() {\n"
5253                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5254                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5255                "}");
5256   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5257                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5258                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5259                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5260                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5261   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5262                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5263                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5264                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5265                "}");
5266 
5267   // Here, it is not necessary to wrap at "." or "->".
5268   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5269                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5270   verifyFormat(
5271       "aaaaaaaaaaa->aaaaaaaaa(\n"
5272       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5273       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5274 
5275   verifyFormat(
5276       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5277       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5278   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5279                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5280   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5281                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5282 
5283   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5284                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5285                "    .a();");
5286 
5287   FormatStyle NoBinPacking = getLLVMStyle();
5288   NoBinPacking.BinPackParameters = false;
5289   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5290                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5291                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5292                "                         aaaaaaaaaaaaaaaaaaa,\n"
5293                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5294                NoBinPacking);
5295 
5296   // If there is a subsequent call, change to hanging indentation.
5297   verifyFormat(
5298       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5299       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5300       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5301   verifyFormat(
5302       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5303       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5304   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5305                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5306                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5307   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5308                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5309                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5310 }
5311 
5312 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5313   verifyFormat("template <typename T>\n"
5314                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5315   verifyFormat("template <typename T>\n"
5316                "// T should be one of {A, B}.\n"
5317                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5318   verifyFormat(
5319       "template <typename T>\n"
5320       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5321   verifyFormat("template <typename T>\n"
5322                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5323                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5324   verifyFormat(
5325       "template <typename T>\n"
5326       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5327       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5328   verifyFormat(
5329       "template <typename T>\n"
5330       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5331       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5332       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5333   verifyFormat("template <typename T>\n"
5334                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5335                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5336   verifyFormat(
5337       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5338       "          typename T4 = char>\n"
5339       "void f();");
5340   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5341                "          template <typename> class cccccccccccccccccccccc,\n"
5342                "          typename ddddddddddddd>\n"
5343                "class C {};");
5344   verifyFormat(
5345       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5346       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5347 
5348   verifyFormat("void f() {\n"
5349                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5350                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5351                "}");
5352 
5353   verifyFormat("template <typename T> class C {};");
5354   verifyFormat("template <typename T> void f();");
5355   verifyFormat("template <typename T> void f() {}");
5356   verifyFormat(
5357       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5358       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5359       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5360       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5361       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5362       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5363       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5364       getLLVMStyleWithColumns(72));
5365   EXPECT_EQ("static_cast<A< //\n"
5366             "    B> *>(\n"
5367             "\n"
5368             "    );",
5369             format("static_cast<A<//\n"
5370                    "    B>*>(\n"
5371                    "\n"
5372                    "    );"));
5373   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5374                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5375 
5376   FormatStyle AlwaysBreak = getLLVMStyle();
5377   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5378   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5379   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5380   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5381   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5382                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5383                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5384   verifyFormat("template <template <typename> class Fooooooo,\n"
5385                "          template <typename> class Baaaaaaar>\n"
5386                "struct C {};",
5387                AlwaysBreak);
5388   verifyFormat("template <typename T> // T can be A, B or C.\n"
5389                "struct C {};",
5390                AlwaysBreak);
5391   verifyFormat("template <enum E> class A {\n"
5392                "public:\n"
5393                "  E *f();\n"
5394                "};");
5395 }
5396 
5397 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5398   verifyFormat(
5399       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5400       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5401   verifyFormat(
5402       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5403       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5404       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5405 
5406   // FIXME: Should we have the extra indent after the second break?
5407   verifyFormat(
5408       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5409       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5410       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5411 
5412   verifyFormat(
5413       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5414       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5415 
5416   // Breaking at nested name specifiers is generally not desirable.
5417   verifyFormat(
5418       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5419       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5420 
5421   verifyFormat(
5422       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5423       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5424       "                   aaaaaaaaaaaaaaaaaaaaa);",
5425       getLLVMStyleWithColumns(74));
5426 
5427   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5428                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5429                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5430 }
5431 
5432 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5433   verifyFormat("A<int> a;");
5434   verifyFormat("A<A<A<int>>> a;");
5435   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5436   verifyFormat("bool x = a < 1 || 2 > a;");
5437   verifyFormat("bool x = 5 < f<int>();");
5438   verifyFormat("bool x = f<int>() > 5;");
5439   verifyFormat("bool x = 5 < a<int>::x;");
5440   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5441   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5442 
5443   verifyGoogleFormat("A<A<int>> a;");
5444   verifyGoogleFormat("A<A<A<int>>> a;");
5445   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5446   verifyGoogleFormat("A<A<int> > a;");
5447   verifyGoogleFormat("A<A<A<int> > > a;");
5448   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5449   verifyGoogleFormat("A<::A<int>> a;");
5450   verifyGoogleFormat("A<::A> a;");
5451   verifyGoogleFormat("A< ::A> a;");
5452   verifyGoogleFormat("A< ::A<int> > a;");
5453   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5454   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5455   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5456   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5457   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5458             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5459 
5460   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5461 
5462   verifyFormat("test >> a >> b;");
5463   verifyFormat("test << a >> b;");
5464 
5465   verifyFormat("f<int>();");
5466   verifyFormat("template <typename T> void f() {}");
5467   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5468   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5469                "sizeof(char)>::type>;");
5470   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5471   verifyFormat("f(a.operator()<A>());");
5472   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5473                "      .template operator()<A>());",
5474                getLLVMStyleWithColumns(35));
5475 
5476   // Not template parameters.
5477   verifyFormat("return a < b && c > d;");
5478   verifyFormat("void f() {\n"
5479                "  while (a < b && c > d) {\n"
5480                "  }\n"
5481                "}");
5482   verifyFormat("template <typename... Types>\n"
5483                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5484 
5485   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5486                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5487                getLLVMStyleWithColumns(60));
5488   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5489   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5490   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5491 }
5492 
5493 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5494   verifyFormat("COMPARE(a, ==, b);");
5495 }
5496 
5497 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5498   verifyFormat("int A::*x;");
5499   verifyFormat("int (S::*func)(void *);");
5500   verifyFormat("void f() { int (S::*func)(void *); }");
5501   verifyFormat("typedef bool *(Class::*Member)() const;");
5502   verifyFormat("void f() {\n"
5503                "  (a->*f)();\n"
5504                "  a->*x;\n"
5505                "  (a.*f)();\n"
5506                "  ((*a).*f)();\n"
5507                "  a.*x;\n"
5508                "}");
5509   verifyFormat("void f() {\n"
5510                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5511                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5512                "}");
5513   verifyFormat(
5514       "(aaaaaaaaaa->*bbbbbbb)(\n"
5515       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5516   FormatStyle Style = getLLVMStyle();
5517   Style.PointerAlignment = FormatStyle::PAS_Left;
5518   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5519 }
5520 
5521 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5522   verifyFormat("int a = -2;");
5523   verifyFormat("f(-1, -2, -3);");
5524   verifyFormat("a[-1] = 5;");
5525   verifyFormat("int a = 5 + -2;");
5526   verifyFormat("if (i == -1) {\n}");
5527   verifyFormat("if (i != -1) {\n}");
5528   verifyFormat("if (i > -1) {\n}");
5529   verifyFormat("if (i < -1) {\n}");
5530   verifyFormat("++(a->f());");
5531   verifyFormat("--(a->f());");
5532   verifyFormat("(a->f())++;");
5533   verifyFormat("a[42]++;");
5534   verifyFormat("if (!(a->f())) {\n}");
5535 
5536   verifyFormat("a-- > b;");
5537   verifyFormat("b ? -a : c;");
5538   verifyFormat("n * sizeof char16;");
5539   verifyFormat("n * alignof char16;", getGoogleStyle());
5540   verifyFormat("sizeof(char);");
5541   verifyFormat("alignof(char);", getGoogleStyle());
5542 
5543   verifyFormat("return -1;");
5544   verifyFormat("switch (a) {\n"
5545                "case -1:\n"
5546                "  break;\n"
5547                "}");
5548   verifyFormat("#define X -1");
5549   verifyFormat("#define X -kConstant");
5550 
5551   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5552   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5553 
5554   verifyFormat("int a = /* confusing comment */ -1;");
5555   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5556   verifyFormat("int a = i /* confusing comment */++;");
5557 }
5558 
5559 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5560   verifyFormat("if (!aaaaaaaaaa( // break\n"
5561                "        aaaaa)) {\n"
5562                "}");
5563   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5564                "    aaaaa));");
5565   verifyFormat("*aaa = aaaaaaa( // break\n"
5566                "    bbbbbb);");
5567 }
5568 
5569 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5570   verifyFormat("bool operator<();");
5571   verifyFormat("bool operator>();");
5572   verifyFormat("bool operator=();");
5573   verifyFormat("bool operator==();");
5574   verifyFormat("bool operator!=();");
5575   verifyFormat("int operator+();");
5576   verifyFormat("int operator++();");
5577   verifyFormat("bool operator,();");
5578   verifyFormat("bool operator();");
5579   verifyFormat("bool operator()();");
5580   verifyFormat("bool operator[]();");
5581   verifyFormat("operator bool();");
5582   verifyFormat("operator int();");
5583   verifyFormat("operator void *();");
5584   verifyFormat("operator SomeType<int>();");
5585   verifyFormat("operator SomeType<int, int>();");
5586   verifyFormat("operator SomeType<SomeType<int>>();");
5587   verifyFormat("void *operator new(std::size_t size);");
5588   verifyFormat("void *operator new[](std::size_t size);");
5589   verifyFormat("void operator delete(void *ptr);");
5590   verifyFormat("void operator delete[](void *ptr);");
5591   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5592                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5593   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5594                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5595 
5596   verifyFormat(
5597       "ostream &operator<<(ostream &OutputStream,\n"
5598       "                    SomeReallyLongType WithSomeReallyLongValue);");
5599   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5600                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5601                "  return left.group < right.group;\n"
5602                "}");
5603   verifyFormat("SomeType &operator=(const SomeType &S);");
5604   verifyFormat("f.template operator()<int>();");
5605 
5606   verifyGoogleFormat("operator void*();");
5607   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5608   verifyGoogleFormat("operator ::A();");
5609 
5610   verifyFormat("using A::operator+;");
5611   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5612                "int i;");
5613 }
5614 
5615 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5616   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5617   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5618   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5619   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5620   verifyFormat("Deleted &operator=(const Deleted &) &;");
5621   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5622   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5623   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5624   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5625   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5626   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5627 
5628   FormatStyle AlignLeft = getLLVMStyle();
5629   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5630   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5631   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5632                AlignLeft);
5633   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5634   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5635   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5636   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5637   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5638   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5639 
5640   FormatStyle Spaces = getLLVMStyle();
5641   Spaces.SpacesInCStyleCastParentheses = true;
5642   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5643   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5644   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5645   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5646 
5647   Spaces.SpacesInCStyleCastParentheses = false;
5648   Spaces.SpacesInParentheses = true;
5649   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5650   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5651   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5652   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5653 }
5654 
5655 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5656   verifyFormat("void f() {\n"
5657                "  A *a = new A;\n"
5658                "  A *a = new (placement) A;\n"
5659                "  delete a;\n"
5660                "  delete (A *)a;\n"
5661                "}");
5662   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5663                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5664   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5665                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5666                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5667   verifyFormat("delete[] h->p;");
5668 }
5669 
5670 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5671   verifyFormat("int *f(int *a) {}");
5672   verifyFormat("int main(int argc, char **argv) {}");
5673   verifyFormat("Test::Test(int b) : a(b * b) {}");
5674   verifyIndependentOfContext("f(a, *a);");
5675   verifyFormat("void g() { f(*a); }");
5676   verifyIndependentOfContext("int a = b * 10;");
5677   verifyIndependentOfContext("int a = 10 * b;");
5678   verifyIndependentOfContext("int a = b * c;");
5679   verifyIndependentOfContext("int a += b * c;");
5680   verifyIndependentOfContext("int a -= b * c;");
5681   verifyIndependentOfContext("int a *= b * c;");
5682   verifyIndependentOfContext("int a /= b * c;");
5683   verifyIndependentOfContext("int a = *b;");
5684   verifyIndependentOfContext("int a = *b * c;");
5685   verifyIndependentOfContext("int a = b * *c;");
5686   verifyIndependentOfContext("int a = b * (10);");
5687   verifyIndependentOfContext("S << b * (10);");
5688   verifyIndependentOfContext("return 10 * b;");
5689   verifyIndependentOfContext("return *b * *c;");
5690   verifyIndependentOfContext("return a & ~b;");
5691   verifyIndependentOfContext("f(b ? *c : *d);");
5692   verifyIndependentOfContext("int a = b ? *c : *d;");
5693   verifyIndependentOfContext("*b = a;");
5694   verifyIndependentOfContext("a * ~b;");
5695   verifyIndependentOfContext("a * !b;");
5696   verifyIndependentOfContext("a * +b;");
5697   verifyIndependentOfContext("a * -b;");
5698   verifyIndependentOfContext("a * ++b;");
5699   verifyIndependentOfContext("a * --b;");
5700   verifyIndependentOfContext("a[4] * b;");
5701   verifyIndependentOfContext("a[a * a] = 1;");
5702   verifyIndependentOfContext("f() * b;");
5703   verifyIndependentOfContext("a * [self dostuff];");
5704   verifyIndependentOfContext("int x = a * (a + b);");
5705   verifyIndependentOfContext("(a *)(a + b);");
5706   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5707   verifyIndependentOfContext("int *pa = (int *)&a;");
5708   verifyIndependentOfContext("return sizeof(int **);");
5709   verifyIndependentOfContext("return sizeof(int ******);");
5710   verifyIndependentOfContext("return (int **&)a;");
5711   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5712   verifyFormat("void f(Type (*parameter)[10]) {}");
5713   verifyFormat("void f(Type (&parameter)[10]) {}");
5714   verifyGoogleFormat("return sizeof(int**);");
5715   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5716   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5717   verifyFormat("auto a = [](int **&, int ***) {};");
5718   verifyFormat("auto PointerBinding = [](const char *S) {};");
5719   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5720   verifyFormat("[](const decltype(*a) &value) {}");
5721   verifyFormat("decltype(a * b) F();");
5722   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5723   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5724   verifyIndependentOfContext("typedef void (*f)(int *a);");
5725   verifyIndependentOfContext("int i{a * b};");
5726   verifyIndependentOfContext("aaa && aaa->f();");
5727   verifyIndependentOfContext("int x = ~*p;");
5728   verifyFormat("Constructor() : a(a), area(width * height) {}");
5729   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5730   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5731   verifyFormat("void f() { f(a, c * d); }");
5732   verifyFormat("void f() { f(new a(), c * d); }");
5733 
5734   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5735 
5736   verifyIndependentOfContext("A<int *> a;");
5737   verifyIndependentOfContext("A<int **> a;");
5738   verifyIndependentOfContext("A<int *, int *> a;");
5739   verifyIndependentOfContext("A<int *[]> a;");
5740   verifyIndependentOfContext(
5741       "const char *const p = reinterpret_cast<const char *const>(q);");
5742   verifyIndependentOfContext("A<int **, int **> a;");
5743   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5744   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5745   verifyFormat("for (; a && b;) {\n}");
5746   verifyFormat("bool foo = true && [] { return false; }();");
5747 
5748   verifyFormat(
5749       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5750       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5751 
5752   verifyGoogleFormat("**outparam = 1;");
5753   verifyGoogleFormat("*outparam = a * b;");
5754   verifyGoogleFormat("int main(int argc, char** argv) {}");
5755   verifyGoogleFormat("A<int*> a;");
5756   verifyGoogleFormat("A<int**> a;");
5757   verifyGoogleFormat("A<int*, int*> a;");
5758   verifyGoogleFormat("A<int**, int**> a;");
5759   verifyGoogleFormat("f(b ? *c : *d);");
5760   verifyGoogleFormat("int a = b ? *c : *d;");
5761   verifyGoogleFormat("Type* t = **x;");
5762   verifyGoogleFormat("Type* t = *++*x;");
5763   verifyGoogleFormat("*++*x;");
5764   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5765   verifyGoogleFormat("Type* t = x++ * y;");
5766   verifyGoogleFormat(
5767       "const char* const p = reinterpret_cast<const char* const>(q);");
5768   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5769   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5770   verifyGoogleFormat("template <typename T>\n"
5771                      "void f(int i = 0, SomeType** temps = NULL);");
5772 
5773   FormatStyle Left = getLLVMStyle();
5774   Left.PointerAlignment = FormatStyle::PAS_Left;
5775   verifyFormat("x = *a(x) = *a(y);", Left);
5776   verifyFormat("for (;; * = b) {\n}", Left);
5777   verifyFormat("return *this += 1;", Left);
5778 
5779   verifyIndependentOfContext("a = *(x + y);");
5780   verifyIndependentOfContext("a = &(x + y);");
5781   verifyIndependentOfContext("*(x + y).call();");
5782   verifyIndependentOfContext("&(x + y)->call();");
5783   verifyFormat("void f() { &(*I).first; }");
5784 
5785   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5786   verifyFormat(
5787       "int *MyValues = {\n"
5788       "    *A, // Operator detection might be confused by the '{'\n"
5789       "    *BB // Operator detection might be confused by previous comment\n"
5790       "};");
5791 
5792   verifyIndependentOfContext("if (int *a = &b)");
5793   verifyIndependentOfContext("if (int &a = *b)");
5794   verifyIndependentOfContext("if (a & b[i])");
5795   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5796   verifyIndependentOfContext("if (*b[i])");
5797   verifyIndependentOfContext("if (int *a = (&b))");
5798   verifyIndependentOfContext("while (int *a = &b)");
5799   verifyIndependentOfContext("size = sizeof *a;");
5800   verifyIndependentOfContext("if (a && (b = c))");
5801   verifyFormat("void f() {\n"
5802                "  for (const int &v : Values) {\n"
5803                "  }\n"
5804                "}");
5805   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5806   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5807   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5808 
5809   verifyFormat("#define A (!a * b)");
5810   verifyFormat("#define MACRO     \\\n"
5811                "  int *i = a * b; \\\n"
5812                "  void f(a *b);",
5813                getLLVMStyleWithColumns(19));
5814 
5815   verifyIndependentOfContext("A = new SomeType *[Length];");
5816   verifyIndependentOfContext("A = new SomeType *[Length]();");
5817   verifyIndependentOfContext("T **t = new T *;");
5818   verifyIndependentOfContext("T **t = new T *();");
5819   verifyGoogleFormat("A = new SomeType*[Length]();");
5820   verifyGoogleFormat("A = new SomeType*[Length];");
5821   verifyGoogleFormat("T** t = new T*;");
5822   verifyGoogleFormat("T** t = new T*();");
5823 
5824   FormatStyle PointerLeft = getLLVMStyle();
5825   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5826   verifyFormat("delete *x;", PointerLeft);
5827   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5828   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5829   verifyFormat("template <bool a, bool b> "
5830                "typename t::if<x && y>::type f() {}");
5831   verifyFormat("template <int *y> f() {}");
5832   verifyFormat("vector<int *> v;");
5833   verifyFormat("vector<int *const> v;");
5834   verifyFormat("vector<int *const **const *> v;");
5835   verifyFormat("vector<int *volatile> v;");
5836   verifyFormat("vector<a * b> v;");
5837   verifyFormat("foo<b && false>();");
5838   verifyFormat("foo<b & 1>();");
5839   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5840   verifyFormat(
5841       "template <class T, class = typename std::enable_if<\n"
5842       "                       std::is_integral<T>::value &&\n"
5843       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5844       "void F();",
5845       getLLVMStyleWithColumns(76));
5846   verifyFormat(
5847       "template <class T,\n"
5848       "          class = typename ::std::enable_if<\n"
5849       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5850       "void F();",
5851       getGoogleStyleWithColumns(68));
5852 
5853   verifyIndependentOfContext("MACRO(int *i);");
5854   verifyIndependentOfContext("MACRO(auto *a);");
5855   verifyIndependentOfContext("MACRO(const A *a);");
5856   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5857   // FIXME: Is there a way to make this work?
5858   // verifyIndependentOfContext("MACRO(A *a);");
5859 
5860   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5861   verifyFormat("return options != nullptr && operator==(*options);");
5862 
5863   EXPECT_EQ("#define OP(x)                                    \\\n"
5864             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5865             "    return s << a.DebugString();                 \\\n"
5866             "  }",
5867             format("#define OP(x) \\\n"
5868                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5869                    "    return s << a.DebugString(); \\\n"
5870                    "  }",
5871                    getLLVMStyleWithColumns(50)));
5872 
5873   // FIXME: We cannot handle this case yet; we might be able to figure out that
5874   // foo<x> d > v; doesn't make sense.
5875   verifyFormat("foo<a<b && c> d> v;");
5876 
5877   FormatStyle PointerMiddle = getLLVMStyle();
5878   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5879   verifyFormat("delete *x;", PointerMiddle);
5880   verifyFormat("int * x;", PointerMiddle);
5881   verifyFormat("template <int * y> f() {}", PointerMiddle);
5882   verifyFormat("int * f(int * a) {}", PointerMiddle);
5883   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5884   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5885   verifyFormat("A<int *> a;", PointerMiddle);
5886   verifyFormat("A<int **> a;", PointerMiddle);
5887   verifyFormat("A<int *, int *> a;", PointerMiddle);
5888   verifyFormat("A<int * []> a;", PointerMiddle);
5889   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5890   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5891   verifyFormat("T ** t = new T *;", PointerMiddle);
5892 
5893   // Member function reference qualifiers aren't binary operators.
5894   verifyFormat("string // break\n"
5895                "operator()() & {}");
5896   verifyFormat("string // break\n"
5897                "operator()() && {}");
5898   verifyGoogleFormat("template <typename T>\n"
5899                      "auto x() & -> int {}");
5900 }
5901 
5902 TEST_F(FormatTest, UnderstandsAttributes) {
5903   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5904   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5905                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5906   FormatStyle AfterType = getLLVMStyle();
5907   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5908   verifyFormat("__attribute__((nodebug)) void\n"
5909                "foo() {}\n",
5910                AfterType);
5911 }
5912 
5913 TEST_F(FormatTest, UnderstandsEllipsis) {
5914   verifyFormat("int printf(const char *fmt, ...);");
5915   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5916   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5917 
5918   FormatStyle PointersLeft = getLLVMStyle();
5919   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5920   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5921 }
5922 
5923 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5924   EXPECT_EQ("int *a;\n"
5925             "int *a;\n"
5926             "int *a;",
5927             format("int *a;\n"
5928                    "int* a;\n"
5929                    "int *a;",
5930                    getGoogleStyle()));
5931   EXPECT_EQ("int* a;\n"
5932             "int* a;\n"
5933             "int* a;",
5934             format("int* a;\n"
5935                    "int* a;\n"
5936                    "int *a;",
5937                    getGoogleStyle()));
5938   EXPECT_EQ("int *a;\n"
5939             "int *a;\n"
5940             "int *a;",
5941             format("int *a;\n"
5942                    "int * a;\n"
5943                    "int *  a;",
5944                    getGoogleStyle()));
5945   EXPECT_EQ("auto x = [] {\n"
5946             "  int *a;\n"
5947             "  int *a;\n"
5948             "  int *a;\n"
5949             "};",
5950             format("auto x=[]{int *a;\n"
5951                    "int * a;\n"
5952                    "int *  a;};",
5953                    getGoogleStyle()));
5954 }
5955 
5956 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5957   verifyFormat("int f(int &&a) {}");
5958   verifyFormat("int f(int a, char &&b) {}");
5959   verifyFormat("void f() { int &&a = b; }");
5960   verifyGoogleFormat("int f(int a, char&& b) {}");
5961   verifyGoogleFormat("void f() { int&& a = b; }");
5962 
5963   verifyIndependentOfContext("A<int &&> a;");
5964   verifyIndependentOfContext("A<int &&, int &&> a;");
5965   verifyGoogleFormat("A<int&&> a;");
5966   verifyGoogleFormat("A<int&&, int&&> a;");
5967 
5968   // Not rvalue references:
5969   verifyFormat("template <bool B, bool C> class A {\n"
5970                "  static_assert(B && C, \"Something is wrong\");\n"
5971                "};");
5972   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5973   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5974   verifyFormat("#define A(a, b) (a && b)");
5975 }
5976 
5977 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5978   verifyFormat("void f() {\n"
5979                "  x[aaaaaaaaa -\n"
5980                "    b] = 23;\n"
5981                "}",
5982                getLLVMStyleWithColumns(15));
5983 }
5984 
5985 TEST_F(FormatTest, FormatsCasts) {
5986   verifyFormat("Type *A = static_cast<Type *>(P);");
5987   verifyFormat("Type *A = (Type *)P;");
5988   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5989   verifyFormat("int a = (int)(2.0f);");
5990   verifyFormat("int a = (int)2.0f;");
5991   verifyFormat("x[(int32)y];");
5992   verifyFormat("x = (int32)y;");
5993   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5994   verifyFormat("int a = (int)*b;");
5995   verifyFormat("int a = (int)2.0f;");
5996   verifyFormat("int a = (int)~0;");
5997   verifyFormat("int a = (int)++a;");
5998   verifyFormat("int a = (int)sizeof(int);");
5999   verifyFormat("int a = (int)+2;");
6000   verifyFormat("my_int a = (my_int)2.0f;");
6001   verifyFormat("my_int a = (my_int)sizeof(int);");
6002   verifyFormat("return (my_int)aaa;");
6003   verifyFormat("#define x ((int)-1)");
6004   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6005   verifyFormat("#define p(q) ((int *)&q)");
6006   verifyFormat("fn(a)(b) + 1;");
6007 
6008   verifyFormat("void f() { my_int a = (my_int)*b; }");
6009   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6010   verifyFormat("my_int a = (my_int)~0;");
6011   verifyFormat("my_int a = (my_int)++a;");
6012   verifyFormat("my_int a = (my_int)-2;");
6013   verifyFormat("my_int a = (my_int)1;");
6014   verifyFormat("my_int a = (my_int *)1;");
6015   verifyFormat("my_int a = (const my_int)-1;");
6016   verifyFormat("my_int a = (const my_int *)-1;");
6017   verifyFormat("my_int a = (my_int)(my_int)-1;");
6018   verifyFormat("my_int a = (ns::my_int)-2;");
6019   verifyFormat("case (my_int)ONE:");
6020   verifyFormat("auto x = (X)this;");
6021 
6022   // FIXME: single value wrapped with paren will be treated as cast.
6023   verifyFormat("void f(int i = (kValue)*kMask) {}");
6024 
6025   verifyFormat("{ (void)F; }");
6026 
6027   // Don't break after a cast's
6028   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6029                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6030                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6031 
6032   // These are not casts.
6033   verifyFormat("void f(int *) {}");
6034   verifyFormat("f(foo)->b;");
6035   verifyFormat("f(foo).b;");
6036   verifyFormat("f(foo)(b);");
6037   verifyFormat("f(foo)[b];");
6038   verifyFormat("[](foo) { return 4; }(bar);");
6039   verifyFormat("(*funptr)(foo)[4];");
6040   verifyFormat("funptrs[4](foo)[4];");
6041   verifyFormat("void f(int *);");
6042   verifyFormat("void f(int *) = 0;");
6043   verifyFormat("void f(SmallVector<int>) {}");
6044   verifyFormat("void f(SmallVector<int>);");
6045   verifyFormat("void f(SmallVector<int>) = 0;");
6046   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6047   verifyFormat("int a = sizeof(int) * b;");
6048   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6049   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6050   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6051   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6052 
6053   // These are not casts, but at some point were confused with casts.
6054   verifyFormat("virtual void foo(int *) override;");
6055   verifyFormat("virtual void foo(char &) const;");
6056   verifyFormat("virtual void foo(int *a, char *) const;");
6057   verifyFormat("int a = sizeof(int *) + b;");
6058   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6059   verifyFormat("bool b = f(g<int>) && c;");
6060   verifyFormat("typedef void (*f)(int i) func;");
6061 
6062   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6063                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6064   // FIXME: The indentation here is not ideal.
6065   verifyFormat(
6066       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6067       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6068       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6069 }
6070 
6071 TEST_F(FormatTest, FormatsFunctionTypes) {
6072   verifyFormat("A<bool()> a;");
6073   verifyFormat("A<SomeType()> a;");
6074   verifyFormat("A<void (*)(int, std::string)> a;");
6075   verifyFormat("A<void *(int)>;");
6076   verifyFormat("void *(*a)(int *, SomeType *);");
6077   verifyFormat("int (*func)(void *);");
6078   verifyFormat("void f() { int (*func)(void *); }");
6079   verifyFormat("template <class CallbackClass>\n"
6080                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6081 
6082   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6083   verifyGoogleFormat("void* (*a)(int);");
6084   verifyGoogleFormat(
6085       "template <class CallbackClass>\n"
6086       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6087 
6088   // Other constructs can look somewhat like function types:
6089   verifyFormat("A<sizeof(*x)> a;");
6090   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6091   verifyFormat("some_var = function(*some_pointer_var)[0];");
6092   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6093   verifyFormat("int x = f(&h)();");
6094 }
6095 
6096 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6097   verifyFormat("A (*foo_)[6];");
6098   verifyFormat("vector<int> (*foo_)[6];");
6099 }
6100 
6101 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6102   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6103                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6104   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6105                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6106   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6107                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6108 
6109   // Different ways of ()-initializiation.
6110   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6111                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6112   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6113                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6114   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6115                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6116   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6117                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6118 }
6119 
6120 TEST_F(FormatTest, BreaksLongDeclarations) {
6121   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6122                "    AnotherNameForTheLongType;");
6123   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6124                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6125   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6126                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6127   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6128                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6129   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6130                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6131   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6132                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6133   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6134                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6135   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6136                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6137   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6138                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6139   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6140                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6141   FormatStyle Indented = getLLVMStyle();
6142   Indented.IndentWrappedFunctionNames = true;
6143   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6144                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6145                Indented);
6146   verifyFormat(
6147       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6148       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6149       Indented);
6150   verifyFormat(
6151       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6152       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6153       Indented);
6154   verifyFormat(
6155       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6156       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6157       Indented);
6158 
6159   // FIXME: Without the comment, this breaks after "(".
6160   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6161                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6162                getGoogleStyle());
6163 
6164   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6165                "                  int LoooooooooooooooooooongParam2) {}");
6166   verifyFormat(
6167       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6168       "                                   SourceLocation L, IdentifierIn *II,\n"
6169       "                                   Type *T) {}");
6170   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6171                "ReallyReaaallyLongFunctionName(\n"
6172                "    const std::string &SomeParameter,\n"
6173                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6174                "        &ReallyReallyLongParameterName,\n"
6175                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6176                "        &AnotherLongParameterName) {}");
6177   verifyFormat("template <typename A>\n"
6178                "SomeLoooooooooooooooooooooongType<\n"
6179                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6180                "Function() {}");
6181 
6182   verifyGoogleFormat(
6183       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6184       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6185   verifyGoogleFormat(
6186       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6187       "                                   SourceLocation L) {}");
6188   verifyGoogleFormat(
6189       "some_namespace::LongReturnType\n"
6190       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6191       "    int first_long_parameter, int second_parameter) {}");
6192 
6193   verifyGoogleFormat("template <typename T>\n"
6194                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6195                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6196   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6197                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6198 
6199   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6200                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6201                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6202   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6203                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6204                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6205   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6206                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6207                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6208                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6209 }
6210 
6211 TEST_F(FormatTest, FormatsArrays) {
6212   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6213                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6214   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6215                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6216   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6217                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6218   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6219                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6220   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6221                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6222   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6223                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6224                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6225   verifyFormat(
6226       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6227       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6228       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6229   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6230                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6231 
6232   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6233                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6234   verifyFormat(
6235       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6236       "                                  .aaaaaaa[0]\n"
6237       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6238   verifyFormat("a[::b::c];");
6239 
6240   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6241 
6242   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6243   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6244 }
6245 
6246 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6247   verifyFormat("(a)->b();");
6248   verifyFormat("--a;");
6249 }
6250 
6251 TEST_F(FormatTest, HandlesIncludeDirectives) {
6252   verifyFormat("#include <string>\n"
6253                "#include <a/b/c.h>\n"
6254                "#include \"a/b/string\"\n"
6255                "#include \"string.h\"\n"
6256                "#include \"string.h\"\n"
6257                "#include <a-a>\n"
6258                "#include < path with space >\n"
6259                "#include_next <test.h>"
6260                "#include \"abc.h\" // this is included for ABC\n"
6261                "#include \"some long include\" // with a comment\n"
6262                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6263                getLLVMStyleWithColumns(35));
6264   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6265   EXPECT_EQ("#include <a>", format("#include<a>"));
6266 
6267   verifyFormat("#import <string>");
6268   verifyFormat("#import <a/b/c.h>");
6269   verifyFormat("#import \"a/b/string\"");
6270   verifyFormat("#import \"string.h\"");
6271   verifyFormat("#import \"string.h\"");
6272   verifyFormat("#if __has_include(<strstream>)\n"
6273                "#include <strstream>\n"
6274                "#endif");
6275 
6276   verifyFormat("#define MY_IMPORT <a/b>");
6277 
6278   // Protocol buffer definition or missing "#".
6279   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6280                getLLVMStyleWithColumns(30));
6281 
6282   FormatStyle Style = getLLVMStyle();
6283   Style.AlwaysBreakBeforeMultilineStrings = true;
6284   Style.ColumnLimit = 0;
6285   verifyFormat("#import \"abc.h\"", Style);
6286 
6287   // But 'import' might also be a regular C++ namespace.
6288   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6289                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6290 }
6291 
6292 //===----------------------------------------------------------------------===//
6293 // Error recovery tests.
6294 //===----------------------------------------------------------------------===//
6295 
6296 TEST_F(FormatTest, IncompleteParameterLists) {
6297   FormatStyle NoBinPacking = getLLVMStyle();
6298   NoBinPacking.BinPackParameters = false;
6299   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6300                "                        double *min_x,\n"
6301                "                        double *max_x,\n"
6302                "                        double *min_y,\n"
6303                "                        double *max_y,\n"
6304                "                        double *min_z,\n"
6305                "                        double *max_z, ) {}",
6306                NoBinPacking);
6307 }
6308 
6309 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6310   verifyFormat("void f() { return; }\n42");
6311   verifyFormat("void f() {\n"
6312                "  if (0)\n"
6313                "    return;\n"
6314                "}\n"
6315                "42");
6316   verifyFormat("void f() { return }\n42");
6317   verifyFormat("void f() {\n"
6318                "  if (0)\n"
6319                "    return\n"
6320                "}\n"
6321                "42");
6322 }
6323 
6324 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6325   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6326   EXPECT_EQ("void f() {\n"
6327             "  if (a)\n"
6328             "    return\n"
6329             "}",
6330             format("void  f  (  )  {  if  ( a )  return  }"));
6331   EXPECT_EQ("namespace N {\n"
6332             "void f()\n"
6333             "}",
6334             format("namespace  N  {  void f()  }"));
6335   EXPECT_EQ("namespace N {\n"
6336             "void f() {}\n"
6337             "void g()\n"
6338             "}",
6339             format("namespace N  { void f( ) { } void g( ) }"));
6340 }
6341 
6342 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6343   verifyFormat("int aaaaaaaa =\n"
6344                "    // Overlylongcomment\n"
6345                "    b;",
6346                getLLVMStyleWithColumns(20));
6347   verifyFormat("function(\n"
6348                "    ShortArgument,\n"
6349                "    LoooooooooooongArgument);\n",
6350                getLLVMStyleWithColumns(20));
6351 }
6352 
6353 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6354   verifyFormat("public:");
6355   verifyFormat("class A {\n"
6356                "public\n"
6357                "  void f() {}\n"
6358                "};");
6359   verifyFormat("public\n"
6360                "int qwerty;");
6361   verifyFormat("public\n"
6362                "B {}");
6363   verifyFormat("public\n"
6364                "{}");
6365   verifyFormat("public\n"
6366                "B { int x; }");
6367 }
6368 
6369 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6370   verifyFormat("{");
6371   verifyFormat("#})");
6372   verifyNoCrash("(/**/[:!] ?[).");
6373 }
6374 
6375 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6376   verifyFormat("do {\n}");
6377   verifyFormat("do {\n}\n"
6378                "f();");
6379   verifyFormat("do {\n}\n"
6380                "wheeee(fun);");
6381   verifyFormat("do {\n"
6382                "  f();\n"
6383                "}");
6384 }
6385 
6386 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6387   verifyFormat("if {\n  foo;\n  foo();\n}");
6388   verifyFormat("switch {\n  foo;\n  foo();\n}");
6389   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6390   verifyFormat("while {\n  foo;\n  foo();\n}");
6391   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6392 }
6393 
6394 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6395   verifyIncompleteFormat("namespace {\n"
6396                          "class Foo { Foo (\n"
6397                          "};\n"
6398                          "} // comment");
6399 }
6400 
6401 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6402   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6403   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6404   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6405   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6406 
6407   EXPECT_EQ("{\n"
6408             "  {\n"
6409             "    breakme(\n"
6410             "        qwe);\n"
6411             "  }\n",
6412             format("{\n"
6413                    "    {\n"
6414                    " breakme(qwe);\n"
6415                    "}\n",
6416                    getLLVMStyleWithColumns(10)));
6417 }
6418 
6419 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6420   verifyFormat("int x = {\n"
6421                "    avariable,\n"
6422                "    b(alongervariable)};",
6423                getLLVMStyleWithColumns(25));
6424 }
6425 
6426 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6427   verifyFormat("return (a)(b){1, 2, 3};");
6428 }
6429 
6430 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6431   verifyFormat("vector<int> x{1, 2, 3, 4};");
6432   verifyFormat("vector<int> x{\n"
6433                "    1, 2, 3, 4,\n"
6434                "};");
6435   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6436   verifyFormat("f({1, 2});");
6437   verifyFormat("auto v = Foo{-1};");
6438   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6439   verifyFormat("Class::Class : member{1, 2, 3} {}");
6440   verifyFormat("new vector<int>{1, 2, 3};");
6441   verifyFormat("new int[3]{1, 2, 3};");
6442   verifyFormat("new int{1};");
6443   verifyFormat("return {arg1, arg2};");
6444   verifyFormat("return {arg1, SomeType{parameter}};");
6445   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6446   verifyFormat("new T{arg1, arg2};");
6447   verifyFormat("f(MyMap[{composite, key}]);");
6448   verifyFormat("class Class {\n"
6449                "  T member = {arg1, arg2};\n"
6450                "};");
6451   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6452   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6453   verifyFormat("int a = std::is_integral<int>{} + 0;");
6454 
6455   verifyFormat("int foo(int i) { return fo1{}(i); }");
6456   verifyFormat("int foo(int i) { return fo1{}(i); }");
6457   verifyFormat("auto i = decltype(x){};");
6458   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6459   verifyFormat("Node n{1, Node{1000}, //\n"
6460                "       2};");
6461   verifyFormat("Aaaa aaaaaaa{\n"
6462                "    {\n"
6463                "        aaaa,\n"
6464                "    },\n"
6465                "};");
6466   verifyFormat("class C : public D {\n"
6467                "  SomeClass SC{2};\n"
6468                "};");
6469   verifyFormat("class C : public A {\n"
6470                "  class D : public B {\n"
6471                "    void f() { int i{2}; }\n"
6472                "  };\n"
6473                "};");
6474   verifyFormat("#define A {a, a},");
6475 
6476   // In combination with BinPackArguments = false.
6477   FormatStyle NoBinPacking = getLLVMStyle();
6478   NoBinPacking.BinPackArguments = false;
6479   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6480                "                      bbbbb,\n"
6481                "                      ccccc,\n"
6482                "                      ddddd,\n"
6483                "                      eeeee,\n"
6484                "                      ffffff,\n"
6485                "                      ggggg,\n"
6486                "                      hhhhhh,\n"
6487                "                      iiiiii,\n"
6488                "                      jjjjjj,\n"
6489                "                      kkkkkk};",
6490                NoBinPacking);
6491   verifyFormat("const Aaaaaa aaaaa = {\n"
6492                "    aaaaa,\n"
6493                "    bbbbb,\n"
6494                "    ccccc,\n"
6495                "    ddddd,\n"
6496                "    eeeee,\n"
6497                "    ffffff,\n"
6498                "    ggggg,\n"
6499                "    hhhhhh,\n"
6500                "    iiiiii,\n"
6501                "    jjjjjj,\n"
6502                "    kkkkkk,\n"
6503                "};",
6504                NoBinPacking);
6505   verifyFormat(
6506       "const Aaaaaa aaaaa = {\n"
6507       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6508       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6509       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6510       "};",
6511       NoBinPacking);
6512 
6513   // FIXME: The alignment of these trailing comments might be bad. Then again,
6514   // this might be utterly useless in real code.
6515   verifyFormat("Constructor::Constructor()\n"
6516                "    : some_value{         //\n"
6517                "                 aaaaaaa, //\n"
6518                "                 bbbbbbb} {}");
6519 
6520   // In braced lists, the first comment is always assumed to belong to the
6521   // first element. Thus, it can be moved to the next or previous line as
6522   // appropriate.
6523   EXPECT_EQ("function({// First element:\n"
6524             "          1,\n"
6525             "          // Second element:\n"
6526             "          2});",
6527             format("function({\n"
6528                    "    // First element:\n"
6529                    "    1,\n"
6530                    "    // Second element:\n"
6531                    "    2});"));
6532   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6533             "    // First element:\n"
6534             "    1,\n"
6535             "    // Second element:\n"
6536             "    2};",
6537             format("std::vector<int> MyNumbers{// First element:\n"
6538                    "                           1,\n"
6539                    "                           // Second element:\n"
6540                    "                           2};",
6541                    getLLVMStyleWithColumns(30)));
6542   // A trailing comma should still lead to an enforced line break.
6543   EXPECT_EQ("vector<int> SomeVector = {\n"
6544             "    // aaa\n"
6545             "    1, 2,\n"
6546             "};",
6547             format("vector<int> SomeVector = { // aaa\n"
6548                    "    1, 2, };"));
6549 
6550   FormatStyle ExtraSpaces = getLLVMStyle();
6551   ExtraSpaces.Cpp11BracedListStyle = false;
6552   ExtraSpaces.ColumnLimit = 75;
6553   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6554   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6555   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6556   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6557   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6558   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6559   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6560   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6561   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6562   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6563   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6564   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6565   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6566   verifyFormat("class Class {\n"
6567                "  T member = { arg1, arg2 };\n"
6568                "};",
6569                ExtraSpaces);
6570   verifyFormat(
6571       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6572       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6573       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6574       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6575       ExtraSpaces);
6576   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6577   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6578                ExtraSpaces);
6579   verifyFormat(
6580       "someFunction(OtherParam,\n"
6581       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6582       "                         param1, param2,\n"
6583       "                         // comment 2\n"
6584       "                         param3, param4 });",
6585       ExtraSpaces);
6586   verifyFormat(
6587       "std::this_thread::sleep_for(\n"
6588       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6589       ExtraSpaces);
6590   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6591                "    aaaaaaa,\n"
6592                "    aaaaaaaaaa,\n"
6593                "    aaaaa,\n"
6594                "    aaaaaaaaaaaaaaa,\n"
6595                "    aaa,\n"
6596                "    aaaaaaaaaa,\n"
6597                "    a,\n"
6598                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6599                "    aaaaaaaaaaaa,\n"
6600                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6601                "    aaaaaaa,\n"
6602                "    a};");
6603   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6604 }
6605 
6606 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6607   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6608                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6609                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6610                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6611                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6612                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6613   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6614                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6615                "                 1, 22, 333, 4444, 55555, //\n"
6616                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6617                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6618   verifyFormat(
6619       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6620       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6621       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6622       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6623       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6624       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6625       "                 7777777};");
6626   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6627                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6628                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6629   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6630                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6631                "    // Separating comment.\n"
6632                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6633   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6634                "    // Leading comment\n"
6635                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6636                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6637   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6638                "                 1, 1, 1, 1};",
6639                getLLVMStyleWithColumns(39));
6640   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6641                "                 1, 1, 1, 1};",
6642                getLLVMStyleWithColumns(38));
6643   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6644                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6645                getLLVMStyleWithColumns(43));
6646   verifyFormat(
6647       "static unsigned SomeValues[10][3] = {\n"
6648       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6649       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6650   verifyFormat("static auto fields = new vector<string>{\n"
6651                "    \"aaaaaaaaaaaaa\",\n"
6652                "    \"aaaaaaaaaaaaa\",\n"
6653                "    \"aaaaaaaaaaaa\",\n"
6654                "    \"aaaaaaaaaaaaaa\",\n"
6655                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6656                "    \"aaaaaaaaaaaa\",\n"
6657                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6658                "};");
6659   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6660   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6661                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6662                "                 3, cccccccccccccccccccccc};",
6663                getLLVMStyleWithColumns(60));
6664 
6665   // Trailing commas.
6666   verifyFormat("vector<int> x = {\n"
6667                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6668                "};",
6669                getLLVMStyleWithColumns(39));
6670   verifyFormat("vector<int> x = {\n"
6671                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6672                "};",
6673                getLLVMStyleWithColumns(39));
6674   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6675                "                 1, 1, 1, 1,\n"
6676                "                 /**/ /**/};",
6677                getLLVMStyleWithColumns(39));
6678 
6679   // Trailing comment in the first line.
6680   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6681                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6682                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6683                "    11111111,   22222222,   333333333,   44444444};");
6684   // Trailing comment in the last line.
6685   verifyFormat("int aaaaa[] = {\n"
6686                "    1, 2, 3, // comment\n"
6687                "    4, 5, 6  // comment\n"
6688                "};");
6689 
6690   // With nested lists, we should either format one item per line or all nested
6691   // lists one on line.
6692   // FIXME: For some nested lists, we can do better.
6693   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6694                "        {aaaaaaaaaaaaaaaaaaa},\n"
6695                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6696                "        {aaaaaaaaaaaaaaaaa}};",
6697                getLLVMStyleWithColumns(60));
6698   verifyFormat(
6699       "SomeStruct my_struct_array = {\n"
6700       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6701       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6702       "    {aaa, aaa},\n"
6703       "    {aaa, aaa},\n"
6704       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6705       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6706       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6707 
6708   // No column layout should be used here.
6709   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6710                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6711 
6712   verifyNoCrash("a<,");
6713 
6714   // No braced initializer here.
6715   verifyFormat("void f() {\n"
6716                "  struct Dummy {};\n"
6717                "  f(v);\n"
6718                "}");
6719 
6720   // Long lists should be formatted in columns even if they are nested.
6721   verifyFormat(
6722       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6723       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6724       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6725       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6726       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6727       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6728 }
6729 
6730 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6731   FormatStyle DoNotMerge = getLLVMStyle();
6732   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6733 
6734   verifyFormat("void f() { return 42; }");
6735   verifyFormat("void f() {\n"
6736                "  return 42;\n"
6737                "}",
6738                DoNotMerge);
6739   verifyFormat("void f() {\n"
6740                "  // Comment\n"
6741                "}");
6742   verifyFormat("{\n"
6743                "#error {\n"
6744                "  int a;\n"
6745                "}");
6746   verifyFormat("{\n"
6747                "  int a;\n"
6748                "#error {\n"
6749                "}");
6750   verifyFormat("void f() {} // comment");
6751   verifyFormat("void f() { int a; } // comment");
6752   verifyFormat("void f() {\n"
6753                "} // comment",
6754                DoNotMerge);
6755   verifyFormat("void f() {\n"
6756                "  int a;\n"
6757                "} // comment",
6758                DoNotMerge);
6759   verifyFormat("void f() {\n"
6760                "} // comment",
6761                getLLVMStyleWithColumns(15));
6762 
6763   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6764   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6765 
6766   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6767   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6768   verifyFormat("class C {\n"
6769                "  C()\n"
6770                "      : iiiiiiii(nullptr),\n"
6771                "        kkkkkkk(nullptr),\n"
6772                "        mmmmmmm(nullptr),\n"
6773                "        nnnnnnn(nullptr) {}\n"
6774                "};",
6775                getGoogleStyle());
6776 
6777   FormatStyle NoColumnLimit = getLLVMStyle();
6778   NoColumnLimit.ColumnLimit = 0;
6779   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6780   EXPECT_EQ("class C {\n"
6781             "  A() : b(0) {}\n"
6782             "};",
6783             format("class C{A():b(0){}};", NoColumnLimit));
6784   EXPECT_EQ("A()\n"
6785             "    : b(0) {\n"
6786             "}",
6787             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6788 
6789   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6790   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6791       FormatStyle::SFS_None;
6792   EXPECT_EQ("A()\n"
6793             "    : b(0) {\n"
6794             "}",
6795             format("A():b(0){}", DoNotMergeNoColumnLimit));
6796   EXPECT_EQ("A()\n"
6797             "    : b(0) {\n"
6798             "}",
6799             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6800 
6801   verifyFormat("#define A          \\\n"
6802                "  void f() {       \\\n"
6803                "    int i;         \\\n"
6804                "  }",
6805                getLLVMStyleWithColumns(20));
6806   verifyFormat("#define A           \\\n"
6807                "  void f() { int i; }",
6808                getLLVMStyleWithColumns(21));
6809   verifyFormat("#define A            \\\n"
6810                "  void f() {         \\\n"
6811                "    int i;           \\\n"
6812                "  }                  \\\n"
6813                "  int j;",
6814                getLLVMStyleWithColumns(22));
6815   verifyFormat("#define A             \\\n"
6816                "  void f() { int i; } \\\n"
6817                "  int j;",
6818                getLLVMStyleWithColumns(23));
6819 }
6820 
6821 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6822   FormatStyle MergeInlineOnly = getLLVMStyle();
6823   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6824   verifyFormat("class C {\n"
6825                "  int f() { return 42; }\n"
6826                "};",
6827                MergeInlineOnly);
6828   verifyFormat("int f() {\n"
6829                "  return 42;\n"
6830                "}",
6831                MergeInlineOnly);
6832 }
6833 
6834 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6835   // Elaborate type variable declarations.
6836   verifyFormat("struct foo a = {bar};\nint n;");
6837   verifyFormat("class foo a = {bar};\nint n;");
6838   verifyFormat("union foo a = {bar};\nint n;");
6839 
6840   // Elaborate types inside function definitions.
6841   verifyFormat("struct foo f() {}\nint n;");
6842   verifyFormat("class foo f() {}\nint n;");
6843   verifyFormat("union foo f() {}\nint n;");
6844 
6845   // Templates.
6846   verifyFormat("template <class X> void f() {}\nint n;");
6847   verifyFormat("template <struct X> void f() {}\nint n;");
6848   verifyFormat("template <union X> void f() {}\nint n;");
6849 
6850   // Actual definitions...
6851   verifyFormat("struct {\n} n;");
6852   verifyFormat(
6853       "template <template <class T, class Y>, class Z> class X {\n} n;");
6854   verifyFormat("union Z {\n  int n;\n} x;");
6855   verifyFormat("class MACRO Z {\n} n;");
6856   verifyFormat("class MACRO(X) Z {\n} n;");
6857   verifyFormat("class __attribute__(X) Z {\n} n;");
6858   verifyFormat("class __declspec(X) Z {\n} n;");
6859   verifyFormat("class A##B##C {\n} n;");
6860   verifyFormat("class alignas(16) Z {\n} n;");
6861   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6862   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6863 
6864   // Redefinition from nested context:
6865   verifyFormat("class A::B::C {\n} n;");
6866 
6867   // Template definitions.
6868   verifyFormat(
6869       "template <typename F>\n"
6870       "Matcher(const Matcher<F> &Other,\n"
6871       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6872       "                             !is_same<F, T>::value>::type * = 0)\n"
6873       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6874 
6875   // FIXME: This is still incorrectly handled at the formatter side.
6876   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6877   verifyFormat("int i = SomeFunction(a<b, a> b);");
6878 
6879   // FIXME:
6880   // This now gets parsed incorrectly as class definition.
6881   // verifyFormat("class A<int> f() {\n}\nint n;");
6882 
6883   // Elaborate types where incorrectly parsing the structural element would
6884   // break the indent.
6885   verifyFormat("if (true)\n"
6886                "  class X x;\n"
6887                "else\n"
6888                "  f();\n");
6889 
6890   // This is simply incomplete. Formatting is not important, but must not crash.
6891   verifyFormat("class A:");
6892 }
6893 
6894 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6895   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6896             format("#error Leave     all         white!!!!! space* alone!\n"));
6897   EXPECT_EQ(
6898       "#warning Leave     all         white!!!!! space* alone!\n",
6899       format("#warning Leave     all         white!!!!! space* alone!\n"));
6900   EXPECT_EQ("#error 1", format("  #  error   1"));
6901   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6902 }
6903 
6904 TEST_F(FormatTest, FormatHashIfExpressions) {
6905   verifyFormat("#if AAAA && BBBB");
6906   verifyFormat("#if (AAAA && BBBB)");
6907   verifyFormat("#elif (AAAA && BBBB)");
6908   // FIXME: Come up with a better indentation for #elif.
6909   verifyFormat(
6910       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6911       "    defined(BBBBBBBB)\n"
6912       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6913       "    defined(BBBBBBBB)\n"
6914       "#endif",
6915       getLLVMStyleWithColumns(65));
6916 }
6917 
6918 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6919   FormatStyle AllowsMergedIf = getGoogleStyle();
6920   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6921   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6922   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6923   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6924   EXPECT_EQ("if (true) return 42;",
6925             format("if (true)\nreturn 42;", AllowsMergedIf));
6926   FormatStyle ShortMergedIf = AllowsMergedIf;
6927   ShortMergedIf.ColumnLimit = 25;
6928   verifyFormat("#define A \\\n"
6929                "  if (true) return 42;",
6930                ShortMergedIf);
6931   verifyFormat("#define A \\\n"
6932                "  f();    \\\n"
6933                "  if (true)\n"
6934                "#define B",
6935                ShortMergedIf);
6936   verifyFormat("#define A \\\n"
6937                "  f();    \\\n"
6938                "  if (true)\n"
6939                "g();",
6940                ShortMergedIf);
6941   verifyFormat("{\n"
6942                "#ifdef A\n"
6943                "  // Comment\n"
6944                "  if (true) continue;\n"
6945                "#endif\n"
6946                "  // Comment\n"
6947                "  if (true) continue;\n"
6948                "}",
6949                ShortMergedIf);
6950   ShortMergedIf.ColumnLimit = 29;
6951   verifyFormat("#define A                   \\\n"
6952                "  if (aaaaaaaaaa) return 1; \\\n"
6953                "  return 2;",
6954                ShortMergedIf);
6955   ShortMergedIf.ColumnLimit = 28;
6956   verifyFormat("#define A         \\\n"
6957                "  if (aaaaaaaaaa) \\\n"
6958                "    return 1;     \\\n"
6959                "  return 2;",
6960                ShortMergedIf);
6961 }
6962 
6963 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6964   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6965                "  f();\n"
6966                "}");
6967   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6968                "  f();\n"
6969                "} /* another comment */ else /* comment #3 */ {\n"
6970                "  g();\n"
6971                "}");
6972   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6973                "  f();\n"
6974                "}");
6975   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6976                "  f();\n"
6977                "}");
6978   verifyFormat("do /* a comment in a strange place */ {\n"
6979                "  f();\n"
6980                "} /* another comment */ while (0);");
6981 }
6982 
6983 TEST_F(FormatTest, BlockComments) {
6984   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6985             format("/* *//* */  /* */\n/* *//* */  /* */"));
6986   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6987   EXPECT_EQ("#define A /*123*/ \\\n"
6988             "  b\n"
6989             "/* */\n"
6990             "someCall(\n"
6991             "    parameter);",
6992             format("#define A /*123*/ b\n"
6993                    "/* */\n"
6994                    "someCall(parameter);",
6995                    getLLVMStyleWithColumns(15)));
6996 
6997   EXPECT_EQ("#define A\n"
6998             "/* */ someCall(\n"
6999             "    parameter);",
7000             format("#define A\n"
7001                    "/* */someCall(parameter);",
7002                    getLLVMStyleWithColumns(15)));
7003   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
7004   EXPECT_EQ("/*\n"
7005             "*\n"
7006             " * aaaaaa\n"
7007             " * aaaaaa\n"
7008             "*/",
7009             format("/*\n"
7010                    "*\n"
7011                    " * aaaaaa aaaaaa\n"
7012                    "*/",
7013                    getLLVMStyleWithColumns(10)));
7014   EXPECT_EQ("/*\n"
7015             "**\n"
7016             "* aaaaaa\n"
7017             "*aaaaaa\n"
7018             "*/",
7019             format("/*\n"
7020                    "**\n"
7021                    "* aaaaaa aaaaaa\n"
7022                    "*/",
7023                    getLLVMStyleWithColumns(10)));
7024 
7025   FormatStyle NoBinPacking = getLLVMStyle();
7026   NoBinPacking.BinPackParameters = false;
7027   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
7028             "             2, /* comment 2 */\n"
7029             "             3, /* comment 3 */\n"
7030             "             aaaa,\n"
7031             "             bbbb);",
7032             format("someFunction (1,   /* comment 1 */\n"
7033                    "                2, /* comment 2 */  \n"
7034                    "               3,   /* comment 3 */\n"
7035                    "aaaa, bbbb );",
7036                    NoBinPacking));
7037   verifyFormat(
7038       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7039       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7040   EXPECT_EQ(
7041       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
7042       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7043       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
7044       format(
7045           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
7046           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
7047           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
7048   EXPECT_EQ(
7049       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7050       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
7051       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
7052       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7053              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
7054              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
7055 
7056   verifyFormat("void f(int * /* unused */) {}");
7057 
7058   EXPECT_EQ("/*\n"
7059             " **\n"
7060             " */",
7061             format("/*\n"
7062                    " **\n"
7063                    " */"));
7064   EXPECT_EQ("/*\n"
7065             " *q\n"
7066             " */",
7067             format("/*\n"
7068                    " *q\n"
7069                    " */"));
7070   EXPECT_EQ("/*\n"
7071             " * q\n"
7072             " */",
7073             format("/*\n"
7074                    " * q\n"
7075                    " */"));
7076   EXPECT_EQ("/*\n"
7077             " **/",
7078             format("/*\n"
7079                    " **/"));
7080   EXPECT_EQ("/*\n"
7081             " ***/",
7082             format("/*\n"
7083                    " ***/"));
7084 }
7085 
7086 TEST_F(FormatTest, BlockCommentsInMacros) {
7087   EXPECT_EQ("#define A          \\\n"
7088             "  {                \\\n"
7089             "    /* one line */ \\\n"
7090             "    someCall();",
7091             format("#define A {        \\\n"
7092                    "  /* one line */   \\\n"
7093                    "  someCall();",
7094                    getLLVMStyleWithColumns(20)));
7095   EXPECT_EQ("#define A          \\\n"
7096             "  {                \\\n"
7097             "    /* previous */ \\\n"
7098             "    /* one line */ \\\n"
7099             "    someCall();",
7100             format("#define A {        \\\n"
7101                    "  /* previous */   \\\n"
7102                    "  /* one line */   \\\n"
7103                    "  someCall();",
7104                    getLLVMStyleWithColumns(20)));
7105 }
7106 
7107 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7108   EXPECT_EQ("a = {\n"
7109             "    1111 /*    */\n"
7110             "};",
7111             format("a = {1111 /*    */\n"
7112                    "};",
7113                    getLLVMStyleWithColumns(15)));
7114   EXPECT_EQ("a = {\n"
7115             "    1111 /*      */\n"
7116             "};",
7117             format("a = {1111 /*      */\n"
7118                    "};",
7119                    getLLVMStyleWithColumns(15)));
7120 
7121   // FIXME: The formatting is still wrong here.
7122   EXPECT_EQ("a = {\n"
7123             "    1111 /*      a\n"
7124             "            */\n"
7125             "};",
7126             format("a = {1111 /*      a */\n"
7127                    "};",
7128                    getLLVMStyleWithColumns(15)));
7129 }
7130 
7131 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7132   verifyFormat("{\n"
7133                "  // a\n"
7134                "  // b");
7135 }
7136 
7137 TEST_F(FormatTest, FormatStarDependingOnContext) {
7138   verifyFormat("void f(int *a);");
7139   verifyFormat("void f() { f(fint * b); }");
7140   verifyFormat("class A {\n  void f(int *a);\n};");
7141   verifyFormat("class A {\n  int *a;\n};");
7142   verifyFormat("namespace a {\n"
7143                "namespace b {\n"
7144                "class A {\n"
7145                "  void f() {}\n"
7146                "  int *a;\n"
7147                "};\n"
7148                "}\n"
7149                "}");
7150 }
7151 
7152 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7153   verifyFormat("while");
7154   verifyFormat("operator");
7155 }
7156 
7157 //===----------------------------------------------------------------------===//
7158 // Objective-C tests.
7159 //===----------------------------------------------------------------------===//
7160 
7161 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7162   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7163   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7164             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7165   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7166   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7167   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7168             format("-(NSInteger)Method3:(id)anObject;"));
7169   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7170             format("-(NSInteger)Method4:(id)anObject;"));
7171   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7172             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7173   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7174             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7175   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7176             "forAllCells:(BOOL)flag;",
7177             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7178                    "forAllCells:(BOOL)flag;"));
7179 
7180   // Very long objectiveC method declaration.
7181   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7182                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7183   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7184                "                    inRange:(NSRange)range\n"
7185                "                   outRange:(NSRange)out_range\n"
7186                "                  outRange1:(NSRange)out_range1\n"
7187                "                  outRange2:(NSRange)out_range2\n"
7188                "                  outRange3:(NSRange)out_range3\n"
7189                "                  outRange4:(NSRange)out_range4\n"
7190                "                  outRange5:(NSRange)out_range5\n"
7191                "                  outRange6:(NSRange)out_range6\n"
7192                "                  outRange7:(NSRange)out_range7\n"
7193                "                  outRange8:(NSRange)out_range8\n"
7194                "                  outRange9:(NSRange)out_range9;");
7195 
7196   // When the function name has to be wrapped.
7197   FormatStyle Style = getLLVMStyle();
7198   Style.IndentWrappedFunctionNames = false;
7199   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7200                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7201                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7202                "}",
7203                Style);
7204   Style.IndentWrappedFunctionNames = true;
7205   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7206                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7207                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7208                "}",
7209                Style);
7210 
7211   verifyFormat("- (int)sum:(vector<int>)numbers;");
7212   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7213   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7214   // protocol lists (but not for template classes):
7215   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7216 
7217   verifyFormat("- (int (*)())foo:(int (*)())f;");
7218   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7219 
7220   // If there's no return type (very rare in practice!), LLVM and Google style
7221   // agree.
7222   verifyFormat("- foo;");
7223   verifyFormat("- foo:(int)f;");
7224   verifyGoogleFormat("- foo:(int)foo;");
7225 }
7226 
7227 TEST_F(FormatTest, FormatObjCInterface) {
7228   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
7229                "@public\n"
7230                "  int field1;\n"
7231                "@protected\n"
7232                "  int field2;\n"
7233                "@private\n"
7234                "  int field3;\n"
7235                "@package\n"
7236                "  int field4;\n"
7237                "}\n"
7238                "+ (id)init;\n"
7239                "@end");
7240 
7241   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
7242                      " @public\n"
7243                      "  int field1;\n"
7244                      " @protected\n"
7245                      "  int field2;\n"
7246                      " @private\n"
7247                      "  int field3;\n"
7248                      " @package\n"
7249                      "  int field4;\n"
7250                      "}\n"
7251                      "+ (id)init;\n"
7252                      "@end");
7253 
7254   verifyFormat("@interface /* wait for it */ Foo\n"
7255                "+ (id)init;\n"
7256                "// Look, a comment!\n"
7257                "- (int)answerWith:(int)i;\n"
7258                "@end");
7259 
7260   verifyFormat("@interface Foo\n"
7261                "@end\n"
7262                "@interface Bar\n"
7263                "@end");
7264 
7265   verifyFormat("@interface Foo : Bar\n"
7266                "+ (id)init;\n"
7267                "@end");
7268 
7269   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
7270                "+ (id)init;\n"
7271                "@end");
7272 
7273   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
7274                      "+ (id)init;\n"
7275                      "@end");
7276 
7277   verifyFormat("@interface Foo (HackStuff)\n"
7278                "+ (id)init;\n"
7279                "@end");
7280 
7281   verifyFormat("@interface Foo ()\n"
7282                "+ (id)init;\n"
7283                "@end");
7284 
7285   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7286                "+ (id)init;\n"
7287                "@end");
7288 
7289   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7290                      "+ (id)init;\n"
7291                      "@end");
7292 
7293   verifyFormat("@interface Foo {\n"
7294                "  int _i;\n"
7295                "}\n"
7296                "+ (id)init;\n"
7297                "@end");
7298 
7299   verifyFormat("@interface Foo : Bar {\n"
7300                "  int _i;\n"
7301                "}\n"
7302                "+ (id)init;\n"
7303                "@end");
7304 
7305   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7306                "  int _i;\n"
7307                "}\n"
7308                "+ (id)init;\n"
7309                "@end");
7310 
7311   verifyFormat("@interface Foo (HackStuff) {\n"
7312                "  int _i;\n"
7313                "}\n"
7314                "+ (id)init;\n"
7315                "@end");
7316 
7317   verifyFormat("@interface Foo () {\n"
7318                "  int _i;\n"
7319                "}\n"
7320                "+ (id)init;\n"
7321                "@end");
7322 
7323   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7324                "  int _i;\n"
7325                "}\n"
7326                "+ (id)init;\n"
7327                "@end");
7328 
7329   FormatStyle OnePerLine = getGoogleStyle();
7330   OnePerLine.BinPackParameters = false;
7331   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7332                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7333                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7334                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7335                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7336                "}",
7337                OnePerLine);
7338 }
7339 
7340 TEST_F(FormatTest, FormatObjCImplementation) {
7341   verifyFormat("@implementation Foo : NSObject {\n"
7342                "@public\n"
7343                "  int field1;\n"
7344                "@protected\n"
7345                "  int field2;\n"
7346                "@private\n"
7347                "  int field3;\n"
7348                "@package\n"
7349                "  int field4;\n"
7350                "}\n"
7351                "+ (id)init {\n}\n"
7352                "@end");
7353 
7354   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7355                      " @public\n"
7356                      "  int field1;\n"
7357                      " @protected\n"
7358                      "  int field2;\n"
7359                      " @private\n"
7360                      "  int field3;\n"
7361                      " @package\n"
7362                      "  int field4;\n"
7363                      "}\n"
7364                      "+ (id)init {\n}\n"
7365                      "@end");
7366 
7367   verifyFormat("@implementation Foo\n"
7368                "+ (id)init {\n"
7369                "  if (true)\n"
7370                "    return nil;\n"
7371                "}\n"
7372                "// Look, a comment!\n"
7373                "- (int)answerWith:(int)i {\n"
7374                "  return i;\n"
7375                "}\n"
7376                "+ (int)answerWith:(int)i {\n"
7377                "  return i;\n"
7378                "}\n"
7379                "@end");
7380 
7381   verifyFormat("@implementation Foo\n"
7382                "@end\n"
7383                "@implementation Bar\n"
7384                "@end");
7385 
7386   EXPECT_EQ("@implementation Foo : Bar\n"
7387             "+ (id)init {\n}\n"
7388             "- (void)foo {\n}\n"
7389             "@end",
7390             format("@implementation Foo : Bar\n"
7391                    "+(id)init{}\n"
7392                    "-(void)foo{}\n"
7393                    "@end"));
7394 
7395   verifyFormat("@implementation Foo {\n"
7396                "  int _i;\n"
7397                "}\n"
7398                "+ (id)init {\n}\n"
7399                "@end");
7400 
7401   verifyFormat("@implementation Foo : Bar {\n"
7402                "  int _i;\n"
7403                "}\n"
7404                "+ (id)init {\n}\n"
7405                "@end");
7406 
7407   verifyFormat("@implementation Foo (HackStuff)\n"
7408                "+ (id)init {\n}\n"
7409                "@end");
7410   verifyFormat("@implementation ObjcClass\n"
7411                "- (void)method;\n"
7412                "{}\n"
7413                "@end");
7414 }
7415 
7416 TEST_F(FormatTest, FormatObjCProtocol) {
7417   verifyFormat("@protocol Foo\n"
7418                "@property(weak) id delegate;\n"
7419                "- (NSUInteger)numberOfThings;\n"
7420                "@end");
7421 
7422   verifyFormat("@protocol MyProtocol <NSObject>\n"
7423                "- (NSUInteger)numberOfThings;\n"
7424                "@end");
7425 
7426   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7427                      "- (NSUInteger)numberOfThings;\n"
7428                      "@end");
7429 
7430   verifyFormat("@protocol Foo;\n"
7431                "@protocol Bar;\n");
7432 
7433   verifyFormat("@protocol Foo\n"
7434                "@end\n"
7435                "@protocol Bar\n"
7436                "@end");
7437 
7438   verifyFormat("@protocol myProtocol\n"
7439                "- (void)mandatoryWithInt:(int)i;\n"
7440                "@optional\n"
7441                "- (void)optional;\n"
7442                "@required\n"
7443                "- (void)required;\n"
7444                "@optional\n"
7445                "@property(assign) int madProp;\n"
7446                "@end\n");
7447 
7448   verifyFormat("@property(nonatomic, assign, readonly)\n"
7449                "    int *looooooooooooooooooooooooooooongNumber;\n"
7450                "@property(nonatomic, assign, readonly)\n"
7451                "    NSString *looooooooooooooooooooooooooooongName;");
7452 
7453   verifyFormat("@implementation PR18406\n"
7454                "}\n"
7455                "@end");
7456 }
7457 
7458 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7459   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7460                "                   rect:(NSRect)theRect\n"
7461                "               interval:(float)theInterval {\n"
7462                "}");
7463   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7464                "      longKeyword:(NSRect)theRect\n"
7465                "    longerKeyword:(float)theInterval\n"
7466                "            error:(NSError **)theError {\n"
7467                "}");
7468   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7469                "          longKeyword:(NSRect)theRect\n"
7470                "    evenLongerKeyword:(float)theInterval\n"
7471                "                error:(NSError **)theError {\n"
7472                "}");
7473   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7474                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7475                "    NS_DESIGNATED_INITIALIZER;",
7476                getLLVMStyleWithColumns(60));
7477 
7478   // Continuation indent width should win over aligning colons if the function
7479   // name is long.
7480   FormatStyle continuationStyle = getGoogleStyle();
7481   continuationStyle.ColumnLimit = 40;
7482   continuationStyle.IndentWrappedFunctionNames = true;
7483   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7484                "    dontAlignNamef:(NSRect)theRect {\n"
7485                "}",
7486                continuationStyle);
7487 
7488   // Make sure we don't break aligning for short parameter names.
7489   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7490                "       aShortf:(NSRect)theRect {\n"
7491                "}",
7492                continuationStyle);
7493 }
7494 
7495 TEST_F(FormatTest, FormatObjCMethodExpr) {
7496   verifyFormat("[foo bar:baz];");
7497   verifyFormat("return [foo bar:baz];");
7498   verifyFormat("return (a)[foo bar:baz];");
7499   verifyFormat("f([foo bar:baz]);");
7500   verifyFormat("f(2, [foo bar:baz]);");
7501   verifyFormat("f(2, a ? b : c);");
7502   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7503 
7504   // Unary operators.
7505   verifyFormat("int a = +[foo bar:baz];");
7506   verifyFormat("int a = -[foo bar:baz];");
7507   verifyFormat("int a = ![foo bar:baz];");
7508   verifyFormat("int a = ~[foo bar:baz];");
7509   verifyFormat("int a = ++[foo bar:baz];");
7510   verifyFormat("int a = --[foo bar:baz];");
7511   verifyFormat("int a = sizeof [foo bar:baz];");
7512   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7513   verifyFormat("int a = &[foo bar:baz];");
7514   verifyFormat("int a = *[foo bar:baz];");
7515   // FIXME: Make casts work, without breaking f()[4].
7516   // verifyFormat("int a = (int)[foo bar:baz];");
7517   // verifyFormat("return (int)[foo bar:baz];");
7518   // verifyFormat("(void)[foo bar:baz];");
7519   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7520 
7521   // Binary operators.
7522   verifyFormat("[foo bar:baz], [foo bar:baz];");
7523   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7524   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7525   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7526   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7527   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7528   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7529   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7530   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7531   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7532   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7533   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7534   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7535   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7536   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7537   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7538   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7539   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7540   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7541   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7542   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7543   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7544   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7545   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7546   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7547   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7548   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7549   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7550   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7551   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7552   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7553   // Whew!
7554 
7555   verifyFormat("return in[42];");
7556   verifyFormat("for (auto v : in[1]) {\n}");
7557   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7558   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7559   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7560   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7561   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7562   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7563                "}");
7564   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7565   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7566   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7567 
7568   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7569   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7570   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7571   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7572   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7573   verifyFormat("[button setAction:@selector(zoomOut:)];");
7574   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7575 
7576   verifyFormat("arr[[self indexForFoo:a]];");
7577   verifyFormat("throw [self errorFor:a];");
7578   verifyFormat("@throw [self errorFor:a];");
7579 
7580   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7581   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7582   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7583 
7584   // This tests that the formatter doesn't break after "backing" but before ":",
7585   // which would be at 80 columns.
7586   verifyFormat(
7587       "void f() {\n"
7588       "  if ((self = [super initWithContentRect:contentRect\n"
7589       "                               styleMask:styleMask ?: otherMask\n"
7590       "                                 backing:NSBackingStoreBuffered\n"
7591       "                                   defer:YES]))");
7592 
7593   verifyFormat(
7594       "[foo checkThatBreakingAfterColonWorksOk:\n"
7595       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7596 
7597   verifyFormat("[myObj short:arg1 // Force line break\n"
7598                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7599                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7600                "                error:arg4];");
7601   verifyFormat(
7602       "void f() {\n"
7603       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7604       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7605       "                                     pos.width(), pos.height())\n"
7606       "                styleMask:NSBorderlessWindowMask\n"
7607       "                  backing:NSBackingStoreBuffered\n"
7608       "                    defer:NO]);\n"
7609       "}");
7610   verifyFormat(
7611       "void f() {\n"
7612       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7613       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7614       "                                 pos.width(), pos.height())\n"
7615       "                syeMask:NSBorderlessWindowMask\n"
7616       "                  bking:NSBackingStoreBuffered\n"
7617       "                    der:NO]);\n"
7618       "}",
7619       getLLVMStyleWithColumns(70));
7620   verifyFormat(
7621       "void f() {\n"
7622       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7623       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7624       "                                     pos.width(), pos.height())\n"
7625       "                styleMask:NSBorderlessWindowMask\n"
7626       "                  backing:NSBackingStoreBuffered\n"
7627       "                    defer:NO]);\n"
7628       "}",
7629       getChromiumStyle(FormatStyle::LK_Cpp));
7630   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7631                "                             with:contentsNativeView];");
7632 
7633   verifyFormat(
7634       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7635       "           owner:nillllll];");
7636 
7637   verifyFormat(
7638       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7639       "        forType:kBookmarkButtonDragType];");
7640 
7641   verifyFormat("[defaultCenter addObserver:self\n"
7642                "                  selector:@selector(willEnterFullscreen)\n"
7643                "                      name:kWillEnterFullscreenNotification\n"
7644                "                    object:nil];");
7645   verifyFormat("[image_rep drawInRect:drawRect\n"
7646                "             fromRect:NSZeroRect\n"
7647                "            operation:NSCompositeCopy\n"
7648                "             fraction:1.0\n"
7649                "       respectFlipped:NO\n"
7650                "                hints:nil];");
7651   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7652                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7653   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7654                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7655   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7656                "    aaaaaaaaaaaaaaaaaaaaaa];");
7657   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7658                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7659                getLLVMStyleWithColumns(60));
7660 
7661   verifyFormat(
7662       "scoped_nsobject<NSTextField> message(\n"
7663       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7664       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7665   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7666                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7667                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7668                "          aaaa:bbb];");
7669   verifyFormat("[self param:function( //\n"
7670                "                parameter)]");
7671   verifyFormat(
7672       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7673       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7674       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7675 
7676   // FIXME: This violates the column limit.
7677   verifyFormat(
7678       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7679       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7680       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7681       getLLVMStyleWithColumns(60));
7682 
7683   // Variadic parameters.
7684   verifyFormat(
7685       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7686   verifyFormat(
7687       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7688       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7689       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7690   verifyFormat("[self // break\n"
7691                "      a:a\n"
7692                "    aaa:aaa];");
7693   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7694                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7695 }
7696 
7697 TEST_F(FormatTest, ObjCAt) {
7698   verifyFormat("@autoreleasepool");
7699   verifyFormat("@catch");
7700   verifyFormat("@class");
7701   verifyFormat("@compatibility_alias");
7702   verifyFormat("@defs");
7703   verifyFormat("@dynamic");
7704   verifyFormat("@encode");
7705   verifyFormat("@end");
7706   verifyFormat("@finally");
7707   verifyFormat("@implementation");
7708   verifyFormat("@import");
7709   verifyFormat("@interface");
7710   verifyFormat("@optional");
7711   verifyFormat("@package");
7712   verifyFormat("@private");
7713   verifyFormat("@property");
7714   verifyFormat("@protected");
7715   verifyFormat("@protocol");
7716   verifyFormat("@public");
7717   verifyFormat("@required");
7718   verifyFormat("@selector");
7719   verifyFormat("@synchronized");
7720   verifyFormat("@synthesize");
7721   verifyFormat("@throw");
7722   verifyFormat("@try");
7723 
7724   EXPECT_EQ("@interface", format("@ interface"));
7725 
7726   // The precise formatting of this doesn't matter, nobody writes code like
7727   // this.
7728   verifyFormat("@ /*foo*/ interface");
7729 }
7730 
7731 TEST_F(FormatTest, ObjCSnippets) {
7732   verifyFormat("@autoreleasepool {\n"
7733                "  foo();\n"
7734                "}");
7735   verifyFormat("@class Foo, Bar;");
7736   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7737   verifyFormat("@dynamic textColor;");
7738   verifyFormat("char *buf1 = @encode(int *);");
7739   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7740   verifyFormat("char *buf1 = @encode(int **);");
7741   verifyFormat("Protocol *proto = @protocol(p1);");
7742   verifyFormat("SEL s = @selector(foo:);");
7743   verifyFormat("@synchronized(self) {\n"
7744                "  f();\n"
7745                "}");
7746 
7747   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7748   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7749 
7750   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7751   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7752   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7753   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7754                getMozillaStyle());
7755   verifyFormat("@property BOOL editable;", getMozillaStyle());
7756   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7757                getWebKitStyle());
7758   verifyFormat("@property BOOL editable;", getWebKitStyle());
7759 
7760   verifyFormat("@import foo.bar;\n"
7761                "@import baz;");
7762 }
7763 
7764 TEST_F(FormatTest, ObjCForIn) {
7765   verifyFormat("- (void)test {\n"
7766                "  for (NSString *n in arrayOfStrings) {\n"
7767                "    foo(n);\n"
7768                "  }\n"
7769                "}");
7770   verifyFormat("- (void)test {\n"
7771                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7772                "    foo(n);\n"
7773                "  }\n"
7774                "}");
7775 }
7776 
7777 TEST_F(FormatTest, ObjCLiterals) {
7778   verifyFormat("@\"String\"");
7779   verifyFormat("@1");
7780   verifyFormat("@+4.8");
7781   verifyFormat("@-4");
7782   verifyFormat("@1LL");
7783   verifyFormat("@.5");
7784   verifyFormat("@'c'");
7785   verifyFormat("@true");
7786 
7787   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7788   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7789   verifyFormat("NSNumber *favoriteColor = @(Green);");
7790   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7791 
7792   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7793 }
7794 
7795 TEST_F(FormatTest, ObjCDictLiterals) {
7796   verifyFormat("@{");
7797   verifyFormat("@{}");
7798   verifyFormat("@{@\"one\" : @1}");
7799   verifyFormat("return @{@\"one\" : @1;");
7800   verifyFormat("@{@\"one\" : @1}");
7801 
7802   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7803   verifyFormat("@{\n"
7804                "  @\"one\" : @{@2 : @1},\n"
7805                "}");
7806 
7807   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7808   verifyIncompleteFormat("[self setDict:@{}");
7809   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7810   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7811   verifyFormat(
7812       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7813   verifyFormat(
7814       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7815 
7816   verifyFormat("NSDictionary *d = @{\n"
7817                "  @\"nam\" : NSUserNam(),\n"
7818                "  @\"dte\" : [NSDate date],\n"
7819                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7820                "};");
7821   verifyFormat(
7822       "@{\n"
7823       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7824       "regularFont,\n"
7825       "};");
7826   verifyGoogleFormat(
7827       "@{\n"
7828       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7829       "regularFont,\n"
7830       "};");
7831   verifyFormat(
7832       "@{\n"
7833       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7834       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7835       "};");
7836 
7837   // We should try to be robust in case someone forgets the "@".
7838   verifyFormat("NSDictionary *d = {\n"
7839                "  @\"nam\" : NSUserNam(),\n"
7840                "  @\"dte\" : [NSDate date],\n"
7841                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7842                "};");
7843   verifyFormat("NSMutableDictionary *dictionary =\n"
7844                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7845                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7846                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7847                "      cccccccccccccccc : ccccccccccccccc\n"
7848                "    }];");
7849 
7850   // Ensure that casts before the key are kept on the same line as the key.
7851   verifyFormat(
7852       "NSDictionary *d = @{\n"
7853       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7854       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7855       "};");
7856 }
7857 
7858 TEST_F(FormatTest, ObjCArrayLiterals) {
7859   verifyIncompleteFormat("@[");
7860   verifyFormat("@[]");
7861   verifyFormat(
7862       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7863   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7864   verifyFormat("NSArray *array = @[ [foo description] ];");
7865 
7866   verifyFormat(
7867       "NSArray *some_variable = @[\n"
7868       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7869       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7870       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7871       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7872       "];");
7873   verifyFormat(
7874       "NSArray *some_variable = @[\n"
7875       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7876       "  @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
7877       "];");
7878   verifyFormat("NSArray *some_variable = @[\n"
7879                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7880                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7881                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7882                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7883                "];");
7884   verifyFormat("NSArray *array = @[\n"
7885                "  @\"a\",\n"
7886                "  @\"a\",\n" // Trailing comma -> one per line.
7887                "];");
7888 
7889   // We should try to be robust in case someone forgets the "@".
7890   verifyFormat("NSArray *some_variable = [\n"
7891                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7892                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7893                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7894                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7895                "];");
7896   verifyFormat(
7897       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7898       "                                             index:(NSUInteger)index\n"
7899       "                                nonDigitAttributes:\n"
7900       "                                    (NSDictionary *)noDigitAttributes;");
7901   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7902                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7903                "]];");
7904 }
7905 
7906 TEST_F(FormatTest, BreaksStringLiterals) {
7907   EXPECT_EQ("\"some text \"\n"
7908             "\"other\";",
7909             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7910   EXPECT_EQ("\"some text \"\n"
7911             "\"other\";",
7912             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7913   EXPECT_EQ(
7914       "#define A  \\\n"
7915       "  \"some \"  \\\n"
7916       "  \"text \"  \\\n"
7917       "  \"other\";",
7918       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7919   EXPECT_EQ(
7920       "#define A  \\\n"
7921       "  \"so \"    \\\n"
7922       "  \"text \"  \\\n"
7923       "  \"other\";",
7924       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7925 
7926   EXPECT_EQ("\"some text\"",
7927             format("\"some text\"", getLLVMStyleWithColumns(1)));
7928   EXPECT_EQ("\"some text\"",
7929             format("\"some text\"", getLLVMStyleWithColumns(11)));
7930   EXPECT_EQ("\"some \"\n"
7931             "\"text\"",
7932             format("\"some text\"", getLLVMStyleWithColumns(10)));
7933   EXPECT_EQ("\"some \"\n"
7934             "\"text\"",
7935             format("\"some text\"", getLLVMStyleWithColumns(7)));
7936   EXPECT_EQ("\"some\"\n"
7937             "\" tex\"\n"
7938             "\"t\"",
7939             format("\"some text\"", getLLVMStyleWithColumns(6)));
7940   EXPECT_EQ("\"some\"\n"
7941             "\" tex\"\n"
7942             "\" and\"",
7943             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7944   EXPECT_EQ("\"some\"\n"
7945             "\"/tex\"\n"
7946             "\"/and\"",
7947             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7948 
7949   EXPECT_EQ("variable =\n"
7950             "    \"long string \"\n"
7951             "    \"literal\";",
7952             format("variable = \"long string literal\";",
7953                    getLLVMStyleWithColumns(20)));
7954 
7955   EXPECT_EQ("variable = f(\n"
7956             "    \"long string \"\n"
7957             "    \"literal\",\n"
7958             "    short,\n"
7959             "    loooooooooooooooooooong);",
7960             format("variable = f(\"long string literal\", short, "
7961                    "loooooooooooooooooooong);",
7962                    getLLVMStyleWithColumns(20)));
7963 
7964   EXPECT_EQ(
7965       "f(g(\"long string \"\n"
7966       "    \"literal\"),\n"
7967       "  b);",
7968       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7969   EXPECT_EQ("f(g(\"long string \"\n"
7970             "    \"literal\",\n"
7971             "    a),\n"
7972             "  b);",
7973             format("f(g(\"long string literal\", a), b);",
7974                    getLLVMStyleWithColumns(20)));
7975   EXPECT_EQ(
7976       "f(\"one two\".split(\n"
7977       "    variable));",
7978       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7979   EXPECT_EQ("f(\"one two three four five six \"\n"
7980             "  \"seven\".split(\n"
7981             "      really_looooong_variable));",
7982             format("f(\"one two three four five six seven\"."
7983                    "split(really_looooong_variable));",
7984                    getLLVMStyleWithColumns(33)));
7985 
7986   EXPECT_EQ("f(\"some \"\n"
7987             "  \"text\",\n"
7988             "  other);",
7989             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7990 
7991   // Only break as a last resort.
7992   verifyFormat(
7993       "aaaaaaaaaaaaaaaaaaaa(\n"
7994       "    aaaaaaaaaaaaaaaaaaaa,\n"
7995       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7996 
7997   EXPECT_EQ("\"splitmea\"\n"
7998             "\"trandomp\"\n"
7999             "\"oint\"",
8000             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8001 
8002   EXPECT_EQ("\"split/\"\n"
8003             "\"pathat/\"\n"
8004             "\"slashes\"",
8005             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8006 
8007   EXPECT_EQ("\"split/\"\n"
8008             "\"pathat/\"\n"
8009             "\"slashes\"",
8010             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8011   EXPECT_EQ("\"split at \"\n"
8012             "\"spaces/at/\"\n"
8013             "\"slashes.at.any$\"\n"
8014             "\"non-alphanumeric%\"\n"
8015             "\"1111111111characte\"\n"
8016             "\"rs\"",
8017             format("\"split at "
8018                    "spaces/at/"
8019                    "slashes.at."
8020                    "any$non-"
8021                    "alphanumeric%"
8022                    "1111111111characte"
8023                    "rs\"",
8024                    getLLVMStyleWithColumns(20)));
8025 
8026   // Verify that splitting the strings understands
8027   // Style::AlwaysBreakBeforeMultilineStrings.
8028   EXPECT_EQ(
8029       "aaaaaaaaaaaa(\n"
8030       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8031       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8032       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8033              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8034              "aaaaaaaaaaaaaaaaaaaaaa\");",
8035              getGoogleStyle()));
8036   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8037             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8038             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8039                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8040                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8041                    getGoogleStyle()));
8042   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8043             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8044             format("llvm::outs() << "
8045                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8046                    "aaaaaaaaaaaaaaaaaaa\";"));
8047   EXPECT_EQ("ffff(\n"
8048             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8049             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8050             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8051                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8052                    getGoogleStyle()));
8053 
8054   FormatStyle Style = getLLVMStyleWithColumns(12);
8055   Style.BreakStringLiterals = false;
8056   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8057 
8058   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8059   AlignLeft.AlignEscapedNewlinesLeft = true;
8060   EXPECT_EQ("#define A \\\n"
8061             "  \"some \" \\\n"
8062             "  \"text \" \\\n"
8063             "  \"other\";",
8064             format("#define A \"some text other\";", AlignLeft));
8065 }
8066 
8067 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8068   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8069   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8070   EXPECT_EQ("int i = a(b());",
8071             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8072 }
8073 
8074 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8075   EXPECT_EQ(
8076       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8077       "(\n"
8078       "    \"x\t\");",
8079       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8080              "aaaaaaa("
8081              "\"x\t\");"));
8082 }
8083 
8084 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8085   EXPECT_EQ(
8086       "u8\"utf8 string \"\n"
8087       "u8\"literal\";",
8088       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8089   EXPECT_EQ(
8090       "u\"utf16 string \"\n"
8091       "u\"literal\";",
8092       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8093   EXPECT_EQ(
8094       "U\"utf32 string \"\n"
8095       "U\"literal\";",
8096       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8097   EXPECT_EQ("L\"wide string \"\n"
8098             "L\"literal\";",
8099             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8100   EXPECT_EQ("@\"NSString \"\n"
8101             "@\"literal\";",
8102             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8103 
8104   // This input makes clang-format try to split the incomplete unicode escape
8105   // sequence, which used to lead to a crasher.
8106   verifyNoCrash(
8107       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8108       getLLVMStyleWithColumns(60));
8109 }
8110 
8111 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8112   FormatStyle Style = getGoogleStyleWithColumns(15);
8113   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8114   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8115   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8116   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8117   EXPECT_EQ("u8R\"x(raw literal)x\";",
8118             format("u8R\"x(raw literal)x\";", Style));
8119 }
8120 
8121 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8122   FormatStyle Style = getLLVMStyleWithColumns(20);
8123   EXPECT_EQ(
8124       "_T(\"aaaaaaaaaaaaaa\")\n"
8125       "_T(\"aaaaaaaaaaaaaa\")\n"
8126       "_T(\"aaaaaaaaaaaa\")",
8127       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8128   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
8129             "     _T(\"aaaaaa\"),\n"
8130             "  z);",
8131             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8132 
8133   // FIXME: Handle embedded spaces in one iteration.
8134   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8135   //            "_T(\"aaaaaaaaaaaaa\")\n"
8136   //            "_T(\"aaaaaaaaaaaaa\")\n"
8137   //            "_T(\"a\")",
8138   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8139   //                   getLLVMStyleWithColumns(20)));
8140   EXPECT_EQ(
8141       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8142       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8143   EXPECT_EQ("f(\n"
8144             "#if !TEST\n"
8145             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8146             "#endif\n"
8147             "    );",
8148             format("f(\n"
8149                    "#if !TEST\n"
8150                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8151                    "#endif\n"
8152                    ");"));
8153   EXPECT_EQ("f(\n"
8154             "\n"
8155             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8156             format("f(\n"
8157                    "\n"
8158                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8159 }
8160 
8161 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8162   EXPECT_EQ(
8163       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8164       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8165       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8166       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8167              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8168              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8169 }
8170 
8171 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8172   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8173             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8174   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8175             "multiline raw string literal xxxxxxxxxxxxxx\n"
8176             ")x\",\n"
8177             "              a),\n"
8178             "            b);",
8179             format("fffffffffff(g(R\"x(\n"
8180                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8181                    ")x\", a), b);",
8182                    getGoogleStyleWithColumns(20)));
8183   EXPECT_EQ("fffffffffff(\n"
8184             "    g(R\"x(qqq\n"
8185             "multiline raw string literal xxxxxxxxxxxxxx\n"
8186             ")x\",\n"
8187             "      a),\n"
8188             "    b);",
8189             format("fffffffffff(g(R\"x(qqq\n"
8190                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8191                    ")x\", a), b);",
8192                    getGoogleStyleWithColumns(20)));
8193 
8194   EXPECT_EQ("fffffffffff(R\"x(\n"
8195             "multiline raw string literal xxxxxxxxxxxxxx\n"
8196             ")x\");",
8197             format("fffffffffff(R\"x(\n"
8198                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8199                    ")x\");",
8200                    getGoogleStyleWithColumns(20)));
8201   EXPECT_EQ("fffffffffff(R\"x(\n"
8202             "multiline raw string literal xxxxxxxxxxxxxx\n"
8203             ")x\" + bbbbbb);",
8204             format("fffffffffff(R\"x(\n"
8205                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8206                    ")x\" +   bbbbbb);",
8207                    getGoogleStyleWithColumns(20)));
8208   EXPECT_EQ("fffffffffff(\n"
8209             "    R\"x(\n"
8210             "multiline raw string literal xxxxxxxxxxxxxx\n"
8211             ")x\" +\n"
8212             "    bbbbbb);",
8213             format("fffffffffff(\n"
8214                    " R\"x(\n"
8215                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8216                    ")x\" + bbbbbb);",
8217                    getGoogleStyleWithColumns(20)));
8218 }
8219 
8220 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8221   verifyFormat("string a = \"unterminated;");
8222   EXPECT_EQ("function(\"unterminated,\n"
8223             "         OtherParameter);",
8224             format("function(  \"unterminated,\n"
8225                    "    OtherParameter);"));
8226 }
8227 
8228 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8229   FormatStyle Style = getLLVMStyle();
8230   Style.Standard = FormatStyle::LS_Cpp03;
8231   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8232             format("#define x(_a) printf(\"foo\"_a);", Style));
8233 }
8234 
8235 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8236 
8237 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8238   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8239             "             \"ddeeefff\");",
8240             format("someFunction(\"aaabbbcccdddeeefff\");",
8241                    getLLVMStyleWithColumns(25)));
8242   EXPECT_EQ("someFunction1234567890(\n"
8243             "    \"aaabbbcccdddeeefff\");",
8244             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8245                    getLLVMStyleWithColumns(26)));
8246   EXPECT_EQ("someFunction1234567890(\n"
8247             "    \"aaabbbcccdddeeeff\"\n"
8248             "    \"f\");",
8249             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8250                    getLLVMStyleWithColumns(25)));
8251   EXPECT_EQ("someFunction1234567890(\n"
8252             "    \"aaabbbcccdddeeeff\"\n"
8253             "    \"f\");",
8254             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8255                    getLLVMStyleWithColumns(24)));
8256   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8257             "             \"ddde \"\n"
8258             "             \"efff\");",
8259             format("someFunction(\"aaabbbcc ddde efff\");",
8260                    getLLVMStyleWithColumns(25)));
8261   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8262             "             \"ddeeefff\");",
8263             format("someFunction(\"aaabbbccc ddeeefff\");",
8264                    getLLVMStyleWithColumns(25)));
8265   EXPECT_EQ("someFunction1234567890(\n"
8266             "    \"aaabb \"\n"
8267             "    \"cccdddeeefff\");",
8268             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8269                    getLLVMStyleWithColumns(25)));
8270   EXPECT_EQ("#define A          \\\n"
8271             "  string s =       \\\n"
8272             "      \"123456789\"  \\\n"
8273             "      \"0\";         \\\n"
8274             "  int i;",
8275             format("#define A string s = \"1234567890\"; int i;",
8276                    getLLVMStyleWithColumns(20)));
8277   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8278   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8279             "             \"dddeeeff\"\n"
8280             "             \"f\");",
8281             format("someFunction(\"aaabbbcc dddeeefff\");",
8282                    getLLVMStyleWithColumns(25)));
8283 }
8284 
8285 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8286   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8287   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8288   EXPECT_EQ("\"test\"\n"
8289             "\"\\n\"",
8290             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8291   EXPECT_EQ("\"tes\\\\\"\n"
8292             "\"n\"",
8293             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8294   EXPECT_EQ("\"\\\\\\\\\"\n"
8295             "\"\\n\"",
8296             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8297   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8298   EXPECT_EQ("\"\\uff01\"\n"
8299             "\"test\"",
8300             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8301   EXPECT_EQ("\"\\Uff01ff02\"",
8302             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8303   EXPECT_EQ("\"\\x000000000001\"\n"
8304             "\"next\"",
8305             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8306   EXPECT_EQ("\"\\x000000000001next\"",
8307             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8308   EXPECT_EQ("\"\\x000000000001\"",
8309             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8310   EXPECT_EQ("\"test\"\n"
8311             "\"\\000000\"\n"
8312             "\"000001\"",
8313             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8314   EXPECT_EQ("\"test\\000\"\n"
8315             "\"00000000\"\n"
8316             "\"1\"",
8317             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8318 }
8319 
8320 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8321   verifyFormat("void f() {\n"
8322                "  return g() {}\n"
8323                "  void h() {}");
8324   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8325                "g();\n"
8326                "}");
8327 }
8328 
8329 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8330   verifyFormat(
8331       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8332 }
8333 
8334 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8335   verifyFormat("class X {\n"
8336                "  void f() {\n"
8337                "  }\n"
8338                "};",
8339                getLLVMStyleWithColumns(12));
8340 }
8341 
8342 TEST_F(FormatTest, ConfigurableIndentWidth) {
8343   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8344   EightIndent.IndentWidth = 8;
8345   EightIndent.ContinuationIndentWidth = 8;
8346   verifyFormat("void f() {\n"
8347                "        someFunction();\n"
8348                "        if (true) {\n"
8349                "                f();\n"
8350                "        }\n"
8351                "}",
8352                EightIndent);
8353   verifyFormat("class X {\n"
8354                "        void f() {\n"
8355                "        }\n"
8356                "};",
8357                EightIndent);
8358   verifyFormat("int x[] = {\n"
8359                "        call(),\n"
8360                "        call()};",
8361                EightIndent);
8362 }
8363 
8364 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8365   verifyFormat("double\n"
8366                "f();",
8367                getLLVMStyleWithColumns(8));
8368 }
8369 
8370 TEST_F(FormatTest, ConfigurableUseOfTab) {
8371   FormatStyle Tab = getLLVMStyleWithColumns(42);
8372   Tab.IndentWidth = 8;
8373   Tab.UseTab = FormatStyle::UT_Always;
8374   Tab.AlignEscapedNewlinesLeft = true;
8375 
8376   EXPECT_EQ("if (aaaaaaaa && // q\n"
8377             "    bb)\t\t// w\n"
8378             "\t;",
8379             format("if (aaaaaaaa &&// q\n"
8380                    "bb)// w\n"
8381                    ";",
8382                    Tab));
8383   EXPECT_EQ("if (aaa && bbb) // w\n"
8384             "\t;",
8385             format("if(aaa&&bbb)// w\n"
8386                    ";",
8387                    Tab));
8388 
8389   verifyFormat("class X {\n"
8390                "\tvoid f() {\n"
8391                "\t\tsomeFunction(parameter1,\n"
8392                "\t\t\t     parameter2);\n"
8393                "\t}\n"
8394                "};",
8395                Tab);
8396   verifyFormat("#define A                        \\\n"
8397                "\tvoid f() {               \\\n"
8398                "\t\tsomeFunction(    \\\n"
8399                "\t\t    parameter1,  \\\n"
8400                "\t\t    parameter2); \\\n"
8401                "\t}",
8402                Tab);
8403 
8404   Tab.TabWidth = 4;
8405   Tab.IndentWidth = 8;
8406   verifyFormat("class TabWidth4Indent8 {\n"
8407                "\t\tvoid f() {\n"
8408                "\t\t\t\tsomeFunction(parameter1,\n"
8409                "\t\t\t\t\t\t\t parameter2);\n"
8410                "\t\t}\n"
8411                "};",
8412                Tab);
8413 
8414   Tab.TabWidth = 4;
8415   Tab.IndentWidth = 4;
8416   verifyFormat("class TabWidth4Indent4 {\n"
8417                "\tvoid f() {\n"
8418                "\t\tsomeFunction(parameter1,\n"
8419                "\t\t\t\t\t parameter2);\n"
8420                "\t}\n"
8421                "};",
8422                Tab);
8423 
8424   Tab.TabWidth = 8;
8425   Tab.IndentWidth = 4;
8426   verifyFormat("class TabWidth8Indent4 {\n"
8427                "    void f() {\n"
8428                "\tsomeFunction(parameter1,\n"
8429                "\t\t     parameter2);\n"
8430                "    }\n"
8431                "};",
8432                Tab);
8433 
8434   Tab.TabWidth = 8;
8435   Tab.IndentWidth = 8;
8436   EXPECT_EQ("/*\n"
8437             "\t      a\t\tcomment\n"
8438             "\t      in multiple lines\n"
8439             "       */",
8440             format("   /*\t \t \n"
8441                    " \t \t a\t\tcomment\t \t\n"
8442                    " \t \t in multiple lines\t\n"
8443                    " \t  */",
8444                    Tab));
8445 
8446   Tab.UseTab = FormatStyle::UT_ForIndentation;
8447   verifyFormat("{\n"
8448                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8449                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8450                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8451                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8452                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8453                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8454                "};",
8455                Tab);
8456   verifyFormat("enum AA {\n"
8457                "\ta1, // Force multiple lines\n"
8458                "\ta2,\n"
8459                "\ta3\n"
8460                "};",
8461                Tab);
8462   EXPECT_EQ("if (aaaaaaaa && // q\n"
8463             "    bb)         // w\n"
8464             "\t;",
8465             format("if (aaaaaaaa &&// q\n"
8466                    "bb)// w\n"
8467                    ";",
8468                    Tab));
8469   verifyFormat("class X {\n"
8470                "\tvoid f() {\n"
8471                "\t\tsomeFunction(parameter1,\n"
8472                "\t\t             parameter2);\n"
8473                "\t}\n"
8474                "};",
8475                Tab);
8476   verifyFormat("{\n"
8477                "\tQ(\n"
8478                "\t    {\n"
8479                "\t\t    int a;\n"
8480                "\t\t    someFunction(aaaaaaaa,\n"
8481                "\t\t                 bbbbbbb);\n"
8482                "\t    },\n"
8483                "\t    p);\n"
8484                "}",
8485                Tab);
8486   EXPECT_EQ("{\n"
8487             "\t/* aaaa\n"
8488             "\t   bbbb */\n"
8489             "}",
8490             format("{\n"
8491                    "/* aaaa\n"
8492                    "   bbbb */\n"
8493                    "}",
8494                    Tab));
8495   EXPECT_EQ("{\n"
8496             "\t/*\n"
8497             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8498             "\t  bbbbbbbbbbbbb\n"
8499             "\t*/\n"
8500             "}",
8501             format("{\n"
8502                    "/*\n"
8503                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8504                    "*/\n"
8505                    "}",
8506                    Tab));
8507   EXPECT_EQ("{\n"
8508             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8509             "\t// bbbbbbbbbbbbb\n"
8510             "}",
8511             format("{\n"
8512                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8513                    "}",
8514                    Tab));
8515   EXPECT_EQ("{\n"
8516             "\t/*\n"
8517             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8518             "\t  bbbbbbbbbbbbb\n"
8519             "\t*/\n"
8520             "}",
8521             format("{\n"
8522                    "\t/*\n"
8523                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8524                    "\t*/\n"
8525                    "}",
8526                    Tab));
8527   EXPECT_EQ("{\n"
8528             "\t/*\n"
8529             "\n"
8530             "\t*/\n"
8531             "}",
8532             format("{\n"
8533                    "\t/*\n"
8534                    "\n"
8535                    "\t*/\n"
8536                    "}",
8537                    Tab));
8538   EXPECT_EQ("{\n"
8539             "\t/*\n"
8540             " asdf\n"
8541             "\t*/\n"
8542             "}",
8543             format("{\n"
8544                    "\t/*\n"
8545                    " asdf\n"
8546                    "\t*/\n"
8547                    "}",
8548                    Tab));
8549 
8550   Tab.UseTab = FormatStyle::UT_Never;
8551   EXPECT_EQ("/*\n"
8552             "              a\t\tcomment\n"
8553             "              in multiple lines\n"
8554             "       */",
8555             format("   /*\t \t \n"
8556                    " \t \t a\t\tcomment\t \t\n"
8557                    " \t \t in multiple lines\t\n"
8558                    " \t  */",
8559                    Tab));
8560   EXPECT_EQ("/* some\n"
8561             "   comment */",
8562             format(" \t \t /* some\n"
8563                    " \t \t    comment */",
8564                    Tab));
8565   EXPECT_EQ("int a; /* some\n"
8566             "   comment */",
8567             format(" \t \t int a; /* some\n"
8568                    " \t \t    comment */",
8569                    Tab));
8570 
8571   EXPECT_EQ("int a; /* some\n"
8572             "comment */",
8573             format(" \t \t int\ta; /* some\n"
8574                    " \t \t    comment */",
8575                    Tab));
8576   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8577             "    comment */",
8578             format(" \t \t f(\"\t\t\"); /* some\n"
8579                    " \t \t    comment */",
8580                    Tab));
8581   EXPECT_EQ("{\n"
8582             "  /*\n"
8583             "   * Comment\n"
8584             "   */\n"
8585             "  int i;\n"
8586             "}",
8587             format("{\n"
8588                    "\t/*\n"
8589                    "\t * Comment\n"
8590                    "\t */\n"
8591                    "\t int i;\n"
8592                    "}"));
8593 
8594   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8595   Tab.TabWidth = 8;
8596   Tab.IndentWidth = 8;
8597   EXPECT_EQ("if (aaaaaaaa && // q\n"
8598             "    bb)         // w\n"
8599             "\t;",
8600             format("if (aaaaaaaa &&// q\n"
8601                    "bb)// w\n"
8602                    ";",
8603                    Tab));
8604   EXPECT_EQ("if (aaa && bbb) // w\n"
8605             "\t;",
8606             format("if(aaa&&bbb)// w\n"
8607                    ";",
8608                    Tab));
8609   verifyFormat("class X {\n"
8610                "\tvoid f() {\n"
8611                "\t\tsomeFunction(parameter1,\n"
8612                "\t\t\t     parameter2);\n"
8613                "\t}\n"
8614                "};",
8615                Tab);
8616   verifyFormat("#define A                        \\\n"
8617                "\tvoid f() {               \\\n"
8618                "\t\tsomeFunction(    \\\n"
8619                "\t\t    parameter1,  \\\n"
8620                "\t\t    parameter2); \\\n"
8621                "\t}",
8622                Tab);
8623   Tab.TabWidth = 4;
8624   Tab.IndentWidth = 8;
8625   verifyFormat("class TabWidth4Indent8 {\n"
8626                "\t\tvoid f() {\n"
8627                "\t\t\t\tsomeFunction(parameter1,\n"
8628                "\t\t\t\t\t\t\t parameter2);\n"
8629                "\t\t}\n"
8630                "};",
8631                Tab);
8632   Tab.TabWidth = 4;
8633   Tab.IndentWidth = 4;
8634   verifyFormat("class TabWidth4Indent4 {\n"
8635                "\tvoid f() {\n"
8636                "\t\tsomeFunction(parameter1,\n"
8637                "\t\t\t\t\t parameter2);\n"
8638                "\t}\n"
8639                "};",
8640                Tab);
8641   Tab.TabWidth = 8;
8642   Tab.IndentWidth = 4;
8643   verifyFormat("class TabWidth8Indent4 {\n"
8644                "    void f() {\n"
8645                "\tsomeFunction(parameter1,\n"
8646                "\t\t     parameter2);\n"
8647                "    }\n"
8648                "};",
8649                Tab);
8650   Tab.TabWidth = 8;
8651   Tab.IndentWidth = 8;
8652   EXPECT_EQ("/*\n"
8653             "\t      a\t\tcomment\n"
8654             "\t      in multiple lines\n"
8655             "       */",
8656             format("   /*\t \t \n"
8657                    " \t \t a\t\tcomment\t \t\n"
8658                    " \t \t in multiple lines\t\n"
8659                    " \t  */",
8660                    Tab));
8661   verifyFormat("{\n"
8662                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8663                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8664                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8665                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8666                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8667                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8668                "};",
8669                Tab);
8670   verifyFormat("enum AA {\n"
8671                "\ta1, // Force multiple lines\n"
8672                "\ta2,\n"
8673                "\ta3\n"
8674                "};",
8675                Tab);
8676   EXPECT_EQ("if (aaaaaaaa && // q\n"
8677             "    bb)         // w\n"
8678             "\t;",
8679             format("if (aaaaaaaa &&// q\n"
8680                    "bb)// w\n"
8681                    ";",
8682                    Tab));
8683   verifyFormat("class X {\n"
8684                "\tvoid f() {\n"
8685                "\t\tsomeFunction(parameter1,\n"
8686                "\t\t\t     parameter2);\n"
8687                "\t}\n"
8688                "};",
8689                Tab);
8690   verifyFormat("{\n"
8691                "\tQ(\n"
8692                "\t    {\n"
8693                "\t\t    int a;\n"
8694                "\t\t    someFunction(aaaaaaaa,\n"
8695                "\t\t\t\t bbbbbbb);\n"
8696                "\t    },\n"
8697                "\t    p);\n"
8698                "}",
8699                Tab);
8700   EXPECT_EQ("{\n"
8701             "\t/* aaaa\n"
8702             "\t   bbbb */\n"
8703             "}",
8704             format("{\n"
8705                    "/* aaaa\n"
8706                    "   bbbb */\n"
8707                    "}",
8708                    Tab));
8709   EXPECT_EQ("{\n"
8710             "\t/*\n"
8711             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8712             "\t  bbbbbbbbbbbbb\n"
8713             "\t*/\n"
8714             "}",
8715             format("{\n"
8716                    "/*\n"
8717                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8718                    "*/\n"
8719                    "}",
8720                    Tab));
8721   EXPECT_EQ("{\n"
8722             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8723             "\t// bbbbbbbbbbbbb\n"
8724             "}",
8725             format("{\n"
8726                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8727                    "}",
8728                    Tab));
8729   EXPECT_EQ("{\n"
8730             "\t/*\n"
8731             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8732             "\t  bbbbbbbbbbbbb\n"
8733             "\t*/\n"
8734             "}",
8735             format("{\n"
8736                    "\t/*\n"
8737                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8738                    "\t*/\n"
8739                    "}",
8740                    Tab));
8741   EXPECT_EQ("{\n"
8742             "\t/*\n"
8743             "\n"
8744             "\t*/\n"
8745             "}",
8746             format("{\n"
8747                    "\t/*\n"
8748                    "\n"
8749                    "\t*/\n"
8750                    "}",
8751                    Tab));
8752   EXPECT_EQ("{\n"
8753             "\t/*\n"
8754             " asdf\n"
8755             "\t*/\n"
8756             "}",
8757             format("{\n"
8758                    "\t/*\n"
8759                    " asdf\n"
8760                    "\t*/\n"
8761                    "}",
8762                    Tab));
8763   EXPECT_EQ("/*\n"
8764             "\t      a\t\tcomment\n"
8765             "\t      in multiple lines\n"
8766             "       */",
8767             format("   /*\t \t \n"
8768                    " \t \t a\t\tcomment\t \t\n"
8769                    " \t \t in multiple lines\t\n"
8770                    " \t  */",
8771                    Tab));
8772   EXPECT_EQ("/* some\n"
8773             "   comment */",
8774             format(" \t \t /* some\n"
8775                    " \t \t    comment */",
8776                    Tab));
8777   EXPECT_EQ("int a; /* some\n"
8778             "   comment */",
8779             format(" \t \t int a; /* some\n"
8780                    " \t \t    comment */",
8781                    Tab));
8782   EXPECT_EQ("int a; /* some\n"
8783             "comment */",
8784             format(" \t \t int\ta; /* some\n"
8785                    " \t \t    comment */",
8786                    Tab));
8787   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8788             "    comment */",
8789             format(" \t \t f(\"\t\t\"); /* some\n"
8790                    " \t \t    comment */",
8791                    Tab));
8792   EXPECT_EQ("{\n"
8793             "  /*\n"
8794             "   * Comment\n"
8795             "   */\n"
8796             "  int i;\n"
8797             "}",
8798             format("{\n"
8799                    "\t/*\n"
8800                    "\t * Comment\n"
8801                    "\t */\n"
8802                    "\t int i;\n"
8803                    "}"));
8804   Tab.AlignConsecutiveAssignments = true;
8805   Tab.AlignConsecutiveDeclarations = true;
8806   Tab.TabWidth = 4;
8807   Tab.IndentWidth = 4;
8808   verifyFormat("class Assign {\n"
8809                "\tvoid f() {\n"
8810                "\t\tint         x      = 123;\n"
8811                "\t\tint         random = 4;\n"
8812                "\t\tstd::string alphabet =\n"
8813                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8814                "\t}\n"
8815                "};",
8816                Tab);
8817 }
8818 
8819 TEST_F(FormatTest, CalculatesOriginalColumn) {
8820   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8821             "q\"; /* some\n"
8822             "       comment */",
8823             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8824                    "q\"; /* some\n"
8825                    "       comment */",
8826                    getLLVMStyle()));
8827   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8828             "/* some\n"
8829             "   comment */",
8830             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8831                    " /* some\n"
8832                    "    comment */",
8833                    getLLVMStyle()));
8834   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8835             "qqq\n"
8836             "/* some\n"
8837             "   comment */",
8838             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8839                    "qqq\n"
8840                    " /* some\n"
8841                    "    comment */",
8842                    getLLVMStyle()));
8843   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8844             "wwww; /* some\n"
8845             "         comment */",
8846             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8847                    "wwww; /* some\n"
8848                    "         comment */",
8849                    getLLVMStyle()));
8850 }
8851 
8852 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8853   FormatStyle NoSpace = getLLVMStyle();
8854   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8855 
8856   verifyFormat("while(true)\n"
8857                "  continue;",
8858                NoSpace);
8859   verifyFormat("for(;;)\n"
8860                "  continue;",
8861                NoSpace);
8862   verifyFormat("if(true)\n"
8863                "  f();\n"
8864                "else if(true)\n"
8865                "  f();",
8866                NoSpace);
8867   verifyFormat("do {\n"
8868                "  do_something();\n"
8869                "} while(something());",
8870                NoSpace);
8871   verifyFormat("switch(x) {\n"
8872                "default:\n"
8873                "  break;\n"
8874                "}",
8875                NoSpace);
8876   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8877   verifyFormat("size_t x = sizeof(x);", NoSpace);
8878   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8879   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8880   verifyFormat("alignas(128) char a[128];", NoSpace);
8881   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8882   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8883   verifyFormat("int f() throw(Deprecated);", NoSpace);
8884   verifyFormat("typedef void (*cb)(int);", NoSpace);
8885   verifyFormat("T A::operator()();", NoSpace);
8886   verifyFormat("X A::operator++(T);", NoSpace);
8887 
8888   FormatStyle Space = getLLVMStyle();
8889   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8890 
8891   verifyFormat("int f ();", Space);
8892   verifyFormat("void f (int a, T b) {\n"
8893                "  while (true)\n"
8894                "    continue;\n"
8895                "}",
8896                Space);
8897   verifyFormat("if (true)\n"
8898                "  f ();\n"
8899                "else if (true)\n"
8900                "  f ();",
8901                Space);
8902   verifyFormat("do {\n"
8903                "  do_something ();\n"
8904                "} while (something ());",
8905                Space);
8906   verifyFormat("switch (x) {\n"
8907                "default:\n"
8908                "  break;\n"
8909                "}",
8910                Space);
8911   verifyFormat("A::A () : a (1) {}", Space);
8912   verifyFormat("void f () __attribute__ ((asdf));", Space);
8913   verifyFormat("*(&a + 1);\n"
8914                "&((&a)[1]);\n"
8915                "a[(b + c) * d];\n"
8916                "(((a + 1) * 2) + 3) * 4;",
8917                Space);
8918   verifyFormat("#define A(x) x", Space);
8919   verifyFormat("#define A (x) x", Space);
8920   verifyFormat("#if defined(x)\n"
8921                "#endif",
8922                Space);
8923   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8924   verifyFormat("size_t x = sizeof (x);", Space);
8925   verifyFormat("auto f (int x) -> decltype (x);", Space);
8926   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8927   verifyFormat("alignas (128) char a[128];", Space);
8928   verifyFormat("size_t x = alignof (MyType);", Space);
8929   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8930   verifyFormat("int f () throw (Deprecated);", Space);
8931   verifyFormat("typedef void (*cb) (int);", Space);
8932   verifyFormat("T A::operator() ();", Space);
8933   verifyFormat("X A::operator++ (T);", Space);
8934 }
8935 
8936 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8937   FormatStyle Spaces = getLLVMStyle();
8938 
8939   Spaces.SpacesInParentheses = true;
8940   verifyFormat("call( x, y, z );", Spaces);
8941   verifyFormat("call();", Spaces);
8942   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8943   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8944                Spaces);
8945   verifyFormat("while ( (bool)1 )\n"
8946                "  continue;",
8947                Spaces);
8948   verifyFormat("for ( ;; )\n"
8949                "  continue;",
8950                Spaces);
8951   verifyFormat("if ( true )\n"
8952                "  f();\n"
8953                "else if ( true )\n"
8954                "  f();",
8955                Spaces);
8956   verifyFormat("do {\n"
8957                "  do_something( (int)i );\n"
8958                "} while ( something() );",
8959                Spaces);
8960   verifyFormat("switch ( x ) {\n"
8961                "default:\n"
8962                "  break;\n"
8963                "}",
8964                Spaces);
8965 
8966   Spaces.SpacesInParentheses = false;
8967   Spaces.SpacesInCStyleCastParentheses = true;
8968   verifyFormat("Type *A = ( Type * )P;", Spaces);
8969   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8970   verifyFormat("x = ( int32 )y;", Spaces);
8971   verifyFormat("int a = ( int )(2.0f);", Spaces);
8972   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8973   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8974   verifyFormat("#define x (( int )-1)", Spaces);
8975 
8976   // Run the first set of tests again with:
8977   Spaces.SpacesInParentheses = false;
8978   Spaces.SpaceInEmptyParentheses = true;
8979   Spaces.SpacesInCStyleCastParentheses = true;
8980   verifyFormat("call(x, y, z);", Spaces);
8981   verifyFormat("call( );", Spaces);
8982   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8983   verifyFormat("while (( bool )1)\n"
8984                "  continue;",
8985                Spaces);
8986   verifyFormat("for (;;)\n"
8987                "  continue;",
8988                Spaces);
8989   verifyFormat("if (true)\n"
8990                "  f( );\n"
8991                "else if (true)\n"
8992                "  f( );",
8993                Spaces);
8994   verifyFormat("do {\n"
8995                "  do_something(( int )i);\n"
8996                "} while (something( ));",
8997                Spaces);
8998   verifyFormat("switch (x) {\n"
8999                "default:\n"
9000                "  break;\n"
9001                "}",
9002                Spaces);
9003 
9004   // Run the first set of tests again with:
9005   Spaces.SpaceAfterCStyleCast = true;
9006   verifyFormat("call(x, y, z);", Spaces);
9007   verifyFormat("call( );", Spaces);
9008   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9009   verifyFormat("while (( bool ) 1)\n"
9010                "  continue;",
9011                Spaces);
9012   verifyFormat("for (;;)\n"
9013                "  continue;",
9014                Spaces);
9015   verifyFormat("if (true)\n"
9016                "  f( );\n"
9017                "else if (true)\n"
9018                "  f( );",
9019                Spaces);
9020   verifyFormat("do {\n"
9021                "  do_something(( int ) i);\n"
9022                "} while (something( ));",
9023                Spaces);
9024   verifyFormat("switch (x) {\n"
9025                "default:\n"
9026                "  break;\n"
9027                "}",
9028                Spaces);
9029 
9030   // Run subset of tests again with:
9031   Spaces.SpacesInCStyleCastParentheses = false;
9032   Spaces.SpaceAfterCStyleCast = true;
9033   verifyFormat("while ((bool) 1)\n"
9034                "  continue;",
9035                Spaces);
9036   verifyFormat("do {\n"
9037                "  do_something((int) i);\n"
9038                "} while (something( ));",
9039                Spaces);
9040 }
9041 
9042 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9043   verifyFormat("int a[5];");
9044   verifyFormat("a[3] += 42;");
9045 
9046   FormatStyle Spaces = getLLVMStyle();
9047   Spaces.SpacesInSquareBrackets = true;
9048   // Lambdas unchanged.
9049   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9050   verifyFormat("return [i, args...] {};", Spaces);
9051 
9052   // Not lambdas.
9053   verifyFormat("int a[ 5 ];", Spaces);
9054   verifyFormat("a[ 3 ] += 42;", Spaces);
9055   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9056   verifyFormat("double &operator[](int i) { return 0; }\n"
9057                "int i;",
9058                Spaces);
9059   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9060   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9061   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9062 }
9063 
9064 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9065   verifyFormat("int a = 5;");
9066   verifyFormat("a += 42;");
9067   verifyFormat("a or_eq 8;");
9068 
9069   FormatStyle Spaces = getLLVMStyle();
9070   Spaces.SpaceBeforeAssignmentOperators = false;
9071   verifyFormat("int a= 5;", Spaces);
9072   verifyFormat("a+= 42;", Spaces);
9073   verifyFormat("a or_eq 8;", Spaces);
9074 }
9075 
9076 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9077   FormatStyle Alignment = getLLVMStyle();
9078   Alignment.AlignConsecutiveAssignments = false;
9079   verifyFormat("int a = 5;\n"
9080                "int oneTwoThree = 123;",
9081                Alignment);
9082   verifyFormat("int a = 5;\n"
9083                "int oneTwoThree = 123;",
9084                Alignment);
9085 
9086   Alignment.AlignConsecutiveAssignments = true;
9087   verifyFormat("int a           = 5;\n"
9088                "int oneTwoThree = 123;",
9089                Alignment);
9090   verifyFormat("int a           = method();\n"
9091                "int oneTwoThree = 133;",
9092                Alignment);
9093   verifyFormat("a &= 5;\n"
9094                "bcd *= 5;\n"
9095                "ghtyf += 5;\n"
9096                "dvfvdb -= 5;\n"
9097                "a /= 5;\n"
9098                "vdsvsv %= 5;\n"
9099                "sfdbddfbdfbb ^= 5;\n"
9100                "dvsdsv |= 5;\n"
9101                "int dsvvdvsdvvv = 123;",
9102                Alignment);
9103   verifyFormat("int i = 1, j = 10;\n"
9104                "something = 2000;",
9105                Alignment);
9106   verifyFormat("something = 2000;\n"
9107                "int i = 1, j = 10;\n",
9108                Alignment);
9109   verifyFormat("something = 2000;\n"
9110                "another   = 911;\n"
9111                "int i = 1, j = 10;\n"
9112                "oneMore = 1;\n"
9113                "i       = 2;",
9114                Alignment);
9115   verifyFormat("int a   = 5;\n"
9116                "int one = 1;\n"
9117                "method();\n"
9118                "int oneTwoThree = 123;\n"
9119                "int oneTwo      = 12;",
9120                Alignment);
9121   verifyFormat("int oneTwoThree = 123;\n"
9122                "int oneTwo      = 12;\n"
9123                "method();\n",
9124                Alignment);
9125   verifyFormat("int oneTwoThree = 123; // comment\n"
9126                "int oneTwo      = 12;  // comment",
9127                Alignment);
9128   EXPECT_EQ("int a = 5;\n"
9129             "\n"
9130             "int oneTwoThree = 123;",
9131             format("int a       = 5;\n"
9132                    "\n"
9133                    "int oneTwoThree= 123;",
9134                    Alignment));
9135   EXPECT_EQ("int a   = 5;\n"
9136             "int one = 1;\n"
9137             "\n"
9138             "int oneTwoThree = 123;",
9139             format("int a = 5;\n"
9140                    "int one = 1;\n"
9141                    "\n"
9142                    "int oneTwoThree = 123;",
9143                    Alignment));
9144   EXPECT_EQ("int a   = 5;\n"
9145             "int one = 1;\n"
9146             "\n"
9147             "int oneTwoThree = 123;\n"
9148             "int oneTwo      = 12;",
9149             format("int a = 5;\n"
9150                    "int one = 1;\n"
9151                    "\n"
9152                    "int oneTwoThree = 123;\n"
9153                    "int oneTwo = 12;",
9154                    Alignment));
9155   Alignment.AlignEscapedNewlinesLeft = true;
9156   verifyFormat("#define A               \\\n"
9157                "  int aaaa       = 12;  \\\n"
9158                "  int b          = 23;  \\\n"
9159                "  int ccc        = 234; \\\n"
9160                "  int dddddddddd = 2345;",
9161                Alignment);
9162   Alignment.AlignEscapedNewlinesLeft = false;
9163   verifyFormat("#define A                                                      "
9164                "                \\\n"
9165                "  int aaaa       = 12;                                         "
9166                "                \\\n"
9167                "  int b          = 23;                                         "
9168                "                \\\n"
9169                "  int ccc        = 234;                                        "
9170                "                \\\n"
9171                "  int dddddddddd = 2345;",
9172                Alignment);
9173   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9174                "k = 4, int l = 5,\n"
9175                "                  int m = 6) {\n"
9176                "  int j      = 10;\n"
9177                "  otherThing = 1;\n"
9178                "}",
9179                Alignment);
9180   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9181                "  int i   = 1;\n"
9182                "  int j   = 2;\n"
9183                "  int big = 10000;\n"
9184                "}",
9185                Alignment);
9186   verifyFormat("class C {\n"
9187                "public:\n"
9188                "  int i            = 1;\n"
9189                "  virtual void f() = 0;\n"
9190                "};",
9191                Alignment);
9192   verifyFormat("int i = 1;\n"
9193                "if (SomeType t = getSomething()) {\n"
9194                "}\n"
9195                "int j   = 2;\n"
9196                "int big = 10000;",
9197                Alignment);
9198   verifyFormat("int j = 7;\n"
9199                "for (int k = 0; k < N; ++k) {\n"
9200                "}\n"
9201                "int j   = 2;\n"
9202                "int big = 10000;\n"
9203                "}",
9204                Alignment);
9205   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9206   verifyFormat("int i = 1;\n"
9207                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9208                "    = someLooooooooooooooooongFunction();\n"
9209                "int j = 2;",
9210                Alignment);
9211   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9212   verifyFormat("int i = 1;\n"
9213                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9214                "    someLooooooooooooooooongFunction();\n"
9215                "int j = 2;",
9216                Alignment);
9217 
9218   verifyFormat("auto lambda = []() {\n"
9219                "  auto i = 0;\n"
9220                "  return 0;\n"
9221                "};\n"
9222                "int i  = 0;\n"
9223                "auto v = type{\n"
9224                "    i = 1,   //\n"
9225                "    (i = 2), //\n"
9226                "    i = 3    //\n"
9227                "};",
9228                Alignment);
9229 
9230   // FIXME: Should align all three assignments
9231   verifyFormat(
9232       "int i      = 1;\n"
9233       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9234       "                          loooooooooooooooooooooongParameterB);\n"
9235       "int j = 2;",
9236       Alignment);
9237 
9238   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9239                "          typename B   = very_long_type_name_1,\n"
9240                "          typename T_2 = very_long_type_name_2>\n"
9241                "auto foo() {}\n",
9242                Alignment);
9243   verifyFormat("int a, b = 1;\n"
9244                "int c  = 2;\n"
9245                "int dd = 3;\n",
9246                Alignment);
9247   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9248                "float b[1][] = {{3.f}};\n",
9249                Alignment);
9250 }
9251 
9252 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9253   FormatStyle Alignment = getLLVMStyle();
9254   Alignment.AlignConsecutiveDeclarations = false;
9255   verifyFormat("float const a = 5;\n"
9256                "int oneTwoThree = 123;",
9257                Alignment);
9258   verifyFormat("int a = 5;\n"
9259                "float const oneTwoThree = 123;",
9260                Alignment);
9261 
9262   Alignment.AlignConsecutiveDeclarations = true;
9263   verifyFormat("float const a = 5;\n"
9264                "int         oneTwoThree = 123;",
9265                Alignment);
9266   verifyFormat("int         a = method();\n"
9267                "float const oneTwoThree = 133;",
9268                Alignment);
9269   verifyFormat("int i = 1, j = 10;\n"
9270                "something = 2000;",
9271                Alignment);
9272   verifyFormat("something = 2000;\n"
9273                "int i = 1, j = 10;\n",
9274                Alignment);
9275   verifyFormat("float      something = 2000;\n"
9276                "double     another = 911;\n"
9277                "int        i = 1, j = 10;\n"
9278                "const int *oneMore = 1;\n"
9279                "unsigned   i = 2;",
9280                Alignment);
9281   verifyFormat("float a = 5;\n"
9282                "int   one = 1;\n"
9283                "method();\n"
9284                "const double       oneTwoThree = 123;\n"
9285                "const unsigned int oneTwo = 12;",
9286                Alignment);
9287   verifyFormat("int      oneTwoThree{0}; // comment\n"
9288                "unsigned oneTwo;         // comment",
9289                Alignment);
9290   EXPECT_EQ("float const a = 5;\n"
9291             "\n"
9292             "int oneTwoThree = 123;",
9293             format("float const   a = 5;\n"
9294                    "\n"
9295                    "int           oneTwoThree= 123;",
9296                    Alignment));
9297   EXPECT_EQ("float a = 5;\n"
9298             "int   one = 1;\n"
9299             "\n"
9300             "unsigned oneTwoThree = 123;",
9301             format("float    a = 5;\n"
9302                    "int      one = 1;\n"
9303                    "\n"
9304                    "unsigned oneTwoThree = 123;",
9305                    Alignment));
9306   EXPECT_EQ("float a = 5;\n"
9307             "int   one = 1;\n"
9308             "\n"
9309             "unsigned oneTwoThree = 123;\n"
9310             "int      oneTwo = 12;",
9311             format("float    a = 5;\n"
9312                    "int one = 1;\n"
9313                    "\n"
9314                    "unsigned oneTwoThree = 123;\n"
9315                    "int oneTwo = 12;",
9316                    Alignment));
9317   Alignment.AlignConsecutiveAssignments = true;
9318   verifyFormat("float      something = 2000;\n"
9319                "double     another   = 911;\n"
9320                "int        i = 1, j = 10;\n"
9321                "const int *oneMore = 1;\n"
9322                "unsigned   i       = 2;",
9323                Alignment);
9324   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9325                "unsigned oneTwo      = 0;   // comment",
9326                Alignment);
9327   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9328             "  int const i   = 1;\n"
9329             "  int *     j   = 2;\n"
9330             "  int       big = 10000;\n"
9331             "\n"
9332             "  unsigned oneTwoThree = 123;\n"
9333             "  int      oneTwo      = 12;\n"
9334             "  method();\n"
9335             "  float k  = 2;\n"
9336             "  int   ll = 10000;\n"
9337             "}",
9338             format("void SomeFunction(int parameter= 0) {\n"
9339                    " int const  i= 1;\n"
9340                    "  int *j=2;\n"
9341                    " int big  =  10000;\n"
9342                    "\n"
9343                    "unsigned oneTwoThree  =123;\n"
9344                    "int oneTwo = 12;\n"
9345                    "  method();\n"
9346                    "float k= 2;\n"
9347                    "int ll=10000;\n"
9348                    "}",
9349                    Alignment));
9350   Alignment.AlignConsecutiveAssignments = false;
9351   Alignment.AlignEscapedNewlinesLeft = true;
9352   verifyFormat("#define A              \\\n"
9353                "  int       aaaa = 12; \\\n"
9354                "  float     b = 23;    \\\n"
9355                "  const int ccc = 234; \\\n"
9356                "  unsigned  dddddddddd = 2345;",
9357                Alignment);
9358   Alignment.AlignEscapedNewlinesLeft = false;
9359   Alignment.ColumnLimit = 30;
9360   verifyFormat("#define A                    \\\n"
9361                "  int       aaaa = 12;       \\\n"
9362                "  float     b = 23;          \\\n"
9363                "  const int ccc = 234;       \\\n"
9364                "  int       dddddddddd = 2345;",
9365                Alignment);
9366   Alignment.ColumnLimit = 80;
9367   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9368                "k = 4, int l = 5,\n"
9369                "                  int m = 6) {\n"
9370                "  const int j = 10;\n"
9371                "  otherThing = 1;\n"
9372                "}",
9373                Alignment);
9374   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9375                "  int const i = 1;\n"
9376                "  int *     j = 2;\n"
9377                "  int       big = 10000;\n"
9378                "}",
9379                Alignment);
9380   verifyFormat("class C {\n"
9381                "public:\n"
9382                "  int          i = 1;\n"
9383                "  virtual void f() = 0;\n"
9384                "};",
9385                Alignment);
9386   verifyFormat("float i = 1;\n"
9387                "if (SomeType t = getSomething()) {\n"
9388                "}\n"
9389                "const unsigned j = 2;\n"
9390                "int            big = 10000;",
9391                Alignment);
9392   verifyFormat("float j = 7;\n"
9393                "for (int k = 0; k < N; ++k) {\n"
9394                "}\n"
9395                "unsigned j = 2;\n"
9396                "int      big = 10000;\n"
9397                "}",
9398                Alignment);
9399   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9400   verifyFormat("float              i = 1;\n"
9401                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9402                "    = someLooooooooooooooooongFunction();\n"
9403                "int j = 2;",
9404                Alignment);
9405   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9406   verifyFormat("int                i = 1;\n"
9407                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9408                "    someLooooooooooooooooongFunction();\n"
9409                "int j = 2;",
9410                Alignment);
9411 
9412   Alignment.AlignConsecutiveAssignments = true;
9413   verifyFormat("auto lambda = []() {\n"
9414                "  auto  ii = 0;\n"
9415                "  float j  = 0;\n"
9416                "  return 0;\n"
9417                "};\n"
9418                "int   i  = 0;\n"
9419                "float i2 = 0;\n"
9420                "auto  v  = type{\n"
9421                "    i = 1,   //\n"
9422                "    (i = 2), //\n"
9423                "    i = 3    //\n"
9424                "};",
9425                Alignment);
9426   Alignment.AlignConsecutiveAssignments = false;
9427 
9428   // FIXME: Should align all three declarations
9429   verifyFormat(
9430       "int      i = 1;\n"
9431       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9432       "                          loooooooooooooooooooooongParameterB);\n"
9433       "int j = 2;",
9434       Alignment);
9435 
9436   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9437   // We expect declarations and assignments to align, as long as it doesn't
9438   // exceed the column limit, starting a new alignemnt sequence whenever it
9439   // happens.
9440   Alignment.AlignConsecutiveAssignments = true;
9441   Alignment.ColumnLimit = 30;
9442   verifyFormat("float    ii              = 1;\n"
9443                "unsigned j               = 2;\n"
9444                "int someVerylongVariable = 1;\n"
9445                "AnotherLongType  ll = 123456;\n"
9446                "VeryVeryLongType k  = 2;\n"
9447                "int              myvar = 1;",
9448                Alignment);
9449   Alignment.ColumnLimit = 80;
9450   Alignment.AlignConsecutiveAssignments = false;
9451 
9452   verifyFormat(
9453       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9454       "          typename LongType, typename B>\n"
9455       "auto foo() {}\n",
9456       Alignment);
9457   verifyFormat("float a, b = 1;\n"
9458                "int   c = 2;\n"
9459                "int   dd = 3;\n",
9460                Alignment);
9461   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9462                "float b[1][] = {{3.f}};\n",
9463                Alignment);
9464   Alignment.AlignConsecutiveAssignments = true;
9465   verifyFormat("float a, b = 1;\n"
9466                "int   c  = 2;\n"
9467                "int   dd = 3;\n",
9468                Alignment);
9469   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9470                "float b[1][] = {{3.f}};\n",
9471                Alignment);
9472   Alignment.AlignConsecutiveAssignments = false;
9473 
9474   Alignment.ColumnLimit = 30;
9475   Alignment.BinPackParameters = false;
9476   verifyFormat("void foo(float     a,\n"
9477                "         float     b,\n"
9478                "         int       c,\n"
9479                "         uint32_t *d) {\n"
9480                "  int *  e = 0;\n"
9481                "  float  f = 0;\n"
9482                "  double g = 0;\n"
9483                "}\n"
9484                "void bar(ino_t     a,\n"
9485                "         int       b,\n"
9486                "         uint32_t *c,\n"
9487                "         bool      d) {}\n",
9488                Alignment);
9489   Alignment.BinPackParameters = true;
9490   Alignment.ColumnLimit = 80;
9491 }
9492 
9493 TEST_F(FormatTest, LinuxBraceBreaking) {
9494   FormatStyle LinuxBraceStyle = getLLVMStyle();
9495   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9496   verifyFormat("namespace a\n"
9497                "{\n"
9498                "class A\n"
9499                "{\n"
9500                "  void f()\n"
9501                "  {\n"
9502                "    if (true) {\n"
9503                "      a();\n"
9504                "      b();\n"
9505                "    } else {\n"
9506                "      a();\n"
9507                "    }\n"
9508                "  }\n"
9509                "  void g() { return; }\n"
9510                "};\n"
9511                "struct B {\n"
9512                "  int x;\n"
9513                "};\n"
9514                "}\n",
9515                LinuxBraceStyle);
9516   verifyFormat("enum X {\n"
9517                "  Y = 0,\n"
9518                "}\n",
9519                LinuxBraceStyle);
9520   verifyFormat("struct S {\n"
9521                "  int Type;\n"
9522                "  union {\n"
9523                "    int x;\n"
9524                "    double y;\n"
9525                "  } Value;\n"
9526                "  class C\n"
9527                "  {\n"
9528                "    MyFavoriteType Value;\n"
9529                "  } Class;\n"
9530                "}\n",
9531                LinuxBraceStyle);
9532 }
9533 
9534 TEST_F(FormatTest, MozillaBraceBreaking) {
9535   FormatStyle MozillaBraceStyle = getLLVMStyle();
9536   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9537   verifyFormat("namespace a {\n"
9538                "class A\n"
9539                "{\n"
9540                "  void f()\n"
9541                "  {\n"
9542                "    if (true) {\n"
9543                "      a();\n"
9544                "      b();\n"
9545                "    }\n"
9546                "  }\n"
9547                "  void g() { return; }\n"
9548                "};\n"
9549                "enum E\n"
9550                "{\n"
9551                "  A,\n"
9552                "  // foo\n"
9553                "  B,\n"
9554                "  C\n"
9555                "};\n"
9556                "struct B\n"
9557                "{\n"
9558                "  int x;\n"
9559                "};\n"
9560                "}\n",
9561                MozillaBraceStyle);
9562   verifyFormat("struct S\n"
9563                "{\n"
9564                "  int Type;\n"
9565                "  union\n"
9566                "  {\n"
9567                "    int x;\n"
9568                "    double y;\n"
9569                "  } Value;\n"
9570                "  class C\n"
9571                "  {\n"
9572                "    MyFavoriteType Value;\n"
9573                "  } Class;\n"
9574                "}\n",
9575                MozillaBraceStyle);
9576 }
9577 
9578 TEST_F(FormatTest, StroustrupBraceBreaking) {
9579   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9580   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9581   verifyFormat("namespace a {\n"
9582                "class A {\n"
9583                "  void f()\n"
9584                "  {\n"
9585                "    if (true) {\n"
9586                "      a();\n"
9587                "      b();\n"
9588                "    }\n"
9589                "  }\n"
9590                "  void g() { return; }\n"
9591                "};\n"
9592                "struct B {\n"
9593                "  int x;\n"
9594                "};\n"
9595                "}\n",
9596                StroustrupBraceStyle);
9597 
9598   verifyFormat("void foo()\n"
9599                "{\n"
9600                "  if (a) {\n"
9601                "    a();\n"
9602                "  }\n"
9603                "  else {\n"
9604                "    b();\n"
9605                "  }\n"
9606                "}\n",
9607                StroustrupBraceStyle);
9608 
9609   verifyFormat("#ifdef _DEBUG\n"
9610                "int foo(int i = 0)\n"
9611                "#else\n"
9612                "int foo(int i = 5)\n"
9613                "#endif\n"
9614                "{\n"
9615                "  return i;\n"
9616                "}",
9617                StroustrupBraceStyle);
9618 
9619   verifyFormat("void foo() {}\n"
9620                "void bar()\n"
9621                "#ifdef _DEBUG\n"
9622                "{\n"
9623                "  foo();\n"
9624                "}\n"
9625                "#else\n"
9626                "{\n"
9627                "}\n"
9628                "#endif",
9629                StroustrupBraceStyle);
9630 
9631   verifyFormat("void foobar() { int i = 5; }\n"
9632                "#ifdef _DEBUG\n"
9633                "void bar() {}\n"
9634                "#else\n"
9635                "void bar() { foobar(); }\n"
9636                "#endif",
9637                StroustrupBraceStyle);
9638 }
9639 
9640 TEST_F(FormatTest, AllmanBraceBreaking) {
9641   FormatStyle AllmanBraceStyle = getLLVMStyle();
9642   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9643   verifyFormat("namespace a\n"
9644                "{\n"
9645                "class A\n"
9646                "{\n"
9647                "  void f()\n"
9648                "  {\n"
9649                "    if (true)\n"
9650                "    {\n"
9651                "      a();\n"
9652                "      b();\n"
9653                "    }\n"
9654                "  }\n"
9655                "  void g() { return; }\n"
9656                "};\n"
9657                "struct B\n"
9658                "{\n"
9659                "  int x;\n"
9660                "};\n"
9661                "}",
9662                AllmanBraceStyle);
9663 
9664   verifyFormat("void f()\n"
9665                "{\n"
9666                "  if (true)\n"
9667                "  {\n"
9668                "    a();\n"
9669                "  }\n"
9670                "  else if (false)\n"
9671                "  {\n"
9672                "    b();\n"
9673                "  }\n"
9674                "  else\n"
9675                "  {\n"
9676                "    c();\n"
9677                "  }\n"
9678                "}\n",
9679                AllmanBraceStyle);
9680 
9681   verifyFormat("void f()\n"
9682                "{\n"
9683                "  for (int i = 0; i < 10; ++i)\n"
9684                "  {\n"
9685                "    a();\n"
9686                "  }\n"
9687                "  while (false)\n"
9688                "  {\n"
9689                "    b();\n"
9690                "  }\n"
9691                "  do\n"
9692                "  {\n"
9693                "    c();\n"
9694                "  } while (false)\n"
9695                "}\n",
9696                AllmanBraceStyle);
9697 
9698   verifyFormat("void f(int a)\n"
9699                "{\n"
9700                "  switch (a)\n"
9701                "  {\n"
9702                "  case 0:\n"
9703                "    break;\n"
9704                "  case 1:\n"
9705                "  {\n"
9706                "    break;\n"
9707                "  }\n"
9708                "  case 2:\n"
9709                "  {\n"
9710                "  }\n"
9711                "  break;\n"
9712                "  default:\n"
9713                "    break;\n"
9714                "  }\n"
9715                "}\n",
9716                AllmanBraceStyle);
9717 
9718   verifyFormat("enum X\n"
9719                "{\n"
9720                "  Y = 0,\n"
9721                "}\n",
9722                AllmanBraceStyle);
9723   verifyFormat("enum X\n"
9724                "{\n"
9725                "  Y = 0\n"
9726                "}\n",
9727                AllmanBraceStyle);
9728 
9729   verifyFormat("@interface BSApplicationController ()\n"
9730                "{\n"
9731                "@private\n"
9732                "  id _extraIvar;\n"
9733                "}\n"
9734                "@end\n",
9735                AllmanBraceStyle);
9736 
9737   verifyFormat("#ifdef _DEBUG\n"
9738                "int foo(int i = 0)\n"
9739                "#else\n"
9740                "int foo(int i = 5)\n"
9741                "#endif\n"
9742                "{\n"
9743                "  return i;\n"
9744                "}",
9745                AllmanBraceStyle);
9746 
9747   verifyFormat("void foo() {}\n"
9748                "void bar()\n"
9749                "#ifdef _DEBUG\n"
9750                "{\n"
9751                "  foo();\n"
9752                "}\n"
9753                "#else\n"
9754                "{\n"
9755                "}\n"
9756                "#endif",
9757                AllmanBraceStyle);
9758 
9759   verifyFormat("void foobar() { int i = 5; }\n"
9760                "#ifdef _DEBUG\n"
9761                "void bar() {}\n"
9762                "#else\n"
9763                "void bar() { foobar(); }\n"
9764                "#endif",
9765                AllmanBraceStyle);
9766 
9767   // This shouldn't affect ObjC blocks..
9768   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9769                "  // ...\n"
9770                "  int i;\n"
9771                "}];",
9772                AllmanBraceStyle);
9773   verifyFormat("void (^block)(void) = ^{\n"
9774                "  // ...\n"
9775                "  int i;\n"
9776                "};",
9777                AllmanBraceStyle);
9778   // .. or dict literals.
9779   verifyFormat("void f()\n"
9780                "{\n"
9781                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9782                "}",
9783                AllmanBraceStyle);
9784   verifyFormat("int f()\n"
9785                "{ // comment\n"
9786                "  return 42;\n"
9787                "}",
9788                AllmanBraceStyle);
9789 
9790   AllmanBraceStyle.ColumnLimit = 19;
9791   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9792   AllmanBraceStyle.ColumnLimit = 18;
9793   verifyFormat("void f()\n"
9794                "{\n"
9795                "  int i;\n"
9796                "}",
9797                AllmanBraceStyle);
9798   AllmanBraceStyle.ColumnLimit = 80;
9799 
9800   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9801   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9802   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9803   verifyFormat("void f(bool b)\n"
9804                "{\n"
9805                "  if (b)\n"
9806                "  {\n"
9807                "    return;\n"
9808                "  }\n"
9809                "}\n",
9810                BreakBeforeBraceShortIfs);
9811   verifyFormat("void f(bool b)\n"
9812                "{\n"
9813                "  if (b) return;\n"
9814                "}\n",
9815                BreakBeforeBraceShortIfs);
9816   verifyFormat("void f(bool b)\n"
9817                "{\n"
9818                "  while (b)\n"
9819                "  {\n"
9820                "    return;\n"
9821                "  }\n"
9822                "}\n",
9823                BreakBeforeBraceShortIfs);
9824 }
9825 
9826 TEST_F(FormatTest, GNUBraceBreaking) {
9827   FormatStyle GNUBraceStyle = getLLVMStyle();
9828   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9829   verifyFormat("namespace a\n"
9830                "{\n"
9831                "class A\n"
9832                "{\n"
9833                "  void f()\n"
9834                "  {\n"
9835                "    int a;\n"
9836                "    {\n"
9837                "      int b;\n"
9838                "    }\n"
9839                "    if (true)\n"
9840                "      {\n"
9841                "        a();\n"
9842                "        b();\n"
9843                "      }\n"
9844                "  }\n"
9845                "  void g() { return; }\n"
9846                "}\n"
9847                "}",
9848                GNUBraceStyle);
9849 
9850   verifyFormat("void f()\n"
9851                "{\n"
9852                "  if (true)\n"
9853                "    {\n"
9854                "      a();\n"
9855                "    }\n"
9856                "  else if (false)\n"
9857                "    {\n"
9858                "      b();\n"
9859                "    }\n"
9860                "  else\n"
9861                "    {\n"
9862                "      c();\n"
9863                "    }\n"
9864                "}\n",
9865                GNUBraceStyle);
9866 
9867   verifyFormat("void f()\n"
9868                "{\n"
9869                "  for (int i = 0; i < 10; ++i)\n"
9870                "    {\n"
9871                "      a();\n"
9872                "    }\n"
9873                "  while (false)\n"
9874                "    {\n"
9875                "      b();\n"
9876                "    }\n"
9877                "  do\n"
9878                "    {\n"
9879                "      c();\n"
9880                "    }\n"
9881                "  while (false);\n"
9882                "}\n",
9883                GNUBraceStyle);
9884 
9885   verifyFormat("void f(int a)\n"
9886                "{\n"
9887                "  switch (a)\n"
9888                "    {\n"
9889                "    case 0:\n"
9890                "      break;\n"
9891                "    case 1:\n"
9892                "      {\n"
9893                "        break;\n"
9894                "      }\n"
9895                "    case 2:\n"
9896                "      {\n"
9897                "      }\n"
9898                "      break;\n"
9899                "    default:\n"
9900                "      break;\n"
9901                "    }\n"
9902                "}\n",
9903                GNUBraceStyle);
9904 
9905   verifyFormat("enum X\n"
9906                "{\n"
9907                "  Y = 0,\n"
9908                "}\n",
9909                GNUBraceStyle);
9910 
9911   verifyFormat("@interface BSApplicationController ()\n"
9912                "{\n"
9913                "@private\n"
9914                "  id _extraIvar;\n"
9915                "}\n"
9916                "@end\n",
9917                GNUBraceStyle);
9918 
9919   verifyFormat("#ifdef _DEBUG\n"
9920                "int foo(int i = 0)\n"
9921                "#else\n"
9922                "int foo(int i = 5)\n"
9923                "#endif\n"
9924                "{\n"
9925                "  return i;\n"
9926                "}",
9927                GNUBraceStyle);
9928 
9929   verifyFormat("void foo() {}\n"
9930                "void bar()\n"
9931                "#ifdef _DEBUG\n"
9932                "{\n"
9933                "  foo();\n"
9934                "}\n"
9935                "#else\n"
9936                "{\n"
9937                "}\n"
9938                "#endif",
9939                GNUBraceStyle);
9940 
9941   verifyFormat("void foobar() { int i = 5; }\n"
9942                "#ifdef _DEBUG\n"
9943                "void bar() {}\n"
9944                "#else\n"
9945                "void bar() { foobar(); }\n"
9946                "#endif",
9947                GNUBraceStyle);
9948 }
9949 
9950 TEST_F(FormatTest, WebKitBraceBreaking) {
9951   FormatStyle WebKitBraceStyle = getLLVMStyle();
9952   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9953   verifyFormat("namespace a {\n"
9954                "class A {\n"
9955                "  void f()\n"
9956                "  {\n"
9957                "    if (true) {\n"
9958                "      a();\n"
9959                "      b();\n"
9960                "    }\n"
9961                "  }\n"
9962                "  void g() { return; }\n"
9963                "};\n"
9964                "enum E {\n"
9965                "  A,\n"
9966                "  // foo\n"
9967                "  B,\n"
9968                "  C\n"
9969                "};\n"
9970                "struct B {\n"
9971                "  int x;\n"
9972                "};\n"
9973                "}\n",
9974                WebKitBraceStyle);
9975   verifyFormat("struct S {\n"
9976                "  int Type;\n"
9977                "  union {\n"
9978                "    int x;\n"
9979                "    double y;\n"
9980                "  } Value;\n"
9981                "  class C {\n"
9982                "    MyFavoriteType Value;\n"
9983                "  } Class;\n"
9984                "};\n",
9985                WebKitBraceStyle);
9986 }
9987 
9988 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9989   verifyFormat("void f() {\n"
9990                "  try {\n"
9991                "  } catch (const Exception &e) {\n"
9992                "  }\n"
9993                "}\n",
9994                getLLVMStyle());
9995 }
9996 
9997 TEST_F(FormatTest, UnderstandsPragmas) {
9998   verifyFormat("#pragma omp reduction(| : var)");
9999   verifyFormat("#pragma omp reduction(+ : var)");
10000 
10001   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10002             "(including parentheses).",
10003             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10004                    "(including parentheses)."));
10005 }
10006 
10007 TEST_F(FormatTest, UnderstandPragmaOption) {
10008   verifyFormat("#pragma option -C -A");
10009 
10010   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10011 }
10012 
10013 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10014   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10015   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10016                                   << " differs from Style #0"
10017 
10018 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10019   SmallVector<FormatStyle, 3> Styles;
10020   Styles.resize(3);
10021 
10022   Styles[0] = getLLVMStyle();
10023   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10024   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10025   EXPECT_ALL_STYLES_EQUAL(Styles);
10026 
10027   Styles[0] = getGoogleStyle();
10028   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10029   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10030   EXPECT_ALL_STYLES_EQUAL(Styles);
10031 
10032   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10033   EXPECT_TRUE(
10034       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10035   EXPECT_TRUE(
10036       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10037   EXPECT_ALL_STYLES_EQUAL(Styles);
10038 
10039   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10040   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10041   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10042   EXPECT_ALL_STYLES_EQUAL(Styles);
10043 
10044   Styles[0] = getMozillaStyle();
10045   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10046   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10047   EXPECT_ALL_STYLES_EQUAL(Styles);
10048 
10049   Styles[0] = getWebKitStyle();
10050   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10051   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10052   EXPECT_ALL_STYLES_EQUAL(Styles);
10053 
10054   Styles[0] = getGNUStyle();
10055   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10056   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10057   EXPECT_ALL_STYLES_EQUAL(Styles);
10058 
10059   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10060 }
10061 
10062 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10063   SmallVector<FormatStyle, 8> Styles;
10064   Styles.resize(2);
10065 
10066   Styles[0] = getGoogleStyle();
10067   Styles[1] = getLLVMStyle();
10068   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10069   EXPECT_ALL_STYLES_EQUAL(Styles);
10070 
10071   Styles.resize(5);
10072   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10073   Styles[1] = getLLVMStyle();
10074   Styles[1].Language = FormatStyle::LK_JavaScript;
10075   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10076 
10077   Styles[2] = getLLVMStyle();
10078   Styles[2].Language = FormatStyle::LK_JavaScript;
10079   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10080                                   "BasedOnStyle: Google",
10081                                   &Styles[2])
10082                    .value());
10083 
10084   Styles[3] = getLLVMStyle();
10085   Styles[3].Language = FormatStyle::LK_JavaScript;
10086   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10087                                   "Language: JavaScript",
10088                                   &Styles[3])
10089                    .value());
10090 
10091   Styles[4] = getLLVMStyle();
10092   Styles[4].Language = FormatStyle::LK_JavaScript;
10093   EXPECT_EQ(0, parseConfiguration("---\n"
10094                                   "BasedOnStyle: LLVM\n"
10095                                   "IndentWidth: 123\n"
10096                                   "---\n"
10097                                   "BasedOnStyle: Google\n"
10098                                   "Language: JavaScript",
10099                                   &Styles[4])
10100                    .value());
10101   EXPECT_ALL_STYLES_EQUAL(Styles);
10102 }
10103 
10104 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10105   Style.FIELD = false;                                                         \
10106   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10107   EXPECT_TRUE(Style.FIELD);                                                    \
10108   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10109   EXPECT_FALSE(Style.FIELD);
10110 
10111 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10112 
10113 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10114   Style.STRUCT.FIELD = false;                                                  \
10115   EXPECT_EQ(0,                                                                 \
10116             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10117                 .value());                                                     \
10118   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10119   EXPECT_EQ(0,                                                                 \
10120             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10121                 .value());                                                     \
10122   EXPECT_FALSE(Style.STRUCT.FIELD);
10123 
10124 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10125   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10126 
10127 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10128   EXPECT_NE(VALUE, Style.FIELD);                                               \
10129   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10130   EXPECT_EQ(VALUE, Style.FIELD)
10131 
10132 TEST_F(FormatTest, ParsesConfigurationBools) {
10133   FormatStyle Style = {};
10134   Style.Language = FormatStyle::LK_Cpp;
10135   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
10136   CHECK_PARSE_BOOL(AlignOperands);
10137   CHECK_PARSE_BOOL(AlignTrailingComments);
10138   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10139   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10140   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10141   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10142   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10143   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10144   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10145   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10146   CHECK_PARSE_BOOL(BinPackArguments);
10147   CHECK_PARSE_BOOL(BinPackParameters);
10148   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10149   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10150   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
10151   CHECK_PARSE_BOOL(BreakStringLiterals);
10152   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10153   CHECK_PARSE_BOOL(DerivePointerAlignment);
10154   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10155   CHECK_PARSE_BOOL(DisableFormat);
10156   CHECK_PARSE_BOOL(IndentCaseLabels);
10157   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10158   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10159   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10160   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10161   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10162   CHECK_PARSE_BOOL(ReflowComments);
10163   CHECK_PARSE_BOOL(SortIncludes);
10164   CHECK_PARSE_BOOL(SpacesInParentheses);
10165   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10166   CHECK_PARSE_BOOL(SpacesInAngles);
10167   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10168   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10169   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10170   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10171   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10172 
10173   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10174   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10175   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10176   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10177   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10178   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10179   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10180   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10181   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10182   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10183   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10184 }
10185 
10186 #undef CHECK_PARSE_BOOL
10187 
10188 TEST_F(FormatTest, ParsesConfiguration) {
10189   FormatStyle Style = {};
10190   Style.Language = FormatStyle::LK_Cpp;
10191   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10192   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10193               ConstructorInitializerIndentWidth, 1234u);
10194   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10195   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10196   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10197   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10198               PenaltyBreakBeforeFirstCallParameter, 1234u);
10199   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10200   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10201               PenaltyReturnTypeOnItsOwnLine, 1234u);
10202   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10203               SpacesBeforeTrailingComments, 1234u);
10204   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10205   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10206   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10207 
10208   Style.PointerAlignment = FormatStyle::PAS_Middle;
10209   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10210               FormatStyle::PAS_Left);
10211   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10212               FormatStyle::PAS_Right);
10213   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10214               FormatStyle::PAS_Middle);
10215   // For backward compatibility:
10216   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10217               FormatStyle::PAS_Left);
10218   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10219               FormatStyle::PAS_Right);
10220   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10221               FormatStyle::PAS_Middle);
10222 
10223   Style.Standard = FormatStyle::LS_Auto;
10224   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10225   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10226   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10227   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10228   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10229 
10230   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10231   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10232               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10233   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10234               FormatStyle::BOS_None);
10235   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10236               FormatStyle::BOS_All);
10237   // For backward compatibility:
10238   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10239               FormatStyle::BOS_None);
10240   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10241               FormatStyle::BOS_All);
10242 
10243   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10244   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10245               FormatStyle::BAS_Align);
10246   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10247               FormatStyle::BAS_DontAlign);
10248   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10249               FormatStyle::BAS_AlwaysBreak);
10250   // For backward compatibility:
10251   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10252               FormatStyle::BAS_DontAlign);
10253   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10254               FormatStyle::BAS_Align);
10255 
10256   Style.UseTab = FormatStyle::UT_ForIndentation;
10257   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10258   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10259   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10260   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10261               FormatStyle::UT_ForContinuationAndIndentation);
10262   // For backward compatibility:
10263   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10264   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10265 
10266   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10267   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10268               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10269   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10270               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10271   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10272               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10273   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10274               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10275   // For backward compatibility:
10276   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10277               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10278   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10279               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10280 
10281   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10282   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10283               FormatStyle::SBPO_Never);
10284   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10285               FormatStyle::SBPO_Always);
10286   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10287               FormatStyle::SBPO_ControlStatements);
10288   // For backward compatibility:
10289   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10290               FormatStyle::SBPO_Never);
10291   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10292               FormatStyle::SBPO_ControlStatements);
10293 
10294   Style.ColumnLimit = 123;
10295   FormatStyle BaseStyle = getLLVMStyle();
10296   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10297   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10298 
10299   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10300   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10301               FormatStyle::BS_Attach);
10302   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10303               FormatStyle::BS_Linux);
10304   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10305               FormatStyle::BS_Mozilla);
10306   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10307               FormatStyle::BS_Stroustrup);
10308   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10309               FormatStyle::BS_Allman);
10310   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10311   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10312               FormatStyle::BS_WebKit);
10313   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10314               FormatStyle::BS_Custom);
10315 
10316   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10317   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10318               FormatStyle::RTBS_None);
10319   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10320               FormatStyle::RTBS_All);
10321   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10322               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10323   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10324               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10325   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10326               AlwaysBreakAfterReturnType,
10327               FormatStyle::RTBS_TopLevelDefinitions);
10328 
10329   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10330   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10331               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10332   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10333               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10334   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10335               AlwaysBreakAfterDefinitionReturnType,
10336               FormatStyle::DRTBS_TopLevel);
10337 
10338   Style.NamespaceIndentation = FormatStyle::NI_All;
10339   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10340               FormatStyle::NI_None);
10341   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10342               FormatStyle::NI_Inner);
10343   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10344               FormatStyle::NI_All);
10345 
10346   // FIXME: This is required because parsing a configuration simply overwrites
10347   // the first N elements of the list instead of resetting it.
10348   Style.ForEachMacros.clear();
10349   std::vector<std::string> BoostForeach;
10350   BoostForeach.push_back("BOOST_FOREACH");
10351   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10352   std::vector<std::string> BoostAndQForeach;
10353   BoostAndQForeach.push_back("BOOST_FOREACH");
10354   BoostAndQForeach.push_back("Q_FOREACH");
10355   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10356               BoostAndQForeach);
10357 
10358   Style.IncludeCategories.clear();
10359   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10360                                                                   {".*", 1}};
10361   CHECK_PARSE("IncludeCategories:\n"
10362               "  - Regex: abc/.*\n"
10363               "    Priority: 2\n"
10364               "  - Regex: .*\n"
10365               "    Priority: 1",
10366               IncludeCategories, ExpectedCategories);
10367   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10368 }
10369 
10370 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10371   FormatStyle Style = {};
10372   Style.Language = FormatStyle::LK_Cpp;
10373   CHECK_PARSE("Language: Cpp\n"
10374               "IndentWidth: 12",
10375               IndentWidth, 12u);
10376   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10377                                "IndentWidth: 34",
10378                                &Style),
10379             ParseError::Unsuitable);
10380   EXPECT_EQ(12u, Style.IndentWidth);
10381   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10382   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10383 
10384   Style.Language = FormatStyle::LK_JavaScript;
10385   CHECK_PARSE("Language: JavaScript\n"
10386               "IndentWidth: 12",
10387               IndentWidth, 12u);
10388   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10389   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10390                                "IndentWidth: 34",
10391                                &Style),
10392             ParseError::Unsuitable);
10393   EXPECT_EQ(23u, Style.IndentWidth);
10394   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10395   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10396 
10397   CHECK_PARSE("BasedOnStyle: LLVM\n"
10398               "IndentWidth: 67",
10399               IndentWidth, 67u);
10400 
10401   CHECK_PARSE("---\n"
10402               "Language: JavaScript\n"
10403               "IndentWidth: 12\n"
10404               "---\n"
10405               "Language: Cpp\n"
10406               "IndentWidth: 34\n"
10407               "...\n",
10408               IndentWidth, 12u);
10409 
10410   Style.Language = FormatStyle::LK_Cpp;
10411   CHECK_PARSE("---\n"
10412               "Language: JavaScript\n"
10413               "IndentWidth: 12\n"
10414               "---\n"
10415               "Language: Cpp\n"
10416               "IndentWidth: 34\n"
10417               "...\n",
10418               IndentWidth, 34u);
10419   CHECK_PARSE("---\n"
10420               "IndentWidth: 78\n"
10421               "---\n"
10422               "Language: JavaScript\n"
10423               "IndentWidth: 56\n"
10424               "...\n",
10425               IndentWidth, 78u);
10426 
10427   Style.ColumnLimit = 123;
10428   Style.IndentWidth = 234;
10429   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10430   Style.TabWidth = 345;
10431   EXPECT_FALSE(parseConfiguration("---\n"
10432                                   "IndentWidth: 456\n"
10433                                   "BreakBeforeBraces: Allman\n"
10434                                   "---\n"
10435                                   "Language: JavaScript\n"
10436                                   "IndentWidth: 111\n"
10437                                   "TabWidth: 111\n"
10438                                   "---\n"
10439                                   "Language: Cpp\n"
10440                                   "BreakBeforeBraces: Stroustrup\n"
10441                                   "TabWidth: 789\n"
10442                                   "...\n",
10443                                   &Style));
10444   EXPECT_EQ(123u, Style.ColumnLimit);
10445   EXPECT_EQ(456u, Style.IndentWidth);
10446   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10447   EXPECT_EQ(789u, Style.TabWidth);
10448 
10449   EXPECT_EQ(parseConfiguration("---\n"
10450                                "Language: JavaScript\n"
10451                                "IndentWidth: 56\n"
10452                                "---\n"
10453                                "IndentWidth: 78\n"
10454                                "...\n",
10455                                &Style),
10456             ParseError::Error);
10457   EXPECT_EQ(parseConfiguration("---\n"
10458                                "Language: JavaScript\n"
10459                                "IndentWidth: 56\n"
10460                                "---\n"
10461                                "Language: JavaScript\n"
10462                                "IndentWidth: 78\n"
10463                                "...\n",
10464                                &Style),
10465             ParseError::Error);
10466 
10467   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10468 }
10469 
10470 #undef CHECK_PARSE
10471 
10472 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10473   FormatStyle Style = {};
10474   Style.Language = FormatStyle::LK_JavaScript;
10475   Style.BreakBeforeTernaryOperators = true;
10476   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10477   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10478 
10479   Style.BreakBeforeTernaryOperators = true;
10480   EXPECT_EQ(0, parseConfiguration("---\n"
10481                                   "BasedOnStyle: Google\n"
10482                                   "---\n"
10483                                   "Language: JavaScript\n"
10484                                   "IndentWidth: 76\n"
10485                                   "...\n",
10486                                   &Style)
10487                    .value());
10488   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10489   EXPECT_EQ(76u, Style.IndentWidth);
10490   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10491 }
10492 
10493 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10494   FormatStyle Style = getLLVMStyle();
10495   std::string YAML = configurationAsText(Style);
10496   FormatStyle ParsedStyle = {};
10497   ParsedStyle.Language = FormatStyle::LK_Cpp;
10498   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10499   EXPECT_EQ(Style, ParsedStyle);
10500 }
10501 
10502 TEST_F(FormatTest, WorksFor8bitEncodings) {
10503   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10504             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10505             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10506             "\"\xef\xee\xf0\xf3...\"",
10507             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10508                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10509                    "\xef\xee\xf0\xf3...\"",
10510                    getLLVMStyleWithColumns(12)));
10511 }
10512 
10513 TEST_F(FormatTest, HandlesUTF8BOM) {
10514   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10515   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10516             format("\xef\xbb\xbf#include <iostream>"));
10517   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10518             format("\xef\xbb\xbf\n#include <iostream>"));
10519 }
10520 
10521 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10522 #if !defined(_MSC_VER)
10523 
10524 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10525   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10526                getLLVMStyleWithColumns(35));
10527   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10528                getLLVMStyleWithColumns(31));
10529   verifyFormat("// Однажды в студёную зимнюю пору...",
10530                getLLVMStyleWithColumns(36));
10531   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10532   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10533                getLLVMStyleWithColumns(39));
10534   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10535                getLLVMStyleWithColumns(35));
10536 }
10537 
10538 TEST_F(FormatTest, SplitsUTF8Strings) {
10539   // Non-printable characters' width is currently considered to be the length in
10540   // bytes in UTF8. The characters can be displayed in very different manner
10541   // (zero-width, single width with a substitution glyph, expanded to their code
10542   // (e.g. "<8d>"), so there's no single correct way to handle them.
10543   EXPECT_EQ("\"aaaaÄ\"\n"
10544             "\"\xc2\x8d\";",
10545             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10546   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10547             "\"\xc2\x8d\";",
10548             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10549   EXPECT_EQ("\"Однажды, в \"\n"
10550             "\"студёную \"\n"
10551             "\"зимнюю \"\n"
10552             "\"пору,\"",
10553             format("\"Однажды, в студёную зимнюю пору,\"",
10554                    getLLVMStyleWithColumns(13)));
10555   EXPECT_EQ(
10556       "\"一 二 三 \"\n"
10557       "\"四 五六 \"\n"
10558       "\"七 八 九 \"\n"
10559       "\"十\"",
10560       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10561   EXPECT_EQ("\"一\t二 \"\n"
10562             "\"\t三 \"\n"
10563             "\"四 五\t六 \"\n"
10564             "\"\t七 \"\n"
10565             "\"八九十\tqq\"",
10566             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10567                    getLLVMStyleWithColumns(11)));
10568 
10569   // UTF8 character in an escape sequence.
10570   EXPECT_EQ("\"aaaaaa\"\n"
10571             "\"\\\xC2\x8D\"",
10572             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10573 }
10574 
10575 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10576   EXPECT_EQ("const char *sssss =\n"
10577             "    \"一二三四五六七八\\\n"
10578             " 九 十\";",
10579             format("const char *sssss = \"一二三四五六七八\\\n"
10580                    " 九 十\";",
10581                    getLLVMStyleWithColumns(30)));
10582 }
10583 
10584 TEST_F(FormatTest, SplitsUTF8LineComments) {
10585   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10586             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10587   EXPECT_EQ("// Я из лесу\n"
10588             "// вышел; был\n"
10589             "// сильный\n"
10590             "// мороз.",
10591             format("// Я из лесу вышел; был сильный мороз.",
10592                    getLLVMStyleWithColumns(13)));
10593   EXPECT_EQ("// 一二三\n"
10594             "// 四五六七\n"
10595             "// 八  九\n"
10596             "// 十",
10597             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10598 }
10599 
10600 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10601   EXPECT_EQ("/* Гляжу,\n"
10602             " * поднимается\n"
10603             " * медленно в\n"
10604             " * гору\n"
10605             " * Лошадка,\n"
10606             " * везущая\n"
10607             " * хворосту\n"
10608             " * воз. */",
10609             format("/* Гляжу, поднимается медленно в гору\n"
10610                    " * Лошадка, везущая хворосту воз. */",
10611                    getLLVMStyleWithColumns(13)));
10612   EXPECT_EQ(
10613       "/* 一二三\n"
10614       " * 四五六七\n"
10615       " * 八  九\n"
10616       " * 十  */",
10617       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10618   EXPECT_EQ("/* �������� ��������\n"
10619             " * ��������\n"
10620             " * ������-�� */",
10621             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10622 }
10623 
10624 #endif // _MSC_VER
10625 
10626 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10627   FormatStyle Style = getLLVMStyle();
10628 
10629   Style.ConstructorInitializerIndentWidth = 4;
10630   verifyFormat(
10631       "SomeClass::Constructor()\n"
10632       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10633       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10634       Style);
10635 
10636   Style.ConstructorInitializerIndentWidth = 2;
10637   verifyFormat(
10638       "SomeClass::Constructor()\n"
10639       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10640       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10641       Style);
10642 
10643   Style.ConstructorInitializerIndentWidth = 0;
10644   verifyFormat(
10645       "SomeClass::Constructor()\n"
10646       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10647       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10648       Style);
10649   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10650   verifyFormat(
10651       "SomeLongTemplateVariableName<\n"
10652       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10653       Style);
10654   verifyFormat(
10655       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10656       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10657       Style);
10658 }
10659 
10660 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10661   FormatStyle Style = getLLVMStyle();
10662   Style.BreakConstructorInitializersBeforeComma = true;
10663   Style.ConstructorInitializerIndentWidth = 4;
10664   verifyFormat("SomeClass::Constructor()\n"
10665                "    : a(a)\n"
10666                "    , b(b)\n"
10667                "    , c(c) {}",
10668                Style);
10669   verifyFormat("SomeClass::Constructor()\n"
10670                "    : a(a) {}",
10671                Style);
10672 
10673   Style.ColumnLimit = 0;
10674   verifyFormat("SomeClass::Constructor()\n"
10675                "    : a(a) {}",
10676                Style);
10677   verifyFormat("SomeClass::Constructor() noexcept\n"
10678                "    : a(a) {}",
10679                Style);
10680   verifyFormat("SomeClass::Constructor()\n"
10681                "    : a(a)\n"
10682                "    , b(b)\n"
10683                "    , c(c) {}",
10684                Style);
10685   verifyFormat("SomeClass::Constructor()\n"
10686                "    : a(a) {\n"
10687                "  foo();\n"
10688                "  bar();\n"
10689                "}",
10690                Style);
10691 
10692   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10693   verifyFormat("SomeClass::Constructor()\n"
10694                "    : a(a)\n"
10695                "    , b(b)\n"
10696                "    , c(c) {\n}",
10697                Style);
10698   verifyFormat("SomeClass::Constructor()\n"
10699                "    : a(a) {\n}",
10700                Style);
10701 
10702   Style.ColumnLimit = 80;
10703   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10704   Style.ConstructorInitializerIndentWidth = 2;
10705   verifyFormat("SomeClass::Constructor()\n"
10706                "  : a(a)\n"
10707                "  , b(b)\n"
10708                "  , c(c) {}",
10709                Style);
10710 
10711   Style.ConstructorInitializerIndentWidth = 0;
10712   verifyFormat("SomeClass::Constructor()\n"
10713                ": a(a)\n"
10714                ", b(b)\n"
10715                ", c(c) {}",
10716                Style);
10717 
10718   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10719   Style.ConstructorInitializerIndentWidth = 4;
10720   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10721   verifyFormat(
10722       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10723       Style);
10724   verifyFormat(
10725       "SomeClass::Constructor()\n"
10726       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10727       Style);
10728   Style.ConstructorInitializerIndentWidth = 4;
10729   Style.ColumnLimit = 60;
10730   verifyFormat("SomeClass::Constructor()\n"
10731                "    : aaaaaaaa(aaaaaaaa)\n"
10732                "    , aaaaaaaa(aaaaaaaa)\n"
10733                "    , aaaaaaaa(aaaaaaaa) {}",
10734                Style);
10735 }
10736 
10737 TEST_F(FormatTest, Destructors) {
10738   verifyFormat("void F(int &i) { i.~int(); }");
10739   verifyFormat("void F(int &i) { i->~int(); }");
10740 }
10741 
10742 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10743   FormatStyle Style = getWebKitStyle();
10744 
10745   // Don't indent in outer namespaces.
10746   verifyFormat("namespace outer {\n"
10747                "int i;\n"
10748                "namespace inner {\n"
10749                "    int i;\n"
10750                "} // namespace inner\n"
10751                "} // namespace outer\n"
10752                "namespace other_outer {\n"
10753                "int i;\n"
10754                "}",
10755                Style);
10756 
10757   // Don't indent case labels.
10758   verifyFormat("switch (variable) {\n"
10759                "case 1:\n"
10760                "case 2:\n"
10761                "    doSomething();\n"
10762                "    break;\n"
10763                "default:\n"
10764                "    ++variable;\n"
10765                "}",
10766                Style);
10767 
10768   // Wrap before binary operators.
10769   EXPECT_EQ("void f()\n"
10770             "{\n"
10771             "    if (aaaaaaaaaaaaaaaa\n"
10772             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10773             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10774             "        return;\n"
10775             "}",
10776             format("void f() {\n"
10777                    "if (aaaaaaaaaaaaaaaa\n"
10778                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10779                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10780                    "return;\n"
10781                    "}",
10782                    Style));
10783 
10784   // Allow functions on a single line.
10785   verifyFormat("void f() { return; }", Style);
10786 
10787   // Constructor initializers are formatted one per line with the "," on the
10788   // new line.
10789   verifyFormat("Constructor()\n"
10790                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10791                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10792                "          aaaaaaaaaaaaaa)\n"
10793                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10794                "{\n"
10795                "}",
10796                Style);
10797   verifyFormat("SomeClass::Constructor()\n"
10798                "    : a(a)\n"
10799                "{\n"
10800                "}",
10801                Style);
10802   EXPECT_EQ("SomeClass::Constructor()\n"
10803             "    : a(a)\n"
10804             "{\n"
10805             "}",
10806             format("SomeClass::Constructor():a(a){}", Style));
10807   verifyFormat("SomeClass::Constructor()\n"
10808                "    : a(a)\n"
10809                "    , b(b)\n"
10810                "    , c(c)\n"
10811                "{\n"
10812                "}",
10813                Style);
10814   verifyFormat("SomeClass::Constructor()\n"
10815                "    : a(a)\n"
10816                "{\n"
10817                "    foo();\n"
10818                "    bar();\n"
10819                "}",
10820                Style);
10821 
10822   // Access specifiers should be aligned left.
10823   verifyFormat("class C {\n"
10824                "public:\n"
10825                "    int i;\n"
10826                "};",
10827                Style);
10828 
10829   // Do not align comments.
10830   verifyFormat("int a; // Do not\n"
10831                "double b; // align comments.",
10832                Style);
10833 
10834   // Do not align operands.
10835   EXPECT_EQ("ASSERT(aaaa\n"
10836             "    || bbbb);",
10837             format("ASSERT ( aaaa\n||bbbb);", Style));
10838 
10839   // Accept input's line breaks.
10840   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10841             "    || bbbbbbbbbbbbbbb) {\n"
10842             "    i++;\n"
10843             "}",
10844             format("if (aaaaaaaaaaaaaaa\n"
10845                    "|| bbbbbbbbbbbbbbb) { i++; }",
10846                    Style));
10847   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10848             "    i++;\n"
10849             "}",
10850             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10851 
10852   // Don't automatically break all macro definitions (llvm.org/PR17842).
10853   verifyFormat("#define aNumber 10", Style);
10854   // However, generally keep the line breaks that the user authored.
10855   EXPECT_EQ("#define aNumber \\\n"
10856             "    10",
10857             format("#define aNumber \\\n"
10858                    " 10",
10859                    Style));
10860 
10861   // Keep empty and one-element array literals on a single line.
10862   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10863             "                                  copyItems:YES];",
10864             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10865                    "copyItems:YES];",
10866                    Style));
10867   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10868             "                                  copyItems:YES];",
10869             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10870                    "             copyItems:YES];",
10871                    Style));
10872   // FIXME: This does not seem right, there should be more indentation before
10873   // the array literal's entries. Nested blocks have the same problem.
10874   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10875             "    @\"a\",\n"
10876             "    @\"a\"\n"
10877             "]\n"
10878             "                                  copyItems:YES];",
10879             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10880                    "     @\"a\",\n"
10881                    "     @\"a\"\n"
10882                    "     ]\n"
10883                    "       copyItems:YES];",
10884                    Style));
10885   EXPECT_EQ(
10886       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10887       "                                  copyItems:YES];",
10888       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10889              "   copyItems:YES];",
10890              Style));
10891 
10892   verifyFormat("[self.a b:c c:d];", Style);
10893   EXPECT_EQ("[self.a b:c\n"
10894             "        c:d];",
10895             format("[self.a b:c\n"
10896                    "c:d];",
10897                    Style));
10898 }
10899 
10900 TEST_F(FormatTest, FormatsLambdas) {
10901   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10902   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10903   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10904   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10905   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10906   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10907   verifyFormat("void f() {\n"
10908                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10909                "}\n");
10910   verifyFormat("void f() {\n"
10911                "  other(x.begin(), //\n"
10912                "        x.end(),   //\n"
10913                "        [&](int, int) { return 1; });\n"
10914                "}\n");
10915   verifyFormat("SomeFunction([]() { // A cool function...\n"
10916                "  return 43;\n"
10917                "});");
10918   EXPECT_EQ("SomeFunction([]() {\n"
10919             "#define A a\n"
10920             "  return 43;\n"
10921             "});",
10922             format("SomeFunction([](){\n"
10923                    "#define A a\n"
10924                    "return 43;\n"
10925                    "});"));
10926   verifyFormat("void f() {\n"
10927                "  SomeFunction([](decltype(x), A *a) {});\n"
10928                "}");
10929   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10930                "    [](const aaaaaaaaaa &a) { return a; });");
10931   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10932                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10933                "});");
10934   verifyFormat("Constructor()\n"
10935                "    : Field([] { // comment\n"
10936                "        int i;\n"
10937                "      }) {}");
10938   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10939                "  return some_parameter.size();\n"
10940                "};");
10941   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10942                "    [](const string &s) { return s; };");
10943   verifyFormat("int i = aaaaaa ? 1 //\n"
10944                "               : [] {\n"
10945                "                   return 2; //\n"
10946                "                 }();");
10947   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10948                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10949                "                  return x == 2; // force break\n"
10950                "                });");
10951   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10952                "    int iiiiiiiiiiii) {\n"
10953                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10954                "});",
10955                getLLVMStyleWithColumns(60));
10956   verifyFormat("SomeFunction({[&] {\n"
10957                "                // comment\n"
10958                "              },\n"
10959                "              [&] {\n"
10960                "                // comment\n"
10961                "              }});");
10962   verifyFormat("SomeFunction({[&] {\n"
10963                "  // comment\n"
10964                "}});");
10965   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10966                "                             [&]() { return true; },\n"
10967                "                         aaaaa aaaaaaaaa);");
10968 
10969   // Lambdas with return types.
10970   verifyFormat("int c = []() -> int { return 2; }();\n");
10971   verifyFormat("int c = []() -> int * { return 2; }();\n");
10972   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10973   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10974   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10975   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10976   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10977   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10978   verifyFormat("[a, a]() -> a<1> {};");
10979   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10980                "                   int j) -> int {\n"
10981                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10982                "};");
10983   verifyFormat(
10984       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10985       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10986       "      return aaaaaaaaaaaaaaaaa;\n"
10987       "    });",
10988       getLLVMStyleWithColumns(70));
10989 
10990   // Multiple lambdas in the same parentheses change indentation rules.
10991   verifyFormat("SomeFunction(\n"
10992                "    []() {\n"
10993                "      int i = 42;\n"
10994                "      return i;\n"
10995                "    },\n"
10996                "    []() {\n"
10997                "      int j = 43;\n"
10998                "      return j;\n"
10999                "    });");
11000 
11001   // More complex introducers.
11002   verifyFormat("return [i, args...] {};");
11003 
11004   // Not lambdas.
11005   verifyFormat("constexpr char hello[]{\"hello\"};");
11006   verifyFormat("double &operator[](int i) { return 0; }\n"
11007                "int i;");
11008   verifyFormat("std::unique_ptr<int[]> foo() {}");
11009   verifyFormat("int i = a[a][a]->f();");
11010   verifyFormat("int i = (*b)[a]->f();");
11011 
11012   // Other corner cases.
11013   verifyFormat("void f() {\n"
11014                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11015                "      );\n"
11016                "}");
11017 
11018   // Lambdas created through weird macros.
11019   verifyFormat("void f() {\n"
11020                "  MACRO((const AA &a) { return 1; });\n"
11021                "  MACRO((AA &a) { return 1; });\n"
11022                "}");
11023 
11024   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11025                "      doo_dah();\n"
11026                "      doo_dah();\n"
11027                "    })) {\n"
11028                "}");
11029   verifyFormat("auto lambda = []() {\n"
11030                "  int a = 2\n"
11031                "#if A\n"
11032                "          + 2\n"
11033                "#endif\n"
11034                "      ;\n"
11035                "};");
11036 }
11037 
11038 TEST_F(FormatTest, FormatsBlocks) {
11039   FormatStyle ShortBlocks = getLLVMStyle();
11040   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11041   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11042   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11043   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11044   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11045   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11046   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11047 
11048   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11049   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11050   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11051 
11052   verifyFormat("[operation setCompletionBlock:^{\n"
11053                "  [self onOperationDone];\n"
11054                "}];");
11055   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11056                "  [self onOperationDone];\n"
11057                "}]};");
11058   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11059                "  f();\n"
11060                "}];");
11061   verifyFormat("int a = [operation block:^int(int *i) {\n"
11062                "  return 1;\n"
11063                "}];");
11064   verifyFormat("[myObject doSomethingWith:arg1\n"
11065                "                      aaa:^int(int *a) {\n"
11066                "                        return 1;\n"
11067                "                      }\n"
11068                "                      bbb:f(a * bbbbbbbb)];");
11069 
11070   verifyFormat("[operation setCompletionBlock:^{\n"
11071                "  [self.delegate newDataAvailable];\n"
11072                "}];",
11073                getLLVMStyleWithColumns(60));
11074   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11075                "  NSString *path = [self sessionFilePath];\n"
11076                "  if (path) {\n"
11077                "    // ...\n"
11078                "  }\n"
11079                "});");
11080   verifyFormat("[[SessionService sharedService]\n"
11081                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11082                "      if (window) {\n"
11083                "        [self windowDidLoad:window];\n"
11084                "      } else {\n"
11085                "        [self errorLoadingWindow];\n"
11086                "      }\n"
11087                "    }];");
11088   verifyFormat("void (^largeBlock)(void) = ^{\n"
11089                "  // ...\n"
11090                "};\n",
11091                getLLVMStyleWithColumns(40));
11092   verifyFormat("[[SessionService sharedService]\n"
11093                "    loadWindowWithCompletionBlock: //\n"
11094                "        ^(SessionWindow *window) {\n"
11095                "          if (window) {\n"
11096                "            [self windowDidLoad:window];\n"
11097                "          } else {\n"
11098                "            [self errorLoadingWindow];\n"
11099                "          }\n"
11100                "        }];",
11101                getLLVMStyleWithColumns(60));
11102   verifyFormat("[myObject doSomethingWith:arg1\n"
11103                "    firstBlock:^(Foo *a) {\n"
11104                "      // ...\n"
11105                "      int i;\n"
11106                "    }\n"
11107                "    secondBlock:^(Bar *b) {\n"
11108                "      // ...\n"
11109                "      int i;\n"
11110                "    }\n"
11111                "    thirdBlock:^Foo(Bar *b) {\n"
11112                "      // ...\n"
11113                "      int i;\n"
11114                "    }];");
11115   verifyFormat("[myObject doSomethingWith:arg1\n"
11116                "               firstBlock:-1\n"
11117                "              secondBlock:^(Bar *b) {\n"
11118                "                // ...\n"
11119                "                int i;\n"
11120                "              }];");
11121 
11122   verifyFormat("f(^{\n"
11123                "  @autoreleasepool {\n"
11124                "    if (a) {\n"
11125                "      g();\n"
11126                "    }\n"
11127                "  }\n"
11128                "});");
11129   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11130 
11131   FormatStyle FourIndent = getLLVMStyle();
11132   FourIndent.ObjCBlockIndentWidth = 4;
11133   verifyFormat("[operation setCompletionBlock:^{\n"
11134                "    [self onOperationDone];\n"
11135                "}];",
11136                FourIndent);
11137 }
11138 
11139 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11140   FormatStyle ZeroColumn = getLLVMStyle();
11141   ZeroColumn.ColumnLimit = 0;
11142 
11143   verifyFormat("[[SessionService sharedService] "
11144                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11145                "  if (window) {\n"
11146                "    [self windowDidLoad:window];\n"
11147                "  } else {\n"
11148                "    [self errorLoadingWindow];\n"
11149                "  }\n"
11150                "}];",
11151                ZeroColumn);
11152   EXPECT_EQ("[[SessionService sharedService]\n"
11153             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11154             "      if (window) {\n"
11155             "        [self windowDidLoad:window];\n"
11156             "      } else {\n"
11157             "        [self errorLoadingWindow];\n"
11158             "      }\n"
11159             "    }];",
11160             format("[[SessionService sharedService]\n"
11161                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11162                    "                if (window) {\n"
11163                    "    [self windowDidLoad:window];\n"
11164                    "  } else {\n"
11165                    "    [self errorLoadingWindow];\n"
11166                    "  }\n"
11167                    "}];",
11168                    ZeroColumn));
11169   verifyFormat("[myObject doSomethingWith:arg1\n"
11170                "    firstBlock:^(Foo *a) {\n"
11171                "      // ...\n"
11172                "      int i;\n"
11173                "    }\n"
11174                "    secondBlock:^(Bar *b) {\n"
11175                "      // ...\n"
11176                "      int i;\n"
11177                "    }\n"
11178                "    thirdBlock:^Foo(Bar *b) {\n"
11179                "      // ...\n"
11180                "      int i;\n"
11181                "    }];",
11182                ZeroColumn);
11183   verifyFormat("f(^{\n"
11184                "  @autoreleasepool {\n"
11185                "    if (a) {\n"
11186                "      g();\n"
11187                "    }\n"
11188                "  }\n"
11189                "});",
11190                ZeroColumn);
11191   verifyFormat("void (^largeBlock)(void) = ^{\n"
11192                "  // ...\n"
11193                "};",
11194                ZeroColumn);
11195 
11196   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11197   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11198             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11199   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11200   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11201             "  int i;\n"
11202             "};",
11203             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11204 }
11205 
11206 TEST_F(FormatTest, SupportsCRLF) {
11207   EXPECT_EQ("int a;\r\n"
11208             "int b;\r\n"
11209             "int c;\r\n",
11210             format("int a;\r\n"
11211                    "  int b;\r\n"
11212                    "    int c;\r\n",
11213                    getLLVMStyle()));
11214   EXPECT_EQ("int a;\r\n"
11215             "int b;\r\n"
11216             "int c;\r\n",
11217             format("int a;\r\n"
11218                    "  int b;\n"
11219                    "    int c;\r\n",
11220                    getLLVMStyle()));
11221   EXPECT_EQ("int a;\n"
11222             "int b;\n"
11223             "int c;\n",
11224             format("int a;\r\n"
11225                    "  int b;\n"
11226                    "    int c;\n",
11227                    getLLVMStyle()));
11228   EXPECT_EQ("\"aaaaaaa \"\r\n"
11229             "\"bbbbbbb\";\r\n",
11230             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11231   EXPECT_EQ("#define A \\\r\n"
11232             "  b;      \\\r\n"
11233             "  c;      \\\r\n"
11234             "  d;\r\n",
11235             format("#define A \\\r\n"
11236                    "  b; \\\r\n"
11237                    "  c; d; \r\n",
11238                    getGoogleStyle()));
11239 
11240   EXPECT_EQ("/*\r\n"
11241             "multi line block comments\r\n"
11242             "should not introduce\r\n"
11243             "an extra carriage return\r\n"
11244             "*/\r\n",
11245             format("/*\r\n"
11246                    "multi line block comments\r\n"
11247                    "should not introduce\r\n"
11248                    "an extra carriage return\r\n"
11249                    "*/\r\n"));
11250 }
11251 
11252 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11253   verifyFormat("MY_CLASS(C) {\n"
11254                "  int i;\n"
11255                "  int j;\n"
11256                "};");
11257 }
11258 
11259 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11260   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11261   TwoIndent.ContinuationIndentWidth = 2;
11262 
11263   EXPECT_EQ("int i =\n"
11264             "  longFunction(\n"
11265             "    arg);",
11266             format("int i = longFunction(arg);", TwoIndent));
11267 
11268   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11269   SixIndent.ContinuationIndentWidth = 6;
11270 
11271   EXPECT_EQ("int i =\n"
11272             "      longFunction(\n"
11273             "            arg);",
11274             format("int i = longFunction(arg);", SixIndent));
11275 }
11276 
11277 TEST_F(FormatTest, SpacesInAngles) {
11278   FormatStyle Spaces = getLLVMStyle();
11279   Spaces.SpacesInAngles = true;
11280 
11281   verifyFormat("static_cast< int >(arg);", Spaces);
11282   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11283   verifyFormat("f< int, float >();", Spaces);
11284   verifyFormat("template <> g() {}", Spaces);
11285   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11286   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11287   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11288                Spaces);
11289 
11290   Spaces.Standard = FormatStyle::LS_Cpp03;
11291   Spaces.SpacesInAngles = true;
11292   verifyFormat("A< A< int > >();", Spaces);
11293 
11294   Spaces.SpacesInAngles = false;
11295   verifyFormat("A<A<int> >();", Spaces);
11296 
11297   Spaces.Standard = FormatStyle::LS_Cpp11;
11298   Spaces.SpacesInAngles = true;
11299   verifyFormat("A< A< int > >();", Spaces);
11300 
11301   Spaces.SpacesInAngles = false;
11302   verifyFormat("A<A<int>>();", Spaces);
11303 }
11304 
11305 TEST_F(FormatTest, TripleAngleBrackets) {
11306   verifyFormat("f<<<1, 1>>>();");
11307   verifyFormat("f<<<1, 1, 1, s>>>();");
11308   verifyFormat("f<<<a, b, c, d>>>();");
11309   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11310   verifyFormat("f<param><<<1, 1>>>();");
11311   verifyFormat("f<1><<<1, 1>>>();");
11312   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11313   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11314                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11315 }
11316 
11317 TEST_F(FormatTest, MergeLessLessAtEnd) {
11318   verifyFormat("<<");
11319   EXPECT_EQ("< < <", format("\\\n<<<"));
11320   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11321                "aaallvm::outs() <<");
11322   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11323                "aaaallvm::outs()\n    <<");
11324 }
11325 
11326 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11327   std::string code = "#if A\n"
11328                      "#if B\n"
11329                      "a.\n"
11330                      "#endif\n"
11331                      "    a = 1;\n"
11332                      "#else\n"
11333                      "#endif\n"
11334                      "#if C\n"
11335                      "#else\n"
11336                      "#endif\n";
11337   EXPECT_EQ(code, format(code));
11338 }
11339 
11340 TEST_F(FormatTest, HandleConflictMarkers) {
11341   // Git/SVN conflict markers.
11342   EXPECT_EQ("int a;\n"
11343             "void f() {\n"
11344             "  callme(some(parameter1,\n"
11345             "<<<<<<< text by the vcs\n"
11346             "              parameter2),\n"
11347             "||||||| text by the vcs\n"
11348             "              parameter2),\n"
11349             "         parameter3,\n"
11350             "======= text by the vcs\n"
11351             "              parameter2, parameter3),\n"
11352             ">>>>>>> text by the vcs\n"
11353             "         otherparameter);\n",
11354             format("int a;\n"
11355                    "void f() {\n"
11356                    "  callme(some(parameter1,\n"
11357                    "<<<<<<< text by the vcs\n"
11358                    "  parameter2),\n"
11359                    "||||||| text by the vcs\n"
11360                    "  parameter2),\n"
11361                    "  parameter3,\n"
11362                    "======= text by the vcs\n"
11363                    "  parameter2,\n"
11364                    "  parameter3),\n"
11365                    ">>>>>>> text by the vcs\n"
11366                    "  otherparameter);\n"));
11367 
11368   // Perforce markers.
11369   EXPECT_EQ("void f() {\n"
11370             "  function(\n"
11371             ">>>> text by the vcs\n"
11372             "      parameter,\n"
11373             "==== text by the vcs\n"
11374             "      parameter,\n"
11375             "==== text by the vcs\n"
11376             "      parameter,\n"
11377             "<<<< text by the vcs\n"
11378             "      parameter);\n",
11379             format("void f() {\n"
11380                    "  function(\n"
11381                    ">>>> text by the vcs\n"
11382                    "  parameter,\n"
11383                    "==== text by the vcs\n"
11384                    "  parameter,\n"
11385                    "==== text by the vcs\n"
11386                    "  parameter,\n"
11387                    "<<<< text by the vcs\n"
11388                    "  parameter);\n"));
11389 
11390   EXPECT_EQ("<<<<<<<\n"
11391             "|||||||\n"
11392             "=======\n"
11393             ">>>>>>>",
11394             format("<<<<<<<\n"
11395                    "|||||||\n"
11396                    "=======\n"
11397                    ">>>>>>>"));
11398 
11399   EXPECT_EQ("<<<<<<<\n"
11400             "|||||||\n"
11401             "int i;\n"
11402             "=======\n"
11403             ">>>>>>>",
11404             format("<<<<<<<\n"
11405                    "|||||||\n"
11406                    "int i;\n"
11407                    "=======\n"
11408                    ">>>>>>>"));
11409 
11410   // FIXME: Handle parsing of macros around conflict markers correctly:
11411   EXPECT_EQ("#define Macro \\\n"
11412             "<<<<<<<\n"
11413             "Something \\\n"
11414             "|||||||\n"
11415             "Else \\\n"
11416             "=======\n"
11417             "Other \\\n"
11418             ">>>>>>>\n"
11419             "    End int i;\n",
11420             format("#define Macro \\\n"
11421                    "<<<<<<<\n"
11422                    "  Something \\\n"
11423                    "|||||||\n"
11424                    "  Else \\\n"
11425                    "=======\n"
11426                    "  Other \\\n"
11427                    ">>>>>>>\n"
11428                    "  End\n"
11429                    "int i;\n"));
11430 }
11431 
11432 TEST_F(FormatTest, DisableRegions) {
11433   EXPECT_EQ("int i;\n"
11434             "// clang-format off\n"
11435             "  int j;\n"
11436             "// clang-format on\n"
11437             "int k;",
11438             format(" int  i;\n"
11439                    "   // clang-format off\n"
11440                    "  int j;\n"
11441                    " // clang-format on\n"
11442                    "   int   k;"));
11443   EXPECT_EQ("int i;\n"
11444             "/* clang-format off */\n"
11445             "  int j;\n"
11446             "/* clang-format on */\n"
11447             "int k;",
11448             format(" int  i;\n"
11449                    "   /* clang-format off */\n"
11450                    "  int j;\n"
11451                    " /* clang-format on */\n"
11452                    "   int   k;"));
11453 }
11454 
11455 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11456   format("? ) =");
11457   verifyNoCrash("#define a\\\n /**/}");
11458 }
11459 
11460 TEST_F(FormatTest, FormatsTableGenCode) {
11461   FormatStyle Style = getLLVMStyle();
11462   Style.Language = FormatStyle::LK_TableGen;
11463   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11464 }
11465 
11466 // Since this test case uses UNIX-style file path. We disable it for MS
11467 // compiler.
11468 #if !defined(_MSC_VER) && !defined(__MINGW32__)
11469 
11470 TEST(FormatStyle, GetStyleOfFile) {
11471   vfs::InMemoryFileSystem FS;
11472   // Test 1: format file in the same directory.
11473   ASSERT_TRUE(
11474       FS.addFile("/a/.clang-format", 0,
11475                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11476   ASSERT_TRUE(
11477       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11478   auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
11479   ASSERT_EQ(Style1, getLLVMStyle());
11480 
11481   // Test 2: fallback to default.
11482   ASSERT_TRUE(
11483       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11484   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
11485   ASSERT_EQ(Style2, getMozillaStyle());
11486 
11487   // Test 3: format file in parent directory.
11488   ASSERT_TRUE(
11489       FS.addFile("/c/.clang-format", 0,
11490                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11491   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11492                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11493   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
11494   ASSERT_EQ(Style3, getGoogleStyle());
11495 }
11496 
11497 #endif // _MSC_VER
11498 
11499 class ReplacementTest : public ::testing::Test {
11500 protected:
11501   tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
11502                                          llvm::StringRef ReplacementText) {
11503     return tooling::Replacement(Context.Sources, Start, Length,
11504                                 ReplacementText);
11505   }
11506 
11507   RewriterTestContext Context;
11508 };
11509 
11510 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11511   // Column limit is 20.
11512   std::string Code = "Type *a =\n"
11513                      "    new Type();\n"
11514                      "g(iiiii, 0, jjjjj,\n"
11515                      "  0, kkkkk, 0, mm);\n"
11516                      "int  bad     = format   ;";
11517   std::string Expected = "auto a = new Type();\n"
11518                          "g(iiiii, nullptr,\n"
11519                          "  jjjjj, nullptr,\n"
11520                          "  kkkkk, nullptr,\n"
11521                          "  mm);\n"
11522                          "int  bad     = format   ;";
11523   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11524   tooling::Replacements Replaces;
11525   Replaces.insert(tooling::Replacement(
11526       Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
11527   Replaces.insert(tooling::Replacement(
11528       Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
11529   Replaces.insert(tooling::Replacement(
11530       Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
11531   Replaces.insert(tooling::Replacement(
11532       Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
11533 
11534   format::FormatStyle Style = format::getLLVMStyle();
11535   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11536   EXPECT_EQ(Expected, applyAllReplacements(
11537                           Code, formatReplacements(Code, Replaces, Style)));
11538 }
11539 
11540 TEST_F(ReplacementTest, FixOnlyAffectedCodeAfterReplacements) {
11541   std::string Code = "namespace A {\n"
11542                      "namespace B {\n"
11543                      "  int x;\n"
11544                      "} // namespace B\n"
11545                      "} // namespace A\n"
11546                      "\n"
11547                      "namespace C {\n"
11548                      "namespace D { int i; }\n"
11549                      "inline namespace E { namespace { int y; } }\n"
11550                      "int x=     0;"
11551                      "}";
11552   std::string Expected = "\n\nnamespace C {\n"
11553                          "namespace D { int i; }\n\n"
11554                          "int x=     0;"
11555                          "}";
11556   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11557   tooling::Replacements Replaces;
11558   Replaces.insert(tooling::Replacement(
11559       Context.Sources, Context.getLocation(ID, 3, 3), 6, ""));
11560   Replaces.insert(tooling::Replacement(
11561       Context.Sources, Context.getLocation(ID, 9, 34), 6, ""));
11562 
11563   format::FormatStyle Style = format::getLLVMStyle();
11564   auto FinalReplaces = formatReplacements(
11565       Code, cleanupAroundReplacements(Code, Replaces, Style), Style);
11566   EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
11567 }
11568 
11569 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11570   std::string Code = "#include \"a.h\"\n"
11571                      "#include \"c.h\"\n"
11572                      "\n"
11573                      "int main() {\n"
11574                      "  return 0;\n"
11575                      "}";
11576   std::string Expected = "#include \"a.h\"\n"
11577                          "#include \"b.h\"\n"
11578                          "#include \"c.h\"\n"
11579                          "\n"
11580                          "int main() {\n"
11581                          "  return 0;\n"
11582                          "}";
11583   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11584   tooling::Replacements Replaces;
11585   Replaces.insert(tooling::Replacement(
11586       Context.Sources, Context.getLocation(ID, 1, 1), 0, "#include \"b.h\"\n"));
11587 
11588   format::FormatStyle Style = format::getLLVMStyle();
11589   Style.SortIncludes = true;
11590   auto FinalReplaces = formatReplacements(Code, Replaces, Style);
11591   EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
11592 }
11593 
11594 } // end namespace
11595 } // end namespace format
11596 } // end namespace clang
11597