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                "  break;\n"
759                "}");
760 }
761 
762 TEST_F(FormatTest, ShortCaseLabels) {
763   FormatStyle Style = getLLVMStyle();
764   Style.AllowShortCaseLabelsOnASingleLine = true;
765   verifyFormat("switch (a) {\n"
766                "case 1: x = 1; break;\n"
767                "case 2: return;\n"
768                "case 3:\n"
769                "case 4:\n"
770                "case 5: return;\n"
771                "case 6: // comment\n"
772                "  return;\n"
773                "case 7:\n"
774                "  // comment\n"
775                "  return;\n"
776                "case 8:\n"
777                "  x = 8; // comment\n"
778                "  break;\n"
779                "default: y = 1; break;\n"
780                "}",
781                Style);
782   verifyFormat("switch (a) {\n"
783                "#if FOO\n"
784                "case 0: return 0;\n"
785                "#endif\n"
786                "}",
787                Style);
788   verifyFormat("switch (a) {\n"
789                "case 1: {\n"
790                "}\n"
791                "case 2: {\n"
792                "  return;\n"
793                "}\n"
794                "case 3: {\n"
795                "  x = 1;\n"
796                "  return;\n"
797                "}\n"
798                "case 4:\n"
799                "  if (x)\n"
800                "    return;\n"
801                "}",
802                Style);
803   Style.ColumnLimit = 21;
804   verifyFormat("switch (a) {\n"
805                "case 1: x = 1; break;\n"
806                "case 2: return;\n"
807                "case 3:\n"
808                "case 4:\n"
809                "case 5: return;\n"
810                "default:\n"
811                "  y = 1;\n"
812                "  break;\n"
813                "}",
814                Style);
815 }
816 
817 TEST_F(FormatTest, FormatsLabels) {
818   verifyFormat("void f() {\n"
819                "  some_code();\n"
820                "test_label:\n"
821                "  some_other_code();\n"
822                "  {\n"
823                "    some_more_code();\n"
824                "  another_label:\n"
825                "    some_more_code();\n"
826                "  }\n"
827                "}");
828   verifyFormat("{\n"
829                "  some_code();\n"
830                "test_label:\n"
831                "  some_other_code();\n"
832                "}");
833   verifyFormat("{\n"
834                "  some_code();\n"
835                "test_label:;\n"
836                "  int i = 0;\n"
837                "}");
838 }
839 
840 //===----------------------------------------------------------------------===//
841 // Tests for comments.
842 //===----------------------------------------------------------------------===//
843 
844 TEST_F(FormatTest, UnderstandsSingleLineComments) {
845   verifyFormat("//* */");
846   verifyFormat("// line 1\n"
847                "// line 2\n"
848                "void f() {}\n");
849 
850   verifyFormat("void f() {\n"
851                "  // Doesn't do anything\n"
852                "}");
853   verifyFormat("SomeObject\n"
854                "    // Calling someFunction on SomeObject\n"
855                "    .someFunction();");
856   verifyFormat("auto result = SomeObject\n"
857                "                  // Calling someFunction on SomeObject\n"
858                "                  .someFunction();");
859   verifyFormat("void f(int i,  // some comment (probably for i)\n"
860                "       int j,  // some comment (probably for j)\n"
861                "       int k); // some comment (probably for k)");
862   verifyFormat("void f(int i,\n"
863                "       // some comment (probably for j)\n"
864                "       int j,\n"
865                "       // some comment (probably for k)\n"
866                "       int k);");
867 
868   verifyFormat("int i    // This is a fancy variable\n"
869                "    = 5; // with nicely aligned comment.");
870 
871   verifyFormat("// Leading comment.\n"
872                "int a; // Trailing comment.");
873   verifyFormat("int a; // Trailing comment\n"
874                "       // on 2\n"
875                "       // or 3 lines.\n"
876                "int b;");
877   verifyFormat("int a; // Trailing comment\n"
878                "\n"
879                "// Leading comment.\n"
880                "int b;");
881   verifyFormat("int a;    // Comment.\n"
882                "          // More details.\n"
883                "int bbbb; // Another comment.");
884   verifyFormat(
885       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
886       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
887       "int cccccccccccccccccccccccccccccc;       // comment\n"
888       "int ddd;                     // looooooooooooooooooooooooong comment\n"
889       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
890       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
891       "int ccccccccccccccccccc;     // comment");
892 
893   verifyFormat("#include \"a\"     // comment\n"
894                "#include \"a/b/c\" // comment");
895   verifyFormat("#include <a>     // comment\n"
896                "#include <a/b/c> // comment");
897   EXPECT_EQ("#include \"a\"     // comment\n"
898             "#include \"a/b/c\" // comment",
899             format("#include \\\n"
900                    "  \"a\" // comment\n"
901                    "#include \"a/b/c\" // comment"));
902 
903   verifyFormat("enum E {\n"
904                "  // comment\n"
905                "  VAL_A, // comment\n"
906                "  VAL_B\n"
907                "};");
908 
909   verifyFormat(
910       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
911       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
912   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
913                "    // Comment inside a statement.\n"
914                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
915   verifyFormat("SomeFunction(a,\n"
916                "             // comment\n"
917                "             b + x);");
918   verifyFormat("SomeFunction(a, a,\n"
919                "             // comment\n"
920                "             b + x);");
921   verifyFormat(
922       "bool aaaaaaaaaaaaa = // comment\n"
923       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
924       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
925 
926   verifyFormat("int aaaa; // aaaaa\n"
927                "int aa;   // aaaaaaa",
928                getLLVMStyleWithColumns(20));
929 
930   EXPECT_EQ("void f() { // This does something ..\n"
931             "}\n"
932             "int a; // This is unrelated",
933             format("void f()    {     // This does something ..\n"
934                    "  }\n"
935                    "int   a;     // This is unrelated"));
936   EXPECT_EQ("class C {\n"
937             "  void f() { // This does something ..\n"
938             "  }          // awesome..\n"
939             "\n"
940             "  int a; // This is unrelated\n"
941             "};",
942             format("class C{void f()    { // This does something ..\n"
943                    "      } // awesome..\n"
944                    " \n"
945                    "int a;    // This is unrelated\n"
946                    "};"));
947 
948   EXPECT_EQ("int i; // single line trailing comment",
949             format("int i;\\\n// single line trailing comment"));
950 
951   verifyGoogleFormat("int a;  // Trailing comment.");
952 
953   verifyFormat("someFunction(anotherFunction( // Force break.\n"
954                "    parameter));");
955 
956   verifyGoogleFormat("#endif  // HEADER_GUARD");
957 
958   verifyFormat("const char *test[] = {\n"
959                "    // A\n"
960                "    \"aaaa\",\n"
961                "    // B\n"
962                "    \"aaaaa\"};");
963   verifyGoogleFormat(
964       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
965       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
966   EXPECT_EQ("D(a, {\n"
967             "  // test\n"
968             "  int a;\n"
969             "});",
970             format("D(a, {\n"
971                    "// test\n"
972                    "int a;\n"
973                    "});"));
974 
975   EXPECT_EQ("lineWith(); // comment\n"
976             "// at start\n"
977             "otherLine();",
978             format("lineWith();   // comment\n"
979                    "// at start\n"
980                    "otherLine();"));
981   EXPECT_EQ("lineWith(); // comment\n"
982             "/*\n"
983             " * at start */\n"
984             "otherLine();",
985             format("lineWith();   // comment\n"
986                    "/*\n"
987                    " * at start */\n"
988                    "otherLine();"));
989   EXPECT_EQ("lineWith(); // comment\n"
990             "            // at start\n"
991             "otherLine();",
992             format("lineWith();   // comment\n"
993                    " // at start\n"
994                    "otherLine();"));
995 
996   EXPECT_EQ("lineWith(); // comment\n"
997             "// at start\n"
998             "otherLine(); // comment",
999             format("lineWith();   // comment\n"
1000                    "// at start\n"
1001                    "otherLine();   // comment"));
1002   EXPECT_EQ("lineWith();\n"
1003             "// at start\n"
1004             "otherLine(); // comment",
1005             format("lineWith();\n"
1006                    " // at start\n"
1007                    "otherLine();   // comment"));
1008   EXPECT_EQ("// first\n"
1009             "// at start\n"
1010             "otherLine(); // comment",
1011             format("// first\n"
1012                    " // at start\n"
1013                    "otherLine();   // comment"));
1014   EXPECT_EQ("f();\n"
1015             "// first\n"
1016             "// at start\n"
1017             "otherLine(); // comment",
1018             format("f();\n"
1019                    "// first\n"
1020                    " // at start\n"
1021                    "otherLine();   // comment"));
1022   verifyFormat("f(); // comment\n"
1023                "// first\n"
1024                "// at start\n"
1025                "otherLine();");
1026   EXPECT_EQ("f(); // comment\n"
1027             "// first\n"
1028             "// at start\n"
1029             "otherLine();",
1030             format("f();   // comment\n"
1031                    "// first\n"
1032                    " // at start\n"
1033                    "otherLine();"));
1034   EXPECT_EQ("f(); // comment\n"
1035             "     // first\n"
1036             "// at start\n"
1037             "otherLine();",
1038             format("f();   // comment\n"
1039                    " // first\n"
1040                    "// at start\n"
1041                    "otherLine();"));
1042   EXPECT_EQ("void f() {\n"
1043             "  lineWith(); // comment\n"
1044             "  // at start\n"
1045             "}",
1046             format("void              f() {\n"
1047                    "  lineWith(); // comment\n"
1048                    "  // at start\n"
1049                    "}"));
1050   EXPECT_EQ("int xy; // a\n"
1051             "int z;  // b",
1052             format("int xy;    // a\n"
1053                    "int z;    //b"));
1054   EXPECT_EQ("int xy; // a\n"
1055             "int z; // bb",
1056             format("int xy;    // a\n"
1057                    "int z;    //bb",
1058                    getLLVMStyleWithColumns(12)));
1059 
1060   verifyFormat("#define A                                                  \\\n"
1061                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1062                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1063                getLLVMStyleWithColumns(60));
1064   verifyFormat(
1065       "#define A                                                   \\\n"
1066       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1067       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1068       getLLVMStyleWithColumns(61));
1069 
1070   verifyFormat("if ( // This is some comment\n"
1071                "    x + 3) {\n"
1072                "}");
1073   EXPECT_EQ("if ( // This is some comment\n"
1074             "     // spanning two lines\n"
1075             "    x + 3) {\n"
1076             "}",
1077             format("if( // This is some comment\n"
1078                    "     // spanning two lines\n"
1079                    " x + 3) {\n"
1080                    "}"));
1081 
1082   verifyNoCrash("/\\\n/");
1083   verifyNoCrash("/\\\n* */");
1084   // The 0-character somehow makes the lexer return a proper comment.
1085   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1086 }
1087 
1088 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1089   EXPECT_EQ("SomeFunction(a,\n"
1090             "             b, // comment\n"
1091             "             c);",
1092             format("SomeFunction(a,\n"
1093                    "          b, // comment\n"
1094                    "      c);"));
1095   EXPECT_EQ("SomeFunction(a, b,\n"
1096             "             // comment\n"
1097             "             c);",
1098             format("SomeFunction(a,\n"
1099                    "          b,\n"
1100                    "  // comment\n"
1101                    "      c);"));
1102   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1103             "             c);",
1104             format("SomeFunction(a, b, // comment (unclear relation)\n"
1105                    "      c);"));
1106   EXPECT_EQ("SomeFunction(a, // comment\n"
1107             "             b,\n"
1108             "             c); // comment",
1109             format("SomeFunction(a,     // comment\n"
1110                    "          b,\n"
1111                    "      c); // comment"));
1112 }
1113 
1114 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1115   EXPECT_EQ("// comment", format("// comment  "));
1116   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1117             format("int aaaaaaa, bbbbbbb; // comment                   ",
1118                    getLLVMStyleWithColumns(33)));
1119   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1120   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1121 }
1122 
1123 TEST_F(FormatTest, UnderstandsBlockComments) {
1124   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1125   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1126   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1127             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1128             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1129                    "/* Trailing comment for aa... */\n"
1130                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1131   EXPECT_EQ(
1132       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1133       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1134       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1135              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1136   EXPECT_EQ(
1137       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1138       "    aaaaaaaaaaaaaaaaaa,\n"
1139       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1140       "}",
1141       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1142              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1143              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1144              "}"));
1145 
1146   FormatStyle NoBinPacking = getLLVMStyle();
1147   NoBinPacking.BinPackParameters = false;
1148   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1149                "         /* parameter 2 */ aaaaaa,\n"
1150                "         /* parameter 3 */ aaaaaa,\n"
1151                "         /* parameter 4 */ aaaaaa);",
1152                NoBinPacking);
1153 
1154   // Aligning block comments in macros.
1155   verifyGoogleFormat("#define A        \\\n"
1156                      "  int i;   /*a*/ \\\n"
1157                      "  int jjj; /*b*/");
1158 }
1159 
1160 TEST_F(FormatTest, AlignsBlockComments) {
1161   EXPECT_EQ("/*\n"
1162             " * Really multi-line\n"
1163             " * comment.\n"
1164             " */\n"
1165             "void f() {}",
1166             format("  /*\n"
1167                    "   * Really multi-line\n"
1168                    "   * comment.\n"
1169                    "   */\n"
1170                    "  void f() {}"));
1171   EXPECT_EQ("class C {\n"
1172             "  /*\n"
1173             "   * Another multi-line\n"
1174             "   * comment.\n"
1175             "   */\n"
1176             "  void f() {}\n"
1177             "};",
1178             format("class C {\n"
1179                    "/*\n"
1180                    " * Another multi-line\n"
1181                    " * comment.\n"
1182                    " */\n"
1183                    "void f() {}\n"
1184                    "};"));
1185   EXPECT_EQ("/*\n"
1186             "  1. This is a comment with non-trivial formatting.\n"
1187             "     1.1. We have to indent/outdent all lines equally\n"
1188             "         1.1.1. to keep the formatting.\n"
1189             " */",
1190             format("  /*\n"
1191                    "    1. This is a comment with non-trivial formatting.\n"
1192                    "       1.1. We have to indent/outdent all lines equally\n"
1193                    "           1.1.1. to keep the formatting.\n"
1194                    "   */"));
1195   EXPECT_EQ("/*\n"
1196             "Don't try to outdent if there's not enough indentation.\n"
1197             "*/",
1198             format("  /*\n"
1199                    " Don't try to outdent if there's not enough indentation.\n"
1200                    " */"));
1201 
1202   EXPECT_EQ("int i; /* Comment with empty...\n"
1203             "        *\n"
1204             "        * line. */",
1205             format("int i; /* Comment with empty...\n"
1206                    "        *\n"
1207                    "        * line. */"));
1208   EXPECT_EQ("int foobar = 0; /* comment */\n"
1209             "int bar = 0;    /* multiline\n"
1210             "                   comment 1 */\n"
1211             "int baz = 0;    /* multiline\n"
1212             "                   comment 2 */\n"
1213             "int bzz = 0;    /* multiline\n"
1214             "                   comment 3 */",
1215             format("int foobar = 0; /* comment */\n"
1216                    "int bar = 0;    /* multiline\n"
1217                    "                   comment 1 */\n"
1218                    "int baz = 0; /* multiline\n"
1219                    "                comment 2 */\n"
1220                    "int bzz = 0;         /* multiline\n"
1221                    "                        comment 3 */"));
1222   EXPECT_EQ("int foobar = 0; /* comment */\n"
1223             "int bar = 0;    /* multiline\n"
1224             "   comment */\n"
1225             "int baz = 0;    /* multiline\n"
1226             "comment */",
1227             format("int foobar = 0; /* comment */\n"
1228                    "int bar = 0; /* multiline\n"
1229                    "comment */\n"
1230                    "int baz = 0;        /* multiline\n"
1231                    "comment */"));
1232 }
1233 
1234 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1235   FormatStyle Style = getLLVMStyleWithColumns(20);
1236   Style.ReflowComments = false;
1237   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1238   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1239 }
1240 
1241 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1242   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1243             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1244             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1245                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1246   EXPECT_EQ(
1247       "void ffffffffffff(\n"
1248       "    int aaaaaaaa, int bbbbbbbb,\n"
1249       "    int cccccccccccc) { /*\n"
1250       "                           aaaaaaaaaa\n"
1251       "                           aaaaaaaaaaaaa\n"
1252       "                           bbbbbbbbbbbbbb\n"
1253       "                           bbbbbbbbbb\n"
1254       "                         */\n"
1255       "}",
1256       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1257              "{ /*\n"
1258              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1259              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1260              "   */\n"
1261              "}",
1262              getLLVMStyleWithColumns(40)));
1263 }
1264 
1265 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1266   EXPECT_EQ("void ffffffffff(\n"
1267             "    int aaaaa /* test */);",
1268             format("void ffffffffff(int aaaaa /* test */);",
1269                    getLLVMStyleWithColumns(35)));
1270 }
1271 
1272 TEST_F(FormatTest, SplitsLongCxxComments) {
1273   EXPECT_EQ("// A comment that\n"
1274             "// doesn't fit on\n"
1275             "// one line",
1276             format("// A comment that doesn't fit on one line",
1277                    getLLVMStyleWithColumns(20)));
1278   EXPECT_EQ("/// A comment that\n"
1279             "/// doesn't fit on\n"
1280             "/// one line",
1281             format("/// A comment that doesn't fit on one line",
1282                    getLLVMStyleWithColumns(20)));
1283   EXPECT_EQ("//! A comment that\n"
1284             "//! doesn't fit on\n"
1285             "//! one line",
1286             format("//! A comment that doesn't fit on one line",
1287                    getLLVMStyleWithColumns(20)));
1288   EXPECT_EQ("// a b c d\n"
1289             "// e f  g\n"
1290             "// h i j k",
1291             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1292   EXPECT_EQ(
1293       "// a b c d\n"
1294       "// e f  g\n"
1295       "// h i j k",
1296       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1297   EXPECT_EQ("if (true) // A comment that\n"
1298             "          // doesn't fit on\n"
1299             "          // one line",
1300             format("if (true) // A comment that doesn't fit on one line   ",
1301                    getLLVMStyleWithColumns(30)));
1302   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1303             format("//    Don't_touch_leading_whitespace",
1304                    getLLVMStyleWithColumns(20)));
1305   EXPECT_EQ("// Add leading\n"
1306             "// whitespace",
1307             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1308   EXPECT_EQ("/// Add leading\n"
1309             "/// whitespace",
1310             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1311   EXPECT_EQ("//! Add leading\n"
1312             "//! whitespace",
1313             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1314   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1315   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1316             "// limit",
1317             format("//Even if it makes the line exceed the column limit",
1318                    getLLVMStyleWithColumns(51)));
1319   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1320 
1321   EXPECT_EQ("// aa bb cc dd",
1322             format("// aa bb             cc dd                   ",
1323                    getLLVMStyleWithColumns(15)));
1324 
1325   EXPECT_EQ("// A comment before\n"
1326             "// a macro\n"
1327             "// definition\n"
1328             "#define a b",
1329             format("// A comment before a macro definition\n"
1330                    "#define a b",
1331                    getLLVMStyleWithColumns(20)));
1332   EXPECT_EQ("void ffffff(\n"
1333             "    int aaaaaaaaa,  // wwww\n"
1334             "    int bbbbbbbbbb, // xxxxxxx\n"
1335             "                    // yyyyyyyyyy\n"
1336             "    int c, int d, int e) {}",
1337             format("void ffffff(\n"
1338                    "    int aaaaaaaaa, // wwww\n"
1339                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1340                    "    int c, int d, int e) {}",
1341                    getLLVMStyleWithColumns(40)));
1342   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1343             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1344                    getLLVMStyleWithColumns(20)));
1345   EXPECT_EQ(
1346       "#define XXX // a b c d\n"
1347       "            // e f g h",
1348       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1349   EXPECT_EQ(
1350       "#define XXX // q w e r\n"
1351       "            // t y u i",
1352       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1353 }
1354 
1355 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1356   EXPECT_EQ("//     A comment\n"
1357             "//     that doesn't\n"
1358             "//     fit on one\n"
1359             "//     line",
1360             format("//     A comment that doesn't fit on one line",
1361                    getLLVMStyleWithColumns(20)));
1362   EXPECT_EQ("///     A comment\n"
1363             "///     that doesn't\n"
1364             "///     fit on one\n"
1365             "///     line",
1366             format("///     A comment that doesn't fit on one line",
1367                    getLLVMStyleWithColumns(20)));
1368 }
1369 
1370 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1371   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1372             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1373             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1374             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1375                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1376                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1377   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1378             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1379             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1380             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1381                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1382                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1383                    getLLVMStyleWithColumns(50)));
1384   // FIXME: One day we might want to implement adjustment of leading whitespace
1385   // of the consecutive lines in this kind of comment:
1386   EXPECT_EQ("double\n"
1387             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1388             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1389             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1390             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1391                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1392                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1393                    getLLVMStyleWithColumns(49)));
1394 }
1395 
1396 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1397   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1398   Pragmas.CommentPragmas = "^ IWYU pragma:";
1399   EXPECT_EQ(
1400       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1401       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1402   EXPECT_EQ(
1403       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1404       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1405 }
1406 
1407 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1408   EXPECT_EQ("if (xxx ==\n"
1409             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1410             "    zzz)\n"
1411             "  q();",
1412             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1413                    "    zzz) q();",
1414                    getLLVMStyleWithColumns(40)));
1415   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1416             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1417             "    zzz)\n"
1418             "  q();",
1419             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1420                    "    zzz) q();",
1421                    getLLVMStyleWithColumns(40)));
1422   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1423             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1424             "    zzz)\n"
1425             "  q();",
1426             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1427                    "    zzz) q();",
1428                    getLLVMStyleWithColumns(40)));
1429   EXPECT_EQ("fffffffff(\n"
1430             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1431             "    zzz);",
1432             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1433                    " zzz);",
1434                    getLLVMStyleWithColumns(40)));
1435 }
1436 
1437 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1438   EXPECT_EQ("#define A(x) /* \\\n"
1439             "  a comment     \\\n"
1440             "  inside */     \\\n"
1441             "  f();",
1442             format("#define A(x) /* \\\n"
1443                    "  a comment     \\\n"
1444                    "  inside */     \\\n"
1445                    "  f();",
1446                    getLLVMStyleWithColumns(17)));
1447   EXPECT_EQ("#define A(      \\\n"
1448             "    x) /*       \\\n"
1449             "  a comment     \\\n"
1450             "  inside */     \\\n"
1451             "  f();",
1452             format("#define A(      \\\n"
1453                    "    x) /*       \\\n"
1454                    "  a comment     \\\n"
1455                    "  inside */     \\\n"
1456                    "  f();",
1457                    getLLVMStyleWithColumns(17)));
1458 }
1459 
1460 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1461   EXPECT_EQ("namespace {}\n// Test\n#define A",
1462             format("namespace {}\n   // Test\n#define A"));
1463   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1464             format("namespace {}\n   /* Test */\n#define A"));
1465   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1466             format("namespace {}\n   /* Test */    #define A"));
1467 }
1468 
1469 TEST_F(FormatTest, SplitsLongLinesInComments) {
1470   EXPECT_EQ("/* This is a long\n"
1471             " * comment that\n"
1472             " * doesn't\n"
1473             " * fit on one line.\n"
1474             " */",
1475             format("/* "
1476                    "This is a long                                         "
1477                    "comment that "
1478                    "doesn't                                    "
1479                    "fit on one line.  */",
1480                    getLLVMStyleWithColumns(20)));
1481   EXPECT_EQ(
1482       "/* a b c d\n"
1483       " * e f  g\n"
1484       " * h i j k\n"
1485       " */",
1486       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1487   EXPECT_EQ(
1488       "/* a b c d\n"
1489       " * e f  g\n"
1490       " * h i j k\n"
1491       " */",
1492       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1493   EXPECT_EQ("/*\n"
1494             "This is a long\n"
1495             "comment that doesn't\n"
1496             "fit on one line.\n"
1497             "*/",
1498             format("/*\n"
1499                    "This is a long                                         "
1500                    "comment that doesn't                                    "
1501                    "fit on one line.                                      \n"
1502                    "*/",
1503                    getLLVMStyleWithColumns(20)));
1504   EXPECT_EQ("/*\n"
1505             " * This is a long\n"
1506             " * comment that\n"
1507             " * doesn't fit on\n"
1508             " * one line.\n"
1509             " */",
1510             format("/*      \n"
1511                    " * This is a long "
1512                    "   comment that     "
1513                    "   doesn't fit on   "
1514                    "   one line.                                            \n"
1515                    " */",
1516                    getLLVMStyleWithColumns(20)));
1517   EXPECT_EQ("/*\n"
1518             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1519             " * so_it_should_be_broken\n"
1520             " * wherever_a_space_occurs\n"
1521             " */",
1522             format("/*\n"
1523                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1524                    "   so_it_should_be_broken "
1525                    "   wherever_a_space_occurs                             \n"
1526                    " */",
1527                    getLLVMStyleWithColumns(20)));
1528   EXPECT_EQ("/*\n"
1529             " *    This_comment_can_not_be_broken_into_lines\n"
1530             " */",
1531             format("/*\n"
1532                    " *    This_comment_can_not_be_broken_into_lines\n"
1533                    " */",
1534                    getLLVMStyleWithColumns(20)));
1535   EXPECT_EQ("{\n"
1536             "  /*\n"
1537             "  This is another\n"
1538             "  long comment that\n"
1539             "  doesn't fit on one\n"
1540             "  line    1234567890\n"
1541             "  */\n"
1542             "}",
1543             format("{\n"
1544                    "/*\n"
1545                    "This is another     "
1546                    "  long comment that "
1547                    "  doesn't fit on one"
1548                    "  line    1234567890\n"
1549                    "*/\n"
1550                    "}",
1551                    getLLVMStyleWithColumns(20)));
1552   EXPECT_EQ("{\n"
1553             "  /*\n"
1554             "   * This        i s\n"
1555             "   * another comment\n"
1556             "   * t hat  doesn' t\n"
1557             "   * fit on one l i\n"
1558             "   * n e\n"
1559             "   */\n"
1560             "}",
1561             format("{\n"
1562                    "/*\n"
1563                    " * This        i s"
1564                    "   another comment"
1565                    "   t hat  doesn' t"
1566                    "   fit on one l i"
1567                    "   n e\n"
1568                    " */\n"
1569                    "}",
1570                    getLLVMStyleWithColumns(20)));
1571   EXPECT_EQ("/*\n"
1572             " * This is a long\n"
1573             " * comment that\n"
1574             " * doesn't fit on\n"
1575             " * one line\n"
1576             " */",
1577             format("   /*\n"
1578                    "    * This is a long comment that doesn't fit on one line\n"
1579                    "    */",
1580                    getLLVMStyleWithColumns(20)));
1581   EXPECT_EQ("{\n"
1582             "  if (something) /* This is a\n"
1583             "                    long\n"
1584             "                    comment */\n"
1585             "    ;\n"
1586             "}",
1587             format("{\n"
1588                    "  if (something) /* This is a long comment */\n"
1589                    "    ;\n"
1590                    "}",
1591                    getLLVMStyleWithColumns(30)));
1592 
1593   EXPECT_EQ("/* A comment before\n"
1594             " * a macro\n"
1595             " * definition */\n"
1596             "#define a b",
1597             format("/* A comment before a macro definition */\n"
1598                    "#define a b",
1599                    getLLVMStyleWithColumns(20)));
1600 
1601   EXPECT_EQ("/* some comment\n"
1602             "     *   a comment\n"
1603             "* that we break\n"
1604             " * another comment\n"
1605             "* we have to break\n"
1606             "* a left comment\n"
1607             " */",
1608             format("  /* some comment\n"
1609                    "       *   a comment that we break\n"
1610                    "   * another comment we have to break\n"
1611                    "* a left comment\n"
1612                    "   */",
1613                    getLLVMStyleWithColumns(20)));
1614 
1615   EXPECT_EQ("/**\n"
1616             " * multiline block\n"
1617             " * comment\n"
1618             " *\n"
1619             " */",
1620             format("/**\n"
1621                    " * multiline block comment\n"
1622                    " *\n"
1623                    " */",
1624                    getLLVMStyleWithColumns(20)));
1625 
1626   EXPECT_EQ("/*\n"
1627             "\n"
1628             "\n"
1629             "    */\n",
1630             format("  /*       \n"
1631                    "      \n"
1632                    "               \n"
1633                    "      */\n"));
1634 
1635   EXPECT_EQ("/* a a */",
1636             format("/* a a            */", getLLVMStyleWithColumns(15)));
1637   EXPECT_EQ("/* a a bc  */",
1638             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1639   EXPECT_EQ("/* aaa aaa\n"
1640             " * aaaaa */",
1641             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1642   EXPECT_EQ("/* aaa aaa\n"
1643             " * aaaaa     */",
1644             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1645 }
1646 
1647 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1648   EXPECT_EQ("#define X          \\\n"
1649             "  /*               \\\n"
1650             "   Test            \\\n"
1651             "   Macro comment   \\\n"
1652             "   with a long     \\\n"
1653             "   line            \\\n"
1654             "   */              \\\n"
1655             "  A + B",
1656             format("#define X \\\n"
1657                    "  /*\n"
1658                    "   Test\n"
1659                    "   Macro comment with a long  line\n"
1660                    "   */ \\\n"
1661                    "  A + B",
1662                    getLLVMStyleWithColumns(20)));
1663   EXPECT_EQ("#define X          \\\n"
1664             "  /* Macro comment \\\n"
1665             "     with a long   \\\n"
1666             "     line */       \\\n"
1667             "  A + B",
1668             format("#define X \\\n"
1669                    "  /* Macro comment with a long\n"
1670                    "     line */ \\\n"
1671                    "  A + B",
1672                    getLLVMStyleWithColumns(20)));
1673   EXPECT_EQ("#define X          \\\n"
1674             "  /* Macro comment \\\n"
1675             "   * with a long   \\\n"
1676             "   * line */       \\\n"
1677             "  A + B",
1678             format("#define X \\\n"
1679                    "  /* Macro comment with a long  line */ \\\n"
1680                    "  A + B",
1681                    getLLVMStyleWithColumns(20)));
1682 }
1683 
1684 TEST_F(FormatTest, CommentsInStaticInitializers) {
1685   EXPECT_EQ(
1686       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1687       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1688       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1689       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1690       "                        aaaaaaaaaaaaaaaaaaaa};",
1691       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1692              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1693              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1694              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1695              "                  aaaaaaaaaaaaaaaaaaaa };"));
1696   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1697                "                        bbbbbbbbbbb, ccccccccccc};");
1698   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1699                "                        // comment for bb....\n"
1700                "                        bbbbbbbbbbb, ccccccccccc};");
1701   verifyGoogleFormat(
1702       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1703       "                        bbbbbbbbbbb, ccccccccccc};");
1704   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1705                      "                        // comment for bb....\n"
1706                      "                        bbbbbbbbbbb, ccccccccccc};");
1707 
1708   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1709                "       {d, e, f},  // Group #2\n"
1710                "       {g, h, i}}; // Group #3");
1711   verifyFormat("S s = {{// Group #1\n"
1712                "        a, b, c},\n"
1713                "       {// Group #2\n"
1714                "        d, e, f},\n"
1715                "       {// Group #3\n"
1716                "        g, h, i}};");
1717 
1718   EXPECT_EQ("S s = {\n"
1719             "    // Some comment\n"
1720             "    a,\n"
1721             "\n"
1722             "    // Comment after empty line\n"
1723             "    b}",
1724             format("S s =    {\n"
1725                    "      // Some comment\n"
1726                    "  a,\n"
1727                    "  \n"
1728                    "     // Comment after empty line\n"
1729                    "      b\n"
1730                    "}"));
1731   EXPECT_EQ("S s = {\n"
1732             "    /* Some comment */\n"
1733             "    a,\n"
1734             "\n"
1735             "    /* Comment after empty line */\n"
1736             "    b}",
1737             format("S s =    {\n"
1738                    "      /* Some comment */\n"
1739                    "  a,\n"
1740                    "  \n"
1741                    "     /* Comment after empty line */\n"
1742                    "      b\n"
1743                    "}"));
1744   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1745                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1746                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1747                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1748 }
1749 
1750 TEST_F(FormatTest, IgnoresIf0Contents) {
1751   EXPECT_EQ("#if 0\n"
1752             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1753             "#endif\n"
1754             "void f() {}",
1755             format("#if 0\n"
1756                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1757                    "#endif\n"
1758                    "void f(  ) {  }"));
1759   EXPECT_EQ("#if false\n"
1760             "void f(  ) {  }\n"
1761             "#endif\n"
1762             "void g() {}\n",
1763             format("#if false\n"
1764                    "void f(  ) {  }\n"
1765                    "#endif\n"
1766                    "void g(  ) {  }\n"));
1767   EXPECT_EQ("enum E {\n"
1768             "  One,\n"
1769             "  Two,\n"
1770             "#if 0\n"
1771             "Three,\n"
1772             "      Four,\n"
1773             "#endif\n"
1774             "  Five\n"
1775             "};",
1776             format("enum E {\n"
1777                    "  One,Two,\n"
1778                    "#if 0\n"
1779                    "Three,\n"
1780                    "      Four,\n"
1781                    "#endif\n"
1782                    "  Five};"));
1783   EXPECT_EQ("enum F {\n"
1784             "  One,\n"
1785             "#if 1\n"
1786             "  Two,\n"
1787             "#if 0\n"
1788             "Three,\n"
1789             "      Four,\n"
1790             "#endif\n"
1791             "  Five\n"
1792             "#endif\n"
1793             "};",
1794             format("enum F {\n"
1795                    "One,\n"
1796                    "#if 1\n"
1797                    "Two,\n"
1798                    "#if 0\n"
1799                    "Three,\n"
1800                    "      Four,\n"
1801                    "#endif\n"
1802                    "Five\n"
1803                    "#endif\n"
1804                    "};"));
1805   EXPECT_EQ("enum G {\n"
1806             "  One,\n"
1807             "#if 0\n"
1808             "Two,\n"
1809             "#else\n"
1810             "  Three,\n"
1811             "#endif\n"
1812             "  Four\n"
1813             "};",
1814             format("enum G {\n"
1815                    "One,\n"
1816                    "#if 0\n"
1817                    "Two,\n"
1818                    "#else\n"
1819                    "Three,\n"
1820                    "#endif\n"
1821                    "Four\n"
1822                    "};"));
1823   EXPECT_EQ("enum H {\n"
1824             "  One,\n"
1825             "#if 0\n"
1826             "#ifdef Q\n"
1827             "Two,\n"
1828             "#else\n"
1829             "Three,\n"
1830             "#endif\n"
1831             "#endif\n"
1832             "  Four\n"
1833             "};",
1834             format("enum H {\n"
1835                    "One,\n"
1836                    "#if 0\n"
1837                    "#ifdef Q\n"
1838                    "Two,\n"
1839                    "#else\n"
1840                    "Three,\n"
1841                    "#endif\n"
1842                    "#endif\n"
1843                    "Four\n"
1844                    "};"));
1845   EXPECT_EQ("enum I {\n"
1846             "  One,\n"
1847             "#if /* test */ 0 || 1\n"
1848             "Two,\n"
1849             "Three,\n"
1850             "#endif\n"
1851             "  Four\n"
1852             "};",
1853             format("enum I {\n"
1854                    "One,\n"
1855                    "#if /* test */ 0 || 1\n"
1856                    "Two,\n"
1857                    "Three,\n"
1858                    "#endif\n"
1859                    "Four\n"
1860                    "};"));
1861   EXPECT_EQ("enum J {\n"
1862             "  One,\n"
1863             "#if 0\n"
1864             "#if 0\n"
1865             "Two,\n"
1866             "#else\n"
1867             "Three,\n"
1868             "#endif\n"
1869             "Four,\n"
1870             "#endif\n"
1871             "  Five\n"
1872             "};",
1873             format("enum J {\n"
1874                    "One,\n"
1875                    "#if 0\n"
1876                    "#if 0\n"
1877                    "Two,\n"
1878                    "#else\n"
1879                    "Three,\n"
1880                    "#endif\n"
1881                    "Four,\n"
1882                    "#endif\n"
1883                    "Five\n"
1884                    "};"));
1885 }
1886 
1887 //===----------------------------------------------------------------------===//
1888 // Tests for classes, namespaces, etc.
1889 //===----------------------------------------------------------------------===//
1890 
1891 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1892   verifyFormat("class A {};");
1893 }
1894 
1895 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1896   verifyFormat("class A {\n"
1897                "public:\n"
1898                "public: // comment\n"
1899                "protected:\n"
1900                "private:\n"
1901                "  void f() {}\n"
1902                "};");
1903   verifyGoogleFormat("class A {\n"
1904                      " public:\n"
1905                      " protected:\n"
1906                      " private:\n"
1907                      "  void f() {}\n"
1908                      "};");
1909   verifyFormat("class A {\n"
1910                "public slots:\n"
1911                "  void f1() {}\n"
1912                "public Q_SLOTS:\n"
1913                "  void f2() {}\n"
1914                "protected slots:\n"
1915                "  void f3() {}\n"
1916                "protected Q_SLOTS:\n"
1917                "  void f4() {}\n"
1918                "private slots:\n"
1919                "  void f5() {}\n"
1920                "private Q_SLOTS:\n"
1921                "  void f6() {}\n"
1922                "signals:\n"
1923                "  void g1();\n"
1924                "Q_SIGNALS:\n"
1925                "  void g2();\n"
1926                "};");
1927 
1928   // Don't interpret 'signals' the wrong way.
1929   verifyFormat("signals.set();");
1930   verifyFormat("for (Signals signals : f()) {\n}");
1931   verifyFormat("{\n"
1932                "  signals.set(); // This needs indentation.\n"
1933                "}");
1934 }
1935 
1936 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1937   EXPECT_EQ("class A {\n"
1938             "public:\n"
1939             "  void f();\n"
1940             "\n"
1941             "private:\n"
1942             "  void g() {}\n"
1943             "  // test\n"
1944             "protected:\n"
1945             "  int h;\n"
1946             "};",
1947             format("class A {\n"
1948                    "public:\n"
1949                    "void f();\n"
1950                    "private:\n"
1951                    "void g() {}\n"
1952                    "// test\n"
1953                    "protected:\n"
1954                    "int h;\n"
1955                    "};"));
1956   EXPECT_EQ("class A {\n"
1957             "protected:\n"
1958             "public:\n"
1959             "  void f();\n"
1960             "};",
1961             format("class A {\n"
1962                    "protected:\n"
1963                    "\n"
1964                    "public:\n"
1965                    "\n"
1966                    "  void f();\n"
1967                    "};"));
1968 
1969   // Even ensure proper spacing inside macros.
1970   EXPECT_EQ("#define B     \\\n"
1971             "  class A {   \\\n"
1972             "   protected: \\\n"
1973             "   public:    \\\n"
1974             "    void f(); \\\n"
1975             "  };",
1976             format("#define B     \\\n"
1977                    "  class A {   \\\n"
1978                    "   protected: \\\n"
1979                    "              \\\n"
1980                    "   public:    \\\n"
1981                    "              \\\n"
1982                    "    void f(); \\\n"
1983                    "  };",
1984                    getGoogleStyle()));
1985   // But don't remove empty lines after macros ending in access specifiers.
1986   EXPECT_EQ("#define A private:\n"
1987             "\n"
1988             "int i;",
1989             format("#define A         private:\n"
1990                    "\n"
1991                    "int              i;"));
1992 }
1993 
1994 TEST_F(FormatTest, FormatsClasses) {
1995   verifyFormat("class A : public B {};");
1996   verifyFormat("class A : public ::B {};");
1997 
1998   verifyFormat(
1999       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2000       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2001   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2002                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2003                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2004   verifyFormat(
2005       "class A : public B, public C, public D, public E, public F {};");
2006   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2007                "                     public C,\n"
2008                "                     public D,\n"
2009                "                     public E,\n"
2010                "                     public F,\n"
2011                "                     public G {};");
2012 
2013   verifyFormat("class\n"
2014                "    ReallyReallyLongClassName {\n"
2015                "  int i;\n"
2016                "};",
2017                getLLVMStyleWithColumns(32));
2018   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2019                "                           aaaaaaaaaaaaaaaa> {};");
2020   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2021                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2022                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2023   verifyFormat("template <class R, class C>\n"
2024                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2025                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2026   verifyFormat("class ::A::B {};");
2027 }
2028 
2029 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2030   verifyFormat("class A {\n} a, b;");
2031   verifyFormat("struct A {\n} a, b;");
2032   verifyFormat("union A {\n} a;");
2033 }
2034 
2035 TEST_F(FormatTest, FormatsEnum) {
2036   verifyFormat("enum {\n"
2037                "  Zero,\n"
2038                "  One = 1,\n"
2039                "  Two = One + 1,\n"
2040                "  Three = (One + Two),\n"
2041                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2042                "  Five = (One, Two, Three, Four, 5)\n"
2043                "};");
2044   verifyGoogleFormat("enum {\n"
2045                      "  Zero,\n"
2046                      "  One = 1,\n"
2047                      "  Two = One + 1,\n"
2048                      "  Three = (One + Two),\n"
2049                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2050                      "  Five = (One, Two, Three, Four, 5)\n"
2051                      "};");
2052   verifyFormat("enum Enum {};");
2053   verifyFormat("enum {};");
2054   verifyFormat("enum X E {} d;");
2055   verifyFormat("enum __attribute__((...)) E {} d;");
2056   verifyFormat("enum __declspec__((...)) E {} d;");
2057   verifyFormat("enum {\n"
2058                "  Bar = Foo<int, int>::value\n"
2059                "};",
2060                getLLVMStyleWithColumns(30));
2061 
2062   verifyFormat("enum ShortEnum { A, B, C };");
2063   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2064 
2065   EXPECT_EQ("enum KeepEmptyLines {\n"
2066             "  ONE,\n"
2067             "\n"
2068             "  TWO,\n"
2069             "\n"
2070             "  THREE\n"
2071             "}",
2072             format("enum KeepEmptyLines {\n"
2073                    "  ONE,\n"
2074                    "\n"
2075                    "  TWO,\n"
2076                    "\n"
2077                    "\n"
2078                    "  THREE\n"
2079                    "}"));
2080   verifyFormat("enum E { // comment\n"
2081                "  ONE,\n"
2082                "  TWO\n"
2083                "};\n"
2084                "int i;");
2085   // Not enums.
2086   verifyFormat("enum X f() {\n"
2087                "  a();\n"
2088                "  return 42;\n"
2089                "}");
2090   verifyFormat("enum X Type::f() {\n"
2091                "  a();\n"
2092                "  return 42;\n"
2093                "}");
2094   verifyFormat("enum ::X f() {\n"
2095                "  a();\n"
2096                "  return 42;\n"
2097                "}");
2098   verifyFormat("enum ns::X f() {\n"
2099                "  a();\n"
2100                "  return 42;\n"
2101                "}");
2102 }
2103 
2104 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2105   verifyFormat("enum Type {\n"
2106                "  One = 0; // These semicolons should be commas.\n"
2107                "  Two = 1;\n"
2108                "};");
2109   verifyFormat("namespace n {\n"
2110                "enum Type {\n"
2111                "  One,\n"
2112                "  Two, // missing };\n"
2113                "  int i;\n"
2114                "}\n"
2115                "void g() {}");
2116 }
2117 
2118 TEST_F(FormatTest, FormatsEnumStruct) {
2119   verifyFormat("enum struct {\n"
2120                "  Zero,\n"
2121                "  One = 1,\n"
2122                "  Two = One + 1,\n"
2123                "  Three = (One + Two),\n"
2124                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2125                "  Five = (One, Two, Three, Four, 5)\n"
2126                "};");
2127   verifyFormat("enum struct Enum {};");
2128   verifyFormat("enum struct {};");
2129   verifyFormat("enum struct X E {} d;");
2130   verifyFormat("enum struct __attribute__((...)) E {} d;");
2131   verifyFormat("enum struct __declspec__((...)) E {} d;");
2132   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2133 }
2134 
2135 TEST_F(FormatTest, FormatsEnumClass) {
2136   verifyFormat("enum class {\n"
2137                "  Zero,\n"
2138                "  One = 1,\n"
2139                "  Two = One + 1,\n"
2140                "  Three = (One + Two),\n"
2141                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2142                "  Five = (One, Two, Three, Four, 5)\n"
2143                "};");
2144   verifyFormat("enum class Enum {};");
2145   verifyFormat("enum class {};");
2146   verifyFormat("enum class X E {} d;");
2147   verifyFormat("enum class __attribute__((...)) E {} d;");
2148   verifyFormat("enum class __declspec__((...)) E {} d;");
2149   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2150 }
2151 
2152 TEST_F(FormatTest, FormatsEnumTypes) {
2153   verifyFormat("enum X : int {\n"
2154                "  A, // Force multiple lines.\n"
2155                "  B\n"
2156                "};");
2157   verifyFormat("enum X : int { A, B };");
2158   verifyFormat("enum X : std::uint32_t { A, B };");
2159 }
2160 
2161 TEST_F(FormatTest, FormatsNSEnums) {
2162   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2163   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2164                      "  // Information about someDecentlyLongValue.\n"
2165                      "  someDecentlyLongValue,\n"
2166                      "  // Information about anotherDecentlyLongValue.\n"
2167                      "  anotherDecentlyLongValue,\n"
2168                      "  // Information about aThirdDecentlyLongValue.\n"
2169                      "  aThirdDecentlyLongValue\n"
2170                      "};");
2171   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2172                      "  a = 1,\n"
2173                      "  b = 2,\n"
2174                      "  c = 3,\n"
2175                      "};");
2176   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2177                      "  a = 1,\n"
2178                      "  b = 2,\n"
2179                      "  c = 3,\n"
2180                      "};");
2181   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2182                      "  a = 1,\n"
2183                      "  b = 2,\n"
2184                      "  c = 3,\n"
2185                      "};");
2186 }
2187 
2188 TEST_F(FormatTest, FormatsBitfields) {
2189   verifyFormat("struct Bitfields {\n"
2190                "  unsigned sClass : 8;\n"
2191                "  unsigned ValueKind : 2;\n"
2192                "};");
2193   verifyFormat("struct A {\n"
2194                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2195                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2196                "};");
2197   verifyFormat("struct MyStruct {\n"
2198                "  uchar data;\n"
2199                "  uchar : 8;\n"
2200                "  uchar : 8;\n"
2201                "  uchar other;\n"
2202                "};");
2203 }
2204 
2205 TEST_F(FormatTest, FormatsNamespaces) {
2206   verifyFormat("namespace some_namespace {\n"
2207                "class A {};\n"
2208                "void f() { f(); }\n"
2209                "}");
2210   verifyFormat("namespace {\n"
2211                "class A {};\n"
2212                "void f() { f(); }\n"
2213                "}");
2214   verifyFormat("inline namespace X {\n"
2215                "class A {};\n"
2216                "void f() { f(); }\n"
2217                "}");
2218   verifyFormat("using namespace some_namespace;\n"
2219                "class A {};\n"
2220                "void f() { f(); }");
2221 
2222   // This code is more common than we thought; if we
2223   // layout this correctly the semicolon will go into
2224   // its own line, which is undesirable.
2225   verifyFormat("namespace {};");
2226   verifyFormat("namespace {\n"
2227                "class A {};\n"
2228                "};");
2229 
2230   verifyFormat("namespace {\n"
2231                "int SomeVariable = 0; // comment\n"
2232                "} // namespace");
2233   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2234             "#define HEADER_GUARD\n"
2235             "namespace my_namespace {\n"
2236             "int i;\n"
2237             "} // my_namespace\n"
2238             "#endif // HEADER_GUARD",
2239             format("#ifndef HEADER_GUARD\n"
2240                    " #define HEADER_GUARD\n"
2241                    "   namespace my_namespace {\n"
2242                    "int i;\n"
2243                    "}    // my_namespace\n"
2244                    "#endif    // HEADER_GUARD"));
2245 
2246   EXPECT_EQ("namespace A::B {\n"
2247             "class C {};\n"
2248             "}",
2249             format("namespace A::B {\n"
2250                    "class C {};\n"
2251                    "}"));
2252 
2253   FormatStyle Style = getLLVMStyle();
2254   Style.NamespaceIndentation = FormatStyle::NI_All;
2255   EXPECT_EQ("namespace out {\n"
2256             "  int i;\n"
2257             "  namespace in {\n"
2258             "    int i;\n"
2259             "  } // namespace\n"
2260             "} // namespace",
2261             format("namespace out {\n"
2262                    "int i;\n"
2263                    "namespace in {\n"
2264                    "int i;\n"
2265                    "} // namespace\n"
2266                    "} // namespace",
2267                    Style));
2268 
2269   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2270   EXPECT_EQ("namespace out {\n"
2271             "int i;\n"
2272             "namespace in {\n"
2273             "  int i;\n"
2274             "} // namespace\n"
2275             "} // namespace",
2276             format("namespace out {\n"
2277                    "int i;\n"
2278                    "namespace in {\n"
2279                    "int i;\n"
2280                    "} // namespace\n"
2281                    "} // namespace",
2282                    Style));
2283 }
2284 
2285 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2286 
2287 TEST_F(FormatTest, FormatsInlineASM) {
2288   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2289   verifyFormat("asm(\"nop\" ::: \"memory\");");
2290   verifyFormat(
2291       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2292       "    \"cpuid\\n\\t\"\n"
2293       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2294       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2295       "    : \"a\"(value));");
2296   EXPECT_EQ(
2297       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2298       "  __asm {\n"
2299       "        mov     edx,[that] // vtable in edx\n"
2300       "        mov     eax,methodIndex\n"
2301       "        call    [edx][eax*4] // stdcall\n"
2302       "  }\n"
2303       "}",
2304       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2305              "    __asm {\n"
2306              "        mov     edx,[that] // vtable in edx\n"
2307              "        mov     eax,methodIndex\n"
2308              "        call    [edx][eax*4] // stdcall\n"
2309              "    }\n"
2310              "}"));
2311   EXPECT_EQ("_asm {\n"
2312             "  xor eax, eax;\n"
2313             "  cpuid;\n"
2314             "}",
2315             format("_asm {\n"
2316                    "  xor eax, eax;\n"
2317                    "  cpuid;\n"
2318                    "}"));
2319   verifyFormat("void function() {\n"
2320                "  // comment\n"
2321                "  asm(\"\");\n"
2322                "}");
2323   EXPECT_EQ("__asm {\n"
2324             "}\n"
2325             "int i;",
2326             format("__asm   {\n"
2327                    "}\n"
2328                    "int   i;"));
2329 }
2330 
2331 TEST_F(FormatTest, FormatTryCatch) {
2332   verifyFormat("try {\n"
2333                "  throw a * b;\n"
2334                "} catch (int a) {\n"
2335                "  // Do nothing.\n"
2336                "} catch (...) {\n"
2337                "  exit(42);\n"
2338                "}");
2339 
2340   // Function-level try statements.
2341   verifyFormat("int f() try { return 4; } catch (...) {\n"
2342                "  return 5;\n"
2343                "}");
2344   verifyFormat("class A {\n"
2345                "  int a;\n"
2346                "  A() try : a(0) {\n"
2347                "  } catch (...) {\n"
2348                "    throw;\n"
2349                "  }\n"
2350                "};\n");
2351 
2352   // Incomplete try-catch blocks.
2353   verifyIncompleteFormat("try {} catch (");
2354 }
2355 
2356 TEST_F(FormatTest, FormatSEHTryCatch) {
2357   verifyFormat("__try {\n"
2358                "  int a = b * c;\n"
2359                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2360                "  // Do nothing.\n"
2361                "}");
2362 
2363   verifyFormat("__try {\n"
2364                "  int a = b * c;\n"
2365                "} __finally {\n"
2366                "  // Do nothing.\n"
2367                "}");
2368 
2369   verifyFormat("DEBUG({\n"
2370                "  __try {\n"
2371                "  } __finally {\n"
2372                "  }\n"
2373                "});\n");
2374 }
2375 
2376 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2377   verifyFormat("try {\n"
2378                "  f();\n"
2379                "} catch {\n"
2380                "  g();\n"
2381                "}");
2382   verifyFormat("try {\n"
2383                "  f();\n"
2384                "} catch (A a) MACRO(x) {\n"
2385                "  g();\n"
2386                "} catch (B b) MACRO(x) {\n"
2387                "  g();\n"
2388                "}");
2389 }
2390 
2391 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2392   FormatStyle Style = getLLVMStyle();
2393   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2394                           FormatStyle::BS_WebKit}) {
2395     Style.BreakBeforeBraces = BraceStyle;
2396     verifyFormat("try {\n"
2397                  "  // something\n"
2398                  "} catch (...) {\n"
2399                  "  // something\n"
2400                  "}",
2401                  Style);
2402   }
2403   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2404   verifyFormat("try {\n"
2405                "  // something\n"
2406                "}\n"
2407                "catch (...) {\n"
2408                "  // something\n"
2409                "}",
2410                Style);
2411   verifyFormat("__try {\n"
2412                "  // something\n"
2413                "}\n"
2414                "__finally {\n"
2415                "  // something\n"
2416                "}",
2417                Style);
2418   verifyFormat("@try {\n"
2419                "  // something\n"
2420                "}\n"
2421                "@finally {\n"
2422                "  // something\n"
2423                "}",
2424                Style);
2425   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2426   verifyFormat("try\n"
2427                "{\n"
2428                "  // something\n"
2429                "}\n"
2430                "catch (...)\n"
2431                "{\n"
2432                "  // something\n"
2433                "}",
2434                Style);
2435   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2436   verifyFormat("try\n"
2437                "  {\n"
2438                "    // something\n"
2439                "  }\n"
2440                "catch (...)\n"
2441                "  {\n"
2442                "    // something\n"
2443                "  }",
2444                Style);
2445   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2446   Style.BraceWrapping.BeforeCatch = true;
2447   verifyFormat("try {\n"
2448                "  // something\n"
2449                "}\n"
2450                "catch (...) {\n"
2451                "  // something\n"
2452                "}",
2453                Style);
2454 }
2455 
2456 TEST_F(FormatTest, FormatObjCTryCatch) {
2457   verifyFormat("@try {\n"
2458                "  f();\n"
2459                "} @catch (NSException e) {\n"
2460                "  @throw;\n"
2461                "} @finally {\n"
2462                "  exit(42);\n"
2463                "}");
2464   verifyFormat("DEBUG({\n"
2465                "  @try {\n"
2466                "  } @finally {\n"
2467                "  }\n"
2468                "});\n");
2469 }
2470 
2471 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2472   FormatStyle Style = getLLVMStyle();
2473   verifyFormat("@autoreleasepool {\n"
2474                "  f();\n"
2475                "}\n"
2476                "@autoreleasepool {\n"
2477                "  f();\n"
2478                "}\n",
2479                Style);
2480   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2481   verifyFormat("@autoreleasepool\n"
2482                "{\n"
2483                "  f();\n"
2484                "}\n"
2485                "@autoreleasepool\n"
2486                "{\n"
2487                "  f();\n"
2488                "}\n",
2489                Style);
2490 }
2491 
2492 TEST_F(FormatTest, StaticInitializers) {
2493   verifyFormat("static SomeClass SC = {1, 'a'};");
2494 
2495   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2496                "    100000000, "
2497                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2498 
2499   // Here, everything other than the "}" would fit on a line.
2500   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2501                "    10000000000000000000000000};");
2502   EXPECT_EQ("S s = {a,\n"
2503             "\n"
2504             "       b};",
2505             format("S s = {\n"
2506                    "  a,\n"
2507                    "\n"
2508                    "  b\n"
2509                    "};"));
2510 
2511   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2512   // line. However, the formatting looks a bit off and this probably doesn't
2513   // happen often in practice.
2514   verifyFormat("static int Variable[1] = {\n"
2515                "    {1000000000000000000000000000000000000}};",
2516                getLLVMStyleWithColumns(40));
2517 }
2518 
2519 TEST_F(FormatTest, DesignatedInitializers) {
2520   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2521   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2522                "                    .bbbbbbbbbb = 2,\n"
2523                "                    .cccccccccc = 3,\n"
2524                "                    .dddddddddd = 4,\n"
2525                "                    .eeeeeeeeee = 5};");
2526   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2527                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2528                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2529                "    .ccccccccccccccccccccccccccc = 3,\n"
2530                "    .ddddddddddddddddddddddddddd = 4,\n"
2531                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2532 
2533   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2534 }
2535 
2536 TEST_F(FormatTest, NestedStaticInitializers) {
2537   verifyFormat("static A x = {{{}}};\n");
2538   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2539                "               {init1, init2, init3, init4}}};",
2540                getLLVMStyleWithColumns(50));
2541 
2542   verifyFormat("somes Status::global_reps[3] = {\n"
2543                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2544                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2545                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2546                getLLVMStyleWithColumns(60));
2547   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2548                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2549                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2550                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2551   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2552                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2553                "rect.fTop}};");
2554 
2555   verifyFormat(
2556       "SomeArrayOfSomeType a = {\n"
2557       "    {{1, 2, 3},\n"
2558       "     {1, 2, 3},\n"
2559       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2560       "      333333333333333333333333333333},\n"
2561       "     {1, 2, 3},\n"
2562       "     {1, 2, 3}}};");
2563   verifyFormat(
2564       "SomeArrayOfSomeType a = {\n"
2565       "    {{1, 2, 3}},\n"
2566       "    {{1, 2, 3}},\n"
2567       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2568       "      333333333333333333333333333333}},\n"
2569       "    {{1, 2, 3}},\n"
2570       "    {{1, 2, 3}}};");
2571 
2572   verifyFormat("struct {\n"
2573                "  unsigned bit;\n"
2574                "  const char *const name;\n"
2575                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2576                "                 {kOsWin, \"Windows\"},\n"
2577                "                 {kOsLinux, \"Linux\"},\n"
2578                "                 {kOsCrOS, \"Chrome OS\"}};");
2579   verifyFormat("struct {\n"
2580                "  unsigned bit;\n"
2581                "  const char *const name;\n"
2582                "} kBitsToOs[] = {\n"
2583                "    {kOsMac, \"Mac\"},\n"
2584                "    {kOsWin, \"Windows\"},\n"
2585                "    {kOsLinux, \"Linux\"},\n"
2586                "    {kOsCrOS, \"Chrome OS\"},\n"
2587                "};");
2588 }
2589 
2590 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2591   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2592                "                      \\\n"
2593                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2594 }
2595 
2596 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2597   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2598                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2599 
2600   // Do break defaulted and deleted functions.
2601   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2602                "    default;",
2603                getLLVMStyleWithColumns(40));
2604   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2605                "    delete;",
2606                getLLVMStyleWithColumns(40));
2607 }
2608 
2609 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2610   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2611                getLLVMStyleWithColumns(40));
2612   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2613                getLLVMStyleWithColumns(40));
2614   EXPECT_EQ("#define Q                              \\\n"
2615             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2616             "  \"aaaaaaaa.cpp\"",
2617             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2618                    getLLVMStyleWithColumns(40)));
2619 }
2620 
2621 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2622   EXPECT_EQ("# 123 \"A string literal\"",
2623             format("   #     123    \"A string literal\""));
2624 }
2625 
2626 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2627   EXPECT_EQ("#;", format("#;"));
2628   verifyFormat("#\n;\n;\n;");
2629 }
2630 
2631 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2632   EXPECT_EQ("#line 42 \"test\"\n",
2633             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2634   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2635                                     getLLVMStyleWithColumns(12)));
2636 }
2637 
2638 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2639   EXPECT_EQ("#line 42 \"test\"",
2640             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2641   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2642 }
2643 
2644 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2645   verifyFormat("#define A \\x20");
2646   verifyFormat("#define A \\ x20");
2647   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2648   verifyFormat("#define A ''");
2649   verifyFormat("#define A ''qqq");
2650   verifyFormat("#define A `qqq");
2651   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2652   EXPECT_EQ("const char *c = STRINGIFY(\n"
2653             "\\na : b);",
2654             format("const char * c = STRINGIFY(\n"
2655                    "\\na : b);"));
2656 
2657   verifyFormat("a\r\\");
2658   verifyFormat("a\v\\");
2659   verifyFormat("a\f\\");
2660 }
2661 
2662 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2663   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2664   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2665   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2666   // FIXME: We never break before the macro name.
2667   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2668 
2669   verifyFormat("#define A A\n#define A A");
2670   verifyFormat("#define A(X) A\n#define A A");
2671 
2672   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2673   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2674 }
2675 
2676 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2677   EXPECT_EQ("// somecomment\n"
2678             "#include \"a.h\"\n"
2679             "#define A(  \\\n"
2680             "    A, B)\n"
2681             "#include \"b.h\"\n"
2682             "// somecomment\n",
2683             format("  // somecomment\n"
2684                    "  #include \"a.h\"\n"
2685                    "#define A(A,\\\n"
2686                    "    B)\n"
2687                    "    #include \"b.h\"\n"
2688                    " // somecomment\n",
2689                    getLLVMStyleWithColumns(13)));
2690 }
2691 
2692 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2693 
2694 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2695   EXPECT_EQ("#define A    \\\n"
2696             "  c;         \\\n"
2697             "  e;\n"
2698             "f;",
2699             format("#define A c; e;\n"
2700                    "f;",
2701                    getLLVMStyleWithColumns(14)));
2702 }
2703 
2704 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2705 
2706 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2707   EXPECT_EQ("int x,\n"
2708             "#define A\n"
2709             "    y;",
2710             format("int x,\n#define A\ny;"));
2711 }
2712 
2713 TEST_F(FormatTest, HashInMacroDefinition) {
2714   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2715   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2716   verifyFormat("#define A  \\\n"
2717                "  {        \\\n"
2718                "    f(#c); \\\n"
2719                "  }",
2720                getLLVMStyleWithColumns(11));
2721 
2722   verifyFormat("#define A(X)         \\\n"
2723                "  void function##X()",
2724                getLLVMStyleWithColumns(22));
2725 
2726   verifyFormat("#define A(a, b, c)   \\\n"
2727                "  void a##b##c()",
2728                getLLVMStyleWithColumns(22));
2729 
2730   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2731 }
2732 
2733 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2734   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2735   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2736 }
2737 
2738 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2739   EXPECT_EQ("#define A b;", format("#define A \\\n"
2740                                    "          \\\n"
2741                                    "  b;",
2742                                    getLLVMStyleWithColumns(25)));
2743   EXPECT_EQ("#define A \\\n"
2744             "          \\\n"
2745             "  a;      \\\n"
2746             "  b;",
2747             format("#define A \\\n"
2748                    "          \\\n"
2749                    "  a;      \\\n"
2750                    "  b;",
2751                    getLLVMStyleWithColumns(11)));
2752   EXPECT_EQ("#define A \\\n"
2753             "  a;      \\\n"
2754             "          \\\n"
2755             "  b;",
2756             format("#define A \\\n"
2757                    "  a;      \\\n"
2758                    "          \\\n"
2759                    "  b;",
2760                    getLLVMStyleWithColumns(11)));
2761 }
2762 
2763 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2764   verifyIncompleteFormat("#define A :");
2765   verifyFormat("#define SOMECASES  \\\n"
2766                "  case 1:          \\\n"
2767                "  case 2\n",
2768                getLLVMStyleWithColumns(20));
2769   verifyFormat("#define A template <typename T>");
2770   verifyIncompleteFormat("#define STR(x) #x\n"
2771                          "f(STR(this_is_a_string_literal{));");
2772   verifyFormat("#pragma omp threadprivate( \\\n"
2773                "    y)), // expected-warning",
2774                getLLVMStyleWithColumns(28));
2775   verifyFormat("#d, = };");
2776   verifyFormat("#if \"a");
2777   verifyIncompleteFormat("({\n"
2778                          "#define b     \\\n"
2779                          "  }           \\\n"
2780                          "  a\n"
2781                          "a",
2782                          getLLVMStyleWithColumns(15));
2783   verifyFormat("#define A     \\\n"
2784                "  {           \\\n"
2785                "    {\n"
2786                "#define B     \\\n"
2787                "  }           \\\n"
2788                "  }",
2789                getLLVMStyleWithColumns(15));
2790   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2791   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2792   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2793   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2794 }
2795 
2796 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2797   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2798   EXPECT_EQ("class A : public QObject {\n"
2799             "  Q_OBJECT\n"
2800             "\n"
2801             "  A() {}\n"
2802             "};",
2803             format("class A  :  public QObject {\n"
2804                    "     Q_OBJECT\n"
2805                    "\n"
2806                    "  A() {\n}\n"
2807                    "}  ;"));
2808   EXPECT_EQ("MACRO\n"
2809             "/*static*/ int i;",
2810             format("MACRO\n"
2811                    " /*static*/ int   i;"));
2812   EXPECT_EQ("SOME_MACRO\n"
2813             "namespace {\n"
2814             "void f();\n"
2815             "}",
2816             format("SOME_MACRO\n"
2817                    "  namespace    {\n"
2818                    "void   f(  );\n"
2819                    "}"));
2820   // Only if the identifier contains at least 5 characters.
2821   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2822   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2823   // Only if everything is upper case.
2824   EXPECT_EQ("class A : public QObject {\n"
2825             "  Q_Object A() {}\n"
2826             "};",
2827             format("class A  :  public QObject {\n"
2828                    "     Q_Object\n"
2829                    "  A() {\n}\n"
2830                    "}  ;"));
2831 
2832   // Only if the next line can actually start an unwrapped line.
2833   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2834             format("SOME_WEIRD_LOG_MACRO\n"
2835                    "<< SomeThing;"));
2836 
2837   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2838                "(n, buffers))\n",
2839                getChromiumStyle(FormatStyle::LK_Cpp));
2840 }
2841 
2842 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2843   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2844             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2845             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2846             "class X {};\n"
2847             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2848             "int *createScopDetectionPass() { return 0; }",
2849             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2850                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2851                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2852                    "  class X {};\n"
2853                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2854                    "  int *createScopDetectionPass() { return 0; }"));
2855   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2856   // braces, so that inner block is indented one level more.
2857   EXPECT_EQ("int q() {\n"
2858             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2859             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2860             "  IPC_END_MESSAGE_MAP()\n"
2861             "}",
2862             format("int q() {\n"
2863                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2864                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2865                    "  IPC_END_MESSAGE_MAP()\n"
2866                    "}"));
2867 
2868   // Same inside macros.
2869   EXPECT_EQ("#define LIST(L) \\\n"
2870             "  L(A)          \\\n"
2871             "  L(B)          \\\n"
2872             "  L(C)",
2873             format("#define LIST(L) \\\n"
2874                    "  L(A) \\\n"
2875                    "  L(B) \\\n"
2876                    "  L(C)",
2877                    getGoogleStyle()));
2878 
2879   // These must not be recognized as macros.
2880   EXPECT_EQ("int q() {\n"
2881             "  f(x);\n"
2882             "  f(x) {}\n"
2883             "  f(x)->g();\n"
2884             "  f(x)->*g();\n"
2885             "  f(x).g();\n"
2886             "  f(x) = x;\n"
2887             "  f(x) += x;\n"
2888             "  f(x) -= x;\n"
2889             "  f(x) *= x;\n"
2890             "  f(x) /= x;\n"
2891             "  f(x) %= x;\n"
2892             "  f(x) &= x;\n"
2893             "  f(x) |= x;\n"
2894             "  f(x) ^= x;\n"
2895             "  f(x) >>= x;\n"
2896             "  f(x) <<= x;\n"
2897             "  f(x)[y].z();\n"
2898             "  LOG(INFO) << x;\n"
2899             "  ifstream(x) >> x;\n"
2900             "}\n",
2901             format("int q() {\n"
2902                    "  f(x)\n;\n"
2903                    "  f(x)\n {}\n"
2904                    "  f(x)\n->g();\n"
2905                    "  f(x)\n->*g();\n"
2906                    "  f(x)\n.g();\n"
2907                    "  f(x)\n = x;\n"
2908                    "  f(x)\n += x;\n"
2909                    "  f(x)\n -= x;\n"
2910                    "  f(x)\n *= x;\n"
2911                    "  f(x)\n /= x;\n"
2912                    "  f(x)\n %= x;\n"
2913                    "  f(x)\n &= x;\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[y].z();\n"
2919                    "  LOG(INFO)\n << x;\n"
2920                    "  ifstream(x)\n >> x;\n"
2921                    "}\n"));
2922   EXPECT_EQ("int q() {\n"
2923             "  F(x)\n"
2924             "  if (1) {\n"
2925             "  }\n"
2926             "  F(x)\n"
2927             "  while (1) {\n"
2928             "  }\n"
2929             "  F(x)\n"
2930             "  G(x);\n"
2931             "  F(x)\n"
2932             "  try {\n"
2933             "    Q();\n"
2934             "  } catch (...) {\n"
2935             "  }\n"
2936             "}\n",
2937             format("int q() {\n"
2938                    "F(x)\n"
2939                    "if (1) {}\n"
2940                    "F(x)\n"
2941                    "while (1) {}\n"
2942                    "F(x)\n"
2943                    "G(x);\n"
2944                    "F(x)\n"
2945                    "try { Q(); } catch (...) {}\n"
2946                    "}\n"));
2947   EXPECT_EQ("class A {\n"
2948             "  A() : t(0) {}\n"
2949             "  A(int i) noexcept() : {}\n"
2950             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2951             "  try : t(0) {\n"
2952             "  } catch (...) {\n"
2953             "  }\n"
2954             "};",
2955             format("class A {\n"
2956                    "  A()\n : t(0) {}\n"
2957                    "  A(int i)\n noexcept() : {}\n"
2958                    "  A(X x)\n"
2959                    "  try : t(0) {} catch (...) {}\n"
2960                    "};"));
2961   EXPECT_EQ("class SomeClass {\n"
2962             "public:\n"
2963             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2964             "};",
2965             format("class SomeClass {\n"
2966                    "public:\n"
2967                    "  SomeClass()\n"
2968                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2969                    "};"));
2970   EXPECT_EQ("class SomeClass {\n"
2971             "public:\n"
2972             "  SomeClass()\n"
2973             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2974             "};",
2975             format("class SomeClass {\n"
2976                    "public:\n"
2977                    "  SomeClass()\n"
2978                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2979                    "};",
2980                    getLLVMStyleWithColumns(40)));
2981 
2982   verifyFormat("MACRO(>)");
2983 }
2984 
2985 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2986   verifyFormat("#define A \\\n"
2987                "  f({     \\\n"
2988                "    g();  \\\n"
2989                "  });",
2990                getLLVMStyleWithColumns(11));
2991 }
2992 
2993 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2994   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2995 }
2996 
2997 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2998   verifyFormat("{\n  { a #c; }\n}");
2999 }
3000 
3001 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3002   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3003             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3004   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3005             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3006 }
3007 
3008 TEST_F(FormatTest, EscapedNewlines) {
3009   EXPECT_EQ(
3010       "#define A \\\n  int i;  \\\n  int j;",
3011       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
3012   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3013   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3014   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
3015   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3016 }
3017 
3018 TEST_F(FormatTest, DontCrashOnBlockComments) {
3019   EXPECT_EQ(
3020       "int xxxxxxxxx; /* "
3021       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
3022       "zzzzzz\n"
3023       "0*/",
3024       format("int xxxxxxxxx;                          /* "
3025              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
3026              "0*/"));
3027 }
3028 
3029 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3030   verifyFormat("#define A \\\n"
3031                "  int v(  \\\n"
3032                "      a); \\\n"
3033                "  int i;",
3034                getLLVMStyleWithColumns(11));
3035 }
3036 
3037 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3038   EXPECT_EQ(
3039       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3040       "                      \\\n"
3041       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3042       "\n"
3043       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3044       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3045       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3046              "\\\n"
3047              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3048              "  \n"
3049              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3050              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3051 }
3052 
3053 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3054   EXPECT_EQ("int\n"
3055             "#define A\n"
3056             "    a;",
3057             format("int\n#define A\na;"));
3058   verifyFormat("functionCallTo(\n"
3059                "    someOtherFunction(\n"
3060                "        withSomeParameters, whichInSequence,\n"
3061                "        areLongerThanALine(andAnotherCall,\n"
3062                "#define A B\n"
3063                "                           withMoreParamters,\n"
3064                "                           whichStronglyInfluenceTheLayout),\n"
3065                "        andMoreParameters),\n"
3066                "    trailing);",
3067                getLLVMStyleWithColumns(69));
3068   verifyFormat("Foo::Foo()\n"
3069                "#ifdef BAR\n"
3070                "    : baz(0)\n"
3071                "#endif\n"
3072                "{\n"
3073                "}");
3074   verifyFormat("void f() {\n"
3075                "  if (true)\n"
3076                "#ifdef A\n"
3077                "    f(42);\n"
3078                "  x();\n"
3079                "#else\n"
3080                "    g();\n"
3081                "  x();\n"
3082                "#endif\n"
3083                "}");
3084   verifyFormat("void f(param1, param2,\n"
3085                "       param3,\n"
3086                "#ifdef A\n"
3087                "       param4(param5,\n"
3088                "#ifdef A1\n"
3089                "              param6,\n"
3090                "#ifdef A2\n"
3091                "              param7),\n"
3092                "#else\n"
3093                "              param8),\n"
3094                "       param9,\n"
3095                "#endif\n"
3096                "       param10,\n"
3097                "#endif\n"
3098                "       param11)\n"
3099                "#else\n"
3100                "       param12)\n"
3101                "#endif\n"
3102                "{\n"
3103                "  x();\n"
3104                "}",
3105                getLLVMStyleWithColumns(28));
3106   verifyFormat("#if 1\n"
3107                "int i;");
3108   verifyFormat("#if 1\n"
3109                "#endif\n"
3110                "#if 1\n"
3111                "#else\n"
3112                "#endif\n");
3113   verifyFormat("DEBUG({\n"
3114                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3115                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3116                "});\n"
3117                "#if a\n"
3118                "#else\n"
3119                "#endif");
3120 
3121   verifyIncompleteFormat("void f(\n"
3122                          "#if A\n"
3123                          "    );\n"
3124                          "#else\n"
3125                          "#endif");
3126 }
3127 
3128 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3129   verifyFormat("#endif\n"
3130                "#if B");
3131 }
3132 
3133 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3134   FormatStyle SingleLine = getLLVMStyle();
3135   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3136   verifyFormat("#if 0\n"
3137                "#elif 1\n"
3138                "#endif\n"
3139                "void foo() {\n"
3140                "  if (test) foo2();\n"
3141                "}",
3142                SingleLine);
3143 }
3144 
3145 TEST_F(FormatTest, LayoutBlockInsideParens) {
3146   verifyFormat("functionCall({ int i; });");
3147   verifyFormat("functionCall({\n"
3148                "  int i;\n"
3149                "  int j;\n"
3150                "});");
3151   verifyFormat("functionCall(\n"
3152                "    {\n"
3153                "      int i;\n"
3154                "      int j;\n"
3155                "    },\n"
3156                "    aaaa, bbbb, cccc);");
3157   verifyFormat("functionA(functionB({\n"
3158                "            int i;\n"
3159                "            int j;\n"
3160                "          }),\n"
3161                "          aaaa, bbbb, cccc);");
3162   verifyFormat("functionCall(\n"
3163                "    {\n"
3164                "      int i;\n"
3165                "      int j;\n"
3166                "    },\n"
3167                "    aaaa, bbbb, // comment\n"
3168                "    cccc);");
3169   verifyFormat("functionA(functionB({\n"
3170                "            int i;\n"
3171                "            int j;\n"
3172                "          }),\n"
3173                "          aaaa, bbbb, // comment\n"
3174                "          cccc);");
3175   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3176   verifyFormat("functionCall(aaaa, bbbb, {\n"
3177                "  int i;\n"
3178                "  int j;\n"
3179                "});");
3180   verifyFormat(
3181       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3182       "    {\n"
3183       "      int i; // break\n"
3184       "    },\n"
3185       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3186       "                                     ccccccccccccccccc));");
3187   verifyFormat("DEBUG({\n"
3188                "  if (a)\n"
3189                "    f();\n"
3190                "});");
3191 }
3192 
3193 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3194   EXPECT_EQ("SOME_MACRO { int i; }\n"
3195             "int i;",
3196             format("  SOME_MACRO  {int i;}  int i;"));
3197 }
3198 
3199 TEST_F(FormatTest, LayoutNestedBlocks) {
3200   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3201                "  struct s {\n"
3202                "    int i;\n"
3203                "  };\n"
3204                "  s kBitsToOs[] = {{10}};\n"
3205                "  for (int i = 0; i < 10; ++i)\n"
3206                "    return;\n"
3207                "}");
3208   verifyFormat("call(parameter, {\n"
3209                "  something();\n"
3210                "  // Comment using all columns.\n"
3211                "  somethingelse();\n"
3212                "});",
3213                getLLVMStyleWithColumns(40));
3214   verifyFormat("DEBUG( //\n"
3215                "    { f(); }, a);");
3216   verifyFormat("DEBUG( //\n"
3217                "    {\n"
3218                "      f(); //\n"
3219                "    },\n"
3220                "    a);");
3221 
3222   EXPECT_EQ("call(parameter, {\n"
3223             "  something();\n"
3224             "  // Comment too\n"
3225             "  // looooooooooong.\n"
3226             "  somethingElse();\n"
3227             "});",
3228             format("call(parameter, {\n"
3229                    "  something();\n"
3230                    "  // Comment too looooooooooong.\n"
3231                    "  somethingElse();\n"
3232                    "});",
3233                    getLLVMStyleWithColumns(29)));
3234   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3235   EXPECT_EQ("DEBUG({ // comment\n"
3236             "  int i;\n"
3237             "});",
3238             format("DEBUG({ // comment\n"
3239                    "int  i;\n"
3240                    "});"));
3241   EXPECT_EQ("DEBUG({\n"
3242             "  int i;\n"
3243             "\n"
3244             "  // comment\n"
3245             "  int j;\n"
3246             "});",
3247             format("DEBUG({\n"
3248                    "  int  i;\n"
3249                    "\n"
3250                    "  // comment\n"
3251                    "  int  j;\n"
3252                    "});"));
3253 
3254   verifyFormat("DEBUG({\n"
3255                "  if (a)\n"
3256                "    return;\n"
3257                "});");
3258   verifyGoogleFormat("DEBUG({\n"
3259                      "  if (a) return;\n"
3260                      "});");
3261   FormatStyle Style = getGoogleStyle();
3262   Style.ColumnLimit = 45;
3263   verifyFormat("Debug(aaaaa,\n"
3264                "      {\n"
3265                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3266                "      },\n"
3267                "      a);",
3268                Style);
3269 
3270   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3271 
3272   verifyNoCrash("^{v^{a}}");
3273 }
3274 
3275 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3276   EXPECT_EQ("#define MACRO()                     \\\n"
3277             "  Debug(aaa, /* force line break */ \\\n"
3278             "        {                           \\\n"
3279             "          int i;                    \\\n"
3280             "          int j;                    \\\n"
3281             "        })",
3282             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3283                    "          {  int   i;  int  j;   })",
3284                    getGoogleStyle()));
3285 
3286   EXPECT_EQ("#define A                                       \\\n"
3287             "  [] {                                          \\\n"
3288             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3289             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3290             "  }",
3291             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3292                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3293                    getGoogleStyle()));
3294 }
3295 
3296 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3297   EXPECT_EQ("{}", format("{}"));
3298   verifyFormat("enum E {};");
3299   verifyFormat("enum E {}");
3300 }
3301 
3302 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3303   FormatStyle Style = getLLVMStyle();
3304   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3305   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3306   verifyFormat("FOO_BEGIN\n"
3307                "  FOO_ENTRY\n"
3308                "FOO_END", Style);
3309   verifyFormat("FOO_BEGIN\n"
3310                "  NESTED_FOO_BEGIN\n"
3311                "    NESTED_FOO_ENTRY\n"
3312                "  NESTED_FOO_END\n"
3313                "FOO_END", Style);
3314   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3315                "  int x;\n"
3316                "  x = 1;\n"
3317                "FOO_END(Baz)", Style);
3318 }
3319 
3320 //===----------------------------------------------------------------------===//
3321 // Line break tests.
3322 //===----------------------------------------------------------------------===//
3323 
3324 TEST_F(FormatTest, PreventConfusingIndents) {
3325   verifyFormat(
3326       "void f() {\n"
3327       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3328       "                         parameter, parameter, parameter)),\n"
3329       "                     SecondLongCall(parameter));\n"
3330       "}");
3331   verifyFormat(
3332       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3333       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3334       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3335       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3336   verifyFormat(
3337       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3338       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3339       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3340       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3341   verifyFormat(
3342       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3343       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3344       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3345       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3346   verifyFormat("int a = bbbb && ccc && fffff(\n"
3347                "#define A Just forcing a new line\n"
3348                "                           ddd);");
3349 }
3350 
3351 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3352   verifyFormat(
3353       "bool aaaaaaa =\n"
3354       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3355       "    bbbbbbbb();");
3356   verifyFormat(
3357       "bool aaaaaaa =\n"
3358       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3359       "    bbbbbbbb();");
3360 
3361   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3362                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3363                "    ccccccccc == ddddddddddd;");
3364   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3365                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3366                "    ccccccccc == ddddddddddd;");
3367   verifyFormat(
3368       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3369       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3370       "    ccccccccc == ddddddddddd;");
3371 
3372   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3373                "                 aaaaaa) &&\n"
3374                "         bbbbbb && cccccc;");
3375   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3376                "                 aaaaaa) >>\n"
3377                "         bbbbbb;");
3378   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3379                "    SourceMgr.getSpellingColumnNumber(\n"
3380                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3381                "    1);");
3382 
3383   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3384                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3385                "    cccccc) {\n}");
3386   verifyFormat("b = a &&\n"
3387                "    // Comment\n"
3388                "    b.c && d;");
3389 
3390   // If the LHS of a comparison is not a binary expression itself, the
3391   // additional linebreak confuses many people.
3392   verifyFormat(
3393       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3394       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3395       "}");
3396   verifyFormat(
3397       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3398       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3399       "}");
3400   verifyFormat(
3401       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3402       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3403       "}");
3404   // Even explicit parentheses stress the precedence enough to make the
3405   // additional break unnecessary.
3406   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3407                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3408                "}");
3409   // This cases is borderline, but with the indentation it is still readable.
3410   verifyFormat(
3411       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3412       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3413       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3414       "}",
3415       getLLVMStyleWithColumns(75));
3416 
3417   // If the LHS is a binary expression, we should still use the additional break
3418   // as otherwise the formatting hides the operator precedence.
3419   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3420                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3421                "    5) {\n"
3422                "}");
3423 
3424   FormatStyle OnePerLine = getLLVMStyle();
3425   OnePerLine.BinPackParameters = false;
3426   verifyFormat(
3427       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3428       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3429       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3430       OnePerLine);
3431 }
3432 
3433 TEST_F(FormatTest, ExpressionIndentation) {
3434   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3435                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3436                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3437                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3438                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3439                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3440                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3441                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3442                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3443   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3444                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3445                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3446                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3447   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3448                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3449                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3450                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3451   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3452                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3453                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3454                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3455   verifyFormat("if () {\n"
3456                "} else if (aaaaa &&\n"
3457                "           bbbbb > // break\n"
3458                "               ccccc) {\n"
3459                "}");
3460 
3461   // Presence of a trailing comment used to change indentation of b.
3462   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3463                "       b;\n"
3464                "return aaaaaaaaaaaaaaaaaaa +\n"
3465                "       b; //",
3466                getLLVMStyleWithColumns(30));
3467 }
3468 
3469 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3470   // Not sure what the best system is here. Like this, the LHS can be found
3471   // immediately above an operator (everything with the same or a higher
3472   // indent). The RHS is aligned right of the operator and so compasses
3473   // everything until something with the same indent as the operator is found.
3474   // FIXME: Is this a good system?
3475   FormatStyle Style = getLLVMStyle();
3476   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3477   verifyFormat(
3478       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3479       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3480       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3481       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3482       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3483       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3484       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3485       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3486       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3487       Style);
3488   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3489                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3490                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3491                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3492                Style);
3493   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3494                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3495                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3496                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3497                Style);
3498   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3499                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3500                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3501                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3502                Style);
3503   verifyFormat("if () {\n"
3504                "} else if (aaaaa\n"
3505                "           && bbbbb // break\n"
3506                "                  > ccccc) {\n"
3507                "}",
3508                Style);
3509   verifyFormat("return (a)\n"
3510                "       // comment\n"
3511                "       + b;",
3512                Style);
3513   verifyFormat(
3514       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3515       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3516       "             + cc;",
3517       Style);
3518 
3519   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3520                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3521                Style);
3522 
3523   // Forced by comments.
3524   verifyFormat(
3525       "unsigned ContentSize =\n"
3526       "    sizeof(int16_t)   // DWARF ARange version number\n"
3527       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3528       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3529       "    + sizeof(int8_t); // Segment Size (in bytes)");
3530 
3531   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3532                "       == boost::fusion::at_c<1>(iiii).second;",
3533                Style);
3534 
3535   Style.ColumnLimit = 60;
3536   verifyFormat("zzzzzzzzzz\n"
3537                "    = bbbbbbbbbbbbbbbbb\n"
3538                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3539                Style);
3540 }
3541 
3542 TEST_F(FormatTest, NoOperandAlignment) {
3543   FormatStyle Style = getLLVMStyle();
3544   Style.AlignOperands = false;
3545   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3546   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3547                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3548                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3549                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3550                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3551                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3552                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3553                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3554                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3555                Style);
3556 
3557   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3558                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3559                "    + cc;",
3560                Style);
3561   verifyFormat("int a = aa\n"
3562                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3563                "        * cccccccccccccccccccccccccccccccccccc;",
3564                Style);
3565 
3566   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3567   verifyFormat("return (a > b\n"
3568                "    // comment1\n"
3569                "    // comment2\n"
3570                "    || c);",
3571                Style);
3572 }
3573 
3574 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3575   FormatStyle Style = getLLVMStyle();
3576   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3577   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3578                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3579                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3580                Style);
3581 }
3582 
3583 TEST_F(FormatTest, ConstructorInitializers) {
3584   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3585   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3586                getLLVMStyleWithColumns(45));
3587   verifyFormat("Constructor()\n"
3588                "    : Inttializer(FitsOnTheLine) {}",
3589                getLLVMStyleWithColumns(44));
3590   verifyFormat("Constructor()\n"
3591                "    : Inttializer(FitsOnTheLine) {}",
3592                getLLVMStyleWithColumns(43));
3593 
3594   verifyFormat("template <typename T>\n"
3595                "Constructor() : Initializer(FitsOnTheLine) {}",
3596                getLLVMStyleWithColumns(45));
3597 
3598   verifyFormat(
3599       "SomeClass::Constructor()\n"
3600       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3601 
3602   verifyFormat(
3603       "SomeClass::Constructor()\n"
3604       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3605       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3606   verifyFormat(
3607       "SomeClass::Constructor()\n"
3608       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3609       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3610   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3611                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3612                "    : aaaaaaaaaa(aaaaaa) {}");
3613 
3614   verifyFormat("Constructor()\n"
3615                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3616                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3617                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3618                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3619 
3620   verifyFormat("Constructor()\n"
3621                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3622                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3623 
3624   verifyFormat("Constructor(int Parameter = 0)\n"
3625                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3626                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3627   verifyFormat("Constructor()\n"
3628                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3629                "}",
3630                getLLVMStyleWithColumns(60));
3631   verifyFormat("Constructor()\n"
3632                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3633                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3634 
3635   // Here a line could be saved by splitting the second initializer onto two
3636   // lines, but that is not desirable.
3637   verifyFormat("Constructor()\n"
3638                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3639                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3640                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3641 
3642   FormatStyle OnePerLine = getLLVMStyle();
3643   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3644   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3645   verifyFormat("SomeClass::Constructor()\n"
3646                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3647                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3648                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3649                OnePerLine);
3650   verifyFormat("SomeClass::Constructor()\n"
3651                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3652                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3653                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3654                OnePerLine);
3655   verifyFormat("MyClass::MyClass(int var)\n"
3656                "    : some_var_(var),            // 4 space indent\n"
3657                "      some_other_var_(var + 1) { // lined up\n"
3658                "}",
3659                OnePerLine);
3660   verifyFormat("Constructor()\n"
3661                "    : aaaaa(aaaaaa),\n"
3662                "      aaaaa(aaaaaa),\n"
3663                "      aaaaa(aaaaaa),\n"
3664                "      aaaaa(aaaaaa),\n"
3665                "      aaaaa(aaaaaa) {}",
3666                OnePerLine);
3667   verifyFormat("Constructor()\n"
3668                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3669                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3670                OnePerLine);
3671   OnePerLine.BinPackParameters = false;
3672   verifyFormat(
3673       "Constructor()\n"
3674       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3675       "          aaaaaaaaaaa().aaa(),\n"
3676       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3677       OnePerLine);
3678   OnePerLine.ColumnLimit = 60;
3679   verifyFormat("Constructor()\n"
3680                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3681                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3682                OnePerLine);
3683 
3684   EXPECT_EQ("Constructor()\n"
3685             "    : // Comment forcing unwanted break.\n"
3686             "      aaaa(aaaa) {}",
3687             format("Constructor() :\n"
3688                    "    // Comment forcing unwanted break.\n"
3689                    "    aaaa(aaaa) {}"));
3690 }
3691 
3692 TEST_F(FormatTest, MemoizationTests) {
3693   // This breaks if the memoization lookup does not take \c Indent and
3694   // \c LastSpace into account.
3695   verifyFormat(
3696       "extern CFRunLoopTimerRef\n"
3697       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3698       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3699       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3700       "                     CFRunLoopTimerContext *context) {}");
3701 
3702   // Deep nesting somewhat works around our memoization.
3703   verifyFormat(
3704       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3705       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3706       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3707       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3708       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3709       getLLVMStyleWithColumns(65));
3710   verifyFormat(
3711       "aaaaa(\n"
3712       "    aaaaa,\n"
3713       "    aaaaa(\n"
3714       "        aaaaa,\n"
3715       "        aaaaa(\n"
3716       "            aaaaa,\n"
3717       "            aaaaa(\n"
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))))))))))));",
3736       getLLVMStyleWithColumns(65));
3737   verifyFormat(
3738       "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"
3739       "                                  a),\n"
3740       "                                a),\n"
3741       "                              a),\n"
3742       "                            a),\n"
3743       "                          a),\n"
3744       "                        a),\n"
3745       "                      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)",
3756       getLLVMStyleWithColumns(65));
3757 
3758   // This test takes VERY long when memoization is broken.
3759   FormatStyle OnePerLine = getLLVMStyle();
3760   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3761   OnePerLine.BinPackParameters = false;
3762   std::string input = "Constructor()\n"
3763                       "    : aaaa(a,\n";
3764   for (unsigned i = 0, e = 80; i != e; ++i) {
3765     input += "           a,\n";
3766   }
3767   input += "           a) {}";
3768   verifyFormat(input, OnePerLine);
3769 }
3770 
3771 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3772   verifyFormat(
3773       "void f() {\n"
3774       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3775       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3776       "    f();\n"
3777       "}");
3778   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3779                "    Intervals[i - 1].getRange().getLast()) {\n}");
3780 }
3781 
3782 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3783   // Principially, we break function declarations in a certain order:
3784   // 1) break amongst arguments.
3785   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3786                "                              Cccccccccccccc cccccccccccccc);");
3787   verifyFormat("template <class TemplateIt>\n"
3788                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3789                "                            TemplateIt *stop) {}");
3790 
3791   // 2) break after return type.
3792   verifyFormat(
3793       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3794       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3795       getGoogleStyle());
3796 
3797   // 3) break after (.
3798   verifyFormat(
3799       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3800       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3801       getGoogleStyle());
3802 
3803   // 4) break before after nested name specifiers.
3804   verifyFormat(
3805       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3806       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3807       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3808       getGoogleStyle());
3809 
3810   // However, there are exceptions, if a sufficient amount of lines can be
3811   // saved.
3812   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3813   // more adjusting.
3814   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3815                "                                  Cccccccccccccc cccccccccc,\n"
3816                "                                  Cccccccccccccc cccccccccc,\n"
3817                "                                  Cccccccccccccc cccccccccc,\n"
3818                "                                  Cccccccccccccc cccccccccc);");
3819   verifyFormat(
3820       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3821       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3822       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3823       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3824       getGoogleStyle());
3825   verifyFormat(
3826       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3827       "                                          Cccccccccccccc cccccccccc,\n"
3828       "                                          Cccccccccccccc cccccccccc,\n"
3829       "                                          Cccccccccccccc cccccccccc,\n"
3830       "                                          Cccccccccccccc cccccccccc,\n"
3831       "                                          Cccccccccccccc cccccccccc,\n"
3832       "                                          Cccccccccccccc cccccccccc);");
3833   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3834                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3835                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3836                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3837                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3838 
3839   // Break after multi-line parameters.
3840   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3841                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3842                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3843                "    bbbb bbbb);");
3844   verifyFormat("void SomeLoooooooooooongFunction(\n"
3845                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3846                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3847                "    int bbbbbbbbbbbbb);");
3848 
3849   // Treat overloaded operators like other functions.
3850   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3851                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3852   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3853                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3854   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3855                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3856   verifyGoogleFormat(
3857       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3858       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3859   verifyGoogleFormat(
3860       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3861       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3862   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3863                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3864   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3865                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3866   verifyGoogleFormat(
3867       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3868       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3869       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3870   verifyGoogleFormat(
3871       "template <typename T>\n"
3872       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3873       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3874       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3875 
3876   FormatStyle Style = getLLVMStyle();
3877   Style.PointerAlignment = FormatStyle::PAS_Left;
3878   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3879                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3880                Style);
3881   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3882                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3883                Style);
3884 }
3885 
3886 TEST_F(FormatTest, TrailingReturnType) {
3887   verifyFormat("auto foo() -> int;\n");
3888   verifyFormat("struct S {\n"
3889                "  auto bar() const -> int;\n"
3890                "};");
3891   verifyFormat("template <size_t Order, typename T>\n"
3892                "auto load_img(const std::string &filename)\n"
3893                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3894   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3895                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3896   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3897   verifyFormat("template <typename T>\n"
3898                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3899                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3900 
3901   // Not trailing return types.
3902   verifyFormat("void f() { auto a = b->c(); }");
3903 }
3904 
3905 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3906   // Avoid breaking before trailing 'const' or other trailing annotations, if
3907   // they are not function-like.
3908   FormatStyle Style = getGoogleStyle();
3909   Style.ColumnLimit = 47;
3910   verifyFormat("void someLongFunction(\n"
3911                "    int someLoooooooooooooongParameter) const {\n}",
3912                getLLVMStyleWithColumns(47));
3913   verifyFormat("LoooooongReturnType\n"
3914                "someLoooooooongFunction() const {}",
3915                getLLVMStyleWithColumns(47));
3916   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3917                "    const {}",
3918                Style);
3919   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3920                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3921   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3922                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3923   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3924                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3925   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3926                "                   aaaaaaaaaaa aaaaa) const override;");
3927   verifyGoogleFormat(
3928       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3929       "    const override;");
3930 
3931   // Even if the first parameter has to be wrapped.
3932   verifyFormat("void someLongFunction(\n"
3933                "    int someLongParameter) const {}",
3934                getLLVMStyleWithColumns(46));
3935   verifyFormat("void someLongFunction(\n"
3936                "    int someLongParameter) const {}",
3937                Style);
3938   verifyFormat("void someLongFunction(\n"
3939                "    int someLongParameter) override {}",
3940                Style);
3941   verifyFormat("void someLongFunction(\n"
3942                "    int someLongParameter) OVERRIDE {}",
3943                Style);
3944   verifyFormat("void someLongFunction(\n"
3945                "    int someLongParameter) final {}",
3946                Style);
3947   verifyFormat("void someLongFunction(\n"
3948                "    int someLongParameter) FINAL {}",
3949                Style);
3950   verifyFormat("void someLongFunction(\n"
3951                "    int parameter) const override {}",
3952                Style);
3953 
3954   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3955   verifyFormat("void someLongFunction(\n"
3956                "    int someLongParameter) const\n"
3957                "{\n"
3958                "}",
3959                Style);
3960 
3961   // Unless these are unknown annotations.
3962   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3963                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3964                "    LONG_AND_UGLY_ANNOTATION;");
3965 
3966   // Breaking before function-like trailing annotations is fine to keep them
3967   // close to their arguments.
3968   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3969                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3970   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3971                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3972   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3973                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3974   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3975                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3976   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3977 
3978   verifyFormat(
3979       "void aaaaaaaaaaaaaaaaaa()\n"
3980       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3981       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3982   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3983                "    __attribute__((unused));");
3984   verifyGoogleFormat(
3985       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3986       "    GUARDED_BY(aaaaaaaaaaaa);");
3987   verifyGoogleFormat(
3988       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3989       "    GUARDED_BY(aaaaaaaaaaaa);");
3990   verifyGoogleFormat(
3991       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3992       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3993   verifyGoogleFormat(
3994       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3995       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3996 }
3997 
3998 TEST_F(FormatTest, FunctionAnnotations) {
3999   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4000                "int OldFunction(const string &parameter) {}");
4001   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4002                "string OldFunction(const string &parameter) {}");
4003   verifyFormat("template <typename T>\n"
4004                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4005                "string OldFunction(const string &parameter) {}");
4006 
4007   // Not function annotations.
4008   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4009                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4010   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4011                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4012   verifyFormat("MACRO(abc).function() // wrap\n"
4013                "    << abc;");
4014   verifyFormat("MACRO(abc)->function() // wrap\n"
4015                "    << abc;");
4016   verifyFormat("MACRO(abc)::function() // wrap\n"
4017                "    << abc;");
4018 }
4019 
4020 TEST_F(FormatTest, BreaksDesireably) {
4021   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4022                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4023                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4024   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4025                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4026                "}");
4027 
4028   verifyFormat(
4029       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4030       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4031 
4032   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4033                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4034                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4035 
4036   verifyFormat(
4037       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4038       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4039       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4040       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4041 
4042   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4043                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4044 
4045   verifyFormat(
4046       "void f() {\n"
4047       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4048       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4049       "}");
4050   verifyFormat(
4051       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4052       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4053   verifyFormat(
4054       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4055       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4056   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4057                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4058                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4059 
4060   // Indent consistently independent of call expression and unary operator.
4061   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4062                "    dddddddddddddddddddddddddddddd));");
4063   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4064                "    dddddddddddddddddddddddddddddd));");
4065   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4066                "    dddddddddddddddddddddddddddddd));");
4067 
4068   // This test case breaks on an incorrect memoization, i.e. an optimization not
4069   // taking into account the StopAt value.
4070   verifyFormat(
4071       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4072       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4073       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4074       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4075 
4076   verifyFormat("{\n  {\n    {\n"
4077                "      Annotation.SpaceRequiredBefore =\n"
4078                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4079                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4080                "    }\n  }\n}");
4081 
4082   // Break on an outer level if there was a break on an inner level.
4083   EXPECT_EQ("f(g(h(a, // comment\n"
4084             "      b, c),\n"
4085             "    d, e),\n"
4086             "  x, y);",
4087             format("f(g(h(a, // comment\n"
4088                    "    b, c), d, e), x, y);"));
4089 
4090   // Prefer breaking similar line breaks.
4091   verifyFormat(
4092       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4093       "                             NSTrackingMouseEnteredAndExited |\n"
4094       "                             NSTrackingActiveAlways;");
4095 }
4096 
4097 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4098   FormatStyle NoBinPacking = getGoogleStyle();
4099   NoBinPacking.BinPackParameters = false;
4100   NoBinPacking.BinPackArguments = true;
4101   verifyFormat("void f() {\n"
4102                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4103                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4104                "}",
4105                NoBinPacking);
4106   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4107                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4108                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4109                NoBinPacking);
4110 
4111   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4112   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4113                "                        vector<int> bbbbbbbbbbbbbbb);",
4114                NoBinPacking);
4115   // FIXME: This behavior difference is probably not wanted. However, currently
4116   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4117   // template arguments from BreakBeforeParameter being set because of the
4118   // one-per-line formatting.
4119   verifyFormat(
4120       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4121       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4122       NoBinPacking);
4123   verifyFormat(
4124       "void fffffffffff(\n"
4125       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4126       "        aaaaaaaaaa);");
4127 }
4128 
4129 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4130   FormatStyle NoBinPacking = getGoogleStyle();
4131   NoBinPacking.BinPackParameters = false;
4132   NoBinPacking.BinPackArguments = false;
4133   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4134                "  aaaaaaaaaaaaaaaaaaaa,\n"
4135                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4136                NoBinPacking);
4137   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4138                "        aaaaaaaaaaaaa,\n"
4139                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4140                NoBinPacking);
4141   verifyFormat(
4142       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4143       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4144       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4145       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4146       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4147       NoBinPacking);
4148   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4149                "    .aaaaaaaaaaaaaaaaaa();",
4150                NoBinPacking);
4151   verifyFormat("void f() {\n"
4152                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4153                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4154                "}",
4155                NoBinPacking);
4156 
4157   verifyFormat(
4158       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4159       "             aaaaaaaaaaaa,\n"
4160       "             aaaaaaaaaaaa);",
4161       NoBinPacking);
4162   verifyFormat(
4163       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4164       "                               ddddddddddddddddddddddddddddd),\n"
4165       "             test);",
4166       NoBinPacking);
4167 
4168   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4169                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4170                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4171                "    aaaaaaaaaaaaaaaaaa;",
4172                NoBinPacking);
4173   verifyFormat("a(\"a\"\n"
4174                "  \"a\",\n"
4175                "  a);");
4176 
4177   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4178   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4179                "                aaaaaaaaa,\n"
4180                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4181                NoBinPacking);
4182   verifyFormat(
4183       "void f() {\n"
4184       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4185       "      .aaaaaaa();\n"
4186       "}",
4187       NoBinPacking);
4188   verifyFormat(
4189       "template <class SomeType, class SomeOtherType>\n"
4190       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4191       NoBinPacking);
4192 }
4193 
4194 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4195   FormatStyle Style = getLLVMStyleWithColumns(15);
4196   Style.ExperimentalAutoDetectBinPacking = true;
4197   EXPECT_EQ("aaa(aaaa,\n"
4198             "    aaaa,\n"
4199             "    aaaa);\n"
4200             "aaa(aaaa,\n"
4201             "    aaaa,\n"
4202             "    aaaa);",
4203             format("aaa(aaaa,\n" // one-per-line
4204                    "  aaaa,\n"
4205                    "    aaaa  );\n"
4206                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4207                    Style));
4208   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4209             "    aaaa);\n"
4210             "aaa(aaaa, aaaa,\n"
4211             "    aaaa);",
4212             format("aaa(aaaa,  aaaa,\n" // bin-packed
4213                    "    aaaa  );\n"
4214                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4215                    Style));
4216 }
4217 
4218 TEST_F(FormatTest, FormatsBuilderPattern) {
4219   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4220                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4221                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4222                "    .StartsWith(\".init\", ORDER_INIT)\n"
4223                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4224                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4225                "    .Default(ORDER_TEXT);\n");
4226 
4227   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4228                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4229   verifyFormat(
4230       "aaaaaaa->aaaaaaa\n"
4231       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4232       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4233       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4234   verifyFormat(
4235       "aaaaaaa->aaaaaaa\n"
4236       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4237       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4238   verifyFormat(
4239       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4240       "    aaaaaaaaaaaaaa);");
4241   verifyFormat(
4242       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4243       "    aaaaaa->aaaaaaaaaaaa()\n"
4244       "        ->aaaaaaaaaaaaaaaa(\n"
4245       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4246       "        ->aaaaaaaaaaaaaaaaa();");
4247   verifyGoogleFormat(
4248       "void f() {\n"
4249       "  someo->Add((new util::filetools::Handler(dir))\n"
4250       "                 ->OnEvent1(NewPermanentCallback(\n"
4251       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4252       "                 ->OnEvent2(NewPermanentCallback(\n"
4253       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4254       "                 ->OnEvent3(NewPermanentCallback(\n"
4255       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4256       "                 ->OnEvent5(NewPermanentCallback(\n"
4257       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4258       "                 ->OnEvent6(NewPermanentCallback(\n"
4259       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4260       "}");
4261 
4262   verifyFormat(
4263       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4264   verifyFormat("aaaaaaaaaaaaaaa()\n"
4265                "    .aaaaaaaaaaaaaaa()\n"
4266                "    .aaaaaaaaaaaaaaa()\n"
4267                "    .aaaaaaaaaaaaaaa()\n"
4268                "    .aaaaaaaaaaaaaaa();");
4269   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4270                "    .aaaaaaaaaaaaaaa()\n"
4271                "    .aaaaaaaaaaaaaaa()\n"
4272                "    .aaaaaaaaaaaaaaa();");
4273   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4274                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4275                "    .aaaaaaaaaaaaaaa();");
4276   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4277                "    ->aaaaaaaaaaaaaae(0)\n"
4278                "    ->aaaaaaaaaaaaaaa();");
4279 
4280   // Don't linewrap after very short segments.
4281   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4282                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4283                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4284   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4285                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4286                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4287   verifyFormat("aaa()\n"
4288                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4289                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4290                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4291 
4292   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4293                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4294                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4295   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4296                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4297                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4298 
4299   // Prefer not to break after empty parentheses.
4300   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4301                "    First->LastNewlineOffset);");
4302 
4303   // Prefer not to create "hanging" indents.
4304   verifyFormat(
4305       "return !soooooooooooooome_map\n"
4306       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4307       "            .second;");
4308   verifyFormat(
4309       "return aaaaaaaaaaaaaaaa\n"
4310       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4311       "    .aaaa(aaaaaaaaaaaaaa);");
4312   // No hanging indent here.
4313   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4314                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4315   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4316                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4317   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4318                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4319                getLLVMStyleWithColumns(60));
4320   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4321                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4322                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4323                getLLVMStyleWithColumns(59));
4324   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4325                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4326                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4327 }
4328 
4329 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4330   verifyFormat(
4331       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4332       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4333   verifyFormat(
4334       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4335       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4336 
4337   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4338                "    ccccccccccccccccccccccccc) {\n}");
4339   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4340                "    ccccccccccccccccccccccccc) {\n}");
4341 
4342   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4343                "    ccccccccccccccccccccccccc) {\n}");
4344   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4345                "    ccccccccccccccccccccccccc) {\n}");
4346 
4347   verifyFormat(
4348       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4349       "    ccccccccccccccccccccccccc) {\n}");
4350   verifyFormat(
4351       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4352       "    ccccccccccccccccccccccccc) {\n}");
4353 
4354   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4355                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4356                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4357                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4358   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4359                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4360                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4361                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4362 
4363   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4364                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4365                "    aaaaaaaaaaaaaaa != aa) {\n}");
4366   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4367                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4368                "    aaaaaaaaaaaaaaa != aa) {\n}");
4369 }
4370 
4371 TEST_F(FormatTest, BreaksAfterAssignments) {
4372   verifyFormat(
4373       "unsigned Cost =\n"
4374       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4375       "                        SI->getPointerAddressSpaceee());\n");
4376   verifyFormat(
4377       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4378       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4379 
4380   verifyFormat(
4381       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4382       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4383   verifyFormat("unsigned OriginalStartColumn =\n"
4384                "    SourceMgr.getSpellingColumnNumber(\n"
4385                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4386                "    1;");
4387 }
4388 
4389 TEST_F(FormatTest, AlignsAfterAssignments) {
4390   verifyFormat(
4391       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4392       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4393   verifyFormat(
4394       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4395       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4396   verifyFormat(
4397       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4398       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4399   verifyFormat(
4400       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4401       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4402   verifyFormat(
4403       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4404       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4405       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4406 }
4407 
4408 TEST_F(FormatTest, AlignsAfterReturn) {
4409   verifyFormat(
4410       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4411       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4412   verifyFormat(
4413       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4414       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4415   verifyFormat(
4416       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4417       "       aaaaaaaaaaaaaaaaaaaaaa();");
4418   verifyFormat(
4419       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4420       "        aaaaaaaaaaaaaaaaaaaaaa());");
4421   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4422                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4423   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4424                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4425                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4426   verifyFormat("return\n"
4427                "    // true if code is one of a or b.\n"
4428                "    code == a || code == b;");
4429 }
4430 
4431 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4432   verifyFormat(
4433       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4434       "                                                aaaaaaaaa aaaaaaa) {}");
4435   verifyFormat(
4436       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4437       "                                               aaaaaaaaaaa aaaaaaaaa);");
4438   verifyFormat(
4439       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4440       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4441   FormatStyle Style = getLLVMStyle();
4442   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4443   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4444                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4445                Style);
4446   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4447                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4448                Style);
4449   verifyFormat("SomeLongVariableName->someFunction(\n"
4450                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4451                Style);
4452   verifyFormat(
4453       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4454       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4455       Style);
4456   verifyFormat(
4457       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4458       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4459       Style);
4460   verifyFormat(
4461       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4462       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4463       Style);
4464 
4465   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4466                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4467                "        b));",
4468                Style);
4469 
4470   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4471   Style.BinPackArguments = false;
4472   Style.BinPackParameters = false;
4473   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4474                "    aaaaaaaaaaa aaaaaaaa,\n"
4475                "    aaaaaaaaa aaaaaaa,\n"
4476                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4477                Style);
4478   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4479                "    aaaaaaaaaaa aaaaaaaaa,\n"
4480                "    aaaaaaaaaaa aaaaaaaaa,\n"
4481                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4482                Style);
4483   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4484                "    aaaaaaaaaaaaaaa,\n"
4485                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4486                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4487                Style);
4488   verifyFormat(
4489       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4490       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4491       Style);
4492   verifyFormat(
4493       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4494       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4495       Style);
4496   verifyFormat(
4497       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4498       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4499       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4500       "    aaaaaaaaaaaaaaaa);",
4501       Style);
4502   verifyFormat(
4503       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4504       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4505       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4506       "    aaaaaaaaaaaaaaaa);",
4507       Style);
4508 }
4509 
4510 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4511   FormatStyle Style = getLLVMStyleWithColumns(40);
4512   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4513                "          bbbbbbbbbbbbbbbbbbbbbb);",
4514                Style);
4515   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4516   Style.AlignOperands = false;
4517   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4518                "          bbbbbbbbbbbbbbbbbbbbbb);",
4519                Style);
4520   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4521   Style.AlignOperands = true;
4522   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4523                "          bbbbbbbbbbbbbbbbbbbbbb);",
4524                Style);
4525   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4526   Style.AlignOperands = false;
4527   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4528                "    bbbbbbbbbbbbbbbbbbbbbb);",
4529                Style);
4530 }
4531 
4532 TEST_F(FormatTest, BreaksConditionalExpressions) {
4533   verifyFormat(
4534       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4535       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4536       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4537   verifyFormat(
4538       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4539       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4540   verifyFormat(
4541       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4542       "                                                    : aaaaaaaaaaaaa);");
4543   verifyFormat(
4544       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4545       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4546       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4547       "                   aaaaaaaaaaaaa);");
4548   verifyFormat(
4549       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4550       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4551       "                   aaaaaaaaaaaaa);");
4552   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4553                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4554                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4555                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4556                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4557   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4558                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4559                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4560                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4561                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4562                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4563                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4564   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4565                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4566                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4567                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4568                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4569   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4570                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4571                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4572   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4573                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4574                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4575                "        : aaaaaaaaaaaaaaaa;");
4576   verifyFormat(
4577       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4578       "    ? aaaaaaaaaaaaaaa\n"
4579       "    : aaaaaaaaaaaaaaa;");
4580   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4581                "          aaaaaaaaa\n"
4582                "      ? b\n"
4583                "      : c);");
4584   verifyFormat("return aaaa == bbbb\n"
4585                "           // comment\n"
4586                "           ? aaaa\n"
4587                "           : bbbb;");
4588   verifyFormat("unsigned Indent =\n"
4589                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4590                "                              ? IndentForLevel[TheLine.Level]\n"
4591                "                              : TheLine * 2,\n"
4592                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4593                getLLVMStyleWithColumns(70));
4594   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4595                "                  ? aaaaaaaaaaaaaaa\n"
4596                "                  : bbbbbbbbbbbbbbb //\n"
4597                "                        ? ccccccccccccccc\n"
4598                "                        : ddddddddddddddd;");
4599   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4600                "                  ? aaaaaaaaaaaaaaa\n"
4601                "                  : (bbbbbbbbbbbbbbb //\n"
4602                "                         ? ccccccccccccccc\n"
4603                "                         : ddddddddddddddd);");
4604   verifyFormat(
4605       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4606       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4607       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4608       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4609       "                                      : aaaaaaaaaa;");
4610   verifyFormat(
4611       "aaaaaa = aaaaaaaaaaaa\n"
4612       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4613       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4614       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4615 
4616   FormatStyle NoBinPacking = getLLVMStyle();
4617   NoBinPacking.BinPackArguments = false;
4618   verifyFormat(
4619       "void f() {\n"
4620       "  g(aaa,\n"
4621       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4622       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4623       "        ? aaaaaaaaaaaaaaa\n"
4624       "        : aaaaaaaaaaaaaaa);\n"
4625       "}",
4626       NoBinPacking);
4627   verifyFormat(
4628       "void f() {\n"
4629       "  g(aaa,\n"
4630       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4631       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4632       "        ?: aaaaaaaaaaaaaaa);\n"
4633       "}",
4634       NoBinPacking);
4635 
4636   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4637                "             // comment.\n"
4638                "             ccccccccccccccccccccccccccccccccccccccc\n"
4639                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4640                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4641 
4642   // Assignments in conditional expressions. Apparently not uncommon :-(.
4643   verifyFormat("return a != b\n"
4644                "           // comment\n"
4645                "           ? a = b\n"
4646                "           : a = b;");
4647   verifyFormat("return a != b\n"
4648                "           // comment\n"
4649                "           ? a = a != b\n"
4650                "                     // comment\n"
4651                "                     ? a = b\n"
4652                "                     : a\n"
4653                "           : a;\n");
4654   verifyFormat("return a != b\n"
4655                "           // comment\n"
4656                "           ? a\n"
4657                "           : a = a != b\n"
4658                "                     // comment\n"
4659                "                     ? a = b\n"
4660                "                     : a;");
4661 }
4662 
4663 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4664   FormatStyle Style = getLLVMStyle();
4665   Style.BreakBeforeTernaryOperators = false;
4666   Style.ColumnLimit = 70;
4667   verifyFormat(
4668       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4669       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4670       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4671       Style);
4672   verifyFormat(
4673       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4674       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4675       Style);
4676   verifyFormat(
4677       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4678       "                                                      aaaaaaaaaaaaa);",
4679       Style);
4680   verifyFormat(
4681       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4682       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4683       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4684       "                   aaaaaaaaaaaaa);",
4685       Style);
4686   verifyFormat(
4687       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4688       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4689       "                   aaaaaaaaaaaaa);",
4690       Style);
4691   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4692                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4693                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4694                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4695                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4696                Style);
4697   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4698                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4699                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4700                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4701                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4702                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4703                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4704                Style);
4705   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4706                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4707                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4708                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4709                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4710                Style);
4711   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4712                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4713                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4714                Style);
4715   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4716                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4717                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4718                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4719                Style);
4720   verifyFormat(
4721       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4722       "    aaaaaaaaaaaaaaa :\n"
4723       "    aaaaaaaaaaaaaaa;",
4724       Style);
4725   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4726                "          aaaaaaaaa ?\n"
4727                "      b :\n"
4728                "      c);",
4729                Style);
4730   verifyFormat(
4731       "unsigned Indent =\n"
4732       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4733       "                              IndentForLevel[TheLine.Level] :\n"
4734       "                              TheLine * 2,\n"
4735       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4736       Style);
4737   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4738                "                  aaaaaaaaaaaaaaa :\n"
4739                "                  bbbbbbbbbbbbbbb ? //\n"
4740                "                      ccccccccccccccc :\n"
4741                "                      ddddddddddddddd;",
4742                Style);
4743   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4744                "                  aaaaaaaaaaaaaaa :\n"
4745                "                  (bbbbbbbbbbbbbbb ? //\n"
4746                "                       ccccccccccccccc :\n"
4747                "                       ddddddddddddddd);",
4748                Style);
4749   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4750                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4751                "            ccccccccccccccccccccccccccc;",
4752                Style);
4753   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4754                "           aaaaa :\n"
4755                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4756                Style);
4757 }
4758 
4759 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4760   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4761                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4762   verifyFormat("bool a = true, b = false;");
4763 
4764   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4765                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4766                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4767                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4768   verifyFormat(
4769       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4770       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4771       "     d = e && f;");
4772   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4773                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4774   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4775                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4776   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4777                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4778 
4779   FormatStyle Style = getGoogleStyle();
4780   Style.PointerAlignment = FormatStyle::PAS_Left;
4781   Style.DerivePointerAlignment = false;
4782   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4783                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4784                "    *b = bbbbbbbbbbbbbbbbbbb;",
4785                Style);
4786   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4787                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4788                Style);
4789 }
4790 
4791 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4792   verifyFormat("arr[foo ? bar : baz];");
4793   verifyFormat("f()[foo ? bar : baz];");
4794   verifyFormat("(a + b)[foo ? bar : baz];");
4795   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4796 }
4797 
4798 TEST_F(FormatTest, AlignsStringLiterals) {
4799   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4800                "                                      \"short literal\");");
4801   verifyFormat(
4802       "looooooooooooooooooooooooongFunction(\n"
4803       "    \"short literal\"\n"
4804       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4805   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4806                "             \" string literals\",\n"
4807                "             and, other, parameters);");
4808   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4809             "      \"5678\";",
4810             format("fun + \"1243\" /* comment */\n"
4811                    "      \"5678\";",
4812                    getLLVMStyleWithColumns(28)));
4813   EXPECT_EQ(
4814       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4815       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4816       "         \"aaaaaaaaaaaaaaaa\";",
4817       format("aaaaaa ="
4818              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4819              "aaaaaaaaaaaaaaaaaaaaa\" "
4820              "\"aaaaaaaaaaaaaaaa\";"));
4821   verifyFormat("a = a + \"a\"\n"
4822                "        \"a\"\n"
4823                "        \"a\";");
4824   verifyFormat("f(\"a\", \"b\"\n"
4825                "       \"c\");");
4826 
4827   verifyFormat(
4828       "#define LL_FORMAT \"ll\"\n"
4829       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4830       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4831 
4832   verifyFormat("#define A(X)          \\\n"
4833                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4834                "  \"ccccc\"",
4835                getLLVMStyleWithColumns(23));
4836   verifyFormat("#define A \"def\"\n"
4837                "f(\"abc\" A \"ghi\"\n"
4838                "  \"jkl\");");
4839 
4840   verifyFormat("f(L\"a\"\n"
4841                "  L\"b\");");
4842   verifyFormat("#define A(X)            \\\n"
4843                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4844                "  L\"ccccc\"",
4845                getLLVMStyleWithColumns(25));
4846 
4847   verifyFormat("f(@\"a\"\n"
4848                "  @\"b\");");
4849   verifyFormat("NSString s = @\"a\"\n"
4850                "             @\"b\"\n"
4851                "             @\"c\";");
4852   verifyFormat("NSString s = @\"a\"\n"
4853                "              \"b\"\n"
4854                "              \"c\";");
4855 }
4856 
4857 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4858   FormatStyle Style = getLLVMStyle();
4859   // No declarations or definitions should be moved to own line.
4860   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4861   verifyFormat("class A {\n"
4862                "  int f() { return 1; }\n"
4863                "  int g();\n"
4864                "};\n"
4865                "int f() { return 1; }\n"
4866                "int g();\n",
4867                Style);
4868 
4869   // All declarations and definitions should have the return type moved to its
4870   // own
4871   // line.
4872   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4873   verifyFormat("class E {\n"
4874                "  int\n"
4875                "  f() {\n"
4876                "    return 1;\n"
4877                "  }\n"
4878                "  int\n"
4879                "  g();\n"
4880                "};\n"
4881                "int\n"
4882                "f() {\n"
4883                "  return 1;\n"
4884                "}\n"
4885                "int\n"
4886                "g();\n",
4887                Style);
4888 
4889   // Top-level definitions, and no kinds of declarations should have the
4890   // return type moved to its own line.
4891   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4892   verifyFormat("class B {\n"
4893                "  int f() { return 1; }\n"
4894                "  int g();\n"
4895                "};\n"
4896                "int\n"
4897                "f() {\n"
4898                "  return 1;\n"
4899                "}\n"
4900                "int g();\n",
4901                Style);
4902 
4903   // Top-level definitions and declarations should have the return type moved
4904   // to its own line.
4905   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4906   verifyFormat("class C {\n"
4907                "  int f() { return 1; }\n"
4908                "  int g();\n"
4909                "};\n"
4910                "int\n"
4911                "f() {\n"
4912                "  return 1;\n"
4913                "}\n"
4914                "int\n"
4915                "g();\n",
4916                Style);
4917 
4918   // All definitions should have the return type moved to its own line, but no
4919   // kinds of declarations.
4920   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4921   verifyFormat("class D {\n"
4922                "  int\n"
4923                "  f() {\n"
4924                "    return 1;\n"
4925                "  }\n"
4926                "  int g();\n"
4927                "};\n"
4928                "int\n"
4929                "f() {\n"
4930                "  return 1;\n"
4931                "}\n"
4932                "int g();\n",
4933                Style);
4934   verifyFormat("const char *\n"
4935                "f(void) {\n" // Break here.
4936                "  return \"\";\n"
4937                "}\n"
4938                "const char *bar(void);\n", // No break here.
4939                Style);
4940   verifyFormat("template <class T>\n"
4941                "T *\n"
4942                "f(T &c) {\n" // Break here.
4943                "  return NULL;\n"
4944                "}\n"
4945                "template <class T> T *f(T &c);\n", // No break here.
4946                Style);
4947   verifyFormat("class C {\n"
4948                "  int\n"
4949                "  operator+() {\n"
4950                "    return 1;\n"
4951                "  }\n"
4952                "  int\n"
4953                "  operator()() {\n"
4954                "    return 1;\n"
4955                "  }\n"
4956                "};\n",
4957                Style);
4958   verifyFormat("void\n"
4959                "A::operator()() {}\n"
4960                "void\n"
4961                "A::operator>>() {}\n"
4962                "void\n"
4963                "A::operator+() {}\n",
4964                Style);
4965   verifyFormat("void *operator new(std::size_t s);", // No break here.
4966                Style);
4967   verifyFormat("void *\n"
4968                "operator new(std::size_t s) {}",
4969                Style);
4970   verifyFormat("void *\n"
4971                "operator delete[](void *ptr) {}",
4972                Style);
4973   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4974   verifyFormat("const char *\n"
4975                "f(void)\n" // Break here.
4976                "{\n"
4977                "  return \"\";\n"
4978                "}\n"
4979                "const char *bar(void);\n", // No break here.
4980                Style);
4981   verifyFormat("template <class T>\n"
4982                "T *\n"     // Problem here: no line break
4983                "f(T &c)\n" // Break here.
4984                "{\n"
4985                "  return NULL;\n"
4986                "}\n"
4987                "template <class T> T *f(T &c);\n", // No break here.
4988                Style);
4989 }
4990 
4991 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4992   FormatStyle NoBreak = getLLVMStyle();
4993   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4994   FormatStyle Break = getLLVMStyle();
4995   Break.AlwaysBreakBeforeMultilineStrings = true;
4996   verifyFormat("aaaa = \"bbbb\"\n"
4997                "       \"cccc\";",
4998                NoBreak);
4999   verifyFormat("aaaa =\n"
5000                "    \"bbbb\"\n"
5001                "    \"cccc\";",
5002                Break);
5003   verifyFormat("aaaa(\"bbbb\"\n"
5004                "     \"cccc\");",
5005                NoBreak);
5006   verifyFormat("aaaa(\n"
5007                "    \"bbbb\"\n"
5008                "    \"cccc\");",
5009                Break);
5010   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5011                "          \"cccc\");",
5012                NoBreak);
5013   verifyFormat("aaaa(qqq,\n"
5014                "     \"bbbb\"\n"
5015                "     \"cccc\");",
5016                Break);
5017   verifyFormat("aaaa(qqq,\n"
5018                "     L\"bbbb\"\n"
5019                "     L\"cccc\");",
5020                Break);
5021   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5022                "                      \"bbbb\"));",
5023                Break);
5024   verifyFormat("string s = someFunction(\n"
5025                "    \"abc\"\n"
5026                "    \"abc\");",
5027                Break);
5028 
5029   // As we break before unary operators, breaking right after them is bad.
5030   verifyFormat("string foo = abc ? \"x\"\n"
5031                "                   \"blah blah blah blah blah blah\"\n"
5032                "                 : \"y\";",
5033                Break);
5034 
5035   // Don't break if there is no column gain.
5036   verifyFormat("f(\"aaaa\"\n"
5037                "  \"bbbb\");",
5038                Break);
5039 
5040   // Treat literals with escaped newlines like multi-line string literals.
5041   EXPECT_EQ("x = \"a\\\n"
5042             "b\\\n"
5043             "c\";",
5044             format("x = \"a\\\n"
5045                    "b\\\n"
5046                    "c\";",
5047                    NoBreak));
5048   EXPECT_EQ("xxxx =\n"
5049             "    \"a\\\n"
5050             "b\\\n"
5051             "c\";",
5052             format("xxxx = \"a\\\n"
5053                    "b\\\n"
5054                    "c\";",
5055                    Break));
5056 
5057   // Exempt ObjC strings for now.
5058   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
5059             "                          @\"bbbb\";",
5060             format("NSString *const kString = @\"aaaa\"\n"
5061                    "@\"bbbb\";",
5062                    Break));
5063 
5064   Break.ColumnLimit = 0;
5065   verifyFormat("const char *hello = \"hello llvm\";", Break);
5066 }
5067 
5068 TEST_F(FormatTest, AlignsPipes) {
5069   verifyFormat(
5070       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5071       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5072       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5073   verifyFormat(
5074       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5075       "                     << aaaaaaaaaaaaaaaaaaaa;");
5076   verifyFormat(
5077       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5078       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5079   verifyFormat(
5080       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5081       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5082       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5083   verifyFormat(
5084       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5085       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5086       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5087   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5088                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5089                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5090                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5091   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5092                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5093   verifyFormat(
5094       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5095       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5096 
5097   verifyFormat("return out << \"somepacket = {\\n\"\n"
5098                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5099                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5100                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5101                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5102                "           << \"}\";");
5103 
5104   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5105                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5106                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5107   verifyFormat(
5108       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5109       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5110       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5111       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5112       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5113   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5114                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5115   verifyFormat(
5116       "void f() {\n"
5117       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5118       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5119       "}");
5120   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5121                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5122   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5123                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5124                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5125                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5126   verifyFormat("LOG_IF(aaa == //\n"
5127                "       bbb)\n"
5128                "    << a << b;");
5129 
5130   // Breaking before the first "<<" is generally not desirable.
5131   verifyFormat(
5132       "llvm::errs()\n"
5133       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5134       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5135       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5136       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5137       getLLVMStyleWithColumns(70));
5138   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5139                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5140                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5141                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5142                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5143                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5144                getLLVMStyleWithColumns(70));
5145 
5146   // But sometimes, breaking before the first "<<" is desirable.
5147   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5148                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5149   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5150                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5151                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5152   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5153                "    << BEF << IsTemplate << Description << E->getType();");
5154   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5155                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5156                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5157   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5158                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5159                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5160                "    << aaa;");
5161 
5162   verifyFormat(
5163       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5164       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5165 
5166   // Incomplete string literal.
5167   EXPECT_EQ("llvm::errs() << \"\n"
5168             "             << a;",
5169             format("llvm::errs() << \"\n<<a;"));
5170 
5171   verifyFormat("void f() {\n"
5172                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5173                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5174                "}");
5175 
5176   // Handle 'endl'.
5177   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5178                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5179   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5180 
5181   // Handle '\n'.
5182   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5183                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5184   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5185                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5186   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5187                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5188   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5189 }
5190 
5191 TEST_F(FormatTest, UnderstandsEquals) {
5192   verifyFormat(
5193       "aaaaaaaaaaaaaaaaa =\n"
5194       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5195   verifyFormat(
5196       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5197       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5198   verifyFormat(
5199       "if (a) {\n"
5200       "  f();\n"
5201       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5202       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5203       "}");
5204 
5205   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5206                "        100000000 + 10000000) {\n}");
5207 }
5208 
5209 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5210   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5211                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5212 
5213   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5214                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5215 
5216   verifyFormat(
5217       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5218       "                                                          Parameter2);");
5219 
5220   verifyFormat(
5221       "ShortObject->shortFunction(\n"
5222       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5223       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5224 
5225   verifyFormat("loooooooooooooongFunction(\n"
5226                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5227 
5228   verifyFormat(
5229       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5230       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5231 
5232   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5233                "    .WillRepeatedly(Return(SomeValue));");
5234   verifyFormat("void f() {\n"
5235                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5236                "      .Times(2)\n"
5237                "      .WillRepeatedly(Return(SomeValue));\n"
5238                "}");
5239   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5240                "    ccccccccccccccccccccccc);");
5241   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5242                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5243                "          .aaaaa(aaaaa),\n"
5244                "      aaaaaaaaaaaaaaaaaaaaa);");
5245   verifyFormat("void f() {\n"
5246                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5247                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5248                "}");
5249   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5250                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5251                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5252                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5253                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5254   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5255                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5256                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5257                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5258                "}");
5259 
5260   // Here, it is not necessary to wrap at "." or "->".
5261   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5262                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5263   verifyFormat(
5264       "aaaaaaaaaaa->aaaaaaaaa(\n"
5265       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5266       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5267 
5268   verifyFormat(
5269       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5270       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5271   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5272                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5273   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5274                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5275 
5276   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5277                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5278                "    .a();");
5279 
5280   FormatStyle NoBinPacking = getLLVMStyle();
5281   NoBinPacking.BinPackParameters = false;
5282   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5283                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5284                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5285                "                         aaaaaaaaaaaaaaaaaaa,\n"
5286                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5287                NoBinPacking);
5288 
5289   // If there is a subsequent call, change to hanging indentation.
5290   verifyFormat(
5291       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5292       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5293       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5294   verifyFormat(
5295       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5296       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5297   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5298                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5299                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5300   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5301                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5302                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5303 }
5304 
5305 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5306   verifyFormat("template <typename T>\n"
5307                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5308   verifyFormat("template <typename T>\n"
5309                "// T should be one of {A, B}.\n"
5310                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5311   verifyFormat(
5312       "template <typename T>\n"
5313       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5314   verifyFormat("template <typename T>\n"
5315                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5316                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5317   verifyFormat(
5318       "template <typename T>\n"
5319       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5320       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5321   verifyFormat(
5322       "template <typename T>\n"
5323       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5324       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5325       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5326   verifyFormat("template <typename T>\n"
5327                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5328                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5329   verifyFormat(
5330       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5331       "          typename T4 = char>\n"
5332       "void f();");
5333   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5334                "          template <typename> class cccccccccccccccccccccc,\n"
5335                "          typename ddddddddddddd>\n"
5336                "class C {};");
5337   verifyFormat(
5338       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5339       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5340 
5341   verifyFormat("void f() {\n"
5342                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5343                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5344                "}");
5345 
5346   verifyFormat("template <typename T> class C {};");
5347   verifyFormat("template <typename T> void f();");
5348   verifyFormat("template <typename T> void f() {}");
5349   verifyFormat(
5350       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5351       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5352       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5353       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5354       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5355       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5356       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5357       getLLVMStyleWithColumns(72));
5358   EXPECT_EQ("static_cast<A< //\n"
5359             "    B> *>(\n"
5360             "\n"
5361             "    );",
5362             format("static_cast<A<//\n"
5363                    "    B>*>(\n"
5364                    "\n"
5365                    "    );"));
5366   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5367                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5368 
5369   FormatStyle AlwaysBreak = getLLVMStyle();
5370   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5371   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5372   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5373   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5374   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5375                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5376                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5377   verifyFormat("template <template <typename> class Fooooooo,\n"
5378                "          template <typename> class Baaaaaaar>\n"
5379                "struct C {};",
5380                AlwaysBreak);
5381   verifyFormat("template <typename T> // T can be A, B or C.\n"
5382                "struct C {};",
5383                AlwaysBreak);
5384 }
5385 
5386 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5387   verifyFormat(
5388       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5389       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5390   verifyFormat(
5391       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5392       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5393       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5394 
5395   // FIXME: Should we have the extra indent after the second break?
5396   verifyFormat(
5397       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5398       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5399       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5400 
5401   verifyFormat(
5402       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5403       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5404 
5405   // Breaking at nested name specifiers is generally not desirable.
5406   verifyFormat(
5407       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5408       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5409 
5410   verifyFormat(
5411       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5412       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5413       "                   aaaaaaaaaaaaaaaaaaaaa);",
5414       getLLVMStyleWithColumns(74));
5415 
5416   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5417                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5418                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5419 }
5420 
5421 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5422   verifyFormat("A<int> a;");
5423   verifyFormat("A<A<A<int>>> a;");
5424   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5425   verifyFormat("bool x = a < 1 || 2 > a;");
5426   verifyFormat("bool x = 5 < f<int>();");
5427   verifyFormat("bool x = f<int>() > 5;");
5428   verifyFormat("bool x = 5 < a<int>::x;");
5429   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5430   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5431 
5432   verifyGoogleFormat("A<A<int>> a;");
5433   verifyGoogleFormat("A<A<A<int>>> a;");
5434   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5435   verifyGoogleFormat("A<A<int> > a;");
5436   verifyGoogleFormat("A<A<A<int> > > a;");
5437   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5438   verifyGoogleFormat("A<::A<int>> a;");
5439   verifyGoogleFormat("A<::A> a;");
5440   verifyGoogleFormat("A< ::A> a;");
5441   verifyGoogleFormat("A< ::A<int> > a;");
5442   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5443   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5444   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5445   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5446   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5447             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5448 
5449   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5450 
5451   verifyFormat("test >> a >> b;");
5452   verifyFormat("test << a >> b;");
5453 
5454   verifyFormat("f<int>();");
5455   verifyFormat("template <typename T> void f() {}");
5456   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5457   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5458                "sizeof(char)>::type>;");
5459   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5460   verifyFormat("f(a.operator()<A>());");
5461   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5462                "      .template operator()<A>());",
5463                getLLVMStyleWithColumns(35));
5464 
5465   // Not template parameters.
5466   verifyFormat("return a < b && c > d;");
5467   verifyFormat("void f() {\n"
5468                "  while (a < b && c > d) {\n"
5469                "  }\n"
5470                "}");
5471   verifyFormat("template <typename... Types>\n"
5472                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5473 
5474   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5475                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5476                getLLVMStyleWithColumns(60));
5477   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5478   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5479   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5480 }
5481 
5482 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5483   verifyFormat("COMPARE(a, ==, b);");
5484 }
5485 
5486 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5487   verifyFormat("int A::*x;");
5488   verifyFormat("int (S::*func)(void *);");
5489   verifyFormat("void f() { int (S::*func)(void *); }");
5490   verifyFormat("typedef bool *(Class::*Member)() const;");
5491   verifyFormat("void f() {\n"
5492                "  (a->*f)();\n"
5493                "  a->*x;\n"
5494                "  (a.*f)();\n"
5495                "  ((*a).*f)();\n"
5496                "  a.*x;\n"
5497                "}");
5498   verifyFormat("void f() {\n"
5499                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5500                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5501                "}");
5502   verifyFormat(
5503       "(aaaaaaaaaa->*bbbbbbb)(\n"
5504       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5505   FormatStyle Style = getLLVMStyle();
5506   Style.PointerAlignment = FormatStyle::PAS_Left;
5507   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5508 }
5509 
5510 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5511   verifyFormat("int a = -2;");
5512   verifyFormat("f(-1, -2, -3);");
5513   verifyFormat("a[-1] = 5;");
5514   verifyFormat("int a = 5 + -2;");
5515   verifyFormat("if (i == -1) {\n}");
5516   verifyFormat("if (i != -1) {\n}");
5517   verifyFormat("if (i > -1) {\n}");
5518   verifyFormat("if (i < -1) {\n}");
5519   verifyFormat("++(a->f());");
5520   verifyFormat("--(a->f());");
5521   verifyFormat("(a->f())++;");
5522   verifyFormat("a[42]++;");
5523   verifyFormat("if (!(a->f())) {\n}");
5524 
5525   verifyFormat("a-- > b;");
5526   verifyFormat("b ? -a : c;");
5527   verifyFormat("n * sizeof char16;");
5528   verifyFormat("n * alignof char16;", getGoogleStyle());
5529   verifyFormat("sizeof(char);");
5530   verifyFormat("alignof(char);", getGoogleStyle());
5531 
5532   verifyFormat("return -1;");
5533   verifyFormat("switch (a) {\n"
5534                "case -1:\n"
5535                "  break;\n"
5536                "}");
5537   verifyFormat("#define X -1");
5538   verifyFormat("#define X -kConstant");
5539 
5540   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5541   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5542 
5543   verifyFormat("int a = /* confusing comment */ -1;");
5544   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5545   verifyFormat("int a = i /* confusing comment */++;");
5546 }
5547 
5548 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5549   verifyFormat("if (!aaaaaaaaaa( // break\n"
5550                "        aaaaa)) {\n"
5551                "}");
5552   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5553                "    aaaaa));");
5554   verifyFormat("*aaa = aaaaaaa( // break\n"
5555                "    bbbbbb);");
5556 }
5557 
5558 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5559   verifyFormat("bool operator<();");
5560   verifyFormat("bool operator>();");
5561   verifyFormat("bool operator=();");
5562   verifyFormat("bool operator==();");
5563   verifyFormat("bool operator!=();");
5564   verifyFormat("int operator+();");
5565   verifyFormat("int operator++();");
5566   verifyFormat("bool operator,();");
5567   verifyFormat("bool operator();");
5568   verifyFormat("bool operator()();");
5569   verifyFormat("bool operator[]();");
5570   verifyFormat("operator bool();");
5571   verifyFormat("operator int();");
5572   verifyFormat("operator void *();");
5573   verifyFormat("operator SomeType<int>();");
5574   verifyFormat("operator SomeType<int, int>();");
5575   verifyFormat("operator SomeType<SomeType<int>>();");
5576   verifyFormat("void *operator new(std::size_t size);");
5577   verifyFormat("void *operator new[](std::size_t size);");
5578   verifyFormat("void operator delete(void *ptr);");
5579   verifyFormat("void operator delete[](void *ptr);");
5580   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5581                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5582   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5583                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5584 
5585   verifyFormat(
5586       "ostream &operator<<(ostream &OutputStream,\n"
5587       "                    SomeReallyLongType WithSomeReallyLongValue);");
5588   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5589                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5590                "  return left.group < right.group;\n"
5591                "}");
5592   verifyFormat("SomeType &operator=(const SomeType &S);");
5593   verifyFormat("f.template operator()<int>();");
5594 
5595   verifyGoogleFormat("operator void*();");
5596   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5597   verifyGoogleFormat("operator ::A();");
5598 
5599   verifyFormat("using A::operator+;");
5600   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5601                "int i;");
5602 }
5603 
5604 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5605   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5606   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5607   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5608   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5609   verifyFormat("Deleted &operator=(const Deleted &) &;");
5610   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5611   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5612   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5613   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5614   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5615   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5616 
5617   FormatStyle AlignLeft = getLLVMStyle();
5618   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5619   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5620   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5621                AlignLeft);
5622   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5623   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5624   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5625   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5626   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5627   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5628 
5629   FormatStyle Spaces = getLLVMStyle();
5630   Spaces.SpacesInCStyleCastParentheses = true;
5631   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5632   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5633   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5634   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5635 
5636   Spaces.SpacesInCStyleCastParentheses = false;
5637   Spaces.SpacesInParentheses = true;
5638   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5639   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5640   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5641   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5642 }
5643 
5644 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5645   verifyFormat("void f() {\n"
5646                "  A *a = new A;\n"
5647                "  A *a = new (placement) A;\n"
5648                "  delete a;\n"
5649                "  delete (A *)a;\n"
5650                "}");
5651   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5652                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5653   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5654                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5655                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5656   verifyFormat("delete[] h->p;");
5657 }
5658 
5659 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5660   verifyFormat("int *f(int *a) {}");
5661   verifyFormat("int main(int argc, char **argv) {}");
5662   verifyFormat("Test::Test(int b) : a(b * b) {}");
5663   verifyIndependentOfContext("f(a, *a);");
5664   verifyFormat("void g() { f(*a); }");
5665   verifyIndependentOfContext("int a = b * 10;");
5666   verifyIndependentOfContext("int a = 10 * b;");
5667   verifyIndependentOfContext("int a = b * c;");
5668   verifyIndependentOfContext("int a += b * c;");
5669   verifyIndependentOfContext("int a -= b * c;");
5670   verifyIndependentOfContext("int a *= b * c;");
5671   verifyIndependentOfContext("int a /= b * c;");
5672   verifyIndependentOfContext("int a = *b;");
5673   verifyIndependentOfContext("int a = *b * c;");
5674   verifyIndependentOfContext("int a = b * *c;");
5675   verifyIndependentOfContext("int a = b * (10);");
5676   verifyIndependentOfContext("S << b * (10);");
5677   verifyIndependentOfContext("return 10 * b;");
5678   verifyIndependentOfContext("return *b * *c;");
5679   verifyIndependentOfContext("return a & ~b;");
5680   verifyIndependentOfContext("f(b ? *c : *d);");
5681   verifyIndependentOfContext("int a = b ? *c : *d;");
5682   verifyIndependentOfContext("*b = a;");
5683   verifyIndependentOfContext("a * ~b;");
5684   verifyIndependentOfContext("a * !b;");
5685   verifyIndependentOfContext("a * +b;");
5686   verifyIndependentOfContext("a * -b;");
5687   verifyIndependentOfContext("a * ++b;");
5688   verifyIndependentOfContext("a * --b;");
5689   verifyIndependentOfContext("a[4] * b;");
5690   verifyIndependentOfContext("a[a * a] = 1;");
5691   verifyIndependentOfContext("f() * b;");
5692   verifyIndependentOfContext("a * [self dostuff];");
5693   verifyIndependentOfContext("int x = a * (a + b);");
5694   verifyIndependentOfContext("(a *)(a + b);");
5695   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5696   verifyIndependentOfContext("int *pa = (int *)&a;");
5697   verifyIndependentOfContext("return sizeof(int **);");
5698   verifyIndependentOfContext("return sizeof(int ******);");
5699   verifyIndependentOfContext("return (int **&)a;");
5700   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5701   verifyFormat("void f(Type (*parameter)[10]) {}");
5702   verifyFormat("void f(Type (&parameter)[10]) {}");
5703   verifyGoogleFormat("return sizeof(int**);");
5704   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5705   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5706   verifyFormat("auto a = [](int **&, int ***) {};");
5707   verifyFormat("auto PointerBinding = [](const char *S) {};");
5708   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5709   verifyFormat("[](const decltype(*a) &value) {}");
5710   verifyFormat("decltype(a * b) F();");
5711   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5712   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5713   verifyIndependentOfContext("typedef void (*f)(int *a);");
5714   verifyIndependentOfContext("int i{a * b};");
5715   verifyIndependentOfContext("aaa && aaa->f();");
5716   verifyIndependentOfContext("int x = ~*p;");
5717   verifyFormat("Constructor() : a(a), area(width * height) {}");
5718   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5719   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5720   verifyFormat("void f() { f(a, c * d); }");
5721   verifyFormat("void f() { f(new a(), c * d); }");
5722 
5723   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5724 
5725   verifyIndependentOfContext("A<int *> a;");
5726   verifyIndependentOfContext("A<int **> a;");
5727   verifyIndependentOfContext("A<int *, int *> a;");
5728   verifyIndependentOfContext("A<int *[]> a;");
5729   verifyIndependentOfContext(
5730       "const char *const p = reinterpret_cast<const char *const>(q);");
5731   verifyIndependentOfContext("A<int **, int **> a;");
5732   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5733   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5734   verifyFormat("for (; a && b;) {\n}");
5735   verifyFormat("bool foo = true && [] { return false; }();");
5736 
5737   verifyFormat(
5738       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5739       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5740 
5741   verifyGoogleFormat("**outparam = 1;");
5742   verifyGoogleFormat("*outparam = a * b;");
5743   verifyGoogleFormat("int main(int argc, char** argv) {}");
5744   verifyGoogleFormat("A<int*> a;");
5745   verifyGoogleFormat("A<int**> a;");
5746   verifyGoogleFormat("A<int*, int*> a;");
5747   verifyGoogleFormat("A<int**, int**> a;");
5748   verifyGoogleFormat("f(b ? *c : *d);");
5749   verifyGoogleFormat("int a = b ? *c : *d;");
5750   verifyGoogleFormat("Type* t = **x;");
5751   verifyGoogleFormat("Type* t = *++*x;");
5752   verifyGoogleFormat("*++*x;");
5753   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5754   verifyGoogleFormat("Type* t = x++ * y;");
5755   verifyGoogleFormat(
5756       "const char* const p = reinterpret_cast<const char* const>(q);");
5757   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5758   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5759   verifyGoogleFormat("template <typename T>\n"
5760                      "void f(int i = 0, SomeType** temps = NULL);");
5761 
5762   FormatStyle Left = getLLVMStyle();
5763   Left.PointerAlignment = FormatStyle::PAS_Left;
5764   verifyFormat("x = *a(x) = *a(y);", Left);
5765   verifyFormat("for (;; * = b) {\n}", Left);
5766   verifyFormat("return *this += 1;", Left);
5767 
5768   verifyIndependentOfContext("a = *(x + y);");
5769   verifyIndependentOfContext("a = &(x + y);");
5770   verifyIndependentOfContext("*(x + y).call();");
5771   verifyIndependentOfContext("&(x + y)->call();");
5772   verifyFormat("void f() { &(*I).first; }");
5773 
5774   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5775   verifyFormat(
5776       "int *MyValues = {\n"
5777       "    *A, // Operator detection might be confused by the '{'\n"
5778       "    *BB // Operator detection might be confused by previous comment\n"
5779       "};");
5780 
5781   verifyIndependentOfContext("if (int *a = &b)");
5782   verifyIndependentOfContext("if (int &a = *b)");
5783   verifyIndependentOfContext("if (a & b[i])");
5784   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5785   verifyIndependentOfContext("if (*b[i])");
5786   verifyIndependentOfContext("if (int *a = (&b))");
5787   verifyIndependentOfContext("while (int *a = &b)");
5788   verifyIndependentOfContext("size = sizeof *a;");
5789   verifyIndependentOfContext("if (a && (b = c))");
5790   verifyFormat("void f() {\n"
5791                "  for (const int &v : Values) {\n"
5792                "  }\n"
5793                "}");
5794   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5795   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5796   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5797 
5798   verifyFormat("#define A (!a * b)");
5799   verifyFormat("#define MACRO     \\\n"
5800                "  int *i = a * b; \\\n"
5801                "  void f(a *b);",
5802                getLLVMStyleWithColumns(19));
5803 
5804   verifyIndependentOfContext("A = new SomeType *[Length];");
5805   verifyIndependentOfContext("A = new SomeType *[Length]();");
5806   verifyIndependentOfContext("T **t = new T *;");
5807   verifyIndependentOfContext("T **t = new T *();");
5808   verifyGoogleFormat("A = new SomeType*[Length]();");
5809   verifyGoogleFormat("A = new SomeType*[Length];");
5810   verifyGoogleFormat("T** t = new T*;");
5811   verifyGoogleFormat("T** t = new T*();");
5812 
5813   FormatStyle PointerLeft = getLLVMStyle();
5814   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5815   verifyFormat("delete *x;", PointerLeft);
5816   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5817   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5818   verifyFormat("template <bool a, bool b> "
5819                "typename t::if<x && y>::type f() {}");
5820   verifyFormat("template <int *y> f() {}");
5821   verifyFormat("vector<int *> v;");
5822   verifyFormat("vector<int *const> v;");
5823   verifyFormat("vector<int *const **const *> v;");
5824   verifyFormat("vector<int *volatile> v;");
5825   verifyFormat("vector<a * b> v;");
5826   verifyFormat("foo<b && false>();");
5827   verifyFormat("foo<b & 1>();");
5828   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5829   verifyFormat(
5830       "template <class T, class = typename std::enable_if<\n"
5831       "                       std::is_integral<T>::value &&\n"
5832       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5833       "void F();",
5834       getLLVMStyleWithColumns(76));
5835   verifyFormat(
5836       "template <class T,\n"
5837       "          class = typename ::std::enable_if<\n"
5838       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5839       "void F();",
5840       getGoogleStyleWithColumns(68));
5841 
5842   verifyIndependentOfContext("MACRO(int *i);");
5843   verifyIndependentOfContext("MACRO(auto *a);");
5844   verifyIndependentOfContext("MACRO(const A *a);");
5845   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5846   // FIXME: Is there a way to make this work?
5847   // verifyIndependentOfContext("MACRO(A *a);");
5848 
5849   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5850   verifyFormat("return options != nullptr && operator==(*options);");
5851 
5852   EXPECT_EQ("#define OP(x)                                    \\\n"
5853             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5854             "    return s << a.DebugString();                 \\\n"
5855             "  }",
5856             format("#define OP(x) \\\n"
5857                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5858                    "    return s << a.DebugString(); \\\n"
5859                    "  }",
5860                    getLLVMStyleWithColumns(50)));
5861 
5862   // FIXME: We cannot handle this case yet; we might be able to figure out that
5863   // foo<x> d > v; doesn't make sense.
5864   verifyFormat("foo<a<b && c> d> v;");
5865 
5866   FormatStyle PointerMiddle = getLLVMStyle();
5867   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5868   verifyFormat("delete *x;", PointerMiddle);
5869   verifyFormat("int * x;", PointerMiddle);
5870   verifyFormat("template <int * y> f() {}", PointerMiddle);
5871   verifyFormat("int * f(int * a) {}", PointerMiddle);
5872   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5873   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5874   verifyFormat("A<int *> a;", PointerMiddle);
5875   verifyFormat("A<int **> a;", PointerMiddle);
5876   verifyFormat("A<int *, int *> a;", PointerMiddle);
5877   verifyFormat("A<int * []> a;", PointerMiddle);
5878   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5879   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5880   verifyFormat("T ** t = new T *;", PointerMiddle);
5881 
5882   // Member function reference qualifiers aren't binary operators.
5883   verifyFormat("string // break\n"
5884                "operator()() & {}");
5885   verifyFormat("string // break\n"
5886                "operator()() && {}");
5887   verifyGoogleFormat("template <typename T>\n"
5888                      "auto x() & -> int {}");
5889 }
5890 
5891 TEST_F(FormatTest, UnderstandsAttributes) {
5892   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5893   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5894                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5895   FormatStyle AfterType = getLLVMStyle();
5896   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5897   verifyFormat("__attribute__((nodebug)) void\n"
5898                "foo() {}\n",
5899                AfterType);
5900 }
5901 
5902 TEST_F(FormatTest, UnderstandsEllipsis) {
5903   verifyFormat("int printf(const char *fmt, ...);");
5904   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5905   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5906 
5907   FormatStyle PointersLeft = getLLVMStyle();
5908   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5909   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5910 }
5911 
5912 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5913   EXPECT_EQ("int *a;\n"
5914             "int *a;\n"
5915             "int *a;",
5916             format("int *a;\n"
5917                    "int* a;\n"
5918                    "int *a;",
5919                    getGoogleStyle()));
5920   EXPECT_EQ("int* a;\n"
5921             "int* a;\n"
5922             "int* a;",
5923             format("int* a;\n"
5924                    "int* a;\n"
5925                    "int *a;",
5926                    getGoogleStyle()));
5927   EXPECT_EQ("int *a;\n"
5928             "int *a;\n"
5929             "int *a;",
5930             format("int *a;\n"
5931                    "int * a;\n"
5932                    "int *  a;",
5933                    getGoogleStyle()));
5934   EXPECT_EQ("auto x = [] {\n"
5935             "  int *a;\n"
5936             "  int *a;\n"
5937             "  int *a;\n"
5938             "};",
5939             format("auto x=[]{int *a;\n"
5940                    "int * a;\n"
5941                    "int *  a;};",
5942                    getGoogleStyle()));
5943 }
5944 
5945 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5946   verifyFormat("int f(int &&a) {}");
5947   verifyFormat("int f(int a, char &&b) {}");
5948   verifyFormat("void f() { int &&a = b; }");
5949   verifyGoogleFormat("int f(int a, char&& b) {}");
5950   verifyGoogleFormat("void f() { int&& a = b; }");
5951 
5952   verifyIndependentOfContext("A<int &&> a;");
5953   verifyIndependentOfContext("A<int &&, int &&> a;");
5954   verifyGoogleFormat("A<int&&> a;");
5955   verifyGoogleFormat("A<int&&, int&&> a;");
5956 
5957   // Not rvalue references:
5958   verifyFormat("template <bool B, bool C> class A {\n"
5959                "  static_assert(B && C, \"Something is wrong\");\n"
5960                "};");
5961   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5962   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5963   verifyFormat("#define A(a, b) (a && b)");
5964 }
5965 
5966 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5967   verifyFormat("void f() {\n"
5968                "  x[aaaaaaaaa -\n"
5969                "    b] = 23;\n"
5970                "}",
5971                getLLVMStyleWithColumns(15));
5972 }
5973 
5974 TEST_F(FormatTest, FormatsCasts) {
5975   verifyFormat("Type *A = static_cast<Type *>(P);");
5976   verifyFormat("Type *A = (Type *)P;");
5977   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5978   verifyFormat("int a = (int)(2.0f);");
5979   verifyFormat("int a = (int)2.0f;");
5980   verifyFormat("x[(int32)y];");
5981   verifyFormat("x = (int32)y;");
5982   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5983   verifyFormat("int a = (int)*b;");
5984   verifyFormat("int a = (int)2.0f;");
5985   verifyFormat("int a = (int)~0;");
5986   verifyFormat("int a = (int)++a;");
5987   verifyFormat("int a = (int)sizeof(int);");
5988   verifyFormat("int a = (int)+2;");
5989   verifyFormat("my_int a = (my_int)2.0f;");
5990   verifyFormat("my_int a = (my_int)sizeof(int);");
5991   verifyFormat("return (my_int)aaa;");
5992   verifyFormat("#define x ((int)-1)");
5993   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5994   verifyFormat("#define p(q) ((int *)&q)");
5995   verifyFormat("fn(a)(b) + 1;");
5996 
5997   verifyFormat("void f() { my_int a = (my_int)*b; }");
5998   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5999   verifyFormat("my_int a = (my_int)~0;");
6000   verifyFormat("my_int a = (my_int)++a;");
6001   verifyFormat("my_int a = (my_int)-2;");
6002   verifyFormat("my_int a = (my_int)1;");
6003   verifyFormat("my_int a = (my_int *)1;");
6004   verifyFormat("my_int a = (const my_int)-1;");
6005   verifyFormat("my_int a = (const my_int *)-1;");
6006   verifyFormat("my_int a = (my_int)(my_int)-1;");
6007   verifyFormat("my_int a = (ns::my_int)-2;");
6008   verifyFormat("case (my_int)ONE:");
6009   verifyFormat("auto x = (X)this;");
6010 
6011   // FIXME: single value wrapped with paren will be treated as cast.
6012   verifyFormat("void f(int i = (kValue)*kMask) {}");
6013 
6014   verifyFormat("{ (void)F; }");
6015 
6016   // Don't break after a cast's
6017   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6018                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6019                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6020 
6021   // These are not casts.
6022   verifyFormat("void f(int *) {}");
6023   verifyFormat("f(foo)->b;");
6024   verifyFormat("f(foo).b;");
6025   verifyFormat("f(foo)(b);");
6026   verifyFormat("f(foo)[b];");
6027   verifyFormat("[](foo) { return 4; }(bar);");
6028   verifyFormat("(*funptr)(foo)[4];");
6029   verifyFormat("funptrs[4](foo)[4];");
6030   verifyFormat("void f(int *);");
6031   verifyFormat("void f(int *) = 0;");
6032   verifyFormat("void f(SmallVector<int>) {}");
6033   verifyFormat("void f(SmallVector<int>);");
6034   verifyFormat("void f(SmallVector<int>) = 0;");
6035   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6036   verifyFormat("int a = sizeof(int) * b;");
6037   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6038   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6039   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6040   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6041 
6042   // These are not casts, but at some point were confused with casts.
6043   verifyFormat("virtual void foo(int *) override;");
6044   verifyFormat("virtual void foo(char &) const;");
6045   verifyFormat("virtual void foo(int *a, char *) const;");
6046   verifyFormat("int a = sizeof(int *) + b;");
6047   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6048   verifyFormat("bool b = f(g<int>) && c;");
6049   verifyFormat("typedef void (*f)(int i) func;");
6050 
6051   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6052                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6053   // FIXME: The indentation here is not ideal.
6054   verifyFormat(
6055       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6056       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6057       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6058 }
6059 
6060 TEST_F(FormatTest, FormatsFunctionTypes) {
6061   verifyFormat("A<bool()> a;");
6062   verifyFormat("A<SomeType()> a;");
6063   verifyFormat("A<void (*)(int, std::string)> a;");
6064   verifyFormat("A<void *(int)>;");
6065   verifyFormat("void *(*a)(int *, SomeType *);");
6066   verifyFormat("int (*func)(void *);");
6067   verifyFormat("void f() { int (*func)(void *); }");
6068   verifyFormat("template <class CallbackClass>\n"
6069                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6070 
6071   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6072   verifyGoogleFormat("void* (*a)(int);");
6073   verifyGoogleFormat(
6074       "template <class CallbackClass>\n"
6075       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6076 
6077   // Other constructs can look somewhat like function types:
6078   verifyFormat("A<sizeof(*x)> a;");
6079   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6080   verifyFormat("some_var = function(*some_pointer_var)[0];");
6081   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6082   verifyFormat("int x = f(&h)();");
6083 }
6084 
6085 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6086   verifyFormat("A (*foo_)[6];");
6087   verifyFormat("vector<int> (*foo_)[6];");
6088 }
6089 
6090 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6091   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6092                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6093   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6094                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6095   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6096                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6097 
6098   // Different ways of ()-initializiation.
6099   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6100                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6101   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6102                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6103   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6104                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6105   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6106                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6107 }
6108 
6109 TEST_F(FormatTest, BreaksLongDeclarations) {
6110   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6111                "    AnotherNameForTheLongType;");
6112   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6113                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6114   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6115                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6116   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6117                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6118   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6119                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6120   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6121                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6122   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6123                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6124   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6125                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6126   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6127                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6128   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6129                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6130   FormatStyle Indented = getLLVMStyle();
6131   Indented.IndentWrappedFunctionNames = true;
6132   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6133                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6134                Indented);
6135   verifyFormat(
6136       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6137       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6138       Indented);
6139   verifyFormat(
6140       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6141       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6142       Indented);
6143   verifyFormat(
6144       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6145       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6146       Indented);
6147 
6148   // FIXME: Without the comment, this breaks after "(".
6149   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6150                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6151                getGoogleStyle());
6152 
6153   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6154                "                  int LoooooooooooooooooooongParam2) {}");
6155   verifyFormat(
6156       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6157       "                                   SourceLocation L, IdentifierIn *II,\n"
6158       "                                   Type *T) {}");
6159   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6160                "ReallyReaaallyLongFunctionName(\n"
6161                "    const std::string &SomeParameter,\n"
6162                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6163                "        &ReallyReallyLongParameterName,\n"
6164                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6165                "        &AnotherLongParameterName) {}");
6166   verifyFormat("template <typename A>\n"
6167                "SomeLoooooooooooooooooooooongType<\n"
6168                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6169                "Function() {}");
6170 
6171   verifyGoogleFormat(
6172       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6173       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6174   verifyGoogleFormat(
6175       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6176       "                                   SourceLocation L) {}");
6177   verifyGoogleFormat(
6178       "some_namespace::LongReturnType\n"
6179       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6180       "    int first_long_parameter, int second_parameter) {}");
6181 
6182   verifyGoogleFormat("template <typename T>\n"
6183                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6184                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6185   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6186                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6187 
6188   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6189                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6190                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6191   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6192                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6193                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6194   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6195                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6196                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6197                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6198 }
6199 
6200 TEST_F(FormatTest, FormatsArrays) {
6201   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6202                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6203   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6204                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6205   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6206                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6207   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6208                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6209   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6210                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6211   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6212                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6213                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6214   verifyFormat(
6215       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6216       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6217       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6218   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6219                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6220 
6221   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6222                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6223   verifyFormat(
6224       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6225       "                                  .aaaaaaa[0]\n"
6226       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6227   verifyFormat("a[::b::c];");
6228 
6229   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6230 
6231   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6232   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6233 }
6234 
6235 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6236   verifyFormat("(a)->b();");
6237   verifyFormat("--a;");
6238 }
6239 
6240 TEST_F(FormatTest, HandlesIncludeDirectives) {
6241   verifyFormat("#include <string>\n"
6242                "#include <a/b/c.h>\n"
6243                "#include \"a/b/string\"\n"
6244                "#include \"string.h\"\n"
6245                "#include \"string.h\"\n"
6246                "#include <a-a>\n"
6247                "#include < path with space >\n"
6248                "#include_next <test.h>"
6249                "#include \"abc.h\" // this is included for ABC\n"
6250                "#include \"some long include\" // with a comment\n"
6251                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6252                getLLVMStyleWithColumns(35));
6253   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6254   EXPECT_EQ("#include <a>", format("#include<a>"));
6255 
6256   verifyFormat("#import <string>");
6257   verifyFormat("#import <a/b/c.h>");
6258   verifyFormat("#import \"a/b/string\"");
6259   verifyFormat("#import \"string.h\"");
6260   verifyFormat("#import \"string.h\"");
6261   verifyFormat("#if __has_include(<strstream>)\n"
6262                "#include <strstream>\n"
6263                "#endif");
6264 
6265   verifyFormat("#define MY_IMPORT <a/b>");
6266 
6267   // Protocol buffer definition or missing "#".
6268   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6269                getLLVMStyleWithColumns(30));
6270 
6271   FormatStyle Style = getLLVMStyle();
6272   Style.AlwaysBreakBeforeMultilineStrings = true;
6273   Style.ColumnLimit = 0;
6274   verifyFormat("#import \"abc.h\"", Style);
6275 
6276   // But 'import' might also be a regular C++ namespace.
6277   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6278                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6279 }
6280 
6281 //===----------------------------------------------------------------------===//
6282 // Error recovery tests.
6283 //===----------------------------------------------------------------------===//
6284 
6285 TEST_F(FormatTest, IncompleteParameterLists) {
6286   FormatStyle NoBinPacking = getLLVMStyle();
6287   NoBinPacking.BinPackParameters = false;
6288   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6289                "                        double *min_x,\n"
6290                "                        double *max_x,\n"
6291                "                        double *min_y,\n"
6292                "                        double *max_y,\n"
6293                "                        double *min_z,\n"
6294                "                        double *max_z, ) {}",
6295                NoBinPacking);
6296 }
6297 
6298 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6299   verifyFormat("void f() { return; }\n42");
6300   verifyFormat("void f() {\n"
6301                "  if (0)\n"
6302                "    return;\n"
6303                "}\n"
6304                "42");
6305   verifyFormat("void f() { return }\n42");
6306   verifyFormat("void f() {\n"
6307                "  if (0)\n"
6308                "    return\n"
6309                "}\n"
6310                "42");
6311 }
6312 
6313 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6314   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6315   EXPECT_EQ("void f() {\n"
6316             "  if (a)\n"
6317             "    return\n"
6318             "}",
6319             format("void  f  (  )  {  if  ( a )  return  }"));
6320   EXPECT_EQ("namespace N {\n"
6321             "void f()\n"
6322             "}",
6323             format("namespace  N  {  void f()  }"));
6324   EXPECT_EQ("namespace N {\n"
6325             "void f() {}\n"
6326             "void g()\n"
6327             "}",
6328             format("namespace N  { void f( ) { } void g( ) }"));
6329 }
6330 
6331 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6332   verifyFormat("int aaaaaaaa =\n"
6333                "    // Overlylongcomment\n"
6334                "    b;",
6335                getLLVMStyleWithColumns(20));
6336   verifyFormat("function(\n"
6337                "    ShortArgument,\n"
6338                "    LoooooooooooongArgument);\n",
6339                getLLVMStyleWithColumns(20));
6340 }
6341 
6342 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6343   verifyFormat("public:");
6344   verifyFormat("class A {\n"
6345                "public\n"
6346                "  void f() {}\n"
6347                "};");
6348   verifyFormat("public\n"
6349                "int qwerty;");
6350   verifyFormat("public\n"
6351                "B {}");
6352   verifyFormat("public\n"
6353                "{}");
6354   verifyFormat("public\n"
6355                "B { int x; }");
6356 }
6357 
6358 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6359   verifyFormat("{");
6360   verifyFormat("#})");
6361   verifyNoCrash("(/**/[:!] ?[).");
6362 }
6363 
6364 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6365   verifyFormat("do {\n}");
6366   verifyFormat("do {\n}\n"
6367                "f();");
6368   verifyFormat("do {\n}\n"
6369                "wheeee(fun);");
6370   verifyFormat("do {\n"
6371                "  f();\n"
6372                "}");
6373 }
6374 
6375 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6376   verifyFormat("if {\n  foo;\n  foo();\n}");
6377   verifyFormat("switch {\n  foo;\n  foo();\n}");
6378   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6379   verifyFormat("while {\n  foo;\n  foo();\n}");
6380   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6381 }
6382 
6383 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6384   verifyIncompleteFormat("namespace {\n"
6385                          "class Foo { Foo (\n"
6386                          "};\n"
6387                          "} // comment");
6388 }
6389 
6390 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6391   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6392   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6393   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6394   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6395 
6396   EXPECT_EQ("{\n"
6397             "  {\n"
6398             "    breakme(\n"
6399             "        qwe);\n"
6400             "  }\n",
6401             format("{\n"
6402                    "    {\n"
6403                    " breakme(qwe);\n"
6404                    "}\n",
6405                    getLLVMStyleWithColumns(10)));
6406 }
6407 
6408 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6409   verifyFormat("int x = {\n"
6410                "    avariable,\n"
6411                "    b(alongervariable)};",
6412                getLLVMStyleWithColumns(25));
6413 }
6414 
6415 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6416   verifyFormat("return (a)(b){1, 2, 3};");
6417 }
6418 
6419 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6420   verifyFormat("vector<int> x{1, 2, 3, 4};");
6421   verifyFormat("vector<int> x{\n"
6422                "    1, 2, 3, 4,\n"
6423                "};");
6424   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6425   verifyFormat("f({1, 2});");
6426   verifyFormat("auto v = Foo{-1};");
6427   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6428   verifyFormat("Class::Class : member{1, 2, 3} {}");
6429   verifyFormat("new vector<int>{1, 2, 3};");
6430   verifyFormat("new int[3]{1, 2, 3};");
6431   verifyFormat("new int{1};");
6432   verifyFormat("return {arg1, arg2};");
6433   verifyFormat("return {arg1, SomeType{parameter}};");
6434   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6435   verifyFormat("new T{arg1, arg2};");
6436   verifyFormat("f(MyMap[{composite, key}]);");
6437   verifyFormat("class Class {\n"
6438                "  T member = {arg1, arg2};\n"
6439                "};");
6440   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6441   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6442   verifyFormat("int a = std::is_integral<int>{} + 0;");
6443 
6444   verifyFormat("int foo(int i) { return fo1{}(i); }");
6445   verifyFormat("int foo(int i) { return fo1{}(i); }");
6446   verifyFormat("auto i = decltype(x){};");
6447   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6448   verifyFormat("Node n{1, Node{1000}, //\n"
6449                "       2};");
6450   verifyFormat("Aaaa aaaaaaa{\n"
6451                "    {\n"
6452                "        aaaa,\n"
6453                "    },\n"
6454                "};");
6455   verifyFormat("class C : public D {\n"
6456                "  SomeClass SC{2};\n"
6457                "};");
6458   verifyFormat("class C : public A {\n"
6459                "  class D : public B {\n"
6460                "    void f() { int i{2}; }\n"
6461                "  };\n"
6462                "};");
6463   verifyFormat("#define A {a, a},");
6464 
6465   // In combination with BinPackArguments = false.
6466   FormatStyle NoBinPacking = getLLVMStyle();
6467   NoBinPacking.BinPackArguments = false;
6468   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6469                "                      bbbbb,\n"
6470                "                      ccccc,\n"
6471                "                      ddddd,\n"
6472                "                      eeeee,\n"
6473                "                      ffffff,\n"
6474                "                      ggggg,\n"
6475                "                      hhhhhh,\n"
6476                "                      iiiiii,\n"
6477                "                      jjjjjj,\n"
6478                "                      kkkkkk};",
6479                NoBinPacking);
6480   verifyFormat("const Aaaaaa aaaaa = {\n"
6481                "    aaaaa,\n"
6482                "    bbbbb,\n"
6483                "    ccccc,\n"
6484                "    ddddd,\n"
6485                "    eeeee,\n"
6486                "    ffffff,\n"
6487                "    ggggg,\n"
6488                "    hhhhhh,\n"
6489                "    iiiiii,\n"
6490                "    jjjjjj,\n"
6491                "    kkkkkk,\n"
6492                "};",
6493                NoBinPacking);
6494   verifyFormat(
6495       "const Aaaaaa aaaaa = {\n"
6496       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6497       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6498       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6499       "};",
6500       NoBinPacking);
6501 
6502   // FIXME: The alignment of these trailing comments might be bad. Then again,
6503   // this might be utterly useless in real code.
6504   verifyFormat("Constructor::Constructor()\n"
6505                "    : some_value{         //\n"
6506                "                 aaaaaaa, //\n"
6507                "                 bbbbbbb} {}");
6508 
6509   // In braced lists, the first comment is always assumed to belong to the
6510   // first element. Thus, it can be moved to the next or previous line as
6511   // appropriate.
6512   EXPECT_EQ("function({// First element:\n"
6513             "          1,\n"
6514             "          // Second element:\n"
6515             "          2});",
6516             format("function({\n"
6517                    "    // First element:\n"
6518                    "    1,\n"
6519                    "    // Second element:\n"
6520                    "    2});"));
6521   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6522             "    // First element:\n"
6523             "    1,\n"
6524             "    // Second element:\n"
6525             "    2};",
6526             format("std::vector<int> MyNumbers{// First element:\n"
6527                    "                           1,\n"
6528                    "                           // Second element:\n"
6529                    "                           2};",
6530                    getLLVMStyleWithColumns(30)));
6531   // A trailing comma should still lead to an enforced line break.
6532   EXPECT_EQ("vector<int> SomeVector = {\n"
6533             "    // aaa\n"
6534             "    1, 2,\n"
6535             "};",
6536             format("vector<int> SomeVector = { // aaa\n"
6537                    "    1, 2, };"));
6538 
6539   FormatStyle ExtraSpaces = getLLVMStyle();
6540   ExtraSpaces.Cpp11BracedListStyle = false;
6541   ExtraSpaces.ColumnLimit = 75;
6542   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6543   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6544   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6545   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6546   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6547   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6548   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6549   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6550   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6551   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6552   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6553   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6554   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6555   verifyFormat("class Class {\n"
6556                "  T member = { arg1, arg2 };\n"
6557                "};",
6558                ExtraSpaces);
6559   verifyFormat(
6560       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6561       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6562       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6563       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6564       ExtraSpaces);
6565   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6566   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6567                ExtraSpaces);
6568   verifyFormat(
6569       "someFunction(OtherParam,\n"
6570       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6571       "                         param1, param2,\n"
6572       "                         // comment 2\n"
6573       "                         param3, param4 });",
6574       ExtraSpaces);
6575   verifyFormat(
6576       "std::this_thread::sleep_for(\n"
6577       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6578       ExtraSpaces);
6579   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6580                "    aaaaaaa,\n"
6581                "    aaaaaaaaaa,\n"
6582                "    aaaaa,\n"
6583                "    aaaaaaaaaaaaaaa,\n"
6584                "    aaa,\n"
6585                "    aaaaaaaaaa,\n"
6586                "    a,\n"
6587                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6588                "    aaaaaaaaaaaa,\n"
6589                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6590                "    aaaaaaa,\n"
6591                "    a};");
6592   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6593 }
6594 
6595 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6596   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6597                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6598                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6599                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6600                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6601                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6602   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6603                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6604                "                 1, 22, 333, 4444, 55555, //\n"
6605                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6606                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6607   verifyFormat(
6608       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6609       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6610       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6611       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6612       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6613       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6614       "                 7777777};");
6615   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6616                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6617                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6618   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6619                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6620                "    // Separating comment.\n"
6621                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6622   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6623                "    // Leading comment\n"
6624                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6625                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6626   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6627                "                 1, 1, 1, 1};",
6628                getLLVMStyleWithColumns(39));
6629   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6630                "                 1, 1, 1, 1};",
6631                getLLVMStyleWithColumns(38));
6632   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6633                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6634                getLLVMStyleWithColumns(43));
6635   verifyFormat(
6636       "static unsigned SomeValues[10][3] = {\n"
6637       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6638       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6639   verifyFormat("static auto fields = new vector<string>{\n"
6640                "    \"aaaaaaaaaaaaa\",\n"
6641                "    \"aaaaaaaaaaaaa\",\n"
6642                "    \"aaaaaaaaaaaa\",\n"
6643                "    \"aaaaaaaaaaaaaa\",\n"
6644                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6645                "    \"aaaaaaaaaaaa\",\n"
6646                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6647                "};");
6648   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6649   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6650                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6651                "                 3, cccccccccccccccccccccc};",
6652                getLLVMStyleWithColumns(60));
6653 
6654   // Trailing commas.
6655   verifyFormat("vector<int> x = {\n"
6656                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6657                "};",
6658                getLLVMStyleWithColumns(39));
6659   verifyFormat("vector<int> x = {\n"
6660                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6661                "};",
6662                getLLVMStyleWithColumns(39));
6663   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6664                "                 1, 1, 1, 1,\n"
6665                "                 /**/ /**/};",
6666                getLLVMStyleWithColumns(39));
6667 
6668   // Trailing comment in the first line.
6669   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6670                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6671                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6672                "    11111111,   22222222,   333333333,   44444444};");
6673   // Trailing comment in the last line.
6674   verifyFormat("int aaaaa[] = {\n"
6675                "    1, 2, 3, // comment\n"
6676                "    4, 5, 6  // comment\n"
6677                "};");
6678 
6679   // With nested lists, we should either format one item per line or all nested
6680   // lists one on line.
6681   // FIXME: For some nested lists, we can do better.
6682   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6683                "        {aaaaaaaaaaaaaaaaaaa},\n"
6684                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6685                "        {aaaaaaaaaaaaaaaaa}};",
6686                getLLVMStyleWithColumns(60));
6687   verifyFormat(
6688       "SomeStruct my_struct_array = {\n"
6689       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6690       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6691       "    {aaa, aaa},\n"
6692       "    {aaa, aaa},\n"
6693       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6694       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6695       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6696 
6697   // No column layout should be used here.
6698   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6699                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6700 
6701   verifyNoCrash("a<,");
6702 
6703   // No braced initializer here.
6704   verifyFormat("void f() {\n"
6705                "  struct Dummy {};\n"
6706                "  f(v);\n"
6707                "}");
6708 
6709   // Long lists should be formatted in columns even if they are nested.
6710   verifyFormat(
6711       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6712       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6713       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6714       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6715       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6716       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6717 }
6718 
6719 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6720   FormatStyle DoNotMerge = getLLVMStyle();
6721   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6722 
6723   verifyFormat("void f() { return 42; }");
6724   verifyFormat("void f() {\n"
6725                "  return 42;\n"
6726                "}",
6727                DoNotMerge);
6728   verifyFormat("void f() {\n"
6729                "  // Comment\n"
6730                "}");
6731   verifyFormat("{\n"
6732                "#error {\n"
6733                "  int a;\n"
6734                "}");
6735   verifyFormat("{\n"
6736                "  int a;\n"
6737                "#error {\n"
6738                "}");
6739   verifyFormat("void f() {} // comment");
6740   verifyFormat("void f() { int a; } // comment");
6741   verifyFormat("void f() {\n"
6742                "} // comment",
6743                DoNotMerge);
6744   verifyFormat("void f() {\n"
6745                "  int a;\n"
6746                "} // comment",
6747                DoNotMerge);
6748   verifyFormat("void f() {\n"
6749                "} // comment",
6750                getLLVMStyleWithColumns(15));
6751 
6752   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6753   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6754 
6755   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6756   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6757   verifyFormat("class C {\n"
6758                "  C()\n"
6759                "      : iiiiiiii(nullptr),\n"
6760                "        kkkkkkk(nullptr),\n"
6761                "        mmmmmmm(nullptr),\n"
6762                "        nnnnnnn(nullptr) {}\n"
6763                "};",
6764                getGoogleStyle());
6765 
6766   FormatStyle NoColumnLimit = getLLVMStyle();
6767   NoColumnLimit.ColumnLimit = 0;
6768   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6769   EXPECT_EQ("class C {\n"
6770             "  A() : b(0) {}\n"
6771             "};",
6772             format("class C{A():b(0){}};", NoColumnLimit));
6773   EXPECT_EQ("A()\n"
6774             "    : b(0) {\n"
6775             "}",
6776             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6777 
6778   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6779   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6780       FormatStyle::SFS_None;
6781   EXPECT_EQ("A()\n"
6782             "    : b(0) {\n"
6783             "}",
6784             format("A():b(0){}", DoNotMergeNoColumnLimit));
6785   EXPECT_EQ("A()\n"
6786             "    : b(0) {\n"
6787             "}",
6788             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6789 
6790   verifyFormat("#define A          \\\n"
6791                "  void f() {       \\\n"
6792                "    int i;         \\\n"
6793                "  }",
6794                getLLVMStyleWithColumns(20));
6795   verifyFormat("#define A           \\\n"
6796                "  void f() { int i; }",
6797                getLLVMStyleWithColumns(21));
6798   verifyFormat("#define A            \\\n"
6799                "  void f() {         \\\n"
6800                "    int i;           \\\n"
6801                "  }                  \\\n"
6802                "  int j;",
6803                getLLVMStyleWithColumns(22));
6804   verifyFormat("#define A             \\\n"
6805                "  void f() { int i; } \\\n"
6806                "  int j;",
6807                getLLVMStyleWithColumns(23));
6808 }
6809 
6810 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6811   FormatStyle MergeInlineOnly = getLLVMStyle();
6812   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6813   verifyFormat("class C {\n"
6814                "  int f() { return 42; }\n"
6815                "};",
6816                MergeInlineOnly);
6817   verifyFormat("int f() {\n"
6818                "  return 42;\n"
6819                "}",
6820                MergeInlineOnly);
6821 }
6822 
6823 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6824   // Elaborate type variable declarations.
6825   verifyFormat("struct foo a = {bar};\nint n;");
6826   verifyFormat("class foo a = {bar};\nint n;");
6827   verifyFormat("union foo a = {bar};\nint n;");
6828 
6829   // Elaborate types inside function definitions.
6830   verifyFormat("struct foo f() {}\nint n;");
6831   verifyFormat("class foo f() {}\nint n;");
6832   verifyFormat("union foo f() {}\nint n;");
6833 
6834   // Templates.
6835   verifyFormat("template <class X> void f() {}\nint n;");
6836   verifyFormat("template <struct X> void f() {}\nint n;");
6837   verifyFormat("template <union X> void f() {}\nint n;");
6838 
6839   // Actual definitions...
6840   verifyFormat("struct {\n} n;");
6841   verifyFormat(
6842       "template <template <class T, class Y>, class Z> class X {\n} n;");
6843   verifyFormat("union Z {\n  int n;\n} x;");
6844   verifyFormat("class MACRO Z {\n} n;");
6845   verifyFormat("class MACRO(X) Z {\n} n;");
6846   verifyFormat("class __attribute__(X) Z {\n} n;");
6847   verifyFormat("class __declspec(X) Z {\n} n;");
6848   verifyFormat("class A##B##C {\n} n;");
6849   verifyFormat("class alignas(16) Z {\n} n;");
6850   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6851   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6852 
6853   // Redefinition from nested context:
6854   verifyFormat("class A::B::C {\n} n;");
6855 
6856   // Template definitions.
6857   verifyFormat(
6858       "template <typename F>\n"
6859       "Matcher(const Matcher<F> &Other,\n"
6860       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6861       "                             !is_same<F, T>::value>::type * = 0)\n"
6862       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6863 
6864   // FIXME: This is still incorrectly handled at the formatter side.
6865   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6866   verifyFormat("int i = SomeFunction(a<b, a> b);");
6867 
6868   // FIXME:
6869   // This now gets parsed incorrectly as class definition.
6870   // verifyFormat("class A<int> f() {\n}\nint n;");
6871 
6872   // Elaborate types where incorrectly parsing the structural element would
6873   // break the indent.
6874   verifyFormat("if (true)\n"
6875                "  class X x;\n"
6876                "else\n"
6877                "  f();\n");
6878 
6879   // This is simply incomplete. Formatting is not important, but must not crash.
6880   verifyFormat("class A:");
6881 }
6882 
6883 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6884   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6885             format("#error Leave     all         white!!!!! space* alone!\n"));
6886   EXPECT_EQ(
6887       "#warning Leave     all         white!!!!! space* alone!\n",
6888       format("#warning Leave     all         white!!!!! space* alone!\n"));
6889   EXPECT_EQ("#error 1", format("  #  error   1"));
6890   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6891 }
6892 
6893 TEST_F(FormatTest, FormatHashIfExpressions) {
6894   verifyFormat("#if AAAA && BBBB");
6895   verifyFormat("#if (AAAA && BBBB)");
6896   verifyFormat("#elif (AAAA && BBBB)");
6897   // FIXME: Come up with a better indentation for #elif.
6898   verifyFormat(
6899       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6900       "    defined(BBBBBBBB)\n"
6901       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6902       "    defined(BBBBBBBB)\n"
6903       "#endif",
6904       getLLVMStyleWithColumns(65));
6905 }
6906 
6907 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6908   FormatStyle AllowsMergedIf = getGoogleStyle();
6909   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6910   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6911   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6912   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6913   EXPECT_EQ("if (true) return 42;",
6914             format("if (true)\nreturn 42;", AllowsMergedIf));
6915   FormatStyle ShortMergedIf = AllowsMergedIf;
6916   ShortMergedIf.ColumnLimit = 25;
6917   verifyFormat("#define A \\\n"
6918                "  if (true) return 42;",
6919                ShortMergedIf);
6920   verifyFormat("#define A \\\n"
6921                "  f();    \\\n"
6922                "  if (true)\n"
6923                "#define B",
6924                ShortMergedIf);
6925   verifyFormat("#define A \\\n"
6926                "  f();    \\\n"
6927                "  if (true)\n"
6928                "g();",
6929                ShortMergedIf);
6930   verifyFormat("{\n"
6931                "#ifdef A\n"
6932                "  // Comment\n"
6933                "  if (true) continue;\n"
6934                "#endif\n"
6935                "  // Comment\n"
6936                "  if (true) continue;\n"
6937                "}",
6938                ShortMergedIf);
6939   ShortMergedIf.ColumnLimit = 29;
6940   verifyFormat("#define A                   \\\n"
6941                "  if (aaaaaaaaaa) return 1; \\\n"
6942                "  return 2;",
6943                ShortMergedIf);
6944   ShortMergedIf.ColumnLimit = 28;
6945   verifyFormat("#define A         \\\n"
6946                "  if (aaaaaaaaaa) \\\n"
6947                "    return 1;     \\\n"
6948                "  return 2;",
6949                ShortMergedIf);
6950 }
6951 
6952 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6953   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6954                "  f();\n"
6955                "}");
6956   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6957                "  f();\n"
6958                "} /* another comment */ else /* comment #3 */ {\n"
6959                "  g();\n"
6960                "}");
6961   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6962                "  f();\n"
6963                "}");
6964   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6965                "  f();\n"
6966                "}");
6967   verifyFormat("do /* a comment in a strange place */ {\n"
6968                "  f();\n"
6969                "} /* another comment */ while (0);");
6970 }
6971 
6972 TEST_F(FormatTest, BlockComments) {
6973   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6974             format("/* *//* */  /* */\n/* *//* */  /* */"));
6975   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6976   EXPECT_EQ("#define A /*123*/ \\\n"
6977             "  b\n"
6978             "/* */\n"
6979             "someCall(\n"
6980             "    parameter);",
6981             format("#define A /*123*/ b\n"
6982                    "/* */\n"
6983                    "someCall(parameter);",
6984                    getLLVMStyleWithColumns(15)));
6985 
6986   EXPECT_EQ("#define A\n"
6987             "/* */ someCall(\n"
6988             "    parameter);",
6989             format("#define A\n"
6990                    "/* */someCall(parameter);",
6991                    getLLVMStyleWithColumns(15)));
6992   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6993   EXPECT_EQ("/*\n"
6994             "*\n"
6995             " * aaaaaa\n"
6996             " * aaaaaa\n"
6997             "*/",
6998             format("/*\n"
6999                    "*\n"
7000                    " * aaaaaa aaaaaa\n"
7001                    "*/",
7002                    getLLVMStyleWithColumns(10)));
7003   EXPECT_EQ("/*\n"
7004             "**\n"
7005             "* aaaaaa\n"
7006             "*aaaaaa\n"
7007             "*/",
7008             format("/*\n"
7009                    "**\n"
7010                    "* aaaaaa aaaaaa\n"
7011                    "*/",
7012                    getLLVMStyleWithColumns(10)));
7013 
7014   FormatStyle NoBinPacking = getLLVMStyle();
7015   NoBinPacking.BinPackParameters = false;
7016   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
7017             "             2, /* comment 2 */\n"
7018             "             3, /* comment 3 */\n"
7019             "             aaaa,\n"
7020             "             bbbb);",
7021             format("someFunction (1,   /* comment 1 */\n"
7022                    "                2, /* comment 2 */  \n"
7023                    "               3,   /* comment 3 */\n"
7024                    "aaaa, bbbb );",
7025                    NoBinPacking));
7026   verifyFormat(
7027       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7028       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7029   EXPECT_EQ(
7030       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
7031       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7032       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
7033       format(
7034           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
7035           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
7036           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
7037   EXPECT_EQ(
7038       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7039       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
7040       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
7041       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7042              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
7043              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
7044 
7045   verifyFormat("void f(int * /* unused */) {}");
7046 
7047   EXPECT_EQ("/*\n"
7048             " **\n"
7049             " */",
7050             format("/*\n"
7051                    " **\n"
7052                    " */"));
7053   EXPECT_EQ("/*\n"
7054             " *q\n"
7055             " */",
7056             format("/*\n"
7057                    " *q\n"
7058                    " */"));
7059   EXPECT_EQ("/*\n"
7060             " * q\n"
7061             " */",
7062             format("/*\n"
7063                    " * q\n"
7064                    " */"));
7065   EXPECT_EQ("/*\n"
7066             " **/",
7067             format("/*\n"
7068                    " **/"));
7069   EXPECT_EQ("/*\n"
7070             " ***/",
7071             format("/*\n"
7072                    " ***/"));
7073 }
7074 
7075 TEST_F(FormatTest, BlockCommentsInMacros) {
7076   EXPECT_EQ("#define A          \\\n"
7077             "  {                \\\n"
7078             "    /* one line */ \\\n"
7079             "    someCall();",
7080             format("#define A {        \\\n"
7081                    "  /* one line */   \\\n"
7082                    "  someCall();",
7083                    getLLVMStyleWithColumns(20)));
7084   EXPECT_EQ("#define A          \\\n"
7085             "  {                \\\n"
7086             "    /* previous */ \\\n"
7087             "    /* one line */ \\\n"
7088             "    someCall();",
7089             format("#define A {        \\\n"
7090                    "  /* previous */   \\\n"
7091                    "  /* one line */   \\\n"
7092                    "  someCall();",
7093                    getLLVMStyleWithColumns(20)));
7094 }
7095 
7096 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7097   EXPECT_EQ("a = {\n"
7098             "    1111 /*    */\n"
7099             "};",
7100             format("a = {1111 /*    */\n"
7101                    "};",
7102                    getLLVMStyleWithColumns(15)));
7103   EXPECT_EQ("a = {\n"
7104             "    1111 /*      */\n"
7105             "};",
7106             format("a = {1111 /*      */\n"
7107                    "};",
7108                    getLLVMStyleWithColumns(15)));
7109 
7110   // FIXME: The formatting is still wrong here.
7111   EXPECT_EQ("a = {\n"
7112             "    1111 /*      a\n"
7113             "            */\n"
7114             "};",
7115             format("a = {1111 /*      a */\n"
7116                    "};",
7117                    getLLVMStyleWithColumns(15)));
7118 }
7119 
7120 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7121   verifyFormat("{\n"
7122                "  // a\n"
7123                "  // b");
7124 }
7125 
7126 TEST_F(FormatTest, FormatStarDependingOnContext) {
7127   verifyFormat("void f(int *a);");
7128   verifyFormat("void f() { f(fint * b); }");
7129   verifyFormat("class A {\n  void f(int *a);\n};");
7130   verifyFormat("class A {\n  int *a;\n};");
7131   verifyFormat("namespace a {\n"
7132                "namespace b {\n"
7133                "class A {\n"
7134                "  void f() {}\n"
7135                "  int *a;\n"
7136                "};\n"
7137                "}\n"
7138                "}");
7139 }
7140 
7141 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7142   verifyFormat("while");
7143   verifyFormat("operator");
7144 }
7145 
7146 //===----------------------------------------------------------------------===//
7147 // Objective-C tests.
7148 //===----------------------------------------------------------------------===//
7149 
7150 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7151   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7152   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7153             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7154   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7155   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7156   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7157             format("-(NSInteger)Method3:(id)anObject;"));
7158   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7159             format("-(NSInteger)Method4:(id)anObject;"));
7160   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7161             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7162   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7163             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7164   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7165             "forAllCells:(BOOL)flag;",
7166             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7167                    "forAllCells:(BOOL)flag;"));
7168 
7169   // Very long objectiveC method declaration.
7170   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7171                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7172   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7173                "                    inRange:(NSRange)range\n"
7174                "                   outRange:(NSRange)out_range\n"
7175                "                  outRange1:(NSRange)out_range1\n"
7176                "                  outRange2:(NSRange)out_range2\n"
7177                "                  outRange3:(NSRange)out_range3\n"
7178                "                  outRange4:(NSRange)out_range4\n"
7179                "                  outRange5:(NSRange)out_range5\n"
7180                "                  outRange6:(NSRange)out_range6\n"
7181                "                  outRange7:(NSRange)out_range7\n"
7182                "                  outRange8:(NSRange)out_range8\n"
7183                "                  outRange9:(NSRange)out_range9;");
7184 
7185   // When the function name has to be wrapped.
7186   FormatStyle Style = getLLVMStyle();
7187   Style.IndentWrappedFunctionNames = false;
7188   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7189                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7190                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7191                "}",
7192                Style);
7193   Style.IndentWrappedFunctionNames = true;
7194   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7195                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7196                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7197                "}",
7198                Style);
7199 
7200   verifyFormat("- (int)sum:(vector<int>)numbers;");
7201   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7202   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7203   // protocol lists (but not for template classes):
7204   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7205 
7206   verifyFormat("- (int (*)())foo:(int (*)())f;");
7207   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7208 
7209   // If there's no return type (very rare in practice!), LLVM and Google style
7210   // agree.
7211   verifyFormat("- foo;");
7212   verifyFormat("- foo:(int)f;");
7213   verifyGoogleFormat("- foo:(int)foo;");
7214 }
7215 
7216 TEST_F(FormatTest, FormatObjCInterface) {
7217   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
7218                "@public\n"
7219                "  int field1;\n"
7220                "@protected\n"
7221                "  int field2;\n"
7222                "@private\n"
7223                "  int field3;\n"
7224                "@package\n"
7225                "  int field4;\n"
7226                "}\n"
7227                "+ (id)init;\n"
7228                "@end");
7229 
7230   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
7231                      " @public\n"
7232                      "  int field1;\n"
7233                      " @protected\n"
7234                      "  int field2;\n"
7235                      " @private\n"
7236                      "  int field3;\n"
7237                      " @package\n"
7238                      "  int field4;\n"
7239                      "}\n"
7240                      "+ (id)init;\n"
7241                      "@end");
7242 
7243   verifyFormat("@interface /* wait for it */ Foo\n"
7244                "+ (id)init;\n"
7245                "// Look, a comment!\n"
7246                "- (int)answerWith:(int)i;\n"
7247                "@end");
7248 
7249   verifyFormat("@interface Foo\n"
7250                "@end\n"
7251                "@interface Bar\n"
7252                "@end");
7253 
7254   verifyFormat("@interface Foo : Bar\n"
7255                "+ (id)init;\n"
7256                "@end");
7257 
7258   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
7259                "+ (id)init;\n"
7260                "@end");
7261 
7262   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
7263                      "+ (id)init;\n"
7264                      "@end");
7265 
7266   verifyFormat("@interface Foo (HackStuff)\n"
7267                "+ (id)init;\n"
7268                "@end");
7269 
7270   verifyFormat("@interface Foo ()\n"
7271                "+ (id)init;\n"
7272                "@end");
7273 
7274   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7275                "+ (id)init;\n"
7276                "@end");
7277 
7278   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7279                      "+ (id)init;\n"
7280                      "@end");
7281 
7282   verifyFormat("@interface Foo {\n"
7283                "  int _i;\n"
7284                "}\n"
7285                "+ (id)init;\n"
7286                "@end");
7287 
7288   verifyFormat("@interface Foo : Bar {\n"
7289                "  int _i;\n"
7290                "}\n"
7291                "+ (id)init;\n"
7292                "@end");
7293 
7294   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7295                "  int _i;\n"
7296                "}\n"
7297                "+ (id)init;\n"
7298                "@end");
7299 
7300   verifyFormat("@interface Foo (HackStuff) {\n"
7301                "  int _i;\n"
7302                "}\n"
7303                "+ (id)init;\n"
7304                "@end");
7305 
7306   verifyFormat("@interface Foo () {\n"
7307                "  int _i;\n"
7308                "}\n"
7309                "+ (id)init;\n"
7310                "@end");
7311 
7312   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7313                "  int _i;\n"
7314                "}\n"
7315                "+ (id)init;\n"
7316                "@end");
7317 
7318   FormatStyle OnePerLine = getGoogleStyle();
7319   OnePerLine.BinPackParameters = false;
7320   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7321                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7322                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7323                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7324                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7325                "}",
7326                OnePerLine);
7327 }
7328 
7329 TEST_F(FormatTest, FormatObjCImplementation) {
7330   verifyFormat("@implementation Foo : NSObject {\n"
7331                "@public\n"
7332                "  int field1;\n"
7333                "@protected\n"
7334                "  int field2;\n"
7335                "@private\n"
7336                "  int field3;\n"
7337                "@package\n"
7338                "  int field4;\n"
7339                "}\n"
7340                "+ (id)init {\n}\n"
7341                "@end");
7342 
7343   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7344                      " @public\n"
7345                      "  int field1;\n"
7346                      " @protected\n"
7347                      "  int field2;\n"
7348                      " @private\n"
7349                      "  int field3;\n"
7350                      " @package\n"
7351                      "  int field4;\n"
7352                      "}\n"
7353                      "+ (id)init {\n}\n"
7354                      "@end");
7355 
7356   verifyFormat("@implementation Foo\n"
7357                "+ (id)init {\n"
7358                "  if (true)\n"
7359                "    return nil;\n"
7360                "}\n"
7361                "// Look, a comment!\n"
7362                "- (int)answerWith:(int)i {\n"
7363                "  return i;\n"
7364                "}\n"
7365                "+ (int)answerWith:(int)i {\n"
7366                "  return i;\n"
7367                "}\n"
7368                "@end");
7369 
7370   verifyFormat("@implementation Foo\n"
7371                "@end\n"
7372                "@implementation Bar\n"
7373                "@end");
7374 
7375   EXPECT_EQ("@implementation Foo : Bar\n"
7376             "+ (id)init {\n}\n"
7377             "- (void)foo {\n}\n"
7378             "@end",
7379             format("@implementation Foo : Bar\n"
7380                    "+(id)init{}\n"
7381                    "-(void)foo{}\n"
7382                    "@end"));
7383 
7384   verifyFormat("@implementation Foo {\n"
7385                "  int _i;\n"
7386                "}\n"
7387                "+ (id)init {\n}\n"
7388                "@end");
7389 
7390   verifyFormat("@implementation Foo : Bar {\n"
7391                "  int _i;\n"
7392                "}\n"
7393                "+ (id)init {\n}\n"
7394                "@end");
7395 
7396   verifyFormat("@implementation Foo (HackStuff)\n"
7397                "+ (id)init {\n}\n"
7398                "@end");
7399   verifyFormat("@implementation ObjcClass\n"
7400                "- (void)method;\n"
7401                "{}\n"
7402                "@end");
7403 }
7404 
7405 TEST_F(FormatTest, FormatObjCProtocol) {
7406   verifyFormat("@protocol Foo\n"
7407                "@property(weak) id delegate;\n"
7408                "- (NSUInteger)numberOfThings;\n"
7409                "@end");
7410 
7411   verifyFormat("@protocol MyProtocol <NSObject>\n"
7412                "- (NSUInteger)numberOfThings;\n"
7413                "@end");
7414 
7415   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7416                      "- (NSUInteger)numberOfThings;\n"
7417                      "@end");
7418 
7419   verifyFormat("@protocol Foo;\n"
7420                "@protocol Bar;\n");
7421 
7422   verifyFormat("@protocol Foo\n"
7423                "@end\n"
7424                "@protocol Bar\n"
7425                "@end");
7426 
7427   verifyFormat("@protocol myProtocol\n"
7428                "- (void)mandatoryWithInt:(int)i;\n"
7429                "@optional\n"
7430                "- (void)optional;\n"
7431                "@required\n"
7432                "- (void)required;\n"
7433                "@optional\n"
7434                "@property(assign) int madProp;\n"
7435                "@end\n");
7436 
7437   verifyFormat("@property(nonatomic, assign, readonly)\n"
7438                "    int *looooooooooooooooooooooooooooongNumber;\n"
7439                "@property(nonatomic, assign, readonly)\n"
7440                "    NSString *looooooooooooooooooooooooooooongName;");
7441 
7442   verifyFormat("@implementation PR18406\n"
7443                "}\n"
7444                "@end");
7445 }
7446 
7447 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7448   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7449                "                   rect:(NSRect)theRect\n"
7450                "               interval:(float)theInterval {\n"
7451                "}");
7452   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7453                "      longKeyword:(NSRect)theRect\n"
7454                "    longerKeyword:(float)theInterval\n"
7455                "            error:(NSError **)theError {\n"
7456                "}");
7457   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7458                "          longKeyword:(NSRect)theRect\n"
7459                "    evenLongerKeyword:(float)theInterval\n"
7460                "                error:(NSError **)theError {\n"
7461                "}");
7462   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7463                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7464                "    NS_DESIGNATED_INITIALIZER;",
7465                getLLVMStyleWithColumns(60));
7466 
7467   // Continuation indent width should win over aligning colons if the function
7468   // name is long.
7469   FormatStyle continuationStyle = getGoogleStyle();
7470   continuationStyle.ColumnLimit = 40;
7471   continuationStyle.IndentWrappedFunctionNames = true;
7472   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7473                "    dontAlignNamef:(NSRect)theRect {\n"
7474                "}",
7475                continuationStyle);
7476 
7477   // Make sure we don't break aligning for short parameter names.
7478   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7479                "       aShortf:(NSRect)theRect {\n"
7480                "}",
7481                continuationStyle);
7482 }
7483 
7484 TEST_F(FormatTest, FormatObjCMethodExpr) {
7485   verifyFormat("[foo bar:baz];");
7486   verifyFormat("return [foo bar:baz];");
7487   verifyFormat("return (a)[foo bar:baz];");
7488   verifyFormat("f([foo bar:baz]);");
7489   verifyFormat("f(2, [foo bar:baz]);");
7490   verifyFormat("f(2, a ? b : c);");
7491   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7492 
7493   // Unary operators.
7494   verifyFormat("int a = +[foo bar:baz];");
7495   verifyFormat("int a = -[foo bar:baz];");
7496   verifyFormat("int a = ![foo bar:baz];");
7497   verifyFormat("int a = ~[foo bar:baz];");
7498   verifyFormat("int a = ++[foo bar:baz];");
7499   verifyFormat("int a = --[foo bar:baz];");
7500   verifyFormat("int a = sizeof [foo bar:baz];");
7501   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7502   verifyFormat("int a = &[foo bar:baz];");
7503   verifyFormat("int a = *[foo bar:baz];");
7504   // FIXME: Make casts work, without breaking f()[4].
7505   // verifyFormat("int a = (int)[foo bar:baz];");
7506   // verifyFormat("return (int)[foo bar:baz];");
7507   // verifyFormat("(void)[foo bar:baz];");
7508   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7509 
7510   // Binary operators.
7511   verifyFormat("[foo bar:baz], [foo bar:baz];");
7512   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7513   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7514   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7515   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7516   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7517   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7518   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7519   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7520   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7521   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7522   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7523   verifyFormat("[foo bar:baz] ? [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];");
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   // Whew!
7543 
7544   verifyFormat("return in[42];");
7545   verifyFormat("for (auto v : in[1]) {\n}");
7546   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7547   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7548   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7549   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7550   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7551   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7552                "}");
7553   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7554   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7555   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7556 
7557   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7558   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7559   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7560   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7561   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7562   verifyFormat("[button setAction:@selector(zoomOut:)];");
7563   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7564 
7565   verifyFormat("arr[[self indexForFoo:a]];");
7566   verifyFormat("throw [self errorFor:a];");
7567   verifyFormat("@throw [self errorFor:a];");
7568 
7569   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7570   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7571   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7572 
7573   // This tests that the formatter doesn't break after "backing" but before ":",
7574   // which would be at 80 columns.
7575   verifyFormat(
7576       "void f() {\n"
7577       "  if ((self = [super initWithContentRect:contentRect\n"
7578       "                               styleMask:styleMask ?: otherMask\n"
7579       "                                 backing:NSBackingStoreBuffered\n"
7580       "                                   defer:YES]))");
7581 
7582   verifyFormat(
7583       "[foo checkThatBreakingAfterColonWorksOk:\n"
7584       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7585 
7586   verifyFormat("[myObj short:arg1 // Force line break\n"
7587                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7588                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7589                "                error:arg4];");
7590   verifyFormat(
7591       "void f() {\n"
7592       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7593       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7594       "                                     pos.width(), pos.height())\n"
7595       "                styleMask:NSBorderlessWindowMask\n"
7596       "                  backing:NSBackingStoreBuffered\n"
7597       "                    defer:NO]);\n"
7598       "}");
7599   verifyFormat(
7600       "void f() {\n"
7601       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7602       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7603       "                                 pos.width(), pos.height())\n"
7604       "                syeMask:NSBorderlessWindowMask\n"
7605       "                  bking:NSBackingStoreBuffered\n"
7606       "                    der:NO]);\n"
7607       "}",
7608       getLLVMStyleWithColumns(70));
7609   verifyFormat(
7610       "void f() {\n"
7611       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7612       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7613       "                                     pos.width(), pos.height())\n"
7614       "                styleMask:NSBorderlessWindowMask\n"
7615       "                  backing:NSBackingStoreBuffered\n"
7616       "                    defer:NO]);\n"
7617       "}",
7618       getChromiumStyle(FormatStyle::LK_Cpp));
7619   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7620                "                             with:contentsNativeView];");
7621 
7622   verifyFormat(
7623       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7624       "           owner:nillllll];");
7625 
7626   verifyFormat(
7627       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7628       "        forType:kBookmarkButtonDragType];");
7629 
7630   verifyFormat("[defaultCenter addObserver:self\n"
7631                "                  selector:@selector(willEnterFullscreen)\n"
7632                "                      name:kWillEnterFullscreenNotification\n"
7633                "                    object:nil];");
7634   verifyFormat("[image_rep drawInRect:drawRect\n"
7635                "             fromRect:NSZeroRect\n"
7636                "            operation:NSCompositeCopy\n"
7637                "             fraction:1.0\n"
7638                "       respectFlipped:NO\n"
7639                "                hints:nil];");
7640   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7641                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7642   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7643                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7644   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7645                "    aaaaaaaaaaaaaaaaaaaaaa];");
7646   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7647                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7648                getLLVMStyleWithColumns(60));
7649 
7650   verifyFormat(
7651       "scoped_nsobject<NSTextField> message(\n"
7652       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7653       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7654   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7655                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7656                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7657                "          aaaa:bbb];");
7658   verifyFormat("[self param:function( //\n"
7659                "                parameter)]");
7660   verifyFormat(
7661       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7662       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7663       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7664 
7665   // FIXME: This violates the column limit.
7666   verifyFormat(
7667       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7668       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7669       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7670       getLLVMStyleWithColumns(60));
7671 
7672   // Variadic parameters.
7673   verifyFormat(
7674       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7675   verifyFormat(
7676       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7677       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7678       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7679   verifyFormat("[self // break\n"
7680                "      a:a\n"
7681                "    aaa:aaa];");
7682   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7683                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7684 }
7685 
7686 TEST_F(FormatTest, ObjCAt) {
7687   verifyFormat("@autoreleasepool");
7688   verifyFormat("@catch");
7689   verifyFormat("@class");
7690   verifyFormat("@compatibility_alias");
7691   verifyFormat("@defs");
7692   verifyFormat("@dynamic");
7693   verifyFormat("@encode");
7694   verifyFormat("@end");
7695   verifyFormat("@finally");
7696   verifyFormat("@implementation");
7697   verifyFormat("@import");
7698   verifyFormat("@interface");
7699   verifyFormat("@optional");
7700   verifyFormat("@package");
7701   verifyFormat("@private");
7702   verifyFormat("@property");
7703   verifyFormat("@protected");
7704   verifyFormat("@protocol");
7705   verifyFormat("@public");
7706   verifyFormat("@required");
7707   verifyFormat("@selector");
7708   verifyFormat("@synchronized");
7709   verifyFormat("@synthesize");
7710   verifyFormat("@throw");
7711   verifyFormat("@try");
7712 
7713   EXPECT_EQ("@interface", format("@ interface"));
7714 
7715   // The precise formatting of this doesn't matter, nobody writes code like
7716   // this.
7717   verifyFormat("@ /*foo*/ interface");
7718 }
7719 
7720 TEST_F(FormatTest, ObjCSnippets) {
7721   verifyFormat("@autoreleasepool {\n"
7722                "  foo();\n"
7723                "}");
7724   verifyFormat("@class Foo, Bar;");
7725   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7726   verifyFormat("@dynamic textColor;");
7727   verifyFormat("char *buf1 = @encode(int *);");
7728   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7729   verifyFormat("char *buf1 = @encode(int **);");
7730   verifyFormat("Protocol *proto = @protocol(p1);");
7731   verifyFormat("SEL s = @selector(foo:);");
7732   verifyFormat("@synchronized(self) {\n"
7733                "  f();\n"
7734                "}");
7735 
7736   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7737   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7738 
7739   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7740   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7741   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7742   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7743                getMozillaStyle());
7744   verifyFormat("@property BOOL editable;", getMozillaStyle());
7745   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7746                getWebKitStyle());
7747   verifyFormat("@property BOOL editable;", getWebKitStyle());
7748 
7749   verifyFormat("@import foo.bar;\n"
7750                "@import baz;");
7751 }
7752 
7753 TEST_F(FormatTest, ObjCForIn) {
7754   verifyFormat("- (void)test {\n"
7755                "  for (NSString *n in arrayOfStrings) {\n"
7756                "    foo(n);\n"
7757                "  }\n"
7758                "}");
7759   verifyFormat("- (void)test {\n"
7760                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7761                "    foo(n);\n"
7762                "  }\n"
7763                "}");
7764 }
7765 
7766 TEST_F(FormatTest, ObjCLiterals) {
7767   verifyFormat("@\"String\"");
7768   verifyFormat("@1");
7769   verifyFormat("@+4.8");
7770   verifyFormat("@-4");
7771   verifyFormat("@1LL");
7772   verifyFormat("@.5");
7773   verifyFormat("@'c'");
7774   verifyFormat("@true");
7775 
7776   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7777   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7778   verifyFormat("NSNumber *favoriteColor = @(Green);");
7779   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7780 
7781   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7782 }
7783 
7784 TEST_F(FormatTest, ObjCDictLiterals) {
7785   verifyFormat("@{");
7786   verifyFormat("@{}");
7787   verifyFormat("@{@\"one\" : @1}");
7788   verifyFormat("return @{@\"one\" : @1;");
7789   verifyFormat("@{@\"one\" : @1}");
7790 
7791   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7792   verifyFormat("@{\n"
7793                "  @\"one\" : @{@2 : @1},\n"
7794                "}");
7795 
7796   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7797   verifyIncompleteFormat("[self setDict:@{}");
7798   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7799   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7800   verifyFormat(
7801       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7802   verifyFormat(
7803       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7804 
7805   verifyFormat("NSDictionary *d = @{\n"
7806                "  @\"nam\" : NSUserNam(),\n"
7807                "  @\"dte\" : [NSDate date],\n"
7808                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7809                "};");
7810   verifyFormat(
7811       "@{\n"
7812       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7813       "regularFont,\n"
7814       "};");
7815   verifyGoogleFormat(
7816       "@{\n"
7817       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7818       "regularFont,\n"
7819       "};");
7820   verifyFormat(
7821       "@{\n"
7822       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7823       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7824       "};");
7825 
7826   // We should try to be robust in case someone forgets the "@".
7827   verifyFormat("NSDictionary *d = {\n"
7828                "  @\"nam\" : NSUserNam(),\n"
7829                "  @\"dte\" : [NSDate date],\n"
7830                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7831                "};");
7832   verifyFormat("NSMutableDictionary *dictionary =\n"
7833                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7834                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7835                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7836                "      cccccccccccccccc : ccccccccccccccc\n"
7837                "    }];");
7838 
7839   // Ensure that casts before the key are kept on the same line as the key.
7840   verifyFormat(
7841       "NSDictionary *d = @{\n"
7842       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7843       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7844       "};");
7845 }
7846 
7847 TEST_F(FormatTest, ObjCArrayLiterals) {
7848   verifyIncompleteFormat("@[");
7849   verifyFormat("@[]");
7850   verifyFormat(
7851       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7852   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7853   verifyFormat("NSArray *array = @[ [foo description] ];");
7854 
7855   verifyFormat(
7856       "NSArray *some_variable = @[\n"
7857       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7858       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7859       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7860       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7861       "];");
7862   verifyFormat(
7863       "NSArray *some_variable = @[\n"
7864       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7865       "  @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
7866       "];");
7867   verifyFormat("NSArray *some_variable = @[\n"
7868                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7869                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7870                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7871                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7872                "];");
7873   verifyFormat("NSArray *array = @[\n"
7874                "  @\"a\",\n"
7875                "  @\"a\",\n" // Trailing comma -> one per line.
7876                "];");
7877 
7878   // We should try to be robust in case someone forgets the "@".
7879   verifyFormat("NSArray *some_variable = [\n"
7880                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7881                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7882                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7883                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7884                "];");
7885   verifyFormat(
7886       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7887       "                                             index:(NSUInteger)index\n"
7888       "                                nonDigitAttributes:\n"
7889       "                                    (NSDictionary *)noDigitAttributes;");
7890   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7891                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7892                "]];");
7893 }
7894 
7895 TEST_F(FormatTest, BreaksStringLiterals) {
7896   EXPECT_EQ("\"some text \"\n"
7897             "\"other\";",
7898             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7899   EXPECT_EQ("\"some text \"\n"
7900             "\"other\";",
7901             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7902   EXPECT_EQ(
7903       "#define A  \\\n"
7904       "  \"some \"  \\\n"
7905       "  \"text \"  \\\n"
7906       "  \"other\";",
7907       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7908   EXPECT_EQ(
7909       "#define A  \\\n"
7910       "  \"so \"    \\\n"
7911       "  \"text \"  \\\n"
7912       "  \"other\";",
7913       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7914 
7915   EXPECT_EQ("\"some text\"",
7916             format("\"some text\"", getLLVMStyleWithColumns(1)));
7917   EXPECT_EQ("\"some text\"",
7918             format("\"some text\"", getLLVMStyleWithColumns(11)));
7919   EXPECT_EQ("\"some \"\n"
7920             "\"text\"",
7921             format("\"some text\"", getLLVMStyleWithColumns(10)));
7922   EXPECT_EQ("\"some \"\n"
7923             "\"text\"",
7924             format("\"some text\"", getLLVMStyleWithColumns(7)));
7925   EXPECT_EQ("\"some\"\n"
7926             "\" tex\"\n"
7927             "\"t\"",
7928             format("\"some text\"", getLLVMStyleWithColumns(6)));
7929   EXPECT_EQ("\"some\"\n"
7930             "\" tex\"\n"
7931             "\" and\"",
7932             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7933   EXPECT_EQ("\"some\"\n"
7934             "\"/tex\"\n"
7935             "\"/and\"",
7936             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7937 
7938   EXPECT_EQ("variable =\n"
7939             "    \"long string \"\n"
7940             "    \"literal\";",
7941             format("variable = \"long string literal\";",
7942                    getLLVMStyleWithColumns(20)));
7943 
7944   EXPECT_EQ("variable = f(\n"
7945             "    \"long string \"\n"
7946             "    \"literal\",\n"
7947             "    short,\n"
7948             "    loooooooooooooooooooong);",
7949             format("variable = f(\"long string literal\", short, "
7950                    "loooooooooooooooooooong);",
7951                    getLLVMStyleWithColumns(20)));
7952 
7953   EXPECT_EQ(
7954       "f(g(\"long string \"\n"
7955       "    \"literal\"),\n"
7956       "  b);",
7957       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7958   EXPECT_EQ("f(g(\"long string \"\n"
7959             "    \"literal\",\n"
7960             "    a),\n"
7961             "  b);",
7962             format("f(g(\"long string literal\", a), b);",
7963                    getLLVMStyleWithColumns(20)));
7964   EXPECT_EQ(
7965       "f(\"one two\".split(\n"
7966       "    variable));",
7967       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7968   EXPECT_EQ("f(\"one two three four five six \"\n"
7969             "  \"seven\".split(\n"
7970             "      really_looooong_variable));",
7971             format("f(\"one two three four five six seven\"."
7972                    "split(really_looooong_variable));",
7973                    getLLVMStyleWithColumns(33)));
7974 
7975   EXPECT_EQ("f(\"some \"\n"
7976             "  \"text\",\n"
7977             "  other);",
7978             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7979 
7980   // Only break as a last resort.
7981   verifyFormat(
7982       "aaaaaaaaaaaaaaaaaaaa(\n"
7983       "    aaaaaaaaaaaaaaaaaaaa,\n"
7984       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7985 
7986   EXPECT_EQ("\"splitmea\"\n"
7987             "\"trandomp\"\n"
7988             "\"oint\"",
7989             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7990 
7991   EXPECT_EQ("\"split/\"\n"
7992             "\"pathat/\"\n"
7993             "\"slashes\"",
7994             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7995 
7996   EXPECT_EQ("\"split/\"\n"
7997             "\"pathat/\"\n"
7998             "\"slashes\"",
7999             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8000   EXPECT_EQ("\"split at \"\n"
8001             "\"spaces/at/\"\n"
8002             "\"slashes.at.any$\"\n"
8003             "\"non-alphanumeric%\"\n"
8004             "\"1111111111characte\"\n"
8005             "\"rs\"",
8006             format("\"split at "
8007                    "spaces/at/"
8008                    "slashes.at."
8009                    "any$non-"
8010                    "alphanumeric%"
8011                    "1111111111characte"
8012                    "rs\"",
8013                    getLLVMStyleWithColumns(20)));
8014 
8015   // Verify that splitting the strings understands
8016   // Style::AlwaysBreakBeforeMultilineStrings.
8017   EXPECT_EQ(
8018       "aaaaaaaaaaaa(\n"
8019       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8020       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8021       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8022              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8023              "aaaaaaaaaaaaaaaaaaaaaa\");",
8024              getGoogleStyle()));
8025   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8026             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8027             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8028                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8029                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8030                    getGoogleStyle()));
8031   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8032             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8033             format("llvm::outs() << "
8034                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8035                    "aaaaaaaaaaaaaaaaaaa\";"));
8036   EXPECT_EQ("ffff(\n"
8037             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8038             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8039             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8040                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8041                    getGoogleStyle()));
8042 
8043   FormatStyle Style = getLLVMStyleWithColumns(12);
8044   Style.BreakStringLiterals = false;
8045   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8046 
8047   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8048   AlignLeft.AlignEscapedNewlinesLeft = true;
8049   EXPECT_EQ("#define A \\\n"
8050             "  \"some \" \\\n"
8051             "  \"text \" \\\n"
8052             "  \"other\";",
8053             format("#define A \"some text other\";", AlignLeft));
8054 }
8055 
8056 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8057   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8058   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8059   EXPECT_EQ("int i = a(b());",
8060             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8061 }
8062 
8063 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8064   EXPECT_EQ(
8065       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8066       "(\n"
8067       "    \"x\t\");",
8068       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8069              "aaaaaaa("
8070              "\"x\t\");"));
8071 }
8072 
8073 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8074   EXPECT_EQ(
8075       "u8\"utf8 string \"\n"
8076       "u8\"literal\";",
8077       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8078   EXPECT_EQ(
8079       "u\"utf16 string \"\n"
8080       "u\"literal\";",
8081       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8082   EXPECT_EQ(
8083       "U\"utf32 string \"\n"
8084       "U\"literal\";",
8085       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8086   EXPECT_EQ("L\"wide string \"\n"
8087             "L\"literal\";",
8088             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8089   EXPECT_EQ("@\"NSString \"\n"
8090             "@\"literal\";",
8091             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8092 
8093   // This input makes clang-format try to split the incomplete unicode escape
8094   // sequence, which used to lead to a crasher.
8095   verifyNoCrash(
8096       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8097       getLLVMStyleWithColumns(60));
8098 }
8099 
8100 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8101   FormatStyle Style = getGoogleStyleWithColumns(15);
8102   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8103   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8104   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8105   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8106   EXPECT_EQ("u8R\"x(raw literal)x\";",
8107             format("u8R\"x(raw literal)x\";", Style));
8108 }
8109 
8110 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8111   FormatStyle Style = getLLVMStyleWithColumns(20);
8112   EXPECT_EQ(
8113       "_T(\"aaaaaaaaaaaaaa\")\n"
8114       "_T(\"aaaaaaaaaaaaaa\")\n"
8115       "_T(\"aaaaaaaaaaaa\")",
8116       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8117   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
8118             "     _T(\"aaaaaa\"),\n"
8119             "  z);",
8120             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8121 
8122   // FIXME: Handle embedded spaces in one iteration.
8123   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8124   //            "_T(\"aaaaaaaaaaaaa\")\n"
8125   //            "_T(\"aaaaaaaaaaaaa\")\n"
8126   //            "_T(\"a\")",
8127   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8128   //                   getLLVMStyleWithColumns(20)));
8129   EXPECT_EQ(
8130       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8131       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8132   EXPECT_EQ("f(\n"
8133             "#if !TEST\n"
8134             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8135             "#endif\n"
8136             "    );",
8137             format("f(\n"
8138                    "#if !TEST\n"
8139                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8140                    "#endif\n"
8141                    ");"));
8142   EXPECT_EQ("f(\n"
8143             "\n"
8144             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8145             format("f(\n"
8146                    "\n"
8147                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8148 }
8149 
8150 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8151   EXPECT_EQ(
8152       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8153       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8154       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8155       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8156              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8157              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8158 }
8159 
8160 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8161   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8162             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8163   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8164             "multiline raw string literal xxxxxxxxxxxxxx\n"
8165             ")x\",\n"
8166             "              a),\n"
8167             "            b);",
8168             format("fffffffffff(g(R\"x(\n"
8169                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8170                    ")x\", a), b);",
8171                    getGoogleStyleWithColumns(20)));
8172   EXPECT_EQ("fffffffffff(\n"
8173             "    g(R\"x(qqq\n"
8174             "multiline raw string literal xxxxxxxxxxxxxx\n"
8175             ")x\",\n"
8176             "      a),\n"
8177             "    b);",
8178             format("fffffffffff(g(R\"x(qqq\n"
8179                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8180                    ")x\", a), b);",
8181                    getGoogleStyleWithColumns(20)));
8182 
8183   EXPECT_EQ("fffffffffff(R\"x(\n"
8184             "multiline raw string literal xxxxxxxxxxxxxx\n"
8185             ")x\");",
8186             format("fffffffffff(R\"x(\n"
8187                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8188                    ")x\");",
8189                    getGoogleStyleWithColumns(20)));
8190   EXPECT_EQ("fffffffffff(R\"x(\n"
8191             "multiline raw string literal xxxxxxxxxxxxxx\n"
8192             ")x\" + bbbbbb);",
8193             format("fffffffffff(R\"x(\n"
8194                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8195                    ")x\" +   bbbbbb);",
8196                    getGoogleStyleWithColumns(20)));
8197   EXPECT_EQ("fffffffffff(\n"
8198             "    R\"x(\n"
8199             "multiline raw string literal xxxxxxxxxxxxxx\n"
8200             ")x\" +\n"
8201             "    bbbbbb);",
8202             format("fffffffffff(\n"
8203                    " R\"x(\n"
8204                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8205                    ")x\" + bbbbbb);",
8206                    getGoogleStyleWithColumns(20)));
8207 }
8208 
8209 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8210   verifyFormat("string a = \"unterminated;");
8211   EXPECT_EQ("function(\"unterminated,\n"
8212             "         OtherParameter);",
8213             format("function(  \"unterminated,\n"
8214                    "    OtherParameter);"));
8215 }
8216 
8217 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8218   FormatStyle Style = getLLVMStyle();
8219   Style.Standard = FormatStyle::LS_Cpp03;
8220   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8221             format("#define x(_a) printf(\"foo\"_a);", Style));
8222 }
8223 
8224 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8225 
8226 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8227   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8228             "             \"ddeeefff\");",
8229             format("someFunction(\"aaabbbcccdddeeefff\");",
8230                    getLLVMStyleWithColumns(25)));
8231   EXPECT_EQ("someFunction1234567890(\n"
8232             "    \"aaabbbcccdddeeefff\");",
8233             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8234                    getLLVMStyleWithColumns(26)));
8235   EXPECT_EQ("someFunction1234567890(\n"
8236             "    \"aaabbbcccdddeeeff\"\n"
8237             "    \"f\");",
8238             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8239                    getLLVMStyleWithColumns(25)));
8240   EXPECT_EQ("someFunction1234567890(\n"
8241             "    \"aaabbbcccdddeeeff\"\n"
8242             "    \"f\");",
8243             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8244                    getLLVMStyleWithColumns(24)));
8245   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8246             "             \"ddde \"\n"
8247             "             \"efff\");",
8248             format("someFunction(\"aaabbbcc ddde efff\");",
8249                    getLLVMStyleWithColumns(25)));
8250   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8251             "             \"ddeeefff\");",
8252             format("someFunction(\"aaabbbccc ddeeefff\");",
8253                    getLLVMStyleWithColumns(25)));
8254   EXPECT_EQ("someFunction1234567890(\n"
8255             "    \"aaabb \"\n"
8256             "    \"cccdddeeefff\");",
8257             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8258                    getLLVMStyleWithColumns(25)));
8259   EXPECT_EQ("#define A          \\\n"
8260             "  string s =       \\\n"
8261             "      \"123456789\"  \\\n"
8262             "      \"0\";         \\\n"
8263             "  int i;",
8264             format("#define A string s = \"1234567890\"; int i;",
8265                    getLLVMStyleWithColumns(20)));
8266   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8267   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8268             "             \"dddeeeff\"\n"
8269             "             \"f\");",
8270             format("someFunction(\"aaabbbcc dddeeefff\");",
8271                    getLLVMStyleWithColumns(25)));
8272 }
8273 
8274 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8275   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8276   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8277   EXPECT_EQ("\"test\"\n"
8278             "\"\\n\"",
8279             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8280   EXPECT_EQ("\"tes\\\\\"\n"
8281             "\"n\"",
8282             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8283   EXPECT_EQ("\"\\\\\\\\\"\n"
8284             "\"\\n\"",
8285             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8286   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8287   EXPECT_EQ("\"\\uff01\"\n"
8288             "\"test\"",
8289             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8290   EXPECT_EQ("\"\\Uff01ff02\"",
8291             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8292   EXPECT_EQ("\"\\x000000000001\"\n"
8293             "\"next\"",
8294             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8295   EXPECT_EQ("\"\\x000000000001next\"",
8296             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8297   EXPECT_EQ("\"\\x000000000001\"",
8298             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8299   EXPECT_EQ("\"test\"\n"
8300             "\"\\000000\"\n"
8301             "\"000001\"",
8302             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8303   EXPECT_EQ("\"test\\000\"\n"
8304             "\"00000000\"\n"
8305             "\"1\"",
8306             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8307 }
8308 
8309 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8310   verifyFormat("void f() {\n"
8311                "  return g() {}\n"
8312                "  void h() {}");
8313   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8314                "g();\n"
8315                "}");
8316 }
8317 
8318 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8319   verifyFormat(
8320       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8321 }
8322 
8323 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8324   verifyFormat("class X {\n"
8325                "  void f() {\n"
8326                "  }\n"
8327                "};",
8328                getLLVMStyleWithColumns(12));
8329 }
8330 
8331 TEST_F(FormatTest, ConfigurableIndentWidth) {
8332   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8333   EightIndent.IndentWidth = 8;
8334   EightIndent.ContinuationIndentWidth = 8;
8335   verifyFormat("void f() {\n"
8336                "        someFunction();\n"
8337                "        if (true) {\n"
8338                "                f();\n"
8339                "        }\n"
8340                "}",
8341                EightIndent);
8342   verifyFormat("class X {\n"
8343                "        void f() {\n"
8344                "        }\n"
8345                "};",
8346                EightIndent);
8347   verifyFormat("int x[] = {\n"
8348                "        call(),\n"
8349                "        call()};",
8350                EightIndent);
8351 }
8352 
8353 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8354   verifyFormat("double\n"
8355                "f();",
8356                getLLVMStyleWithColumns(8));
8357 }
8358 
8359 TEST_F(FormatTest, ConfigurableUseOfTab) {
8360   FormatStyle Tab = getLLVMStyleWithColumns(42);
8361   Tab.IndentWidth = 8;
8362   Tab.UseTab = FormatStyle::UT_Always;
8363   Tab.AlignEscapedNewlinesLeft = true;
8364 
8365   EXPECT_EQ("if (aaaaaaaa && // q\n"
8366             "    bb)\t\t// w\n"
8367             "\t;",
8368             format("if (aaaaaaaa &&// q\n"
8369                    "bb)// w\n"
8370                    ";",
8371                    Tab));
8372   EXPECT_EQ("if (aaa && bbb) // w\n"
8373             "\t;",
8374             format("if(aaa&&bbb)// w\n"
8375                    ";",
8376                    Tab));
8377 
8378   verifyFormat("class X {\n"
8379                "\tvoid f() {\n"
8380                "\t\tsomeFunction(parameter1,\n"
8381                "\t\t\t     parameter2);\n"
8382                "\t}\n"
8383                "};",
8384                Tab);
8385   verifyFormat("#define A                        \\\n"
8386                "\tvoid f() {               \\\n"
8387                "\t\tsomeFunction(    \\\n"
8388                "\t\t    parameter1,  \\\n"
8389                "\t\t    parameter2); \\\n"
8390                "\t}",
8391                Tab);
8392 
8393   Tab.TabWidth = 4;
8394   Tab.IndentWidth = 8;
8395   verifyFormat("class TabWidth4Indent8 {\n"
8396                "\t\tvoid f() {\n"
8397                "\t\t\t\tsomeFunction(parameter1,\n"
8398                "\t\t\t\t\t\t\t parameter2);\n"
8399                "\t\t}\n"
8400                "};",
8401                Tab);
8402 
8403   Tab.TabWidth = 4;
8404   Tab.IndentWidth = 4;
8405   verifyFormat("class TabWidth4Indent4 {\n"
8406                "\tvoid f() {\n"
8407                "\t\tsomeFunction(parameter1,\n"
8408                "\t\t\t\t\t parameter2);\n"
8409                "\t}\n"
8410                "};",
8411                Tab);
8412 
8413   Tab.TabWidth = 8;
8414   Tab.IndentWidth = 4;
8415   verifyFormat("class TabWidth8Indent4 {\n"
8416                "    void f() {\n"
8417                "\tsomeFunction(parameter1,\n"
8418                "\t\t     parameter2);\n"
8419                "    }\n"
8420                "};",
8421                Tab);
8422 
8423   Tab.TabWidth = 8;
8424   Tab.IndentWidth = 8;
8425   EXPECT_EQ("/*\n"
8426             "\t      a\t\tcomment\n"
8427             "\t      in multiple lines\n"
8428             "       */",
8429             format("   /*\t \t \n"
8430                    " \t \t a\t\tcomment\t \t\n"
8431                    " \t \t in multiple lines\t\n"
8432                    " \t  */",
8433                    Tab));
8434 
8435   Tab.UseTab = FormatStyle::UT_ForIndentation;
8436   verifyFormat("{\n"
8437                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8438                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8439                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8440                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8441                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8442                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8443                "};",
8444                Tab);
8445   verifyFormat("enum AA {\n"
8446                "\ta1, // Force multiple lines\n"
8447                "\ta2,\n"
8448                "\ta3\n"
8449                "};",
8450                Tab);
8451   EXPECT_EQ("if (aaaaaaaa && // q\n"
8452             "    bb)         // w\n"
8453             "\t;",
8454             format("if (aaaaaaaa &&// q\n"
8455                    "bb)// w\n"
8456                    ";",
8457                    Tab));
8458   verifyFormat("class X {\n"
8459                "\tvoid f() {\n"
8460                "\t\tsomeFunction(parameter1,\n"
8461                "\t\t             parameter2);\n"
8462                "\t}\n"
8463                "};",
8464                Tab);
8465   verifyFormat("{\n"
8466                "\tQ(\n"
8467                "\t    {\n"
8468                "\t\t    int a;\n"
8469                "\t\t    someFunction(aaaaaaaa,\n"
8470                "\t\t                 bbbbbbb);\n"
8471                "\t    },\n"
8472                "\t    p);\n"
8473                "}",
8474                Tab);
8475   EXPECT_EQ("{\n"
8476             "\t/* aaaa\n"
8477             "\t   bbbb */\n"
8478             "}",
8479             format("{\n"
8480                    "/* aaaa\n"
8481                    "   bbbb */\n"
8482                    "}",
8483                    Tab));
8484   EXPECT_EQ("{\n"
8485             "\t/*\n"
8486             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8487             "\t  bbbbbbbbbbbbb\n"
8488             "\t*/\n"
8489             "}",
8490             format("{\n"
8491                    "/*\n"
8492                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8493                    "*/\n"
8494                    "}",
8495                    Tab));
8496   EXPECT_EQ("{\n"
8497             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8498             "\t// bbbbbbbbbbbbb\n"
8499             "}",
8500             format("{\n"
8501                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8502                    "}",
8503                    Tab));
8504   EXPECT_EQ("{\n"
8505             "\t/*\n"
8506             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8507             "\t  bbbbbbbbbbbbb\n"
8508             "\t*/\n"
8509             "}",
8510             format("{\n"
8511                    "\t/*\n"
8512                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8513                    "\t*/\n"
8514                    "}",
8515                    Tab));
8516   EXPECT_EQ("{\n"
8517             "\t/*\n"
8518             "\n"
8519             "\t*/\n"
8520             "}",
8521             format("{\n"
8522                    "\t/*\n"
8523                    "\n"
8524                    "\t*/\n"
8525                    "}",
8526                    Tab));
8527   EXPECT_EQ("{\n"
8528             "\t/*\n"
8529             " asdf\n"
8530             "\t*/\n"
8531             "}",
8532             format("{\n"
8533                    "\t/*\n"
8534                    " asdf\n"
8535                    "\t*/\n"
8536                    "}",
8537                    Tab));
8538 
8539   Tab.UseTab = FormatStyle::UT_Never;
8540   EXPECT_EQ("/*\n"
8541             "              a\t\tcomment\n"
8542             "              in multiple lines\n"
8543             "       */",
8544             format("   /*\t \t \n"
8545                    " \t \t a\t\tcomment\t \t\n"
8546                    " \t \t in multiple lines\t\n"
8547                    " \t  */",
8548                    Tab));
8549   EXPECT_EQ("/* some\n"
8550             "   comment */",
8551             format(" \t \t /* some\n"
8552                    " \t \t    comment */",
8553                    Tab));
8554   EXPECT_EQ("int a; /* some\n"
8555             "   comment */",
8556             format(" \t \t int a; /* some\n"
8557                    " \t \t    comment */",
8558                    Tab));
8559 
8560   EXPECT_EQ("int a; /* some\n"
8561             "comment */",
8562             format(" \t \t int\ta; /* some\n"
8563                    " \t \t    comment */",
8564                    Tab));
8565   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8566             "    comment */",
8567             format(" \t \t f(\"\t\t\"); /* some\n"
8568                    " \t \t    comment */",
8569                    Tab));
8570   EXPECT_EQ("{\n"
8571             "  /*\n"
8572             "   * Comment\n"
8573             "   */\n"
8574             "  int i;\n"
8575             "}",
8576             format("{\n"
8577                    "\t/*\n"
8578                    "\t * Comment\n"
8579                    "\t */\n"
8580                    "\t int i;\n"
8581                    "}"));
8582 
8583   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8584   Tab.TabWidth = 8;
8585   Tab.IndentWidth = 8;
8586   EXPECT_EQ("if (aaaaaaaa && // q\n"
8587             "    bb)         // w\n"
8588             "\t;",
8589             format("if (aaaaaaaa &&// q\n"
8590                    "bb)// w\n"
8591                    ";",
8592                    Tab));
8593   EXPECT_EQ("if (aaa && bbb) // w\n"
8594             "\t;",
8595             format("if(aaa&&bbb)// w\n"
8596                    ";",
8597                    Tab));
8598   verifyFormat("class X {\n"
8599                "\tvoid f() {\n"
8600                "\t\tsomeFunction(parameter1,\n"
8601                "\t\t\t     parameter2);\n"
8602                "\t}\n"
8603                "};",
8604                Tab);
8605   verifyFormat("#define A                        \\\n"
8606                "\tvoid f() {               \\\n"
8607                "\t\tsomeFunction(    \\\n"
8608                "\t\t    parameter1,  \\\n"
8609                "\t\t    parameter2); \\\n"
8610                "\t}",
8611                Tab);
8612   Tab.TabWidth = 4;
8613   Tab.IndentWidth = 8;
8614   verifyFormat("class TabWidth4Indent8 {\n"
8615                "\t\tvoid f() {\n"
8616                "\t\t\t\tsomeFunction(parameter1,\n"
8617                "\t\t\t\t\t\t\t parameter2);\n"
8618                "\t\t}\n"
8619                "};",
8620                Tab);
8621   Tab.TabWidth = 4;
8622   Tab.IndentWidth = 4;
8623   verifyFormat("class TabWidth4Indent4 {\n"
8624                "\tvoid f() {\n"
8625                "\t\tsomeFunction(parameter1,\n"
8626                "\t\t\t\t\t parameter2);\n"
8627                "\t}\n"
8628                "};",
8629                Tab);
8630   Tab.TabWidth = 8;
8631   Tab.IndentWidth = 4;
8632   verifyFormat("class TabWidth8Indent4 {\n"
8633                "    void f() {\n"
8634                "\tsomeFunction(parameter1,\n"
8635                "\t\t     parameter2);\n"
8636                "    }\n"
8637                "};",
8638                Tab);
8639   Tab.TabWidth = 8;
8640   Tab.IndentWidth = 8;
8641   EXPECT_EQ("/*\n"
8642             "\t      a\t\tcomment\n"
8643             "\t      in multiple lines\n"
8644             "       */",
8645             format("   /*\t \t \n"
8646                    " \t \t a\t\tcomment\t \t\n"
8647                    " \t \t in multiple lines\t\n"
8648                    " \t  */",
8649                    Tab));
8650   verifyFormat("{\n"
8651                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8652                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8653                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8654                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8655                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8656                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8657                "};",
8658                Tab);
8659   verifyFormat("enum AA {\n"
8660                "\ta1, // Force multiple lines\n"
8661                "\ta2,\n"
8662                "\ta3\n"
8663                "};",
8664                Tab);
8665   EXPECT_EQ("if (aaaaaaaa && // q\n"
8666             "    bb)         // w\n"
8667             "\t;",
8668             format("if (aaaaaaaa &&// q\n"
8669                    "bb)// w\n"
8670                    ";",
8671                    Tab));
8672   verifyFormat("class X {\n"
8673                "\tvoid f() {\n"
8674                "\t\tsomeFunction(parameter1,\n"
8675                "\t\t\t     parameter2);\n"
8676                "\t}\n"
8677                "};",
8678                Tab);
8679   verifyFormat("{\n"
8680                "\tQ(\n"
8681                "\t    {\n"
8682                "\t\t    int a;\n"
8683                "\t\t    someFunction(aaaaaaaa,\n"
8684                "\t\t\t\t bbbbbbb);\n"
8685                "\t    },\n"
8686                "\t    p);\n"
8687                "}",
8688                Tab);
8689   EXPECT_EQ("{\n"
8690             "\t/* aaaa\n"
8691             "\t   bbbb */\n"
8692             "}",
8693             format("{\n"
8694                    "/* aaaa\n"
8695                    "   bbbb */\n"
8696                    "}",
8697                    Tab));
8698   EXPECT_EQ("{\n"
8699             "\t/*\n"
8700             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8701             "\t  bbbbbbbbbbbbb\n"
8702             "\t*/\n"
8703             "}",
8704             format("{\n"
8705                    "/*\n"
8706                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8707                    "*/\n"
8708                    "}",
8709                    Tab));
8710   EXPECT_EQ("{\n"
8711             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8712             "\t// bbbbbbbbbbbbb\n"
8713             "}",
8714             format("{\n"
8715                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8716                    "}",
8717                    Tab));
8718   EXPECT_EQ("{\n"
8719             "\t/*\n"
8720             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8721             "\t  bbbbbbbbbbbbb\n"
8722             "\t*/\n"
8723             "}",
8724             format("{\n"
8725                    "\t/*\n"
8726                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8727                    "\t*/\n"
8728                    "}",
8729                    Tab));
8730   EXPECT_EQ("{\n"
8731             "\t/*\n"
8732             "\n"
8733             "\t*/\n"
8734             "}",
8735             format("{\n"
8736                    "\t/*\n"
8737                    "\n"
8738                    "\t*/\n"
8739                    "}",
8740                    Tab));
8741   EXPECT_EQ("{\n"
8742             "\t/*\n"
8743             " asdf\n"
8744             "\t*/\n"
8745             "}",
8746             format("{\n"
8747                    "\t/*\n"
8748                    " asdf\n"
8749                    "\t*/\n"
8750                    "}",
8751                    Tab));
8752   EXPECT_EQ("/*\n"
8753             "\t      a\t\tcomment\n"
8754             "\t      in multiple lines\n"
8755             "       */",
8756             format("   /*\t \t \n"
8757                    " \t \t a\t\tcomment\t \t\n"
8758                    " \t \t in multiple lines\t\n"
8759                    " \t  */",
8760                    Tab));
8761   EXPECT_EQ("/* some\n"
8762             "   comment */",
8763             format(" \t \t /* some\n"
8764                    " \t \t    comment */",
8765                    Tab));
8766   EXPECT_EQ("int a; /* some\n"
8767             "   comment */",
8768             format(" \t \t int a; /* some\n"
8769                    " \t \t    comment */",
8770                    Tab));
8771   EXPECT_EQ("int a; /* some\n"
8772             "comment */",
8773             format(" \t \t int\ta; /* some\n"
8774                    " \t \t    comment */",
8775                    Tab));
8776   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8777             "    comment */",
8778             format(" \t \t f(\"\t\t\"); /* some\n"
8779                    " \t \t    comment */",
8780                    Tab));
8781   EXPECT_EQ("{\n"
8782             "  /*\n"
8783             "   * Comment\n"
8784             "   */\n"
8785             "  int i;\n"
8786             "}",
8787             format("{\n"
8788                    "\t/*\n"
8789                    "\t * Comment\n"
8790                    "\t */\n"
8791                    "\t int i;\n"
8792                    "}"));
8793   Tab.AlignConsecutiveAssignments = true;
8794   Tab.AlignConsecutiveDeclarations = true;
8795   Tab.TabWidth = 4;
8796   Tab.IndentWidth = 4;
8797   verifyFormat("class Assign {\n"
8798                "\tvoid f() {\n"
8799                "\t\tint         x      = 123;\n"
8800                "\t\tint         random = 4;\n"
8801                "\t\tstd::string alphabet =\n"
8802                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8803                "\t}\n"
8804                "};",
8805                Tab);
8806 }
8807 
8808 TEST_F(FormatTest, CalculatesOriginalColumn) {
8809   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8810             "q\"; /* some\n"
8811             "       comment */",
8812             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8813                    "q\"; /* some\n"
8814                    "       comment */",
8815                    getLLVMStyle()));
8816   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8817             "/* some\n"
8818             "   comment */",
8819             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8820                    " /* some\n"
8821                    "    comment */",
8822                    getLLVMStyle()));
8823   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8824             "qqq\n"
8825             "/* some\n"
8826             "   comment */",
8827             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8828                    "qqq\n"
8829                    " /* some\n"
8830                    "    comment */",
8831                    getLLVMStyle()));
8832   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8833             "wwww; /* some\n"
8834             "         comment */",
8835             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8836                    "wwww; /* some\n"
8837                    "         comment */",
8838                    getLLVMStyle()));
8839 }
8840 
8841 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8842   FormatStyle NoSpace = getLLVMStyle();
8843   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8844 
8845   verifyFormat("while(true)\n"
8846                "  continue;",
8847                NoSpace);
8848   verifyFormat("for(;;)\n"
8849                "  continue;",
8850                NoSpace);
8851   verifyFormat("if(true)\n"
8852                "  f();\n"
8853                "else if(true)\n"
8854                "  f();",
8855                NoSpace);
8856   verifyFormat("do {\n"
8857                "  do_something();\n"
8858                "} while(something());",
8859                NoSpace);
8860   verifyFormat("switch(x) {\n"
8861                "default:\n"
8862                "  break;\n"
8863                "}",
8864                NoSpace);
8865   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8866   verifyFormat("size_t x = sizeof(x);", NoSpace);
8867   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8868   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8869   verifyFormat("alignas(128) char a[128];", NoSpace);
8870   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8871   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8872   verifyFormat("int f() throw(Deprecated);", NoSpace);
8873   verifyFormat("typedef void (*cb)(int);", NoSpace);
8874   verifyFormat("T A::operator()();", NoSpace);
8875   verifyFormat("X A::operator++(T);", NoSpace);
8876 
8877   FormatStyle Space = getLLVMStyle();
8878   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8879 
8880   verifyFormat("int f ();", Space);
8881   verifyFormat("void f (int a, T b) {\n"
8882                "  while (true)\n"
8883                "    continue;\n"
8884                "}",
8885                Space);
8886   verifyFormat("if (true)\n"
8887                "  f ();\n"
8888                "else if (true)\n"
8889                "  f ();",
8890                Space);
8891   verifyFormat("do {\n"
8892                "  do_something ();\n"
8893                "} while (something ());",
8894                Space);
8895   verifyFormat("switch (x) {\n"
8896                "default:\n"
8897                "  break;\n"
8898                "}",
8899                Space);
8900   verifyFormat("A::A () : a (1) {}", Space);
8901   verifyFormat("void f () __attribute__ ((asdf));", Space);
8902   verifyFormat("*(&a + 1);\n"
8903                "&((&a)[1]);\n"
8904                "a[(b + c) * d];\n"
8905                "(((a + 1) * 2) + 3) * 4;",
8906                Space);
8907   verifyFormat("#define A(x) x", Space);
8908   verifyFormat("#define A (x) x", Space);
8909   verifyFormat("#if defined(x)\n"
8910                "#endif",
8911                Space);
8912   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8913   verifyFormat("size_t x = sizeof (x);", Space);
8914   verifyFormat("auto f (int x) -> decltype (x);", Space);
8915   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8916   verifyFormat("alignas (128) char a[128];", Space);
8917   verifyFormat("size_t x = alignof (MyType);", Space);
8918   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8919   verifyFormat("int f () throw (Deprecated);", Space);
8920   verifyFormat("typedef void (*cb) (int);", Space);
8921   verifyFormat("T A::operator() ();", Space);
8922   verifyFormat("X A::operator++ (T);", Space);
8923 }
8924 
8925 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8926   FormatStyle Spaces = getLLVMStyle();
8927 
8928   Spaces.SpacesInParentheses = true;
8929   verifyFormat("call( x, y, z );", Spaces);
8930   verifyFormat("call();", Spaces);
8931   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8932   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8933                Spaces);
8934   verifyFormat("while ( (bool)1 )\n"
8935                "  continue;",
8936                Spaces);
8937   verifyFormat("for ( ;; )\n"
8938                "  continue;",
8939                Spaces);
8940   verifyFormat("if ( true )\n"
8941                "  f();\n"
8942                "else if ( true )\n"
8943                "  f();",
8944                Spaces);
8945   verifyFormat("do {\n"
8946                "  do_something( (int)i );\n"
8947                "} while ( something() );",
8948                Spaces);
8949   verifyFormat("switch ( x ) {\n"
8950                "default:\n"
8951                "  break;\n"
8952                "}",
8953                Spaces);
8954 
8955   Spaces.SpacesInParentheses = false;
8956   Spaces.SpacesInCStyleCastParentheses = true;
8957   verifyFormat("Type *A = ( Type * )P;", Spaces);
8958   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8959   verifyFormat("x = ( int32 )y;", Spaces);
8960   verifyFormat("int a = ( int )(2.0f);", Spaces);
8961   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8962   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8963   verifyFormat("#define x (( int )-1)", Spaces);
8964 
8965   // Run the first set of tests again with:
8966   Spaces.SpacesInParentheses = false;
8967   Spaces.SpaceInEmptyParentheses = true;
8968   Spaces.SpacesInCStyleCastParentheses = true;
8969   verifyFormat("call(x, y, z);", Spaces);
8970   verifyFormat("call( );", Spaces);
8971   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8972   verifyFormat("while (( bool )1)\n"
8973                "  continue;",
8974                Spaces);
8975   verifyFormat("for (;;)\n"
8976                "  continue;",
8977                Spaces);
8978   verifyFormat("if (true)\n"
8979                "  f( );\n"
8980                "else if (true)\n"
8981                "  f( );",
8982                Spaces);
8983   verifyFormat("do {\n"
8984                "  do_something(( int )i);\n"
8985                "} while (something( ));",
8986                Spaces);
8987   verifyFormat("switch (x) {\n"
8988                "default:\n"
8989                "  break;\n"
8990                "}",
8991                Spaces);
8992 
8993   // Run the first set of tests again with:
8994   Spaces.SpaceAfterCStyleCast = true;
8995   verifyFormat("call(x, y, z);", Spaces);
8996   verifyFormat("call( );", Spaces);
8997   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8998   verifyFormat("while (( bool ) 1)\n"
8999                "  continue;",
9000                Spaces);
9001   verifyFormat("for (;;)\n"
9002                "  continue;",
9003                Spaces);
9004   verifyFormat("if (true)\n"
9005                "  f( );\n"
9006                "else if (true)\n"
9007                "  f( );",
9008                Spaces);
9009   verifyFormat("do {\n"
9010                "  do_something(( int ) i);\n"
9011                "} while (something( ));",
9012                Spaces);
9013   verifyFormat("switch (x) {\n"
9014                "default:\n"
9015                "  break;\n"
9016                "}",
9017                Spaces);
9018 
9019   // Run subset of tests again with:
9020   Spaces.SpacesInCStyleCastParentheses = false;
9021   Spaces.SpaceAfterCStyleCast = true;
9022   verifyFormat("while ((bool) 1)\n"
9023                "  continue;",
9024                Spaces);
9025   verifyFormat("do {\n"
9026                "  do_something((int) i);\n"
9027                "} while (something( ));",
9028                Spaces);
9029 }
9030 
9031 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9032   verifyFormat("int a[5];");
9033   verifyFormat("a[3] += 42;");
9034 
9035   FormatStyle Spaces = getLLVMStyle();
9036   Spaces.SpacesInSquareBrackets = true;
9037   // Lambdas unchanged.
9038   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9039   verifyFormat("return [i, args...] {};", Spaces);
9040 
9041   // Not lambdas.
9042   verifyFormat("int a[ 5 ];", Spaces);
9043   verifyFormat("a[ 3 ] += 42;", Spaces);
9044   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9045   verifyFormat("double &operator[](int i) { return 0; }\n"
9046                "int i;",
9047                Spaces);
9048   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9049   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9050   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9051 }
9052 
9053 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9054   verifyFormat("int a = 5;");
9055   verifyFormat("a += 42;");
9056   verifyFormat("a or_eq 8;");
9057 
9058   FormatStyle Spaces = getLLVMStyle();
9059   Spaces.SpaceBeforeAssignmentOperators = false;
9060   verifyFormat("int a= 5;", Spaces);
9061   verifyFormat("a+= 42;", Spaces);
9062   verifyFormat("a or_eq 8;", Spaces);
9063 }
9064 
9065 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9066   FormatStyle Alignment = getLLVMStyle();
9067   Alignment.AlignConsecutiveAssignments = false;
9068   verifyFormat("int a = 5;\n"
9069                "int oneTwoThree = 123;",
9070                Alignment);
9071   verifyFormat("int a = 5;\n"
9072                "int oneTwoThree = 123;",
9073                Alignment);
9074 
9075   Alignment.AlignConsecutiveAssignments = true;
9076   verifyFormat("int a           = 5;\n"
9077                "int oneTwoThree = 123;",
9078                Alignment);
9079   verifyFormat("int a           = method();\n"
9080                "int oneTwoThree = 133;",
9081                Alignment);
9082   verifyFormat("a &= 5;\n"
9083                "bcd *= 5;\n"
9084                "ghtyf += 5;\n"
9085                "dvfvdb -= 5;\n"
9086                "a /= 5;\n"
9087                "vdsvsv %= 5;\n"
9088                "sfdbddfbdfbb ^= 5;\n"
9089                "dvsdsv |= 5;\n"
9090                "int dsvvdvsdvvv = 123;",
9091                Alignment);
9092   verifyFormat("int i = 1, j = 10;\n"
9093                "something = 2000;",
9094                Alignment);
9095   verifyFormat("something = 2000;\n"
9096                "int i = 1, j = 10;\n",
9097                Alignment);
9098   verifyFormat("something = 2000;\n"
9099                "another   = 911;\n"
9100                "int i = 1, j = 10;\n"
9101                "oneMore = 1;\n"
9102                "i       = 2;",
9103                Alignment);
9104   verifyFormat("int a   = 5;\n"
9105                "int one = 1;\n"
9106                "method();\n"
9107                "int oneTwoThree = 123;\n"
9108                "int oneTwo      = 12;",
9109                Alignment);
9110   verifyFormat("int oneTwoThree = 123;\n"
9111                "int oneTwo      = 12;\n"
9112                "method();\n",
9113                Alignment);
9114   verifyFormat("int oneTwoThree = 123; // comment\n"
9115                "int oneTwo      = 12;  // comment",
9116                Alignment);
9117   EXPECT_EQ("int a = 5;\n"
9118             "\n"
9119             "int oneTwoThree = 123;",
9120             format("int a       = 5;\n"
9121                    "\n"
9122                    "int oneTwoThree= 123;",
9123                    Alignment));
9124   EXPECT_EQ("int a   = 5;\n"
9125             "int one = 1;\n"
9126             "\n"
9127             "int oneTwoThree = 123;",
9128             format("int a = 5;\n"
9129                    "int one = 1;\n"
9130                    "\n"
9131                    "int oneTwoThree = 123;",
9132                    Alignment));
9133   EXPECT_EQ("int a   = 5;\n"
9134             "int one = 1;\n"
9135             "\n"
9136             "int oneTwoThree = 123;\n"
9137             "int oneTwo      = 12;",
9138             format("int a = 5;\n"
9139                    "int one = 1;\n"
9140                    "\n"
9141                    "int oneTwoThree = 123;\n"
9142                    "int oneTwo = 12;",
9143                    Alignment));
9144   Alignment.AlignEscapedNewlinesLeft = true;
9145   verifyFormat("#define A               \\\n"
9146                "  int aaaa       = 12;  \\\n"
9147                "  int b          = 23;  \\\n"
9148                "  int ccc        = 234; \\\n"
9149                "  int dddddddddd = 2345;",
9150                Alignment);
9151   Alignment.AlignEscapedNewlinesLeft = false;
9152   verifyFormat("#define A                                                      "
9153                "                \\\n"
9154                "  int aaaa       = 12;                                         "
9155                "                \\\n"
9156                "  int b          = 23;                                         "
9157                "                \\\n"
9158                "  int ccc        = 234;                                        "
9159                "                \\\n"
9160                "  int dddddddddd = 2345;",
9161                Alignment);
9162   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9163                "k = 4, int l = 5,\n"
9164                "                  int m = 6) {\n"
9165                "  int j      = 10;\n"
9166                "  otherThing = 1;\n"
9167                "}",
9168                Alignment);
9169   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9170                "  int i   = 1;\n"
9171                "  int j   = 2;\n"
9172                "  int big = 10000;\n"
9173                "}",
9174                Alignment);
9175   verifyFormat("class C {\n"
9176                "public:\n"
9177                "  int i            = 1;\n"
9178                "  virtual void f() = 0;\n"
9179                "};",
9180                Alignment);
9181   verifyFormat("int i = 1;\n"
9182                "if (SomeType t = getSomething()) {\n"
9183                "}\n"
9184                "int j   = 2;\n"
9185                "int big = 10000;",
9186                Alignment);
9187   verifyFormat("int j = 7;\n"
9188                "for (int k = 0; k < N; ++k) {\n"
9189                "}\n"
9190                "int j   = 2;\n"
9191                "int big = 10000;\n"
9192                "}",
9193                Alignment);
9194   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9195   verifyFormat("int i = 1;\n"
9196                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9197                "    = someLooooooooooooooooongFunction();\n"
9198                "int j = 2;",
9199                Alignment);
9200   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9201   verifyFormat("int i = 1;\n"
9202                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9203                "    someLooooooooooooooooongFunction();\n"
9204                "int j = 2;",
9205                Alignment);
9206 
9207   verifyFormat("auto lambda = []() {\n"
9208                "  auto i = 0;\n"
9209                "  return 0;\n"
9210                "};\n"
9211                "int i  = 0;\n"
9212                "auto v = type{\n"
9213                "    i = 1,   //\n"
9214                "    (i = 2), //\n"
9215                "    i = 3    //\n"
9216                "};",
9217                Alignment);
9218 
9219   // FIXME: Should align all three assignments
9220   verifyFormat(
9221       "int i      = 1;\n"
9222       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9223       "                          loooooooooooooooooooooongParameterB);\n"
9224       "int j = 2;",
9225       Alignment);
9226 
9227   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9228                "          typename B   = very_long_type_name_1,\n"
9229                "          typename T_2 = very_long_type_name_2>\n"
9230                "auto foo() {}\n",
9231                Alignment);
9232   verifyFormat("int a, b = 1;\n"
9233                "int c  = 2;\n"
9234                "int dd = 3;\n",
9235                Alignment);
9236   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9237                "float b[1][] = {{3.f}};\n",
9238                Alignment);
9239 }
9240 
9241 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9242   FormatStyle Alignment = getLLVMStyle();
9243   Alignment.AlignConsecutiveDeclarations = false;
9244   verifyFormat("float const a = 5;\n"
9245                "int oneTwoThree = 123;",
9246                Alignment);
9247   verifyFormat("int a = 5;\n"
9248                "float const oneTwoThree = 123;",
9249                Alignment);
9250 
9251   Alignment.AlignConsecutiveDeclarations = true;
9252   verifyFormat("float const a = 5;\n"
9253                "int         oneTwoThree = 123;",
9254                Alignment);
9255   verifyFormat("int         a = method();\n"
9256                "float const oneTwoThree = 133;",
9257                Alignment);
9258   verifyFormat("int i = 1, j = 10;\n"
9259                "something = 2000;",
9260                Alignment);
9261   verifyFormat("something = 2000;\n"
9262                "int i = 1, j = 10;\n",
9263                Alignment);
9264   verifyFormat("float      something = 2000;\n"
9265                "double     another = 911;\n"
9266                "int        i = 1, j = 10;\n"
9267                "const int *oneMore = 1;\n"
9268                "unsigned   i = 2;",
9269                Alignment);
9270   verifyFormat("float a = 5;\n"
9271                "int   one = 1;\n"
9272                "method();\n"
9273                "const double       oneTwoThree = 123;\n"
9274                "const unsigned int oneTwo = 12;",
9275                Alignment);
9276   verifyFormat("int      oneTwoThree{0}; // comment\n"
9277                "unsigned oneTwo;         // comment",
9278                Alignment);
9279   EXPECT_EQ("float const a = 5;\n"
9280             "\n"
9281             "int oneTwoThree = 123;",
9282             format("float const   a = 5;\n"
9283                    "\n"
9284                    "int           oneTwoThree= 123;",
9285                    Alignment));
9286   EXPECT_EQ("float a = 5;\n"
9287             "int   one = 1;\n"
9288             "\n"
9289             "unsigned oneTwoThree = 123;",
9290             format("float    a = 5;\n"
9291                    "int      one = 1;\n"
9292                    "\n"
9293                    "unsigned oneTwoThree = 123;",
9294                    Alignment));
9295   EXPECT_EQ("float a = 5;\n"
9296             "int   one = 1;\n"
9297             "\n"
9298             "unsigned oneTwoThree = 123;\n"
9299             "int      oneTwo = 12;",
9300             format("float    a = 5;\n"
9301                    "int one = 1;\n"
9302                    "\n"
9303                    "unsigned oneTwoThree = 123;\n"
9304                    "int oneTwo = 12;",
9305                    Alignment));
9306   Alignment.AlignConsecutiveAssignments = true;
9307   verifyFormat("float      something = 2000;\n"
9308                "double     another   = 911;\n"
9309                "int        i = 1, j = 10;\n"
9310                "const int *oneMore = 1;\n"
9311                "unsigned   i       = 2;",
9312                Alignment);
9313   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9314                "unsigned oneTwo      = 0;   // comment",
9315                Alignment);
9316   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9317             "  int const i   = 1;\n"
9318             "  int *     j   = 2;\n"
9319             "  int       big = 10000;\n"
9320             "\n"
9321             "  unsigned oneTwoThree = 123;\n"
9322             "  int      oneTwo      = 12;\n"
9323             "  method();\n"
9324             "  float k  = 2;\n"
9325             "  int   ll = 10000;\n"
9326             "}",
9327             format("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                    Alignment));
9339   Alignment.AlignConsecutiveAssignments = false;
9340   Alignment.AlignEscapedNewlinesLeft = true;
9341   verifyFormat("#define A              \\\n"
9342                "  int       aaaa = 12; \\\n"
9343                "  float     b = 23;    \\\n"
9344                "  const int ccc = 234; \\\n"
9345                "  unsigned  dddddddddd = 2345;",
9346                Alignment);
9347   Alignment.AlignEscapedNewlinesLeft = false;
9348   Alignment.ColumnLimit = 30;
9349   verifyFormat("#define A                    \\\n"
9350                "  int       aaaa = 12;       \\\n"
9351                "  float     b = 23;          \\\n"
9352                "  const int ccc = 234;       \\\n"
9353                "  int       dddddddddd = 2345;",
9354                Alignment);
9355   Alignment.ColumnLimit = 80;
9356   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9357                "k = 4, int l = 5,\n"
9358                "                  int m = 6) {\n"
9359                "  const int j = 10;\n"
9360                "  otherThing = 1;\n"
9361                "}",
9362                Alignment);
9363   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9364                "  int const i = 1;\n"
9365                "  int *     j = 2;\n"
9366                "  int       big = 10000;\n"
9367                "}",
9368                Alignment);
9369   verifyFormat("class C {\n"
9370                "public:\n"
9371                "  int          i = 1;\n"
9372                "  virtual void f() = 0;\n"
9373                "};",
9374                Alignment);
9375   verifyFormat("float i = 1;\n"
9376                "if (SomeType t = getSomething()) {\n"
9377                "}\n"
9378                "const unsigned j = 2;\n"
9379                "int            big = 10000;",
9380                Alignment);
9381   verifyFormat("float j = 7;\n"
9382                "for (int k = 0; k < N; ++k) {\n"
9383                "}\n"
9384                "unsigned j = 2;\n"
9385                "int      big = 10000;\n"
9386                "}",
9387                Alignment);
9388   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9389   verifyFormat("float              i = 1;\n"
9390                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9391                "    = someLooooooooooooooooongFunction();\n"
9392                "int j = 2;",
9393                Alignment);
9394   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9395   verifyFormat("int                i = 1;\n"
9396                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9397                "    someLooooooooooooooooongFunction();\n"
9398                "int j = 2;",
9399                Alignment);
9400 
9401   Alignment.AlignConsecutiveAssignments = true;
9402   verifyFormat("auto lambda = []() {\n"
9403                "  auto  ii = 0;\n"
9404                "  float j  = 0;\n"
9405                "  return 0;\n"
9406                "};\n"
9407                "int   i  = 0;\n"
9408                "float i2 = 0;\n"
9409                "auto  v  = type{\n"
9410                "    i = 1,   //\n"
9411                "    (i = 2), //\n"
9412                "    i = 3    //\n"
9413                "};",
9414                Alignment);
9415   Alignment.AlignConsecutiveAssignments = false;
9416 
9417   // FIXME: Should align all three declarations
9418   verifyFormat(
9419       "int      i = 1;\n"
9420       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9421       "                          loooooooooooooooooooooongParameterB);\n"
9422       "int j = 2;",
9423       Alignment);
9424 
9425   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9426   // We expect declarations and assignments to align, as long as it doesn't
9427   // exceed the column limit, starting a new alignemnt sequence whenever it
9428   // happens.
9429   Alignment.AlignConsecutiveAssignments = true;
9430   Alignment.ColumnLimit = 30;
9431   verifyFormat("float    ii              = 1;\n"
9432                "unsigned j               = 2;\n"
9433                "int someVerylongVariable = 1;\n"
9434                "AnotherLongType  ll = 123456;\n"
9435                "VeryVeryLongType k  = 2;\n"
9436                "int              myvar = 1;",
9437                Alignment);
9438   Alignment.ColumnLimit = 80;
9439   Alignment.AlignConsecutiveAssignments = false;
9440 
9441   verifyFormat(
9442       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9443       "          typename LongType, typename B>\n"
9444       "auto foo() {}\n",
9445       Alignment);
9446   verifyFormat("float a, b = 1;\n"
9447                "int   c = 2;\n"
9448                "int   dd = 3;\n",
9449                Alignment);
9450   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9451                "float b[1][] = {{3.f}};\n",
9452                Alignment);
9453   Alignment.AlignConsecutiveAssignments = true;
9454   verifyFormat("float a, b = 1;\n"
9455                "int   c  = 2;\n"
9456                "int   dd = 3;\n",
9457                Alignment);
9458   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9459                "float b[1][] = {{3.f}};\n",
9460                Alignment);
9461   Alignment.AlignConsecutiveAssignments = false;
9462 
9463   Alignment.ColumnLimit = 30;
9464   Alignment.BinPackParameters = false;
9465   verifyFormat("void foo(float     a,\n"
9466                "         float     b,\n"
9467                "         int       c,\n"
9468                "         uint32_t *d) {\n"
9469                "  int *  e = 0;\n"
9470                "  float  f = 0;\n"
9471                "  double g = 0;\n"
9472                "}\n"
9473                "void bar(ino_t     a,\n"
9474                "         int       b,\n"
9475                "         uint32_t *c,\n"
9476                "         bool      d) {}\n",
9477                Alignment);
9478   Alignment.BinPackParameters = true;
9479   Alignment.ColumnLimit = 80;
9480 }
9481 
9482 TEST_F(FormatTest, LinuxBraceBreaking) {
9483   FormatStyle LinuxBraceStyle = getLLVMStyle();
9484   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9485   verifyFormat("namespace a\n"
9486                "{\n"
9487                "class A\n"
9488                "{\n"
9489                "  void f()\n"
9490                "  {\n"
9491                "    if (true) {\n"
9492                "      a();\n"
9493                "      b();\n"
9494                "    } else {\n"
9495                "      a();\n"
9496                "    }\n"
9497                "  }\n"
9498                "  void g() { return; }\n"
9499                "};\n"
9500                "struct B {\n"
9501                "  int x;\n"
9502                "};\n"
9503                "}\n",
9504                LinuxBraceStyle);
9505   verifyFormat("enum X {\n"
9506                "  Y = 0,\n"
9507                "}\n",
9508                LinuxBraceStyle);
9509   verifyFormat("struct S {\n"
9510                "  int Type;\n"
9511                "  union {\n"
9512                "    int x;\n"
9513                "    double y;\n"
9514                "  } Value;\n"
9515                "  class C\n"
9516                "  {\n"
9517                "    MyFavoriteType Value;\n"
9518                "  } Class;\n"
9519                "}\n",
9520                LinuxBraceStyle);
9521 }
9522 
9523 TEST_F(FormatTest, MozillaBraceBreaking) {
9524   FormatStyle MozillaBraceStyle = getLLVMStyle();
9525   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9526   verifyFormat("namespace a {\n"
9527                "class A\n"
9528                "{\n"
9529                "  void f()\n"
9530                "  {\n"
9531                "    if (true) {\n"
9532                "      a();\n"
9533                "      b();\n"
9534                "    }\n"
9535                "  }\n"
9536                "  void g() { return; }\n"
9537                "};\n"
9538                "enum E\n"
9539                "{\n"
9540                "  A,\n"
9541                "  // foo\n"
9542                "  B,\n"
9543                "  C\n"
9544                "};\n"
9545                "struct B\n"
9546                "{\n"
9547                "  int x;\n"
9548                "};\n"
9549                "}\n",
9550                MozillaBraceStyle);
9551   verifyFormat("struct S\n"
9552                "{\n"
9553                "  int Type;\n"
9554                "  union\n"
9555                "  {\n"
9556                "    int x;\n"
9557                "    double y;\n"
9558                "  } Value;\n"
9559                "  class C\n"
9560                "  {\n"
9561                "    MyFavoriteType Value;\n"
9562                "  } Class;\n"
9563                "}\n",
9564                MozillaBraceStyle);
9565 }
9566 
9567 TEST_F(FormatTest, StroustrupBraceBreaking) {
9568   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9569   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9570   verifyFormat("namespace a {\n"
9571                "class A {\n"
9572                "  void f()\n"
9573                "  {\n"
9574                "    if (true) {\n"
9575                "      a();\n"
9576                "      b();\n"
9577                "    }\n"
9578                "  }\n"
9579                "  void g() { return; }\n"
9580                "};\n"
9581                "struct B {\n"
9582                "  int x;\n"
9583                "};\n"
9584                "}\n",
9585                StroustrupBraceStyle);
9586 
9587   verifyFormat("void foo()\n"
9588                "{\n"
9589                "  if (a) {\n"
9590                "    a();\n"
9591                "  }\n"
9592                "  else {\n"
9593                "    b();\n"
9594                "  }\n"
9595                "}\n",
9596                StroustrupBraceStyle);
9597 
9598   verifyFormat("#ifdef _DEBUG\n"
9599                "int foo(int i = 0)\n"
9600                "#else\n"
9601                "int foo(int i = 5)\n"
9602                "#endif\n"
9603                "{\n"
9604                "  return i;\n"
9605                "}",
9606                StroustrupBraceStyle);
9607 
9608   verifyFormat("void foo() {}\n"
9609                "void bar()\n"
9610                "#ifdef _DEBUG\n"
9611                "{\n"
9612                "  foo();\n"
9613                "}\n"
9614                "#else\n"
9615                "{\n"
9616                "}\n"
9617                "#endif",
9618                StroustrupBraceStyle);
9619 
9620   verifyFormat("void foobar() { int i = 5; }\n"
9621                "#ifdef _DEBUG\n"
9622                "void bar() {}\n"
9623                "#else\n"
9624                "void bar() { foobar(); }\n"
9625                "#endif",
9626                StroustrupBraceStyle);
9627 }
9628 
9629 TEST_F(FormatTest, AllmanBraceBreaking) {
9630   FormatStyle AllmanBraceStyle = getLLVMStyle();
9631   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9632   verifyFormat("namespace a\n"
9633                "{\n"
9634                "class A\n"
9635                "{\n"
9636                "  void f()\n"
9637                "  {\n"
9638                "    if (true)\n"
9639                "    {\n"
9640                "      a();\n"
9641                "      b();\n"
9642                "    }\n"
9643                "  }\n"
9644                "  void g() { return; }\n"
9645                "};\n"
9646                "struct B\n"
9647                "{\n"
9648                "  int x;\n"
9649                "};\n"
9650                "}",
9651                AllmanBraceStyle);
9652 
9653   verifyFormat("void f()\n"
9654                "{\n"
9655                "  if (true)\n"
9656                "  {\n"
9657                "    a();\n"
9658                "  }\n"
9659                "  else if (false)\n"
9660                "  {\n"
9661                "    b();\n"
9662                "  }\n"
9663                "  else\n"
9664                "  {\n"
9665                "    c();\n"
9666                "  }\n"
9667                "}\n",
9668                AllmanBraceStyle);
9669 
9670   verifyFormat("void f()\n"
9671                "{\n"
9672                "  for (int i = 0; i < 10; ++i)\n"
9673                "  {\n"
9674                "    a();\n"
9675                "  }\n"
9676                "  while (false)\n"
9677                "  {\n"
9678                "    b();\n"
9679                "  }\n"
9680                "  do\n"
9681                "  {\n"
9682                "    c();\n"
9683                "  } while (false)\n"
9684                "}\n",
9685                AllmanBraceStyle);
9686 
9687   verifyFormat("void f(int a)\n"
9688                "{\n"
9689                "  switch (a)\n"
9690                "  {\n"
9691                "  case 0:\n"
9692                "    break;\n"
9693                "  case 1:\n"
9694                "  {\n"
9695                "    break;\n"
9696                "  }\n"
9697                "  case 2:\n"
9698                "  {\n"
9699                "  }\n"
9700                "  break;\n"
9701                "  default:\n"
9702                "    break;\n"
9703                "  }\n"
9704                "}\n",
9705                AllmanBraceStyle);
9706 
9707   verifyFormat("enum X\n"
9708                "{\n"
9709                "  Y = 0,\n"
9710                "}\n",
9711                AllmanBraceStyle);
9712   verifyFormat("enum X\n"
9713                "{\n"
9714                "  Y = 0\n"
9715                "}\n",
9716                AllmanBraceStyle);
9717 
9718   verifyFormat("@interface BSApplicationController ()\n"
9719                "{\n"
9720                "@private\n"
9721                "  id _extraIvar;\n"
9722                "}\n"
9723                "@end\n",
9724                AllmanBraceStyle);
9725 
9726   verifyFormat("#ifdef _DEBUG\n"
9727                "int foo(int i = 0)\n"
9728                "#else\n"
9729                "int foo(int i = 5)\n"
9730                "#endif\n"
9731                "{\n"
9732                "  return i;\n"
9733                "}",
9734                AllmanBraceStyle);
9735 
9736   verifyFormat("void foo() {}\n"
9737                "void bar()\n"
9738                "#ifdef _DEBUG\n"
9739                "{\n"
9740                "  foo();\n"
9741                "}\n"
9742                "#else\n"
9743                "{\n"
9744                "}\n"
9745                "#endif",
9746                AllmanBraceStyle);
9747 
9748   verifyFormat("void foobar() { int i = 5; }\n"
9749                "#ifdef _DEBUG\n"
9750                "void bar() {}\n"
9751                "#else\n"
9752                "void bar() { foobar(); }\n"
9753                "#endif",
9754                AllmanBraceStyle);
9755 
9756   // This shouldn't affect ObjC blocks..
9757   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9758                "  // ...\n"
9759                "  int i;\n"
9760                "}];",
9761                AllmanBraceStyle);
9762   verifyFormat("void (^block)(void) = ^{\n"
9763                "  // ...\n"
9764                "  int i;\n"
9765                "};",
9766                AllmanBraceStyle);
9767   // .. or dict literals.
9768   verifyFormat("void f()\n"
9769                "{\n"
9770                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9771                "}",
9772                AllmanBraceStyle);
9773   verifyFormat("int f()\n"
9774                "{ // comment\n"
9775                "  return 42;\n"
9776                "}",
9777                AllmanBraceStyle);
9778 
9779   AllmanBraceStyle.ColumnLimit = 19;
9780   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9781   AllmanBraceStyle.ColumnLimit = 18;
9782   verifyFormat("void f()\n"
9783                "{\n"
9784                "  int i;\n"
9785                "}",
9786                AllmanBraceStyle);
9787   AllmanBraceStyle.ColumnLimit = 80;
9788 
9789   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9790   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9791   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9792   verifyFormat("void f(bool b)\n"
9793                "{\n"
9794                "  if (b)\n"
9795                "  {\n"
9796                "    return;\n"
9797                "  }\n"
9798                "}\n",
9799                BreakBeforeBraceShortIfs);
9800   verifyFormat("void f(bool b)\n"
9801                "{\n"
9802                "  if (b) return;\n"
9803                "}\n",
9804                BreakBeforeBraceShortIfs);
9805   verifyFormat("void f(bool b)\n"
9806                "{\n"
9807                "  while (b)\n"
9808                "  {\n"
9809                "    return;\n"
9810                "  }\n"
9811                "}\n",
9812                BreakBeforeBraceShortIfs);
9813 }
9814 
9815 TEST_F(FormatTest, GNUBraceBreaking) {
9816   FormatStyle GNUBraceStyle = getLLVMStyle();
9817   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9818   verifyFormat("namespace a\n"
9819                "{\n"
9820                "class A\n"
9821                "{\n"
9822                "  void f()\n"
9823                "  {\n"
9824                "    int a;\n"
9825                "    {\n"
9826                "      int b;\n"
9827                "    }\n"
9828                "    if (true)\n"
9829                "      {\n"
9830                "        a();\n"
9831                "        b();\n"
9832                "      }\n"
9833                "  }\n"
9834                "  void g() { return; }\n"
9835                "}\n"
9836                "}",
9837                GNUBraceStyle);
9838 
9839   verifyFormat("void f()\n"
9840                "{\n"
9841                "  if (true)\n"
9842                "    {\n"
9843                "      a();\n"
9844                "    }\n"
9845                "  else if (false)\n"
9846                "    {\n"
9847                "      b();\n"
9848                "    }\n"
9849                "  else\n"
9850                "    {\n"
9851                "      c();\n"
9852                "    }\n"
9853                "}\n",
9854                GNUBraceStyle);
9855 
9856   verifyFormat("void f()\n"
9857                "{\n"
9858                "  for (int i = 0; i < 10; ++i)\n"
9859                "    {\n"
9860                "      a();\n"
9861                "    }\n"
9862                "  while (false)\n"
9863                "    {\n"
9864                "      b();\n"
9865                "    }\n"
9866                "  do\n"
9867                "    {\n"
9868                "      c();\n"
9869                "    }\n"
9870                "  while (false);\n"
9871                "}\n",
9872                GNUBraceStyle);
9873 
9874   verifyFormat("void f(int a)\n"
9875                "{\n"
9876                "  switch (a)\n"
9877                "    {\n"
9878                "    case 0:\n"
9879                "      break;\n"
9880                "    case 1:\n"
9881                "      {\n"
9882                "        break;\n"
9883                "      }\n"
9884                "    case 2:\n"
9885                "      {\n"
9886                "      }\n"
9887                "      break;\n"
9888                "    default:\n"
9889                "      break;\n"
9890                "    }\n"
9891                "}\n",
9892                GNUBraceStyle);
9893 
9894   verifyFormat("enum X\n"
9895                "{\n"
9896                "  Y = 0,\n"
9897                "}\n",
9898                GNUBraceStyle);
9899 
9900   verifyFormat("@interface BSApplicationController ()\n"
9901                "{\n"
9902                "@private\n"
9903                "  id _extraIvar;\n"
9904                "}\n"
9905                "@end\n",
9906                GNUBraceStyle);
9907 
9908   verifyFormat("#ifdef _DEBUG\n"
9909                "int foo(int i = 0)\n"
9910                "#else\n"
9911                "int foo(int i = 5)\n"
9912                "#endif\n"
9913                "{\n"
9914                "  return i;\n"
9915                "}",
9916                GNUBraceStyle);
9917 
9918   verifyFormat("void foo() {}\n"
9919                "void bar()\n"
9920                "#ifdef _DEBUG\n"
9921                "{\n"
9922                "  foo();\n"
9923                "}\n"
9924                "#else\n"
9925                "{\n"
9926                "}\n"
9927                "#endif",
9928                GNUBraceStyle);
9929 
9930   verifyFormat("void foobar() { int i = 5; }\n"
9931                "#ifdef _DEBUG\n"
9932                "void bar() {}\n"
9933                "#else\n"
9934                "void bar() { foobar(); }\n"
9935                "#endif",
9936                GNUBraceStyle);
9937 }
9938 
9939 TEST_F(FormatTest, WebKitBraceBreaking) {
9940   FormatStyle WebKitBraceStyle = getLLVMStyle();
9941   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9942   verifyFormat("namespace a {\n"
9943                "class A {\n"
9944                "  void f()\n"
9945                "  {\n"
9946                "    if (true) {\n"
9947                "      a();\n"
9948                "      b();\n"
9949                "    }\n"
9950                "  }\n"
9951                "  void g() { return; }\n"
9952                "};\n"
9953                "enum E {\n"
9954                "  A,\n"
9955                "  // foo\n"
9956                "  B,\n"
9957                "  C\n"
9958                "};\n"
9959                "struct B {\n"
9960                "  int x;\n"
9961                "};\n"
9962                "}\n",
9963                WebKitBraceStyle);
9964   verifyFormat("struct S {\n"
9965                "  int Type;\n"
9966                "  union {\n"
9967                "    int x;\n"
9968                "    double y;\n"
9969                "  } Value;\n"
9970                "  class C {\n"
9971                "    MyFavoriteType Value;\n"
9972                "  } Class;\n"
9973                "};\n",
9974                WebKitBraceStyle);
9975 }
9976 
9977 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9978   verifyFormat("void f() {\n"
9979                "  try {\n"
9980                "  } catch (const Exception &e) {\n"
9981                "  }\n"
9982                "}\n",
9983                getLLVMStyle());
9984 }
9985 
9986 TEST_F(FormatTest, UnderstandsPragmas) {
9987   verifyFormat("#pragma omp reduction(| : var)");
9988   verifyFormat("#pragma omp reduction(+ : var)");
9989 
9990   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9991             "(including parentheses).",
9992             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9993                    "(including parentheses)."));
9994 }
9995 
9996 TEST_F(FormatTest, UnderstandPragmaOption) {
9997   verifyFormat("#pragma option -C -A");
9998 
9999   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10000 }
10001 
10002 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10003   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10004   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10005                                   << " differs from Style #0"
10006 
10007 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10008   SmallVector<FormatStyle, 3> Styles;
10009   Styles.resize(3);
10010 
10011   Styles[0] = getLLVMStyle();
10012   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10013   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10014   EXPECT_ALL_STYLES_EQUAL(Styles);
10015 
10016   Styles[0] = getGoogleStyle();
10017   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10018   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10019   EXPECT_ALL_STYLES_EQUAL(Styles);
10020 
10021   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10022   EXPECT_TRUE(
10023       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10024   EXPECT_TRUE(
10025       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10026   EXPECT_ALL_STYLES_EQUAL(Styles);
10027 
10028   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10029   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10030   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10031   EXPECT_ALL_STYLES_EQUAL(Styles);
10032 
10033   Styles[0] = getMozillaStyle();
10034   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10035   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10036   EXPECT_ALL_STYLES_EQUAL(Styles);
10037 
10038   Styles[0] = getWebKitStyle();
10039   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10040   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10041   EXPECT_ALL_STYLES_EQUAL(Styles);
10042 
10043   Styles[0] = getGNUStyle();
10044   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10045   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10046   EXPECT_ALL_STYLES_EQUAL(Styles);
10047 
10048   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10049 }
10050 
10051 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10052   SmallVector<FormatStyle, 8> Styles;
10053   Styles.resize(2);
10054 
10055   Styles[0] = getGoogleStyle();
10056   Styles[1] = getLLVMStyle();
10057   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10058   EXPECT_ALL_STYLES_EQUAL(Styles);
10059 
10060   Styles.resize(5);
10061   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10062   Styles[1] = getLLVMStyle();
10063   Styles[1].Language = FormatStyle::LK_JavaScript;
10064   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10065 
10066   Styles[2] = getLLVMStyle();
10067   Styles[2].Language = FormatStyle::LK_JavaScript;
10068   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10069                                   "BasedOnStyle: Google",
10070                                   &Styles[2])
10071                    .value());
10072 
10073   Styles[3] = getLLVMStyle();
10074   Styles[3].Language = FormatStyle::LK_JavaScript;
10075   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10076                                   "Language: JavaScript",
10077                                   &Styles[3])
10078                    .value());
10079 
10080   Styles[4] = getLLVMStyle();
10081   Styles[4].Language = FormatStyle::LK_JavaScript;
10082   EXPECT_EQ(0, parseConfiguration("---\n"
10083                                   "BasedOnStyle: LLVM\n"
10084                                   "IndentWidth: 123\n"
10085                                   "---\n"
10086                                   "BasedOnStyle: Google\n"
10087                                   "Language: JavaScript",
10088                                   &Styles[4])
10089                    .value());
10090   EXPECT_ALL_STYLES_EQUAL(Styles);
10091 }
10092 
10093 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10094   Style.FIELD = false;                                                         \
10095   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10096   EXPECT_TRUE(Style.FIELD);                                                    \
10097   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10098   EXPECT_FALSE(Style.FIELD);
10099 
10100 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10101 
10102 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10103   Style.STRUCT.FIELD = false;                                                  \
10104   EXPECT_EQ(0,                                                                 \
10105             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10106                 .value());                                                     \
10107   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10108   EXPECT_EQ(0,                                                                 \
10109             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10110                 .value());                                                     \
10111   EXPECT_FALSE(Style.STRUCT.FIELD);
10112 
10113 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10114   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10115 
10116 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10117   EXPECT_NE(VALUE, Style.FIELD);                                               \
10118   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10119   EXPECT_EQ(VALUE, Style.FIELD)
10120 
10121 TEST_F(FormatTest, ParsesConfigurationBools) {
10122   FormatStyle Style = {};
10123   Style.Language = FormatStyle::LK_Cpp;
10124   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
10125   CHECK_PARSE_BOOL(AlignOperands);
10126   CHECK_PARSE_BOOL(AlignTrailingComments);
10127   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10128   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10129   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10130   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10131   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10132   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10133   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10134   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10135   CHECK_PARSE_BOOL(BinPackArguments);
10136   CHECK_PARSE_BOOL(BinPackParameters);
10137   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10138   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10139   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
10140   CHECK_PARSE_BOOL(BreakStringLiterals);
10141   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10142   CHECK_PARSE_BOOL(DerivePointerAlignment);
10143   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10144   CHECK_PARSE_BOOL(DisableFormat);
10145   CHECK_PARSE_BOOL(IndentCaseLabels);
10146   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10147   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10148   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10149   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10150   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10151   CHECK_PARSE_BOOL(ReflowComments);
10152   CHECK_PARSE_BOOL(SortIncludes);
10153   CHECK_PARSE_BOOL(SpacesInParentheses);
10154   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10155   CHECK_PARSE_BOOL(SpacesInAngles);
10156   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10157   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10158   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10159   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10160   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10161 
10162   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10163   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10164   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10165   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10166   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10167   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10168   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10169   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10170   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10171   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10172   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10173 }
10174 
10175 #undef CHECK_PARSE_BOOL
10176 
10177 TEST_F(FormatTest, ParsesConfiguration) {
10178   FormatStyle Style = {};
10179   Style.Language = FormatStyle::LK_Cpp;
10180   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10181   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10182               ConstructorInitializerIndentWidth, 1234u);
10183   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10184   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10185   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10186   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10187               PenaltyBreakBeforeFirstCallParameter, 1234u);
10188   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10189   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10190               PenaltyReturnTypeOnItsOwnLine, 1234u);
10191   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10192               SpacesBeforeTrailingComments, 1234u);
10193   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10194   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10195   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10196 
10197   Style.PointerAlignment = FormatStyle::PAS_Middle;
10198   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10199               FormatStyle::PAS_Left);
10200   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10201               FormatStyle::PAS_Right);
10202   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10203               FormatStyle::PAS_Middle);
10204   // For backward compatibility:
10205   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10206               FormatStyle::PAS_Left);
10207   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10208               FormatStyle::PAS_Right);
10209   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10210               FormatStyle::PAS_Middle);
10211 
10212   Style.Standard = FormatStyle::LS_Auto;
10213   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10214   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10215   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10216   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10217   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10218 
10219   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10220   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10221               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10222   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10223               FormatStyle::BOS_None);
10224   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10225               FormatStyle::BOS_All);
10226   // For backward compatibility:
10227   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10228               FormatStyle::BOS_None);
10229   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10230               FormatStyle::BOS_All);
10231 
10232   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10233   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10234               FormatStyle::BAS_Align);
10235   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10236               FormatStyle::BAS_DontAlign);
10237   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10238               FormatStyle::BAS_AlwaysBreak);
10239   // For backward compatibility:
10240   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10241               FormatStyle::BAS_DontAlign);
10242   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10243               FormatStyle::BAS_Align);
10244 
10245   Style.UseTab = FormatStyle::UT_ForIndentation;
10246   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10247   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10248   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10249   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10250               FormatStyle::UT_ForContinuationAndIndentation);
10251   // For backward compatibility:
10252   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10253   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10254 
10255   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10256   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10257               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10258   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10259               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10260   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10261               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10262   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10263               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10264   // For backward compatibility:
10265   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10266               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10267   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10268               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10269 
10270   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10271   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10272               FormatStyle::SBPO_Never);
10273   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10274               FormatStyle::SBPO_Always);
10275   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10276               FormatStyle::SBPO_ControlStatements);
10277   // For backward compatibility:
10278   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10279               FormatStyle::SBPO_Never);
10280   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10281               FormatStyle::SBPO_ControlStatements);
10282 
10283   Style.ColumnLimit = 123;
10284   FormatStyle BaseStyle = getLLVMStyle();
10285   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10286   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10287 
10288   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10289   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10290               FormatStyle::BS_Attach);
10291   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10292               FormatStyle::BS_Linux);
10293   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10294               FormatStyle::BS_Mozilla);
10295   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10296               FormatStyle::BS_Stroustrup);
10297   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10298               FormatStyle::BS_Allman);
10299   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10300   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10301               FormatStyle::BS_WebKit);
10302   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10303               FormatStyle::BS_Custom);
10304 
10305   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10306   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10307               FormatStyle::RTBS_None);
10308   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10309               FormatStyle::RTBS_All);
10310   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10311               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10312   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10313               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10314   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10315               AlwaysBreakAfterReturnType,
10316               FormatStyle::RTBS_TopLevelDefinitions);
10317 
10318   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10319   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10320               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10321   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10322               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10323   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10324               AlwaysBreakAfterDefinitionReturnType,
10325               FormatStyle::DRTBS_TopLevel);
10326 
10327   Style.NamespaceIndentation = FormatStyle::NI_All;
10328   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10329               FormatStyle::NI_None);
10330   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10331               FormatStyle::NI_Inner);
10332   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10333               FormatStyle::NI_All);
10334 
10335   // FIXME: This is required because parsing a configuration simply overwrites
10336   // the first N elements of the list instead of resetting it.
10337   Style.ForEachMacros.clear();
10338   std::vector<std::string> BoostForeach;
10339   BoostForeach.push_back("BOOST_FOREACH");
10340   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10341   std::vector<std::string> BoostAndQForeach;
10342   BoostAndQForeach.push_back("BOOST_FOREACH");
10343   BoostAndQForeach.push_back("Q_FOREACH");
10344   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10345               BoostAndQForeach);
10346 
10347   Style.IncludeCategories.clear();
10348   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10349                                                                   {".*", 1}};
10350   CHECK_PARSE("IncludeCategories:\n"
10351               "  - Regex: abc/.*\n"
10352               "    Priority: 2\n"
10353               "  - Regex: .*\n"
10354               "    Priority: 1",
10355               IncludeCategories, ExpectedCategories);
10356   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10357 }
10358 
10359 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10360   FormatStyle Style = {};
10361   Style.Language = FormatStyle::LK_Cpp;
10362   CHECK_PARSE("Language: Cpp\n"
10363               "IndentWidth: 12",
10364               IndentWidth, 12u);
10365   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10366                                "IndentWidth: 34",
10367                                &Style),
10368             ParseError::Unsuitable);
10369   EXPECT_EQ(12u, Style.IndentWidth);
10370   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10371   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10372 
10373   Style.Language = FormatStyle::LK_JavaScript;
10374   CHECK_PARSE("Language: JavaScript\n"
10375               "IndentWidth: 12",
10376               IndentWidth, 12u);
10377   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10378   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10379                                "IndentWidth: 34",
10380                                &Style),
10381             ParseError::Unsuitable);
10382   EXPECT_EQ(23u, Style.IndentWidth);
10383   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10384   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10385 
10386   CHECK_PARSE("BasedOnStyle: LLVM\n"
10387               "IndentWidth: 67",
10388               IndentWidth, 67u);
10389 
10390   CHECK_PARSE("---\n"
10391               "Language: JavaScript\n"
10392               "IndentWidth: 12\n"
10393               "---\n"
10394               "Language: Cpp\n"
10395               "IndentWidth: 34\n"
10396               "...\n",
10397               IndentWidth, 12u);
10398 
10399   Style.Language = FormatStyle::LK_Cpp;
10400   CHECK_PARSE("---\n"
10401               "Language: JavaScript\n"
10402               "IndentWidth: 12\n"
10403               "---\n"
10404               "Language: Cpp\n"
10405               "IndentWidth: 34\n"
10406               "...\n",
10407               IndentWidth, 34u);
10408   CHECK_PARSE("---\n"
10409               "IndentWidth: 78\n"
10410               "---\n"
10411               "Language: JavaScript\n"
10412               "IndentWidth: 56\n"
10413               "...\n",
10414               IndentWidth, 78u);
10415 
10416   Style.ColumnLimit = 123;
10417   Style.IndentWidth = 234;
10418   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10419   Style.TabWidth = 345;
10420   EXPECT_FALSE(parseConfiguration("---\n"
10421                                   "IndentWidth: 456\n"
10422                                   "BreakBeforeBraces: Allman\n"
10423                                   "---\n"
10424                                   "Language: JavaScript\n"
10425                                   "IndentWidth: 111\n"
10426                                   "TabWidth: 111\n"
10427                                   "---\n"
10428                                   "Language: Cpp\n"
10429                                   "BreakBeforeBraces: Stroustrup\n"
10430                                   "TabWidth: 789\n"
10431                                   "...\n",
10432                                   &Style));
10433   EXPECT_EQ(123u, Style.ColumnLimit);
10434   EXPECT_EQ(456u, Style.IndentWidth);
10435   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10436   EXPECT_EQ(789u, Style.TabWidth);
10437 
10438   EXPECT_EQ(parseConfiguration("---\n"
10439                                "Language: JavaScript\n"
10440                                "IndentWidth: 56\n"
10441                                "---\n"
10442                                "IndentWidth: 78\n"
10443                                "...\n",
10444                                &Style),
10445             ParseError::Error);
10446   EXPECT_EQ(parseConfiguration("---\n"
10447                                "Language: JavaScript\n"
10448                                "IndentWidth: 56\n"
10449                                "---\n"
10450                                "Language: JavaScript\n"
10451                                "IndentWidth: 78\n"
10452                                "...\n",
10453                                &Style),
10454             ParseError::Error);
10455 
10456   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10457 }
10458 
10459 #undef CHECK_PARSE
10460 
10461 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10462   FormatStyle Style = {};
10463   Style.Language = FormatStyle::LK_JavaScript;
10464   Style.BreakBeforeTernaryOperators = true;
10465   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10466   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10467 
10468   Style.BreakBeforeTernaryOperators = true;
10469   EXPECT_EQ(0, parseConfiguration("---\n"
10470                                   "BasedOnStyle: Google\n"
10471                                   "---\n"
10472                                   "Language: JavaScript\n"
10473                                   "IndentWidth: 76\n"
10474                                   "...\n",
10475                                   &Style)
10476                    .value());
10477   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10478   EXPECT_EQ(76u, Style.IndentWidth);
10479   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10480 }
10481 
10482 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10483   FormatStyle Style = getLLVMStyle();
10484   std::string YAML = configurationAsText(Style);
10485   FormatStyle ParsedStyle = {};
10486   ParsedStyle.Language = FormatStyle::LK_Cpp;
10487   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10488   EXPECT_EQ(Style, ParsedStyle);
10489 }
10490 
10491 TEST_F(FormatTest, WorksFor8bitEncodings) {
10492   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10493             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10494             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10495             "\"\xef\xee\xf0\xf3...\"",
10496             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10497                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10498                    "\xef\xee\xf0\xf3...\"",
10499                    getLLVMStyleWithColumns(12)));
10500 }
10501 
10502 TEST_F(FormatTest, HandlesUTF8BOM) {
10503   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10504   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10505             format("\xef\xbb\xbf#include <iostream>"));
10506   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10507             format("\xef\xbb\xbf\n#include <iostream>"));
10508 }
10509 
10510 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10511 #if !defined(_MSC_VER)
10512 
10513 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10514   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10515                getLLVMStyleWithColumns(35));
10516   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10517                getLLVMStyleWithColumns(31));
10518   verifyFormat("// Однажды в студёную зимнюю пору...",
10519                getLLVMStyleWithColumns(36));
10520   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10521   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10522                getLLVMStyleWithColumns(39));
10523   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10524                getLLVMStyleWithColumns(35));
10525 }
10526 
10527 TEST_F(FormatTest, SplitsUTF8Strings) {
10528   // Non-printable characters' width is currently considered to be the length in
10529   // bytes in UTF8. The characters can be displayed in very different manner
10530   // (zero-width, single width with a substitution glyph, expanded to their code
10531   // (e.g. "<8d>"), so there's no single correct way to handle them.
10532   EXPECT_EQ("\"aaaaÄ\"\n"
10533             "\"\xc2\x8d\";",
10534             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10535   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10536             "\"\xc2\x8d\";",
10537             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10538   EXPECT_EQ("\"Однажды, в \"\n"
10539             "\"студёную \"\n"
10540             "\"зимнюю \"\n"
10541             "\"пору,\"",
10542             format("\"Однажды, в студёную зимнюю пору,\"",
10543                    getLLVMStyleWithColumns(13)));
10544   EXPECT_EQ(
10545       "\"一 二 三 \"\n"
10546       "\"四 五六 \"\n"
10547       "\"七 八 九 \"\n"
10548       "\"十\"",
10549       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10550   EXPECT_EQ("\"一\t二 \"\n"
10551             "\"\t三 \"\n"
10552             "\"四 五\t六 \"\n"
10553             "\"\t七 \"\n"
10554             "\"八九十\tqq\"",
10555             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10556                    getLLVMStyleWithColumns(11)));
10557 
10558   // UTF8 character in an escape sequence.
10559   EXPECT_EQ("\"aaaaaa\"\n"
10560             "\"\\\xC2\x8D\"",
10561             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10562 }
10563 
10564 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10565   EXPECT_EQ("const char *sssss =\n"
10566             "    \"一二三四五六七八\\\n"
10567             " 九 十\";",
10568             format("const char *sssss = \"一二三四五六七八\\\n"
10569                    " 九 十\";",
10570                    getLLVMStyleWithColumns(30)));
10571 }
10572 
10573 TEST_F(FormatTest, SplitsUTF8LineComments) {
10574   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10575             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10576   EXPECT_EQ("// Я из лесу\n"
10577             "// вышел; был\n"
10578             "// сильный\n"
10579             "// мороз.",
10580             format("// Я из лесу вышел; был сильный мороз.",
10581                    getLLVMStyleWithColumns(13)));
10582   EXPECT_EQ("// 一二三\n"
10583             "// 四五六七\n"
10584             "// 八  九\n"
10585             "// 十",
10586             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10587 }
10588 
10589 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10590   EXPECT_EQ("/* Гляжу,\n"
10591             " * поднимается\n"
10592             " * медленно в\n"
10593             " * гору\n"
10594             " * Лошадка,\n"
10595             " * везущая\n"
10596             " * хворосту\n"
10597             " * воз. */",
10598             format("/* Гляжу, поднимается медленно в гору\n"
10599                    " * Лошадка, везущая хворосту воз. */",
10600                    getLLVMStyleWithColumns(13)));
10601   EXPECT_EQ(
10602       "/* 一二三\n"
10603       " * 四五六七\n"
10604       " * 八  九\n"
10605       " * 十  */",
10606       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10607   EXPECT_EQ("/* �������� ��������\n"
10608             " * ��������\n"
10609             " * ������-�� */",
10610             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10611 }
10612 
10613 #endif // _MSC_VER
10614 
10615 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10616   FormatStyle Style = getLLVMStyle();
10617 
10618   Style.ConstructorInitializerIndentWidth = 4;
10619   verifyFormat(
10620       "SomeClass::Constructor()\n"
10621       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10622       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10623       Style);
10624 
10625   Style.ConstructorInitializerIndentWidth = 2;
10626   verifyFormat(
10627       "SomeClass::Constructor()\n"
10628       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10629       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10630       Style);
10631 
10632   Style.ConstructorInitializerIndentWidth = 0;
10633   verifyFormat(
10634       "SomeClass::Constructor()\n"
10635       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10636       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10637       Style);
10638   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10639   verifyFormat(
10640       "SomeLongTemplateVariableName<\n"
10641       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10642       Style);
10643   verifyFormat(
10644       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10645       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10646       Style);
10647 }
10648 
10649 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10650   FormatStyle Style = getLLVMStyle();
10651   Style.BreakConstructorInitializersBeforeComma = true;
10652   Style.ConstructorInitializerIndentWidth = 4;
10653   verifyFormat("SomeClass::Constructor()\n"
10654                "    : a(a)\n"
10655                "    , b(b)\n"
10656                "    , c(c) {}",
10657                Style);
10658   verifyFormat("SomeClass::Constructor()\n"
10659                "    : a(a) {}",
10660                Style);
10661 
10662   Style.ColumnLimit = 0;
10663   verifyFormat("SomeClass::Constructor()\n"
10664                "    : a(a) {}",
10665                Style);
10666   verifyFormat("SomeClass::Constructor() noexcept\n"
10667                "    : a(a) {}",
10668                Style);
10669   verifyFormat("SomeClass::Constructor()\n"
10670                "    : a(a)\n"
10671                "    , b(b)\n"
10672                "    , c(c) {}",
10673                Style);
10674   verifyFormat("SomeClass::Constructor()\n"
10675                "    : a(a) {\n"
10676                "  foo();\n"
10677                "  bar();\n"
10678                "}",
10679                Style);
10680 
10681   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10682   verifyFormat("SomeClass::Constructor()\n"
10683                "    : a(a)\n"
10684                "    , b(b)\n"
10685                "    , c(c) {\n}",
10686                Style);
10687   verifyFormat("SomeClass::Constructor()\n"
10688                "    : a(a) {\n}",
10689                Style);
10690 
10691   Style.ColumnLimit = 80;
10692   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10693   Style.ConstructorInitializerIndentWidth = 2;
10694   verifyFormat("SomeClass::Constructor()\n"
10695                "  : a(a)\n"
10696                "  , b(b)\n"
10697                "  , c(c) {}",
10698                Style);
10699 
10700   Style.ConstructorInitializerIndentWidth = 0;
10701   verifyFormat("SomeClass::Constructor()\n"
10702                ": a(a)\n"
10703                ", b(b)\n"
10704                ", c(c) {}",
10705                Style);
10706 
10707   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10708   Style.ConstructorInitializerIndentWidth = 4;
10709   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10710   verifyFormat(
10711       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10712       Style);
10713   verifyFormat(
10714       "SomeClass::Constructor()\n"
10715       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10716       Style);
10717   Style.ConstructorInitializerIndentWidth = 4;
10718   Style.ColumnLimit = 60;
10719   verifyFormat("SomeClass::Constructor()\n"
10720                "    : aaaaaaaa(aaaaaaaa)\n"
10721                "    , aaaaaaaa(aaaaaaaa)\n"
10722                "    , aaaaaaaa(aaaaaaaa) {}",
10723                Style);
10724 }
10725 
10726 TEST_F(FormatTest, Destructors) {
10727   verifyFormat("void F(int &i) { i.~int(); }");
10728   verifyFormat("void F(int &i) { i->~int(); }");
10729 }
10730 
10731 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10732   FormatStyle Style = getWebKitStyle();
10733 
10734   // Don't indent in outer namespaces.
10735   verifyFormat("namespace outer {\n"
10736                "int i;\n"
10737                "namespace inner {\n"
10738                "    int i;\n"
10739                "} // namespace inner\n"
10740                "} // namespace outer\n"
10741                "namespace other_outer {\n"
10742                "int i;\n"
10743                "}",
10744                Style);
10745 
10746   // Don't indent case labels.
10747   verifyFormat("switch (variable) {\n"
10748                "case 1:\n"
10749                "case 2:\n"
10750                "    doSomething();\n"
10751                "    break;\n"
10752                "default:\n"
10753                "    ++variable;\n"
10754                "}",
10755                Style);
10756 
10757   // Wrap before binary operators.
10758   EXPECT_EQ("void f()\n"
10759             "{\n"
10760             "    if (aaaaaaaaaaaaaaaa\n"
10761             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10762             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10763             "        return;\n"
10764             "}",
10765             format("void f() {\n"
10766                    "if (aaaaaaaaaaaaaaaa\n"
10767                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10768                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10769                    "return;\n"
10770                    "}",
10771                    Style));
10772 
10773   // Allow functions on a single line.
10774   verifyFormat("void f() { return; }", Style);
10775 
10776   // Constructor initializers are formatted one per line with the "," on the
10777   // new line.
10778   verifyFormat("Constructor()\n"
10779                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10780                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10781                "          aaaaaaaaaaaaaa)\n"
10782                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10783                "{\n"
10784                "}",
10785                Style);
10786   verifyFormat("SomeClass::Constructor()\n"
10787                "    : a(a)\n"
10788                "{\n"
10789                "}",
10790                Style);
10791   EXPECT_EQ("SomeClass::Constructor()\n"
10792             "    : a(a)\n"
10793             "{\n"
10794             "}",
10795             format("SomeClass::Constructor():a(a){}", Style));
10796   verifyFormat("SomeClass::Constructor()\n"
10797                "    : a(a)\n"
10798                "    , b(b)\n"
10799                "    , c(c)\n"
10800                "{\n"
10801                "}",
10802                Style);
10803   verifyFormat("SomeClass::Constructor()\n"
10804                "    : a(a)\n"
10805                "{\n"
10806                "    foo();\n"
10807                "    bar();\n"
10808                "}",
10809                Style);
10810 
10811   // Access specifiers should be aligned left.
10812   verifyFormat("class C {\n"
10813                "public:\n"
10814                "    int i;\n"
10815                "};",
10816                Style);
10817 
10818   // Do not align comments.
10819   verifyFormat("int a; // Do not\n"
10820                "double b; // align comments.",
10821                Style);
10822 
10823   // Do not align operands.
10824   EXPECT_EQ("ASSERT(aaaa\n"
10825             "    || bbbb);",
10826             format("ASSERT ( aaaa\n||bbbb);", Style));
10827 
10828   // Accept input's line breaks.
10829   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10830             "    || bbbbbbbbbbbbbbb) {\n"
10831             "    i++;\n"
10832             "}",
10833             format("if (aaaaaaaaaaaaaaa\n"
10834                    "|| bbbbbbbbbbbbbbb) { i++; }",
10835                    Style));
10836   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10837             "    i++;\n"
10838             "}",
10839             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10840 
10841   // Don't automatically break all macro definitions (llvm.org/PR17842).
10842   verifyFormat("#define aNumber 10", Style);
10843   // However, generally keep the line breaks that the user authored.
10844   EXPECT_EQ("#define aNumber \\\n"
10845             "    10",
10846             format("#define aNumber \\\n"
10847                    " 10",
10848                    Style));
10849 
10850   // Keep empty and one-element array literals on a single line.
10851   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10852             "                                  copyItems:YES];",
10853             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10854                    "copyItems:YES];",
10855                    Style));
10856   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10857             "                                  copyItems:YES];",
10858             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10859                    "             copyItems:YES];",
10860                    Style));
10861   // FIXME: This does not seem right, there should be more indentation before
10862   // the array literal's entries. Nested blocks have the same problem.
10863   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10864             "    @\"a\",\n"
10865             "    @\"a\"\n"
10866             "]\n"
10867             "                                  copyItems:YES];",
10868             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10869                    "     @\"a\",\n"
10870                    "     @\"a\"\n"
10871                    "     ]\n"
10872                    "       copyItems:YES];",
10873                    Style));
10874   EXPECT_EQ(
10875       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10876       "                                  copyItems:YES];",
10877       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10878              "   copyItems:YES];",
10879              Style));
10880 
10881   verifyFormat("[self.a b:c c:d];", Style);
10882   EXPECT_EQ("[self.a b:c\n"
10883             "        c:d];",
10884             format("[self.a b:c\n"
10885                    "c:d];",
10886                    Style));
10887 }
10888 
10889 TEST_F(FormatTest, FormatsLambdas) {
10890   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10891   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10892   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10893   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10894   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10895   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10896   verifyFormat("void f() {\n"
10897                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10898                "}\n");
10899   verifyFormat("void f() {\n"
10900                "  other(x.begin(), //\n"
10901                "        x.end(),   //\n"
10902                "        [&](int, int) { return 1; });\n"
10903                "}\n");
10904   verifyFormat("SomeFunction([]() { // A cool function...\n"
10905                "  return 43;\n"
10906                "});");
10907   EXPECT_EQ("SomeFunction([]() {\n"
10908             "#define A a\n"
10909             "  return 43;\n"
10910             "});",
10911             format("SomeFunction([](){\n"
10912                    "#define A a\n"
10913                    "return 43;\n"
10914                    "});"));
10915   verifyFormat("void f() {\n"
10916                "  SomeFunction([](decltype(x), A *a) {});\n"
10917                "}");
10918   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10919                "    [](const aaaaaaaaaa &a) { return a; });");
10920   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10921                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10922                "});");
10923   verifyFormat("Constructor()\n"
10924                "    : Field([] { // comment\n"
10925                "        int i;\n"
10926                "      }) {}");
10927   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10928                "  return some_parameter.size();\n"
10929                "};");
10930   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10931                "    [](const string &s) { return s; };");
10932   verifyFormat("int i = aaaaaa ? 1 //\n"
10933                "               : [] {\n"
10934                "                   return 2; //\n"
10935                "                 }();");
10936   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10937                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10938                "                  return x == 2; // force break\n"
10939                "                });");
10940   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10941                "    int iiiiiiiiiiii) {\n"
10942                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10943                "});",
10944                getLLVMStyleWithColumns(60));
10945   verifyFormat("SomeFunction({[&] {\n"
10946                "                // comment\n"
10947                "              },\n"
10948                "              [&] {\n"
10949                "                // comment\n"
10950                "              }});");
10951   verifyFormat("SomeFunction({[&] {\n"
10952                "  // comment\n"
10953                "}});");
10954   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10955                "                             [&]() { return true; },\n"
10956                "                         aaaaa aaaaaaaaa);");
10957 
10958   // Lambdas with return types.
10959   verifyFormat("int c = []() -> int { return 2; }();\n");
10960   verifyFormat("int c = []() -> int * { return 2; }();\n");
10961   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10962   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10963   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10964   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10965   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10966   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10967   verifyFormat("[a, a]() -> a<1> {};");
10968   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10969                "                   int j) -> int {\n"
10970                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10971                "};");
10972   verifyFormat(
10973       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10974       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10975       "      return aaaaaaaaaaaaaaaaa;\n"
10976       "    });",
10977       getLLVMStyleWithColumns(70));
10978 
10979   // Multiple lambdas in the same parentheses change indentation rules.
10980   verifyFormat("SomeFunction(\n"
10981                "    []() {\n"
10982                "      int i = 42;\n"
10983                "      return i;\n"
10984                "    },\n"
10985                "    []() {\n"
10986                "      int j = 43;\n"
10987                "      return j;\n"
10988                "    });");
10989 
10990   // More complex introducers.
10991   verifyFormat("return [i, args...] {};");
10992 
10993   // Not lambdas.
10994   verifyFormat("constexpr char hello[]{\"hello\"};");
10995   verifyFormat("double &operator[](int i) { return 0; }\n"
10996                "int i;");
10997   verifyFormat("std::unique_ptr<int[]> foo() {}");
10998   verifyFormat("int i = a[a][a]->f();");
10999   verifyFormat("int i = (*b)[a]->f();");
11000 
11001   // Other corner cases.
11002   verifyFormat("void f() {\n"
11003                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11004                "      );\n"
11005                "}");
11006 
11007   // Lambdas created through weird macros.
11008   verifyFormat("void f() {\n"
11009                "  MACRO((const AA &a) { return 1; });\n"
11010                "  MACRO((AA &a) { return 1; });\n"
11011                "}");
11012 
11013   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11014                "      doo_dah();\n"
11015                "      doo_dah();\n"
11016                "    })) {\n"
11017                "}");
11018   verifyFormat("auto lambda = []() {\n"
11019                "  int a = 2\n"
11020                "#if A\n"
11021                "          + 2\n"
11022                "#endif\n"
11023                "      ;\n"
11024                "};");
11025 }
11026 
11027 TEST_F(FormatTest, FormatsBlocks) {
11028   FormatStyle ShortBlocks = getLLVMStyle();
11029   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11030   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11031   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11032   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11033   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11034   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11035   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11036 
11037   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11038   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11039   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11040 
11041   verifyFormat("[operation setCompletionBlock:^{\n"
11042                "  [self onOperationDone];\n"
11043                "}];");
11044   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11045                "  [self onOperationDone];\n"
11046                "}]};");
11047   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11048                "  f();\n"
11049                "}];");
11050   verifyFormat("int a = [operation block:^int(int *i) {\n"
11051                "  return 1;\n"
11052                "}];");
11053   verifyFormat("[myObject doSomethingWith:arg1\n"
11054                "                      aaa:^int(int *a) {\n"
11055                "                        return 1;\n"
11056                "                      }\n"
11057                "                      bbb:f(a * bbbbbbbb)];");
11058 
11059   verifyFormat("[operation setCompletionBlock:^{\n"
11060                "  [self.delegate newDataAvailable];\n"
11061                "}];",
11062                getLLVMStyleWithColumns(60));
11063   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11064                "  NSString *path = [self sessionFilePath];\n"
11065                "  if (path) {\n"
11066                "    // ...\n"
11067                "  }\n"
11068                "});");
11069   verifyFormat("[[SessionService sharedService]\n"
11070                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11071                "      if (window) {\n"
11072                "        [self windowDidLoad:window];\n"
11073                "      } else {\n"
11074                "        [self errorLoadingWindow];\n"
11075                "      }\n"
11076                "    }];");
11077   verifyFormat("void (^largeBlock)(void) = ^{\n"
11078                "  // ...\n"
11079                "};\n",
11080                getLLVMStyleWithColumns(40));
11081   verifyFormat("[[SessionService sharedService]\n"
11082                "    loadWindowWithCompletionBlock: //\n"
11083                "        ^(SessionWindow *window) {\n"
11084                "          if (window) {\n"
11085                "            [self windowDidLoad:window];\n"
11086                "          } else {\n"
11087                "            [self errorLoadingWindow];\n"
11088                "          }\n"
11089                "        }];",
11090                getLLVMStyleWithColumns(60));
11091   verifyFormat("[myObject doSomethingWith:arg1\n"
11092                "    firstBlock:^(Foo *a) {\n"
11093                "      // ...\n"
11094                "      int i;\n"
11095                "    }\n"
11096                "    secondBlock:^(Bar *b) {\n"
11097                "      // ...\n"
11098                "      int i;\n"
11099                "    }\n"
11100                "    thirdBlock:^Foo(Bar *b) {\n"
11101                "      // ...\n"
11102                "      int i;\n"
11103                "    }];");
11104   verifyFormat("[myObject doSomethingWith:arg1\n"
11105                "               firstBlock:-1\n"
11106                "              secondBlock:^(Bar *b) {\n"
11107                "                // ...\n"
11108                "                int i;\n"
11109                "              }];");
11110 
11111   verifyFormat("f(^{\n"
11112                "  @autoreleasepool {\n"
11113                "    if (a) {\n"
11114                "      g();\n"
11115                "    }\n"
11116                "  }\n"
11117                "});");
11118   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11119 
11120   FormatStyle FourIndent = getLLVMStyle();
11121   FourIndent.ObjCBlockIndentWidth = 4;
11122   verifyFormat("[operation setCompletionBlock:^{\n"
11123                "    [self onOperationDone];\n"
11124                "}];",
11125                FourIndent);
11126 }
11127 
11128 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11129   FormatStyle ZeroColumn = getLLVMStyle();
11130   ZeroColumn.ColumnLimit = 0;
11131 
11132   verifyFormat("[[SessionService sharedService] "
11133                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11134                "  if (window) {\n"
11135                "    [self windowDidLoad:window];\n"
11136                "  } else {\n"
11137                "    [self errorLoadingWindow];\n"
11138                "  }\n"
11139                "}];",
11140                ZeroColumn);
11141   EXPECT_EQ("[[SessionService sharedService]\n"
11142             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11143             "      if (window) {\n"
11144             "        [self windowDidLoad:window];\n"
11145             "      } else {\n"
11146             "        [self errorLoadingWindow];\n"
11147             "      }\n"
11148             "    }];",
11149             format("[[SessionService sharedService]\n"
11150                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11151                    "                if (window) {\n"
11152                    "    [self windowDidLoad:window];\n"
11153                    "  } else {\n"
11154                    "    [self errorLoadingWindow];\n"
11155                    "  }\n"
11156                    "}];",
11157                    ZeroColumn));
11158   verifyFormat("[myObject doSomethingWith:arg1\n"
11159                "    firstBlock:^(Foo *a) {\n"
11160                "      // ...\n"
11161                "      int i;\n"
11162                "    }\n"
11163                "    secondBlock:^(Bar *b) {\n"
11164                "      // ...\n"
11165                "      int i;\n"
11166                "    }\n"
11167                "    thirdBlock:^Foo(Bar *b) {\n"
11168                "      // ...\n"
11169                "      int i;\n"
11170                "    }];",
11171                ZeroColumn);
11172   verifyFormat("f(^{\n"
11173                "  @autoreleasepool {\n"
11174                "    if (a) {\n"
11175                "      g();\n"
11176                "    }\n"
11177                "  }\n"
11178                "});",
11179                ZeroColumn);
11180   verifyFormat("void (^largeBlock)(void) = ^{\n"
11181                "  // ...\n"
11182                "};",
11183                ZeroColumn);
11184 
11185   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11186   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11187             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11188   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11189   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11190             "  int i;\n"
11191             "};",
11192             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11193 }
11194 
11195 TEST_F(FormatTest, SupportsCRLF) {
11196   EXPECT_EQ("int a;\r\n"
11197             "int b;\r\n"
11198             "int c;\r\n",
11199             format("int a;\r\n"
11200                    "  int b;\r\n"
11201                    "    int c;\r\n",
11202                    getLLVMStyle()));
11203   EXPECT_EQ("int a;\r\n"
11204             "int b;\r\n"
11205             "int c;\r\n",
11206             format("int a;\r\n"
11207                    "  int b;\n"
11208                    "    int c;\r\n",
11209                    getLLVMStyle()));
11210   EXPECT_EQ("int a;\n"
11211             "int b;\n"
11212             "int c;\n",
11213             format("int a;\r\n"
11214                    "  int b;\n"
11215                    "    int c;\n",
11216                    getLLVMStyle()));
11217   EXPECT_EQ("\"aaaaaaa \"\r\n"
11218             "\"bbbbbbb\";\r\n",
11219             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11220   EXPECT_EQ("#define A \\\r\n"
11221             "  b;      \\\r\n"
11222             "  c;      \\\r\n"
11223             "  d;\r\n",
11224             format("#define A \\\r\n"
11225                    "  b; \\\r\n"
11226                    "  c; d; \r\n",
11227                    getGoogleStyle()));
11228 
11229   EXPECT_EQ("/*\r\n"
11230             "multi line block comments\r\n"
11231             "should not introduce\r\n"
11232             "an extra carriage return\r\n"
11233             "*/\r\n",
11234             format("/*\r\n"
11235                    "multi line block comments\r\n"
11236                    "should not introduce\r\n"
11237                    "an extra carriage return\r\n"
11238                    "*/\r\n"));
11239 }
11240 
11241 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11242   verifyFormat("MY_CLASS(C) {\n"
11243                "  int i;\n"
11244                "  int j;\n"
11245                "};");
11246 }
11247 
11248 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11249   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11250   TwoIndent.ContinuationIndentWidth = 2;
11251 
11252   EXPECT_EQ("int i =\n"
11253             "  longFunction(\n"
11254             "    arg);",
11255             format("int i = longFunction(arg);", TwoIndent));
11256 
11257   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11258   SixIndent.ContinuationIndentWidth = 6;
11259 
11260   EXPECT_EQ("int i =\n"
11261             "      longFunction(\n"
11262             "            arg);",
11263             format("int i = longFunction(arg);", SixIndent));
11264 }
11265 
11266 TEST_F(FormatTest, SpacesInAngles) {
11267   FormatStyle Spaces = getLLVMStyle();
11268   Spaces.SpacesInAngles = true;
11269 
11270   verifyFormat("static_cast< int >(arg);", Spaces);
11271   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11272   verifyFormat("f< int, float >();", Spaces);
11273   verifyFormat("template <> g() {}", Spaces);
11274   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11275   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11276   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11277                Spaces);
11278 
11279   Spaces.Standard = FormatStyle::LS_Cpp03;
11280   Spaces.SpacesInAngles = true;
11281   verifyFormat("A< A< int > >();", Spaces);
11282 
11283   Spaces.SpacesInAngles = false;
11284   verifyFormat("A<A<int> >();", Spaces);
11285 
11286   Spaces.Standard = FormatStyle::LS_Cpp11;
11287   Spaces.SpacesInAngles = true;
11288   verifyFormat("A< A< int > >();", Spaces);
11289 
11290   Spaces.SpacesInAngles = false;
11291   verifyFormat("A<A<int>>();", Spaces);
11292 }
11293 
11294 TEST_F(FormatTest, TripleAngleBrackets) {
11295   verifyFormat("f<<<1, 1>>>();");
11296   verifyFormat("f<<<1, 1, 1, s>>>();");
11297   verifyFormat("f<<<a, b, c, d>>>();");
11298   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11299   verifyFormat("f<param><<<1, 1>>>();");
11300   verifyFormat("f<1><<<1, 1>>>();");
11301   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11302   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11303                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11304 }
11305 
11306 TEST_F(FormatTest, MergeLessLessAtEnd) {
11307   verifyFormat("<<");
11308   EXPECT_EQ("< < <", format("\\\n<<<"));
11309   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11310                "aaallvm::outs() <<");
11311   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11312                "aaaallvm::outs()\n    <<");
11313 }
11314 
11315 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11316   std::string code = "#if A\n"
11317                      "#if B\n"
11318                      "a.\n"
11319                      "#endif\n"
11320                      "    a = 1;\n"
11321                      "#else\n"
11322                      "#endif\n"
11323                      "#if C\n"
11324                      "#else\n"
11325                      "#endif\n";
11326   EXPECT_EQ(code, format(code));
11327 }
11328 
11329 TEST_F(FormatTest, HandleConflictMarkers) {
11330   // Git/SVN conflict markers.
11331   EXPECT_EQ("int a;\n"
11332             "void f() {\n"
11333             "  callme(some(parameter1,\n"
11334             "<<<<<<< text by the vcs\n"
11335             "              parameter2),\n"
11336             "||||||| text by the vcs\n"
11337             "              parameter2),\n"
11338             "         parameter3,\n"
11339             "======= text by the vcs\n"
11340             "              parameter2, parameter3),\n"
11341             ">>>>>>> text by the vcs\n"
11342             "         otherparameter);\n",
11343             format("int a;\n"
11344                    "void f() {\n"
11345                    "  callme(some(parameter1,\n"
11346                    "<<<<<<< text by the vcs\n"
11347                    "  parameter2),\n"
11348                    "||||||| text by the vcs\n"
11349                    "  parameter2),\n"
11350                    "  parameter3,\n"
11351                    "======= text by the vcs\n"
11352                    "  parameter2,\n"
11353                    "  parameter3),\n"
11354                    ">>>>>>> text by the vcs\n"
11355                    "  otherparameter);\n"));
11356 
11357   // Perforce markers.
11358   EXPECT_EQ("void f() {\n"
11359             "  function(\n"
11360             ">>>> text by the vcs\n"
11361             "      parameter,\n"
11362             "==== text by the vcs\n"
11363             "      parameter,\n"
11364             "==== text by the vcs\n"
11365             "      parameter,\n"
11366             "<<<< text by the vcs\n"
11367             "      parameter);\n",
11368             format("void f() {\n"
11369                    "  function(\n"
11370                    ">>>> text by the vcs\n"
11371                    "  parameter,\n"
11372                    "==== text by the vcs\n"
11373                    "  parameter,\n"
11374                    "==== text by the vcs\n"
11375                    "  parameter,\n"
11376                    "<<<< text by the vcs\n"
11377                    "  parameter);\n"));
11378 
11379   EXPECT_EQ("<<<<<<<\n"
11380             "|||||||\n"
11381             "=======\n"
11382             ">>>>>>>",
11383             format("<<<<<<<\n"
11384                    "|||||||\n"
11385                    "=======\n"
11386                    ">>>>>>>"));
11387 
11388   EXPECT_EQ("<<<<<<<\n"
11389             "|||||||\n"
11390             "int i;\n"
11391             "=======\n"
11392             ">>>>>>>",
11393             format("<<<<<<<\n"
11394                    "|||||||\n"
11395                    "int i;\n"
11396                    "=======\n"
11397                    ">>>>>>>"));
11398 
11399   // FIXME: Handle parsing of macros around conflict markers correctly:
11400   EXPECT_EQ("#define Macro \\\n"
11401             "<<<<<<<\n"
11402             "Something \\\n"
11403             "|||||||\n"
11404             "Else \\\n"
11405             "=======\n"
11406             "Other \\\n"
11407             ">>>>>>>\n"
11408             "    End int i;\n",
11409             format("#define Macro \\\n"
11410                    "<<<<<<<\n"
11411                    "  Something \\\n"
11412                    "|||||||\n"
11413                    "  Else \\\n"
11414                    "=======\n"
11415                    "  Other \\\n"
11416                    ">>>>>>>\n"
11417                    "  End\n"
11418                    "int i;\n"));
11419 }
11420 
11421 TEST_F(FormatTest, DisableRegions) {
11422   EXPECT_EQ("int i;\n"
11423             "// clang-format off\n"
11424             "  int j;\n"
11425             "// clang-format on\n"
11426             "int k;",
11427             format(" int  i;\n"
11428                    "   // clang-format off\n"
11429                    "  int j;\n"
11430                    " // clang-format on\n"
11431                    "   int   k;"));
11432   EXPECT_EQ("int i;\n"
11433             "/* clang-format off */\n"
11434             "  int j;\n"
11435             "/* clang-format on */\n"
11436             "int k;",
11437             format(" int  i;\n"
11438                    "   /* clang-format off */\n"
11439                    "  int j;\n"
11440                    " /* clang-format on */\n"
11441                    "   int   k;"));
11442 }
11443 
11444 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11445   format("? ) =");
11446   verifyNoCrash("#define a\\\n /**/}");
11447 }
11448 
11449 TEST_F(FormatTest, FormatsTableGenCode) {
11450   FormatStyle Style = getLLVMStyle();
11451   Style.Language = FormatStyle::LK_TableGen;
11452   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11453 }
11454 
11455 // Since this test case uses UNIX-style file path. We disable it for MS
11456 // compiler.
11457 #if !defined(_MSC_VER) && !defined(__MINGW32__)
11458 
11459 TEST(FormatStyle, GetStyleOfFile) {
11460   vfs::InMemoryFileSystem FS;
11461   // Test 1: format file in the same directory.
11462   ASSERT_TRUE(
11463       FS.addFile("/a/.clang-format", 0,
11464                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11465   ASSERT_TRUE(
11466       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11467   auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
11468   ASSERT_EQ(Style1, getLLVMStyle());
11469 
11470   // Test 2: fallback to default.
11471   ASSERT_TRUE(
11472       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11473   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
11474   ASSERT_EQ(Style2, getMozillaStyle());
11475 
11476   // Test 3: format file in parent directory.
11477   ASSERT_TRUE(
11478       FS.addFile("/c/.clang-format", 0,
11479                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11480   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11481                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11482   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
11483   ASSERT_EQ(Style3, getGoogleStyle());
11484 }
11485 
11486 #endif // _MSC_VER
11487 
11488 class ReplacementTest : public ::testing::Test {
11489 protected:
11490   tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
11491                                          llvm::StringRef ReplacementText) {
11492     return tooling::Replacement(Context.Sources, Start, Length,
11493                                 ReplacementText);
11494   }
11495 
11496   RewriterTestContext Context;
11497 };
11498 
11499 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11500   // Column limit is 20.
11501   std::string Code = "Type *a =\n"
11502                      "    new Type();\n"
11503                      "g(iiiii, 0, jjjjj,\n"
11504                      "  0, kkkkk, 0, mm);\n"
11505                      "int  bad     = format   ;";
11506   std::string Expected = "auto a = new Type();\n"
11507                          "g(iiiii, nullptr,\n"
11508                          "  jjjjj, nullptr,\n"
11509                          "  kkkkk, nullptr,\n"
11510                          "  mm);\n"
11511                          "int  bad     = format   ;";
11512   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11513   tooling::Replacements Replaces;
11514   Replaces.insert(tooling::Replacement(
11515       Context.Sources, Context.getLocation(ID, 1, 1), 6, "auto "));
11516   Replaces.insert(tooling::Replacement(
11517       Context.Sources, Context.getLocation(ID, 3, 10), 1, "nullptr"));
11518   Replaces.insert(tooling::Replacement(
11519       Context.Sources, Context.getLocation(ID, 4, 3), 1, "nullptr"));
11520   Replaces.insert(tooling::Replacement(
11521       Context.Sources, Context.getLocation(ID, 4, 13), 1, "nullptr"));
11522 
11523   format::FormatStyle Style = format::getLLVMStyle();
11524   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11525   EXPECT_EQ(Expected, applyAllReplacements(
11526                           Code, formatReplacements(Code, Replaces, Style)));
11527 }
11528 
11529 TEST_F(ReplacementTest, FixOnlyAffectedCodeAfterReplacements) {
11530   std::string Code = "namespace A {\n"
11531                      "namespace B {\n"
11532                      "  int x;\n"
11533                      "} // namespace B\n"
11534                      "} // namespace A\n"
11535                      "\n"
11536                      "namespace C {\n"
11537                      "namespace D { int i; }\n"
11538                      "inline namespace E { namespace { int y; } }\n"
11539                      "int x=     0;"
11540                      "}";
11541   std::string Expected = "\n\nnamespace C {\n"
11542                          "namespace D { int i; }\n\n"
11543                          "int x=     0;"
11544                          "}";
11545   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11546   tooling::Replacements Replaces;
11547   Replaces.insert(tooling::Replacement(
11548       Context.Sources, Context.getLocation(ID, 3, 3), 6, ""));
11549   Replaces.insert(tooling::Replacement(
11550       Context.Sources, Context.getLocation(ID, 9, 34), 6, ""));
11551 
11552   format::FormatStyle Style = format::getLLVMStyle();
11553   auto FinalReplaces = formatReplacements(
11554       Code, cleanupAroundReplacements(Code, Replaces, Style), Style);
11555   EXPECT_EQ(Expected, applyAllReplacements(Code, FinalReplaces));
11556 }
11557 
11558 } // end namespace
11559 } // end namespace format
11560 } // end namespace clang
11561