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/ReplacementTest.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 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum IncompleteCheck {
34     IC_ExpectComplete,
35     IC_ExpectIncomplete,
36     IC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     bool IncompleteFormat = false;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
48     if (CheckIncomplete != IC_DoNotCheck) {
49       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
50       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
51     }
52     ReplacementCount = Replaces.size();
53     auto Result = applyAllReplacements(Code, Replaces);
54     EXPECT_TRUE(static_cast<bool>(Result));
55     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
56     return *Result;
57   }
58 
59   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60     FormatStyle Style = getLLVMStyle();
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
66     FormatStyle Style = getGoogleStyle();
67     Style.ColumnLimit = ColumnLimit;
68     return Style;
69   }
70 
71   void verifyFormat(llvm::StringRef Code,
72                     const FormatStyle &Style = getLLVMStyle()) {
73     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
74   }
75 
76   void verifyIncompleteFormat(llvm::StringRef Code,
77                               const FormatStyle &Style = getLLVMStyle()) {
78     EXPECT_EQ(Code.str(),
79               format(test::messUp(Code), Style, IC_ExpectIncomplete));
80   }
81 
82   void verifyGoogleFormat(llvm::StringRef Code) {
83     verifyFormat(Code, getGoogleStyle());
84   }
85 
86   void verifyIndependentOfContext(llvm::StringRef text) {
87     verifyFormat(text);
88     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
89   }
90 
91   /// \brief Verify that clang-format does not crash on the given input.
92   void verifyNoCrash(llvm::StringRef Code,
93                      const FormatStyle &Style = getLLVMStyle()) {
94     format(Code, Style, IC_DoNotCheck);
95   }
96 
97   int ReplacementCount;
98 };
99 
100 TEST_F(FormatTest, MessUp) {
101   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
102   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
103   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
104   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
105   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
106 }
107 
108 //===----------------------------------------------------------------------===//
109 // Basic function tests.
110 //===----------------------------------------------------------------------===//
111 
112 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
113   EXPECT_EQ(";", format(";"));
114 }
115 
116 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
117   EXPECT_EQ("int i;", format("  int i;"));
118   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
119   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
120   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
121 }
122 
123 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
124   EXPECT_EQ("int i;", format("int\ni;"));
125 }
126 
127 TEST_F(FormatTest, FormatsNestedBlockStatements) {
128   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
129 }
130 
131 TEST_F(FormatTest, FormatsNestedCall) {
132   verifyFormat("Method(f1, f2(f3));");
133   verifyFormat("Method(f1(f2, f3()));");
134   verifyFormat("Method(f1(f2, (f3())));");
135 }
136 
137 TEST_F(FormatTest, NestedNameSpecifiers) {
138   verifyFormat("vector<::Type> v;");
139   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
140   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
141   verifyFormat("bool a = 2 < ::SomeFunction();");
142 }
143 
144 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
145   EXPECT_EQ("if (a) {\n"
146             "  f();\n"
147             "}",
148             format("if(a){f();}"));
149   EXPECT_EQ(4, ReplacementCount);
150   EXPECT_EQ("if (a) {\n"
151             "  f();\n"
152             "}",
153             format("if (a) {\n"
154                    "  f();\n"
155                    "}"));
156   EXPECT_EQ(0, ReplacementCount);
157   EXPECT_EQ("/*\r\n"
158             "\r\n"
159             "*/\r\n",
160             format("/*\r\n"
161                    "\r\n"
162                    "*/\r\n"));
163   EXPECT_EQ(0, ReplacementCount);
164 }
165 
166 TEST_F(FormatTest, RemovesEmptyLines) {
167   EXPECT_EQ("class C {\n"
168             "  int i;\n"
169             "};",
170             format("class C {\n"
171                    " int i;\n"
172                    "\n"
173                    "};"));
174 
175   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
176   EXPECT_EQ("namespace N {\n"
177             "\n"
178             "int i;\n"
179             "}",
180             format("namespace N {\n"
181                    "\n"
182                    "int    i;\n"
183                    "}",
184                    getGoogleStyle()));
185   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
186             "\n"
187             "int i;\n"
188             "}",
189             format("extern /**/ \"C\" /**/ {\n"
190                    "\n"
191                    "int    i;\n"
192                    "}",
193                    getGoogleStyle()));
194 
195   // ...but do keep inlining and removing empty lines for non-block extern "C"
196   // functions.
197   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
198   EXPECT_EQ("extern \"C\" int f() {\n"
199             "  int i = 42;\n"
200             "  return i;\n"
201             "}",
202             format("extern \"C\" int f() {\n"
203                    "\n"
204                    "  int i = 42;\n"
205                    "  return i;\n"
206                    "}",
207                    getGoogleStyle()));
208 
209   // Remove empty lines at the beginning and end of blocks.
210   EXPECT_EQ("void f() {\n"
211             "\n"
212             "  if (a) {\n"
213             "\n"
214             "    f();\n"
215             "  }\n"
216             "}",
217             format("void f() {\n"
218                    "\n"
219                    "  if (a) {\n"
220                    "\n"
221                    "    f();\n"
222                    "\n"
223                    "  }\n"
224                    "\n"
225                    "}",
226                    getLLVMStyle()));
227   EXPECT_EQ("void f() {\n"
228             "  if (a) {\n"
229             "    f();\n"
230             "  }\n"
231             "}",
232             format("void f() {\n"
233                    "\n"
234                    "  if (a) {\n"
235                    "\n"
236                    "    f();\n"
237                    "\n"
238                    "  }\n"
239                    "\n"
240                    "}",
241                    getGoogleStyle()));
242 
243   // Don't remove empty lines in more complex control statements.
244   EXPECT_EQ("void f() {\n"
245             "  if (a) {\n"
246             "    f();\n"
247             "\n"
248             "  } else if (b) {\n"
249             "    f();\n"
250             "  }\n"
251             "}",
252             format("void f() {\n"
253                    "  if (a) {\n"
254                    "    f();\n"
255                    "\n"
256                    "  } else if (b) {\n"
257                    "    f();\n"
258                    "\n"
259                    "  }\n"
260                    "\n"
261                    "}"));
262 
263   // FIXME: This is slightly inconsistent.
264   EXPECT_EQ("namespace {\n"
265             "int i;\n"
266             "}",
267             format("namespace {\n"
268                    "int i;\n"
269                    "\n"
270                    "}"));
271   EXPECT_EQ("namespace {\n"
272             "int i;\n"
273             "\n"
274             "} // namespace",
275             format("namespace {\n"
276                    "int i;\n"
277                    "\n"
278                    "}  // namespace"));
279 
280   FormatStyle Style = getLLVMStyle();
281   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
282   Style.MaxEmptyLinesToKeep = 2;
283   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
284   Style.BraceWrapping.AfterClass = true;
285   Style.BraceWrapping.AfterFunction = true;
286   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
287 
288   EXPECT_EQ("class Foo\n"
289             "{\n"
290             "  Foo() {}\n"
291             "\n"
292             "  void funk() {}\n"
293             "};",
294             format("class Foo\n"
295                    "{\n"
296                    "  Foo()\n"
297                    "  {\n"
298                    "  }\n"
299                    "\n"
300                    "  void funk() {}\n"
301                    "};",
302                    Style));
303 }
304 
305 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
306   verifyFormat("x = (a) and (b);");
307   verifyFormat("x = (a) or (b);");
308   verifyFormat("x = (a) bitand (b);");
309   verifyFormat("x = (a) bitor (b);");
310   verifyFormat("x = (a) not_eq (b);");
311   verifyFormat("x = (a) and_eq (b);");
312   verifyFormat("x = (a) or_eq (b);");
313   verifyFormat("x = (a) xor (b);");
314 }
315 
316 //===----------------------------------------------------------------------===//
317 // Tests for control statements.
318 //===----------------------------------------------------------------------===//
319 
320 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
321   verifyFormat("if (true)\n  f();\ng();");
322   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
323   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
324 
325   FormatStyle AllowsMergedIf = getLLVMStyle();
326   AllowsMergedIf.AlignEscapedNewlinesLeft = true;
327   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
328   verifyFormat("if (a)\n"
329                "  // comment\n"
330                "  f();",
331                AllowsMergedIf);
332   verifyFormat("{\n"
333                "  if (a)\n"
334                "  label:\n"
335                "    f();\n"
336                "}",
337                AllowsMergedIf);
338   verifyFormat("#define A \\\n"
339                "  if (a)  \\\n"
340                "  label:  \\\n"
341                "    f()",
342                AllowsMergedIf);
343   verifyFormat("if (a)\n"
344                "  ;",
345                AllowsMergedIf);
346   verifyFormat("if (a)\n"
347                "  if (b) return;",
348                AllowsMergedIf);
349 
350   verifyFormat("if (a) // Can't merge this\n"
351                "  f();\n",
352                AllowsMergedIf);
353   verifyFormat("if (a) /* still don't merge */\n"
354                "  f();",
355                AllowsMergedIf);
356   verifyFormat("if (a) { // Never merge this\n"
357                "  f();\n"
358                "}",
359                AllowsMergedIf);
360   verifyFormat("if (a) { /* Never merge this */\n"
361                "  f();\n"
362                "}",
363                AllowsMergedIf);
364 
365   AllowsMergedIf.ColumnLimit = 14;
366   verifyFormat("if (a) return;", AllowsMergedIf);
367   verifyFormat("if (aaaaaaaaa)\n"
368                "  return;",
369                AllowsMergedIf);
370 
371   AllowsMergedIf.ColumnLimit = 13;
372   verifyFormat("if (a)\n  return;", AllowsMergedIf);
373 }
374 
375 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
376   FormatStyle AllowsMergedLoops = getLLVMStyle();
377   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
378   verifyFormat("while (true) continue;", AllowsMergedLoops);
379   verifyFormat("for (;;) continue;", AllowsMergedLoops);
380   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
381   verifyFormat("while (true)\n"
382                "  ;",
383                AllowsMergedLoops);
384   verifyFormat("for (;;)\n"
385                "  ;",
386                AllowsMergedLoops);
387   verifyFormat("for (;;)\n"
388                "  for (;;) continue;",
389                AllowsMergedLoops);
390   verifyFormat("for (;;) // Can't merge this\n"
391                "  continue;",
392                AllowsMergedLoops);
393   verifyFormat("for (;;) /* still don't merge */\n"
394                "  continue;",
395                AllowsMergedLoops);
396 }
397 
398 TEST_F(FormatTest, FormatShortBracedStatements) {
399   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
400   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
401 
402   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
403   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
404 
405   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
406   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
407   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
408   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
409   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
410   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
411   verifyFormat("if (true) { //\n"
412                "  f();\n"
413                "}",
414                AllowSimpleBracedStatements);
415   verifyFormat("if (true) {\n"
416                "  f();\n"
417                "  f();\n"
418                "}",
419                AllowSimpleBracedStatements);
420   verifyFormat("if (true) {\n"
421                "  f();\n"
422                "} else {\n"
423                "  f();\n"
424                "}",
425                AllowSimpleBracedStatements);
426 
427   verifyFormat("template <int> struct A2 {\n"
428                "  struct B {};\n"
429                "};",
430                AllowSimpleBracedStatements);
431 
432   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
433   verifyFormat("if (true) {\n"
434                "  f();\n"
435                "}",
436                AllowSimpleBracedStatements);
437   verifyFormat("if (true) {\n"
438                "  f();\n"
439                "} else {\n"
440                "  f();\n"
441                "}",
442                AllowSimpleBracedStatements);
443 
444   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
445   verifyFormat("while (true) {\n"
446                "  f();\n"
447                "}",
448                AllowSimpleBracedStatements);
449   verifyFormat("for (;;) {\n"
450                "  f();\n"
451                "}",
452                AllowSimpleBracedStatements);
453 }
454 
455 TEST_F(FormatTest, ParseIfElse) {
456   verifyFormat("if (true)\n"
457                "  if (true)\n"
458                "    if (true)\n"
459                "      f();\n"
460                "    else\n"
461                "      g();\n"
462                "  else\n"
463                "    h();\n"
464                "else\n"
465                "  i();");
466   verifyFormat("if (true)\n"
467                "  if (true)\n"
468                "    if (true) {\n"
469                "      if (true)\n"
470                "        f();\n"
471                "    } else {\n"
472                "      g();\n"
473                "    }\n"
474                "  else\n"
475                "    h();\n"
476                "else {\n"
477                "  i();\n"
478                "}");
479   verifyFormat("void f() {\n"
480                "  if (a) {\n"
481                "  } else {\n"
482                "  }\n"
483                "}");
484 }
485 
486 TEST_F(FormatTest, ElseIf) {
487   verifyFormat("if (a) {\n} else if (b) {\n}");
488   verifyFormat("if (a)\n"
489                "  f();\n"
490                "else if (b)\n"
491                "  g();\n"
492                "else\n"
493                "  h();");
494   verifyFormat("if (a) {\n"
495                "  f();\n"
496                "}\n"
497                "// or else ..\n"
498                "else {\n"
499                "  g()\n"
500                "}");
501 
502   verifyFormat("if (a) {\n"
503                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
504                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
505                "}");
506   verifyFormat("if (a) {\n"
507                "} else if (\n"
508                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
509                "}",
510                getLLVMStyleWithColumns(62));
511 }
512 
513 TEST_F(FormatTest, FormatsForLoop) {
514   verifyFormat(
515       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
516       "     ++VeryVeryLongLoopVariable)\n"
517       "  ;");
518   verifyFormat("for (;;)\n"
519                "  f();");
520   verifyFormat("for (;;) {\n}");
521   verifyFormat("for (;;) {\n"
522                "  f();\n"
523                "}");
524   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
525 
526   verifyFormat(
527       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
528       "                                          E = UnwrappedLines.end();\n"
529       "     I != E; ++I) {\n}");
530 
531   verifyFormat(
532       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
533       "     ++IIIII) {\n}");
534   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
535                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
536                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
537   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
538                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
539                "         E = FD->getDeclsInPrototypeScope().end();\n"
540                "     I != E; ++I) {\n}");
541   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
542                "         I = Container.begin(),\n"
543                "         E = Container.end();\n"
544                "     I != E; ++I) {\n}",
545                getLLVMStyleWithColumns(76));
546 
547   verifyFormat(
548       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
549       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
550       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
551       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
552       "     ++aaaaaaaaaaa) {\n}");
553   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
554                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
555                "     ++i) {\n}");
556   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
557                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
558                "}");
559   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
560                "         aaaaaaaaaa);\n"
561                "     iter; ++iter) {\n"
562                "}");
563   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
564                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
565                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
566                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
567 
568   FormatStyle NoBinPacking = getLLVMStyle();
569   NoBinPacking.BinPackParameters = false;
570   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
571                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
572                "                                           aaaaaaaaaaaaaaaa,\n"
573                "                                           aaaaaaaaaaaaaaaa,\n"
574                "                                           aaaaaaaaaaaaaaaa);\n"
575                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
576                "}",
577                NoBinPacking);
578   verifyFormat(
579       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
580       "                                          E = UnwrappedLines.end();\n"
581       "     I != E;\n"
582       "     ++I) {\n}",
583       NoBinPacking);
584 }
585 
586 TEST_F(FormatTest, RangeBasedForLoops) {
587   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
588                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
589   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
590                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
591   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
592                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
593   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
594                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
595 }
596 
597 TEST_F(FormatTest, ForEachLoops) {
598   verifyFormat("void f() {\n"
599                "  foreach (Item *item, itemlist) {}\n"
600                "  Q_FOREACH (Item *item, itemlist) {}\n"
601                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
602                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
603                "}");
604 
605   // As function-like macros.
606   verifyFormat("#define foreach(x, y)\n"
607                "#define Q_FOREACH(x, y)\n"
608                "#define BOOST_FOREACH(x, y)\n"
609                "#define UNKNOWN_FOREACH(x, y)\n");
610 
611   // Not as function-like macros.
612   verifyFormat("#define foreach (x, y)\n"
613                "#define Q_FOREACH (x, y)\n"
614                "#define BOOST_FOREACH (x, y)\n"
615                "#define UNKNOWN_FOREACH (x, y)\n");
616 }
617 
618 TEST_F(FormatTest, FormatsWhileLoop) {
619   verifyFormat("while (true) {\n}");
620   verifyFormat("while (true)\n"
621                "  f();");
622   verifyFormat("while () {\n}");
623   verifyFormat("while () {\n"
624                "  f();\n"
625                "}");
626 }
627 
628 TEST_F(FormatTest, FormatsDoWhile) {
629   verifyFormat("do {\n"
630                "  do_something();\n"
631                "} while (something());");
632   verifyFormat("do\n"
633                "  do_something();\n"
634                "while (something());");
635 }
636 
637 TEST_F(FormatTest, FormatsSwitchStatement) {
638   verifyFormat("switch (x) {\n"
639                "case 1:\n"
640                "  f();\n"
641                "  break;\n"
642                "case kFoo:\n"
643                "case ns::kBar:\n"
644                "case kBaz:\n"
645                "  break;\n"
646                "default:\n"
647                "  g();\n"
648                "  break;\n"
649                "}");
650   verifyFormat("switch (x) {\n"
651                "case 1: {\n"
652                "  f();\n"
653                "  break;\n"
654                "}\n"
655                "case 2: {\n"
656                "  break;\n"
657                "}\n"
658                "}");
659   verifyFormat("switch (x) {\n"
660                "case 1: {\n"
661                "  f();\n"
662                "  {\n"
663                "    g();\n"
664                "    h();\n"
665                "  }\n"
666                "  break;\n"
667                "}\n"
668                "}");
669   verifyFormat("switch (x) {\n"
670                "case 1: {\n"
671                "  f();\n"
672                "  if (foo) {\n"
673                "    g();\n"
674                "    h();\n"
675                "  }\n"
676                "  break;\n"
677                "}\n"
678                "}");
679   verifyFormat("switch (x) {\n"
680                "case 1: {\n"
681                "  f();\n"
682                "  g();\n"
683                "} break;\n"
684                "}");
685   verifyFormat("switch (test)\n"
686                "  ;");
687   verifyFormat("switch (x) {\n"
688                "default: {\n"
689                "  // Do nothing.\n"
690                "}\n"
691                "}");
692   verifyFormat("switch (x) {\n"
693                "// comment\n"
694                "// if 1, do f()\n"
695                "case 1:\n"
696                "  f();\n"
697                "}");
698   verifyFormat("switch (x) {\n"
699                "case 1:\n"
700                "  // Do amazing stuff\n"
701                "  {\n"
702                "    f();\n"
703                "    g();\n"
704                "  }\n"
705                "  break;\n"
706                "}");
707   verifyFormat("#define A          \\\n"
708                "  switch (x) {     \\\n"
709                "  case a:          \\\n"
710                "    foo = b;       \\\n"
711                "  }",
712                getLLVMStyleWithColumns(20));
713   verifyFormat("#define OPERATION_CASE(name)           \\\n"
714                "  case OP_name:                        \\\n"
715                "    return operations::Operation##name\n",
716                getLLVMStyleWithColumns(40));
717   verifyFormat("switch (x) {\n"
718                "case 1:;\n"
719                "default:;\n"
720                "  int i;\n"
721                "}");
722 
723   verifyGoogleFormat("switch (x) {\n"
724                      "  case 1:\n"
725                      "    f();\n"
726                      "    break;\n"
727                      "  case kFoo:\n"
728                      "  case ns::kBar:\n"
729                      "  case kBaz:\n"
730                      "    break;\n"
731                      "  default:\n"
732                      "    g();\n"
733                      "    break;\n"
734                      "}");
735   verifyGoogleFormat("switch (x) {\n"
736                      "  case 1: {\n"
737                      "    f();\n"
738                      "    break;\n"
739                      "  }\n"
740                      "}");
741   verifyGoogleFormat("switch (test)\n"
742                      "  ;");
743 
744   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
745                      "  case OP_name:              \\\n"
746                      "    return operations::Operation##name\n");
747   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
748                      "  // Get the correction operation class.\n"
749                      "  switch (OpCode) {\n"
750                      "    CASE(Add);\n"
751                      "    CASE(Subtract);\n"
752                      "    default:\n"
753                      "      return operations::Unknown;\n"
754                      "  }\n"
755                      "#undef OPERATION_CASE\n"
756                      "}");
757   verifyFormat("DEBUG({\n"
758                "  switch (x) {\n"
759                "  case A:\n"
760                "    f();\n"
761                "    break;\n"
762                "  // On B:\n"
763                "  case B:\n"
764                "    g();\n"
765                "    break;\n"
766                "  }\n"
767                "});");
768   verifyFormat("switch (a) {\n"
769                "case (b):\n"
770                "  return;\n"
771                "}");
772 
773   verifyFormat("switch (a) {\n"
774                "case some_namespace::\n"
775                "    some_constant:\n"
776                "  return;\n"
777                "}",
778                getLLVMStyleWithColumns(34));
779 }
780 
781 TEST_F(FormatTest, CaseRanges) {
782   verifyFormat("switch (x) {\n"
783                "case 'A' ... 'Z':\n"
784                "case 1 ... 5:\n"
785                "case a ... b:\n"
786                "  break;\n"
787                "}");
788 }
789 
790 TEST_F(FormatTest, ShortCaseLabels) {
791   FormatStyle Style = getLLVMStyle();
792   Style.AllowShortCaseLabelsOnASingleLine = true;
793   verifyFormat("switch (a) {\n"
794                "case 1: x = 1; break;\n"
795                "case 2: return;\n"
796                "case 3:\n"
797                "case 4:\n"
798                "case 5: return;\n"
799                "case 6: // comment\n"
800                "  return;\n"
801                "case 7:\n"
802                "  // comment\n"
803                "  return;\n"
804                "case 8:\n"
805                "  x = 8; // comment\n"
806                "  break;\n"
807                "default: y = 1; break;\n"
808                "}",
809                Style);
810   verifyFormat("switch (a) {\n"
811                "#if FOO\n"
812                "case 0: return 0;\n"
813                "#endif\n"
814                "}",
815                Style);
816   verifyFormat("switch (a) {\n"
817                "case 1: {\n"
818                "}\n"
819                "case 2: {\n"
820                "  return;\n"
821                "}\n"
822                "case 3: {\n"
823                "  x = 1;\n"
824                "  return;\n"
825                "}\n"
826                "case 4:\n"
827                "  if (x)\n"
828                "    return;\n"
829                "}",
830                Style);
831   Style.ColumnLimit = 21;
832   verifyFormat("switch (a) {\n"
833                "case 1: x = 1; break;\n"
834                "case 2: return;\n"
835                "case 3:\n"
836                "case 4:\n"
837                "case 5: return;\n"
838                "default:\n"
839                "  y = 1;\n"
840                "  break;\n"
841                "}",
842                Style);
843 }
844 
845 TEST_F(FormatTest, FormatsLabels) {
846   verifyFormat("void f() {\n"
847                "  some_code();\n"
848                "test_label:\n"
849                "  some_other_code();\n"
850                "  {\n"
851                "    some_more_code();\n"
852                "  another_label:\n"
853                "    some_more_code();\n"
854                "  }\n"
855                "}");
856   verifyFormat("{\n"
857                "  some_code();\n"
858                "test_label:\n"
859                "  some_other_code();\n"
860                "}");
861   verifyFormat("{\n"
862                "  some_code();\n"
863                "test_label:;\n"
864                "  int i = 0;\n"
865                "}");
866 }
867 
868 //===----------------------------------------------------------------------===//
869 // Tests for comments.
870 //===----------------------------------------------------------------------===//
871 
872 TEST_F(FormatTest, UnderstandsSingleLineComments) {
873   verifyFormat("//* */");
874   verifyFormat("// line 1\n"
875                "// line 2\n"
876                "void f() {}\n");
877 
878   verifyFormat("void f() {\n"
879                "  // Doesn't do anything\n"
880                "}");
881   verifyFormat("SomeObject\n"
882                "    // Calling someFunction on SomeObject\n"
883                "    .someFunction();");
884   verifyFormat("auto result = SomeObject\n"
885                "                  // Calling someFunction on SomeObject\n"
886                "                  .someFunction();");
887   verifyFormat("void f(int i,  // some comment (probably for i)\n"
888                "       int j,  // some comment (probably for j)\n"
889                "       int k); // some comment (probably for k)");
890   verifyFormat("void f(int i,\n"
891                "       // some comment (probably for j)\n"
892                "       int j,\n"
893                "       // some comment (probably for k)\n"
894                "       int k);");
895 
896   verifyFormat("int i    // This is a fancy variable\n"
897                "    = 5; // with nicely aligned comment.");
898 
899   verifyFormat("// Leading comment.\n"
900                "int a; // Trailing comment.");
901   verifyFormat("int a; // Trailing comment\n"
902                "       // on 2\n"
903                "       // or 3 lines.\n"
904                "int b;");
905   verifyFormat("int a; // Trailing comment\n"
906                "\n"
907                "// Leading comment.\n"
908                "int b;");
909   verifyFormat("int a;    // Comment.\n"
910                "          // More details.\n"
911                "int bbbb; // Another comment.");
912   verifyFormat(
913       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
914       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
915       "int cccccccccccccccccccccccccccccc;       // comment\n"
916       "int ddd;                     // looooooooooooooooooooooooong comment\n"
917       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
918       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
919       "int ccccccccccccccccccc;     // comment");
920 
921   verifyFormat("#include \"a\"     // comment\n"
922                "#include \"a/b/c\" // comment");
923   verifyFormat("#include <a>     // comment\n"
924                "#include <a/b/c> // comment");
925   EXPECT_EQ("#include \"a\"     // comment\n"
926             "#include \"a/b/c\" // comment",
927             format("#include \\\n"
928                    "  \"a\" // comment\n"
929                    "#include \"a/b/c\" // comment"));
930 
931   verifyFormat("enum E {\n"
932                "  // comment\n"
933                "  VAL_A, // comment\n"
934                "  VAL_B\n"
935                "};");
936 
937   verifyFormat(
938       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
939       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
940   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
941                "    // Comment inside a statement.\n"
942                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
943   verifyFormat("SomeFunction(a,\n"
944                "             // comment\n"
945                "             b + x);");
946   verifyFormat("SomeFunction(a, a,\n"
947                "             // comment\n"
948                "             b + x);");
949   verifyFormat(
950       "bool aaaaaaaaaaaaa = // comment\n"
951       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
952       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
953 
954   verifyFormat("int aaaa; // aaaaa\n"
955                "int aa;   // aaaaaaa",
956                getLLVMStyleWithColumns(20));
957 
958   EXPECT_EQ("void f() { // This does something ..\n"
959             "}\n"
960             "int a; // This is unrelated",
961             format("void f()    {     // This does something ..\n"
962                    "  }\n"
963                    "int   a;     // This is unrelated"));
964   EXPECT_EQ("class C {\n"
965             "  void f() { // This does something ..\n"
966             "  }          // awesome..\n"
967             "\n"
968             "  int a; // This is unrelated\n"
969             "};",
970             format("class C{void f()    { // This does something ..\n"
971                    "      } // awesome..\n"
972                    " \n"
973                    "int a;    // This is unrelated\n"
974                    "};"));
975 
976   EXPECT_EQ("int i; // single line trailing comment",
977             format("int i;\\\n// single line trailing comment"));
978 
979   verifyGoogleFormat("int a;  // Trailing comment.");
980 
981   verifyFormat("someFunction(anotherFunction( // Force break.\n"
982                "    parameter));");
983 
984   verifyGoogleFormat("#endif  // HEADER_GUARD");
985 
986   verifyFormat("const char *test[] = {\n"
987                "    // A\n"
988                "    \"aaaa\",\n"
989                "    // B\n"
990                "    \"aaaaa\"};");
991   verifyGoogleFormat(
992       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
993       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
994   EXPECT_EQ("D(a, {\n"
995             "  // test\n"
996             "  int a;\n"
997             "});",
998             format("D(a, {\n"
999                    "// test\n"
1000                    "int a;\n"
1001                    "});"));
1002 
1003   EXPECT_EQ("lineWith(); // comment\n"
1004             "// at start\n"
1005             "otherLine();",
1006             format("lineWith();   // comment\n"
1007                    "// at start\n"
1008                    "otherLine();"));
1009   EXPECT_EQ("lineWith(); // comment\n"
1010             "/*\n"
1011             " * at start */\n"
1012             "otherLine();",
1013             format("lineWith();   // comment\n"
1014                    "/*\n"
1015                    " * at start */\n"
1016                    "otherLine();"));
1017   EXPECT_EQ("lineWith(); // comment\n"
1018             "            // at start\n"
1019             "otherLine();",
1020             format("lineWith();   // comment\n"
1021                    " // at start\n"
1022                    "otherLine();"));
1023 
1024   EXPECT_EQ("lineWith(); // comment\n"
1025             "// at start\n"
1026             "otherLine(); // comment",
1027             format("lineWith();   // comment\n"
1028                    "// at start\n"
1029                    "otherLine();   // comment"));
1030   EXPECT_EQ("lineWith();\n"
1031             "// at start\n"
1032             "otherLine(); // comment",
1033             format("lineWith();\n"
1034                    " // at start\n"
1035                    "otherLine();   // comment"));
1036   EXPECT_EQ("// first\n"
1037             "// at start\n"
1038             "otherLine(); // comment",
1039             format("// first\n"
1040                    " // at start\n"
1041                    "otherLine();   // comment"));
1042   EXPECT_EQ("f();\n"
1043             "// first\n"
1044             "// at start\n"
1045             "otherLine(); // comment",
1046             format("f();\n"
1047                    "// first\n"
1048                    " // at start\n"
1049                    "otherLine();   // comment"));
1050   verifyFormat("f(); // comment\n"
1051                "// first\n"
1052                "// at start\n"
1053                "otherLine();");
1054   EXPECT_EQ("f(); // comment\n"
1055             "// first\n"
1056             "// at start\n"
1057             "otherLine();",
1058             format("f();   // comment\n"
1059                    "// first\n"
1060                    " // at start\n"
1061                    "otherLine();"));
1062   EXPECT_EQ("f(); // comment\n"
1063             "     // first\n"
1064             "// at start\n"
1065             "otherLine();",
1066             format("f();   // comment\n"
1067                    " // first\n"
1068                    "// at start\n"
1069                    "otherLine();"));
1070   EXPECT_EQ("void f() {\n"
1071             "  lineWith(); // comment\n"
1072             "  // at start\n"
1073             "}",
1074             format("void              f() {\n"
1075                    "  lineWith(); // comment\n"
1076                    "  // at start\n"
1077                    "}"));
1078   EXPECT_EQ("int xy; // a\n"
1079             "int z;  // b",
1080             format("int xy;    // a\n"
1081                    "int z;    //b"));
1082   EXPECT_EQ("int xy; // a\n"
1083             "int z; // bb",
1084             format("int xy;    // a\n"
1085                    "int z;    //bb",
1086                    getLLVMStyleWithColumns(12)));
1087 
1088   verifyFormat("#define A                                                  \\\n"
1089                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1090                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1091                getLLVMStyleWithColumns(60));
1092   verifyFormat(
1093       "#define A                                                   \\\n"
1094       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1095       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1096       getLLVMStyleWithColumns(61));
1097 
1098   verifyFormat("if ( // This is some comment\n"
1099                "    x + 3) {\n"
1100                "}");
1101   EXPECT_EQ("if ( // This is some comment\n"
1102             "     // spanning two lines\n"
1103             "    x + 3) {\n"
1104             "}",
1105             format("if( // This is some comment\n"
1106                    "     // spanning two lines\n"
1107                    " x + 3) {\n"
1108                    "}"));
1109 
1110   verifyNoCrash("/\\\n/");
1111   verifyNoCrash("/\\\n* */");
1112   // The 0-character somehow makes the lexer return a proper comment.
1113   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1114 }
1115 
1116 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1117   EXPECT_EQ("SomeFunction(a,\n"
1118             "             b, // comment\n"
1119             "             c);",
1120             format("SomeFunction(a,\n"
1121                    "          b, // comment\n"
1122                    "      c);"));
1123   EXPECT_EQ("SomeFunction(a, b,\n"
1124             "             // comment\n"
1125             "             c);",
1126             format("SomeFunction(a,\n"
1127                    "          b,\n"
1128                    "  // comment\n"
1129                    "      c);"));
1130   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1131             "             c);",
1132             format("SomeFunction(a, b, // comment (unclear relation)\n"
1133                    "      c);"));
1134   EXPECT_EQ("SomeFunction(a, // comment\n"
1135             "             b,\n"
1136             "             c); // comment",
1137             format("SomeFunction(a,     // comment\n"
1138                    "          b,\n"
1139                    "      c); // comment"));
1140   EXPECT_EQ("aaaaaaaaaa(aaaa(aaaa,\n"
1141             "                aaaa), //\n"
1142             "           aaaa, bbbbb);",
1143             format("aaaaaaaaaa(aaaa(aaaa,\n"
1144                    "aaaa), //\n"
1145                    "aaaa, bbbbb);"));
1146 }
1147 
1148 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1149   EXPECT_EQ("// comment", format("// comment  "));
1150   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1151             format("int aaaaaaa, bbbbbbb; // comment                   ",
1152                    getLLVMStyleWithColumns(33)));
1153   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1154   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1155 }
1156 
1157 TEST_F(FormatTest, UnderstandsBlockComments) {
1158   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1159   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
1160   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1161             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1162             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1163                    "/* Trailing comment for aa... */\n"
1164                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1165   EXPECT_EQ(
1166       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1167       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1168       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1169              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1170   EXPECT_EQ(
1171       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1172       "    aaaaaaaaaaaaaaaaaa,\n"
1173       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1174       "}",
1175       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1176              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1177              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1178              "}"));
1179   verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
1180                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1181 
1182   FormatStyle NoBinPacking = getLLVMStyle();
1183   NoBinPacking.BinPackParameters = false;
1184   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1185                "         /* parameter 2 */ aaaaaa,\n"
1186                "         /* parameter 3 */ aaaaaa,\n"
1187                "         /* parameter 4 */ aaaaaa);",
1188                NoBinPacking);
1189 
1190   // Aligning block comments in macros.
1191   verifyGoogleFormat("#define A        \\\n"
1192                      "  int i;   /*a*/ \\\n"
1193                      "  int jjj; /*b*/");
1194 }
1195 
1196 TEST_F(FormatTest, AlignsBlockComments) {
1197   EXPECT_EQ("/*\n"
1198             " * Really multi-line\n"
1199             " * comment.\n"
1200             " */\n"
1201             "void f() {}",
1202             format("  /*\n"
1203                    "   * Really multi-line\n"
1204                    "   * comment.\n"
1205                    "   */\n"
1206                    "  void f() {}"));
1207   EXPECT_EQ("class C {\n"
1208             "  /*\n"
1209             "   * Another multi-line\n"
1210             "   * comment.\n"
1211             "   */\n"
1212             "  void f() {}\n"
1213             "};",
1214             format("class C {\n"
1215                    "/*\n"
1216                    " * Another multi-line\n"
1217                    " * comment.\n"
1218                    " */\n"
1219                    "void f() {}\n"
1220                    "};"));
1221   EXPECT_EQ("/*\n"
1222             "  1. This is a comment with non-trivial formatting.\n"
1223             "     1.1. We have to indent/outdent all lines equally\n"
1224             "         1.1.1. to keep the formatting.\n"
1225             " */",
1226             format("  /*\n"
1227                    "    1. This is a comment with non-trivial formatting.\n"
1228                    "       1.1. We have to indent/outdent all lines equally\n"
1229                    "           1.1.1. to keep the formatting.\n"
1230                    "   */"));
1231   EXPECT_EQ("/*\n"
1232             "Don't try to outdent if there's not enough indentation.\n"
1233             "*/",
1234             format("  /*\n"
1235                    " Don't try to outdent if there's not enough indentation.\n"
1236                    " */"));
1237 
1238   EXPECT_EQ("int i; /* Comment with empty...\n"
1239             "        *\n"
1240             "        * line. */",
1241             format("int i; /* Comment with empty...\n"
1242                    "        *\n"
1243                    "        * line. */"));
1244   EXPECT_EQ("int foobar = 0; /* comment */\n"
1245             "int bar = 0;    /* multiline\n"
1246             "                   comment 1 */\n"
1247             "int baz = 0;    /* multiline\n"
1248             "                   comment 2 */\n"
1249             "int bzz = 0;    /* multiline\n"
1250             "                   comment 3 */",
1251             format("int foobar = 0; /* comment */\n"
1252                    "int bar = 0;    /* multiline\n"
1253                    "                   comment 1 */\n"
1254                    "int baz = 0; /* multiline\n"
1255                    "                comment 2 */\n"
1256                    "int bzz = 0;         /* multiline\n"
1257                    "                        comment 3 */"));
1258   EXPECT_EQ("int foobar = 0; /* comment */\n"
1259             "int bar = 0;    /* multiline\n"
1260             "   comment */\n"
1261             "int baz = 0;    /* multiline\n"
1262             "comment */",
1263             format("int foobar = 0; /* comment */\n"
1264                    "int bar = 0; /* multiline\n"
1265                    "comment */\n"
1266                    "int baz = 0;        /* multiline\n"
1267                    "comment */"));
1268 }
1269 
1270 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1271   FormatStyle Style = getLLVMStyleWithColumns(20);
1272   Style.ReflowComments = false;
1273   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1274   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1275 }
1276 
1277 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1278   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1279             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1280             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1281                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1282   EXPECT_EQ(
1283       "void ffffffffffff(\n"
1284       "    int aaaaaaaa, int bbbbbbbb,\n"
1285       "    int cccccccccccc) { /*\n"
1286       "                           aaaaaaaaaa\n"
1287       "                           aaaaaaaaaaaaa\n"
1288       "                           bbbbbbbbbbbbbb\n"
1289       "                           bbbbbbbbbb\n"
1290       "                         */\n"
1291       "}",
1292       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1293              "{ /*\n"
1294              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1295              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1296              "   */\n"
1297              "}",
1298              getLLVMStyleWithColumns(40)));
1299 }
1300 
1301 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1302   EXPECT_EQ("void ffffffffff(\n"
1303             "    int aaaaa /* test */);",
1304             format("void ffffffffff(int aaaaa /* test */);",
1305                    getLLVMStyleWithColumns(35)));
1306 }
1307 
1308 TEST_F(FormatTest, SplitsLongCxxComments) {
1309   EXPECT_EQ("// A comment that\n"
1310             "// doesn't fit on\n"
1311             "// one line",
1312             format("// A comment that doesn't fit on one line",
1313                    getLLVMStyleWithColumns(20)));
1314   EXPECT_EQ("/// A comment that\n"
1315             "/// doesn't fit on\n"
1316             "/// one line",
1317             format("/// A comment that doesn't fit on one line",
1318                    getLLVMStyleWithColumns(20)));
1319   EXPECT_EQ("//! A comment that\n"
1320             "//! doesn't fit on\n"
1321             "//! one line",
1322             format("//! A comment that doesn't fit on one line",
1323                    getLLVMStyleWithColumns(20)));
1324   EXPECT_EQ("// a b c d\n"
1325             "// e f  g\n"
1326             "// h i j k",
1327             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1328   EXPECT_EQ(
1329       "// a b c d\n"
1330       "// e f  g\n"
1331       "// h i j k",
1332       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1333   EXPECT_EQ("if (true) // A comment that\n"
1334             "          // doesn't fit on\n"
1335             "          // one line",
1336             format("if (true) // A comment that doesn't fit on one line   ",
1337                    getLLVMStyleWithColumns(30)));
1338   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1339             format("//    Don't_touch_leading_whitespace",
1340                    getLLVMStyleWithColumns(20)));
1341   EXPECT_EQ("// Add leading\n"
1342             "// whitespace",
1343             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1344   EXPECT_EQ("/// Add leading\n"
1345             "/// whitespace",
1346             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1347   EXPECT_EQ("//! Add leading\n"
1348             "//! whitespace",
1349             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1350   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1351   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1352             "// limit",
1353             format("//Even if it makes the line exceed the column limit",
1354                    getLLVMStyleWithColumns(51)));
1355   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1356 
1357   EXPECT_EQ("// aa bb cc dd",
1358             format("// aa bb             cc dd                   ",
1359                    getLLVMStyleWithColumns(15)));
1360 
1361   EXPECT_EQ("// A comment before\n"
1362             "// a macro\n"
1363             "// definition\n"
1364             "#define a b",
1365             format("// A comment before a macro definition\n"
1366                    "#define a b",
1367                    getLLVMStyleWithColumns(20)));
1368   EXPECT_EQ("void ffffff(\n"
1369             "    int aaaaaaaaa,  // wwww\n"
1370             "    int bbbbbbbbbb, // xxxxxxx\n"
1371             "                    // yyyyyyyyyy\n"
1372             "    int c, int d, int e) {}",
1373             format("void ffffff(\n"
1374                    "    int aaaaaaaaa, // wwww\n"
1375                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1376                    "    int c, int d, int e) {}",
1377                    getLLVMStyleWithColumns(40)));
1378   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1379             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1380                    getLLVMStyleWithColumns(20)));
1381   EXPECT_EQ(
1382       "#define XXX // a b c d\n"
1383       "            // e f g h",
1384       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1385   EXPECT_EQ(
1386       "#define XXX // q w e r\n"
1387       "            // t y u i",
1388       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1389 }
1390 
1391 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1392   EXPECT_EQ("//     A comment\n"
1393             "//     that doesn't\n"
1394             "//     fit on one\n"
1395             "//     line",
1396             format("//     A comment that doesn't fit on one line",
1397                    getLLVMStyleWithColumns(20)));
1398   EXPECT_EQ("///     A comment\n"
1399             "///     that doesn't\n"
1400             "///     fit on one\n"
1401             "///     line",
1402             format("///     A comment that doesn't fit on one line",
1403                    getLLVMStyleWithColumns(20)));
1404 }
1405 
1406 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1407   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1408             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1409             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1410             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1411                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1412                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1413   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1414             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1415             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1416             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1417                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1418                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1419                    getLLVMStyleWithColumns(50)));
1420   // FIXME: One day we might want to implement adjustment of leading whitespace
1421   // of the consecutive lines in this kind of comment:
1422   EXPECT_EQ("double\n"
1423             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1424             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1425             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1426             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1427                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1428                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1429                    getLLVMStyleWithColumns(49)));
1430 }
1431 
1432 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1433   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1434   Pragmas.CommentPragmas = "^ IWYU pragma:";
1435   EXPECT_EQ(
1436       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1437       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1438   EXPECT_EQ(
1439       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1440       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1441 }
1442 
1443 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1444   EXPECT_EQ("if (xxx ==\n"
1445             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1446             "    zzz)\n"
1447             "  q();",
1448             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1449                    "    zzz) q();",
1450                    getLLVMStyleWithColumns(40)));
1451   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1452             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1453             "    zzz)\n"
1454             "  q();",
1455             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1456                    "    zzz) q();",
1457                    getLLVMStyleWithColumns(40)));
1458   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1459             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1460             "    zzz)\n"
1461             "  q();",
1462             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1463                    "    zzz) q();",
1464                    getLLVMStyleWithColumns(40)));
1465   EXPECT_EQ("fffffffff(\n"
1466             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1467             "    zzz);",
1468             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1469                    " zzz);",
1470                    getLLVMStyleWithColumns(40)));
1471 }
1472 
1473 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1474   EXPECT_EQ("#define A(x) /* \\\n"
1475             "  a comment     \\\n"
1476             "  inside */     \\\n"
1477             "  f();",
1478             format("#define A(x) /* \\\n"
1479                    "  a comment     \\\n"
1480                    "  inside */     \\\n"
1481                    "  f();",
1482                    getLLVMStyleWithColumns(17)));
1483   EXPECT_EQ("#define A(      \\\n"
1484             "    x) /*       \\\n"
1485             "  a comment     \\\n"
1486             "  inside */     \\\n"
1487             "  f();",
1488             format("#define A(      \\\n"
1489                    "    x) /*       \\\n"
1490                    "  a comment     \\\n"
1491                    "  inside */     \\\n"
1492                    "  f();",
1493                    getLLVMStyleWithColumns(17)));
1494 }
1495 
1496 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1497   EXPECT_EQ("namespace {}\n// Test\n#define A",
1498             format("namespace {}\n   // Test\n#define A"));
1499   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1500             format("namespace {}\n   /* Test */\n#define A"));
1501   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1502             format("namespace {}\n   /* Test */    #define A"));
1503 }
1504 
1505 TEST_F(FormatTest, SplitsLongLinesInComments) {
1506   EXPECT_EQ("/* This is a long\n"
1507             " * comment that\n"
1508             " * doesn't\n"
1509             " * fit on one line.\n"
1510             " */",
1511             format("/* "
1512                    "This is a long                                         "
1513                    "comment that "
1514                    "doesn't                                    "
1515                    "fit on one line.  */",
1516                    getLLVMStyleWithColumns(20)));
1517   EXPECT_EQ(
1518       "/* a b c d\n"
1519       " * e f  g\n"
1520       " * h i j k\n"
1521       " */",
1522       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1523   EXPECT_EQ(
1524       "/* a b c d\n"
1525       " * e f  g\n"
1526       " * h i j k\n"
1527       " */",
1528       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1529   EXPECT_EQ("/*\n"
1530             "This is a long\n"
1531             "comment that doesn't\n"
1532             "fit on one line.\n"
1533             "*/",
1534             format("/*\n"
1535                    "This is a long                                         "
1536                    "comment that doesn't                                    "
1537                    "fit on one line.                                      \n"
1538                    "*/",
1539                    getLLVMStyleWithColumns(20)));
1540   EXPECT_EQ("/*\n"
1541             " * This is a long\n"
1542             " * comment that\n"
1543             " * doesn't fit on\n"
1544             " * one line.\n"
1545             " */",
1546             format("/*      \n"
1547                    " * This is a long "
1548                    "   comment that     "
1549                    "   doesn't fit on   "
1550                    "   one line.                                            \n"
1551                    " */",
1552                    getLLVMStyleWithColumns(20)));
1553   EXPECT_EQ("/*\n"
1554             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1555             " * so_it_should_be_broken\n"
1556             " * wherever_a_space_occurs\n"
1557             " */",
1558             format("/*\n"
1559                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1560                    "   so_it_should_be_broken "
1561                    "   wherever_a_space_occurs                             \n"
1562                    " */",
1563                    getLLVMStyleWithColumns(20)));
1564   EXPECT_EQ("/*\n"
1565             " *    This_comment_can_not_be_broken_into_lines\n"
1566             " */",
1567             format("/*\n"
1568                    " *    This_comment_can_not_be_broken_into_lines\n"
1569                    " */",
1570                    getLLVMStyleWithColumns(20)));
1571   EXPECT_EQ("{\n"
1572             "  /*\n"
1573             "  This is another\n"
1574             "  long comment that\n"
1575             "  doesn't fit on one\n"
1576             "  line    1234567890\n"
1577             "  */\n"
1578             "}",
1579             format("{\n"
1580                    "/*\n"
1581                    "This is another     "
1582                    "  long comment that "
1583                    "  doesn't fit on one"
1584                    "  line    1234567890\n"
1585                    "*/\n"
1586                    "}",
1587                    getLLVMStyleWithColumns(20)));
1588   EXPECT_EQ("{\n"
1589             "  /*\n"
1590             "   * This        i s\n"
1591             "   * another comment\n"
1592             "   * t hat  doesn' t\n"
1593             "   * fit on one l i\n"
1594             "   * n e\n"
1595             "   */\n"
1596             "}",
1597             format("{\n"
1598                    "/*\n"
1599                    " * This        i s"
1600                    "   another comment"
1601                    "   t hat  doesn' t"
1602                    "   fit on one l i"
1603                    "   n e\n"
1604                    " */\n"
1605                    "}",
1606                    getLLVMStyleWithColumns(20)));
1607   EXPECT_EQ("/*\n"
1608             " * This is a long\n"
1609             " * comment that\n"
1610             " * doesn't fit on\n"
1611             " * one line\n"
1612             " */",
1613             format("   /*\n"
1614                    "    * This is a long comment that doesn't fit on one line\n"
1615                    "    */",
1616                    getLLVMStyleWithColumns(20)));
1617   EXPECT_EQ("{\n"
1618             "  if (something) /* This is a\n"
1619             "                    long\n"
1620             "                    comment */\n"
1621             "    ;\n"
1622             "}",
1623             format("{\n"
1624                    "  if (something) /* This is a long comment */\n"
1625                    "    ;\n"
1626                    "}",
1627                    getLLVMStyleWithColumns(30)));
1628 
1629   EXPECT_EQ("/* A comment before\n"
1630             " * a macro\n"
1631             " * definition */\n"
1632             "#define a b",
1633             format("/* A comment before a macro definition */\n"
1634                    "#define a b",
1635                    getLLVMStyleWithColumns(20)));
1636 
1637   EXPECT_EQ("/* some comment\n"
1638             "     *   a comment\n"
1639             "* that we break\n"
1640             " * another comment\n"
1641             "* we have to break\n"
1642             "* a left comment\n"
1643             " */",
1644             format("  /* some comment\n"
1645                    "       *   a comment that we break\n"
1646                    "   * another comment we have to break\n"
1647                    "* a left comment\n"
1648                    "   */",
1649                    getLLVMStyleWithColumns(20)));
1650 
1651   EXPECT_EQ("/**\n"
1652             " * multiline block\n"
1653             " * comment\n"
1654             " *\n"
1655             " */",
1656             format("/**\n"
1657                    " * multiline block comment\n"
1658                    " *\n"
1659                    " */",
1660                    getLLVMStyleWithColumns(20)));
1661 
1662   EXPECT_EQ("/*\n"
1663             "\n"
1664             "\n"
1665             "    */\n",
1666             format("  /*       \n"
1667                    "      \n"
1668                    "               \n"
1669                    "      */\n"));
1670 
1671   EXPECT_EQ("/* a a */",
1672             format("/* a a            */", getLLVMStyleWithColumns(15)));
1673   EXPECT_EQ("/* a a bc  */",
1674             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1675   EXPECT_EQ("/* aaa aaa\n"
1676             " * aaaaa */",
1677             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1678   EXPECT_EQ("/* aaa aaa\n"
1679             " * aaaaa     */",
1680             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1681 }
1682 
1683 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1684   EXPECT_EQ("#define X          \\\n"
1685             "  /*               \\\n"
1686             "   Test            \\\n"
1687             "   Macro comment   \\\n"
1688             "   with a long     \\\n"
1689             "   line            \\\n"
1690             "   */              \\\n"
1691             "  A + B",
1692             format("#define X \\\n"
1693                    "  /*\n"
1694                    "   Test\n"
1695                    "   Macro comment with a long  line\n"
1696                    "   */ \\\n"
1697                    "  A + B",
1698                    getLLVMStyleWithColumns(20)));
1699   EXPECT_EQ("#define X          \\\n"
1700             "  /* Macro comment \\\n"
1701             "     with a long   \\\n"
1702             "     line */       \\\n"
1703             "  A + B",
1704             format("#define X \\\n"
1705                    "  /* Macro comment with a long\n"
1706                    "     line */ \\\n"
1707                    "  A + B",
1708                    getLLVMStyleWithColumns(20)));
1709   EXPECT_EQ("#define X          \\\n"
1710             "  /* Macro comment \\\n"
1711             "   * with a long   \\\n"
1712             "   * line */       \\\n"
1713             "  A + B",
1714             format("#define X \\\n"
1715                    "  /* Macro comment with a long  line */ \\\n"
1716                    "  A + B",
1717                    getLLVMStyleWithColumns(20)));
1718 }
1719 
1720 TEST_F(FormatTest, CommentsInStaticInitializers) {
1721   EXPECT_EQ(
1722       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1723       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1724       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1725       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1726       "                        aaaaaaaaaaaaaaaaaaaa};",
1727       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1728              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1729              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1730              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1731              "                  aaaaaaaaaaaaaaaaaaaa };"));
1732   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1733                "                        bbbbbbbbbbb, ccccccccccc};");
1734   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1735                "                        // comment for bb....\n"
1736                "                        bbbbbbbbbbb, ccccccccccc};");
1737   verifyGoogleFormat(
1738       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1739       "                        bbbbbbbbbbb, ccccccccccc};");
1740   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1741                      "                        // comment for bb....\n"
1742                      "                        bbbbbbbbbbb, ccccccccccc};");
1743 
1744   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1745                "       {d, e, f},  // Group #2\n"
1746                "       {g, h, i}}; // Group #3");
1747   verifyFormat("S s = {{// Group #1\n"
1748                "        a, b, c},\n"
1749                "       {// Group #2\n"
1750                "        d, e, f},\n"
1751                "       {// Group #3\n"
1752                "        g, h, i}};");
1753 
1754   EXPECT_EQ("S s = {\n"
1755             "    // Some comment\n"
1756             "    a,\n"
1757             "\n"
1758             "    // Comment after empty line\n"
1759             "    b}",
1760             format("S s =    {\n"
1761                    "      // Some comment\n"
1762                    "  a,\n"
1763                    "  \n"
1764                    "     // Comment after empty line\n"
1765                    "      b\n"
1766                    "}"));
1767   EXPECT_EQ("S s = {\n"
1768             "    /* Some comment */\n"
1769             "    a,\n"
1770             "\n"
1771             "    /* Comment after empty line */\n"
1772             "    b}",
1773             format("S s =    {\n"
1774                    "      /* Some comment */\n"
1775                    "  a,\n"
1776                    "  \n"
1777                    "     /* Comment after empty line */\n"
1778                    "      b\n"
1779                    "}"));
1780   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1781                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1782                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1783                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1784 }
1785 
1786 TEST_F(FormatTest, IgnoresIf0Contents) {
1787   EXPECT_EQ("#if 0\n"
1788             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1789             "#endif\n"
1790             "void f() {}",
1791             format("#if 0\n"
1792                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1793                    "#endif\n"
1794                    "void f(  ) {  }"));
1795   EXPECT_EQ("#if false\n"
1796             "void f(  ) {  }\n"
1797             "#endif\n"
1798             "void g() {}\n",
1799             format("#if false\n"
1800                    "void f(  ) {  }\n"
1801                    "#endif\n"
1802                    "void g(  ) {  }\n"));
1803   EXPECT_EQ("enum E {\n"
1804             "  One,\n"
1805             "  Two,\n"
1806             "#if 0\n"
1807             "Three,\n"
1808             "      Four,\n"
1809             "#endif\n"
1810             "  Five\n"
1811             "};",
1812             format("enum E {\n"
1813                    "  One,Two,\n"
1814                    "#if 0\n"
1815                    "Three,\n"
1816                    "      Four,\n"
1817                    "#endif\n"
1818                    "  Five};"));
1819   EXPECT_EQ("enum F {\n"
1820             "  One,\n"
1821             "#if 1\n"
1822             "  Two,\n"
1823             "#if 0\n"
1824             "Three,\n"
1825             "      Four,\n"
1826             "#endif\n"
1827             "  Five\n"
1828             "#endif\n"
1829             "};",
1830             format("enum F {\n"
1831                    "One,\n"
1832                    "#if 1\n"
1833                    "Two,\n"
1834                    "#if 0\n"
1835                    "Three,\n"
1836                    "      Four,\n"
1837                    "#endif\n"
1838                    "Five\n"
1839                    "#endif\n"
1840                    "};"));
1841   EXPECT_EQ("enum G {\n"
1842             "  One,\n"
1843             "#if 0\n"
1844             "Two,\n"
1845             "#else\n"
1846             "  Three,\n"
1847             "#endif\n"
1848             "  Four\n"
1849             "};",
1850             format("enum G {\n"
1851                    "One,\n"
1852                    "#if 0\n"
1853                    "Two,\n"
1854                    "#else\n"
1855                    "Three,\n"
1856                    "#endif\n"
1857                    "Four\n"
1858                    "};"));
1859   EXPECT_EQ("enum H {\n"
1860             "  One,\n"
1861             "#if 0\n"
1862             "#ifdef Q\n"
1863             "Two,\n"
1864             "#else\n"
1865             "Three,\n"
1866             "#endif\n"
1867             "#endif\n"
1868             "  Four\n"
1869             "};",
1870             format("enum H {\n"
1871                    "One,\n"
1872                    "#if 0\n"
1873                    "#ifdef Q\n"
1874                    "Two,\n"
1875                    "#else\n"
1876                    "Three,\n"
1877                    "#endif\n"
1878                    "#endif\n"
1879                    "Four\n"
1880                    "};"));
1881   EXPECT_EQ("enum I {\n"
1882             "  One,\n"
1883             "#if /* test */ 0 || 1\n"
1884             "Two,\n"
1885             "Three,\n"
1886             "#endif\n"
1887             "  Four\n"
1888             "};",
1889             format("enum I {\n"
1890                    "One,\n"
1891                    "#if /* test */ 0 || 1\n"
1892                    "Two,\n"
1893                    "Three,\n"
1894                    "#endif\n"
1895                    "Four\n"
1896                    "};"));
1897   EXPECT_EQ("enum J {\n"
1898             "  One,\n"
1899             "#if 0\n"
1900             "#if 0\n"
1901             "Two,\n"
1902             "#else\n"
1903             "Three,\n"
1904             "#endif\n"
1905             "Four,\n"
1906             "#endif\n"
1907             "  Five\n"
1908             "};",
1909             format("enum J {\n"
1910                    "One,\n"
1911                    "#if 0\n"
1912                    "#if 0\n"
1913                    "Two,\n"
1914                    "#else\n"
1915                    "Three,\n"
1916                    "#endif\n"
1917                    "Four,\n"
1918                    "#endif\n"
1919                    "Five\n"
1920                    "};"));
1921 }
1922 
1923 //===----------------------------------------------------------------------===//
1924 // Tests for classes, namespaces, etc.
1925 //===----------------------------------------------------------------------===//
1926 
1927 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1928   verifyFormat("class A {};");
1929 }
1930 
1931 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1932   verifyFormat("class A {\n"
1933                "public:\n"
1934                "public: // comment\n"
1935                "protected:\n"
1936                "private:\n"
1937                "  void f() {}\n"
1938                "};");
1939   verifyGoogleFormat("class A {\n"
1940                      " public:\n"
1941                      " protected:\n"
1942                      " private:\n"
1943                      "  void f() {}\n"
1944                      "};");
1945   verifyFormat("class A {\n"
1946                "public slots:\n"
1947                "  void f1() {}\n"
1948                "public Q_SLOTS:\n"
1949                "  void f2() {}\n"
1950                "protected slots:\n"
1951                "  void f3() {}\n"
1952                "protected Q_SLOTS:\n"
1953                "  void f4() {}\n"
1954                "private slots:\n"
1955                "  void f5() {}\n"
1956                "private Q_SLOTS:\n"
1957                "  void f6() {}\n"
1958                "signals:\n"
1959                "  void g1();\n"
1960                "Q_SIGNALS:\n"
1961                "  void g2();\n"
1962                "};");
1963 
1964   // Don't interpret 'signals' the wrong way.
1965   verifyFormat("signals.set();");
1966   verifyFormat("for (Signals signals : f()) {\n}");
1967   verifyFormat("{\n"
1968                "  signals.set(); // This needs indentation.\n"
1969                "}");
1970   verifyFormat("void f() {\n"
1971                "label:\n"
1972                "  signals.baz();\n"
1973                "}");
1974 }
1975 
1976 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1977   EXPECT_EQ("class A {\n"
1978             "public:\n"
1979             "  void f();\n"
1980             "\n"
1981             "private:\n"
1982             "  void g() {}\n"
1983             "  // test\n"
1984             "protected:\n"
1985             "  int h;\n"
1986             "};",
1987             format("class A {\n"
1988                    "public:\n"
1989                    "void f();\n"
1990                    "private:\n"
1991                    "void g() {}\n"
1992                    "// test\n"
1993                    "protected:\n"
1994                    "int h;\n"
1995                    "};"));
1996   EXPECT_EQ("class A {\n"
1997             "protected:\n"
1998             "public:\n"
1999             "  void f();\n"
2000             "};",
2001             format("class A {\n"
2002                    "protected:\n"
2003                    "\n"
2004                    "public:\n"
2005                    "\n"
2006                    "  void f();\n"
2007                    "};"));
2008 
2009   // Even ensure proper spacing inside macros.
2010   EXPECT_EQ("#define B     \\\n"
2011             "  class A {   \\\n"
2012             "   protected: \\\n"
2013             "   public:    \\\n"
2014             "    void f(); \\\n"
2015             "  };",
2016             format("#define B     \\\n"
2017                    "  class A {   \\\n"
2018                    "   protected: \\\n"
2019                    "              \\\n"
2020                    "   public:    \\\n"
2021                    "              \\\n"
2022                    "    void f(); \\\n"
2023                    "  };",
2024                    getGoogleStyle()));
2025   // But don't remove empty lines after macros ending in access specifiers.
2026   EXPECT_EQ("#define A private:\n"
2027             "\n"
2028             "int i;",
2029             format("#define A         private:\n"
2030                    "\n"
2031                    "int              i;"));
2032 }
2033 
2034 TEST_F(FormatTest, FormatsClasses) {
2035   verifyFormat("class A : public B {};");
2036   verifyFormat("class A : public ::B {};");
2037 
2038   verifyFormat(
2039       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2040       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2041   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2042                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2043                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2044   verifyFormat(
2045       "class A : public B, public C, public D, public E, public F {};");
2046   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2047                "                     public C,\n"
2048                "                     public D,\n"
2049                "                     public E,\n"
2050                "                     public F,\n"
2051                "                     public G {};");
2052 
2053   verifyFormat("class\n"
2054                "    ReallyReallyLongClassName {\n"
2055                "  int i;\n"
2056                "};",
2057                getLLVMStyleWithColumns(32));
2058   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2059                "                           aaaaaaaaaaaaaaaa> {};");
2060   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2061                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2062                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2063   verifyFormat("template <class R, class C>\n"
2064                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2065                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2066   verifyFormat("class ::A::B {};");
2067 }
2068 
2069 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2070   verifyFormat("class A {\n} a, b;");
2071   verifyFormat("struct A {\n} a, b;");
2072   verifyFormat("union A {\n} a;");
2073 }
2074 
2075 TEST_F(FormatTest, FormatsEnum) {
2076   verifyFormat("enum {\n"
2077                "  Zero,\n"
2078                "  One = 1,\n"
2079                "  Two = One + 1,\n"
2080                "  Three = (One + Two),\n"
2081                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2082                "  Five = (One, Two, Three, Four, 5)\n"
2083                "};");
2084   verifyGoogleFormat("enum {\n"
2085                      "  Zero,\n"
2086                      "  One = 1,\n"
2087                      "  Two = One + 1,\n"
2088                      "  Three = (One + Two),\n"
2089                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2090                      "  Five = (One, Two, Three, Four, 5)\n"
2091                      "};");
2092   verifyFormat("enum Enum {};");
2093   verifyFormat("enum {};");
2094   verifyFormat("enum X E {} d;");
2095   verifyFormat("enum __attribute__((...)) E {} d;");
2096   verifyFormat("enum __declspec__((...)) E {} d;");
2097   verifyFormat("enum {\n"
2098                "  Bar = Foo<int, int>::value\n"
2099                "};",
2100                getLLVMStyleWithColumns(30));
2101 
2102   verifyFormat("enum ShortEnum { A, B, C };");
2103   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2104 
2105   EXPECT_EQ("enum KeepEmptyLines {\n"
2106             "  ONE,\n"
2107             "\n"
2108             "  TWO,\n"
2109             "\n"
2110             "  THREE\n"
2111             "}",
2112             format("enum KeepEmptyLines {\n"
2113                    "  ONE,\n"
2114                    "\n"
2115                    "  TWO,\n"
2116                    "\n"
2117                    "\n"
2118                    "  THREE\n"
2119                    "}"));
2120   verifyFormat("enum E { // comment\n"
2121                "  ONE,\n"
2122                "  TWO\n"
2123                "};\n"
2124                "int i;");
2125   // Not enums.
2126   verifyFormat("enum X f() {\n"
2127                "  a();\n"
2128                "  return 42;\n"
2129                "}");
2130   verifyFormat("enum X Type::f() {\n"
2131                "  a();\n"
2132                "  return 42;\n"
2133                "}");
2134   verifyFormat("enum ::X f() {\n"
2135                "  a();\n"
2136                "  return 42;\n"
2137                "}");
2138   verifyFormat("enum ns::X f() {\n"
2139                "  a();\n"
2140                "  return 42;\n"
2141                "}");
2142 }
2143 
2144 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2145   verifyFormat("enum Type {\n"
2146                "  One = 0; // These semicolons should be commas.\n"
2147                "  Two = 1;\n"
2148                "};");
2149   verifyFormat("namespace n {\n"
2150                "enum Type {\n"
2151                "  One,\n"
2152                "  Two, // missing };\n"
2153                "  int i;\n"
2154                "}\n"
2155                "void g() {}");
2156 }
2157 
2158 TEST_F(FormatTest, FormatsEnumStruct) {
2159   verifyFormat("enum struct {\n"
2160                "  Zero,\n"
2161                "  One = 1,\n"
2162                "  Two = One + 1,\n"
2163                "  Three = (One + Two),\n"
2164                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2165                "  Five = (One, Two, Three, Four, 5)\n"
2166                "};");
2167   verifyFormat("enum struct Enum {};");
2168   verifyFormat("enum struct {};");
2169   verifyFormat("enum struct X E {} d;");
2170   verifyFormat("enum struct __attribute__((...)) E {} d;");
2171   verifyFormat("enum struct __declspec__((...)) E {} d;");
2172   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2173 }
2174 
2175 TEST_F(FormatTest, FormatsEnumClass) {
2176   verifyFormat("enum class {\n"
2177                "  Zero,\n"
2178                "  One = 1,\n"
2179                "  Two = One + 1,\n"
2180                "  Three = (One + Two),\n"
2181                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2182                "  Five = (One, Two, Three, Four, 5)\n"
2183                "};");
2184   verifyFormat("enum class Enum {};");
2185   verifyFormat("enum class {};");
2186   verifyFormat("enum class X E {} d;");
2187   verifyFormat("enum class __attribute__((...)) E {} d;");
2188   verifyFormat("enum class __declspec__((...)) E {} d;");
2189   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2190 }
2191 
2192 TEST_F(FormatTest, FormatsEnumTypes) {
2193   verifyFormat("enum X : int {\n"
2194                "  A, // Force multiple lines.\n"
2195                "  B\n"
2196                "};");
2197   verifyFormat("enum X : int { A, B };");
2198   verifyFormat("enum X : std::uint32_t { A, B };");
2199 }
2200 
2201 TEST_F(FormatTest, FormatsNSEnums) {
2202   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2203   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2204                      "  // Information about someDecentlyLongValue.\n"
2205                      "  someDecentlyLongValue,\n"
2206                      "  // Information about anotherDecentlyLongValue.\n"
2207                      "  anotherDecentlyLongValue,\n"
2208                      "  // Information about aThirdDecentlyLongValue.\n"
2209                      "  aThirdDecentlyLongValue\n"
2210                      "};");
2211   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2212                      "  a = 1,\n"
2213                      "  b = 2,\n"
2214                      "  c = 3,\n"
2215                      "};");
2216   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2217                      "  a = 1,\n"
2218                      "  b = 2,\n"
2219                      "  c = 3,\n"
2220                      "};");
2221   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2222                      "  a = 1,\n"
2223                      "  b = 2,\n"
2224                      "  c = 3,\n"
2225                      "};");
2226 }
2227 
2228 TEST_F(FormatTest, FormatsBitfields) {
2229   verifyFormat("struct Bitfields {\n"
2230                "  unsigned sClass : 8;\n"
2231                "  unsigned ValueKind : 2;\n"
2232                "};");
2233   verifyFormat("struct A {\n"
2234                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2235                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2236                "};");
2237   verifyFormat("struct MyStruct {\n"
2238                "  uchar data;\n"
2239                "  uchar : 8;\n"
2240                "  uchar : 8;\n"
2241                "  uchar other;\n"
2242                "};");
2243 }
2244 
2245 TEST_F(FormatTest, FormatsNamespaces) {
2246   verifyFormat("namespace some_namespace {\n"
2247                "class A {};\n"
2248                "void f() { f(); }\n"
2249                "}");
2250   verifyFormat("namespace {\n"
2251                "class A {};\n"
2252                "void f() { f(); }\n"
2253                "}");
2254   verifyFormat("inline namespace X {\n"
2255                "class A {};\n"
2256                "void f() { f(); }\n"
2257                "}");
2258   verifyFormat("using namespace some_namespace;\n"
2259                "class A {};\n"
2260                "void f() { f(); }");
2261 
2262   // This code is more common than we thought; if we
2263   // layout this correctly the semicolon will go into
2264   // its own line, which is undesirable.
2265   verifyFormat("namespace {};");
2266   verifyFormat("namespace {\n"
2267                "class A {};\n"
2268                "};");
2269 
2270   verifyFormat("namespace {\n"
2271                "int SomeVariable = 0; // comment\n"
2272                "} // namespace");
2273   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2274             "#define HEADER_GUARD\n"
2275             "namespace my_namespace {\n"
2276             "int i;\n"
2277             "} // my_namespace\n"
2278             "#endif // HEADER_GUARD",
2279             format("#ifndef HEADER_GUARD\n"
2280                    " #define HEADER_GUARD\n"
2281                    "   namespace my_namespace {\n"
2282                    "int i;\n"
2283                    "}    // my_namespace\n"
2284                    "#endif    // HEADER_GUARD"));
2285 
2286   EXPECT_EQ("namespace A::B {\n"
2287             "class C {};\n"
2288             "}",
2289             format("namespace A::B {\n"
2290                    "class C {};\n"
2291                    "}"));
2292 
2293   FormatStyle Style = getLLVMStyle();
2294   Style.NamespaceIndentation = FormatStyle::NI_All;
2295   EXPECT_EQ("namespace out {\n"
2296             "  int i;\n"
2297             "  namespace in {\n"
2298             "    int i;\n"
2299             "  } // namespace\n"
2300             "} // namespace",
2301             format("namespace out {\n"
2302                    "int i;\n"
2303                    "namespace in {\n"
2304                    "int i;\n"
2305                    "} // namespace\n"
2306                    "} // namespace",
2307                    Style));
2308 
2309   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2310   EXPECT_EQ("namespace out {\n"
2311             "int i;\n"
2312             "namespace in {\n"
2313             "  int i;\n"
2314             "} // namespace\n"
2315             "} // namespace",
2316             format("namespace out {\n"
2317                    "int i;\n"
2318                    "namespace in {\n"
2319                    "int i;\n"
2320                    "} // namespace\n"
2321                    "} // namespace",
2322                    Style));
2323 }
2324 
2325 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2326 
2327 TEST_F(FormatTest, FormatsInlineASM) {
2328   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2329   verifyFormat("asm(\"nop\" ::: \"memory\");");
2330   verifyFormat(
2331       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2332       "    \"cpuid\\n\\t\"\n"
2333       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2334       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2335       "    : \"a\"(value));");
2336   EXPECT_EQ(
2337       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2338       "  __asm {\n"
2339       "        mov     edx,[that] // vtable in edx\n"
2340       "        mov     eax,methodIndex\n"
2341       "        call    [edx][eax*4] // stdcall\n"
2342       "  }\n"
2343       "}",
2344       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2345              "    __asm {\n"
2346              "        mov     edx,[that] // vtable in edx\n"
2347              "        mov     eax,methodIndex\n"
2348              "        call    [edx][eax*4] // stdcall\n"
2349              "    }\n"
2350              "}"));
2351   EXPECT_EQ("_asm {\n"
2352             "  xor eax, eax;\n"
2353             "  cpuid;\n"
2354             "}",
2355             format("_asm {\n"
2356                    "  xor eax, eax;\n"
2357                    "  cpuid;\n"
2358                    "}"));
2359   verifyFormat("void function() {\n"
2360                "  // comment\n"
2361                "  asm(\"\");\n"
2362                "}");
2363   EXPECT_EQ("__asm {\n"
2364             "}\n"
2365             "int i;",
2366             format("__asm   {\n"
2367                    "}\n"
2368                    "int   i;"));
2369 }
2370 
2371 TEST_F(FormatTest, FormatTryCatch) {
2372   verifyFormat("try {\n"
2373                "  throw a * b;\n"
2374                "} catch (int a) {\n"
2375                "  // Do nothing.\n"
2376                "} catch (...) {\n"
2377                "  exit(42);\n"
2378                "}");
2379 
2380   // Function-level try statements.
2381   verifyFormat("int f() try { return 4; } catch (...) {\n"
2382                "  return 5;\n"
2383                "}");
2384   verifyFormat("class A {\n"
2385                "  int a;\n"
2386                "  A() try : a(0) {\n"
2387                "  } catch (...) {\n"
2388                "    throw;\n"
2389                "  }\n"
2390                "};\n");
2391 
2392   // Incomplete try-catch blocks.
2393   verifyIncompleteFormat("try {} catch (");
2394 }
2395 
2396 TEST_F(FormatTest, FormatSEHTryCatch) {
2397   verifyFormat("__try {\n"
2398                "  int a = b * c;\n"
2399                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2400                "  // Do nothing.\n"
2401                "}");
2402 
2403   verifyFormat("__try {\n"
2404                "  int a = b * c;\n"
2405                "} __finally {\n"
2406                "  // Do nothing.\n"
2407                "}");
2408 
2409   verifyFormat("DEBUG({\n"
2410                "  __try {\n"
2411                "  } __finally {\n"
2412                "  }\n"
2413                "});\n");
2414 }
2415 
2416 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2417   verifyFormat("try {\n"
2418                "  f();\n"
2419                "} catch {\n"
2420                "  g();\n"
2421                "}");
2422   verifyFormat("try {\n"
2423                "  f();\n"
2424                "} catch (A a) MACRO(x) {\n"
2425                "  g();\n"
2426                "} catch (B b) MACRO(x) {\n"
2427                "  g();\n"
2428                "}");
2429 }
2430 
2431 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2432   FormatStyle Style = getLLVMStyle();
2433   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2434                           FormatStyle::BS_WebKit}) {
2435     Style.BreakBeforeBraces = BraceStyle;
2436     verifyFormat("try {\n"
2437                  "  // something\n"
2438                  "} catch (...) {\n"
2439                  "  // something\n"
2440                  "}",
2441                  Style);
2442   }
2443   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2444   verifyFormat("try {\n"
2445                "  // something\n"
2446                "}\n"
2447                "catch (...) {\n"
2448                "  // something\n"
2449                "}",
2450                Style);
2451   verifyFormat("__try {\n"
2452                "  // something\n"
2453                "}\n"
2454                "__finally {\n"
2455                "  // something\n"
2456                "}",
2457                Style);
2458   verifyFormat("@try {\n"
2459                "  // something\n"
2460                "}\n"
2461                "@finally {\n"
2462                "  // something\n"
2463                "}",
2464                Style);
2465   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2466   verifyFormat("try\n"
2467                "{\n"
2468                "  // something\n"
2469                "}\n"
2470                "catch (...)\n"
2471                "{\n"
2472                "  // something\n"
2473                "}",
2474                Style);
2475   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2476   verifyFormat("try\n"
2477                "  {\n"
2478                "    // something\n"
2479                "  }\n"
2480                "catch (...)\n"
2481                "  {\n"
2482                "    // something\n"
2483                "  }",
2484                Style);
2485   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2486   Style.BraceWrapping.BeforeCatch = true;
2487   verifyFormat("try {\n"
2488                "  // something\n"
2489                "}\n"
2490                "catch (...) {\n"
2491                "  // something\n"
2492                "}",
2493                Style);
2494 }
2495 
2496 TEST_F(FormatTest, StaticInitializers) {
2497   verifyFormat("static SomeClass SC = {1, 'a'};");
2498 
2499   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2500                "    100000000, "
2501                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2502 
2503   // Here, everything other than the "}" would fit on a line.
2504   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2505                "    10000000000000000000000000};");
2506   EXPECT_EQ("S s = {a,\n"
2507             "\n"
2508             "       b};",
2509             format("S s = {\n"
2510                    "  a,\n"
2511                    "\n"
2512                    "  b\n"
2513                    "};"));
2514 
2515   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2516   // line. However, the formatting looks a bit off and this probably doesn't
2517   // happen often in practice.
2518   verifyFormat("static int Variable[1] = {\n"
2519                "    {1000000000000000000000000000000000000}};",
2520                getLLVMStyleWithColumns(40));
2521 }
2522 
2523 TEST_F(FormatTest, DesignatedInitializers) {
2524   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2525   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2526                "                    .bbbbbbbbbb = 2,\n"
2527                "                    .cccccccccc = 3,\n"
2528                "                    .dddddddddd = 4,\n"
2529                "                    .eeeeeeeeee = 5};");
2530   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2531                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2532                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2533                "    .ccccccccccccccccccccccccccc = 3,\n"
2534                "    .ddddddddddddddddddddddddddd = 4,\n"
2535                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2536 
2537   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2538 }
2539 
2540 TEST_F(FormatTest, NestedStaticInitializers) {
2541   verifyFormat("static A x = {{{}}};\n");
2542   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2543                "               {init1, init2, init3, init4}}};",
2544                getLLVMStyleWithColumns(50));
2545 
2546   verifyFormat("somes Status::global_reps[3] = {\n"
2547                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2548                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2549                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2550                getLLVMStyleWithColumns(60));
2551   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2552                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2553                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2554                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2555   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2556                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2557                "rect.fTop}};");
2558 
2559   verifyFormat(
2560       "SomeArrayOfSomeType a = {\n"
2561       "    {{1, 2, 3},\n"
2562       "     {1, 2, 3},\n"
2563       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2564       "      333333333333333333333333333333},\n"
2565       "     {1, 2, 3},\n"
2566       "     {1, 2, 3}}};");
2567   verifyFormat(
2568       "SomeArrayOfSomeType a = {\n"
2569       "    {{1, 2, 3}},\n"
2570       "    {{1, 2, 3}},\n"
2571       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2572       "      333333333333333333333333333333}},\n"
2573       "    {{1, 2, 3}},\n"
2574       "    {{1, 2, 3}}};");
2575 
2576   verifyFormat("struct {\n"
2577                "  unsigned bit;\n"
2578                "  const char *const name;\n"
2579                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2580                "                 {kOsWin, \"Windows\"},\n"
2581                "                 {kOsLinux, \"Linux\"},\n"
2582                "                 {kOsCrOS, \"Chrome OS\"}};");
2583   verifyFormat("struct {\n"
2584                "  unsigned bit;\n"
2585                "  const char *const name;\n"
2586                "} kBitsToOs[] = {\n"
2587                "    {kOsMac, \"Mac\"},\n"
2588                "    {kOsWin, \"Windows\"},\n"
2589                "    {kOsLinux, \"Linux\"},\n"
2590                "    {kOsCrOS, \"Chrome OS\"},\n"
2591                "};");
2592 }
2593 
2594 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2595   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2596                "                      \\\n"
2597                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2598 }
2599 
2600 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2601   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2602                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2603 
2604   // Do break defaulted and deleted functions.
2605   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2606                "    default;",
2607                getLLVMStyleWithColumns(40));
2608   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2609                "    delete;",
2610                getLLVMStyleWithColumns(40));
2611 }
2612 
2613 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2614   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2615                getLLVMStyleWithColumns(40));
2616   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2617                getLLVMStyleWithColumns(40));
2618   EXPECT_EQ("#define Q                              \\\n"
2619             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2620             "  \"aaaaaaaa.cpp\"",
2621             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2622                    getLLVMStyleWithColumns(40)));
2623 }
2624 
2625 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2626   EXPECT_EQ("# 123 \"A string literal\"",
2627             format("   #     123    \"A string literal\""));
2628 }
2629 
2630 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2631   EXPECT_EQ("#;", format("#;"));
2632   verifyFormat("#\n;\n;\n;");
2633 }
2634 
2635 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2636   EXPECT_EQ("#line 42 \"test\"\n",
2637             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2638   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2639                                     getLLVMStyleWithColumns(12)));
2640 }
2641 
2642 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2643   EXPECT_EQ("#line 42 \"test\"",
2644             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2645   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2646 }
2647 
2648 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2649   verifyFormat("#define A \\x20");
2650   verifyFormat("#define A \\ x20");
2651   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2652   verifyFormat("#define A ''");
2653   verifyFormat("#define A ''qqq");
2654   verifyFormat("#define A `qqq");
2655   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2656   EXPECT_EQ("const char *c = STRINGIFY(\n"
2657             "\\na : b);",
2658             format("const char * c = STRINGIFY(\n"
2659                    "\\na : b);"));
2660 
2661   verifyFormat("a\r\\");
2662   verifyFormat("a\v\\");
2663   verifyFormat("a\f\\");
2664 }
2665 
2666 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2667   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2668   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2669   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2670   // FIXME: We never break before the macro name.
2671   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2672 
2673   verifyFormat("#define A A\n#define A A");
2674   verifyFormat("#define A(X) A\n#define A A");
2675 
2676   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2677   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2678 }
2679 
2680 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2681   EXPECT_EQ("// somecomment\n"
2682             "#include \"a.h\"\n"
2683             "#define A(  \\\n"
2684             "    A, B)\n"
2685             "#include \"b.h\"\n"
2686             "// somecomment\n",
2687             format("  // somecomment\n"
2688                    "  #include \"a.h\"\n"
2689                    "#define A(A,\\\n"
2690                    "    B)\n"
2691                    "    #include \"b.h\"\n"
2692                    " // somecomment\n",
2693                    getLLVMStyleWithColumns(13)));
2694 }
2695 
2696 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2697 
2698 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2699   EXPECT_EQ("#define A    \\\n"
2700             "  c;         \\\n"
2701             "  e;\n"
2702             "f;",
2703             format("#define A c; e;\n"
2704                    "f;",
2705                    getLLVMStyleWithColumns(14)));
2706 }
2707 
2708 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2709 
2710 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2711   EXPECT_EQ("int x,\n"
2712             "#define A\n"
2713             "    y;",
2714             format("int x,\n#define A\ny;"));
2715 }
2716 
2717 TEST_F(FormatTest, HashInMacroDefinition) {
2718   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2719   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2720   verifyFormat("#define A  \\\n"
2721                "  {        \\\n"
2722                "    f(#c); \\\n"
2723                "  }",
2724                getLLVMStyleWithColumns(11));
2725 
2726   verifyFormat("#define A(X)         \\\n"
2727                "  void function##X()",
2728                getLLVMStyleWithColumns(22));
2729 
2730   verifyFormat("#define A(a, b, c)   \\\n"
2731                "  void a##b##c()",
2732                getLLVMStyleWithColumns(22));
2733 
2734   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2735 }
2736 
2737 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2738   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2739   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2740 }
2741 
2742 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2743   EXPECT_EQ("#define A b;", format("#define A \\\n"
2744                                    "          \\\n"
2745                                    "  b;",
2746                                    getLLVMStyleWithColumns(25)));
2747   EXPECT_EQ("#define A \\\n"
2748             "          \\\n"
2749             "  a;      \\\n"
2750             "  b;",
2751             format("#define A \\\n"
2752                    "          \\\n"
2753                    "  a;      \\\n"
2754                    "  b;",
2755                    getLLVMStyleWithColumns(11)));
2756   EXPECT_EQ("#define A \\\n"
2757             "  a;      \\\n"
2758             "          \\\n"
2759             "  b;",
2760             format("#define A \\\n"
2761                    "  a;      \\\n"
2762                    "          \\\n"
2763                    "  b;",
2764                    getLLVMStyleWithColumns(11)));
2765 }
2766 
2767 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2768   verifyIncompleteFormat("#define A :");
2769   verifyFormat("#define SOMECASES  \\\n"
2770                "  case 1:          \\\n"
2771                "  case 2\n",
2772                getLLVMStyleWithColumns(20));
2773   verifyFormat("#define MACRO(a) \\\n"
2774                "  if (a)         \\\n"
2775                "    f();         \\\n"
2776                "  else           \\\n"
2777                "    g()",
2778                getLLVMStyleWithColumns(18));
2779   verifyFormat("#define A template <typename T>");
2780   verifyIncompleteFormat("#define STR(x) #x\n"
2781                          "f(STR(this_is_a_string_literal{));");
2782   verifyFormat("#pragma omp threadprivate( \\\n"
2783                "    y)), // expected-warning",
2784                getLLVMStyleWithColumns(28));
2785   verifyFormat("#d, = };");
2786   verifyFormat("#if \"a");
2787   verifyIncompleteFormat("({\n"
2788                          "#define b     \\\n"
2789                          "  }           \\\n"
2790                          "  a\n"
2791                          "a",
2792                          getLLVMStyleWithColumns(15));
2793   verifyFormat("#define A     \\\n"
2794                "  {           \\\n"
2795                "    {\n"
2796                "#define B     \\\n"
2797                "  }           \\\n"
2798                "  }",
2799                getLLVMStyleWithColumns(15));
2800   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2801   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2802   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2803   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2804 }
2805 
2806 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2807   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2808   EXPECT_EQ("class A : public QObject {\n"
2809             "  Q_OBJECT\n"
2810             "\n"
2811             "  A() {}\n"
2812             "};",
2813             format("class A  :  public QObject {\n"
2814                    "     Q_OBJECT\n"
2815                    "\n"
2816                    "  A() {\n}\n"
2817                    "}  ;"));
2818   EXPECT_EQ("MACRO\n"
2819             "/*static*/ int i;",
2820             format("MACRO\n"
2821                    " /*static*/ int   i;"));
2822   EXPECT_EQ("SOME_MACRO\n"
2823             "namespace {\n"
2824             "void f();\n"
2825             "}",
2826             format("SOME_MACRO\n"
2827                    "  namespace    {\n"
2828                    "void   f(  );\n"
2829                    "}"));
2830   // Only if the identifier contains at least 5 characters.
2831   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2832   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2833   // Only if everything is upper case.
2834   EXPECT_EQ("class A : public QObject {\n"
2835             "  Q_Object A() {}\n"
2836             "};",
2837             format("class A  :  public QObject {\n"
2838                    "     Q_Object\n"
2839                    "  A() {\n}\n"
2840                    "}  ;"));
2841 
2842   // Only if the next line can actually start an unwrapped line.
2843   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2844             format("SOME_WEIRD_LOG_MACRO\n"
2845                    "<< SomeThing;"));
2846 
2847   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2848                "(n, buffers))\n",
2849                getChromiumStyle(FormatStyle::LK_Cpp));
2850 }
2851 
2852 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2853   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2854             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2855             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2856             "class X {};\n"
2857             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2858             "int *createScopDetectionPass() { return 0; }",
2859             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2860                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2861                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2862                    "  class X {};\n"
2863                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2864                    "  int *createScopDetectionPass() { return 0; }"));
2865   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2866   // braces, so that inner block is indented one level more.
2867   EXPECT_EQ("int q() {\n"
2868             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2869             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2870             "  IPC_END_MESSAGE_MAP()\n"
2871             "}",
2872             format("int q() {\n"
2873                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2874                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2875                    "  IPC_END_MESSAGE_MAP()\n"
2876                    "}"));
2877 
2878   // Same inside macros.
2879   EXPECT_EQ("#define LIST(L) \\\n"
2880             "  L(A)          \\\n"
2881             "  L(B)          \\\n"
2882             "  L(C)",
2883             format("#define LIST(L) \\\n"
2884                    "  L(A) \\\n"
2885                    "  L(B) \\\n"
2886                    "  L(C)",
2887                    getGoogleStyle()));
2888 
2889   // These must not be recognized as macros.
2890   EXPECT_EQ("int q() {\n"
2891             "  f(x);\n"
2892             "  f(x) {}\n"
2893             "  f(x)->g();\n"
2894             "  f(x)->*g();\n"
2895             "  f(x).g();\n"
2896             "  f(x) = x;\n"
2897             "  f(x) += x;\n"
2898             "  f(x) -= x;\n"
2899             "  f(x) *= x;\n"
2900             "  f(x) /= x;\n"
2901             "  f(x) %= x;\n"
2902             "  f(x) &= x;\n"
2903             "  f(x) |= x;\n"
2904             "  f(x) ^= x;\n"
2905             "  f(x) >>= x;\n"
2906             "  f(x) <<= x;\n"
2907             "  f(x)[y].z();\n"
2908             "  LOG(INFO) << x;\n"
2909             "  ifstream(x) >> x;\n"
2910             "}\n",
2911             format("int q() {\n"
2912                    "  f(x)\n;\n"
2913                    "  f(x)\n {}\n"
2914                    "  f(x)\n->g();\n"
2915                    "  f(x)\n->*g();\n"
2916                    "  f(x)\n.g();\n"
2917                    "  f(x)\n = x;\n"
2918                    "  f(x)\n += x;\n"
2919                    "  f(x)\n -= x;\n"
2920                    "  f(x)\n *= x;\n"
2921                    "  f(x)\n /= x;\n"
2922                    "  f(x)\n %= x;\n"
2923                    "  f(x)\n &= x;\n"
2924                    "  f(x)\n |= x;\n"
2925                    "  f(x)\n ^= x;\n"
2926                    "  f(x)\n >>= x;\n"
2927                    "  f(x)\n <<= x;\n"
2928                    "  f(x)\n[y].z();\n"
2929                    "  LOG(INFO)\n << x;\n"
2930                    "  ifstream(x)\n >> x;\n"
2931                    "}\n"));
2932   EXPECT_EQ("int q() {\n"
2933             "  F(x)\n"
2934             "  if (1) {\n"
2935             "  }\n"
2936             "  F(x)\n"
2937             "  while (1) {\n"
2938             "  }\n"
2939             "  F(x)\n"
2940             "  G(x);\n"
2941             "  F(x)\n"
2942             "  try {\n"
2943             "    Q();\n"
2944             "  } catch (...) {\n"
2945             "  }\n"
2946             "}\n",
2947             format("int q() {\n"
2948                    "F(x)\n"
2949                    "if (1) {}\n"
2950                    "F(x)\n"
2951                    "while (1) {}\n"
2952                    "F(x)\n"
2953                    "G(x);\n"
2954                    "F(x)\n"
2955                    "try { Q(); } catch (...) {}\n"
2956                    "}\n"));
2957   EXPECT_EQ("class A {\n"
2958             "  A() : t(0) {}\n"
2959             "  A(int i) noexcept() : {}\n"
2960             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2961             "  try : t(0) {\n"
2962             "  } catch (...) {\n"
2963             "  }\n"
2964             "};",
2965             format("class A {\n"
2966                    "  A()\n : t(0) {}\n"
2967                    "  A(int i)\n noexcept() : {}\n"
2968                    "  A(X x)\n"
2969                    "  try : t(0) {} catch (...) {}\n"
2970                    "};"));
2971   EXPECT_EQ("class SomeClass {\n"
2972             "public:\n"
2973             "  SomeClass() 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   EXPECT_EQ("class SomeClass {\n"
2981             "public:\n"
2982             "  SomeClass()\n"
2983             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2984             "};",
2985             format("class SomeClass {\n"
2986                    "public:\n"
2987                    "  SomeClass()\n"
2988                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2989                    "};",
2990                    getLLVMStyleWithColumns(40)));
2991 
2992   verifyFormat("MACRO(>)");
2993 }
2994 
2995 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2996   verifyFormat("#define A \\\n"
2997                "  f({     \\\n"
2998                "    g();  \\\n"
2999                "  });",
3000                getLLVMStyleWithColumns(11));
3001 }
3002 
3003 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
3004   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
3005 }
3006 
3007 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3008   verifyFormat("{\n  { a #c; }\n}");
3009 }
3010 
3011 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3012   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3013             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3014   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3015             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3016 }
3017 
3018 TEST_F(FormatTest, EscapedNewlines) {
3019   EXPECT_EQ(
3020       "#define A \\\n  int i;  \\\n  int j;",
3021       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
3022   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3023   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3024   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
3025   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3026 }
3027 
3028 TEST_F(FormatTest, DontCrashOnBlockComments) {
3029   EXPECT_EQ(
3030       "int xxxxxxxxx; /* "
3031       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
3032       "zzzzzz\n"
3033       "0*/",
3034       format("int xxxxxxxxx;                          /* "
3035              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
3036              "0*/"));
3037 }
3038 
3039 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3040   verifyFormat("#define A \\\n"
3041                "  int v(  \\\n"
3042                "      a); \\\n"
3043                "  int i;",
3044                getLLVMStyleWithColumns(11));
3045 }
3046 
3047 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3048   EXPECT_EQ(
3049       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3050       "                      \\\n"
3051       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3052       "\n"
3053       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3054       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3055       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3056              "\\\n"
3057              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3058              "  \n"
3059              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3060              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3061 }
3062 
3063 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3064   EXPECT_EQ("int\n"
3065             "#define A\n"
3066             "    a;",
3067             format("int\n#define A\na;"));
3068   verifyFormat("functionCallTo(\n"
3069                "    someOtherFunction(\n"
3070                "        withSomeParameters, whichInSequence,\n"
3071                "        areLongerThanALine(andAnotherCall,\n"
3072                "#define A B\n"
3073                "                           withMoreParamters,\n"
3074                "                           whichStronglyInfluenceTheLayout),\n"
3075                "        andMoreParameters),\n"
3076                "    trailing);",
3077                getLLVMStyleWithColumns(69));
3078   verifyFormat("Foo::Foo()\n"
3079                "#ifdef BAR\n"
3080                "    : baz(0)\n"
3081                "#endif\n"
3082                "{\n"
3083                "}");
3084   verifyFormat("void f() {\n"
3085                "  if (true)\n"
3086                "#ifdef A\n"
3087                "    f(42);\n"
3088                "  x();\n"
3089                "#else\n"
3090                "    g();\n"
3091                "  x();\n"
3092                "#endif\n"
3093                "}");
3094   verifyFormat("void f(param1, param2,\n"
3095                "       param3,\n"
3096                "#ifdef A\n"
3097                "       param4(param5,\n"
3098                "#ifdef A1\n"
3099                "              param6,\n"
3100                "#ifdef A2\n"
3101                "              param7),\n"
3102                "#else\n"
3103                "              param8),\n"
3104                "       param9,\n"
3105                "#endif\n"
3106                "       param10,\n"
3107                "#endif\n"
3108                "       param11)\n"
3109                "#else\n"
3110                "       param12)\n"
3111                "#endif\n"
3112                "{\n"
3113                "  x();\n"
3114                "}",
3115                getLLVMStyleWithColumns(28));
3116   verifyFormat("#if 1\n"
3117                "int i;");
3118   verifyFormat("#if 1\n"
3119                "#endif\n"
3120                "#if 1\n"
3121                "#else\n"
3122                "#endif\n");
3123   verifyFormat("DEBUG({\n"
3124                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3125                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3126                "});\n"
3127                "#if a\n"
3128                "#else\n"
3129                "#endif");
3130 
3131   verifyIncompleteFormat("void f(\n"
3132                          "#if A\n"
3133                          "    );\n"
3134                          "#else\n"
3135                          "#endif");
3136 }
3137 
3138 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3139   verifyFormat("#endif\n"
3140                "#if B");
3141 }
3142 
3143 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3144   FormatStyle SingleLine = getLLVMStyle();
3145   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3146   verifyFormat("#if 0\n"
3147                "#elif 1\n"
3148                "#endif\n"
3149                "void foo() {\n"
3150                "  if (test) foo2();\n"
3151                "}",
3152                SingleLine);
3153 }
3154 
3155 TEST_F(FormatTest, LayoutBlockInsideParens) {
3156   verifyFormat("functionCall({ int i; });");
3157   verifyFormat("functionCall({\n"
3158                "  int i;\n"
3159                "  int j;\n"
3160                "});");
3161   verifyFormat("functionCall(\n"
3162                "    {\n"
3163                "      int i;\n"
3164                "      int j;\n"
3165                "    },\n"
3166                "    aaaa, bbbb, cccc);");
3167   verifyFormat("functionA(functionB({\n"
3168                "            int i;\n"
3169                "            int j;\n"
3170                "          }),\n"
3171                "          aaaa, bbbb, cccc);");
3172   verifyFormat("functionCall(\n"
3173                "    {\n"
3174                "      int i;\n"
3175                "      int j;\n"
3176                "    },\n"
3177                "    aaaa, bbbb, // comment\n"
3178                "    cccc);");
3179   verifyFormat("functionA(functionB({\n"
3180                "            int i;\n"
3181                "            int j;\n"
3182                "          }),\n"
3183                "          aaaa, bbbb, // comment\n"
3184                "          cccc);");
3185   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3186   verifyFormat("functionCall(aaaa, bbbb, {\n"
3187                "  int i;\n"
3188                "  int j;\n"
3189                "});");
3190   verifyFormat(
3191       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3192       "    {\n"
3193       "      int i; // break\n"
3194       "    },\n"
3195       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3196       "                                     ccccccccccccccccc));");
3197   verifyFormat("DEBUG({\n"
3198                "  if (a)\n"
3199                "    f();\n"
3200                "});");
3201 }
3202 
3203 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3204   EXPECT_EQ("SOME_MACRO { int i; }\n"
3205             "int i;",
3206             format("  SOME_MACRO  {int i;}  int i;"));
3207 }
3208 
3209 TEST_F(FormatTest, LayoutNestedBlocks) {
3210   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3211                "  struct s {\n"
3212                "    int i;\n"
3213                "  };\n"
3214                "  s kBitsToOs[] = {{10}};\n"
3215                "  for (int i = 0; i < 10; ++i)\n"
3216                "    return;\n"
3217                "}");
3218   verifyFormat("call(parameter, {\n"
3219                "  something();\n"
3220                "  // Comment using all columns.\n"
3221                "  somethingelse();\n"
3222                "});",
3223                getLLVMStyleWithColumns(40));
3224   verifyFormat("DEBUG( //\n"
3225                "    { f(); }, a);");
3226   verifyFormat("DEBUG( //\n"
3227                "    {\n"
3228                "      f(); //\n"
3229                "    },\n"
3230                "    a);");
3231 
3232   EXPECT_EQ("call(parameter, {\n"
3233             "  something();\n"
3234             "  // Comment too\n"
3235             "  // looooooooooong.\n"
3236             "  somethingElse();\n"
3237             "});",
3238             format("call(parameter, {\n"
3239                    "  something();\n"
3240                    "  // Comment too looooooooooong.\n"
3241                    "  somethingElse();\n"
3242                    "});",
3243                    getLLVMStyleWithColumns(29)));
3244   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3245   EXPECT_EQ("DEBUG({ // comment\n"
3246             "  int i;\n"
3247             "});",
3248             format("DEBUG({ // comment\n"
3249                    "int  i;\n"
3250                    "});"));
3251   EXPECT_EQ("DEBUG({\n"
3252             "  int i;\n"
3253             "\n"
3254             "  // comment\n"
3255             "  int j;\n"
3256             "});",
3257             format("DEBUG({\n"
3258                    "  int  i;\n"
3259                    "\n"
3260                    "  // comment\n"
3261                    "  int  j;\n"
3262                    "});"));
3263 
3264   verifyFormat("DEBUG({\n"
3265                "  if (a)\n"
3266                "    return;\n"
3267                "});");
3268   verifyGoogleFormat("DEBUG({\n"
3269                      "  if (a) return;\n"
3270                      "});");
3271   FormatStyle Style = getGoogleStyle();
3272   Style.ColumnLimit = 45;
3273   verifyFormat("Debug(aaaaa,\n"
3274                "      {\n"
3275                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3276                "      },\n"
3277                "      a);",
3278                Style);
3279 
3280   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3281 
3282   verifyNoCrash("^{v^{a}}");
3283 }
3284 
3285 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3286   EXPECT_EQ("#define MACRO()                     \\\n"
3287             "  Debug(aaa, /* force line break */ \\\n"
3288             "        {                           \\\n"
3289             "          int i;                    \\\n"
3290             "          int j;                    \\\n"
3291             "        })",
3292             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3293                    "          {  int   i;  int  j;   })",
3294                    getGoogleStyle()));
3295 
3296   EXPECT_EQ("#define A                                       \\\n"
3297             "  [] {                                          \\\n"
3298             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3299             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3300             "  }",
3301             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3302                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3303                    getGoogleStyle()));
3304 }
3305 
3306 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3307   EXPECT_EQ("{}", format("{}"));
3308   verifyFormat("enum E {};");
3309   verifyFormat("enum E {}");
3310 }
3311 
3312 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3313   FormatStyle Style = getLLVMStyle();
3314   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3315   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3316   verifyFormat("FOO_BEGIN\n"
3317                "  FOO_ENTRY\n"
3318                "FOO_END", Style);
3319   verifyFormat("FOO_BEGIN\n"
3320                "  NESTED_FOO_BEGIN\n"
3321                "    NESTED_FOO_ENTRY\n"
3322                "  NESTED_FOO_END\n"
3323                "FOO_END", Style);
3324   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3325                "  int x;\n"
3326                "  x = 1;\n"
3327                "FOO_END(Baz)", Style);
3328 }
3329 
3330 //===----------------------------------------------------------------------===//
3331 // Line break tests.
3332 //===----------------------------------------------------------------------===//
3333 
3334 TEST_F(FormatTest, PreventConfusingIndents) {
3335   verifyFormat(
3336       "void f() {\n"
3337       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3338       "                         parameter, parameter, parameter)),\n"
3339       "                     SecondLongCall(parameter));\n"
3340       "}");
3341   verifyFormat(
3342       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3343       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3344       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3345       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3346   verifyFormat(
3347       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3348       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3349       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3350       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3351   verifyFormat(
3352       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3353       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3354       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3355       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3356   verifyFormat("int a = bbbb && ccc && fffff(\n"
3357                "#define A Just forcing a new line\n"
3358                "                           ddd);");
3359 }
3360 
3361 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3362   verifyFormat(
3363       "bool aaaaaaa =\n"
3364       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3365       "    bbbbbbbb();");
3366   verifyFormat(
3367       "bool aaaaaaa =\n"
3368       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3369       "    bbbbbbbb();");
3370 
3371   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3372                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3373                "    ccccccccc == ddddddddddd;");
3374   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3375                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3376                "    ccccccccc == ddddddddddd;");
3377   verifyFormat(
3378       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3379       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3380       "    ccccccccc == ddddddddddd;");
3381 
3382   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3383                "                 aaaaaa) &&\n"
3384                "         bbbbbb && cccccc;");
3385   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3386                "                 aaaaaa) >>\n"
3387                "         bbbbbb;");
3388   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3389                "    SourceMgr.getSpellingColumnNumber(\n"
3390                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3391                "    1);");
3392 
3393   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3394                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3395                "    cccccc) {\n}");
3396   verifyFormat("b = a &&\n"
3397                "    // Comment\n"
3398                "    b.c && d;");
3399 
3400   // If the LHS of a comparison is not a binary expression itself, the
3401   // additional linebreak confuses many people.
3402   verifyFormat(
3403       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3404       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3405       "}");
3406   verifyFormat(
3407       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3408       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3409       "}");
3410   verifyFormat(
3411       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3412       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3413       "}");
3414   // Even explicit parentheses stress the precedence enough to make the
3415   // additional break unnecessary.
3416   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3417                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3418                "}");
3419   // This cases is borderline, but with the indentation it is still readable.
3420   verifyFormat(
3421       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3422       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3423       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3424       "}",
3425       getLLVMStyleWithColumns(75));
3426 
3427   // If the LHS is a binary expression, we should still use the additional break
3428   // as otherwise the formatting hides the operator precedence.
3429   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3430                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3431                "    5) {\n"
3432                "}");
3433 
3434   FormatStyle OnePerLine = getLLVMStyle();
3435   OnePerLine.BinPackParameters = false;
3436   verifyFormat(
3437       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3438       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3439       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3440       OnePerLine);
3441 }
3442 
3443 TEST_F(FormatTest, ExpressionIndentation) {
3444   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3445                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3446                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3447                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3448                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3449                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3450                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3451                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3452                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3453   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3454                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3455                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3456                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3457   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3458                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3459                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3460                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3461   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3462                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3463                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3464                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3465   verifyFormat("if () {\n"
3466                "} else if (aaaaa &&\n"
3467                "           bbbbb > // break\n"
3468                "               ccccc) {\n"
3469                "}");
3470 
3471   // Presence of a trailing comment used to change indentation of b.
3472   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3473                "       b;\n"
3474                "return aaaaaaaaaaaaaaaaaaa +\n"
3475                "       b; //",
3476                getLLVMStyleWithColumns(30));
3477 }
3478 
3479 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3480   // Not sure what the best system is here. Like this, the LHS can be found
3481   // immediately above an operator (everything with the same or a higher
3482   // indent). The RHS is aligned right of the operator and so compasses
3483   // everything until something with the same indent as the operator is found.
3484   // FIXME: Is this a good system?
3485   FormatStyle Style = getLLVMStyle();
3486   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3487   verifyFormat(
3488       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3489       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3490       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3491       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3492       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3493       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3494       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3495       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3496       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3497       Style);
3498   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3499                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3500                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3501                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3502                Style);
3503   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3504                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3505                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3506                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3507                Style);
3508   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3509                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3510                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3511                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3512                Style);
3513   verifyFormat("if () {\n"
3514                "} else if (aaaaa\n"
3515                "           && bbbbb // break\n"
3516                "                  > ccccc) {\n"
3517                "}",
3518                Style);
3519   verifyFormat("return (a)\n"
3520                "       // comment\n"
3521                "       + b;",
3522                Style);
3523   verifyFormat(
3524       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3525       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3526       "             + cc;",
3527       Style);
3528 
3529   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3530                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3531                Style);
3532 
3533   // Forced by comments.
3534   verifyFormat(
3535       "unsigned ContentSize =\n"
3536       "    sizeof(int16_t)   // DWARF ARange version number\n"
3537       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3538       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3539       "    + sizeof(int8_t); // Segment Size (in bytes)");
3540 
3541   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3542                "       == boost::fusion::at_c<1>(iiii).second;",
3543                Style);
3544 
3545   Style.ColumnLimit = 60;
3546   verifyFormat("zzzzzzzzzz\n"
3547                "    = bbbbbbbbbbbbbbbbb\n"
3548                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3549                Style);
3550 }
3551 
3552 TEST_F(FormatTest, NoOperandAlignment) {
3553   FormatStyle Style = getLLVMStyle();
3554   Style.AlignOperands = false;
3555   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3556   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3557                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3558                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3559                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3560                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3561                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3562                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3563                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3564                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3565                Style);
3566 
3567   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3568                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3569                "    + cc;",
3570                Style);
3571   verifyFormat("int a = aa\n"
3572                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3573                "        * cccccccccccccccccccccccccccccccccccc;",
3574                Style);
3575 
3576   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3577   verifyFormat("return (a > b\n"
3578                "    // comment1\n"
3579                "    // comment2\n"
3580                "    || c);",
3581                Style);
3582 }
3583 
3584 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3585   FormatStyle Style = getLLVMStyle();
3586   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3587   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3588                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3589                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3590                Style);
3591 }
3592 
3593 TEST_F(FormatTest, ConstructorInitializers) {
3594   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3595   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3596                getLLVMStyleWithColumns(45));
3597   verifyFormat("Constructor()\n"
3598                "    : Inttializer(FitsOnTheLine) {}",
3599                getLLVMStyleWithColumns(44));
3600   verifyFormat("Constructor()\n"
3601                "    : Inttializer(FitsOnTheLine) {}",
3602                getLLVMStyleWithColumns(43));
3603 
3604   verifyFormat("template <typename T>\n"
3605                "Constructor() : Initializer(FitsOnTheLine) {}",
3606                getLLVMStyleWithColumns(45));
3607 
3608   verifyFormat(
3609       "SomeClass::Constructor()\n"
3610       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3611 
3612   verifyFormat(
3613       "SomeClass::Constructor()\n"
3614       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3615       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3616   verifyFormat(
3617       "SomeClass::Constructor()\n"
3618       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3619       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3620   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3621                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3622                "    : aaaaaaaaaa(aaaaaa) {}");
3623 
3624   verifyFormat("Constructor()\n"
3625                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3626                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3627                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3628                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3629 
3630   verifyFormat("Constructor()\n"
3631                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3632                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3633 
3634   verifyFormat("Constructor(int Parameter = 0)\n"
3635                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3636                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3637   verifyFormat("Constructor()\n"
3638                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3639                "}",
3640                getLLVMStyleWithColumns(60));
3641   verifyFormat("Constructor()\n"
3642                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3643                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3644 
3645   // Here a line could be saved by splitting the second initializer onto two
3646   // lines, but that is not desirable.
3647   verifyFormat("Constructor()\n"
3648                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3649                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3650                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3651 
3652   FormatStyle OnePerLine = getLLVMStyle();
3653   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3654   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3655   verifyFormat("SomeClass::Constructor()\n"
3656                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3657                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3658                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3659                OnePerLine);
3660   verifyFormat("SomeClass::Constructor()\n"
3661                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3662                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3663                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3664                OnePerLine);
3665   verifyFormat("MyClass::MyClass(int var)\n"
3666                "    : some_var_(var),            // 4 space indent\n"
3667                "      some_other_var_(var + 1) { // lined up\n"
3668                "}",
3669                OnePerLine);
3670   verifyFormat("Constructor()\n"
3671                "    : aaaaa(aaaaaa),\n"
3672                "      aaaaa(aaaaaa),\n"
3673                "      aaaaa(aaaaaa),\n"
3674                "      aaaaa(aaaaaa),\n"
3675                "      aaaaa(aaaaaa) {}",
3676                OnePerLine);
3677   verifyFormat("Constructor()\n"
3678                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3679                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3680                OnePerLine);
3681   OnePerLine.BinPackParameters = false;
3682   verifyFormat(
3683       "Constructor()\n"
3684       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3685       "          aaaaaaaaaaa().aaa(),\n"
3686       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3687       OnePerLine);
3688   OnePerLine.ColumnLimit = 60;
3689   verifyFormat("Constructor()\n"
3690                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3691                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3692                OnePerLine);
3693 
3694   EXPECT_EQ("Constructor()\n"
3695             "    : // Comment forcing unwanted break.\n"
3696             "      aaaa(aaaa) {}",
3697             format("Constructor() :\n"
3698                    "    // Comment forcing unwanted break.\n"
3699                    "    aaaa(aaaa) {}"));
3700 }
3701 
3702 TEST_F(FormatTest, MemoizationTests) {
3703   // This breaks if the memoization lookup does not take \c Indent and
3704   // \c LastSpace into account.
3705   verifyFormat(
3706       "extern CFRunLoopTimerRef\n"
3707       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3708       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3709       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3710       "                     CFRunLoopTimerContext *context) {}");
3711 
3712   // Deep nesting somewhat works around our memoization.
3713   verifyFormat(
3714       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3715       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3716       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3717       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3718       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3719       getLLVMStyleWithColumns(65));
3720   verifyFormat(
3721       "aaaaa(\n"
3722       "    aaaaa,\n"
3723       "    aaaaa(\n"
3724       "        aaaaa,\n"
3725       "        aaaaa(\n"
3726       "            aaaaa,\n"
3727       "            aaaaa(\n"
3728       "                aaaaa,\n"
3729       "                aaaaa(\n"
3730       "                    aaaaa,\n"
3731       "                    aaaaa(\n"
3732       "                        aaaaa,\n"
3733       "                        aaaaa(\n"
3734       "                            aaaaa,\n"
3735       "                            aaaaa(\n"
3736       "                                aaaaa,\n"
3737       "                                aaaaa(\n"
3738       "                                    aaaaa,\n"
3739       "                                    aaaaa(\n"
3740       "                                        aaaaa,\n"
3741       "                                        aaaaa(\n"
3742       "                                            aaaaa,\n"
3743       "                                            aaaaa(\n"
3744       "                                                aaaaa,\n"
3745       "                                                aaaaa))))))))))));",
3746       getLLVMStyleWithColumns(65));
3747   verifyFormat(
3748       "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"
3749       "                                  a),\n"
3750       "                                a),\n"
3751       "                              a),\n"
3752       "                            a),\n"
3753       "                          a),\n"
3754       "                        a),\n"
3755       "                      a),\n"
3756       "                    a),\n"
3757       "                  a),\n"
3758       "                a),\n"
3759       "              a),\n"
3760       "            a),\n"
3761       "          a),\n"
3762       "        a),\n"
3763       "      a),\n"
3764       "    a),\n"
3765       "  a)",
3766       getLLVMStyleWithColumns(65));
3767 
3768   // This test takes VERY long when memoization is broken.
3769   FormatStyle OnePerLine = getLLVMStyle();
3770   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3771   OnePerLine.BinPackParameters = false;
3772   std::string input = "Constructor()\n"
3773                       "    : aaaa(a,\n";
3774   for (unsigned i = 0, e = 80; i != e; ++i) {
3775     input += "           a,\n";
3776   }
3777   input += "           a) {}";
3778   verifyFormat(input, OnePerLine);
3779 }
3780 
3781 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3782   verifyFormat(
3783       "void f() {\n"
3784       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3785       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3786       "    f();\n"
3787       "}");
3788   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3789                "    Intervals[i - 1].getRange().getLast()) {\n}");
3790 }
3791 
3792 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3793   // Principially, we break function declarations in a certain order:
3794   // 1) break amongst arguments.
3795   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3796                "                              Cccccccccccccc cccccccccccccc);");
3797   verifyFormat("template <class TemplateIt>\n"
3798                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3799                "                            TemplateIt *stop) {}");
3800 
3801   // 2) break after return type.
3802   verifyFormat(
3803       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3804       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3805       getGoogleStyle());
3806 
3807   // 3) break after (.
3808   verifyFormat(
3809       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3810       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3811       getGoogleStyle());
3812 
3813   // 4) break before after nested name specifiers.
3814   verifyFormat(
3815       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3816       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3817       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3818       getGoogleStyle());
3819 
3820   // However, there are exceptions, if a sufficient amount of lines can be
3821   // saved.
3822   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3823   // more adjusting.
3824   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3825                "                                  Cccccccccccccc cccccccccc,\n"
3826                "                                  Cccccccccccccc cccccccccc,\n"
3827                "                                  Cccccccccccccc cccccccccc,\n"
3828                "                                  Cccccccccccccc cccccccccc);");
3829   verifyFormat(
3830       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3831       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3832       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3833       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3834       getGoogleStyle());
3835   verifyFormat(
3836       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3837       "                                          Cccccccccccccc cccccccccc,\n"
3838       "                                          Cccccccccccccc cccccccccc,\n"
3839       "                                          Cccccccccccccc cccccccccc,\n"
3840       "                                          Cccccccccccccc cccccccccc,\n"
3841       "                                          Cccccccccccccc cccccccccc,\n"
3842       "                                          Cccccccccccccc cccccccccc);");
3843   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3844                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3845                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3846                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3847                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3848 
3849   // Break after multi-line parameters.
3850   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3851                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3852                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3853                "    bbbb bbbb);");
3854   verifyFormat("void SomeLoooooooooooongFunction(\n"
3855                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3856                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3857                "    int bbbbbbbbbbbbb);");
3858 
3859   // Treat overloaded operators like other functions.
3860   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3861                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3862   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3863                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3864   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3865                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3866   verifyGoogleFormat(
3867       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3868       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3869   verifyGoogleFormat(
3870       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3871       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3872   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3873                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3874   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3875                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3876   verifyGoogleFormat(
3877       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3878       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3879       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3880   verifyGoogleFormat(
3881       "template <typename T>\n"
3882       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3883       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3884       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3885 
3886   FormatStyle Style = getLLVMStyle();
3887   Style.PointerAlignment = FormatStyle::PAS_Left;
3888   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3889                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3890                Style);
3891   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3892                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3893                Style);
3894 }
3895 
3896 TEST_F(FormatTest, TrailingReturnType) {
3897   verifyFormat("auto foo() -> int;\n");
3898   verifyFormat("struct S {\n"
3899                "  auto bar() const -> int;\n"
3900                "};");
3901   verifyFormat("template <size_t Order, typename T>\n"
3902                "auto load_img(const std::string &filename)\n"
3903                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3904   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3905                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3906   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3907   verifyFormat("template <typename T>\n"
3908                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3909                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3910 
3911   // Not trailing return types.
3912   verifyFormat("void f() { auto a = b->c(); }");
3913 }
3914 
3915 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3916   // Avoid breaking before trailing 'const' or other trailing annotations, if
3917   // they are not function-like.
3918   FormatStyle Style = getGoogleStyle();
3919   Style.ColumnLimit = 47;
3920   verifyFormat("void someLongFunction(\n"
3921                "    int someLoooooooooooooongParameter) const {\n}",
3922                getLLVMStyleWithColumns(47));
3923   verifyFormat("LoooooongReturnType\n"
3924                "someLoooooooongFunction() const {}",
3925                getLLVMStyleWithColumns(47));
3926   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3927                "    const {}",
3928                Style);
3929   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3930                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3931   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3932                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3933   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3934                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3935   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3936                "                   aaaaaaaaaaa aaaaa) const override;");
3937   verifyGoogleFormat(
3938       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3939       "    const override;");
3940 
3941   // Even if the first parameter has to be wrapped.
3942   verifyFormat("void someLongFunction(\n"
3943                "    int someLongParameter) const {}",
3944                getLLVMStyleWithColumns(46));
3945   verifyFormat("void someLongFunction(\n"
3946                "    int someLongParameter) const {}",
3947                Style);
3948   verifyFormat("void someLongFunction(\n"
3949                "    int someLongParameter) override {}",
3950                Style);
3951   verifyFormat("void someLongFunction(\n"
3952                "    int someLongParameter) OVERRIDE {}",
3953                Style);
3954   verifyFormat("void someLongFunction(\n"
3955                "    int someLongParameter) final {}",
3956                Style);
3957   verifyFormat("void someLongFunction(\n"
3958                "    int someLongParameter) FINAL {}",
3959                Style);
3960   verifyFormat("void someLongFunction(\n"
3961                "    int parameter) const override {}",
3962                Style);
3963 
3964   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3965   verifyFormat("void someLongFunction(\n"
3966                "    int someLongParameter) const\n"
3967                "{\n"
3968                "}",
3969                Style);
3970 
3971   // Unless these are unknown annotations.
3972   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3973                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3974                "    LONG_AND_UGLY_ANNOTATION;");
3975 
3976   // Breaking before function-like trailing annotations is fine to keep them
3977   // close to their arguments.
3978   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3979                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3980   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3981                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3982   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3983                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3984   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3985                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3986   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3987 
3988   verifyFormat(
3989       "void aaaaaaaaaaaaaaaaaa()\n"
3990       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3991       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3992   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3993                "    __attribute__((unused));");
3994   verifyGoogleFormat(
3995       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3996       "    GUARDED_BY(aaaaaaaaaaaa);");
3997   verifyGoogleFormat(
3998       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3999       "    GUARDED_BY(aaaaaaaaaaaa);");
4000   verifyGoogleFormat(
4001       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4002       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4003   verifyGoogleFormat(
4004       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4005       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4006 }
4007 
4008 TEST_F(FormatTest, FunctionAnnotations) {
4009   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4010                "int OldFunction(const string &parameter) {}");
4011   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4012                "string OldFunction(const string &parameter) {}");
4013   verifyFormat("template <typename T>\n"
4014                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4015                "string OldFunction(const string &parameter) {}");
4016 
4017   // Not function annotations.
4018   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4019                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4020   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4021                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4022   verifyFormat("MACRO(abc).function() // wrap\n"
4023                "    << abc;");
4024   verifyFormat("MACRO(abc)->function() // wrap\n"
4025                "    << abc;");
4026   verifyFormat("MACRO(abc)::function() // wrap\n"
4027                "    << abc;");
4028 }
4029 
4030 TEST_F(FormatTest, BreaksDesireably) {
4031   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4032                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4033                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4034   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4035                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4036                "}");
4037 
4038   verifyFormat(
4039       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4040       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4041 
4042   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4043                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4044                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4045 
4046   verifyFormat(
4047       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4048       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4049       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4050       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4051 
4052   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4053                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4054 
4055   verifyFormat(
4056       "void f() {\n"
4057       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4058       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4059       "}");
4060   verifyFormat(
4061       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4062       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4063   verifyFormat(
4064       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4065       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4066   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4067                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4068                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4069 
4070   // Indent consistently independent of call expression and unary operator.
4071   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4072                "    dddddddddddddddddddddddddddddd));");
4073   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4074                "    dddddddddddddddddddddddddddddd));");
4075   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4076                "    dddddddddddddddddddddddddddddd));");
4077 
4078   // This test case breaks on an incorrect memoization, i.e. an optimization not
4079   // taking into account the StopAt value.
4080   verifyFormat(
4081       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4082       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4083       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4084       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4085 
4086   verifyFormat("{\n  {\n    {\n"
4087                "      Annotation.SpaceRequiredBefore =\n"
4088                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4089                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4090                "    }\n  }\n}");
4091 
4092   // Break on an outer level if there was a break on an inner level.
4093   EXPECT_EQ("f(g(h(a, // comment\n"
4094             "      b, c),\n"
4095             "    d, e),\n"
4096             "  x, y);",
4097             format("f(g(h(a, // comment\n"
4098                    "    b, c), d, e), x, y);"));
4099 
4100   // Prefer breaking similar line breaks.
4101   verifyFormat(
4102       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4103       "                             NSTrackingMouseEnteredAndExited |\n"
4104       "                             NSTrackingActiveAlways;");
4105 }
4106 
4107 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4108   FormatStyle NoBinPacking = getGoogleStyle();
4109   NoBinPacking.BinPackParameters = false;
4110   NoBinPacking.BinPackArguments = true;
4111   verifyFormat("void f() {\n"
4112                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4113                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4114                "}",
4115                NoBinPacking);
4116   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4117                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4118                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4119                NoBinPacking);
4120 
4121   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4122   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4123                "                        vector<int> bbbbbbbbbbbbbbb);",
4124                NoBinPacking);
4125   // FIXME: This behavior difference is probably not wanted. However, currently
4126   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4127   // template arguments from BreakBeforeParameter being set because of the
4128   // one-per-line formatting.
4129   verifyFormat(
4130       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4131       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4132       NoBinPacking);
4133   verifyFormat(
4134       "void fffffffffff(\n"
4135       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4136       "        aaaaaaaaaa);");
4137 }
4138 
4139 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4140   FormatStyle NoBinPacking = getGoogleStyle();
4141   NoBinPacking.BinPackParameters = false;
4142   NoBinPacking.BinPackArguments = false;
4143   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4144                "  aaaaaaaaaaaaaaaaaaaa,\n"
4145                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4146                NoBinPacking);
4147   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4148                "        aaaaaaaaaaaaa,\n"
4149                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4150                NoBinPacking);
4151   verifyFormat(
4152       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4153       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4154       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4155       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4156       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4157       NoBinPacking);
4158   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4159                "    .aaaaaaaaaaaaaaaaaa();",
4160                NoBinPacking);
4161   verifyFormat("void f() {\n"
4162                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4163                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4164                "}",
4165                NoBinPacking);
4166 
4167   verifyFormat(
4168       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4169       "             aaaaaaaaaaaa,\n"
4170       "             aaaaaaaaaaaa);",
4171       NoBinPacking);
4172   verifyFormat(
4173       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4174       "                               ddddddddddddddddddddddddddddd),\n"
4175       "             test);",
4176       NoBinPacking);
4177 
4178   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4179                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4180                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4181                "    aaaaaaaaaaaaaaaaaa;",
4182                NoBinPacking);
4183   verifyFormat("a(\"a\"\n"
4184                "  \"a\",\n"
4185                "  a);");
4186 
4187   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4188   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4189                "                aaaaaaaaa,\n"
4190                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4191                NoBinPacking);
4192   verifyFormat(
4193       "void f() {\n"
4194       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4195       "      .aaaaaaa();\n"
4196       "}",
4197       NoBinPacking);
4198   verifyFormat(
4199       "template <class SomeType, class SomeOtherType>\n"
4200       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4201       NoBinPacking);
4202 }
4203 
4204 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4205   FormatStyle Style = getLLVMStyleWithColumns(15);
4206   Style.ExperimentalAutoDetectBinPacking = true;
4207   EXPECT_EQ("aaa(aaaa,\n"
4208             "    aaaa,\n"
4209             "    aaaa);\n"
4210             "aaa(aaaa,\n"
4211             "    aaaa,\n"
4212             "    aaaa);",
4213             format("aaa(aaaa,\n" // one-per-line
4214                    "  aaaa,\n"
4215                    "    aaaa  );\n"
4216                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4217                    Style));
4218   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4219             "    aaaa);\n"
4220             "aaa(aaaa, aaaa,\n"
4221             "    aaaa);",
4222             format("aaa(aaaa,  aaaa,\n" // bin-packed
4223                    "    aaaa  );\n"
4224                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4225                    Style));
4226 }
4227 
4228 TEST_F(FormatTest, FormatsBuilderPattern) {
4229   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4230                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4231                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4232                "    .StartsWith(\".init\", ORDER_INIT)\n"
4233                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4234                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4235                "    .Default(ORDER_TEXT);\n");
4236 
4237   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4238                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4239   verifyFormat(
4240       "aaaaaaa->aaaaaaa\n"
4241       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4242       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4243       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4244   verifyFormat(
4245       "aaaaaaa->aaaaaaa\n"
4246       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4247       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4248   verifyFormat(
4249       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4250       "    aaaaaaaaaaaaaa);");
4251   verifyFormat(
4252       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4253       "    aaaaaa->aaaaaaaaaaaa()\n"
4254       "        ->aaaaaaaaaaaaaaaa(\n"
4255       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4256       "        ->aaaaaaaaaaaaaaaaa();");
4257   verifyGoogleFormat(
4258       "void f() {\n"
4259       "  someo->Add((new util::filetools::Handler(dir))\n"
4260       "                 ->OnEvent1(NewPermanentCallback(\n"
4261       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4262       "                 ->OnEvent2(NewPermanentCallback(\n"
4263       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4264       "                 ->OnEvent3(NewPermanentCallback(\n"
4265       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4266       "                 ->OnEvent5(NewPermanentCallback(\n"
4267       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4268       "                 ->OnEvent6(NewPermanentCallback(\n"
4269       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4270       "}");
4271 
4272   verifyFormat(
4273       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4274   verifyFormat("aaaaaaaaaaaaaaa()\n"
4275                "    .aaaaaaaaaaaaaaa()\n"
4276                "    .aaaaaaaaaaaaaaa()\n"
4277                "    .aaaaaaaaaaaaaaa()\n"
4278                "    .aaaaaaaaaaaaaaa();");
4279   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4280                "    .aaaaaaaaaaaaaaa()\n"
4281                "    .aaaaaaaaaaaaaaa()\n"
4282                "    .aaaaaaaaaaaaaaa();");
4283   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4284                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4285                "    .aaaaaaaaaaaaaaa();");
4286   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4287                "    ->aaaaaaaaaaaaaae(0)\n"
4288                "    ->aaaaaaaaaaaaaaa();");
4289 
4290   // Don't linewrap after very short segments.
4291   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4292                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4293                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4294   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4295                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4296                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4297   verifyFormat("aaa()\n"
4298                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4299                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4300                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4301 
4302   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4303                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4304                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4305   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4306                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4307                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4308 
4309   // Prefer not to break after empty parentheses.
4310   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4311                "    First->LastNewlineOffset);");
4312 
4313   // Prefer not to create "hanging" indents.
4314   verifyFormat(
4315       "return !soooooooooooooome_map\n"
4316       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4317       "            .second;");
4318   verifyFormat(
4319       "return aaaaaaaaaaaaaaaa\n"
4320       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4321       "    .aaaa(aaaaaaaaaaaaaa);");
4322   // No hanging indent here.
4323   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4324                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4325   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4326                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4327   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4328                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4329                getLLVMStyleWithColumns(60));
4330   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4331                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4332                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4333                getLLVMStyleWithColumns(59));
4334   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4335                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4336                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4337 }
4338 
4339 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4340   verifyFormat(
4341       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4342       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4343   verifyFormat(
4344       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4345       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4346 
4347   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4348                "    ccccccccccccccccccccccccc) {\n}");
4349   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4350                "    ccccccccccccccccccccccccc) {\n}");
4351 
4352   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4353                "    ccccccccccccccccccccccccc) {\n}");
4354   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4355                "    ccccccccccccccccccccccccc) {\n}");
4356 
4357   verifyFormat(
4358       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4359       "    ccccccccccccccccccccccccc) {\n}");
4360   verifyFormat(
4361       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4362       "    ccccccccccccccccccccccccc) {\n}");
4363 
4364   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4365                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4366                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4367                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4368   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4369                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4370                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4371                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4372 
4373   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4374                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4375                "    aaaaaaaaaaaaaaa != aa) {\n}");
4376   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4377                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4378                "    aaaaaaaaaaaaaaa != aa) {\n}");
4379 }
4380 
4381 TEST_F(FormatTest, BreaksAfterAssignments) {
4382   verifyFormat(
4383       "unsigned Cost =\n"
4384       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4385       "                        SI->getPointerAddressSpaceee());\n");
4386   verifyFormat(
4387       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4388       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4389 
4390   verifyFormat(
4391       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4392       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4393   verifyFormat("unsigned OriginalStartColumn =\n"
4394                "    SourceMgr.getSpellingColumnNumber(\n"
4395                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4396                "    1;");
4397 }
4398 
4399 TEST_F(FormatTest, AlignsAfterAssignments) {
4400   verifyFormat(
4401       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4402       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4403   verifyFormat(
4404       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4405       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4406   verifyFormat(
4407       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4408       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4409   verifyFormat(
4410       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4411       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4412   verifyFormat(
4413       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4414       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4415       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4416 }
4417 
4418 TEST_F(FormatTest, AlignsAfterReturn) {
4419   verifyFormat(
4420       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4421       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4422   verifyFormat(
4423       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4424       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4425   verifyFormat(
4426       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4427       "       aaaaaaaaaaaaaaaaaaaaaa();");
4428   verifyFormat(
4429       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4430       "        aaaaaaaaaaaaaaaaaaaaaa());");
4431   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4432                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4433   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4434                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4435                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4436   verifyFormat("return\n"
4437                "    // true if code is one of a or b.\n"
4438                "    code == a || code == b;");
4439 }
4440 
4441 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4442   verifyFormat(
4443       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4444       "                                                aaaaaaaaa aaaaaaa) {}");
4445   verifyFormat(
4446       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4447       "                                               aaaaaaaaaaa aaaaaaaaa);");
4448   verifyFormat(
4449       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4450       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4451   FormatStyle Style = getLLVMStyle();
4452   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4453   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4454                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4455                Style);
4456   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4457                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4458                Style);
4459   verifyFormat("SomeLongVariableName->someFunction(\n"
4460                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4461                Style);
4462   verifyFormat(
4463       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4464       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4465       Style);
4466   verifyFormat(
4467       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4468       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4469       Style);
4470   verifyFormat(
4471       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4472       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4473       Style);
4474 
4475   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4476                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4477                "        b));",
4478                Style);
4479 
4480   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4481   Style.BinPackArguments = false;
4482   Style.BinPackParameters = false;
4483   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4484                "    aaaaaaaaaaa aaaaaaaa,\n"
4485                "    aaaaaaaaa aaaaaaa,\n"
4486                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4487                Style);
4488   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4489                "    aaaaaaaaaaa aaaaaaaaa,\n"
4490                "    aaaaaaaaaaa aaaaaaaaa,\n"
4491                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4492                Style);
4493   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4494                "    aaaaaaaaaaaaaaa,\n"
4495                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4496                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4497                Style);
4498   verifyFormat(
4499       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4500       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4501       Style);
4502   verifyFormat(
4503       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4504       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4505       Style);
4506   verifyFormat(
4507       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4508       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4509       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4510       "    aaaaaaaaaaaaaaaa);",
4511       Style);
4512   verifyFormat(
4513       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4514       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4515       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4516       "    aaaaaaaaaaaaaaaa);",
4517       Style);
4518 }
4519 
4520 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4521   FormatStyle Style = getLLVMStyleWithColumns(40);
4522   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4523                "          bbbbbbbbbbbbbbbbbbbbbb);",
4524                Style);
4525   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4526   Style.AlignOperands = false;
4527   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4528                "          bbbbbbbbbbbbbbbbbbbbbb);",
4529                Style);
4530   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4531   Style.AlignOperands = true;
4532   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4533                "          bbbbbbbbbbbbbbbbbbbbbb);",
4534                Style);
4535   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4536   Style.AlignOperands = false;
4537   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4538                "    bbbbbbbbbbbbbbbbbbbbbb);",
4539                Style);
4540 }
4541 
4542 TEST_F(FormatTest, BreaksConditionalExpressions) {
4543   verifyFormat(
4544       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
4545       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4546       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4547   verifyFormat(
4548       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
4549       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4550       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4551   verifyFormat(
4552       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4553       "                                                    : aaaaaaaaaaaaa);");
4554   verifyFormat(
4555       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4556       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4557       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4558       "                   aaaaaaaaaaaaa);");
4559   verifyFormat(
4560       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4561       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4562       "                   aaaaaaaaaaaaa);");
4563   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4564                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4565                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4566                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4567                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4568   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4569                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4570                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4571                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4572                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4573                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4574                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4575   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4576                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4577                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4578                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4579                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4580   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4581                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4582                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4583   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4584                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4585                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4586                "        : aaaaaaaaaaaaaaaa;");
4587   verifyFormat(
4588       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4589       "    ? aaaaaaaaaaaaaaa\n"
4590       "    : aaaaaaaaaaaaaaa;");
4591   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4592                "          aaaaaaaaa\n"
4593                "      ? b\n"
4594                "      : c);");
4595   verifyFormat("return aaaa == bbbb\n"
4596                "           // comment\n"
4597                "           ? aaaa\n"
4598                "           : bbbb;");
4599   verifyFormat("unsigned Indent =\n"
4600                "    format(TheLine.First,\n"
4601                "           IndentForLevel[TheLine.Level] >= 0\n"
4602                "               ? IndentForLevel[TheLine.Level]\n"
4603                "               : TheLine * 2,\n"
4604                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4605                getLLVMStyleWithColumns(60));
4606   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4607                "                  ? aaaaaaaaaaaaaaa\n"
4608                "                  : bbbbbbbbbbbbbbb //\n"
4609                "                        ? ccccccccccccccc\n"
4610                "                        : ddddddddddddddd;");
4611   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4612                "                  ? aaaaaaaaaaaaaaa\n"
4613                "                  : (bbbbbbbbbbbbbbb //\n"
4614                "                         ? ccccccccccccccc\n"
4615                "                         : ddddddddddddddd);");
4616   verifyFormat(
4617       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4618       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4619       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4620       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4621       "                                      : aaaaaaaaaa;");
4622   verifyFormat(
4623       "aaaaaa = aaaaaaaaaaaa\n"
4624       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4625       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4626       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4627 
4628   FormatStyle NoBinPacking = getLLVMStyle();
4629   NoBinPacking.BinPackArguments = false;
4630   verifyFormat(
4631       "void f() {\n"
4632       "  g(aaa,\n"
4633       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4634       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4635       "        ? aaaaaaaaaaaaaaa\n"
4636       "        : aaaaaaaaaaaaaaa);\n"
4637       "}",
4638       NoBinPacking);
4639   verifyFormat(
4640       "void f() {\n"
4641       "  g(aaa,\n"
4642       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4643       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4644       "        ?: aaaaaaaaaaaaaaa);\n"
4645       "}",
4646       NoBinPacking);
4647 
4648   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4649                "             // comment.\n"
4650                "             ccccccccccccccccccccccccccccccccccccccc\n"
4651                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4652                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4653 
4654   // Assignments in conditional expressions. Apparently not uncommon :-(.
4655   verifyFormat("return a != b\n"
4656                "           // comment\n"
4657                "           ? a = b\n"
4658                "           : a = b;");
4659   verifyFormat("return a != b\n"
4660                "           // comment\n"
4661                "           ? a = a != b\n"
4662                "                     // comment\n"
4663                "                     ? a = b\n"
4664                "                     : a\n"
4665                "           : a;\n");
4666   verifyFormat("return a != b\n"
4667                "           // comment\n"
4668                "           ? a\n"
4669                "           : a = a != b\n"
4670                "                     // comment\n"
4671                "                     ? a = b\n"
4672                "                     : a;");
4673 }
4674 
4675 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4676   FormatStyle Style = getLLVMStyle();
4677   Style.BreakBeforeTernaryOperators = false;
4678   Style.ColumnLimit = 70;
4679   verifyFormat(
4680       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
4681       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4682       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4683       Style);
4684   verifyFormat(
4685       "aaaa(aaaaaaaaaaaaaaaaaaaa,\n"
4686       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4687       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4688       Style);
4689   verifyFormat(
4690       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4691       "                                                      aaaaaaaaaaaaa);",
4692       Style);
4693   verifyFormat(
4694       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4695       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4696       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4697       "                   aaaaaaaaaaaaa);",
4698       Style);
4699   verifyFormat(
4700       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4701       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4702       "                   aaaaaaaaaaaaa);",
4703       Style);
4704   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4705                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4706                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4707                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4708                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4709                Style);
4710   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4711                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4712                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4713                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4714                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4715                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4716                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4717                Style);
4718   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4719                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4720                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4721                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4722                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4723                Style);
4724   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4725                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4726                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4727                Style);
4728   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4729                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4730                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4731                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4732                Style);
4733   verifyFormat(
4734       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4735       "    aaaaaaaaaaaaaaa :\n"
4736       "    aaaaaaaaaaaaaaa;",
4737       Style);
4738   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4739                "          aaaaaaaaa ?\n"
4740                "      b :\n"
4741                "      c);",
4742                Style);
4743   verifyFormat("unsigned Indent =\n"
4744                "    format(TheLine.First,\n"
4745                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4746                "               IndentForLevel[TheLine.Level] :\n"
4747                "               TheLine * 2,\n"
4748                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4749                Style);
4750   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4751                "                  aaaaaaaaaaaaaaa :\n"
4752                "                  bbbbbbbbbbbbbbb ? //\n"
4753                "                      ccccccccccccccc :\n"
4754                "                      ddddddddddddddd;",
4755                Style);
4756   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4757                "                  aaaaaaaaaaaaaaa :\n"
4758                "                  (bbbbbbbbbbbbbbb ? //\n"
4759                "                       ccccccccccccccc :\n"
4760                "                       ddddddddddddddd);",
4761                Style);
4762   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4763                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4764                "            ccccccccccccccccccccccccccc;",
4765                Style);
4766   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4767                "           aaaaa :\n"
4768                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4769                Style);
4770 }
4771 
4772 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4773   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4774                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4775   verifyFormat("bool a = true, b = false;");
4776 
4777   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4778                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4779                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4780                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4781   verifyFormat(
4782       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4783       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4784       "     d = e && f;");
4785   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4786                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4787   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4788                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4789   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4790                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4791 
4792   FormatStyle Style = getGoogleStyle();
4793   Style.PointerAlignment = FormatStyle::PAS_Left;
4794   Style.DerivePointerAlignment = false;
4795   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4796                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4797                "    *b = bbbbbbbbbbbbbbbbbbb;",
4798                Style);
4799   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4800                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4801                Style);
4802   verifyFormat("vector<int*> a, b;", Style);
4803   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4804 }
4805 
4806 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4807   verifyFormat("arr[foo ? bar : baz];");
4808   verifyFormat("f()[foo ? bar : baz];");
4809   verifyFormat("(a + b)[foo ? bar : baz];");
4810   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4811 }
4812 
4813 TEST_F(FormatTest, AlignsStringLiterals) {
4814   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4815                "                                      \"short literal\");");
4816   verifyFormat(
4817       "looooooooooooooooooooooooongFunction(\n"
4818       "    \"short literal\"\n"
4819       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4820   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4821                "             \" string literals\",\n"
4822                "             and, other, parameters);");
4823   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4824             "      \"5678\";",
4825             format("fun + \"1243\" /* comment */\n"
4826                    "      \"5678\";",
4827                    getLLVMStyleWithColumns(28)));
4828   EXPECT_EQ(
4829       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4830       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4831       "         \"aaaaaaaaaaaaaaaa\";",
4832       format("aaaaaa ="
4833              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4834              "aaaaaaaaaaaaaaaaaaaaa\" "
4835              "\"aaaaaaaaaaaaaaaa\";"));
4836   verifyFormat("a = a + \"a\"\n"
4837                "        \"a\"\n"
4838                "        \"a\";");
4839   verifyFormat("f(\"a\", \"b\"\n"
4840                "       \"c\");");
4841 
4842   verifyFormat(
4843       "#define LL_FORMAT \"ll\"\n"
4844       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4845       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4846 
4847   verifyFormat("#define A(X)          \\\n"
4848                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4849                "  \"ccccc\"",
4850                getLLVMStyleWithColumns(23));
4851   verifyFormat("#define A \"def\"\n"
4852                "f(\"abc\" A \"ghi\"\n"
4853                "  \"jkl\");");
4854 
4855   verifyFormat("f(L\"a\"\n"
4856                "  L\"b\");");
4857   verifyFormat("#define A(X)            \\\n"
4858                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4859                "  L\"ccccc\"",
4860                getLLVMStyleWithColumns(25));
4861 
4862   verifyFormat("f(@\"a\"\n"
4863                "  @\"b\");");
4864   verifyFormat("NSString s = @\"a\"\n"
4865                "             @\"b\"\n"
4866                "             @\"c\";");
4867   verifyFormat("NSString s = @\"a\"\n"
4868                "              \"b\"\n"
4869                "              \"c\";");
4870 }
4871 
4872 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4873   FormatStyle Style = getLLVMStyle();
4874   // No declarations or definitions should be moved to own line.
4875   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4876   verifyFormat("class A {\n"
4877                "  int f() { return 1; }\n"
4878                "  int g();\n"
4879                "};\n"
4880                "int f() { return 1; }\n"
4881                "int g();\n",
4882                Style);
4883 
4884   // All declarations and definitions should have the return type moved to its
4885   // own
4886   // line.
4887   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4888   verifyFormat("class E {\n"
4889                "  int\n"
4890                "  f() {\n"
4891                "    return 1;\n"
4892                "  }\n"
4893                "  int\n"
4894                "  g();\n"
4895                "};\n"
4896                "int\n"
4897                "f() {\n"
4898                "  return 1;\n"
4899                "}\n"
4900                "int\n"
4901                "g();\n",
4902                Style);
4903 
4904   // Top-level definitions, and no kinds of declarations should have the
4905   // return type moved to its own line.
4906   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4907   verifyFormat("class B {\n"
4908                "  int f() { return 1; }\n"
4909                "  int g();\n"
4910                "};\n"
4911                "int\n"
4912                "f() {\n"
4913                "  return 1;\n"
4914                "}\n"
4915                "int g();\n",
4916                Style);
4917 
4918   // Top-level definitions and declarations should have the return type moved
4919   // to its own line.
4920   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4921   verifyFormat("class C {\n"
4922                "  int f() { return 1; }\n"
4923                "  int g();\n"
4924                "};\n"
4925                "int\n"
4926                "f() {\n"
4927                "  return 1;\n"
4928                "}\n"
4929                "int\n"
4930                "g();\n",
4931                Style);
4932 
4933   // All definitions should have the return type moved to its own line, but no
4934   // kinds of declarations.
4935   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4936   verifyFormat("class D {\n"
4937                "  int\n"
4938                "  f() {\n"
4939                "    return 1;\n"
4940                "  }\n"
4941                "  int g();\n"
4942                "};\n"
4943                "int\n"
4944                "f() {\n"
4945                "  return 1;\n"
4946                "}\n"
4947                "int g();\n",
4948                Style);
4949   verifyFormat("const char *\n"
4950                "f(void) {\n" // Break here.
4951                "  return \"\";\n"
4952                "}\n"
4953                "const char *bar(void);\n", // No break here.
4954                Style);
4955   verifyFormat("template <class T>\n"
4956                "T *\n"
4957                "f(T &c) {\n" // Break here.
4958                "  return NULL;\n"
4959                "}\n"
4960                "template <class T> T *f(T &c);\n", // No break here.
4961                Style);
4962   verifyFormat("class C {\n"
4963                "  int\n"
4964                "  operator+() {\n"
4965                "    return 1;\n"
4966                "  }\n"
4967                "  int\n"
4968                "  operator()() {\n"
4969                "    return 1;\n"
4970                "  }\n"
4971                "};\n",
4972                Style);
4973   verifyFormat("void\n"
4974                "A::operator()() {}\n"
4975                "void\n"
4976                "A::operator>>() {}\n"
4977                "void\n"
4978                "A::operator+() {}\n",
4979                Style);
4980   verifyFormat("void *operator new(std::size_t s);", // No break here.
4981                Style);
4982   verifyFormat("void *\n"
4983                "operator new(std::size_t s) {}",
4984                Style);
4985   verifyFormat("void *\n"
4986                "operator delete[](void *ptr) {}",
4987                Style);
4988   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4989   verifyFormat("const char *\n"
4990                "f(void)\n" // Break here.
4991                "{\n"
4992                "  return \"\";\n"
4993                "}\n"
4994                "const char *bar(void);\n", // No break here.
4995                Style);
4996   verifyFormat("template <class T>\n"
4997                "T *\n"     // Problem here: no line break
4998                "f(T &c)\n" // Break here.
4999                "{\n"
5000                "  return NULL;\n"
5001                "}\n"
5002                "template <class T> T *f(T &c);\n", // No break here.
5003                Style);
5004 }
5005 
5006 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5007   FormatStyle NoBreak = getLLVMStyle();
5008   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5009   FormatStyle Break = getLLVMStyle();
5010   Break.AlwaysBreakBeforeMultilineStrings = true;
5011   verifyFormat("aaaa = \"bbbb\"\n"
5012                "       \"cccc\";",
5013                NoBreak);
5014   verifyFormat("aaaa =\n"
5015                "    \"bbbb\"\n"
5016                "    \"cccc\";",
5017                Break);
5018   verifyFormat("aaaa(\"bbbb\"\n"
5019                "     \"cccc\");",
5020                NoBreak);
5021   verifyFormat("aaaa(\n"
5022                "    \"bbbb\"\n"
5023                "    \"cccc\");",
5024                Break);
5025   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5026                "          \"cccc\");",
5027                NoBreak);
5028   verifyFormat("aaaa(qqq,\n"
5029                "     \"bbbb\"\n"
5030                "     \"cccc\");",
5031                Break);
5032   verifyFormat("aaaa(qqq,\n"
5033                "     L\"bbbb\"\n"
5034                "     L\"cccc\");",
5035                Break);
5036   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5037                "                      \"bbbb\"));",
5038                Break);
5039   verifyFormat("string s = someFunction(\n"
5040                "    \"abc\"\n"
5041                "    \"abc\");",
5042                Break);
5043 
5044   // As we break before unary operators, breaking right after them is bad.
5045   verifyFormat("string foo = abc ? \"x\"\n"
5046                "                   \"blah blah blah blah blah blah\"\n"
5047                "                 : \"y\";",
5048                Break);
5049 
5050   // Don't break if there is no column gain.
5051   verifyFormat("f(\"aaaa\"\n"
5052                "  \"bbbb\");",
5053                Break);
5054 
5055   // Treat literals with escaped newlines like multi-line string literals.
5056   EXPECT_EQ("x = \"a\\\n"
5057             "b\\\n"
5058             "c\";",
5059             format("x = \"a\\\n"
5060                    "b\\\n"
5061                    "c\";",
5062                    NoBreak));
5063   EXPECT_EQ("xxxx =\n"
5064             "    \"a\\\n"
5065             "b\\\n"
5066             "c\";",
5067             format("xxxx = \"a\\\n"
5068                    "b\\\n"
5069                    "c\";",
5070                    Break));
5071 
5072   // Exempt ObjC strings for now.
5073   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
5074             "                          @\"bbbb\";",
5075             format("NSString *const kString = @\"aaaa\"\n"
5076                    "@\"bbbb\";",
5077                    Break));
5078 
5079   Break.ColumnLimit = 0;
5080   verifyFormat("const char *hello = \"hello llvm\";", Break);
5081 }
5082 
5083 TEST_F(FormatTest, AlignsPipes) {
5084   verifyFormat(
5085       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5086       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5087       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5088   verifyFormat(
5089       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5090       "                     << aaaaaaaaaaaaaaaaaaaa;");
5091   verifyFormat(
5092       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5093       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5094   verifyFormat(
5095       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5096       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5097       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5098   verifyFormat(
5099       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5100       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5101       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5102   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5103                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5104                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5105                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5106   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5107                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5108   verifyFormat(
5109       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5110       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5111 
5112   verifyFormat("return out << \"somepacket = {\\n\"\n"
5113                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5114                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5115                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5116                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5117                "           << \"}\";");
5118 
5119   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5120                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5121                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5122   verifyFormat(
5123       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5124       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5125       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5126       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5127       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5128   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5129                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5130   verifyFormat(
5131       "void f() {\n"
5132       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5133       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5134       "}");
5135   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5136                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5137   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5138                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5139                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5140                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5141   verifyFormat("LOG_IF(aaa == //\n"
5142                "       bbb)\n"
5143                "    << a << b;");
5144 
5145   // Breaking before the first "<<" is generally not desirable.
5146   verifyFormat(
5147       "llvm::errs()\n"
5148       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5149       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5150       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5151       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5152       getLLVMStyleWithColumns(70));
5153   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5154                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5155                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5156                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5157                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5158                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5159                getLLVMStyleWithColumns(70));
5160 
5161   // But sometimes, breaking before the first "<<" is desirable.
5162   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5163                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5164   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5165                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5166                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5167   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5168                "    << BEF << IsTemplate << Description << E->getType();");
5169   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5170                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5171                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5172   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5173                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5174                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5175                "    << aaa;");
5176 
5177   verifyFormat(
5178       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5179       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5180 
5181   // Incomplete string literal.
5182   EXPECT_EQ("llvm::errs() << \"\n"
5183             "             << a;",
5184             format("llvm::errs() << \"\n<<a;"));
5185 
5186   verifyFormat("void f() {\n"
5187                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5188                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5189                "}");
5190 
5191   // Handle 'endl'.
5192   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5193                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5194   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5195 
5196   // Handle '\n'.
5197   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5198                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5199   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5200                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5201   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5202                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5203   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5204 }
5205 
5206 TEST_F(FormatTest, UnderstandsEquals) {
5207   verifyFormat(
5208       "aaaaaaaaaaaaaaaaa =\n"
5209       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5210   verifyFormat(
5211       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5212       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5213   verifyFormat(
5214       "if (a) {\n"
5215       "  f();\n"
5216       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5217       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5218       "}");
5219 
5220   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5221                "        100000000 + 10000000) {\n}");
5222 }
5223 
5224 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5225   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5226                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5227 
5228   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5229                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5230 
5231   verifyFormat(
5232       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5233       "                                                          Parameter2);");
5234 
5235   verifyFormat(
5236       "ShortObject->shortFunction(\n"
5237       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5238       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5239 
5240   verifyFormat("loooooooooooooongFunction(\n"
5241                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5242 
5243   verifyFormat(
5244       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5245       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5246 
5247   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5248                "    .WillRepeatedly(Return(SomeValue));");
5249   verifyFormat("void f() {\n"
5250                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5251                "      .Times(2)\n"
5252                "      .WillRepeatedly(Return(SomeValue));\n"
5253                "}");
5254   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5255                "    ccccccccccccccccccccccc);");
5256   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5257                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5258                "          .aaaaa(aaaaa),\n"
5259                "      aaaaaaaaaaaaaaaaaaaaa);");
5260   verifyFormat("void f() {\n"
5261                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5262                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5263                "}");
5264   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5265                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5266                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5267                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5268                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5269   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5270                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5271                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5272                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5273                "}");
5274 
5275   // Here, it is not necessary to wrap at "." or "->".
5276   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5277                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5278   verifyFormat(
5279       "aaaaaaaaaaa->aaaaaaaaa(\n"
5280       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5281       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5282 
5283   verifyFormat(
5284       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5285       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5286   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5287                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5288   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5289                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5290 
5291   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5292                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5293                "    .a();");
5294 
5295   FormatStyle NoBinPacking = getLLVMStyle();
5296   NoBinPacking.BinPackParameters = false;
5297   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5298                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5299                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5300                "                         aaaaaaaaaaaaaaaaaaa,\n"
5301                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5302                NoBinPacking);
5303 
5304   // If there is a subsequent call, change to hanging indentation.
5305   verifyFormat(
5306       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5307       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5308       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5309   verifyFormat(
5310       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5311       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5312   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5313                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5314                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5315   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5316                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5317                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5318 }
5319 
5320 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5321   verifyFormat("template <typename T>\n"
5322                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5323   verifyFormat("template <typename T>\n"
5324                "// T should be one of {A, B}.\n"
5325                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5326   verifyFormat(
5327       "template <typename T>\n"
5328       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5329   verifyFormat("template <typename T>\n"
5330                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5331                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5332   verifyFormat(
5333       "template <typename T>\n"
5334       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5335       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5336   verifyFormat(
5337       "template <typename T>\n"
5338       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5339       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5340       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5341   verifyFormat("template <typename T>\n"
5342                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5343                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5344   verifyFormat(
5345       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5346       "          typename T4 = char>\n"
5347       "void f();");
5348   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5349                "          template <typename> class cccccccccccccccccccccc,\n"
5350                "          typename ddddddddddddd>\n"
5351                "class C {};");
5352   verifyFormat(
5353       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5354       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5355 
5356   verifyFormat("void f() {\n"
5357                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5358                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5359                "}");
5360 
5361   verifyFormat("template <typename T> class C {};");
5362   verifyFormat("template <typename T> void f();");
5363   verifyFormat("template <typename T> void f() {}");
5364   verifyFormat(
5365       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5366       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5367       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5368       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5369       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5370       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5371       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5372       getLLVMStyleWithColumns(72));
5373   EXPECT_EQ("static_cast<A< //\n"
5374             "    B> *>(\n"
5375             "\n"
5376             "    );",
5377             format("static_cast<A<//\n"
5378                    "    B>*>(\n"
5379                    "\n"
5380                    "    );"));
5381   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5382                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5383 
5384   FormatStyle AlwaysBreak = getLLVMStyle();
5385   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5386   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5387   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5388   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5389   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5390                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5391                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5392   verifyFormat("template <template <typename> class Fooooooo,\n"
5393                "          template <typename> class Baaaaaaar>\n"
5394                "struct C {};",
5395                AlwaysBreak);
5396   verifyFormat("template <typename T> // T can be A, B or C.\n"
5397                "struct C {};",
5398                AlwaysBreak);
5399   verifyFormat("template <enum E> class A {\n"
5400                "public:\n"
5401                "  E *f();\n"
5402                "};");
5403 }
5404 
5405 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5406   verifyFormat(
5407       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5408       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5409   verifyFormat(
5410       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5411       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5412       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5413 
5414   // FIXME: Should we have the extra indent after the second break?
5415   verifyFormat(
5416       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5417       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5418       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5419 
5420   verifyFormat(
5421       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5422       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5423 
5424   // Breaking at nested name specifiers is generally not desirable.
5425   verifyFormat(
5426       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5427       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5428 
5429   verifyFormat(
5430       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5431       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5432       "                   aaaaaaaaaaaaaaaaaaaaa);",
5433       getLLVMStyleWithColumns(74));
5434 
5435   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5436                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5437                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5438 }
5439 
5440 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5441   verifyFormat("A<int> a;");
5442   verifyFormat("A<A<A<int>>> a;");
5443   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5444   verifyFormat("bool x = a < 1 || 2 > a;");
5445   verifyFormat("bool x = 5 < f<int>();");
5446   verifyFormat("bool x = f<int>() > 5;");
5447   verifyFormat("bool x = 5 < a<int>::x;");
5448   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5449   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5450 
5451   verifyGoogleFormat("A<A<int>> a;");
5452   verifyGoogleFormat("A<A<A<int>>> a;");
5453   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5454   verifyGoogleFormat("A<A<int> > a;");
5455   verifyGoogleFormat("A<A<A<int> > > a;");
5456   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5457   verifyGoogleFormat("A<::A<int>> a;");
5458   verifyGoogleFormat("A<::A> a;");
5459   verifyGoogleFormat("A< ::A> a;");
5460   verifyGoogleFormat("A< ::A<int> > a;");
5461   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5462   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5463   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5464   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5465   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5466             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5467 
5468   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5469 
5470   verifyFormat("test >> a >> b;");
5471   verifyFormat("test << a >> b;");
5472 
5473   verifyFormat("f<int>();");
5474   verifyFormat("template <typename T> void f() {}");
5475   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5476   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5477                "sizeof(char)>::type>;");
5478   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5479   verifyFormat("f(a.operator()<A>());");
5480   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5481                "      .template operator()<A>());",
5482                getLLVMStyleWithColumns(35));
5483 
5484   // Not template parameters.
5485   verifyFormat("return a < b && c > d;");
5486   verifyFormat("void f() {\n"
5487                "  while (a < b && c > d) {\n"
5488                "  }\n"
5489                "}");
5490   verifyFormat("template <typename... Types>\n"
5491                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5492 
5493   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5494                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5495                getLLVMStyleWithColumns(60));
5496   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5497   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5498   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5499 }
5500 
5501 TEST_F(FormatTest, BitshiftOperatorWidth) {
5502   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5503             "                   bar */",
5504             format("int    a=1<<2;  /* foo\n"
5505                    "                   bar */"));
5506 
5507   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5508             "                     bar */",
5509             format("int  b  =256>>1 ;  /* foo\n"
5510                    "                      bar */"));
5511 }
5512 
5513 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5514   verifyFormat("COMPARE(a, ==, b);");
5515   verifyFormat("auto s = sizeof...(Ts) - 1;");
5516 }
5517 
5518 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5519   verifyFormat("int A::*x;");
5520   verifyFormat("int (S::*func)(void *);");
5521   verifyFormat("void f() { int (S::*func)(void *); }");
5522   verifyFormat("typedef bool *(Class::*Member)() const;");
5523   verifyFormat("void f() {\n"
5524                "  (a->*f)();\n"
5525                "  a->*x;\n"
5526                "  (a.*f)();\n"
5527                "  ((*a).*f)();\n"
5528                "  a.*x;\n"
5529                "}");
5530   verifyFormat("void f() {\n"
5531                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5532                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5533                "}");
5534   verifyFormat(
5535       "(aaaaaaaaaa->*bbbbbbb)(\n"
5536       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5537   FormatStyle Style = getLLVMStyle();
5538   Style.PointerAlignment = FormatStyle::PAS_Left;
5539   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5540 }
5541 
5542 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5543   verifyFormat("int a = -2;");
5544   verifyFormat("f(-1, -2, -3);");
5545   verifyFormat("a[-1] = 5;");
5546   verifyFormat("int a = 5 + -2;");
5547   verifyFormat("if (i == -1) {\n}");
5548   verifyFormat("if (i != -1) {\n}");
5549   verifyFormat("if (i > -1) {\n}");
5550   verifyFormat("if (i < -1) {\n}");
5551   verifyFormat("++(a->f());");
5552   verifyFormat("--(a->f());");
5553   verifyFormat("(a->f())++;");
5554   verifyFormat("a[42]++;");
5555   verifyFormat("if (!(a->f())) {\n}");
5556 
5557   verifyFormat("a-- > b;");
5558   verifyFormat("b ? -a : c;");
5559   verifyFormat("n * sizeof char16;");
5560   verifyFormat("n * alignof char16;", getGoogleStyle());
5561   verifyFormat("sizeof(char);");
5562   verifyFormat("alignof(char);", getGoogleStyle());
5563 
5564   verifyFormat("return -1;");
5565   verifyFormat("switch (a) {\n"
5566                "case -1:\n"
5567                "  break;\n"
5568                "}");
5569   verifyFormat("#define X -1");
5570   verifyFormat("#define X -kConstant");
5571 
5572   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5573   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5574 
5575   verifyFormat("int a = /* confusing comment */ -1;");
5576   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5577   verifyFormat("int a = i /* confusing comment */++;");
5578 }
5579 
5580 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5581   verifyFormat("if (!aaaaaaaaaa( // break\n"
5582                "        aaaaa)) {\n"
5583                "}");
5584   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5585                "    aaaaa));");
5586   verifyFormat("*aaa = aaaaaaa( // break\n"
5587                "    bbbbbb);");
5588 }
5589 
5590 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5591   verifyFormat("bool operator<();");
5592   verifyFormat("bool operator>();");
5593   verifyFormat("bool operator=();");
5594   verifyFormat("bool operator==();");
5595   verifyFormat("bool operator!=();");
5596   verifyFormat("int operator+();");
5597   verifyFormat("int operator++();");
5598   verifyFormat("bool operator,();");
5599   verifyFormat("bool operator();");
5600   verifyFormat("bool operator()();");
5601   verifyFormat("bool operator[]();");
5602   verifyFormat("operator bool();");
5603   verifyFormat("operator int();");
5604   verifyFormat("operator void *();");
5605   verifyFormat("operator SomeType<int>();");
5606   verifyFormat("operator SomeType<int, int>();");
5607   verifyFormat("operator SomeType<SomeType<int>>();");
5608   verifyFormat("void *operator new(std::size_t size);");
5609   verifyFormat("void *operator new[](std::size_t size);");
5610   verifyFormat("void operator delete(void *ptr);");
5611   verifyFormat("void operator delete[](void *ptr);");
5612   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5613                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5614   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5615                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5616 
5617   verifyFormat(
5618       "ostream &operator<<(ostream &OutputStream,\n"
5619       "                    SomeReallyLongType WithSomeReallyLongValue);");
5620   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5621                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5622                "  return left.group < right.group;\n"
5623                "}");
5624   verifyFormat("SomeType &operator=(const SomeType &S);");
5625   verifyFormat("f.template operator()<int>();");
5626 
5627   verifyGoogleFormat("operator void*();");
5628   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5629   verifyGoogleFormat("operator ::A();");
5630 
5631   verifyFormat("using A::operator+;");
5632   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5633                "int i;");
5634 }
5635 
5636 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5637   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5638   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5639   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5640   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5641   verifyFormat("Deleted &operator=(const Deleted &) &;");
5642   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5643   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5644   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5645   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5646   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5647   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5648   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
5649   verifyFormat("template <typename T>\n"
5650                "void F(T) && = delete;",
5651                getGoogleStyle());
5652 
5653   FormatStyle AlignLeft = getLLVMStyle();
5654   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5655   verifyFormat("void A::b() && {}", AlignLeft);
5656   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5657   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5658                AlignLeft);
5659   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5660   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5661   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5662   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5663   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5664   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5665   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
5666 
5667   FormatStyle Spaces = getLLVMStyle();
5668   Spaces.SpacesInCStyleCastParentheses = true;
5669   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5670   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5671   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5672   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5673 
5674   Spaces.SpacesInCStyleCastParentheses = false;
5675   Spaces.SpacesInParentheses = true;
5676   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5677   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5678   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5679   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5680 }
5681 
5682 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5683   verifyFormat("void f() {\n"
5684                "  A *a = new A;\n"
5685                "  A *a = new (placement) A;\n"
5686                "  delete a;\n"
5687                "  delete (A *)a;\n"
5688                "}");
5689   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5690                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5691   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5692                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5693                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5694   verifyFormat("delete[] h->p;");
5695 }
5696 
5697 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5698   verifyFormat("int *f(int *a) {}");
5699   verifyFormat("int main(int argc, char **argv) {}");
5700   verifyFormat("Test::Test(int b) : a(b * b) {}");
5701   verifyIndependentOfContext("f(a, *a);");
5702   verifyFormat("void g() { f(*a); }");
5703   verifyIndependentOfContext("int a = b * 10;");
5704   verifyIndependentOfContext("int a = 10 * b;");
5705   verifyIndependentOfContext("int a = b * c;");
5706   verifyIndependentOfContext("int a += b * c;");
5707   verifyIndependentOfContext("int a -= b * c;");
5708   verifyIndependentOfContext("int a *= b * c;");
5709   verifyIndependentOfContext("int a /= b * c;");
5710   verifyIndependentOfContext("int a = *b;");
5711   verifyIndependentOfContext("int a = *b * c;");
5712   verifyIndependentOfContext("int a = b * *c;");
5713   verifyIndependentOfContext("int a = b * (10);");
5714   verifyIndependentOfContext("S << b * (10);");
5715   verifyIndependentOfContext("return 10 * b;");
5716   verifyIndependentOfContext("return *b * *c;");
5717   verifyIndependentOfContext("return a & ~b;");
5718   verifyIndependentOfContext("f(b ? *c : *d);");
5719   verifyIndependentOfContext("int a = b ? *c : *d;");
5720   verifyIndependentOfContext("*b = a;");
5721   verifyIndependentOfContext("a * ~b;");
5722   verifyIndependentOfContext("a * !b;");
5723   verifyIndependentOfContext("a * +b;");
5724   verifyIndependentOfContext("a * -b;");
5725   verifyIndependentOfContext("a * ++b;");
5726   verifyIndependentOfContext("a * --b;");
5727   verifyIndependentOfContext("a[4] * b;");
5728   verifyIndependentOfContext("a[a * a] = 1;");
5729   verifyIndependentOfContext("f() * b;");
5730   verifyIndependentOfContext("a * [self dostuff];");
5731   verifyIndependentOfContext("int x = a * (a + b);");
5732   verifyIndependentOfContext("(a *)(a + b);");
5733   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5734   verifyIndependentOfContext("int *pa = (int *)&a;");
5735   verifyIndependentOfContext("return sizeof(int **);");
5736   verifyIndependentOfContext("return sizeof(int ******);");
5737   verifyIndependentOfContext("return (int **&)a;");
5738   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5739   verifyFormat("void f(Type (*parameter)[10]) {}");
5740   verifyFormat("void f(Type (&parameter)[10]) {}");
5741   verifyGoogleFormat("return sizeof(int**);");
5742   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5743   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5744   verifyFormat("auto a = [](int **&, int ***) {};");
5745   verifyFormat("auto PointerBinding = [](const char *S) {};");
5746   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5747   verifyFormat("[](const decltype(*a) &value) {}");
5748   verifyFormat("decltype(a * b) F();");
5749   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5750   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5751   verifyIndependentOfContext("typedef void (*f)(int *a);");
5752   verifyIndependentOfContext("int i{a * b};");
5753   verifyIndependentOfContext("aaa && aaa->f();");
5754   verifyIndependentOfContext("int x = ~*p;");
5755   verifyFormat("Constructor() : a(a), area(width * height) {}");
5756   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5757   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5758   verifyFormat("void f() { f(a, c * d); }");
5759   verifyFormat("void f() { f(new a(), c * d); }");
5760 
5761   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5762 
5763   verifyIndependentOfContext("A<int *> a;");
5764   verifyIndependentOfContext("A<int **> a;");
5765   verifyIndependentOfContext("A<int *, int *> a;");
5766   verifyIndependentOfContext("A<int *[]> a;");
5767   verifyIndependentOfContext(
5768       "const char *const p = reinterpret_cast<const char *const>(q);");
5769   verifyIndependentOfContext("A<int **, int **> a;");
5770   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5771   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5772   verifyFormat("for (; a && b;) {\n}");
5773   verifyFormat("bool foo = true && [] { return false; }();");
5774 
5775   verifyFormat(
5776       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5777       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5778 
5779   verifyGoogleFormat("int const* a = &b;");
5780   verifyGoogleFormat("**outparam = 1;");
5781   verifyGoogleFormat("*outparam = a * b;");
5782   verifyGoogleFormat("int main(int argc, char** argv) {}");
5783   verifyGoogleFormat("A<int*> a;");
5784   verifyGoogleFormat("A<int**> a;");
5785   verifyGoogleFormat("A<int*, int*> a;");
5786   verifyGoogleFormat("A<int**, int**> a;");
5787   verifyGoogleFormat("f(b ? *c : *d);");
5788   verifyGoogleFormat("int a = b ? *c : *d;");
5789   verifyGoogleFormat("Type* t = **x;");
5790   verifyGoogleFormat("Type* t = *++*x;");
5791   verifyGoogleFormat("*++*x;");
5792   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5793   verifyGoogleFormat("Type* t = x++ * y;");
5794   verifyGoogleFormat(
5795       "const char* const p = reinterpret_cast<const char* const>(q);");
5796   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5797   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5798   verifyGoogleFormat("template <typename T>\n"
5799                      "void f(int i = 0, SomeType** temps = NULL);");
5800 
5801   FormatStyle Left = getLLVMStyle();
5802   Left.PointerAlignment = FormatStyle::PAS_Left;
5803   verifyFormat("x = *a(x) = *a(y);", Left);
5804   verifyFormat("for (;; *a = b) {\n}", Left);
5805   verifyFormat("return *this += 1;", Left);
5806 
5807   verifyIndependentOfContext("a = *(x + y);");
5808   verifyIndependentOfContext("a = &(x + y);");
5809   verifyIndependentOfContext("*(x + y).call();");
5810   verifyIndependentOfContext("&(x + y)->call();");
5811   verifyFormat("void f() { &(*I).first; }");
5812 
5813   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5814   verifyFormat(
5815       "int *MyValues = {\n"
5816       "    *A, // Operator detection might be confused by the '{'\n"
5817       "    *BB // Operator detection might be confused by previous comment\n"
5818       "};");
5819 
5820   verifyIndependentOfContext("if (int *a = &b)");
5821   verifyIndependentOfContext("if (int &a = *b)");
5822   verifyIndependentOfContext("if (a & b[i])");
5823   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5824   verifyIndependentOfContext("if (*b[i])");
5825   verifyIndependentOfContext("if (int *a = (&b))");
5826   verifyIndependentOfContext("while (int *a = &b)");
5827   verifyIndependentOfContext("size = sizeof *a;");
5828   verifyIndependentOfContext("if (a && (b = c))");
5829   verifyFormat("void f() {\n"
5830                "  for (const int &v : Values) {\n"
5831                "  }\n"
5832                "}");
5833   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5834   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5835   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5836 
5837   verifyFormat("#define A (!a * b)");
5838   verifyFormat("#define MACRO     \\\n"
5839                "  int *i = a * b; \\\n"
5840                "  void f(a *b);",
5841                getLLVMStyleWithColumns(19));
5842 
5843   verifyIndependentOfContext("A = new SomeType *[Length];");
5844   verifyIndependentOfContext("A = new SomeType *[Length]();");
5845   verifyIndependentOfContext("T **t = new T *;");
5846   verifyIndependentOfContext("T **t = new T *();");
5847   verifyGoogleFormat("A = new SomeType*[Length]();");
5848   verifyGoogleFormat("A = new SomeType*[Length];");
5849   verifyGoogleFormat("T** t = new T*;");
5850   verifyGoogleFormat("T** t = new T*();");
5851 
5852   FormatStyle PointerLeft = getLLVMStyle();
5853   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5854   verifyFormat("delete *x;", PointerLeft);
5855   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5856   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5857   verifyFormat("template <bool a, bool b> "
5858                "typename t::if<x && y>::type f() {}");
5859   verifyFormat("template <int *y> f() {}");
5860   verifyFormat("vector<int *> v;");
5861   verifyFormat("vector<int *const> v;");
5862   verifyFormat("vector<int *const **const *> v;");
5863   verifyFormat("vector<int *volatile> v;");
5864   verifyFormat("vector<a * b> v;");
5865   verifyFormat("foo<b && false>();");
5866   verifyFormat("foo<b & 1>();");
5867   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5868   verifyFormat(
5869       "template <class T,\n"
5870       "          class = typename std::enable_if<\n"
5871       "              std::is_integral<T>::value &&\n"
5872       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5873       "void F();",
5874       getLLVMStyleWithColumns(70));
5875   verifyFormat(
5876       "template <class T,\n"
5877       "          class = typename ::std::enable_if<\n"
5878       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5879       "void F();",
5880       getGoogleStyleWithColumns(68));
5881 
5882   verifyIndependentOfContext("MACRO(int *i);");
5883   verifyIndependentOfContext("MACRO(auto *a);");
5884   verifyIndependentOfContext("MACRO(const A *a);");
5885   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5886   verifyFormat("void f() { f(float{1}, a * a); }");
5887   // FIXME: Is there a way to make this work?
5888   // verifyIndependentOfContext("MACRO(A *a);");
5889 
5890   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5891   verifyFormat("return options != nullptr && operator==(*options);");
5892 
5893   EXPECT_EQ("#define OP(x)                                    \\\n"
5894             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5895             "    return s << a.DebugString();                 \\\n"
5896             "  }",
5897             format("#define OP(x) \\\n"
5898                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5899                    "    return s << a.DebugString(); \\\n"
5900                    "  }",
5901                    getLLVMStyleWithColumns(50)));
5902 
5903   // FIXME: We cannot handle this case yet; we might be able to figure out that
5904   // foo<x> d > v; doesn't make sense.
5905   verifyFormat("foo<a<b && c> d> v;");
5906 
5907   FormatStyle PointerMiddle = getLLVMStyle();
5908   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5909   verifyFormat("delete *x;", PointerMiddle);
5910   verifyFormat("int * x;", PointerMiddle);
5911   verifyFormat("template <int * y> f() {}", PointerMiddle);
5912   verifyFormat("int * f(int * a) {}", PointerMiddle);
5913   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5914   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5915   verifyFormat("A<int *> a;", PointerMiddle);
5916   verifyFormat("A<int **> a;", PointerMiddle);
5917   verifyFormat("A<int *, int *> a;", PointerMiddle);
5918   verifyFormat("A<int * []> a;", PointerMiddle);
5919   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5920   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5921   verifyFormat("T ** t = new T *;", PointerMiddle);
5922 
5923   // Member function reference qualifiers aren't binary operators.
5924   verifyFormat("string // break\n"
5925                "operator()() & {}");
5926   verifyFormat("string // break\n"
5927                "operator()() && {}");
5928   verifyGoogleFormat("template <typename T>\n"
5929                      "auto x() & -> int {}");
5930 }
5931 
5932 TEST_F(FormatTest, UnderstandsAttributes) {
5933   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5934   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5935                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5936   FormatStyle AfterType = getLLVMStyle();
5937   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5938   verifyFormat("__attribute__((nodebug)) void\n"
5939                "foo() {}\n",
5940                AfterType);
5941 }
5942 
5943 TEST_F(FormatTest, UnderstandsEllipsis) {
5944   verifyFormat("int printf(const char *fmt, ...);");
5945   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5946   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5947 
5948   FormatStyle PointersLeft = getLLVMStyle();
5949   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5950   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5951 }
5952 
5953 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5954   EXPECT_EQ("int *a;\n"
5955             "int *a;\n"
5956             "int *a;",
5957             format("int *a;\n"
5958                    "int* a;\n"
5959                    "int *a;",
5960                    getGoogleStyle()));
5961   EXPECT_EQ("int* a;\n"
5962             "int* a;\n"
5963             "int* a;",
5964             format("int* a;\n"
5965                    "int* a;\n"
5966                    "int *a;",
5967                    getGoogleStyle()));
5968   EXPECT_EQ("int *a;\n"
5969             "int *a;\n"
5970             "int *a;",
5971             format("int *a;\n"
5972                    "int * a;\n"
5973                    "int *  a;",
5974                    getGoogleStyle()));
5975   EXPECT_EQ("auto x = [] {\n"
5976             "  int *a;\n"
5977             "  int *a;\n"
5978             "  int *a;\n"
5979             "};",
5980             format("auto x=[]{int *a;\n"
5981                    "int * a;\n"
5982                    "int *  a;};",
5983                    getGoogleStyle()));
5984 }
5985 
5986 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5987   verifyFormat("int f(int &&a) {}");
5988   verifyFormat("int f(int a, char &&b) {}");
5989   verifyFormat("void f() { int &&a = b; }");
5990   verifyGoogleFormat("int f(int a, char&& b) {}");
5991   verifyGoogleFormat("void f() { int&& a = b; }");
5992 
5993   verifyIndependentOfContext("A<int &&> a;");
5994   verifyIndependentOfContext("A<int &&, int &&> a;");
5995   verifyGoogleFormat("A<int&&> a;");
5996   verifyGoogleFormat("A<int&&, int&&> a;");
5997 
5998   // Not rvalue references:
5999   verifyFormat("template <bool B, bool C> class A {\n"
6000                "  static_assert(B && C, \"Something is wrong\");\n"
6001                "};");
6002   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6003   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6004   verifyFormat("#define A(a, b) (a && b)");
6005 }
6006 
6007 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6008   verifyFormat("void f() {\n"
6009                "  x[aaaaaaaaa -\n"
6010                "    b] = 23;\n"
6011                "}",
6012                getLLVMStyleWithColumns(15));
6013 }
6014 
6015 TEST_F(FormatTest, FormatsCasts) {
6016   verifyFormat("Type *A = static_cast<Type *>(P);");
6017   verifyFormat("Type *A = (Type *)P;");
6018   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6019   verifyFormat("int a = (int)(2.0f);");
6020   verifyFormat("int a = (int)2.0f;");
6021   verifyFormat("x[(int32)y];");
6022   verifyFormat("x = (int32)y;");
6023   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6024   verifyFormat("int a = (int)*b;");
6025   verifyFormat("int a = (int)2.0f;");
6026   verifyFormat("int a = (int)~0;");
6027   verifyFormat("int a = (int)++a;");
6028   verifyFormat("int a = (int)sizeof(int);");
6029   verifyFormat("int a = (int)+2;");
6030   verifyFormat("my_int a = (my_int)2.0f;");
6031   verifyFormat("my_int a = (my_int)sizeof(int);");
6032   verifyFormat("return (my_int)aaa;");
6033   verifyFormat("#define x ((int)-1)");
6034   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6035   verifyFormat("#define p(q) ((int *)&q)");
6036   verifyFormat("fn(a)(b) + 1;");
6037 
6038   verifyFormat("void f() { my_int a = (my_int)*b; }");
6039   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6040   verifyFormat("my_int a = (my_int)~0;");
6041   verifyFormat("my_int a = (my_int)++a;");
6042   verifyFormat("my_int a = (my_int)-2;");
6043   verifyFormat("my_int a = (my_int)1;");
6044   verifyFormat("my_int a = (my_int *)1;");
6045   verifyFormat("my_int a = (const my_int)-1;");
6046   verifyFormat("my_int a = (const my_int *)-1;");
6047   verifyFormat("my_int a = (my_int)(my_int)-1;");
6048   verifyFormat("my_int a = (ns::my_int)-2;");
6049   verifyFormat("case (my_int)ONE:");
6050   verifyFormat("auto x = (X)this;");
6051 
6052   // FIXME: single value wrapped with paren will be treated as cast.
6053   verifyFormat("void f(int i = (kValue)*kMask) {}");
6054 
6055   verifyFormat("{ (void)F; }");
6056 
6057   // Don't break after a cast's
6058   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6059                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6060                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6061 
6062   // These are not casts.
6063   verifyFormat("void f(int *) {}");
6064   verifyFormat("f(foo)->b;");
6065   verifyFormat("f(foo).b;");
6066   verifyFormat("f(foo)(b);");
6067   verifyFormat("f(foo)[b];");
6068   verifyFormat("[](foo) { return 4; }(bar);");
6069   verifyFormat("(*funptr)(foo)[4];");
6070   verifyFormat("funptrs[4](foo)[4];");
6071   verifyFormat("void f(int *);");
6072   verifyFormat("void f(int *) = 0;");
6073   verifyFormat("void f(SmallVector<int>) {}");
6074   verifyFormat("void f(SmallVector<int>);");
6075   verifyFormat("void f(SmallVector<int>) = 0;");
6076   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6077   verifyFormat("int a = sizeof(int) * b;");
6078   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6079   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6080   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6081   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6082 
6083   // These are not casts, but at some point were confused with casts.
6084   verifyFormat("virtual void foo(int *) override;");
6085   verifyFormat("virtual void foo(char &) const;");
6086   verifyFormat("virtual void foo(int *a, char *) const;");
6087   verifyFormat("int a = sizeof(int *) + b;");
6088   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6089   verifyFormat("bool b = f(g<int>) && c;");
6090   verifyFormat("typedef void (*f)(int i) func;");
6091 
6092   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6093                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6094   // FIXME: The indentation here is not ideal.
6095   verifyFormat(
6096       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6097       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6098       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6099 }
6100 
6101 TEST_F(FormatTest, FormatsFunctionTypes) {
6102   verifyFormat("A<bool()> a;");
6103   verifyFormat("A<SomeType()> a;");
6104   verifyFormat("A<void (*)(int, std::string)> a;");
6105   verifyFormat("A<void *(int)>;");
6106   verifyFormat("void *(*a)(int *, SomeType *);");
6107   verifyFormat("int (*func)(void *);");
6108   verifyFormat("void f() { int (*func)(void *); }");
6109   verifyFormat("template <class CallbackClass>\n"
6110                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6111 
6112   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6113   verifyGoogleFormat("void* (*a)(int);");
6114   verifyGoogleFormat(
6115       "template <class CallbackClass>\n"
6116       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6117 
6118   // Other constructs can look somewhat like function types:
6119   verifyFormat("A<sizeof(*x)> a;");
6120   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6121   verifyFormat("some_var = function(*some_pointer_var)[0];");
6122   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6123   verifyFormat("int x = f(&h)();");
6124   verifyFormat("returnsFunction(&param1, &param2)(param);");
6125 }
6126 
6127 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6128   verifyFormat("A (*foo_)[6];");
6129   verifyFormat("vector<int> (*foo_)[6];");
6130 }
6131 
6132 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6133   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6134                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6135   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6136                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6137   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6138                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6139 
6140   // Different ways of ()-initializiation.
6141   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6142                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6143   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6144                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6145   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6146                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6147   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6148                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6149 }
6150 
6151 TEST_F(FormatTest, BreaksLongDeclarations) {
6152   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6153                "    AnotherNameForTheLongType;");
6154   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6155                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6156   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6157                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6158   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6159                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6160   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6161                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6162   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6163                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6164   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6165                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6166   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6167                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6168   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6169                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6170   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6171                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6172   FormatStyle Indented = getLLVMStyle();
6173   Indented.IndentWrappedFunctionNames = true;
6174   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6175                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6176                Indented);
6177   verifyFormat(
6178       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6179       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6180       Indented);
6181   verifyFormat(
6182       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6183       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6184       Indented);
6185   verifyFormat(
6186       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6187       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6188       Indented);
6189 
6190   // FIXME: Without the comment, this breaks after "(".
6191   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6192                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6193                getGoogleStyle());
6194 
6195   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6196                "                  int LoooooooooooooooooooongParam2) {}");
6197   verifyFormat(
6198       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6199       "                                   SourceLocation L, IdentifierIn *II,\n"
6200       "                                   Type *T) {}");
6201   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6202                "ReallyReaaallyLongFunctionName(\n"
6203                "    const std::string &SomeParameter,\n"
6204                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6205                "        &ReallyReallyLongParameterName,\n"
6206                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6207                "        &AnotherLongParameterName) {}");
6208   verifyFormat("template <typename A>\n"
6209                "SomeLoooooooooooooooooooooongType<\n"
6210                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6211                "Function() {}");
6212 
6213   verifyGoogleFormat(
6214       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6215       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6216   verifyGoogleFormat(
6217       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6218       "                                   SourceLocation L) {}");
6219   verifyGoogleFormat(
6220       "some_namespace::LongReturnType\n"
6221       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6222       "    int first_long_parameter, int second_parameter) {}");
6223 
6224   verifyGoogleFormat("template <typename T>\n"
6225                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6226                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6227   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6228                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6229 
6230   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6231                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6232                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6233   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6234                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6235                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6236   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6237                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6238                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6239                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6240 }
6241 
6242 TEST_F(FormatTest, FormatsArrays) {
6243   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6244                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6245   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6246                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6247   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6248                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6249   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6250                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6251   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6252                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6253   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6254                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6255                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6256   verifyFormat(
6257       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6258       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6259       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6260   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6261                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6262 
6263   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6264                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6265   verifyFormat(
6266       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6267       "                                  .aaaaaaa[0]\n"
6268       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6269   verifyFormat("a[::b::c];");
6270 
6271   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6272 
6273   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6274   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6275 }
6276 
6277 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6278   verifyFormat("(a)->b();");
6279   verifyFormat("--a;");
6280 }
6281 
6282 TEST_F(FormatTest, HandlesIncludeDirectives) {
6283   verifyFormat("#include <string>\n"
6284                "#include <a/b/c.h>\n"
6285                "#include \"a/b/string\"\n"
6286                "#include \"string.h\"\n"
6287                "#include \"string.h\"\n"
6288                "#include <a-a>\n"
6289                "#include < path with space >\n"
6290                "#include_next <test.h>"
6291                "#include \"abc.h\" // this is included for ABC\n"
6292                "#include \"some long include\" // with a comment\n"
6293                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6294                getLLVMStyleWithColumns(35));
6295   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6296   EXPECT_EQ("#include <a>", format("#include<a>"));
6297 
6298   verifyFormat("#import <string>");
6299   verifyFormat("#import <a/b/c.h>");
6300   verifyFormat("#import \"a/b/string\"");
6301   verifyFormat("#import \"string.h\"");
6302   verifyFormat("#import \"string.h\"");
6303   verifyFormat("#if __has_include(<strstream>)\n"
6304                "#include <strstream>\n"
6305                "#endif");
6306 
6307   verifyFormat("#define MY_IMPORT <a/b>");
6308 
6309   // Protocol buffer definition or missing "#".
6310   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6311                getLLVMStyleWithColumns(30));
6312 
6313   FormatStyle Style = getLLVMStyle();
6314   Style.AlwaysBreakBeforeMultilineStrings = true;
6315   Style.ColumnLimit = 0;
6316   verifyFormat("#import \"abc.h\"", Style);
6317 
6318   // But 'import' might also be a regular C++ namespace.
6319   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6320                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6321 }
6322 
6323 //===----------------------------------------------------------------------===//
6324 // Error recovery tests.
6325 //===----------------------------------------------------------------------===//
6326 
6327 TEST_F(FormatTest, IncompleteParameterLists) {
6328   FormatStyle NoBinPacking = getLLVMStyle();
6329   NoBinPacking.BinPackParameters = false;
6330   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6331                "                        double *min_x,\n"
6332                "                        double *max_x,\n"
6333                "                        double *min_y,\n"
6334                "                        double *max_y,\n"
6335                "                        double *min_z,\n"
6336                "                        double *max_z, ) {}",
6337                NoBinPacking);
6338 }
6339 
6340 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6341   verifyFormat("void f() { return; }\n42");
6342   verifyFormat("void f() {\n"
6343                "  if (0)\n"
6344                "    return;\n"
6345                "}\n"
6346                "42");
6347   verifyFormat("void f() { return }\n42");
6348   verifyFormat("void f() {\n"
6349                "  if (0)\n"
6350                "    return\n"
6351                "}\n"
6352                "42");
6353 }
6354 
6355 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6356   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6357   EXPECT_EQ("void f() {\n"
6358             "  if (a)\n"
6359             "    return\n"
6360             "}",
6361             format("void  f  (  )  {  if  ( a )  return  }"));
6362   EXPECT_EQ("namespace N {\n"
6363             "void f()\n"
6364             "}",
6365             format("namespace  N  {  void f()  }"));
6366   EXPECT_EQ("namespace N {\n"
6367             "void f() {}\n"
6368             "void g()\n"
6369             "}",
6370             format("namespace N  { void f( ) { } void g( ) }"));
6371 }
6372 
6373 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6374   verifyFormat("int aaaaaaaa =\n"
6375                "    // Overlylongcomment\n"
6376                "    b;",
6377                getLLVMStyleWithColumns(20));
6378   verifyFormat("function(\n"
6379                "    ShortArgument,\n"
6380                "    LoooooooooooongArgument);\n",
6381                getLLVMStyleWithColumns(20));
6382 }
6383 
6384 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6385   verifyFormat("public:");
6386   verifyFormat("class A {\n"
6387                "public\n"
6388                "  void f() {}\n"
6389                "};");
6390   verifyFormat("public\n"
6391                "int qwerty;");
6392   verifyFormat("public\n"
6393                "B {}");
6394   verifyFormat("public\n"
6395                "{}");
6396   verifyFormat("public\n"
6397                "B { int x; }");
6398 }
6399 
6400 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6401   verifyFormat("{");
6402   verifyFormat("#})");
6403   verifyNoCrash("(/**/[:!] ?[).");
6404 }
6405 
6406 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6407   verifyFormat("do {\n}");
6408   verifyFormat("do {\n}\n"
6409                "f();");
6410   verifyFormat("do {\n}\n"
6411                "wheeee(fun);");
6412   verifyFormat("do {\n"
6413                "  f();\n"
6414                "}");
6415 }
6416 
6417 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6418   verifyFormat("if {\n  foo;\n  foo();\n}");
6419   verifyFormat("switch {\n  foo;\n  foo();\n}");
6420   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6421   verifyFormat("while {\n  foo;\n  foo();\n}");
6422   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6423 }
6424 
6425 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6426   verifyIncompleteFormat("namespace {\n"
6427                          "class Foo { Foo (\n"
6428                          "};\n"
6429                          "} // comment");
6430 }
6431 
6432 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6433   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6434   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6435   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6436   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6437 
6438   EXPECT_EQ("{\n"
6439             "  {\n"
6440             "    breakme(\n"
6441             "        qwe);\n"
6442             "  }\n",
6443             format("{\n"
6444                    "    {\n"
6445                    " breakme(qwe);\n"
6446                    "}\n",
6447                    getLLVMStyleWithColumns(10)));
6448 }
6449 
6450 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6451   verifyFormat("int x = {\n"
6452                "    avariable,\n"
6453                "    b(alongervariable)};",
6454                getLLVMStyleWithColumns(25));
6455 }
6456 
6457 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6458   verifyFormat("return (a)(b){1, 2, 3};");
6459 }
6460 
6461 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6462   verifyFormat("vector<int> x{1, 2, 3, 4};");
6463   verifyFormat("vector<int> x{\n"
6464                "    1, 2, 3, 4,\n"
6465                "};");
6466   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6467   verifyFormat("f({1, 2});");
6468   verifyFormat("auto v = Foo{-1};");
6469   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6470   verifyFormat("Class::Class : member{1, 2, 3} {}");
6471   verifyFormat("new vector<int>{1, 2, 3};");
6472   verifyFormat("new int[3]{1, 2, 3};");
6473   verifyFormat("new int{1};");
6474   verifyFormat("return {arg1, arg2};");
6475   verifyFormat("return {arg1, SomeType{parameter}};");
6476   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6477   verifyFormat("new T{arg1, arg2};");
6478   verifyFormat("f(MyMap[{composite, key}]);");
6479   verifyFormat("class Class {\n"
6480                "  T member = {arg1, arg2};\n"
6481                "};");
6482   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6483   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6484   verifyFormat("int a = std::is_integral<int>{} + 0;");
6485 
6486   verifyFormat("int foo(int i) { return fo1{}(i); }");
6487   verifyFormat("int foo(int i) { return fo1{}(i); }");
6488   verifyFormat("auto i = decltype(x){};");
6489   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6490   verifyFormat("Node n{1, Node{1000}, //\n"
6491                "       2};");
6492   verifyFormat("Aaaa aaaaaaa{\n"
6493                "    {\n"
6494                "        aaaa,\n"
6495                "    },\n"
6496                "};");
6497   verifyFormat("class C : public D {\n"
6498                "  SomeClass SC{2};\n"
6499                "};");
6500   verifyFormat("class C : public A {\n"
6501                "  class D : public B {\n"
6502                "    void f() { int i{2}; }\n"
6503                "  };\n"
6504                "};");
6505   verifyFormat("#define A {a, a},");
6506 
6507   // In combination with BinPackArguments = false.
6508   FormatStyle NoBinPacking = getLLVMStyle();
6509   NoBinPacking.BinPackArguments = false;
6510   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6511                "                      bbbbb,\n"
6512                "                      ccccc,\n"
6513                "                      ddddd,\n"
6514                "                      eeeee,\n"
6515                "                      ffffff,\n"
6516                "                      ggggg,\n"
6517                "                      hhhhhh,\n"
6518                "                      iiiiii,\n"
6519                "                      jjjjjj,\n"
6520                "                      kkkkkk};",
6521                NoBinPacking);
6522   verifyFormat("const Aaaaaa aaaaa = {\n"
6523                "    aaaaa,\n"
6524                "    bbbbb,\n"
6525                "    ccccc,\n"
6526                "    ddddd,\n"
6527                "    eeeee,\n"
6528                "    ffffff,\n"
6529                "    ggggg,\n"
6530                "    hhhhhh,\n"
6531                "    iiiiii,\n"
6532                "    jjjjjj,\n"
6533                "    kkkkkk,\n"
6534                "};",
6535                NoBinPacking);
6536   verifyFormat(
6537       "const Aaaaaa aaaaa = {\n"
6538       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6539       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6540       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6541       "};",
6542       NoBinPacking);
6543 
6544   // FIXME: The alignment of these trailing comments might be bad. Then again,
6545   // this might be utterly useless in real code.
6546   verifyFormat("Constructor::Constructor()\n"
6547                "    : some_value{         //\n"
6548                "                 aaaaaaa, //\n"
6549                "                 bbbbbbb} {}");
6550 
6551   // In braced lists, the first comment is always assumed to belong to the
6552   // first element. Thus, it can be moved to the next or previous line as
6553   // appropriate.
6554   EXPECT_EQ("function({// First element:\n"
6555             "          1,\n"
6556             "          // Second element:\n"
6557             "          2});",
6558             format("function({\n"
6559                    "    // First element:\n"
6560                    "    1,\n"
6561                    "    // Second element:\n"
6562                    "    2});"));
6563   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6564             "    // First element:\n"
6565             "    1,\n"
6566             "    // Second element:\n"
6567             "    2};",
6568             format("std::vector<int> MyNumbers{// First element:\n"
6569                    "                           1,\n"
6570                    "                           // Second element:\n"
6571                    "                           2};",
6572                    getLLVMStyleWithColumns(30)));
6573   // A trailing comma should still lead to an enforced line break.
6574   EXPECT_EQ("vector<int> SomeVector = {\n"
6575             "    // aaa\n"
6576             "    1, 2,\n"
6577             "};",
6578             format("vector<int> SomeVector = { // aaa\n"
6579                    "    1, 2, };"));
6580 
6581   FormatStyle ExtraSpaces = getLLVMStyle();
6582   ExtraSpaces.Cpp11BracedListStyle = false;
6583   ExtraSpaces.ColumnLimit = 75;
6584   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6585   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6586   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6587   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6588   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6589   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6590   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6591   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6592   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6593   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6594   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6595   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6596   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6597   verifyFormat("class Class {\n"
6598                "  T member = { arg1, arg2 };\n"
6599                "};",
6600                ExtraSpaces);
6601   verifyFormat(
6602       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6603       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6604       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6605       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6606       ExtraSpaces);
6607   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6608   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6609                ExtraSpaces);
6610   verifyFormat(
6611       "someFunction(OtherParam,\n"
6612       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6613       "                         param1, param2,\n"
6614       "                         // comment 2\n"
6615       "                         param3, param4 });",
6616       ExtraSpaces);
6617   verifyFormat(
6618       "std::this_thread::sleep_for(\n"
6619       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6620       ExtraSpaces);
6621   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6622                "    aaaaaaa,\n"
6623                "    aaaaaaaaaa,\n"
6624                "    aaaaa,\n"
6625                "    aaaaaaaaaaaaaaa,\n"
6626                "    aaa,\n"
6627                "    aaaaaaaaaa,\n"
6628                "    a,\n"
6629                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6630                "    aaaaaaaaaaaa,\n"
6631                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6632                "    aaaaaaa,\n"
6633                "    a};");
6634   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6635 }
6636 
6637 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6638   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6639                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6640                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6641                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6642                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6643                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6644   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6645                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6646                "                 1, 22, 333, 4444, 55555, //\n"
6647                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6648                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6649   verifyFormat(
6650       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6651       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6652       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6653       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6654       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6655       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6656       "                 7777777};");
6657   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6658                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6659                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6660   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6661                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6662                "    // Separating comment.\n"
6663                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6664   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6665                "    // Leading comment\n"
6666                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6667                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6668   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6669                "                 1, 1, 1, 1};",
6670                getLLVMStyleWithColumns(39));
6671   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6672                "                 1, 1, 1, 1};",
6673                getLLVMStyleWithColumns(38));
6674   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6675                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6676                getLLVMStyleWithColumns(43));
6677   verifyFormat(
6678       "static unsigned SomeValues[10][3] = {\n"
6679       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6680       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6681   verifyFormat("static auto fields = new vector<string>{\n"
6682                "    \"aaaaaaaaaaaaa\",\n"
6683                "    \"aaaaaaaaaaaaa\",\n"
6684                "    \"aaaaaaaaaaaa\",\n"
6685                "    \"aaaaaaaaaaaaaa\",\n"
6686                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6687                "    \"aaaaaaaaaaaa\",\n"
6688                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6689                "};");
6690   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6691   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6692                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6693                "                 3, cccccccccccccccccccccc};",
6694                getLLVMStyleWithColumns(60));
6695 
6696   // Trailing commas.
6697   verifyFormat("vector<int> x = {\n"
6698                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6699                "};",
6700                getLLVMStyleWithColumns(39));
6701   verifyFormat("vector<int> x = {\n"
6702                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6703                "};",
6704                getLLVMStyleWithColumns(39));
6705   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6706                "                 1, 1, 1, 1,\n"
6707                "                 /**/ /**/};",
6708                getLLVMStyleWithColumns(39));
6709 
6710   // Trailing comment in the first line.
6711   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6712                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6713                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6714                "    11111111,   22222222,   333333333,   44444444};");
6715   // Trailing comment in the last line.
6716   verifyFormat("int aaaaa[] = {\n"
6717                "    1, 2, 3, // comment\n"
6718                "    4, 5, 6  // comment\n"
6719                "};");
6720 
6721   // With nested lists, we should either format one item per line or all nested
6722   // lists one on line.
6723   // FIXME: For some nested lists, we can do better.
6724   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6725                "        {aaaaaaaaaaaaaaaaaaa},\n"
6726                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6727                "        {aaaaaaaaaaaaaaaaa}};",
6728                getLLVMStyleWithColumns(60));
6729   verifyFormat(
6730       "SomeStruct my_struct_array = {\n"
6731       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6732       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6733       "    {aaa, aaa},\n"
6734       "    {aaa, aaa},\n"
6735       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6736       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6737       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6738 
6739   // No column layout should be used here.
6740   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6741                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6742 
6743   verifyNoCrash("a<,");
6744 
6745   // No braced initializer here.
6746   verifyFormat("void f() {\n"
6747                "  struct Dummy {};\n"
6748                "  f(v);\n"
6749                "}");
6750 
6751   // Long lists should be formatted in columns even if they are nested.
6752   verifyFormat(
6753       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6754       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6755       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6756       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6757       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6758       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6759 }
6760 
6761 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6762   FormatStyle DoNotMerge = getLLVMStyle();
6763   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6764 
6765   verifyFormat("void f() { return 42; }");
6766   verifyFormat("void f() {\n"
6767                "  return 42;\n"
6768                "}",
6769                DoNotMerge);
6770   verifyFormat("void f() {\n"
6771                "  // Comment\n"
6772                "}");
6773   verifyFormat("{\n"
6774                "#error {\n"
6775                "  int a;\n"
6776                "}");
6777   verifyFormat("{\n"
6778                "  int a;\n"
6779                "#error {\n"
6780                "}");
6781   verifyFormat("void f() {} // comment");
6782   verifyFormat("void f() { int a; } // comment");
6783   verifyFormat("void f() {\n"
6784                "} // comment",
6785                DoNotMerge);
6786   verifyFormat("void f() {\n"
6787                "  int a;\n"
6788                "} // comment",
6789                DoNotMerge);
6790   verifyFormat("void f() {\n"
6791                "} // comment",
6792                getLLVMStyleWithColumns(15));
6793 
6794   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6795   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6796 
6797   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6798   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6799   verifyFormat("class C {\n"
6800                "  C()\n"
6801                "      : iiiiiiii(nullptr),\n"
6802                "        kkkkkkk(nullptr),\n"
6803                "        mmmmmmm(nullptr),\n"
6804                "        nnnnnnn(nullptr) {}\n"
6805                "};",
6806                getGoogleStyle());
6807 
6808   FormatStyle NoColumnLimit = getLLVMStyle();
6809   NoColumnLimit.ColumnLimit = 0;
6810   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6811   EXPECT_EQ("class C {\n"
6812             "  A() : b(0) {}\n"
6813             "};",
6814             format("class C{A():b(0){}};", NoColumnLimit));
6815   EXPECT_EQ("A()\n"
6816             "    : b(0) {\n"
6817             "}",
6818             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6819 
6820   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6821   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6822       FormatStyle::SFS_None;
6823   EXPECT_EQ("A()\n"
6824             "    : b(0) {\n"
6825             "}",
6826             format("A():b(0){}", DoNotMergeNoColumnLimit));
6827   EXPECT_EQ("A()\n"
6828             "    : b(0) {\n"
6829             "}",
6830             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6831 
6832   verifyFormat("#define A          \\\n"
6833                "  void f() {       \\\n"
6834                "    int i;         \\\n"
6835                "  }",
6836                getLLVMStyleWithColumns(20));
6837   verifyFormat("#define A           \\\n"
6838                "  void f() { int i; }",
6839                getLLVMStyleWithColumns(21));
6840   verifyFormat("#define A            \\\n"
6841                "  void f() {         \\\n"
6842                "    int i;           \\\n"
6843                "  }                  \\\n"
6844                "  int j;",
6845                getLLVMStyleWithColumns(22));
6846   verifyFormat("#define A             \\\n"
6847                "  void f() { int i; } \\\n"
6848                "  int j;",
6849                getLLVMStyleWithColumns(23));
6850 }
6851 
6852 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6853   FormatStyle MergeInlineOnly = getLLVMStyle();
6854   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6855   verifyFormat("class C {\n"
6856                "  int f() { return 42; }\n"
6857                "};",
6858                MergeInlineOnly);
6859   verifyFormat("int f() {\n"
6860                "  return 42;\n"
6861                "}",
6862                MergeInlineOnly);
6863 }
6864 
6865 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6866   // Elaborate type variable declarations.
6867   verifyFormat("struct foo a = {bar};\nint n;");
6868   verifyFormat("class foo a = {bar};\nint n;");
6869   verifyFormat("union foo a = {bar};\nint n;");
6870 
6871   // Elaborate types inside function definitions.
6872   verifyFormat("struct foo f() {}\nint n;");
6873   verifyFormat("class foo f() {}\nint n;");
6874   verifyFormat("union foo f() {}\nint n;");
6875 
6876   // Templates.
6877   verifyFormat("template <class X> void f() {}\nint n;");
6878   verifyFormat("template <struct X> void f() {}\nint n;");
6879   verifyFormat("template <union X> void f() {}\nint n;");
6880 
6881   // Actual definitions...
6882   verifyFormat("struct {\n} n;");
6883   verifyFormat(
6884       "template <template <class T, class Y>, class Z> class X {\n} n;");
6885   verifyFormat("union Z {\n  int n;\n} x;");
6886   verifyFormat("class MACRO Z {\n} n;");
6887   verifyFormat("class MACRO(X) Z {\n} n;");
6888   verifyFormat("class __attribute__(X) Z {\n} n;");
6889   verifyFormat("class __declspec(X) Z {\n} n;");
6890   verifyFormat("class A##B##C {\n} n;");
6891   verifyFormat("class alignas(16) Z {\n} n;");
6892   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6893   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6894 
6895   // Redefinition from nested context:
6896   verifyFormat("class A::B::C {\n} n;");
6897 
6898   // Template definitions.
6899   verifyFormat(
6900       "template <typename F>\n"
6901       "Matcher(const Matcher<F> &Other,\n"
6902       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6903       "                             !is_same<F, T>::value>::type * = 0)\n"
6904       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6905 
6906   // FIXME: This is still incorrectly handled at the formatter side.
6907   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6908   verifyFormat("int i = SomeFunction(a<b, a> b);");
6909 
6910   // FIXME:
6911   // This now gets parsed incorrectly as class definition.
6912   // verifyFormat("class A<int> f() {\n}\nint n;");
6913 
6914   // Elaborate types where incorrectly parsing the structural element would
6915   // break the indent.
6916   verifyFormat("if (true)\n"
6917                "  class X x;\n"
6918                "else\n"
6919                "  f();\n");
6920 
6921   // This is simply incomplete. Formatting is not important, but must not crash.
6922   verifyFormat("class A:");
6923 }
6924 
6925 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6926   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6927             format("#error Leave     all         white!!!!! space* alone!\n"));
6928   EXPECT_EQ(
6929       "#warning Leave     all         white!!!!! space* alone!\n",
6930       format("#warning Leave     all         white!!!!! space* alone!\n"));
6931   EXPECT_EQ("#error 1", format("  #  error   1"));
6932   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6933 }
6934 
6935 TEST_F(FormatTest, FormatHashIfExpressions) {
6936   verifyFormat("#if AAAA && BBBB");
6937   verifyFormat("#if (AAAA && BBBB)");
6938   verifyFormat("#elif (AAAA && BBBB)");
6939   // FIXME: Come up with a better indentation for #elif.
6940   verifyFormat(
6941       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6942       "    defined(BBBBBBBB)\n"
6943       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6944       "    defined(BBBBBBBB)\n"
6945       "#endif",
6946       getLLVMStyleWithColumns(65));
6947 }
6948 
6949 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6950   FormatStyle AllowsMergedIf = getGoogleStyle();
6951   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6952   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6953   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6954   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6955   EXPECT_EQ("if (true) return 42;",
6956             format("if (true)\nreturn 42;", AllowsMergedIf));
6957   FormatStyle ShortMergedIf = AllowsMergedIf;
6958   ShortMergedIf.ColumnLimit = 25;
6959   verifyFormat("#define A \\\n"
6960                "  if (true) return 42;",
6961                ShortMergedIf);
6962   verifyFormat("#define A \\\n"
6963                "  f();    \\\n"
6964                "  if (true)\n"
6965                "#define B",
6966                ShortMergedIf);
6967   verifyFormat("#define A \\\n"
6968                "  f();    \\\n"
6969                "  if (true)\n"
6970                "g();",
6971                ShortMergedIf);
6972   verifyFormat("{\n"
6973                "#ifdef A\n"
6974                "  // Comment\n"
6975                "  if (true) continue;\n"
6976                "#endif\n"
6977                "  // Comment\n"
6978                "  if (true) continue;\n"
6979                "}",
6980                ShortMergedIf);
6981   ShortMergedIf.ColumnLimit = 29;
6982   verifyFormat("#define A                   \\\n"
6983                "  if (aaaaaaaaaa) return 1; \\\n"
6984                "  return 2;",
6985                ShortMergedIf);
6986   ShortMergedIf.ColumnLimit = 28;
6987   verifyFormat("#define A         \\\n"
6988                "  if (aaaaaaaaaa) \\\n"
6989                "    return 1;     \\\n"
6990                "  return 2;",
6991                ShortMergedIf);
6992 }
6993 
6994 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6995   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6996                "  f();\n"
6997                "}");
6998   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6999                "  f();\n"
7000                "} /* another comment */ else /* comment #3 */ {\n"
7001                "  g();\n"
7002                "}");
7003   verifyFormat("while (0) /* a comment in a strange place */ {\n"
7004                "  f();\n"
7005                "}");
7006   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
7007                "  f();\n"
7008                "}");
7009   verifyFormat("do /* a comment in a strange place */ {\n"
7010                "  f();\n"
7011                "} /* another comment */ while (0);");
7012 }
7013 
7014 TEST_F(FormatTest, BlockComments) {
7015   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
7016             format("/* *//* */  /* */\n/* *//* */  /* */"));
7017   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
7018   EXPECT_EQ("#define A /*123*/ \\\n"
7019             "  b\n"
7020             "/* */\n"
7021             "someCall(\n"
7022             "    parameter);",
7023             format("#define A /*123*/ b\n"
7024                    "/* */\n"
7025                    "someCall(parameter);",
7026                    getLLVMStyleWithColumns(15)));
7027 
7028   EXPECT_EQ("#define A\n"
7029             "/* */ someCall(\n"
7030             "    parameter);",
7031             format("#define A\n"
7032                    "/* */someCall(parameter);",
7033                    getLLVMStyleWithColumns(15)));
7034   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
7035   EXPECT_EQ("/*\n"
7036             "*\n"
7037             " * aaaaaa\n"
7038             " * aaaaaa\n"
7039             "*/",
7040             format("/*\n"
7041                    "*\n"
7042                    " * aaaaaa aaaaaa\n"
7043                    "*/",
7044                    getLLVMStyleWithColumns(10)));
7045   EXPECT_EQ("/*\n"
7046             "**\n"
7047             "* aaaaaa\n"
7048             "*aaaaaa\n"
7049             "*/",
7050             format("/*\n"
7051                    "**\n"
7052                    "* aaaaaa aaaaaa\n"
7053                    "*/",
7054                    getLLVMStyleWithColumns(10)));
7055   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7056             "    /* line 1\n"
7057             "       bbbbbbbbbbbb */\n"
7058             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7059             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7060                    "    /* line 1\n"
7061                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7062             getLLVMStyleWithColumns(50)));
7063 
7064   FormatStyle NoBinPacking = getLLVMStyle();
7065   NoBinPacking.BinPackParameters = false;
7066   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
7067             "             2, /* comment 2 */\n"
7068             "             3, /* comment 3 */\n"
7069             "             aaaa,\n"
7070             "             bbbb);",
7071             format("someFunction (1,   /* comment 1 */\n"
7072                    "                2, /* comment 2 */  \n"
7073                    "               3,   /* comment 3 */\n"
7074                    "aaaa, bbbb );",
7075                    NoBinPacking));
7076   verifyFormat(
7077       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7078       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7079   EXPECT_EQ(
7080       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
7081       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7082       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
7083       format(
7084           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
7085           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
7086           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
7087   EXPECT_EQ(
7088       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7089       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
7090       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
7091       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7092              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
7093              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
7094 
7095   verifyFormat("void f(int * /* unused */) {}");
7096 
7097   EXPECT_EQ("/*\n"
7098             " **\n"
7099             " */",
7100             format("/*\n"
7101                    " **\n"
7102                    " */"));
7103   EXPECT_EQ("/*\n"
7104             " *q\n"
7105             " */",
7106             format("/*\n"
7107                    " *q\n"
7108                    " */"));
7109   EXPECT_EQ("/*\n"
7110             " * q\n"
7111             " */",
7112             format("/*\n"
7113                    " * q\n"
7114                    " */"));
7115   EXPECT_EQ("/*\n"
7116             " **/",
7117             format("/*\n"
7118                    " **/"));
7119   EXPECT_EQ("/*\n"
7120             " ***/",
7121             format("/*\n"
7122                    " ***/"));
7123 }
7124 
7125 TEST_F(FormatTest, BlockCommentsInMacros) {
7126   EXPECT_EQ("#define A          \\\n"
7127             "  {                \\\n"
7128             "    /* one line */ \\\n"
7129             "    someCall();",
7130             format("#define A {        \\\n"
7131                    "  /* one line */   \\\n"
7132                    "  someCall();",
7133                    getLLVMStyleWithColumns(20)));
7134   EXPECT_EQ("#define A          \\\n"
7135             "  {                \\\n"
7136             "    /* previous */ \\\n"
7137             "    /* one line */ \\\n"
7138             "    someCall();",
7139             format("#define A {        \\\n"
7140                    "  /* previous */   \\\n"
7141                    "  /* one line */   \\\n"
7142                    "  someCall();",
7143                    getLLVMStyleWithColumns(20)));
7144 }
7145 
7146 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7147   EXPECT_EQ("a = {\n"
7148             "    1111 /*    */\n"
7149             "};",
7150             format("a = {1111 /*    */\n"
7151                    "};",
7152                    getLLVMStyleWithColumns(15)));
7153   EXPECT_EQ("a = {\n"
7154             "    1111 /*      */\n"
7155             "};",
7156             format("a = {1111 /*      */\n"
7157                    "};",
7158                    getLLVMStyleWithColumns(15)));
7159 
7160   // FIXME: The formatting is still wrong here.
7161   EXPECT_EQ("a = {\n"
7162             "    1111 /*      a\n"
7163             "            */\n"
7164             "};",
7165             format("a = {1111 /*      a */\n"
7166                    "};",
7167                    getLLVMStyleWithColumns(15)));
7168 }
7169 
7170 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7171   verifyFormat("{\n"
7172                "  // a\n"
7173                "  // b");
7174 }
7175 
7176 TEST_F(FormatTest, FormatStarDependingOnContext) {
7177   verifyFormat("void f(int *a);");
7178   verifyFormat("void f() { f(fint * b); }");
7179   verifyFormat("class A {\n  void f(int *a);\n};");
7180   verifyFormat("class A {\n  int *a;\n};");
7181   verifyFormat("namespace a {\n"
7182                "namespace b {\n"
7183                "class A {\n"
7184                "  void f() {}\n"
7185                "  int *a;\n"
7186                "};\n"
7187                "}\n"
7188                "}");
7189 }
7190 
7191 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7192   verifyFormat("while");
7193   verifyFormat("operator");
7194 }
7195 
7196 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7197   // This code would be painfully slow to format if we didn't skip it.
7198   std::string Code("A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n" // 20x
7199                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7200                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7201                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7202                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7203                    "A(1, 1)\n"
7204                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7205                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7206                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7207                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7208                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7209                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7210                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7211                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7212                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7213                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7214   // Deeply nested part is untouched, rest is formatted.
7215   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7216             format(std::string("int    i;\n") + Code + "int    j;\n",
7217                    getLLVMStyle(), IC_ExpectIncomplete));
7218 }
7219 
7220 //===----------------------------------------------------------------------===//
7221 // Objective-C tests.
7222 //===----------------------------------------------------------------------===//
7223 
7224 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7225   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7226   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7227             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7228   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7229   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7230   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7231             format("-(NSInteger)Method3:(id)anObject;"));
7232   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7233             format("-(NSInteger)Method4:(id)anObject;"));
7234   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7235             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7236   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7237             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7238   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7239             "forAllCells:(BOOL)flag;",
7240             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7241                    "forAllCells:(BOOL)flag;"));
7242 
7243   // Very long objectiveC method declaration.
7244   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7245                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7246   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7247                "                    inRange:(NSRange)range\n"
7248                "                   outRange:(NSRange)out_range\n"
7249                "                  outRange1:(NSRange)out_range1\n"
7250                "                  outRange2:(NSRange)out_range2\n"
7251                "                  outRange3:(NSRange)out_range3\n"
7252                "                  outRange4:(NSRange)out_range4\n"
7253                "                  outRange5:(NSRange)out_range5\n"
7254                "                  outRange6:(NSRange)out_range6\n"
7255                "                  outRange7:(NSRange)out_range7\n"
7256                "                  outRange8:(NSRange)out_range8\n"
7257                "                  outRange9:(NSRange)out_range9;");
7258 
7259   // When the function name has to be wrapped.
7260   FormatStyle Style = getLLVMStyle();
7261   Style.IndentWrappedFunctionNames = false;
7262   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7263                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7264                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7265                "}",
7266                Style);
7267   Style.IndentWrappedFunctionNames = true;
7268   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7269                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7270                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7271                "}",
7272                Style);
7273 
7274   verifyFormat("- (int)sum:(vector<int>)numbers;");
7275   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7276   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7277   // protocol lists (but not for template classes):
7278   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7279 
7280   verifyFormat("- (int (*)())foo:(int (*)())f;");
7281   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7282 
7283   // If there's no return type (very rare in practice!), LLVM and Google style
7284   // agree.
7285   verifyFormat("- foo;");
7286   verifyFormat("- foo:(int)f;");
7287   verifyGoogleFormat("- foo:(int)foo;");
7288 }
7289 
7290 
7291 TEST_F(FormatTest, BreaksStringLiterals) {
7292   EXPECT_EQ("\"some text \"\n"
7293             "\"other\";",
7294             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7295   EXPECT_EQ("\"some text \"\n"
7296             "\"other\";",
7297             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7298   EXPECT_EQ(
7299       "#define A  \\\n"
7300       "  \"some \"  \\\n"
7301       "  \"text \"  \\\n"
7302       "  \"other\";",
7303       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7304   EXPECT_EQ(
7305       "#define A  \\\n"
7306       "  \"so \"    \\\n"
7307       "  \"text \"  \\\n"
7308       "  \"other\";",
7309       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7310 
7311   EXPECT_EQ("\"some text\"",
7312             format("\"some text\"", getLLVMStyleWithColumns(1)));
7313   EXPECT_EQ("\"some text\"",
7314             format("\"some text\"", getLLVMStyleWithColumns(11)));
7315   EXPECT_EQ("\"some \"\n"
7316             "\"text\"",
7317             format("\"some text\"", getLLVMStyleWithColumns(10)));
7318   EXPECT_EQ("\"some \"\n"
7319             "\"text\"",
7320             format("\"some text\"", getLLVMStyleWithColumns(7)));
7321   EXPECT_EQ("\"some\"\n"
7322             "\" tex\"\n"
7323             "\"t\"",
7324             format("\"some text\"", getLLVMStyleWithColumns(6)));
7325   EXPECT_EQ("\"some\"\n"
7326             "\" tex\"\n"
7327             "\" and\"",
7328             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7329   EXPECT_EQ("\"some\"\n"
7330             "\"/tex\"\n"
7331             "\"/and\"",
7332             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7333 
7334   EXPECT_EQ("variable =\n"
7335             "    \"long string \"\n"
7336             "    \"literal\";",
7337             format("variable = \"long string literal\";",
7338                    getLLVMStyleWithColumns(20)));
7339 
7340   EXPECT_EQ("variable = f(\n"
7341             "    \"long string \"\n"
7342             "    \"literal\",\n"
7343             "    short,\n"
7344             "    loooooooooooooooooooong);",
7345             format("variable = f(\"long string literal\", short, "
7346                    "loooooooooooooooooooong);",
7347                    getLLVMStyleWithColumns(20)));
7348 
7349   EXPECT_EQ(
7350       "f(g(\"long string \"\n"
7351       "    \"literal\"),\n"
7352       "  b);",
7353       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7354   EXPECT_EQ("f(g(\"long string \"\n"
7355             "    \"literal\",\n"
7356             "    a),\n"
7357             "  b);",
7358             format("f(g(\"long string literal\", a), b);",
7359                    getLLVMStyleWithColumns(20)));
7360   EXPECT_EQ(
7361       "f(\"one two\".split(\n"
7362       "    variable));",
7363       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7364   EXPECT_EQ("f(\"one two three four five six \"\n"
7365             "  \"seven\".split(\n"
7366             "      really_looooong_variable));",
7367             format("f(\"one two three four five six seven\"."
7368                    "split(really_looooong_variable));",
7369                    getLLVMStyleWithColumns(33)));
7370 
7371   EXPECT_EQ("f(\"some \"\n"
7372             "  \"text\",\n"
7373             "  other);",
7374             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7375 
7376   // Only break as a last resort.
7377   verifyFormat(
7378       "aaaaaaaaaaaaaaaaaaaa(\n"
7379       "    aaaaaaaaaaaaaaaaaaaa,\n"
7380       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7381 
7382   EXPECT_EQ("\"splitmea\"\n"
7383             "\"trandomp\"\n"
7384             "\"oint\"",
7385             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7386 
7387   EXPECT_EQ("\"split/\"\n"
7388             "\"pathat/\"\n"
7389             "\"slashes\"",
7390             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7391 
7392   EXPECT_EQ("\"split/\"\n"
7393             "\"pathat/\"\n"
7394             "\"slashes\"",
7395             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7396   EXPECT_EQ("\"split at \"\n"
7397             "\"spaces/at/\"\n"
7398             "\"slashes.at.any$\"\n"
7399             "\"non-alphanumeric%\"\n"
7400             "\"1111111111characte\"\n"
7401             "\"rs\"",
7402             format("\"split at "
7403                    "spaces/at/"
7404                    "slashes.at."
7405                    "any$non-"
7406                    "alphanumeric%"
7407                    "1111111111characte"
7408                    "rs\"",
7409                    getLLVMStyleWithColumns(20)));
7410 
7411   // Verify that splitting the strings understands
7412   // Style::AlwaysBreakBeforeMultilineStrings.
7413   EXPECT_EQ(
7414       "aaaaaaaaaaaa(\n"
7415       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7416       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7417       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7418              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7419              "aaaaaaaaaaaaaaaaaaaaaa\");",
7420              getGoogleStyle()));
7421   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7422             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7423             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7424                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7425                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7426                    getGoogleStyle()));
7427   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7428             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7429             format("llvm::outs() << "
7430                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7431                    "aaaaaaaaaaaaaaaaaaa\";"));
7432   EXPECT_EQ("ffff(\n"
7433             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7434             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7435             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7436                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7437                    getGoogleStyle()));
7438 
7439   FormatStyle Style = getLLVMStyleWithColumns(12);
7440   Style.BreakStringLiterals = false;
7441   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7442 
7443   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7444   AlignLeft.AlignEscapedNewlinesLeft = true;
7445   EXPECT_EQ("#define A \\\n"
7446             "  \"some \" \\\n"
7447             "  \"text \" \\\n"
7448             "  \"other\";",
7449             format("#define A \"some text other\";", AlignLeft));
7450 }
7451 
7452 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7453   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7454   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7455   EXPECT_EQ("int i = a(b());",
7456             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7457 }
7458 
7459 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7460   EXPECT_EQ(
7461       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7462       "(\n"
7463       "    \"x\t\");",
7464       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7465              "aaaaaaa("
7466              "\"x\t\");"));
7467 }
7468 
7469 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7470   EXPECT_EQ(
7471       "u8\"utf8 string \"\n"
7472       "u8\"literal\";",
7473       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7474   EXPECT_EQ(
7475       "u\"utf16 string \"\n"
7476       "u\"literal\";",
7477       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7478   EXPECT_EQ(
7479       "U\"utf32 string \"\n"
7480       "U\"literal\";",
7481       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7482   EXPECT_EQ("L\"wide string \"\n"
7483             "L\"literal\";",
7484             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7485   EXPECT_EQ("@\"NSString \"\n"
7486             "@\"literal\";",
7487             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7488 
7489   // This input makes clang-format try to split the incomplete unicode escape
7490   // sequence, which used to lead to a crasher.
7491   verifyNoCrash(
7492       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7493       getLLVMStyleWithColumns(60));
7494 }
7495 
7496 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7497   FormatStyle Style = getGoogleStyleWithColumns(15);
7498   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7499   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7500   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7501   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7502   EXPECT_EQ("u8R\"x(raw literal)x\";",
7503             format("u8R\"x(raw literal)x\";", Style));
7504 }
7505 
7506 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7507   FormatStyle Style = getLLVMStyleWithColumns(20);
7508   EXPECT_EQ(
7509       "_T(\"aaaaaaaaaaaaaa\")\n"
7510       "_T(\"aaaaaaaaaaaaaa\")\n"
7511       "_T(\"aaaaaaaaaaaa\")",
7512       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7513   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7514             "     _T(\"aaaaaa\"),\n"
7515             "  z);",
7516             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7517 
7518   // FIXME: Handle embedded spaces in one iteration.
7519   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7520   //            "_T(\"aaaaaaaaaaaaa\")\n"
7521   //            "_T(\"aaaaaaaaaaaaa\")\n"
7522   //            "_T(\"a\")",
7523   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7524   //                   getLLVMStyleWithColumns(20)));
7525   EXPECT_EQ(
7526       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7527       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7528   EXPECT_EQ("f(\n"
7529             "#if !TEST\n"
7530             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7531             "#endif\n"
7532             "    );",
7533             format("f(\n"
7534                    "#if !TEST\n"
7535                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7536                    "#endif\n"
7537                    ");"));
7538   EXPECT_EQ("f(\n"
7539             "\n"
7540             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7541             format("f(\n"
7542                    "\n"
7543                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7544 }
7545 
7546 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7547   EXPECT_EQ(
7548       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7549       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7550       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7551       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7552              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7553              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7554 }
7555 
7556 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7557   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7558             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7559   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7560             "multiline raw string literal xxxxxxxxxxxxxx\n"
7561             ")x\",\n"
7562             "              a),\n"
7563             "            b);",
7564             format("fffffffffff(g(R\"x(\n"
7565                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7566                    ")x\", a), b);",
7567                    getGoogleStyleWithColumns(20)));
7568   EXPECT_EQ("fffffffffff(\n"
7569             "    g(R\"x(qqq\n"
7570             "multiline raw string literal xxxxxxxxxxxxxx\n"
7571             ")x\",\n"
7572             "      a),\n"
7573             "    b);",
7574             format("fffffffffff(g(R\"x(qqq\n"
7575                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7576                    ")x\", a), b);",
7577                    getGoogleStyleWithColumns(20)));
7578 
7579   EXPECT_EQ("fffffffffff(R\"x(\n"
7580             "multiline raw string literal xxxxxxxxxxxxxx\n"
7581             ")x\");",
7582             format("fffffffffff(R\"x(\n"
7583                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7584                    ")x\");",
7585                    getGoogleStyleWithColumns(20)));
7586   EXPECT_EQ("fffffffffff(R\"x(\n"
7587             "multiline raw string literal xxxxxxxxxxxxxx\n"
7588             ")x\" + bbbbbb);",
7589             format("fffffffffff(R\"x(\n"
7590                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7591                    ")x\" +   bbbbbb);",
7592                    getGoogleStyleWithColumns(20)));
7593   EXPECT_EQ("fffffffffff(\n"
7594             "    R\"x(\n"
7595             "multiline raw string literal xxxxxxxxxxxxxx\n"
7596             ")x\" +\n"
7597             "    bbbbbb);",
7598             format("fffffffffff(\n"
7599                    " R\"x(\n"
7600                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7601                    ")x\" + bbbbbb);",
7602                    getGoogleStyleWithColumns(20)));
7603 }
7604 
7605 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7606   verifyFormat("string a = \"unterminated;");
7607   EXPECT_EQ("function(\"unterminated,\n"
7608             "         OtherParameter);",
7609             format("function(  \"unterminated,\n"
7610                    "    OtherParameter);"));
7611 }
7612 
7613 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7614   FormatStyle Style = getLLVMStyle();
7615   Style.Standard = FormatStyle::LS_Cpp03;
7616   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7617             format("#define x(_a) printf(\"foo\"_a);", Style));
7618 }
7619 
7620 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7621 
7622 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7623   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7624             "             \"ddeeefff\");",
7625             format("someFunction(\"aaabbbcccdddeeefff\");",
7626                    getLLVMStyleWithColumns(25)));
7627   EXPECT_EQ("someFunction1234567890(\n"
7628             "    \"aaabbbcccdddeeefff\");",
7629             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7630                    getLLVMStyleWithColumns(26)));
7631   EXPECT_EQ("someFunction1234567890(\n"
7632             "    \"aaabbbcccdddeeeff\"\n"
7633             "    \"f\");",
7634             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7635                    getLLVMStyleWithColumns(25)));
7636   EXPECT_EQ("someFunction1234567890(\n"
7637             "    \"aaabbbcccdddeeeff\"\n"
7638             "    \"f\");",
7639             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7640                    getLLVMStyleWithColumns(24)));
7641   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7642             "             \"ddde \"\n"
7643             "             \"efff\");",
7644             format("someFunction(\"aaabbbcc ddde efff\");",
7645                    getLLVMStyleWithColumns(25)));
7646   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7647             "             \"ddeeefff\");",
7648             format("someFunction(\"aaabbbccc ddeeefff\");",
7649                    getLLVMStyleWithColumns(25)));
7650   EXPECT_EQ("someFunction1234567890(\n"
7651             "    \"aaabb \"\n"
7652             "    \"cccdddeeefff\");",
7653             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7654                    getLLVMStyleWithColumns(25)));
7655   EXPECT_EQ("#define A          \\\n"
7656             "  string s =       \\\n"
7657             "      \"123456789\"  \\\n"
7658             "      \"0\";         \\\n"
7659             "  int i;",
7660             format("#define A string s = \"1234567890\"; int i;",
7661                    getLLVMStyleWithColumns(20)));
7662   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7663   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7664             "             \"dddeeeff\"\n"
7665             "             \"f\");",
7666             format("someFunction(\"aaabbbcc dddeeefff\");",
7667                    getLLVMStyleWithColumns(25)));
7668 }
7669 
7670 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7671   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7672   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7673   EXPECT_EQ("\"test\"\n"
7674             "\"\\n\"",
7675             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7676   EXPECT_EQ("\"tes\\\\\"\n"
7677             "\"n\"",
7678             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7679   EXPECT_EQ("\"\\\\\\\\\"\n"
7680             "\"\\n\"",
7681             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7682   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7683   EXPECT_EQ("\"\\uff01\"\n"
7684             "\"test\"",
7685             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7686   EXPECT_EQ("\"\\Uff01ff02\"",
7687             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7688   EXPECT_EQ("\"\\x000000000001\"\n"
7689             "\"next\"",
7690             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7691   EXPECT_EQ("\"\\x000000000001next\"",
7692             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7693   EXPECT_EQ("\"\\x000000000001\"",
7694             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7695   EXPECT_EQ("\"test\"\n"
7696             "\"\\000000\"\n"
7697             "\"000001\"",
7698             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7699   EXPECT_EQ("\"test\\000\"\n"
7700             "\"00000000\"\n"
7701             "\"1\"",
7702             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7703 }
7704 
7705 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7706   verifyFormat("void f() {\n"
7707                "  return g() {}\n"
7708                "  void h() {}");
7709   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7710                "g();\n"
7711                "}");
7712 }
7713 
7714 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7715   verifyFormat(
7716       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7717 }
7718 
7719 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7720   verifyFormat("class X {\n"
7721                "  void f() {\n"
7722                "  }\n"
7723                "};",
7724                getLLVMStyleWithColumns(12));
7725 }
7726 
7727 TEST_F(FormatTest, ConfigurableIndentWidth) {
7728   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7729   EightIndent.IndentWidth = 8;
7730   EightIndent.ContinuationIndentWidth = 8;
7731   verifyFormat("void f() {\n"
7732                "        someFunction();\n"
7733                "        if (true) {\n"
7734                "                f();\n"
7735                "        }\n"
7736                "}",
7737                EightIndent);
7738   verifyFormat("class X {\n"
7739                "        void f() {\n"
7740                "        }\n"
7741                "};",
7742                EightIndent);
7743   verifyFormat("int x[] = {\n"
7744                "        call(),\n"
7745                "        call()};",
7746                EightIndent);
7747 }
7748 
7749 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7750   verifyFormat("double\n"
7751                "f();",
7752                getLLVMStyleWithColumns(8));
7753 }
7754 
7755 TEST_F(FormatTest, ConfigurableUseOfTab) {
7756   FormatStyle Tab = getLLVMStyleWithColumns(42);
7757   Tab.IndentWidth = 8;
7758   Tab.UseTab = FormatStyle::UT_Always;
7759   Tab.AlignEscapedNewlinesLeft = true;
7760 
7761   EXPECT_EQ("if (aaaaaaaa && // q\n"
7762             "    bb)\t\t// w\n"
7763             "\t;",
7764             format("if (aaaaaaaa &&// q\n"
7765                    "bb)// w\n"
7766                    ";",
7767                    Tab));
7768   EXPECT_EQ("if (aaa && bbb) // w\n"
7769             "\t;",
7770             format("if(aaa&&bbb)// w\n"
7771                    ";",
7772                    Tab));
7773 
7774   verifyFormat("class X {\n"
7775                "\tvoid f() {\n"
7776                "\t\tsomeFunction(parameter1,\n"
7777                "\t\t\t     parameter2);\n"
7778                "\t}\n"
7779                "};",
7780                Tab);
7781   verifyFormat("#define A                        \\\n"
7782                "\tvoid f() {               \\\n"
7783                "\t\tsomeFunction(    \\\n"
7784                "\t\t    parameter1,  \\\n"
7785                "\t\t    parameter2); \\\n"
7786                "\t}",
7787                Tab);
7788 
7789   Tab.TabWidth = 4;
7790   Tab.IndentWidth = 8;
7791   verifyFormat("class TabWidth4Indent8 {\n"
7792                "\t\tvoid f() {\n"
7793                "\t\t\t\tsomeFunction(parameter1,\n"
7794                "\t\t\t\t\t\t\t parameter2);\n"
7795                "\t\t}\n"
7796                "};",
7797                Tab);
7798 
7799   Tab.TabWidth = 4;
7800   Tab.IndentWidth = 4;
7801   verifyFormat("class TabWidth4Indent4 {\n"
7802                "\tvoid f() {\n"
7803                "\t\tsomeFunction(parameter1,\n"
7804                "\t\t\t\t\t parameter2);\n"
7805                "\t}\n"
7806                "};",
7807                Tab);
7808 
7809   Tab.TabWidth = 8;
7810   Tab.IndentWidth = 4;
7811   verifyFormat("class TabWidth8Indent4 {\n"
7812                "    void f() {\n"
7813                "\tsomeFunction(parameter1,\n"
7814                "\t\t     parameter2);\n"
7815                "    }\n"
7816                "};",
7817                Tab);
7818 
7819   Tab.TabWidth = 8;
7820   Tab.IndentWidth = 8;
7821   EXPECT_EQ("/*\n"
7822             "\t      a\t\tcomment\n"
7823             "\t      in multiple lines\n"
7824             "       */",
7825             format("   /*\t \t \n"
7826                    " \t \t a\t\tcomment\t \t\n"
7827                    " \t \t in multiple lines\t\n"
7828                    " \t  */",
7829                    Tab));
7830 
7831   Tab.UseTab = FormatStyle::UT_ForIndentation;
7832   verifyFormat("{\n"
7833                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7834                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7835                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7836                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7837                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7838                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7839                "};",
7840                Tab);
7841   verifyFormat("enum AA {\n"
7842                "\ta1, // Force multiple lines\n"
7843                "\ta2,\n"
7844                "\ta3\n"
7845                "};",
7846                Tab);
7847   EXPECT_EQ("if (aaaaaaaa && // q\n"
7848             "    bb)         // w\n"
7849             "\t;",
7850             format("if (aaaaaaaa &&// q\n"
7851                    "bb)// w\n"
7852                    ";",
7853                    Tab));
7854   verifyFormat("class X {\n"
7855                "\tvoid f() {\n"
7856                "\t\tsomeFunction(parameter1,\n"
7857                "\t\t             parameter2);\n"
7858                "\t}\n"
7859                "};",
7860                Tab);
7861   verifyFormat("{\n"
7862                "\tQ(\n"
7863                "\t    {\n"
7864                "\t\t    int a;\n"
7865                "\t\t    someFunction(aaaaaaaa,\n"
7866                "\t\t                 bbbbbbb);\n"
7867                "\t    },\n"
7868                "\t    p);\n"
7869                "}",
7870                Tab);
7871   EXPECT_EQ("{\n"
7872             "\t/* aaaa\n"
7873             "\t   bbbb */\n"
7874             "}",
7875             format("{\n"
7876                    "/* aaaa\n"
7877                    "   bbbb */\n"
7878                    "}",
7879                    Tab));
7880   EXPECT_EQ("{\n"
7881             "\t/*\n"
7882             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7883             "\t  bbbbbbbbbbbbb\n"
7884             "\t*/\n"
7885             "}",
7886             format("{\n"
7887                    "/*\n"
7888                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7889                    "*/\n"
7890                    "}",
7891                    Tab));
7892   EXPECT_EQ("{\n"
7893             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7894             "\t// bbbbbbbbbbbbb\n"
7895             "}",
7896             format("{\n"
7897                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7898                    "}",
7899                    Tab));
7900   EXPECT_EQ("{\n"
7901             "\t/*\n"
7902             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7903             "\t  bbbbbbbbbbbbb\n"
7904             "\t*/\n"
7905             "}",
7906             format("{\n"
7907                    "\t/*\n"
7908                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7909                    "\t*/\n"
7910                    "}",
7911                    Tab));
7912   EXPECT_EQ("{\n"
7913             "\t/*\n"
7914             "\n"
7915             "\t*/\n"
7916             "}",
7917             format("{\n"
7918                    "\t/*\n"
7919                    "\n"
7920                    "\t*/\n"
7921                    "}",
7922                    Tab));
7923   EXPECT_EQ("{\n"
7924             "\t/*\n"
7925             " asdf\n"
7926             "\t*/\n"
7927             "}",
7928             format("{\n"
7929                    "\t/*\n"
7930                    " asdf\n"
7931                    "\t*/\n"
7932                    "}",
7933                    Tab));
7934 
7935   Tab.UseTab = FormatStyle::UT_Never;
7936   EXPECT_EQ("/*\n"
7937             "              a\t\tcomment\n"
7938             "              in multiple lines\n"
7939             "       */",
7940             format("   /*\t \t \n"
7941                    " \t \t a\t\tcomment\t \t\n"
7942                    " \t \t in multiple lines\t\n"
7943                    " \t  */",
7944                    Tab));
7945   EXPECT_EQ("/* some\n"
7946             "   comment */",
7947             format(" \t \t /* some\n"
7948                    " \t \t    comment */",
7949                    Tab));
7950   EXPECT_EQ("int a; /* some\n"
7951             "   comment */",
7952             format(" \t \t int a; /* some\n"
7953                    " \t \t    comment */",
7954                    Tab));
7955 
7956   EXPECT_EQ("int a; /* some\n"
7957             "comment */",
7958             format(" \t \t int\ta; /* some\n"
7959                    " \t \t    comment */",
7960                    Tab));
7961   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7962             "    comment */",
7963             format(" \t \t f(\"\t\t\"); /* some\n"
7964                    " \t \t    comment */",
7965                    Tab));
7966   EXPECT_EQ("{\n"
7967             "  /*\n"
7968             "   * Comment\n"
7969             "   */\n"
7970             "  int i;\n"
7971             "}",
7972             format("{\n"
7973                    "\t/*\n"
7974                    "\t * Comment\n"
7975                    "\t */\n"
7976                    "\t int i;\n"
7977                    "}"));
7978 
7979   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
7980   Tab.TabWidth = 8;
7981   Tab.IndentWidth = 8;
7982   EXPECT_EQ("if (aaaaaaaa && // q\n"
7983             "    bb)         // w\n"
7984             "\t;",
7985             format("if (aaaaaaaa &&// q\n"
7986                    "bb)// w\n"
7987                    ";",
7988                    Tab));
7989   EXPECT_EQ("if (aaa && bbb) // w\n"
7990             "\t;",
7991             format("if(aaa&&bbb)// w\n"
7992                    ";",
7993                    Tab));
7994   verifyFormat("class X {\n"
7995                "\tvoid f() {\n"
7996                "\t\tsomeFunction(parameter1,\n"
7997                "\t\t\t     parameter2);\n"
7998                "\t}\n"
7999                "};",
8000                Tab);
8001   verifyFormat("#define A                        \\\n"
8002                "\tvoid f() {               \\\n"
8003                "\t\tsomeFunction(    \\\n"
8004                "\t\t    parameter1,  \\\n"
8005                "\t\t    parameter2); \\\n"
8006                "\t}",
8007                Tab);
8008   Tab.TabWidth = 4;
8009   Tab.IndentWidth = 8;
8010   verifyFormat("class TabWidth4Indent8 {\n"
8011                "\t\tvoid f() {\n"
8012                "\t\t\t\tsomeFunction(parameter1,\n"
8013                "\t\t\t\t\t\t\t parameter2);\n"
8014                "\t\t}\n"
8015                "};",
8016                Tab);
8017   Tab.TabWidth = 4;
8018   Tab.IndentWidth = 4;
8019   verifyFormat("class TabWidth4Indent4 {\n"
8020                "\tvoid f() {\n"
8021                "\t\tsomeFunction(parameter1,\n"
8022                "\t\t\t\t\t parameter2);\n"
8023                "\t}\n"
8024                "};",
8025                Tab);
8026   Tab.TabWidth = 8;
8027   Tab.IndentWidth = 4;
8028   verifyFormat("class TabWidth8Indent4 {\n"
8029                "    void f() {\n"
8030                "\tsomeFunction(parameter1,\n"
8031                "\t\t     parameter2);\n"
8032                "    }\n"
8033                "};",
8034                Tab);
8035   Tab.TabWidth = 8;
8036   Tab.IndentWidth = 8;
8037   EXPECT_EQ("/*\n"
8038             "\t      a\t\tcomment\n"
8039             "\t      in multiple lines\n"
8040             "       */",
8041             format("   /*\t \t \n"
8042                    " \t \t a\t\tcomment\t \t\n"
8043                    " \t \t in multiple lines\t\n"
8044                    " \t  */",
8045                    Tab));
8046   verifyFormat("{\n"
8047                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8048                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8049                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8050                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8051                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8052                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8053                "};",
8054                Tab);
8055   verifyFormat("enum AA {\n"
8056                "\ta1, // Force multiple lines\n"
8057                "\ta2,\n"
8058                "\ta3\n"
8059                "};",
8060                Tab);
8061   EXPECT_EQ("if (aaaaaaaa && // q\n"
8062             "    bb)         // w\n"
8063             "\t;",
8064             format("if (aaaaaaaa &&// q\n"
8065                    "bb)// w\n"
8066                    ";",
8067                    Tab));
8068   verifyFormat("class X {\n"
8069                "\tvoid f() {\n"
8070                "\t\tsomeFunction(parameter1,\n"
8071                "\t\t\t     parameter2);\n"
8072                "\t}\n"
8073                "};",
8074                Tab);
8075   verifyFormat("{\n"
8076                "\tQ(\n"
8077                "\t    {\n"
8078                "\t\t    int a;\n"
8079                "\t\t    someFunction(aaaaaaaa,\n"
8080                "\t\t\t\t bbbbbbb);\n"
8081                "\t    },\n"
8082                "\t    p);\n"
8083                "}",
8084                Tab);
8085   EXPECT_EQ("{\n"
8086             "\t/* aaaa\n"
8087             "\t   bbbb */\n"
8088             "}",
8089             format("{\n"
8090                    "/* aaaa\n"
8091                    "   bbbb */\n"
8092                    "}",
8093                    Tab));
8094   EXPECT_EQ("{\n"
8095             "\t/*\n"
8096             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8097             "\t  bbbbbbbbbbbbb\n"
8098             "\t*/\n"
8099             "}",
8100             format("{\n"
8101                    "/*\n"
8102                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8103                    "*/\n"
8104                    "}",
8105                    Tab));
8106   EXPECT_EQ("{\n"
8107             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8108             "\t// bbbbbbbbbbbbb\n"
8109             "}",
8110             format("{\n"
8111                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8112                    "}",
8113                    Tab));
8114   EXPECT_EQ("{\n"
8115             "\t/*\n"
8116             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8117             "\t  bbbbbbbbbbbbb\n"
8118             "\t*/\n"
8119             "}",
8120             format("{\n"
8121                    "\t/*\n"
8122                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8123                    "\t*/\n"
8124                    "}",
8125                    Tab));
8126   EXPECT_EQ("{\n"
8127             "\t/*\n"
8128             "\n"
8129             "\t*/\n"
8130             "}",
8131             format("{\n"
8132                    "\t/*\n"
8133                    "\n"
8134                    "\t*/\n"
8135                    "}",
8136                    Tab));
8137   EXPECT_EQ("{\n"
8138             "\t/*\n"
8139             " asdf\n"
8140             "\t*/\n"
8141             "}",
8142             format("{\n"
8143                    "\t/*\n"
8144                    " asdf\n"
8145                    "\t*/\n"
8146                    "}",
8147                    Tab));
8148   EXPECT_EQ("/*\n"
8149             "\t      a\t\tcomment\n"
8150             "\t      in multiple lines\n"
8151             "       */",
8152             format("   /*\t \t \n"
8153                    " \t \t a\t\tcomment\t \t\n"
8154                    " \t \t in multiple lines\t\n"
8155                    " \t  */",
8156                    Tab));
8157   EXPECT_EQ("/* some\n"
8158             "   comment */",
8159             format(" \t \t /* some\n"
8160                    " \t \t    comment */",
8161                    Tab));
8162   EXPECT_EQ("int a; /* some\n"
8163             "   comment */",
8164             format(" \t \t int a; /* some\n"
8165                    " \t \t    comment */",
8166                    Tab));
8167   EXPECT_EQ("int a; /* some\n"
8168             "comment */",
8169             format(" \t \t int\ta; /* some\n"
8170                    " \t \t    comment */",
8171                    Tab));
8172   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8173             "    comment */",
8174             format(" \t \t f(\"\t\t\"); /* some\n"
8175                    " \t \t    comment */",
8176                    Tab));
8177   EXPECT_EQ("{\n"
8178             "  /*\n"
8179             "   * Comment\n"
8180             "   */\n"
8181             "  int i;\n"
8182             "}",
8183             format("{\n"
8184                    "\t/*\n"
8185                    "\t * Comment\n"
8186                    "\t */\n"
8187                    "\t int i;\n"
8188                    "}"));
8189   Tab.AlignConsecutiveAssignments = true;
8190   Tab.AlignConsecutiveDeclarations = true;
8191   Tab.TabWidth = 4;
8192   Tab.IndentWidth = 4;
8193   verifyFormat("class Assign {\n"
8194                "\tvoid f() {\n"
8195                "\t\tint         x      = 123;\n"
8196                "\t\tint         random = 4;\n"
8197                "\t\tstd::string alphabet =\n"
8198                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8199                "\t}\n"
8200                "};",
8201                Tab);
8202 }
8203 
8204 TEST_F(FormatTest, CalculatesOriginalColumn) {
8205   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8206             "q\"; /* some\n"
8207             "       comment */",
8208             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8209                    "q\"; /* some\n"
8210                    "       comment */",
8211                    getLLVMStyle()));
8212   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8213             "/* some\n"
8214             "   comment */",
8215             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8216                    " /* some\n"
8217                    "    comment */",
8218                    getLLVMStyle()));
8219   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8220             "qqq\n"
8221             "/* some\n"
8222             "   comment */",
8223             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8224                    "qqq\n"
8225                    " /* some\n"
8226                    "    comment */",
8227                    getLLVMStyle()));
8228   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8229             "wwww; /* some\n"
8230             "         comment */",
8231             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8232                    "wwww; /* some\n"
8233                    "         comment */",
8234                    getLLVMStyle()));
8235 }
8236 
8237 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8238   FormatStyle NoSpace = getLLVMStyle();
8239   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8240 
8241   verifyFormat("while(true)\n"
8242                "  continue;",
8243                NoSpace);
8244   verifyFormat("for(;;)\n"
8245                "  continue;",
8246                NoSpace);
8247   verifyFormat("if(true)\n"
8248                "  f();\n"
8249                "else if(true)\n"
8250                "  f();",
8251                NoSpace);
8252   verifyFormat("do {\n"
8253                "  do_something();\n"
8254                "} while(something());",
8255                NoSpace);
8256   verifyFormat("switch(x) {\n"
8257                "default:\n"
8258                "  break;\n"
8259                "}",
8260                NoSpace);
8261   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8262   verifyFormat("size_t x = sizeof(x);", NoSpace);
8263   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8264   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8265   verifyFormat("alignas(128) char a[128];", NoSpace);
8266   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8267   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8268   verifyFormat("int f() throw(Deprecated);", NoSpace);
8269   verifyFormat("typedef void (*cb)(int);", NoSpace);
8270   verifyFormat("T A::operator()();", NoSpace);
8271   verifyFormat("X A::operator++(T);", NoSpace);
8272 
8273   FormatStyle Space = getLLVMStyle();
8274   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8275 
8276   verifyFormat("int f ();", Space);
8277   verifyFormat("void f (int a, T b) {\n"
8278                "  while (true)\n"
8279                "    continue;\n"
8280                "}",
8281                Space);
8282   verifyFormat("if (true)\n"
8283                "  f ();\n"
8284                "else if (true)\n"
8285                "  f ();",
8286                Space);
8287   verifyFormat("do {\n"
8288                "  do_something ();\n"
8289                "} while (something ());",
8290                Space);
8291   verifyFormat("switch (x) {\n"
8292                "default:\n"
8293                "  break;\n"
8294                "}",
8295                Space);
8296   verifyFormat("A::A () : a (1) {}", Space);
8297   verifyFormat("void f () __attribute__ ((asdf));", Space);
8298   verifyFormat("*(&a + 1);\n"
8299                "&((&a)[1]);\n"
8300                "a[(b + c) * d];\n"
8301                "(((a + 1) * 2) + 3) * 4;",
8302                Space);
8303   verifyFormat("#define A(x) x", Space);
8304   verifyFormat("#define A (x) x", Space);
8305   verifyFormat("#if defined(x)\n"
8306                "#endif",
8307                Space);
8308   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8309   verifyFormat("size_t x = sizeof (x);", Space);
8310   verifyFormat("auto f (int x) -> decltype (x);", Space);
8311   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8312   verifyFormat("alignas (128) char a[128];", Space);
8313   verifyFormat("size_t x = alignof (MyType);", Space);
8314   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8315   verifyFormat("int f () throw (Deprecated);", Space);
8316   verifyFormat("typedef void (*cb) (int);", Space);
8317   verifyFormat("T A::operator() ();", Space);
8318   verifyFormat("X A::operator++ (T);", Space);
8319 }
8320 
8321 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8322   FormatStyle Spaces = getLLVMStyle();
8323 
8324   Spaces.SpacesInParentheses = true;
8325   verifyFormat("call( x, y, z );", Spaces);
8326   verifyFormat("call();", Spaces);
8327   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8328   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8329                Spaces);
8330   verifyFormat("while ( (bool)1 )\n"
8331                "  continue;",
8332                Spaces);
8333   verifyFormat("for ( ;; )\n"
8334                "  continue;",
8335                Spaces);
8336   verifyFormat("if ( true )\n"
8337                "  f();\n"
8338                "else if ( true )\n"
8339                "  f();",
8340                Spaces);
8341   verifyFormat("do {\n"
8342                "  do_something( (int)i );\n"
8343                "} while ( something() );",
8344                Spaces);
8345   verifyFormat("switch ( x ) {\n"
8346                "default:\n"
8347                "  break;\n"
8348                "}",
8349                Spaces);
8350 
8351   Spaces.SpacesInParentheses = false;
8352   Spaces.SpacesInCStyleCastParentheses = true;
8353   verifyFormat("Type *A = ( Type * )P;", Spaces);
8354   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8355   verifyFormat("x = ( int32 )y;", Spaces);
8356   verifyFormat("int a = ( int )(2.0f);", Spaces);
8357   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8358   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8359   verifyFormat("#define x (( int )-1)", Spaces);
8360 
8361   // Run the first set of tests again with:
8362   Spaces.SpacesInParentheses = false;
8363   Spaces.SpaceInEmptyParentheses = true;
8364   Spaces.SpacesInCStyleCastParentheses = true;
8365   verifyFormat("call(x, y, z);", Spaces);
8366   verifyFormat("call( );", Spaces);
8367   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8368   verifyFormat("while (( bool )1)\n"
8369                "  continue;",
8370                Spaces);
8371   verifyFormat("for (;;)\n"
8372                "  continue;",
8373                Spaces);
8374   verifyFormat("if (true)\n"
8375                "  f( );\n"
8376                "else if (true)\n"
8377                "  f( );",
8378                Spaces);
8379   verifyFormat("do {\n"
8380                "  do_something(( int )i);\n"
8381                "} while (something( ));",
8382                Spaces);
8383   verifyFormat("switch (x) {\n"
8384                "default:\n"
8385                "  break;\n"
8386                "}",
8387                Spaces);
8388 
8389   // Run the first set of tests again with:
8390   Spaces.SpaceAfterCStyleCast = true;
8391   verifyFormat("call(x, y, z);", Spaces);
8392   verifyFormat("call( );", Spaces);
8393   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8394   verifyFormat("while (( bool ) 1)\n"
8395                "  continue;",
8396                Spaces);
8397   verifyFormat("for (;;)\n"
8398                "  continue;",
8399                Spaces);
8400   verifyFormat("if (true)\n"
8401                "  f( );\n"
8402                "else if (true)\n"
8403                "  f( );",
8404                Spaces);
8405   verifyFormat("do {\n"
8406                "  do_something(( int ) i);\n"
8407                "} while (something( ));",
8408                Spaces);
8409   verifyFormat("switch (x) {\n"
8410                "default:\n"
8411                "  break;\n"
8412                "}",
8413                Spaces);
8414 
8415   // Run subset of tests again with:
8416   Spaces.SpacesInCStyleCastParentheses = false;
8417   Spaces.SpaceAfterCStyleCast = true;
8418   verifyFormat("while ((bool) 1)\n"
8419                "  continue;",
8420                Spaces);
8421   verifyFormat("do {\n"
8422                "  do_something((int) i);\n"
8423                "} while (something( ));",
8424                Spaces);
8425 }
8426 
8427 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8428   verifyFormat("int a[5];");
8429   verifyFormat("a[3] += 42;");
8430 
8431   FormatStyle Spaces = getLLVMStyle();
8432   Spaces.SpacesInSquareBrackets = true;
8433   // Lambdas unchanged.
8434   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8435   verifyFormat("return [i, args...] {};", Spaces);
8436 
8437   // Not lambdas.
8438   verifyFormat("int a[ 5 ];", Spaces);
8439   verifyFormat("a[ 3 ] += 42;", Spaces);
8440   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8441   verifyFormat("double &operator[](int i) { return 0; }\n"
8442                "int i;",
8443                Spaces);
8444   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8445   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8446   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8447 }
8448 
8449 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8450   verifyFormat("int a = 5;");
8451   verifyFormat("a += 42;");
8452   verifyFormat("a or_eq 8;");
8453 
8454   FormatStyle Spaces = getLLVMStyle();
8455   Spaces.SpaceBeforeAssignmentOperators = false;
8456   verifyFormat("int a= 5;", Spaces);
8457   verifyFormat("a+= 42;", Spaces);
8458   verifyFormat("a or_eq 8;", Spaces);
8459 }
8460 
8461 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8462   FormatStyle Alignment = getLLVMStyle();
8463   Alignment.AlignConsecutiveAssignments = false;
8464   verifyFormat("int a = 5;\n"
8465                "int oneTwoThree = 123;",
8466                Alignment);
8467   verifyFormat("int a = 5;\n"
8468                "int oneTwoThree = 123;",
8469                Alignment);
8470 
8471   Alignment.AlignConsecutiveAssignments = true;
8472   verifyFormat("int a           = 5;\n"
8473                "int oneTwoThree = 123;",
8474                Alignment);
8475   verifyFormat("int a           = method();\n"
8476                "int oneTwoThree = 133;",
8477                Alignment);
8478   verifyFormat("a &= 5;\n"
8479                "bcd *= 5;\n"
8480                "ghtyf += 5;\n"
8481                "dvfvdb -= 5;\n"
8482                "a /= 5;\n"
8483                "vdsvsv %= 5;\n"
8484                "sfdbddfbdfbb ^= 5;\n"
8485                "dvsdsv |= 5;\n"
8486                "int dsvvdvsdvvv = 123;",
8487                Alignment);
8488   verifyFormat("int i = 1, j = 10;\n"
8489                "something = 2000;",
8490                Alignment);
8491   verifyFormat("something = 2000;\n"
8492                "int i = 1, j = 10;\n",
8493                Alignment);
8494   verifyFormat("something = 2000;\n"
8495                "another   = 911;\n"
8496                "int i = 1, j = 10;\n"
8497                "oneMore = 1;\n"
8498                "i       = 2;",
8499                Alignment);
8500   verifyFormat("int a   = 5;\n"
8501                "int one = 1;\n"
8502                "method();\n"
8503                "int oneTwoThree = 123;\n"
8504                "int oneTwo      = 12;",
8505                Alignment);
8506   verifyFormat("int oneTwoThree = 123;\n"
8507                "int oneTwo      = 12;\n"
8508                "method();\n",
8509                Alignment);
8510   verifyFormat("int oneTwoThree = 123; // comment\n"
8511                "int oneTwo      = 12;  // comment",
8512                Alignment);
8513   EXPECT_EQ("int a = 5;\n"
8514             "\n"
8515             "int oneTwoThree = 123;",
8516             format("int a       = 5;\n"
8517                    "\n"
8518                    "int oneTwoThree= 123;",
8519                    Alignment));
8520   EXPECT_EQ("int a   = 5;\n"
8521             "int one = 1;\n"
8522             "\n"
8523             "int oneTwoThree = 123;",
8524             format("int a = 5;\n"
8525                    "int one = 1;\n"
8526                    "\n"
8527                    "int oneTwoThree = 123;",
8528                    Alignment));
8529   EXPECT_EQ("int a   = 5;\n"
8530             "int one = 1;\n"
8531             "\n"
8532             "int oneTwoThree = 123;\n"
8533             "int oneTwo      = 12;",
8534             format("int a = 5;\n"
8535                    "int one = 1;\n"
8536                    "\n"
8537                    "int oneTwoThree = 123;\n"
8538                    "int oneTwo = 12;",
8539                    Alignment));
8540   Alignment.AlignEscapedNewlinesLeft = true;
8541   verifyFormat("#define A               \\\n"
8542                "  int aaaa       = 12;  \\\n"
8543                "  int b          = 23;  \\\n"
8544                "  int ccc        = 234; \\\n"
8545                "  int dddddddddd = 2345;",
8546                Alignment);
8547   Alignment.AlignEscapedNewlinesLeft = false;
8548   verifyFormat("#define A                                                      "
8549                "                \\\n"
8550                "  int aaaa       = 12;                                         "
8551                "                \\\n"
8552                "  int b          = 23;                                         "
8553                "                \\\n"
8554                "  int ccc        = 234;                                        "
8555                "                \\\n"
8556                "  int dddddddddd = 2345;",
8557                Alignment);
8558   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8559                "k = 4, int l = 5,\n"
8560                "                  int m = 6) {\n"
8561                "  int j      = 10;\n"
8562                "  otherThing = 1;\n"
8563                "}",
8564                Alignment);
8565   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8566                "  int i   = 1;\n"
8567                "  int j   = 2;\n"
8568                "  int big = 10000;\n"
8569                "}",
8570                Alignment);
8571   verifyFormat("class C {\n"
8572                "public:\n"
8573                "  int i            = 1;\n"
8574                "  virtual void f() = 0;\n"
8575                "};",
8576                Alignment);
8577   verifyFormat("int i = 1;\n"
8578                "if (SomeType t = getSomething()) {\n"
8579                "}\n"
8580                "int j   = 2;\n"
8581                "int big = 10000;",
8582                Alignment);
8583   verifyFormat("int j = 7;\n"
8584                "for (int k = 0; k < N; ++k) {\n"
8585                "}\n"
8586                "int j   = 2;\n"
8587                "int big = 10000;\n"
8588                "}",
8589                Alignment);
8590   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8591   verifyFormat("int i = 1;\n"
8592                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8593                "    = someLooooooooooooooooongFunction();\n"
8594                "int j = 2;",
8595                Alignment);
8596   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8597   verifyFormat("int i = 1;\n"
8598                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8599                "    someLooooooooooooooooongFunction();\n"
8600                "int j = 2;",
8601                Alignment);
8602 
8603   verifyFormat("auto lambda = []() {\n"
8604                "  auto i = 0;\n"
8605                "  return 0;\n"
8606                "};\n"
8607                "int i  = 0;\n"
8608                "auto v = type{\n"
8609                "    i = 1,   //\n"
8610                "    (i = 2), //\n"
8611                "    i = 3    //\n"
8612                "};",
8613                Alignment);
8614 
8615   // FIXME: Should align all three assignments
8616   verifyFormat(
8617       "int i      = 1;\n"
8618       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8619       "                          loooooooooooooooooooooongParameterB);\n"
8620       "int j = 2;",
8621       Alignment);
8622 
8623   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8624                "          typename B   = very_long_type_name_1,\n"
8625                "          typename T_2 = very_long_type_name_2>\n"
8626                "auto foo() {}\n",
8627                Alignment);
8628   verifyFormat("int a, b = 1;\n"
8629                "int c  = 2;\n"
8630                "int dd = 3;\n",
8631                Alignment);
8632   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8633                "float b[1][] = {{3.f}};\n",
8634                Alignment);
8635 }
8636 
8637 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8638   FormatStyle Alignment = getLLVMStyle();
8639   Alignment.AlignConsecutiveDeclarations = false;
8640   verifyFormat("float const a = 5;\n"
8641                "int oneTwoThree = 123;",
8642                Alignment);
8643   verifyFormat("int a = 5;\n"
8644                "float const oneTwoThree = 123;",
8645                Alignment);
8646 
8647   Alignment.AlignConsecutiveDeclarations = true;
8648   verifyFormat("float const a = 5;\n"
8649                "int         oneTwoThree = 123;",
8650                Alignment);
8651   verifyFormat("int         a = method();\n"
8652                "float const oneTwoThree = 133;",
8653                Alignment);
8654   verifyFormat("int i = 1, j = 10;\n"
8655                "something = 2000;",
8656                Alignment);
8657   verifyFormat("something = 2000;\n"
8658                "int i = 1, j = 10;\n",
8659                Alignment);
8660   verifyFormat("float      something = 2000;\n"
8661                "double     another = 911;\n"
8662                "int        i = 1, j = 10;\n"
8663                "const int *oneMore = 1;\n"
8664                "unsigned   i = 2;",
8665                Alignment);
8666   verifyFormat("float a = 5;\n"
8667                "int   one = 1;\n"
8668                "method();\n"
8669                "const double       oneTwoThree = 123;\n"
8670                "const unsigned int oneTwo = 12;",
8671                Alignment);
8672   verifyFormat("int      oneTwoThree{0}; // comment\n"
8673                "unsigned oneTwo;         // comment",
8674                Alignment);
8675   EXPECT_EQ("float const a = 5;\n"
8676             "\n"
8677             "int oneTwoThree = 123;",
8678             format("float const   a = 5;\n"
8679                    "\n"
8680                    "int           oneTwoThree= 123;",
8681                    Alignment));
8682   EXPECT_EQ("float a = 5;\n"
8683             "int   one = 1;\n"
8684             "\n"
8685             "unsigned oneTwoThree = 123;",
8686             format("float    a = 5;\n"
8687                    "int      one = 1;\n"
8688                    "\n"
8689                    "unsigned oneTwoThree = 123;",
8690                    Alignment));
8691   EXPECT_EQ("float a = 5;\n"
8692             "int   one = 1;\n"
8693             "\n"
8694             "unsigned oneTwoThree = 123;\n"
8695             "int      oneTwo = 12;",
8696             format("float    a = 5;\n"
8697                    "int one = 1;\n"
8698                    "\n"
8699                    "unsigned oneTwoThree = 123;\n"
8700                    "int oneTwo = 12;",
8701                    Alignment));
8702   Alignment.AlignConsecutiveAssignments = true;
8703   verifyFormat("float      something = 2000;\n"
8704                "double     another   = 911;\n"
8705                "int        i = 1, j = 10;\n"
8706                "const int *oneMore = 1;\n"
8707                "unsigned   i       = 2;",
8708                Alignment);
8709   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8710                "unsigned oneTwo      = 0;   // comment",
8711                Alignment);
8712   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8713             "  int const i   = 1;\n"
8714             "  int *     j   = 2;\n"
8715             "  int       big = 10000;\n"
8716             "\n"
8717             "  unsigned oneTwoThree = 123;\n"
8718             "  int      oneTwo      = 12;\n"
8719             "  method();\n"
8720             "  float k  = 2;\n"
8721             "  int   ll = 10000;\n"
8722             "}",
8723             format("void SomeFunction(int parameter= 0) {\n"
8724                    " int const  i= 1;\n"
8725                    "  int *j=2;\n"
8726                    " int big  =  10000;\n"
8727                    "\n"
8728                    "unsigned oneTwoThree  =123;\n"
8729                    "int oneTwo = 12;\n"
8730                    "  method();\n"
8731                    "float k= 2;\n"
8732                    "int ll=10000;\n"
8733                    "}",
8734                    Alignment));
8735   Alignment.AlignConsecutiveAssignments = false;
8736   Alignment.AlignEscapedNewlinesLeft = true;
8737   verifyFormat("#define A              \\\n"
8738                "  int       aaaa = 12; \\\n"
8739                "  float     b = 23;    \\\n"
8740                "  const int ccc = 234; \\\n"
8741                "  unsigned  dddddddddd = 2345;",
8742                Alignment);
8743   Alignment.AlignEscapedNewlinesLeft = false;
8744   Alignment.ColumnLimit = 30;
8745   verifyFormat("#define A                    \\\n"
8746                "  int       aaaa = 12;       \\\n"
8747                "  float     b = 23;          \\\n"
8748                "  const int ccc = 234;       \\\n"
8749                "  int       dddddddddd = 2345;",
8750                Alignment);
8751   Alignment.ColumnLimit = 80;
8752   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8753                "k = 4, int l = 5,\n"
8754                "                  int m = 6) {\n"
8755                "  const int j = 10;\n"
8756                "  otherThing = 1;\n"
8757                "}",
8758                Alignment);
8759   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8760                "  int const i = 1;\n"
8761                "  int *     j = 2;\n"
8762                "  int       big = 10000;\n"
8763                "}",
8764                Alignment);
8765   verifyFormat("class C {\n"
8766                "public:\n"
8767                "  int          i = 1;\n"
8768                "  virtual void f() = 0;\n"
8769                "};",
8770                Alignment);
8771   verifyFormat("float i = 1;\n"
8772                "if (SomeType t = getSomething()) {\n"
8773                "}\n"
8774                "const unsigned j = 2;\n"
8775                "int            big = 10000;",
8776                Alignment);
8777   verifyFormat("float j = 7;\n"
8778                "for (int k = 0; k < N; ++k) {\n"
8779                "}\n"
8780                "unsigned j = 2;\n"
8781                "int      big = 10000;\n"
8782                "}",
8783                Alignment);
8784   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8785   verifyFormat("float              i = 1;\n"
8786                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8787                "    = someLooooooooooooooooongFunction();\n"
8788                "int j = 2;",
8789                Alignment);
8790   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8791   verifyFormat("int                i = 1;\n"
8792                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8793                "    someLooooooooooooooooongFunction();\n"
8794                "int j = 2;",
8795                Alignment);
8796 
8797   Alignment.AlignConsecutiveAssignments = true;
8798   verifyFormat("auto lambda = []() {\n"
8799                "  auto  ii = 0;\n"
8800                "  float j  = 0;\n"
8801                "  return 0;\n"
8802                "};\n"
8803                "int   i  = 0;\n"
8804                "float i2 = 0;\n"
8805                "auto  v  = type{\n"
8806                "    i = 1,   //\n"
8807                "    (i = 2), //\n"
8808                "    i = 3    //\n"
8809                "};",
8810                Alignment);
8811   Alignment.AlignConsecutiveAssignments = false;
8812 
8813   // FIXME: Should align all three declarations
8814   verifyFormat(
8815       "int      i = 1;\n"
8816       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8817       "                          loooooooooooooooooooooongParameterB);\n"
8818       "int j = 2;",
8819       Alignment);
8820 
8821   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8822   // We expect declarations and assignments to align, as long as it doesn't
8823   // exceed the column limit, starting a new alignemnt sequence whenever it
8824   // happens.
8825   Alignment.AlignConsecutiveAssignments = true;
8826   Alignment.ColumnLimit = 30;
8827   verifyFormat("float    ii              = 1;\n"
8828                "unsigned j               = 2;\n"
8829                "int someVerylongVariable = 1;\n"
8830                "AnotherLongType  ll = 123456;\n"
8831                "VeryVeryLongType k  = 2;\n"
8832                "int              myvar = 1;",
8833                Alignment);
8834   Alignment.ColumnLimit = 80;
8835   Alignment.AlignConsecutiveAssignments = false;
8836 
8837   verifyFormat(
8838       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8839       "          typename LongType, typename B>\n"
8840       "auto foo() {}\n",
8841       Alignment);
8842   verifyFormat("float a, b = 1;\n"
8843                "int   c = 2;\n"
8844                "int   dd = 3;\n",
8845                Alignment);
8846   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8847                "float b[1][] = {{3.f}};\n",
8848                Alignment);
8849   Alignment.AlignConsecutiveAssignments = true;
8850   verifyFormat("float a, b = 1;\n"
8851                "int   c  = 2;\n"
8852                "int   dd = 3;\n",
8853                Alignment);
8854   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8855                "float b[1][] = {{3.f}};\n",
8856                Alignment);
8857   Alignment.AlignConsecutiveAssignments = false;
8858 
8859   Alignment.ColumnLimit = 30;
8860   Alignment.BinPackParameters = false;
8861   verifyFormat("void foo(float     a,\n"
8862                "         float     b,\n"
8863                "         int       c,\n"
8864                "         uint32_t *d) {\n"
8865                "  int *  e = 0;\n"
8866                "  float  f = 0;\n"
8867                "  double g = 0;\n"
8868                "}\n"
8869                "void bar(ino_t     a,\n"
8870                "         int       b,\n"
8871                "         uint32_t *c,\n"
8872                "         bool      d) {}\n",
8873                Alignment);
8874   Alignment.BinPackParameters = true;
8875   Alignment.ColumnLimit = 80;
8876 }
8877 
8878 TEST_F(FormatTest, LinuxBraceBreaking) {
8879   FormatStyle LinuxBraceStyle = getLLVMStyle();
8880   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8881   verifyFormat("namespace a\n"
8882                "{\n"
8883                "class A\n"
8884                "{\n"
8885                "  void f()\n"
8886                "  {\n"
8887                "    if (true) {\n"
8888                "      a();\n"
8889                "      b();\n"
8890                "    } else {\n"
8891                "      a();\n"
8892                "    }\n"
8893                "  }\n"
8894                "  void g() { return; }\n"
8895                "};\n"
8896                "struct B {\n"
8897                "  int x;\n"
8898                "};\n"
8899                "}\n",
8900                LinuxBraceStyle);
8901   verifyFormat("enum X {\n"
8902                "  Y = 0,\n"
8903                "}\n",
8904                LinuxBraceStyle);
8905   verifyFormat("struct S {\n"
8906                "  int Type;\n"
8907                "  union {\n"
8908                "    int x;\n"
8909                "    double y;\n"
8910                "  } Value;\n"
8911                "  class C\n"
8912                "  {\n"
8913                "    MyFavoriteType Value;\n"
8914                "  } Class;\n"
8915                "}\n",
8916                LinuxBraceStyle);
8917 }
8918 
8919 TEST_F(FormatTest, MozillaBraceBreaking) {
8920   FormatStyle MozillaBraceStyle = getLLVMStyle();
8921   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8922   verifyFormat("namespace a {\n"
8923                "class A\n"
8924                "{\n"
8925                "  void f()\n"
8926                "  {\n"
8927                "    if (true) {\n"
8928                "      a();\n"
8929                "      b();\n"
8930                "    }\n"
8931                "  }\n"
8932                "  void g() { return; }\n"
8933                "};\n"
8934                "enum E\n"
8935                "{\n"
8936                "  A,\n"
8937                "  // foo\n"
8938                "  B,\n"
8939                "  C\n"
8940                "};\n"
8941                "struct B\n"
8942                "{\n"
8943                "  int x;\n"
8944                "};\n"
8945                "}\n",
8946                MozillaBraceStyle);
8947   verifyFormat("struct S\n"
8948                "{\n"
8949                "  int Type;\n"
8950                "  union\n"
8951                "  {\n"
8952                "    int x;\n"
8953                "    double y;\n"
8954                "  } Value;\n"
8955                "  class C\n"
8956                "  {\n"
8957                "    MyFavoriteType Value;\n"
8958                "  } Class;\n"
8959                "}\n",
8960                MozillaBraceStyle);
8961 }
8962 
8963 TEST_F(FormatTest, StroustrupBraceBreaking) {
8964   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8965   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8966   verifyFormat("namespace a {\n"
8967                "class A {\n"
8968                "  void f()\n"
8969                "  {\n"
8970                "    if (true) {\n"
8971                "      a();\n"
8972                "      b();\n"
8973                "    }\n"
8974                "  }\n"
8975                "  void g() { return; }\n"
8976                "};\n"
8977                "struct B {\n"
8978                "  int x;\n"
8979                "};\n"
8980                "}\n",
8981                StroustrupBraceStyle);
8982 
8983   verifyFormat("void foo()\n"
8984                "{\n"
8985                "  if (a) {\n"
8986                "    a();\n"
8987                "  }\n"
8988                "  else {\n"
8989                "    b();\n"
8990                "  }\n"
8991                "}\n",
8992                StroustrupBraceStyle);
8993 
8994   verifyFormat("#ifdef _DEBUG\n"
8995                "int foo(int i = 0)\n"
8996                "#else\n"
8997                "int foo(int i = 5)\n"
8998                "#endif\n"
8999                "{\n"
9000                "  return i;\n"
9001                "}",
9002                StroustrupBraceStyle);
9003 
9004   verifyFormat("void foo() {}\n"
9005                "void bar()\n"
9006                "#ifdef _DEBUG\n"
9007                "{\n"
9008                "  foo();\n"
9009                "}\n"
9010                "#else\n"
9011                "{\n"
9012                "}\n"
9013                "#endif",
9014                StroustrupBraceStyle);
9015 
9016   verifyFormat("void foobar() { int i = 5; }\n"
9017                "#ifdef _DEBUG\n"
9018                "void bar() {}\n"
9019                "#else\n"
9020                "void bar() { foobar(); }\n"
9021                "#endif",
9022                StroustrupBraceStyle);
9023 }
9024 
9025 TEST_F(FormatTest, AllmanBraceBreaking) {
9026   FormatStyle AllmanBraceStyle = getLLVMStyle();
9027   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9028   verifyFormat("namespace a\n"
9029                "{\n"
9030                "class A\n"
9031                "{\n"
9032                "  void f()\n"
9033                "  {\n"
9034                "    if (true)\n"
9035                "    {\n"
9036                "      a();\n"
9037                "      b();\n"
9038                "    }\n"
9039                "  }\n"
9040                "  void g() { return; }\n"
9041                "};\n"
9042                "struct B\n"
9043                "{\n"
9044                "  int x;\n"
9045                "};\n"
9046                "}",
9047                AllmanBraceStyle);
9048 
9049   verifyFormat("void f()\n"
9050                "{\n"
9051                "  if (true)\n"
9052                "  {\n"
9053                "    a();\n"
9054                "  }\n"
9055                "  else if (false)\n"
9056                "  {\n"
9057                "    b();\n"
9058                "  }\n"
9059                "  else\n"
9060                "  {\n"
9061                "    c();\n"
9062                "  }\n"
9063                "}\n",
9064                AllmanBraceStyle);
9065 
9066   verifyFormat("void f()\n"
9067                "{\n"
9068                "  for (int i = 0; i < 10; ++i)\n"
9069                "  {\n"
9070                "    a();\n"
9071                "  }\n"
9072                "  while (false)\n"
9073                "  {\n"
9074                "    b();\n"
9075                "  }\n"
9076                "  do\n"
9077                "  {\n"
9078                "    c();\n"
9079                "  } while (false)\n"
9080                "}\n",
9081                AllmanBraceStyle);
9082 
9083   verifyFormat("void f(int a)\n"
9084                "{\n"
9085                "  switch (a)\n"
9086                "  {\n"
9087                "  case 0:\n"
9088                "    break;\n"
9089                "  case 1:\n"
9090                "  {\n"
9091                "    break;\n"
9092                "  }\n"
9093                "  case 2:\n"
9094                "  {\n"
9095                "  }\n"
9096                "  break;\n"
9097                "  default:\n"
9098                "    break;\n"
9099                "  }\n"
9100                "}\n",
9101                AllmanBraceStyle);
9102 
9103   verifyFormat("enum X\n"
9104                "{\n"
9105                "  Y = 0,\n"
9106                "}\n",
9107                AllmanBraceStyle);
9108   verifyFormat("enum X\n"
9109                "{\n"
9110                "  Y = 0\n"
9111                "}\n",
9112                AllmanBraceStyle);
9113 
9114   verifyFormat("@interface BSApplicationController ()\n"
9115                "{\n"
9116                "@private\n"
9117                "  id _extraIvar;\n"
9118                "}\n"
9119                "@end\n",
9120                AllmanBraceStyle);
9121 
9122   verifyFormat("#ifdef _DEBUG\n"
9123                "int foo(int i = 0)\n"
9124                "#else\n"
9125                "int foo(int i = 5)\n"
9126                "#endif\n"
9127                "{\n"
9128                "  return i;\n"
9129                "}",
9130                AllmanBraceStyle);
9131 
9132   verifyFormat("void foo() {}\n"
9133                "void bar()\n"
9134                "#ifdef _DEBUG\n"
9135                "{\n"
9136                "  foo();\n"
9137                "}\n"
9138                "#else\n"
9139                "{\n"
9140                "}\n"
9141                "#endif",
9142                AllmanBraceStyle);
9143 
9144   verifyFormat("void foobar() { int i = 5; }\n"
9145                "#ifdef _DEBUG\n"
9146                "void bar() {}\n"
9147                "#else\n"
9148                "void bar() { foobar(); }\n"
9149                "#endif",
9150                AllmanBraceStyle);
9151 
9152   // This shouldn't affect ObjC blocks..
9153   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9154                "  // ...\n"
9155                "  int i;\n"
9156                "}];",
9157                AllmanBraceStyle);
9158   verifyFormat("void (^block)(void) = ^{\n"
9159                "  // ...\n"
9160                "  int i;\n"
9161                "};",
9162                AllmanBraceStyle);
9163   // .. or dict literals.
9164   verifyFormat("void f()\n"
9165                "{\n"
9166                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9167                "}",
9168                AllmanBraceStyle);
9169   verifyFormat("int f()\n"
9170                "{ // comment\n"
9171                "  return 42;\n"
9172                "}",
9173                AllmanBraceStyle);
9174 
9175   AllmanBraceStyle.ColumnLimit = 19;
9176   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9177   AllmanBraceStyle.ColumnLimit = 18;
9178   verifyFormat("void f()\n"
9179                "{\n"
9180                "  int i;\n"
9181                "}",
9182                AllmanBraceStyle);
9183   AllmanBraceStyle.ColumnLimit = 80;
9184 
9185   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9186   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9187   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9188   verifyFormat("void f(bool b)\n"
9189                "{\n"
9190                "  if (b)\n"
9191                "  {\n"
9192                "    return;\n"
9193                "  }\n"
9194                "}\n",
9195                BreakBeforeBraceShortIfs);
9196   verifyFormat("void f(bool b)\n"
9197                "{\n"
9198                "  if (b) return;\n"
9199                "}\n",
9200                BreakBeforeBraceShortIfs);
9201   verifyFormat("void f(bool b)\n"
9202                "{\n"
9203                "  while (b)\n"
9204                "  {\n"
9205                "    return;\n"
9206                "  }\n"
9207                "}\n",
9208                BreakBeforeBraceShortIfs);
9209 }
9210 
9211 TEST_F(FormatTest, GNUBraceBreaking) {
9212   FormatStyle GNUBraceStyle = getLLVMStyle();
9213   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9214   verifyFormat("namespace a\n"
9215                "{\n"
9216                "class A\n"
9217                "{\n"
9218                "  void f()\n"
9219                "  {\n"
9220                "    int a;\n"
9221                "    {\n"
9222                "      int b;\n"
9223                "    }\n"
9224                "    if (true)\n"
9225                "      {\n"
9226                "        a();\n"
9227                "        b();\n"
9228                "      }\n"
9229                "  }\n"
9230                "  void g() { return; }\n"
9231                "}\n"
9232                "}",
9233                GNUBraceStyle);
9234 
9235   verifyFormat("void f()\n"
9236                "{\n"
9237                "  if (true)\n"
9238                "    {\n"
9239                "      a();\n"
9240                "    }\n"
9241                "  else if (false)\n"
9242                "    {\n"
9243                "      b();\n"
9244                "    }\n"
9245                "  else\n"
9246                "    {\n"
9247                "      c();\n"
9248                "    }\n"
9249                "}\n",
9250                GNUBraceStyle);
9251 
9252   verifyFormat("void f()\n"
9253                "{\n"
9254                "  for (int i = 0; i < 10; ++i)\n"
9255                "    {\n"
9256                "      a();\n"
9257                "    }\n"
9258                "  while (false)\n"
9259                "    {\n"
9260                "      b();\n"
9261                "    }\n"
9262                "  do\n"
9263                "    {\n"
9264                "      c();\n"
9265                "    }\n"
9266                "  while (false);\n"
9267                "}\n",
9268                GNUBraceStyle);
9269 
9270   verifyFormat("void f(int a)\n"
9271                "{\n"
9272                "  switch (a)\n"
9273                "    {\n"
9274                "    case 0:\n"
9275                "      break;\n"
9276                "    case 1:\n"
9277                "      {\n"
9278                "        break;\n"
9279                "      }\n"
9280                "    case 2:\n"
9281                "      {\n"
9282                "      }\n"
9283                "      break;\n"
9284                "    default:\n"
9285                "      break;\n"
9286                "    }\n"
9287                "}\n",
9288                GNUBraceStyle);
9289 
9290   verifyFormat("enum X\n"
9291                "{\n"
9292                "  Y = 0,\n"
9293                "}\n",
9294                GNUBraceStyle);
9295 
9296   verifyFormat("@interface BSApplicationController ()\n"
9297                "{\n"
9298                "@private\n"
9299                "  id _extraIvar;\n"
9300                "}\n"
9301                "@end\n",
9302                GNUBraceStyle);
9303 
9304   verifyFormat("#ifdef _DEBUG\n"
9305                "int foo(int i = 0)\n"
9306                "#else\n"
9307                "int foo(int i = 5)\n"
9308                "#endif\n"
9309                "{\n"
9310                "  return i;\n"
9311                "}",
9312                GNUBraceStyle);
9313 
9314   verifyFormat("void foo() {}\n"
9315                "void bar()\n"
9316                "#ifdef _DEBUG\n"
9317                "{\n"
9318                "  foo();\n"
9319                "}\n"
9320                "#else\n"
9321                "{\n"
9322                "}\n"
9323                "#endif",
9324                GNUBraceStyle);
9325 
9326   verifyFormat("void foobar() { int i = 5; }\n"
9327                "#ifdef _DEBUG\n"
9328                "void bar() {}\n"
9329                "#else\n"
9330                "void bar() { foobar(); }\n"
9331                "#endif",
9332                GNUBraceStyle);
9333 }
9334 
9335 TEST_F(FormatTest, WebKitBraceBreaking) {
9336   FormatStyle WebKitBraceStyle = getLLVMStyle();
9337   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9338   verifyFormat("namespace a {\n"
9339                "class A {\n"
9340                "  void f()\n"
9341                "  {\n"
9342                "    if (true) {\n"
9343                "      a();\n"
9344                "      b();\n"
9345                "    }\n"
9346                "  }\n"
9347                "  void g() { return; }\n"
9348                "};\n"
9349                "enum E {\n"
9350                "  A,\n"
9351                "  // foo\n"
9352                "  B,\n"
9353                "  C\n"
9354                "};\n"
9355                "struct B {\n"
9356                "  int x;\n"
9357                "};\n"
9358                "}\n",
9359                WebKitBraceStyle);
9360   verifyFormat("struct S {\n"
9361                "  int Type;\n"
9362                "  union {\n"
9363                "    int x;\n"
9364                "    double y;\n"
9365                "  } Value;\n"
9366                "  class C {\n"
9367                "    MyFavoriteType Value;\n"
9368                "  } Class;\n"
9369                "};\n",
9370                WebKitBraceStyle);
9371 }
9372 
9373 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9374   verifyFormat("void f() {\n"
9375                "  try {\n"
9376                "  } catch (const Exception &e) {\n"
9377                "  }\n"
9378                "}\n",
9379                getLLVMStyle());
9380 }
9381 
9382 TEST_F(FormatTest, UnderstandsPragmas) {
9383   verifyFormat("#pragma omp reduction(| : var)");
9384   verifyFormat("#pragma omp reduction(+ : var)");
9385 
9386   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9387             "(including parentheses).",
9388             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9389                    "(including parentheses)."));
9390 }
9391 
9392 TEST_F(FormatTest, UnderstandPragmaOption) {
9393   verifyFormat("#pragma option -C -A");
9394 
9395   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9396 }
9397 
9398 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9399   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9400   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9401                                   << " differs from Style #0"
9402 
9403 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9404   SmallVector<FormatStyle, 3> Styles;
9405   Styles.resize(3);
9406 
9407   Styles[0] = getLLVMStyle();
9408   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9409   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9410   EXPECT_ALL_STYLES_EQUAL(Styles);
9411 
9412   Styles[0] = getGoogleStyle();
9413   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9414   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9415   EXPECT_ALL_STYLES_EQUAL(Styles);
9416 
9417   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9418   EXPECT_TRUE(
9419       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9420   EXPECT_TRUE(
9421       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9422   EXPECT_ALL_STYLES_EQUAL(Styles);
9423 
9424   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9425   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9426   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9427   EXPECT_ALL_STYLES_EQUAL(Styles);
9428 
9429   Styles[0] = getMozillaStyle();
9430   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9431   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9432   EXPECT_ALL_STYLES_EQUAL(Styles);
9433 
9434   Styles[0] = getWebKitStyle();
9435   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9436   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9437   EXPECT_ALL_STYLES_EQUAL(Styles);
9438 
9439   Styles[0] = getGNUStyle();
9440   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9441   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9442   EXPECT_ALL_STYLES_EQUAL(Styles);
9443 
9444   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9445 }
9446 
9447 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9448   SmallVector<FormatStyle, 8> Styles;
9449   Styles.resize(2);
9450 
9451   Styles[0] = getGoogleStyle();
9452   Styles[1] = getLLVMStyle();
9453   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9454   EXPECT_ALL_STYLES_EQUAL(Styles);
9455 
9456   Styles.resize(5);
9457   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9458   Styles[1] = getLLVMStyle();
9459   Styles[1].Language = FormatStyle::LK_JavaScript;
9460   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9461 
9462   Styles[2] = getLLVMStyle();
9463   Styles[2].Language = FormatStyle::LK_JavaScript;
9464   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9465                                   "BasedOnStyle: Google",
9466                                   &Styles[2])
9467                    .value());
9468 
9469   Styles[3] = getLLVMStyle();
9470   Styles[3].Language = FormatStyle::LK_JavaScript;
9471   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9472                                   "Language: JavaScript",
9473                                   &Styles[3])
9474                    .value());
9475 
9476   Styles[4] = getLLVMStyle();
9477   Styles[4].Language = FormatStyle::LK_JavaScript;
9478   EXPECT_EQ(0, parseConfiguration("---\n"
9479                                   "BasedOnStyle: LLVM\n"
9480                                   "IndentWidth: 123\n"
9481                                   "---\n"
9482                                   "BasedOnStyle: Google\n"
9483                                   "Language: JavaScript",
9484                                   &Styles[4])
9485                    .value());
9486   EXPECT_ALL_STYLES_EQUAL(Styles);
9487 }
9488 
9489 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9490   Style.FIELD = false;                                                         \
9491   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9492   EXPECT_TRUE(Style.FIELD);                                                    \
9493   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9494   EXPECT_FALSE(Style.FIELD);
9495 
9496 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9497 
9498 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9499   Style.STRUCT.FIELD = false;                                                  \
9500   EXPECT_EQ(0,                                                                 \
9501             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9502                 .value());                                                     \
9503   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9504   EXPECT_EQ(0,                                                                 \
9505             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9506                 .value());                                                     \
9507   EXPECT_FALSE(Style.STRUCT.FIELD);
9508 
9509 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9510   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9511 
9512 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9513   EXPECT_NE(VALUE, Style.FIELD);                                               \
9514   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9515   EXPECT_EQ(VALUE, Style.FIELD)
9516 
9517 TEST_F(FormatTest, ParsesConfigurationBools) {
9518   FormatStyle Style = {};
9519   Style.Language = FormatStyle::LK_Cpp;
9520   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9521   CHECK_PARSE_BOOL(AlignOperands);
9522   CHECK_PARSE_BOOL(AlignTrailingComments);
9523   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9524   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9525   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9526   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9527   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9528   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9529   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9530   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9531   CHECK_PARSE_BOOL(BinPackArguments);
9532   CHECK_PARSE_BOOL(BinPackParameters);
9533   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
9534   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9535   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9536   CHECK_PARSE_BOOL(BreakStringLiterals);
9537   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9538   CHECK_PARSE_BOOL(DerivePointerAlignment);
9539   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9540   CHECK_PARSE_BOOL(DisableFormat);
9541   CHECK_PARSE_BOOL(IndentCaseLabels);
9542   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9543   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9544   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9545   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9546   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9547   CHECK_PARSE_BOOL(ReflowComments);
9548   CHECK_PARSE_BOOL(SortIncludes);
9549   CHECK_PARSE_BOOL(SpacesInParentheses);
9550   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9551   CHECK_PARSE_BOOL(SpacesInAngles);
9552   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9553   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9554   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9555   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9556   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
9557   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9558 
9559   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9560   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9561   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9562   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9563   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9564   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9565   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9566   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9567   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9568   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9569   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9570 }
9571 
9572 #undef CHECK_PARSE_BOOL
9573 
9574 TEST_F(FormatTest, ParsesConfiguration) {
9575   FormatStyle Style = {};
9576   Style.Language = FormatStyle::LK_Cpp;
9577   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9578   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9579               ConstructorInitializerIndentWidth, 1234u);
9580   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9581   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9582   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9583   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9584               PenaltyBreakBeforeFirstCallParameter, 1234u);
9585   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9586   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9587               PenaltyReturnTypeOnItsOwnLine, 1234u);
9588   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9589               SpacesBeforeTrailingComments, 1234u);
9590   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9591   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9592   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
9593 
9594   Style.PointerAlignment = FormatStyle::PAS_Middle;
9595   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9596               FormatStyle::PAS_Left);
9597   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9598               FormatStyle::PAS_Right);
9599   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9600               FormatStyle::PAS_Middle);
9601   // For backward compatibility:
9602   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9603               FormatStyle::PAS_Left);
9604   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9605               FormatStyle::PAS_Right);
9606   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9607               FormatStyle::PAS_Middle);
9608 
9609   Style.Standard = FormatStyle::LS_Auto;
9610   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9611   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9612   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9613   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9614   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9615 
9616   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9617   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9618               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9619   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9620               FormatStyle::BOS_None);
9621   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9622               FormatStyle::BOS_All);
9623   // For backward compatibility:
9624   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9625               FormatStyle::BOS_None);
9626   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9627               FormatStyle::BOS_All);
9628 
9629   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9630   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9631               FormatStyle::BAS_Align);
9632   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9633               FormatStyle::BAS_DontAlign);
9634   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9635               FormatStyle::BAS_AlwaysBreak);
9636   // For backward compatibility:
9637   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9638               FormatStyle::BAS_DontAlign);
9639   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9640               FormatStyle::BAS_Align);
9641 
9642   Style.UseTab = FormatStyle::UT_ForIndentation;
9643   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9644   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9645   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9646   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9647               FormatStyle::UT_ForContinuationAndIndentation);
9648   // For backward compatibility:
9649   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9650   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9651 
9652   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9653   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9654               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9655   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9656               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9657   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9658               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9659   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9660               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9661   // For backward compatibility:
9662   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9663               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9664   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9665               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9666 
9667   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9668   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9669               FormatStyle::SBPO_Never);
9670   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9671               FormatStyle::SBPO_Always);
9672   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9673               FormatStyle::SBPO_ControlStatements);
9674   // For backward compatibility:
9675   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9676               FormatStyle::SBPO_Never);
9677   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9678               FormatStyle::SBPO_ControlStatements);
9679 
9680   Style.ColumnLimit = 123;
9681   FormatStyle BaseStyle = getLLVMStyle();
9682   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9683   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9684 
9685   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9686   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9687               FormatStyle::BS_Attach);
9688   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9689               FormatStyle::BS_Linux);
9690   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9691               FormatStyle::BS_Mozilla);
9692   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9693               FormatStyle::BS_Stroustrup);
9694   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9695               FormatStyle::BS_Allman);
9696   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9697   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9698               FormatStyle::BS_WebKit);
9699   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9700               FormatStyle::BS_Custom);
9701 
9702   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9703   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9704               FormatStyle::RTBS_None);
9705   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9706               FormatStyle::RTBS_All);
9707   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9708               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9709   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9710               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9711   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9712               AlwaysBreakAfterReturnType,
9713               FormatStyle::RTBS_TopLevelDefinitions);
9714 
9715   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9716   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9717               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9718   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9719               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9720   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9721               AlwaysBreakAfterDefinitionReturnType,
9722               FormatStyle::DRTBS_TopLevel);
9723 
9724   Style.NamespaceIndentation = FormatStyle::NI_All;
9725   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9726               FormatStyle::NI_None);
9727   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9728               FormatStyle::NI_Inner);
9729   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9730               FormatStyle::NI_All);
9731 
9732   // FIXME: This is required because parsing a configuration simply overwrites
9733   // the first N elements of the list instead of resetting it.
9734   Style.ForEachMacros.clear();
9735   std::vector<std::string> BoostForeach;
9736   BoostForeach.push_back("BOOST_FOREACH");
9737   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9738   std::vector<std::string> BoostAndQForeach;
9739   BoostAndQForeach.push_back("BOOST_FOREACH");
9740   BoostAndQForeach.push_back("Q_FOREACH");
9741   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9742               BoostAndQForeach);
9743 
9744   Style.IncludeCategories.clear();
9745   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9746                                                                   {".*", 1}};
9747   CHECK_PARSE("IncludeCategories:\n"
9748               "  - Regex: abc/.*\n"
9749               "    Priority: 2\n"
9750               "  - Regex: .*\n"
9751               "    Priority: 1",
9752               IncludeCategories, ExpectedCategories);
9753   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
9754 }
9755 
9756 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9757   FormatStyle Style = {};
9758   Style.Language = FormatStyle::LK_Cpp;
9759   CHECK_PARSE("Language: Cpp\n"
9760               "IndentWidth: 12",
9761               IndentWidth, 12u);
9762   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9763                                "IndentWidth: 34",
9764                                &Style),
9765             ParseError::Unsuitable);
9766   EXPECT_EQ(12u, Style.IndentWidth);
9767   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9768   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9769 
9770   Style.Language = FormatStyle::LK_JavaScript;
9771   CHECK_PARSE("Language: JavaScript\n"
9772               "IndentWidth: 12",
9773               IndentWidth, 12u);
9774   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9775   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9776                                "IndentWidth: 34",
9777                                &Style),
9778             ParseError::Unsuitable);
9779   EXPECT_EQ(23u, Style.IndentWidth);
9780   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9781   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9782 
9783   CHECK_PARSE("BasedOnStyle: LLVM\n"
9784               "IndentWidth: 67",
9785               IndentWidth, 67u);
9786 
9787   CHECK_PARSE("---\n"
9788               "Language: JavaScript\n"
9789               "IndentWidth: 12\n"
9790               "---\n"
9791               "Language: Cpp\n"
9792               "IndentWidth: 34\n"
9793               "...\n",
9794               IndentWidth, 12u);
9795 
9796   Style.Language = FormatStyle::LK_Cpp;
9797   CHECK_PARSE("---\n"
9798               "Language: JavaScript\n"
9799               "IndentWidth: 12\n"
9800               "---\n"
9801               "Language: Cpp\n"
9802               "IndentWidth: 34\n"
9803               "...\n",
9804               IndentWidth, 34u);
9805   CHECK_PARSE("---\n"
9806               "IndentWidth: 78\n"
9807               "---\n"
9808               "Language: JavaScript\n"
9809               "IndentWidth: 56\n"
9810               "...\n",
9811               IndentWidth, 78u);
9812 
9813   Style.ColumnLimit = 123;
9814   Style.IndentWidth = 234;
9815   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9816   Style.TabWidth = 345;
9817   EXPECT_FALSE(parseConfiguration("---\n"
9818                                   "IndentWidth: 456\n"
9819                                   "BreakBeforeBraces: Allman\n"
9820                                   "---\n"
9821                                   "Language: JavaScript\n"
9822                                   "IndentWidth: 111\n"
9823                                   "TabWidth: 111\n"
9824                                   "---\n"
9825                                   "Language: Cpp\n"
9826                                   "BreakBeforeBraces: Stroustrup\n"
9827                                   "TabWidth: 789\n"
9828                                   "...\n",
9829                                   &Style));
9830   EXPECT_EQ(123u, Style.ColumnLimit);
9831   EXPECT_EQ(456u, Style.IndentWidth);
9832   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9833   EXPECT_EQ(789u, Style.TabWidth);
9834 
9835   EXPECT_EQ(parseConfiguration("---\n"
9836                                "Language: JavaScript\n"
9837                                "IndentWidth: 56\n"
9838                                "---\n"
9839                                "IndentWidth: 78\n"
9840                                "...\n",
9841                                &Style),
9842             ParseError::Error);
9843   EXPECT_EQ(parseConfiguration("---\n"
9844                                "Language: JavaScript\n"
9845                                "IndentWidth: 56\n"
9846                                "---\n"
9847                                "Language: JavaScript\n"
9848                                "IndentWidth: 78\n"
9849                                "...\n",
9850                                &Style),
9851             ParseError::Error);
9852 
9853   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9854 }
9855 
9856 #undef CHECK_PARSE
9857 
9858 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9859   FormatStyle Style = {};
9860   Style.Language = FormatStyle::LK_JavaScript;
9861   Style.BreakBeforeTernaryOperators = true;
9862   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9863   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9864 
9865   Style.BreakBeforeTernaryOperators = true;
9866   EXPECT_EQ(0, parseConfiguration("---\n"
9867                                   "BasedOnStyle: Google\n"
9868                                   "---\n"
9869                                   "Language: JavaScript\n"
9870                                   "IndentWidth: 76\n"
9871                                   "...\n",
9872                                   &Style)
9873                    .value());
9874   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9875   EXPECT_EQ(76u, Style.IndentWidth);
9876   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9877 }
9878 
9879 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9880   FormatStyle Style = getLLVMStyle();
9881   std::string YAML = configurationAsText(Style);
9882   FormatStyle ParsedStyle = {};
9883   ParsedStyle.Language = FormatStyle::LK_Cpp;
9884   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9885   EXPECT_EQ(Style, ParsedStyle);
9886 }
9887 
9888 TEST_F(FormatTest, WorksFor8bitEncodings) {
9889   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9890             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9891             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9892             "\"\xef\xee\xf0\xf3...\"",
9893             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9894                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9895                    "\xef\xee\xf0\xf3...\"",
9896                    getLLVMStyleWithColumns(12)));
9897 }
9898 
9899 TEST_F(FormatTest, HandlesUTF8BOM) {
9900   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9901   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9902             format("\xef\xbb\xbf#include <iostream>"));
9903   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9904             format("\xef\xbb\xbf\n#include <iostream>"));
9905 }
9906 
9907 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9908 #if !defined(_MSC_VER)
9909 
9910 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9911   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9912                getLLVMStyleWithColumns(35));
9913   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9914                getLLVMStyleWithColumns(31));
9915   verifyFormat("// Однажды в студёную зимнюю пору...",
9916                getLLVMStyleWithColumns(36));
9917   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9918   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9919                getLLVMStyleWithColumns(39));
9920   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9921                getLLVMStyleWithColumns(35));
9922 }
9923 
9924 TEST_F(FormatTest, SplitsUTF8Strings) {
9925   // Non-printable characters' width is currently considered to be the length in
9926   // bytes in UTF8. The characters can be displayed in very different manner
9927   // (zero-width, single width with a substitution glyph, expanded to their code
9928   // (e.g. "<8d>"), so there's no single correct way to handle them.
9929   EXPECT_EQ("\"aaaaÄ\"\n"
9930             "\"\xc2\x8d\";",
9931             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9932   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9933             "\"\xc2\x8d\";",
9934             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9935   EXPECT_EQ("\"Однажды, в \"\n"
9936             "\"студёную \"\n"
9937             "\"зимнюю \"\n"
9938             "\"пору,\"",
9939             format("\"Однажды, в студёную зимнюю пору,\"",
9940                    getLLVMStyleWithColumns(13)));
9941   EXPECT_EQ(
9942       "\"一 二 三 \"\n"
9943       "\"四 五六 \"\n"
9944       "\"七 八 九 \"\n"
9945       "\"十\"",
9946       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9947   EXPECT_EQ("\"一\t二 \"\n"
9948             "\"\t三 \"\n"
9949             "\"四 五\t六 \"\n"
9950             "\"\t七 \"\n"
9951             "\"八九十\tqq\"",
9952             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9953                    getLLVMStyleWithColumns(11)));
9954 
9955   // UTF8 character in an escape sequence.
9956   EXPECT_EQ("\"aaaaaa\"\n"
9957             "\"\\\xC2\x8D\"",
9958             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9959 }
9960 
9961 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9962   EXPECT_EQ("const char *sssss =\n"
9963             "    \"一二三四五六七八\\\n"
9964             " 九 十\";",
9965             format("const char *sssss = \"一二三四五六七八\\\n"
9966                    " 九 十\";",
9967                    getLLVMStyleWithColumns(30)));
9968 }
9969 
9970 TEST_F(FormatTest, SplitsUTF8LineComments) {
9971   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9972             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9973   EXPECT_EQ("// Я из лесу\n"
9974             "// вышел; был\n"
9975             "// сильный\n"
9976             "// мороз.",
9977             format("// Я из лесу вышел; был сильный мороз.",
9978                    getLLVMStyleWithColumns(13)));
9979   EXPECT_EQ("// 一二三\n"
9980             "// 四五六七\n"
9981             "// 八  九\n"
9982             "// 十",
9983             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9984 }
9985 
9986 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9987   EXPECT_EQ("/* Гляжу,\n"
9988             " * поднимается\n"
9989             " * медленно в\n"
9990             " * гору\n"
9991             " * Лошадка,\n"
9992             " * везущая\n"
9993             " * хворосту\n"
9994             " * воз. */",
9995             format("/* Гляжу, поднимается медленно в гору\n"
9996                    " * Лошадка, везущая хворосту воз. */",
9997                    getLLVMStyleWithColumns(13)));
9998   EXPECT_EQ(
9999       "/* 一二三\n"
10000       " * 四五六七\n"
10001       " * 八  九\n"
10002       " * 十  */",
10003       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10004   EXPECT_EQ("/* �������� ��������\n"
10005             " * ��������\n"
10006             " * ������-�� */",
10007             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10008 }
10009 
10010 #endif // _MSC_VER
10011 
10012 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10013   FormatStyle Style = getLLVMStyle();
10014 
10015   Style.ConstructorInitializerIndentWidth = 4;
10016   verifyFormat(
10017       "SomeClass::Constructor()\n"
10018       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10019       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10020       Style);
10021 
10022   Style.ConstructorInitializerIndentWidth = 2;
10023   verifyFormat(
10024       "SomeClass::Constructor()\n"
10025       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10026       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10027       Style);
10028 
10029   Style.ConstructorInitializerIndentWidth = 0;
10030   verifyFormat(
10031       "SomeClass::Constructor()\n"
10032       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10033       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10034       Style);
10035   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10036   verifyFormat(
10037       "SomeLongTemplateVariableName<\n"
10038       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10039       Style);
10040   verifyFormat(
10041       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10042       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10043       Style);
10044 }
10045 
10046 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10047   FormatStyle Style = getLLVMStyle();
10048   Style.BreakConstructorInitializersBeforeComma = true;
10049   Style.ConstructorInitializerIndentWidth = 4;
10050   verifyFormat("SomeClass::Constructor()\n"
10051                "    : a(a)\n"
10052                "    , b(b)\n"
10053                "    , c(c) {}",
10054                Style);
10055   verifyFormat("SomeClass::Constructor()\n"
10056                "    : a(a) {}",
10057                Style);
10058 
10059   Style.ColumnLimit = 0;
10060   verifyFormat("SomeClass::Constructor()\n"
10061                "    : a(a) {}",
10062                Style);
10063   verifyFormat("SomeClass::Constructor() noexcept\n"
10064                "    : a(a) {}",
10065                Style);
10066   verifyFormat("SomeClass::Constructor()\n"
10067                "    : a(a)\n"
10068                "    , b(b)\n"
10069                "    , c(c) {}",
10070                Style);
10071   verifyFormat("SomeClass::Constructor()\n"
10072                "    : a(a) {\n"
10073                "  foo();\n"
10074                "  bar();\n"
10075                "}",
10076                Style);
10077 
10078   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10079   verifyFormat("SomeClass::Constructor()\n"
10080                "    : a(a)\n"
10081                "    , b(b)\n"
10082                "    , c(c) {\n}",
10083                Style);
10084   verifyFormat("SomeClass::Constructor()\n"
10085                "    : a(a) {\n}",
10086                Style);
10087 
10088   Style.ColumnLimit = 80;
10089   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10090   Style.ConstructorInitializerIndentWidth = 2;
10091   verifyFormat("SomeClass::Constructor()\n"
10092                "  : a(a)\n"
10093                "  , b(b)\n"
10094                "  , c(c) {}",
10095                Style);
10096 
10097   Style.ConstructorInitializerIndentWidth = 0;
10098   verifyFormat("SomeClass::Constructor()\n"
10099                ": a(a)\n"
10100                ", b(b)\n"
10101                ", c(c) {}",
10102                Style);
10103 
10104   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10105   Style.ConstructorInitializerIndentWidth = 4;
10106   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10107   verifyFormat(
10108       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10109       Style);
10110   verifyFormat(
10111       "SomeClass::Constructor()\n"
10112       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10113       Style);
10114   Style.ConstructorInitializerIndentWidth = 4;
10115   Style.ColumnLimit = 60;
10116   verifyFormat("SomeClass::Constructor()\n"
10117                "    : aaaaaaaa(aaaaaaaa)\n"
10118                "    , aaaaaaaa(aaaaaaaa)\n"
10119                "    , aaaaaaaa(aaaaaaaa) {}",
10120                Style);
10121 }
10122 
10123 TEST_F(FormatTest, Destructors) {
10124   verifyFormat("void F(int &i) { i.~int(); }");
10125   verifyFormat("void F(int &i) { i->~int(); }");
10126 }
10127 
10128 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10129   FormatStyle Style = getWebKitStyle();
10130 
10131   // Don't indent in outer namespaces.
10132   verifyFormat("namespace outer {\n"
10133                "int i;\n"
10134                "namespace inner {\n"
10135                "    int i;\n"
10136                "} // namespace inner\n"
10137                "} // namespace outer\n"
10138                "namespace other_outer {\n"
10139                "int i;\n"
10140                "}",
10141                Style);
10142 
10143   // Don't indent case labels.
10144   verifyFormat("switch (variable) {\n"
10145                "case 1:\n"
10146                "case 2:\n"
10147                "    doSomething();\n"
10148                "    break;\n"
10149                "default:\n"
10150                "    ++variable;\n"
10151                "}",
10152                Style);
10153 
10154   // Wrap before binary operators.
10155   EXPECT_EQ("void f()\n"
10156             "{\n"
10157             "    if (aaaaaaaaaaaaaaaa\n"
10158             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10159             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10160             "        return;\n"
10161             "}",
10162             format("void f() {\n"
10163                    "if (aaaaaaaaaaaaaaaa\n"
10164                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10165                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10166                    "return;\n"
10167                    "}",
10168                    Style));
10169 
10170   // Allow functions on a single line.
10171   verifyFormat("void f() { return; }", Style);
10172 
10173   // Constructor initializers are formatted one per line with the "," on the
10174   // new line.
10175   verifyFormat("Constructor()\n"
10176                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10177                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10178                "          aaaaaaaaaaaaaa)\n"
10179                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10180                "{\n"
10181                "}",
10182                Style);
10183   verifyFormat("SomeClass::Constructor()\n"
10184                "    : a(a)\n"
10185                "{\n"
10186                "}",
10187                Style);
10188   EXPECT_EQ("SomeClass::Constructor()\n"
10189             "    : a(a)\n"
10190             "{\n"
10191             "}",
10192             format("SomeClass::Constructor():a(a){}", Style));
10193   verifyFormat("SomeClass::Constructor()\n"
10194                "    : a(a)\n"
10195                "    , b(b)\n"
10196                "    , c(c)\n"
10197                "{\n"
10198                "}",
10199                Style);
10200   verifyFormat("SomeClass::Constructor()\n"
10201                "    : a(a)\n"
10202                "{\n"
10203                "    foo();\n"
10204                "    bar();\n"
10205                "}",
10206                Style);
10207 
10208   // Access specifiers should be aligned left.
10209   verifyFormat("class C {\n"
10210                "public:\n"
10211                "    int i;\n"
10212                "};",
10213                Style);
10214 
10215   // Do not align comments.
10216   verifyFormat("int a; // Do not\n"
10217                "double b; // align comments.",
10218                Style);
10219 
10220   // Do not align operands.
10221   EXPECT_EQ("ASSERT(aaaa\n"
10222             "    || bbbb);",
10223             format("ASSERT ( aaaa\n||bbbb);", Style));
10224 
10225   // Accept input's line breaks.
10226   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10227             "    || bbbbbbbbbbbbbbb) {\n"
10228             "    i++;\n"
10229             "}",
10230             format("if (aaaaaaaaaaaaaaa\n"
10231                    "|| bbbbbbbbbbbbbbb) { i++; }",
10232                    Style));
10233   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10234             "    i++;\n"
10235             "}",
10236             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10237 
10238   // Don't automatically break all macro definitions (llvm.org/PR17842).
10239   verifyFormat("#define aNumber 10", Style);
10240   // However, generally keep the line breaks that the user authored.
10241   EXPECT_EQ("#define aNumber \\\n"
10242             "    10",
10243             format("#define aNumber \\\n"
10244                    " 10",
10245                    Style));
10246 
10247   // Keep empty and one-element array literals on a single line.
10248   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10249             "                                  copyItems:YES];",
10250             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10251                    "copyItems:YES];",
10252                    Style));
10253   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10254             "                                  copyItems:YES];",
10255             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10256                    "             copyItems:YES];",
10257                    Style));
10258   // FIXME: This does not seem right, there should be more indentation before
10259   // the array literal's entries. Nested blocks have the same problem.
10260   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10261             "    @\"a\",\n"
10262             "    @\"a\"\n"
10263             "]\n"
10264             "                                  copyItems:YES];",
10265             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10266                    "     @\"a\",\n"
10267                    "     @\"a\"\n"
10268                    "     ]\n"
10269                    "       copyItems:YES];",
10270                    Style));
10271   EXPECT_EQ(
10272       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10273       "                                  copyItems:YES];",
10274       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10275              "   copyItems:YES];",
10276              Style));
10277 
10278   verifyFormat("[self.a b:c c:d];", Style);
10279   EXPECT_EQ("[self.a b:c\n"
10280             "        c:d];",
10281             format("[self.a b:c\n"
10282                    "c:d];",
10283                    Style));
10284 }
10285 
10286 TEST_F(FormatTest, FormatsLambdas) {
10287   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10288   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10289   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10290   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10291   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10292   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10293   verifyFormat("int x = f(*+[] {});");
10294   verifyFormat("void f() {\n"
10295                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10296                "}\n");
10297   verifyFormat("void f() {\n"
10298                "  other(x.begin(), //\n"
10299                "        x.end(),   //\n"
10300                "        [&](int, int) { return 1; });\n"
10301                "}\n");
10302   verifyFormat("SomeFunction([]() { // A cool function...\n"
10303                "  return 43;\n"
10304                "});");
10305   EXPECT_EQ("SomeFunction([]() {\n"
10306             "#define A a\n"
10307             "  return 43;\n"
10308             "});",
10309             format("SomeFunction([](){\n"
10310                    "#define A a\n"
10311                    "return 43;\n"
10312                    "});"));
10313   verifyFormat("void f() {\n"
10314                "  SomeFunction([](decltype(x), A *a) {});\n"
10315                "}");
10316   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10317                "    [](const aaaaaaaaaa &a) { return a; });");
10318   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10319                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10320                "});");
10321   verifyFormat("Constructor()\n"
10322                "    : Field([] { // comment\n"
10323                "        int i;\n"
10324                "      }) {}");
10325   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10326                "  return some_parameter.size();\n"
10327                "};");
10328   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10329                "    [](const string &s) { return s; };");
10330   verifyFormat("int i = aaaaaa ? 1 //\n"
10331                "               : [] {\n"
10332                "                   return 2; //\n"
10333                "                 }();");
10334   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10335                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10336                "                  return x == 2; // force break\n"
10337                "                });");
10338   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10339                "    int iiiiiiiiiiii) {\n"
10340                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10341                "});",
10342                getLLVMStyleWithColumns(60));
10343   verifyFormat("SomeFunction({[&] {\n"
10344                "                // comment\n"
10345                "              },\n"
10346                "              [&] {\n"
10347                "                // comment\n"
10348                "              }});");
10349   verifyFormat("SomeFunction({[&] {\n"
10350                "  // comment\n"
10351                "}});");
10352   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10353                "                             [&]() { return true; },\n"
10354                "                         aaaaa aaaaaaaaa);");
10355 
10356   // Lambdas with return types.
10357   verifyFormat("int c = []() -> int { return 2; }();\n");
10358   verifyFormat("int c = []() -> int * { return 2; }();\n");
10359   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10360   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10361   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10362   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10363   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10364   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10365   verifyFormat("[a, a]() -> a<1> {};");
10366   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10367                "                   int j) -> int {\n"
10368                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10369                "};");
10370   verifyFormat(
10371       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10372       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10373       "      return aaaaaaaaaaaaaaaaa;\n"
10374       "    });",
10375       getLLVMStyleWithColumns(70));
10376   verifyFormat("[]() //\n"
10377                "    -> int {\n"
10378                "  return 1; //\n"
10379                "};");
10380 
10381   // Multiple lambdas in the same parentheses change indentation rules.
10382   verifyFormat("SomeFunction(\n"
10383                "    []() {\n"
10384                "      int i = 42;\n"
10385                "      return i;\n"
10386                "    },\n"
10387                "    []() {\n"
10388                "      int j = 43;\n"
10389                "      return j;\n"
10390                "    });");
10391 
10392   // More complex introducers.
10393   verifyFormat("return [i, args...] {};");
10394 
10395   // Not lambdas.
10396   verifyFormat("constexpr char hello[]{\"hello\"};");
10397   verifyFormat("double &operator[](int i) { return 0; }\n"
10398                "int i;");
10399   verifyFormat("std::unique_ptr<int[]> foo() {}");
10400   verifyFormat("int i = a[a][a]->f();");
10401   verifyFormat("int i = (*b)[a]->f();");
10402 
10403   // Other corner cases.
10404   verifyFormat("void f() {\n"
10405                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10406                "      );\n"
10407                "}");
10408 
10409   // Lambdas created through weird macros.
10410   verifyFormat("void f() {\n"
10411                "  MACRO((const AA &a) { return 1; });\n"
10412                "  MACRO((AA &a) { return 1; });\n"
10413                "}");
10414 
10415   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10416                "      doo_dah();\n"
10417                "      doo_dah();\n"
10418                "    })) {\n"
10419                "}");
10420   verifyFormat("auto lambda = []() {\n"
10421                "  int a = 2\n"
10422                "#if A\n"
10423                "          + 2\n"
10424                "#endif\n"
10425                "      ;\n"
10426                "};");
10427 }
10428 
10429 TEST_F(FormatTest, FormatsBlocks) {
10430   FormatStyle ShortBlocks = getLLVMStyle();
10431   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10432   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10433   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10434   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10435   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10436   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10437   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10438 
10439   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10440   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10441   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10442 
10443   verifyFormat("[operation setCompletionBlock:^{\n"
10444                "  [self onOperationDone];\n"
10445                "}];");
10446   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10447                "  [self onOperationDone];\n"
10448                "}]};");
10449   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10450                "  f();\n"
10451                "}];");
10452   verifyFormat("int a = [operation block:^int(int *i) {\n"
10453                "  return 1;\n"
10454                "}];");
10455   verifyFormat("[myObject doSomethingWith:arg1\n"
10456                "                      aaa:^int(int *a) {\n"
10457                "                        return 1;\n"
10458                "                      }\n"
10459                "                      bbb:f(a * bbbbbbbb)];");
10460 
10461   verifyFormat("[operation setCompletionBlock:^{\n"
10462                "  [self.delegate newDataAvailable];\n"
10463                "}];",
10464                getLLVMStyleWithColumns(60));
10465   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10466                "  NSString *path = [self sessionFilePath];\n"
10467                "  if (path) {\n"
10468                "    // ...\n"
10469                "  }\n"
10470                "});");
10471   verifyFormat("[[SessionService sharedService]\n"
10472                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10473                "      if (window) {\n"
10474                "        [self windowDidLoad:window];\n"
10475                "      } else {\n"
10476                "        [self errorLoadingWindow];\n"
10477                "      }\n"
10478                "    }];");
10479   verifyFormat("void (^largeBlock)(void) = ^{\n"
10480                "  // ...\n"
10481                "};\n",
10482                getLLVMStyleWithColumns(40));
10483   verifyFormat("[[SessionService sharedService]\n"
10484                "    loadWindowWithCompletionBlock: //\n"
10485                "        ^(SessionWindow *window) {\n"
10486                "          if (window) {\n"
10487                "            [self windowDidLoad:window];\n"
10488                "          } else {\n"
10489                "            [self errorLoadingWindow];\n"
10490                "          }\n"
10491                "        }];",
10492                getLLVMStyleWithColumns(60));
10493   verifyFormat("[myObject doSomethingWith:arg1\n"
10494                "    firstBlock:^(Foo *a) {\n"
10495                "      // ...\n"
10496                "      int i;\n"
10497                "    }\n"
10498                "    secondBlock:^(Bar *b) {\n"
10499                "      // ...\n"
10500                "      int i;\n"
10501                "    }\n"
10502                "    thirdBlock:^Foo(Bar *b) {\n"
10503                "      // ...\n"
10504                "      int i;\n"
10505                "    }];");
10506   verifyFormat("[myObject doSomethingWith:arg1\n"
10507                "               firstBlock:-1\n"
10508                "              secondBlock:^(Bar *b) {\n"
10509                "                // ...\n"
10510                "                int i;\n"
10511                "              }];");
10512 
10513   verifyFormat("f(^{\n"
10514                "  @autoreleasepool {\n"
10515                "    if (a) {\n"
10516                "      g();\n"
10517                "    }\n"
10518                "  }\n"
10519                "});");
10520   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10521   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
10522                "};");
10523 
10524   FormatStyle FourIndent = getLLVMStyle();
10525   FourIndent.ObjCBlockIndentWidth = 4;
10526   verifyFormat("[operation setCompletionBlock:^{\n"
10527                "    [self onOperationDone];\n"
10528                "}];",
10529                FourIndent);
10530 }
10531 
10532 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10533   FormatStyle ZeroColumn = getLLVMStyle();
10534   ZeroColumn.ColumnLimit = 0;
10535 
10536   verifyFormat("[[SessionService sharedService] "
10537                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10538                "  if (window) {\n"
10539                "    [self windowDidLoad:window];\n"
10540                "  } else {\n"
10541                "    [self errorLoadingWindow];\n"
10542                "  }\n"
10543                "}];",
10544                ZeroColumn);
10545   EXPECT_EQ("[[SessionService sharedService]\n"
10546             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10547             "      if (window) {\n"
10548             "        [self windowDidLoad:window];\n"
10549             "      } else {\n"
10550             "        [self errorLoadingWindow];\n"
10551             "      }\n"
10552             "    }];",
10553             format("[[SessionService sharedService]\n"
10554                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10555                    "                if (window) {\n"
10556                    "    [self windowDidLoad:window];\n"
10557                    "  } else {\n"
10558                    "    [self errorLoadingWindow];\n"
10559                    "  }\n"
10560                    "}];",
10561                    ZeroColumn));
10562   verifyFormat("[myObject doSomethingWith:arg1\n"
10563                "    firstBlock:^(Foo *a) {\n"
10564                "      // ...\n"
10565                "      int i;\n"
10566                "    }\n"
10567                "    secondBlock:^(Bar *b) {\n"
10568                "      // ...\n"
10569                "      int i;\n"
10570                "    }\n"
10571                "    thirdBlock:^Foo(Bar *b) {\n"
10572                "      // ...\n"
10573                "      int i;\n"
10574                "    }];",
10575                ZeroColumn);
10576   verifyFormat("f(^{\n"
10577                "  @autoreleasepool {\n"
10578                "    if (a) {\n"
10579                "      g();\n"
10580                "    }\n"
10581                "  }\n"
10582                "});",
10583                ZeroColumn);
10584   verifyFormat("void (^largeBlock)(void) = ^{\n"
10585                "  // ...\n"
10586                "};",
10587                ZeroColumn);
10588 
10589   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10590   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10591             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10592   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10593   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10594             "  int i;\n"
10595             "};",
10596             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10597 }
10598 
10599 TEST_F(FormatTest, SupportsCRLF) {
10600   EXPECT_EQ("int a;\r\n"
10601             "int b;\r\n"
10602             "int c;\r\n",
10603             format("int a;\r\n"
10604                    "  int b;\r\n"
10605                    "    int c;\r\n",
10606                    getLLVMStyle()));
10607   EXPECT_EQ("int a;\r\n"
10608             "int b;\r\n"
10609             "int c;\r\n",
10610             format("int a;\r\n"
10611                    "  int b;\n"
10612                    "    int c;\r\n",
10613                    getLLVMStyle()));
10614   EXPECT_EQ("int a;\n"
10615             "int b;\n"
10616             "int c;\n",
10617             format("int a;\r\n"
10618                    "  int b;\n"
10619                    "    int c;\n",
10620                    getLLVMStyle()));
10621   EXPECT_EQ("\"aaaaaaa \"\r\n"
10622             "\"bbbbbbb\";\r\n",
10623             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10624   EXPECT_EQ("#define A \\\r\n"
10625             "  b;      \\\r\n"
10626             "  c;      \\\r\n"
10627             "  d;\r\n",
10628             format("#define A \\\r\n"
10629                    "  b; \\\r\n"
10630                    "  c; d; \r\n",
10631                    getGoogleStyle()));
10632 
10633   EXPECT_EQ("/*\r\n"
10634             "multi line block comments\r\n"
10635             "should not introduce\r\n"
10636             "an extra carriage return\r\n"
10637             "*/\r\n",
10638             format("/*\r\n"
10639                    "multi line block comments\r\n"
10640                    "should not introduce\r\n"
10641                    "an extra carriage return\r\n"
10642                    "*/\r\n"));
10643 }
10644 
10645 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10646   verifyFormat("MY_CLASS(C) {\n"
10647                "  int i;\n"
10648                "  int j;\n"
10649                "};");
10650 }
10651 
10652 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10653   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10654   TwoIndent.ContinuationIndentWidth = 2;
10655 
10656   EXPECT_EQ("int i =\n"
10657             "  longFunction(\n"
10658             "    arg);",
10659             format("int i = longFunction(arg);", TwoIndent));
10660 
10661   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10662   SixIndent.ContinuationIndentWidth = 6;
10663 
10664   EXPECT_EQ("int i =\n"
10665             "      longFunction(\n"
10666             "            arg);",
10667             format("int i = longFunction(arg);", SixIndent));
10668 }
10669 
10670 TEST_F(FormatTest, SpacesInAngles) {
10671   FormatStyle Spaces = getLLVMStyle();
10672   Spaces.SpacesInAngles = true;
10673 
10674   verifyFormat("static_cast< int >(arg);", Spaces);
10675   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10676   verifyFormat("f< int, float >();", Spaces);
10677   verifyFormat("template <> g() {}", Spaces);
10678   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10679   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10680   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10681                Spaces);
10682 
10683   Spaces.Standard = FormatStyle::LS_Cpp03;
10684   Spaces.SpacesInAngles = true;
10685   verifyFormat("A< A< int > >();", Spaces);
10686 
10687   Spaces.SpacesInAngles = false;
10688   verifyFormat("A<A<int> >();", Spaces);
10689 
10690   Spaces.Standard = FormatStyle::LS_Cpp11;
10691   Spaces.SpacesInAngles = true;
10692   verifyFormat("A< A< int > >();", Spaces);
10693 
10694   Spaces.SpacesInAngles = false;
10695   verifyFormat("A<A<int>>();", Spaces);
10696 }
10697 
10698 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
10699   FormatStyle Style = getLLVMStyle();
10700   Style.SpaceAfterTemplateKeyword = false;
10701   verifyFormat("template<int> void foo();", Style);
10702 }
10703 
10704 TEST_F(FormatTest, TripleAngleBrackets) {
10705   verifyFormat("f<<<1, 1>>>();");
10706   verifyFormat("f<<<1, 1, 1, s>>>();");
10707   verifyFormat("f<<<a, b, c, d>>>();");
10708   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10709   verifyFormat("f<param><<<1, 1>>>();");
10710   verifyFormat("f<1><<<1, 1>>>();");
10711   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10712   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10713                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10714   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
10715                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
10716 }
10717 
10718 TEST_F(FormatTest, MergeLessLessAtEnd) {
10719   verifyFormat("<<");
10720   EXPECT_EQ("< < <", format("\\\n<<<"));
10721   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10722                "aaallvm::outs() <<");
10723   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10724                "aaaallvm::outs()\n    <<");
10725 }
10726 
10727 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10728   std::string code = "#if A\n"
10729                      "#if B\n"
10730                      "a.\n"
10731                      "#endif\n"
10732                      "    a = 1;\n"
10733                      "#else\n"
10734                      "#endif\n"
10735                      "#if C\n"
10736                      "#else\n"
10737                      "#endif\n";
10738   EXPECT_EQ(code, format(code));
10739 }
10740 
10741 TEST_F(FormatTest, HandleConflictMarkers) {
10742   // Git/SVN conflict markers.
10743   EXPECT_EQ("int a;\n"
10744             "void f() {\n"
10745             "  callme(some(parameter1,\n"
10746             "<<<<<<< text by the vcs\n"
10747             "              parameter2),\n"
10748             "||||||| text by the vcs\n"
10749             "              parameter2),\n"
10750             "         parameter3,\n"
10751             "======= text by the vcs\n"
10752             "              parameter2, parameter3),\n"
10753             ">>>>>>> text by the vcs\n"
10754             "         otherparameter);\n",
10755             format("int a;\n"
10756                    "void f() {\n"
10757                    "  callme(some(parameter1,\n"
10758                    "<<<<<<< text by the vcs\n"
10759                    "  parameter2),\n"
10760                    "||||||| text by the vcs\n"
10761                    "  parameter2),\n"
10762                    "  parameter3,\n"
10763                    "======= text by the vcs\n"
10764                    "  parameter2,\n"
10765                    "  parameter3),\n"
10766                    ">>>>>>> text by the vcs\n"
10767                    "  otherparameter);\n"));
10768 
10769   // Perforce markers.
10770   EXPECT_EQ("void f() {\n"
10771             "  function(\n"
10772             ">>>> text by the vcs\n"
10773             "      parameter,\n"
10774             "==== text by the vcs\n"
10775             "      parameter,\n"
10776             "==== text by the vcs\n"
10777             "      parameter,\n"
10778             "<<<< text by the vcs\n"
10779             "      parameter);\n",
10780             format("void f() {\n"
10781                    "  function(\n"
10782                    ">>>> text by the vcs\n"
10783                    "  parameter,\n"
10784                    "==== text by the vcs\n"
10785                    "  parameter,\n"
10786                    "==== text by the vcs\n"
10787                    "  parameter,\n"
10788                    "<<<< text by the vcs\n"
10789                    "  parameter);\n"));
10790 
10791   EXPECT_EQ("<<<<<<<\n"
10792             "|||||||\n"
10793             "=======\n"
10794             ">>>>>>>",
10795             format("<<<<<<<\n"
10796                    "|||||||\n"
10797                    "=======\n"
10798                    ">>>>>>>"));
10799 
10800   EXPECT_EQ("<<<<<<<\n"
10801             "|||||||\n"
10802             "int i;\n"
10803             "=======\n"
10804             ">>>>>>>",
10805             format("<<<<<<<\n"
10806                    "|||||||\n"
10807                    "int i;\n"
10808                    "=======\n"
10809                    ">>>>>>>"));
10810 
10811   // FIXME: Handle parsing of macros around conflict markers correctly:
10812   EXPECT_EQ("#define Macro \\\n"
10813             "<<<<<<<\n"
10814             "Something \\\n"
10815             "|||||||\n"
10816             "Else \\\n"
10817             "=======\n"
10818             "Other \\\n"
10819             ">>>>>>>\n"
10820             "    End int i;\n",
10821             format("#define Macro \\\n"
10822                    "<<<<<<<\n"
10823                    "  Something \\\n"
10824                    "|||||||\n"
10825                    "  Else \\\n"
10826                    "=======\n"
10827                    "  Other \\\n"
10828                    ">>>>>>>\n"
10829                    "  End\n"
10830                    "int i;\n"));
10831 }
10832 
10833 TEST_F(FormatTest, DisableRegions) {
10834   EXPECT_EQ("int i;\n"
10835             "// clang-format off\n"
10836             "  int j;\n"
10837             "// clang-format on\n"
10838             "int k;",
10839             format(" int  i;\n"
10840                    "   // clang-format off\n"
10841                    "  int j;\n"
10842                    " // clang-format on\n"
10843                    "   int   k;"));
10844   EXPECT_EQ("int i;\n"
10845             "/* clang-format off */\n"
10846             "  int j;\n"
10847             "/* clang-format on */\n"
10848             "int k;",
10849             format(" int  i;\n"
10850                    "   /* clang-format off */\n"
10851                    "  int j;\n"
10852                    " /* clang-format on */\n"
10853                    "   int   k;"));
10854 }
10855 
10856 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10857   format("? ) =");
10858   verifyNoCrash("#define a\\\n /**/}");
10859 }
10860 
10861 TEST_F(FormatTest, FormatsTableGenCode) {
10862   FormatStyle Style = getLLVMStyle();
10863   Style.Language = FormatStyle::LK_TableGen;
10864   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10865 }
10866 
10867 TEST_F(FormatTest, ArrayOfTemplates) {
10868   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10869             format("auto a = new unique_ptr<int > [ 10];"));
10870 
10871   FormatStyle Spaces = getLLVMStyle();
10872   Spaces.SpacesInSquareBrackets = true;
10873   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10874             format("auto a = new unique_ptr<int > [10];", Spaces));
10875 }
10876 
10877 TEST_F(FormatTest, ArrayAsTemplateType) {
10878   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10879             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10880 
10881   FormatStyle Spaces = getLLVMStyle();
10882   Spaces.SpacesInSquareBrackets = true;
10883   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10884             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10885 }
10886 
10887 // Since this test case uses UNIX-style file path. We disable it for MS
10888 // compiler.
10889 #if !defined(_MSC_VER) && !defined(__MINGW32__)
10890 
10891 TEST(FormatStyle, GetStyleOfFile) {
10892   vfs::InMemoryFileSystem FS;
10893   // Test 1: format file in the same directory.
10894   ASSERT_TRUE(
10895       FS.addFile("/a/.clang-format", 0,
10896                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10897   ASSERT_TRUE(
10898       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10899   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10900   ASSERT_EQ(Style1, getLLVMStyle());
10901 
10902   // Test 2: fallback to default.
10903   ASSERT_TRUE(
10904       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10905   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10906   ASSERT_EQ(Style2, getMozillaStyle());
10907 
10908   // Test 3: format file in parent directory.
10909   ASSERT_TRUE(
10910       FS.addFile("/c/.clang-format", 0,
10911                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
10912   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
10913                          llvm::MemoryBuffer::getMemBuffer("int i;")));
10914   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
10915   ASSERT_EQ(Style3, getGoogleStyle());
10916 }
10917 
10918 #endif // _MSC_VER
10919 
10920 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
10921   // Column limit is 20.
10922   std::string Code = "Type *a =\n"
10923                      "    new Type();\n"
10924                      "g(iiiii, 0, jjjjj,\n"
10925                      "  0, kkkkk, 0, mm);\n"
10926                      "int  bad     = format   ;";
10927   std::string Expected = "auto a = new Type();\n"
10928                          "g(iiiii, nullptr,\n"
10929                          "  jjjjj, nullptr,\n"
10930                          "  kkkkk, nullptr,\n"
10931                          "  mm);\n"
10932                          "int  bad     = format   ;";
10933   FileID ID = Context.createInMemoryFile("format.cpp", Code);
10934   tooling::Replacements Replaces = toReplacements(
10935       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
10936                             "auto "),
10937        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
10938                             "nullptr"),
10939        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
10940                             "nullptr"),
10941        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
10942                             "nullptr")});
10943 
10944   format::FormatStyle Style = format::getLLVMStyle();
10945   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
10946   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10947   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10948       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10949   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10950   EXPECT_TRUE(static_cast<bool>(Result));
10951   EXPECT_EQ(Expected, *Result);
10952 }
10953 
10954 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
10955   std::string Code = "#include \"a.h\"\n"
10956                      "#include \"c.h\"\n"
10957                      "\n"
10958                      "int main() {\n"
10959                      "  return 0;\n"
10960                      "}";
10961   std::string Expected = "#include \"a.h\"\n"
10962                          "#include \"b.h\"\n"
10963                          "#include \"c.h\"\n"
10964                          "\n"
10965                          "int main() {\n"
10966                          "  return 0;\n"
10967                          "}";
10968   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
10969   tooling::Replacements Replaces = toReplacements(
10970       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
10971                             "#include \"b.h\"\n")});
10972 
10973   format::FormatStyle Style = format::getLLVMStyle();
10974   Style.SortIncludes = true;
10975   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10976   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10977       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10978   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10979   EXPECT_TRUE(static_cast<bool>(Result));
10980   EXPECT_EQ(Expected, *Result);
10981 }
10982 
10983 TEST_F(FormatTest, AllignTrailingComments) {
10984   EXPECT_EQ("#define MACRO(V)                       \\\n"
10985             "  V(Rt2) /* one more char */           \\\n"
10986             "  V(Rs)  /* than here  */              \\\n"
10987             "/* comment 3 */\n",
10988             format("#define MACRO(V)\\\n"
10989                    "V(Rt2)  /* one more char */ \\\n"
10990                    "V(Rs) /* than here  */    \\\n"
10991                    "/* comment 3 */         \\\n",
10992                    getLLVMStyleWithColumns(40)));
10993 }
10994 } // end namespace
10995 } // end namespace format
10996 } // end namespace clang
10997