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       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5096       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5097   verifyFormat(
5098       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5099       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5100       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5101   verifyFormat(
5102       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5103       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5104       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5105   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5106                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5107                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5108                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5109   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5110                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5111   verifyFormat(
5112       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5113       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5114 
5115   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5116                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5117   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5118                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5119                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5120                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5121   verifyFormat("LOG_IF(aaa == //\n"
5122                "       bbb)\n"
5123                "    << a << b;");
5124 
5125   // But sometimes, breaking before the first "<<" is desirable.
5126   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5127                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5128   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5129                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5130                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5131   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5132                "    << BEF << IsTemplate << Description << E->getType();");
5133   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5134                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5135                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5136   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5137                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5138                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5139                "    << aaa;");
5140 
5141   verifyFormat(
5142       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5143       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5144 
5145   // Incomplete string literal.
5146   EXPECT_EQ("llvm::errs() << \"\n"
5147             "             << a;",
5148             format("llvm::errs() << \"\n<<a;"));
5149 
5150   verifyFormat("void f() {\n"
5151                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5152                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5153                "}");
5154 
5155   // Handle 'endl'.
5156   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5157                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5158   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5159 
5160   // Handle '\n'.
5161   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5162                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5163   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5164                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5165   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5166                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5167   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5168 }
5169 
5170 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5171   verifyFormat("return out << \"somepacket = {\\n\"\n"
5172                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5173                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5174                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5175                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5176                "           << \"}\";");
5177 
5178   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5179                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5180                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5181   verifyFormat(
5182       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5183       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5184       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5185       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5186       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5187   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5188                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5189   verifyFormat(
5190       "void f() {\n"
5191       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5192       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5193       "}");
5194 
5195   // Breaking before the first "<<" is generally not desirable.
5196   verifyFormat(
5197       "llvm::errs()\n"
5198       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5199       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5200       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5201       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5202       getLLVMStyleWithColumns(70));
5203   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5204                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5205                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5206                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5207                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5208                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5209                getLLVMStyleWithColumns(70));
5210 
5211   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5212                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5213                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5214   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5215                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5216                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5217   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5218                "           (aaaa + aaaa);",
5219                getLLVMStyleWithColumns(40));
5220   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5221                "                  (aaaaaaa + aaaaa));",
5222                getLLVMStyleWithColumns(40));
5223   verifyFormat(
5224       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5225       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5226       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5227 }
5228 
5229 TEST_F(FormatTest, UnderstandsEquals) {
5230   verifyFormat(
5231       "aaaaaaaaaaaaaaaaa =\n"
5232       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5233   verifyFormat(
5234       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5235       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5236   verifyFormat(
5237       "if (a) {\n"
5238       "  f();\n"
5239       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5240       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5241       "}");
5242 
5243   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5244                "        100000000 + 10000000) {\n}");
5245 }
5246 
5247 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5248   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5249                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5250 
5251   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5252                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5253 
5254   verifyFormat(
5255       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5256       "                                                          Parameter2);");
5257 
5258   verifyFormat(
5259       "ShortObject->shortFunction(\n"
5260       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5261       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5262 
5263   verifyFormat("loooooooooooooongFunction(\n"
5264                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5265 
5266   verifyFormat(
5267       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5268       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5269 
5270   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5271                "    .WillRepeatedly(Return(SomeValue));");
5272   verifyFormat("void f() {\n"
5273                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5274                "      .Times(2)\n"
5275                "      .WillRepeatedly(Return(SomeValue));\n"
5276                "}");
5277   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5278                "    ccccccccccccccccccccccc);");
5279   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5280                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5281                "          .aaaaa(aaaaa),\n"
5282                "      aaaaaaaaaaaaaaaaaaaaa);");
5283   verifyFormat("void f() {\n"
5284                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5285                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5286                "}");
5287   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5288                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5289                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5290                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5291                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5292   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5293                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5294                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5295                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5296                "}");
5297 
5298   // Here, it is not necessary to wrap at "." or "->".
5299   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5300                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5301   verifyFormat(
5302       "aaaaaaaaaaa->aaaaaaaaa(\n"
5303       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5304       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5305 
5306   verifyFormat(
5307       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5308       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5309   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5310                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5311   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5312                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5313 
5314   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5315                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5316                "    .a();");
5317 
5318   FormatStyle NoBinPacking = getLLVMStyle();
5319   NoBinPacking.BinPackParameters = false;
5320   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5321                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5322                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5323                "                         aaaaaaaaaaaaaaaaaaa,\n"
5324                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5325                NoBinPacking);
5326 
5327   // If there is a subsequent call, change to hanging indentation.
5328   verifyFormat(
5329       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5330       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5331       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5332   verifyFormat(
5333       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5334       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5335   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5336                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5337                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5338   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5339                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5340                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5341 }
5342 
5343 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5344   verifyFormat("template <typename T>\n"
5345                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5346   verifyFormat("template <typename T>\n"
5347                "// T should be one of {A, B}.\n"
5348                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5349   verifyFormat(
5350       "template <typename T>\n"
5351       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5352   verifyFormat("template <typename T>\n"
5353                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5354                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5355   verifyFormat(
5356       "template <typename T>\n"
5357       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5358       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5359   verifyFormat(
5360       "template <typename T>\n"
5361       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5362       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5363       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5364   verifyFormat("template <typename T>\n"
5365                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5366                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5367   verifyFormat(
5368       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5369       "          typename T4 = char>\n"
5370       "void f();");
5371   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5372                "          template <typename> class cccccccccccccccccccccc,\n"
5373                "          typename ddddddddddddd>\n"
5374                "class C {};");
5375   verifyFormat(
5376       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5377       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5378 
5379   verifyFormat("void f() {\n"
5380                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5381                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5382                "}");
5383 
5384   verifyFormat("template <typename T> class C {};");
5385   verifyFormat("template <typename T> void f();");
5386   verifyFormat("template <typename T> void f() {}");
5387   verifyFormat(
5388       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5389       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5390       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5391       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5392       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5393       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5394       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5395       getLLVMStyleWithColumns(72));
5396   EXPECT_EQ("static_cast<A< //\n"
5397             "    B> *>(\n"
5398             "\n"
5399             "    );",
5400             format("static_cast<A<//\n"
5401                    "    B>*>(\n"
5402                    "\n"
5403                    "    );"));
5404   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5405                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5406 
5407   FormatStyle AlwaysBreak = getLLVMStyle();
5408   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5409   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5410   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5411   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5412   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5413                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5414                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5415   verifyFormat("template <template <typename> class Fooooooo,\n"
5416                "          template <typename> class Baaaaaaar>\n"
5417                "struct C {};",
5418                AlwaysBreak);
5419   verifyFormat("template <typename T> // T can be A, B or C.\n"
5420                "struct C {};",
5421                AlwaysBreak);
5422   verifyFormat("template <enum E> class A {\n"
5423                "public:\n"
5424                "  E *f();\n"
5425                "};");
5426 }
5427 
5428 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5429   verifyFormat(
5430       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5431       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5432   verifyFormat(
5433       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5434       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5435       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5436 
5437   // FIXME: Should we have the extra indent after the second break?
5438   verifyFormat(
5439       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5440       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5441       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5442 
5443   verifyFormat(
5444       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5445       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5446 
5447   // Breaking at nested name specifiers is generally not desirable.
5448   verifyFormat(
5449       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5450       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5451 
5452   verifyFormat(
5453       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5454       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5455       "                   aaaaaaaaaaaaaaaaaaaaa);",
5456       getLLVMStyleWithColumns(74));
5457 
5458   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5459                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5460                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5461 }
5462 
5463 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5464   verifyFormat("A<int> a;");
5465   verifyFormat("A<A<A<int>>> a;");
5466   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5467   verifyFormat("bool x = a < 1 || 2 > a;");
5468   verifyFormat("bool x = 5 < f<int>();");
5469   verifyFormat("bool x = f<int>() > 5;");
5470   verifyFormat("bool x = 5 < a<int>::x;");
5471   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5472   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5473 
5474   verifyGoogleFormat("A<A<int>> a;");
5475   verifyGoogleFormat("A<A<A<int>>> a;");
5476   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5477   verifyGoogleFormat("A<A<int> > a;");
5478   verifyGoogleFormat("A<A<A<int> > > a;");
5479   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5480   verifyGoogleFormat("A<::A<int>> a;");
5481   verifyGoogleFormat("A<::A> a;");
5482   verifyGoogleFormat("A< ::A> a;");
5483   verifyGoogleFormat("A< ::A<int> > a;");
5484   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5485   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5486   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5487   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5488   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5489             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5490 
5491   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5492 
5493   verifyFormat("test >> a >> b;");
5494   verifyFormat("test << a >> b;");
5495 
5496   verifyFormat("f<int>();");
5497   verifyFormat("template <typename T> void f() {}");
5498   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5499   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5500                "sizeof(char)>::type>;");
5501   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5502   verifyFormat("f(a.operator()<A>());");
5503   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5504                "      .template operator()<A>());",
5505                getLLVMStyleWithColumns(35));
5506 
5507   // Not template parameters.
5508   verifyFormat("return a < b && c > d;");
5509   verifyFormat("void f() {\n"
5510                "  while (a < b && c > d) {\n"
5511                "  }\n"
5512                "}");
5513   verifyFormat("template <typename... Types>\n"
5514                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5515 
5516   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5517                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5518                getLLVMStyleWithColumns(60));
5519   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5520   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5521   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5522 }
5523 
5524 TEST_F(FormatTest, BitshiftOperatorWidth) {
5525   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5526             "                   bar */",
5527             format("int    a=1<<2;  /* foo\n"
5528                    "                   bar */"));
5529 
5530   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5531             "                     bar */",
5532             format("int  b  =256>>1 ;  /* foo\n"
5533                    "                      bar */"));
5534 }
5535 
5536 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5537   verifyFormat("COMPARE(a, ==, b);");
5538   verifyFormat("auto s = sizeof...(Ts) - 1;");
5539 }
5540 
5541 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5542   verifyFormat("int A::*x;");
5543   verifyFormat("int (S::*func)(void *);");
5544   verifyFormat("void f() { int (S::*func)(void *); }");
5545   verifyFormat("typedef bool *(Class::*Member)() const;");
5546   verifyFormat("void f() {\n"
5547                "  (a->*f)();\n"
5548                "  a->*x;\n"
5549                "  (a.*f)();\n"
5550                "  ((*a).*f)();\n"
5551                "  a.*x;\n"
5552                "}");
5553   verifyFormat("void f() {\n"
5554                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5555                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5556                "}");
5557   verifyFormat(
5558       "(aaaaaaaaaa->*bbbbbbb)(\n"
5559       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5560   FormatStyle Style = getLLVMStyle();
5561   Style.PointerAlignment = FormatStyle::PAS_Left;
5562   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5563 }
5564 
5565 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5566   verifyFormat("int a = -2;");
5567   verifyFormat("f(-1, -2, -3);");
5568   verifyFormat("a[-1] = 5;");
5569   verifyFormat("int a = 5 + -2;");
5570   verifyFormat("if (i == -1) {\n}");
5571   verifyFormat("if (i != -1) {\n}");
5572   verifyFormat("if (i > -1) {\n}");
5573   verifyFormat("if (i < -1) {\n}");
5574   verifyFormat("++(a->f());");
5575   verifyFormat("--(a->f());");
5576   verifyFormat("(a->f())++;");
5577   verifyFormat("a[42]++;");
5578   verifyFormat("if (!(a->f())) {\n}");
5579 
5580   verifyFormat("a-- > b;");
5581   verifyFormat("b ? -a : c;");
5582   verifyFormat("n * sizeof char16;");
5583   verifyFormat("n * alignof char16;", getGoogleStyle());
5584   verifyFormat("sizeof(char);");
5585   verifyFormat("alignof(char);", getGoogleStyle());
5586 
5587   verifyFormat("return -1;");
5588   verifyFormat("switch (a) {\n"
5589                "case -1:\n"
5590                "  break;\n"
5591                "}");
5592   verifyFormat("#define X -1");
5593   verifyFormat("#define X -kConstant");
5594 
5595   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5596   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5597 
5598   verifyFormat("int a = /* confusing comment */ -1;");
5599   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5600   verifyFormat("int a = i /* confusing comment */++;");
5601 }
5602 
5603 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5604   verifyFormat("if (!aaaaaaaaaa( // break\n"
5605                "        aaaaa)) {\n"
5606                "}");
5607   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5608                "    aaaaa));");
5609   verifyFormat("*aaa = aaaaaaa( // break\n"
5610                "    bbbbbb);");
5611 }
5612 
5613 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5614   verifyFormat("bool operator<();");
5615   verifyFormat("bool operator>();");
5616   verifyFormat("bool operator=();");
5617   verifyFormat("bool operator==();");
5618   verifyFormat("bool operator!=();");
5619   verifyFormat("int operator+();");
5620   verifyFormat("int operator++();");
5621   verifyFormat("bool operator,();");
5622   verifyFormat("bool operator();");
5623   verifyFormat("bool operator()();");
5624   verifyFormat("bool operator[]();");
5625   verifyFormat("operator bool();");
5626   verifyFormat("operator int();");
5627   verifyFormat("operator void *();");
5628   verifyFormat("operator SomeType<int>();");
5629   verifyFormat("operator SomeType<int, int>();");
5630   verifyFormat("operator SomeType<SomeType<int>>();");
5631   verifyFormat("void *operator new(std::size_t size);");
5632   verifyFormat("void *operator new[](std::size_t size);");
5633   verifyFormat("void operator delete(void *ptr);");
5634   verifyFormat("void operator delete[](void *ptr);");
5635   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5636                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5637   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5638                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5639 
5640   verifyFormat(
5641       "ostream &operator<<(ostream &OutputStream,\n"
5642       "                    SomeReallyLongType WithSomeReallyLongValue);");
5643   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5644                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5645                "  return left.group < right.group;\n"
5646                "}");
5647   verifyFormat("SomeType &operator=(const SomeType &S);");
5648   verifyFormat("f.template operator()<int>();");
5649 
5650   verifyGoogleFormat("operator void*();");
5651   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5652   verifyGoogleFormat("operator ::A();");
5653 
5654   verifyFormat("using A::operator+;");
5655   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5656                "int i;");
5657 }
5658 
5659 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5660   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5661   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5662   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5663   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5664   verifyFormat("Deleted &operator=(const Deleted &) &;");
5665   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5666   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5667   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5668   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5669   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5670   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5671   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
5672   verifyFormat("template <typename T>\n"
5673                "void F(T) && = delete;",
5674                getGoogleStyle());
5675 
5676   FormatStyle AlignLeft = getLLVMStyle();
5677   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5678   verifyFormat("void A::b() && {}", AlignLeft);
5679   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5680   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5681                AlignLeft);
5682   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5683   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5684   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5685   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5686   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5687   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5688   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
5689 
5690   FormatStyle Spaces = getLLVMStyle();
5691   Spaces.SpacesInCStyleCastParentheses = true;
5692   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5693   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5694   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5695   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5696 
5697   Spaces.SpacesInCStyleCastParentheses = false;
5698   Spaces.SpacesInParentheses = true;
5699   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5700   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5701   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5702   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5703 }
5704 
5705 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5706   verifyFormat("void f() {\n"
5707                "  A *a = new A;\n"
5708                "  A *a = new (placement) A;\n"
5709                "  delete a;\n"
5710                "  delete (A *)a;\n"
5711                "}");
5712   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5713                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5714   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5715                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5716                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5717   verifyFormat("delete[] h->p;");
5718 }
5719 
5720 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5721   verifyFormat("int *f(int *a) {}");
5722   verifyFormat("int main(int argc, char **argv) {}");
5723   verifyFormat("Test::Test(int b) : a(b * b) {}");
5724   verifyIndependentOfContext("f(a, *a);");
5725   verifyFormat("void g() { f(*a); }");
5726   verifyIndependentOfContext("int a = b * 10;");
5727   verifyIndependentOfContext("int a = 10 * b;");
5728   verifyIndependentOfContext("int a = b * c;");
5729   verifyIndependentOfContext("int a += b * c;");
5730   verifyIndependentOfContext("int a -= b * c;");
5731   verifyIndependentOfContext("int a *= b * c;");
5732   verifyIndependentOfContext("int a /= b * c;");
5733   verifyIndependentOfContext("int a = *b;");
5734   verifyIndependentOfContext("int a = *b * c;");
5735   verifyIndependentOfContext("int a = b * *c;");
5736   verifyIndependentOfContext("int a = b * (10);");
5737   verifyIndependentOfContext("S << b * (10);");
5738   verifyIndependentOfContext("return 10 * b;");
5739   verifyIndependentOfContext("return *b * *c;");
5740   verifyIndependentOfContext("return a & ~b;");
5741   verifyIndependentOfContext("f(b ? *c : *d);");
5742   verifyIndependentOfContext("int a = b ? *c : *d;");
5743   verifyIndependentOfContext("*b = a;");
5744   verifyIndependentOfContext("a * ~b;");
5745   verifyIndependentOfContext("a * !b;");
5746   verifyIndependentOfContext("a * +b;");
5747   verifyIndependentOfContext("a * -b;");
5748   verifyIndependentOfContext("a * ++b;");
5749   verifyIndependentOfContext("a * --b;");
5750   verifyIndependentOfContext("a[4] * b;");
5751   verifyIndependentOfContext("a[a * a] = 1;");
5752   verifyIndependentOfContext("f() * b;");
5753   verifyIndependentOfContext("a * [self dostuff];");
5754   verifyIndependentOfContext("int x = a * (a + b);");
5755   verifyIndependentOfContext("(a *)(a + b);");
5756   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5757   verifyIndependentOfContext("int *pa = (int *)&a;");
5758   verifyIndependentOfContext("return sizeof(int **);");
5759   verifyIndependentOfContext("return sizeof(int ******);");
5760   verifyIndependentOfContext("return (int **&)a;");
5761   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5762   verifyFormat("void f(Type (*parameter)[10]) {}");
5763   verifyFormat("void f(Type (&parameter)[10]) {}");
5764   verifyGoogleFormat("return sizeof(int**);");
5765   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5766   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5767   verifyFormat("auto a = [](int **&, int ***) {};");
5768   verifyFormat("auto PointerBinding = [](const char *S) {};");
5769   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5770   verifyFormat("[](const decltype(*a) &value) {}");
5771   verifyFormat("decltype(a * b) F();");
5772   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5773   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5774   verifyIndependentOfContext("typedef void (*f)(int *a);");
5775   verifyIndependentOfContext("int i{a * b};");
5776   verifyIndependentOfContext("aaa && aaa->f();");
5777   verifyIndependentOfContext("int x = ~*p;");
5778   verifyFormat("Constructor() : a(a), area(width * height) {}");
5779   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5780   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5781   verifyFormat("void f() { f(a, c * d); }");
5782   verifyFormat("void f() { f(new a(), c * d); }");
5783   verifyFormat("void f(const MyOverride &override);");
5784   verifyFormat("void f(const MyFinal &final);");
5785   verifyIndependentOfContext("bool a = f() && override.f();");
5786   verifyIndependentOfContext("bool a = f() && final.f();");
5787 
5788   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5789 
5790   verifyIndependentOfContext("A<int *> a;");
5791   verifyIndependentOfContext("A<int **> a;");
5792   verifyIndependentOfContext("A<int *, int *> a;");
5793   verifyIndependentOfContext("A<int *[]> a;");
5794   verifyIndependentOfContext(
5795       "const char *const p = reinterpret_cast<const char *const>(q);");
5796   verifyIndependentOfContext("A<int **, int **> a;");
5797   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5798   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5799   verifyFormat("for (; a && b;) {\n}");
5800   verifyFormat("bool foo = true && [] { return false; }();");
5801 
5802   verifyFormat(
5803       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5804       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5805 
5806   verifyGoogleFormat("int const* a = &b;");
5807   verifyGoogleFormat("**outparam = 1;");
5808   verifyGoogleFormat("*outparam = a * b;");
5809   verifyGoogleFormat("int main(int argc, char** argv) {}");
5810   verifyGoogleFormat("A<int*> a;");
5811   verifyGoogleFormat("A<int**> a;");
5812   verifyGoogleFormat("A<int*, int*> a;");
5813   verifyGoogleFormat("A<int**, int**> a;");
5814   verifyGoogleFormat("f(b ? *c : *d);");
5815   verifyGoogleFormat("int a = b ? *c : *d;");
5816   verifyGoogleFormat("Type* t = **x;");
5817   verifyGoogleFormat("Type* t = *++*x;");
5818   verifyGoogleFormat("*++*x;");
5819   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5820   verifyGoogleFormat("Type* t = x++ * y;");
5821   verifyGoogleFormat(
5822       "const char* const p = reinterpret_cast<const char* const>(q);");
5823   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5824   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5825   verifyGoogleFormat("template <typename T>\n"
5826                      "void f(int i = 0, SomeType** temps = NULL);");
5827 
5828   FormatStyle Left = getLLVMStyle();
5829   Left.PointerAlignment = FormatStyle::PAS_Left;
5830   verifyFormat("x = *a(x) = *a(y);", Left);
5831   verifyFormat("for (;; *a = b) {\n}", Left);
5832   verifyFormat("return *this += 1;", Left);
5833 
5834   verifyIndependentOfContext("a = *(x + y);");
5835   verifyIndependentOfContext("a = &(x + y);");
5836   verifyIndependentOfContext("*(x + y).call();");
5837   verifyIndependentOfContext("&(x + y)->call();");
5838   verifyFormat("void f() { &(*I).first; }");
5839 
5840   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5841   verifyFormat(
5842       "int *MyValues = {\n"
5843       "    *A, // Operator detection might be confused by the '{'\n"
5844       "    *BB // Operator detection might be confused by previous comment\n"
5845       "};");
5846 
5847   verifyIndependentOfContext("if (int *a = &b)");
5848   verifyIndependentOfContext("if (int &a = *b)");
5849   verifyIndependentOfContext("if (a & b[i])");
5850   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5851   verifyIndependentOfContext("if (*b[i])");
5852   verifyIndependentOfContext("if (int *a = (&b))");
5853   verifyIndependentOfContext("while (int *a = &b)");
5854   verifyIndependentOfContext("size = sizeof *a;");
5855   verifyIndependentOfContext("if (a && (b = c))");
5856   verifyFormat("void f() {\n"
5857                "  for (const int &v : Values) {\n"
5858                "  }\n"
5859                "}");
5860   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5861   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5862   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5863 
5864   verifyFormat("#define A (!a * b)");
5865   verifyFormat("#define MACRO     \\\n"
5866                "  int *i = a * b; \\\n"
5867                "  void f(a *b);",
5868                getLLVMStyleWithColumns(19));
5869 
5870   verifyIndependentOfContext("A = new SomeType *[Length];");
5871   verifyIndependentOfContext("A = new SomeType *[Length]();");
5872   verifyIndependentOfContext("T **t = new T *;");
5873   verifyIndependentOfContext("T **t = new T *();");
5874   verifyGoogleFormat("A = new SomeType*[Length]();");
5875   verifyGoogleFormat("A = new SomeType*[Length];");
5876   verifyGoogleFormat("T** t = new T*;");
5877   verifyGoogleFormat("T** t = new T*();");
5878 
5879   FormatStyle PointerLeft = getLLVMStyle();
5880   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5881   verifyFormat("delete *x;", PointerLeft);
5882   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5883   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5884   verifyFormat("template <bool a, bool b> "
5885                "typename t::if<x && y>::type f() {}");
5886   verifyFormat("template <int *y> f() {}");
5887   verifyFormat("vector<int *> v;");
5888   verifyFormat("vector<int *const> v;");
5889   verifyFormat("vector<int *const **const *> v;");
5890   verifyFormat("vector<int *volatile> v;");
5891   verifyFormat("vector<a * b> v;");
5892   verifyFormat("foo<b && false>();");
5893   verifyFormat("foo<b & 1>();");
5894   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5895   verifyFormat(
5896       "template <class T,\n"
5897       "          class = typename std::enable_if<\n"
5898       "              std::is_integral<T>::value &&\n"
5899       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5900       "void F();",
5901       getLLVMStyleWithColumns(70));
5902   verifyFormat(
5903       "template <class T,\n"
5904       "          class = typename ::std::enable_if<\n"
5905       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5906       "void F();",
5907       getGoogleStyleWithColumns(68));
5908 
5909   verifyIndependentOfContext("MACRO(int *i);");
5910   verifyIndependentOfContext("MACRO(auto *a);");
5911   verifyIndependentOfContext("MACRO(const A *a);");
5912   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5913   verifyFormat("void f() { f(float{1}, a * a); }");
5914   // FIXME: Is there a way to make this work?
5915   // verifyIndependentOfContext("MACRO(A *a);");
5916 
5917   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5918   verifyFormat("return options != nullptr && operator==(*options);");
5919 
5920   EXPECT_EQ("#define OP(x)                                    \\\n"
5921             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5922             "    return s << a.DebugString();                 \\\n"
5923             "  }",
5924             format("#define OP(x) \\\n"
5925                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5926                    "    return s << a.DebugString(); \\\n"
5927                    "  }",
5928                    getLLVMStyleWithColumns(50)));
5929 
5930   // FIXME: We cannot handle this case yet; we might be able to figure out that
5931   // foo<x> d > v; doesn't make sense.
5932   verifyFormat("foo<a<b && c> d> v;");
5933 
5934   FormatStyle PointerMiddle = getLLVMStyle();
5935   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5936   verifyFormat("delete *x;", PointerMiddle);
5937   verifyFormat("int * x;", PointerMiddle);
5938   verifyFormat("template <int * y> f() {}", PointerMiddle);
5939   verifyFormat("int * f(int * a) {}", PointerMiddle);
5940   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5941   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5942   verifyFormat("A<int *> a;", PointerMiddle);
5943   verifyFormat("A<int **> a;", PointerMiddle);
5944   verifyFormat("A<int *, int *> a;", PointerMiddle);
5945   verifyFormat("A<int * []> a;", PointerMiddle);
5946   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5947   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5948   verifyFormat("T ** t = new T *;", PointerMiddle);
5949 
5950   // Member function reference qualifiers aren't binary operators.
5951   verifyFormat("string // break\n"
5952                "operator()() & {}");
5953   verifyFormat("string // break\n"
5954                "operator()() && {}");
5955   verifyGoogleFormat("template <typename T>\n"
5956                      "auto x() & -> int {}");
5957 }
5958 
5959 TEST_F(FormatTest, UnderstandsAttributes) {
5960   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5961   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5962                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5963   FormatStyle AfterType = getLLVMStyle();
5964   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5965   verifyFormat("__attribute__((nodebug)) void\n"
5966                "foo() {}\n",
5967                AfterType);
5968 }
5969 
5970 TEST_F(FormatTest, UnderstandsEllipsis) {
5971   verifyFormat("int printf(const char *fmt, ...);");
5972   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5973   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5974 
5975   FormatStyle PointersLeft = getLLVMStyle();
5976   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5977   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5978 }
5979 
5980 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5981   EXPECT_EQ("int *a;\n"
5982             "int *a;\n"
5983             "int *a;",
5984             format("int *a;\n"
5985                    "int* a;\n"
5986                    "int *a;",
5987                    getGoogleStyle()));
5988   EXPECT_EQ("int* a;\n"
5989             "int* a;\n"
5990             "int* a;",
5991             format("int* a;\n"
5992                    "int* a;\n"
5993                    "int *a;",
5994                    getGoogleStyle()));
5995   EXPECT_EQ("int *a;\n"
5996             "int *a;\n"
5997             "int *a;",
5998             format("int *a;\n"
5999                    "int * a;\n"
6000                    "int *  a;",
6001                    getGoogleStyle()));
6002   EXPECT_EQ("auto x = [] {\n"
6003             "  int *a;\n"
6004             "  int *a;\n"
6005             "  int *a;\n"
6006             "};",
6007             format("auto x=[]{int *a;\n"
6008                    "int * a;\n"
6009                    "int *  a;};",
6010                    getGoogleStyle()));
6011 }
6012 
6013 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6014   verifyFormat("int f(int &&a) {}");
6015   verifyFormat("int f(int a, char &&b) {}");
6016   verifyFormat("void f() { int &&a = b; }");
6017   verifyGoogleFormat("int f(int a, char&& b) {}");
6018   verifyGoogleFormat("void f() { int&& a = b; }");
6019 
6020   verifyIndependentOfContext("A<int &&> a;");
6021   verifyIndependentOfContext("A<int &&, int &&> a;");
6022   verifyGoogleFormat("A<int&&> a;");
6023   verifyGoogleFormat("A<int&&, int&&> a;");
6024 
6025   // Not rvalue references:
6026   verifyFormat("template <bool B, bool C> class A {\n"
6027                "  static_assert(B && C, \"Something is wrong\");\n"
6028                "};");
6029   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6030   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6031   verifyFormat("#define A(a, b) (a && b)");
6032 }
6033 
6034 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6035   verifyFormat("void f() {\n"
6036                "  x[aaaaaaaaa -\n"
6037                "    b] = 23;\n"
6038                "}",
6039                getLLVMStyleWithColumns(15));
6040 }
6041 
6042 TEST_F(FormatTest, FormatsCasts) {
6043   verifyFormat("Type *A = static_cast<Type *>(P);");
6044   verifyFormat("Type *A = (Type *)P;");
6045   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6046   verifyFormat("int a = (int)(2.0f);");
6047   verifyFormat("int a = (int)2.0f;");
6048   verifyFormat("x[(int32)y];");
6049   verifyFormat("x = (int32)y;");
6050   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6051   verifyFormat("int a = (int)*b;");
6052   verifyFormat("int a = (int)2.0f;");
6053   verifyFormat("int a = (int)~0;");
6054   verifyFormat("int a = (int)++a;");
6055   verifyFormat("int a = (int)sizeof(int);");
6056   verifyFormat("int a = (int)+2;");
6057   verifyFormat("my_int a = (my_int)2.0f;");
6058   verifyFormat("my_int a = (my_int)sizeof(int);");
6059   verifyFormat("return (my_int)aaa;");
6060   verifyFormat("#define x ((int)-1)");
6061   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6062   verifyFormat("#define p(q) ((int *)&q)");
6063   verifyFormat("fn(a)(b) + 1;");
6064 
6065   verifyFormat("void f() { my_int a = (my_int)*b; }");
6066   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6067   verifyFormat("my_int a = (my_int)~0;");
6068   verifyFormat("my_int a = (my_int)++a;");
6069   verifyFormat("my_int a = (my_int)-2;");
6070   verifyFormat("my_int a = (my_int)1;");
6071   verifyFormat("my_int a = (my_int *)1;");
6072   verifyFormat("my_int a = (const my_int)-1;");
6073   verifyFormat("my_int a = (const my_int *)-1;");
6074   verifyFormat("my_int a = (my_int)(my_int)-1;");
6075   verifyFormat("my_int a = (ns::my_int)-2;");
6076   verifyFormat("case (my_int)ONE:");
6077   verifyFormat("auto x = (X)this;");
6078 
6079   // FIXME: single value wrapped with paren will be treated as cast.
6080   verifyFormat("void f(int i = (kValue)*kMask) {}");
6081 
6082   verifyFormat("{ (void)F; }");
6083 
6084   // Don't break after a cast's
6085   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6086                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6087                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6088 
6089   // These are not casts.
6090   verifyFormat("void f(int *) {}");
6091   verifyFormat("f(foo)->b;");
6092   verifyFormat("f(foo).b;");
6093   verifyFormat("f(foo)(b);");
6094   verifyFormat("f(foo)[b];");
6095   verifyFormat("[](foo) { return 4; }(bar);");
6096   verifyFormat("(*funptr)(foo)[4];");
6097   verifyFormat("funptrs[4](foo)[4];");
6098   verifyFormat("void f(int *);");
6099   verifyFormat("void f(int *) = 0;");
6100   verifyFormat("void f(SmallVector<int>) {}");
6101   verifyFormat("void f(SmallVector<int>);");
6102   verifyFormat("void f(SmallVector<int>) = 0;");
6103   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6104   verifyFormat("int a = sizeof(int) * b;");
6105   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6106   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6107   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6108   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6109 
6110   // These are not casts, but at some point were confused with casts.
6111   verifyFormat("virtual void foo(int *) override;");
6112   verifyFormat("virtual void foo(char &) const;");
6113   verifyFormat("virtual void foo(int *a, char *) const;");
6114   verifyFormat("int a = sizeof(int *) + b;");
6115   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6116   verifyFormat("bool b = f(g<int>) && c;");
6117   verifyFormat("typedef void (*f)(int i) func;");
6118 
6119   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6120                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6121   // FIXME: The indentation here is not ideal.
6122   verifyFormat(
6123       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6124       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6125       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6126 }
6127 
6128 TEST_F(FormatTest, FormatsFunctionTypes) {
6129   verifyFormat("A<bool()> a;");
6130   verifyFormat("A<SomeType()> a;");
6131   verifyFormat("A<void (*)(int, std::string)> a;");
6132   verifyFormat("A<void *(int)>;");
6133   verifyFormat("void *(*a)(int *, SomeType *);");
6134   verifyFormat("int (*func)(void *);");
6135   verifyFormat("void f() { int (*func)(void *); }");
6136   verifyFormat("template <class CallbackClass>\n"
6137                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6138 
6139   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6140   verifyGoogleFormat("void* (*a)(int);");
6141   verifyGoogleFormat(
6142       "template <class CallbackClass>\n"
6143       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6144 
6145   // Other constructs can look somewhat like function types:
6146   verifyFormat("A<sizeof(*x)> a;");
6147   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6148   verifyFormat("some_var = function(*some_pointer_var)[0];");
6149   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6150   verifyFormat("int x = f(&h)();");
6151   verifyFormat("returnsFunction(&param1, &param2)(param);");
6152 }
6153 
6154 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6155   verifyFormat("A (*foo_)[6];");
6156   verifyFormat("vector<int> (*foo_)[6];");
6157 }
6158 
6159 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6160   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6161                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6162   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6163                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6164   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6165                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6166 
6167   // Different ways of ()-initializiation.
6168   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6169                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6170   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6171                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6172   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6173                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6174   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6175                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6176 }
6177 
6178 TEST_F(FormatTest, BreaksLongDeclarations) {
6179   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6180                "    AnotherNameForTheLongType;");
6181   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6182                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6183   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6184                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6185   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6186                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6187   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6188                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6189   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6190                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6191   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6192                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6193   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6194                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6195   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6196                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6197   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6198                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6199   FormatStyle Indented = getLLVMStyle();
6200   Indented.IndentWrappedFunctionNames = true;
6201   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6202                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6203                Indented);
6204   verifyFormat(
6205       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6206       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6207       Indented);
6208   verifyFormat(
6209       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6210       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6211       Indented);
6212   verifyFormat(
6213       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6214       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6215       Indented);
6216 
6217   // FIXME: Without the comment, this breaks after "(".
6218   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6219                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6220                getGoogleStyle());
6221 
6222   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6223                "                  int LoooooooooooooooooooongParam2) {}");
6224   verifyFormat(
6225       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6226       "                                   SourceLocation L, IdentifierIn *II,\n"
6227       "                                   Type *T) {}");
6228   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6229                "ReallyReaaallyLongFunctionName(\n"
6230                "    const std::string &SomeParameter,\n"
6231                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6232                "        &ReallyReallyLongParameterName,\n"
6233                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6234                "        &AnotherLongParameterName) {}");
6235   verifyFormat("template <typename A>\n"
6236                "SomeLoooooooooooooooooooooongType<\n"
6237                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6238                "Function() {}");
6239 
6240   verifyGoogleFormat(
6241       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6242       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6243   verifyGoogleFormat(
6244       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6245       "                                   SourceLocation L) {}");
6246   verifyGoogleFormat(
6247       "some_namespace::LongReturnType\n"
6248       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6249       "    int first_long_parameter, int second_parameter) {}");
6250 
6251   verifyGoogleFormat("template <typename T>\n"
6252                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6253                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6254   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6255                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6256 
6257   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6258                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6259                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6260   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6261                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6262                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6263   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6264                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6265                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6266                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6267 }
6268 
6269 TEST_F(FormatTest, FormatsArrays) {
6270   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6271                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6272   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6273                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6274   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6275                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6276   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6277                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6278   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6279                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6280   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6281                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6282                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6283   verifyFormat(
6284       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6285       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6286       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6287   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6288                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6289 
6290   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6291                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6292   verifyFormat(
6293       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6294       "                                  .aaaaaaa[0]\n"
6295       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6296   verifyFormat("a[::b::c];");
6297 
6298   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6299 
6300   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6301   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6302 }
6303 
6304 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6305   verifyFormat("(a)->b();");
6306   verifyFormat("--a;");
6307 }
6308 
6309 TEST_F(FormatTest, HandlesIncludeDirectives) {
6310   verifyFormat("#include <string>\n"
6311                "#include <a/b/c.h>\n"
6312                "#include \"a/b/string\"\n"
6313                "#include \"string.h\"\n"
6314                "#include \"string.h\"\n"
6315                "#include <a-a>\n"
6316                "#include < path with space >\n"
6317                "#include_next <test.h>"
6318                "#include \"abc.h\" // this is included for ABC\n"
6319                "#include \"some long include\" // with a comment\n"
6320                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6321                getLLVMStyleWithColumns(35));
6322   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6323   EXPECT_EQ("#include <a>", format("#include<a>"));
6324 
6325   verifyFormat("#import <string>");
6326   verifyFormat("#import <a/b/c.h>");
6327   verifyFormat("#import \"a/b/string\"");
6328   verifyFormat("#import \"string.h\"");
6329   verifyFormat("#import \"string.h\"");
6330   verifyFormat("#if __has_include(<strstream>)\n"
6331                "#include <strstream>\n"
6332                "#endif");
6333 
6334   verifyFormat("#define MY_IMPORT <a/b>");
6335 
6336   // Protocol buffer definition or missing "#".
6337   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6338                getLLVMStyleWithColumns(30));
6339 
6340   FormatStyle Style = getLLVMStyle();
6341   Style.AlwaysBreakBeforeMultilineStrings = true;
6342   Style.ColumnLimit = 0;
6343   verifyFormat("#import \"abc.h\"", Style);
6344 
6345   // But 'import' might also be a regular C++ namespace.
6346   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6347                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6348 }
6349 
6350 //===----------------------------------------------------------------------===//
6351 // Error recovery tests.
6352 //===----------------------------------------------------------------------===//
6353 
6354 TEST_F(FormatTest, IncompleteParameterLists) {
6355   FormatStyle NoBinPacking = getLLVMStyle();
6356   NoBinPacking.BinPackParameters = false;
6357   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6358                "                        double *min_x,\n"
6359                "                        double *max_x,\n"
6360                "                        double *min_y,\n"
6361                "                        double *max_y,\n"
6362                "                        double *min_z,\n"
6363                "                        double *max_z, ) {}",
6364                NoBinPacking);
6365 }
6366 
6367 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6368   verifyFormat("void f() { return; }\n42");
6369   verifyFormat("void f() {\n"
6370                "  if (0)\n"
6371                "    return;\n"
6372                "}\n"
6373                "42");
6374   verifyFormat("void f() { return }\n42");
6375   verifyFormat("void f() {\n"
6376                "  if (0)\n"
6377                "    return\n"
6378                "}\n"
6379                "42");
6380 }
6381 
6382 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6383   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6384   EXPECT_EQ("void f() {\n"
6385             "  if (a)\n"
6386             "    return\n"
6387             "}",
6388             format("void  f  (  )  {  if  ( a )  return  }"));
6389   EXPECT_EQ("namespace N {\n"
6390             "void f()\n"
6391             "}",
6392             format("namespace  N  {  void f()  }"));
6393   EXPECT_EQ("namespace N {\n"
6394             "void f() {}\n"
6395             "void g()\n"
6396             "}",
6397             format("namespace N  { void f( ) { } void g( ) }"));
6398 }
6399 
6400 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6401   verifyFormat("int aaaaaaaa =\n"
6402                "    // Overlylongcomment\n"
6403                "    b;",
6404                getLLVMStyleWithColumns(20));
6405   verifyFormat("function(\n"
6406                "    ShortArgument,\n"
6407                "    LoooooooooooongArgument);\n",
6408                getLLVMStyleWithColumns(20));
6409 }
6410 
6411 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6412   verifyFormat("public:");
6413   verifyFormat("class A {\n"
6414                "public\n"
6415                "  void f() {}\n"
6416                "};");
6417   verifyFormat("public\n"
6418                "int qwerty;");
6419   verifyFormat("public\n"
6420                "B {}");
6421   verifyFormat("public\n"
6422                "{}");
6423   verifyFormat("public\n"
6424                "B { int x; }");
6425 }
6426 
6427 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6428   verifyFormat("{");
6429   verifyFormat("#})");
6430   verifyNoCrash("(/**/[:!] ?[).");
6431 }
6432 
6433 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6434   verifyFormat("do {\n}");
6435   verifyFormat("do {\n}\n"
6436                "f();");
6437   verifyFormat("do {\n}\n"
6438                "wheeee(fun);");
6439   verifyFormat("do {\n"
6440                "  f();\n"
6441                "}");
6442 }
6443 
6444 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6445   verifyFormat("if {\n  foo;\n  foo();\n}");
6446   verifyFormat("switch {\n  foo;\n  foo();\n}");
6447   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6448   verifyFormat("while {\n  foo;\n  foo();\n}");
6449   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6450 }
6451 
6452 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6453   verifyIncompleteFormat("namespace {\n"
6454                          "class Foo { Foo (\n"
6455                          "};\n"
6456                          "} // comment");
6457 }
6458 
6459 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6460   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6461   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6462   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6463   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6464 
6465   EXPECT_EQ("{\n"
6466             "  {\n"
6467             "    breakme(\n"
6468             "        qwe);\n"
6469             "  }\n",
6470             format("{\n"
6471                    "    {\n"
6472                    " breakme(qwe);\n"
6473                    "}\n",
6474                    getLLVMStyleWithColumns(10)));
6475 }
6476 
6477 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6478   verifyFormat("int x = {\n"
6479                "    avariable,\n"
6480                "    b(alongervariable)};",
6481                getLLVMStyleWithColumns(25));
6482 }
6483 
6484 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6485   verifyFormat("return (a)(b){1, 2, 3};");
6486 }
6487 
6488 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6489   verifyFormat("vector<int> x{1, 2, 3, 4};");
6490   verifyFormat("vector<int> x{\n"
6491                "    1, 2, 3, 4,\n"
6492                "};");
6493   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6494   verifyFormat("f({1, 2});");
6495   verifyFormat("auto v = Foo{-1};");
6496   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6497   verifyFormat("Class::Class : member{1, 2, 3} {}");
6498   verifyFormat("new vector<int>{1, 2, 3};");
6499   verifyFormat("new int[3]{1, 2, 3};");
6500   verifyFormat("new int{1};");
6501   verifyFormat("return {arg1, arg2};");
6502   verifyFormat("return {arg1, SomeType{parameter}};");
6503   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6504   verifyFormat("new T{arg1, arg2};");
6505   verifyFormat("f(MyMap[{composite, key}]);");
6506   verifyFormat("class Class {\n"
6507                "  T member = {arg1, arg2};\n"
6508                "};");
6509   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6510   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6511   verifyFormat("int a = std::is_integral<int>{} + 0;");
6512 
6513   verifyFormat("int foo(int i) { return fo1{}(i); }");
6514   verifyFormat("int foo(int i) { return fo1{}(i); }");
6515   verifyFormat("auto i = decltype(x){};");
6516   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6517   verifyFormat("Node n{1, Node{1000}, //\n"
6518                "       2};");
6519   verifyFormat("Aaaa aaaaaaa{\n"
6520                "    {\n"
6521                "        aaaa,\n"
6522                "    },\n"
6523                "};");
6524   verifyFormat("class C : public D {\n"
6525                "  SomeClass SC{2};\n"
6526                "};");
6527   verifyFormat("class C : public A {\n"
6528                "  class D : public B {\n"
6529                "    void f() { int i{2}; }\n"
6530                "  };\n"
6531                "};");
6532   verifyFormat("#define A {a, a},");
6533 
6534   // Cases where distinguising braced lists and blocks is hard.
6535   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6536   verifyFormat("void f() {\n"
6537                "  return; // comment\n"
6538                "}\n"
6539                "SomeType t;");
6540   verifyFormat("void f() {\n"
6541                "  if (a) {\n"
6542                "    f();\n"
6543                "  }\n"
6544                "}\n"
6545                "SomeType t;");
6546 
6547   // In combination with BinPackArguments = false.
6548   FormatStyle NoBinPacking = getLLVMStyle();
6549   NoBinPacking.BinPackArguments = false;
6550   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6551                "                      bbbbb,\n"
6552                "                      ccccc,\n"
6553                "                      ddddd,\n"
6554                "                      eeeee,\n"
6555                "                      ffffff,\n"
6556                "                      ggggg,\n"
6557                "                      hhhhhh,\n"
6558                "                      iiiiii,\n"
6559                "                      jjjjjj,\n"
6560                "                      kkkkkk};",
6561                NoBinPacking);
6562   verifyFormat("const Aaaaaa aaaaa = {\n"
6563                "    aaaaa,\n"
6564                "    bbbbb,\n"
6565                "    ccccc,\n"
6566                "    ddddd,\n"
6567                "    eeeee,\n"
6568                "    ffffff,\n"
6569                "    ggggg,\n"
6570                "    hhhhhh,\n"
6571                "    iiiiii,\n"
6572                "    jjjjjj,\n"
6573                "    kkkkkk,\n"
6574                "};",
6575                NoBinPacking);
6576   verifyFormat(
6577       "const Aaaaaa aaaaa = {\n"
6578       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6579       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6580       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6581       "};",
6582       NoBinPacking);
6583 
6584   // FIXME: The alignment of these trailing comments might be bad. Then again,
6585   // this might be utterly useless in real code.
6586   verifyFormat("Constructor::Constructor()\n"
6587                "    : some_value{         //\n"
6588                "                 aaaaaaa, //\n"
6589                "                 bbbbbbb} {}");
6590 
6591   // In braced lists, the first comment is always assumed to belong to the
6592   // first element. Thus, it can be moved to the next or previous line as
6593   // appropriate.
6594   EXPECT_EQ("function({// First element:\n"
6595             "          1,\n"
6596             "          // Second element:\n"
6597             "          2});",
6598             format("function({\n"
6599                    "    // First element:\n"
6600                    "    1,\n"
6601                    "    // Second element:\n"
6602                    "    2});"));
6603   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6604             "    // First element:\n"
6605             "    1,\n"
6606             "    // Second element:\n"
6607             "    2};",
6608             format("std::vector<int> MyNumbers{// First element:\n"
6609                    "                           1,\n"
6610                    "                           // Second element:\n"
6611                    "                           2};",
6612                    getLLVMStyleWithColumns(30)));
6613   // A trailing comma should still lead to an enforced line break.
6614   EXPECT_EQ("vector<int> SomeVector = {\n"
6615             "    // aaa\n"
6616             "    1, 2,\n"
6617             "};",
6618             format("vector<int> SomeVector = { // aaa\n"
6619                    "    1, 2, };"));
6620 
6621   FormatStyle ExtraSpaces = getLLVMStyle();
6622   ExtraSpaces.Cpp11BracedListStyle = false;
6623   ExtraSpaces.ColumnLimit = 75;
6624   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6625   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6626   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6627   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6628   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6629   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6630   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6631   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6632   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6633   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6634   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6635   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6636   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6637   verifyFormat("class Class {\n"
6638                "  T member = { arg1, arg2 };\n"
6639                "};",
6640                ExtraSpaces);
6641   verifyFormat(
6642       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6643       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6644       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6645       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6646       ExtraSpaces);
6647   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6648   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6649                ExtraSpaces);
6650   verifyFormat(
6651       "someFunction(OtherParam,\n"
6652       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6653       "                         param1, param2,\n"
6654       "                         // comment 2\n"
6655       "                         param3, param4 });",
6656       ExtraSpaces);
6657   verifyFormat(
6658       "std::this_thread::sleep_for(\n"
6659       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6660       ExtraSpaces);
6661   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6662                "    aaaaaaa,\n"
6663                "    aaaaaaaaaa,\n"
6664                "    aaaaa,\n"
6665                "    aaaaaaaaaaaaaaa,\n"
6666                "    aaa,\n"
6667                "    aaaaaaaaaa,\n"
6668                "    a,\n"
6669                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6670                "    aaaaaaaaaaaa,\n"
6671                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6672                "    aaaaaaa,\n"
6673                "    a};");
6674   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6675 }
6676 
6677 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6678   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6679                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6680                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6681                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6682                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6683                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6684   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6685                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6686                "                 1, 22, 333, 4444, 55555, //\n"
6687                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6688                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6689   verifyFormat(
6690       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6691       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6692       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6693       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6694       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6695       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6696       "                 7777777};");
6697   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6698                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6699                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6700   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6701                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6702                "    // Separating comment.\n"
6703                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6704   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6705                "    // Leading comment\n"
6706                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6707                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6708   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6709                "                 1, 1, 1, 1};",
6710                getLLVMStyleWithColumns(39));
6711   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6712                "                 1, 1, 1, 1};",
6713                getLLVMStyleWithColumns(38));
6714   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6715                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6716                getLLVMStyleWithColumns(43));
6717   verifyFormat(
6718       "static unsigned SomeValues[10][3] = {\n"
6719       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6720       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6721   verifyFormat("static auto fields = new vector<string>{\n"
6722                "    \"aaaaaaaaaaaaa\",\n"
6723                "    \"aaaaaaaaaaaaa\",\n"
6724                "    \"aaaaaaaaaaaa\",\n"
6725                "    \"aaaaaaaaaaaaaa\",\n"
6726                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6727                "    \"aaaaaaaaaaaa\",\n"
6728                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6729                "};");
6730   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6731   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6732                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6733                "                 3, cccccccccccccccccccccc};",
6734                getLLVMStyleWithColumns(60));
6735 
6736   // Trailing commas.
6737   verifyFormat("vector<int> x = {\n"
6738                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6739                "};",
6740                getLLVMStyleWithColumns(39));
6741   verifyFormat("vector<int> x = {\n"
6742                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6743                "};",
6744                getLLVMStyleWithColumns(39));
6745   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6746                "                 1, 1, 1, 1,\n"
6747                "                 /**/ /**/};",
6748                getLLVMStyleWithColumns(39));
6749 
6750   // Trailing comment in the first line.
6751   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6752                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6753                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6754                "    11111111,   22222222,   333333333,   44444444};");
6755   // Trailing comment in the last line.
6756   verifyFormat("int aaaaa[] = {\n"
6757                "    1, 2, 3, // comment\n"
6758                "    4, 5, 6  // comment\n"
6759                "};");
6760 
6761   // With nested lists, we should either format one item per line or all nested
6762   // lists one on line.
6763   // FIXME: For some nested lists, we can do better.
6764   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6765                "        {aaaaaaaaaaaaaaaaaaa},\n"
6766                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6767                "        {aaaaaaaaaaaaaaaaa}};",
6768                getLLVMStyleWithColumns(60));
6769   verifyFormat(
6770       "SomeStruct my_struct_array = {\n"
6771       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6772       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6773       "    {aaa, aaa},\n"
6774       "    {aaa, aaa},\n"
6775       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6776       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6777       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6778 
6779   // No column layout should be used here.
6780   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6781                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6782 
6783   verifyNoCrash("a<,");
6784 
6785   // No braced initializer here.
6786   verifyFormat("void f() {\n"
6787                "  struct Dummy {};\n"
6788                "  f(v);\n"
6789                "}");
6790 
6791   // Long lists should be formatted in columns even if they are nested.
6792   verifyFormat(
6793       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6794       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6795       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6796       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6797       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6798       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6799 
6800   // Allow "single-column" layout even if that violates the column limit. There
6801   // isn't going to be a better way.
6802   verifyFormat("std::vector<int> a = {\n"
6803                "    aaaaaaaa,\n"
6804                "    aaaaaaaa,\n"
6805                "    aaaaaaaa,\n"
6806                "    aaaaaaaa,\n"
6807                "    aaaaaaaaaa,\n"
6808                "    aaaaaaaa,\n"
6809                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6810                getLLVMStyleWithColumns(30));
6811   verifyFormat("vector<int> aaaa = {\n"
6812                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6813                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6814                "    aaaaaa.aaaaaaa,\n"
6815                "    aaaaaa.aaaaaaa,\n"
6816                "    aaaaaa.aaaaaaa,\n"
6817                "    aaaaaa.aaaaaaa,\n"
6818                "};");
6819 }
6820 
6821 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6822   FormatStyle DoNotMerge = getLLVMStyle();
6823   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6824 
6825   verifyFormat("void f() { return 42; }");
6826   verifyFormat("void f() {\n"
6827                "  return 42;\n"
6828                "}",
6829                DoNotMerge);
6830   verifyFormat("void f() {\n"
6831                "  // Comment\n"
6832                "}");
6833   verifyFormat("{\n"
6834                "#error {\n"
6835                "  int a;\n"
6836                "}");
6837   verifyFormat("{\n"
6838                "  int a;\n"
6839                "#error {\n"
6840                "}");
6841   verifyFormat("void f() {} // comment");
6842   verifyFormat("void f() { int a; } // comment");
6843   verifyFormat("void f() {\n"
6844                "} // comment",
6845                DoNotMerge);
6846   verifyFormat("void f() {\n"
6847                "  int a;\n"
6848                "} // comment",
6849                DoNotMerge);
6850   verifyFormat("void f() {\n"
6851                "} // comment",
6852                getLLVMStyleWithColumns(15));
6853 
6854   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6855   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6856 
6857   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6858   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6859   verifyFormat("class C {\n"
6860                "  C()\n"
6861                "      : iiiiiiii(nullptr),\n"
6862                "        kkkkkkk(nullptr),\n"
6863                "        mmmmmmm(nullptr),\n"
6864                "        nnnnnnn(nullptr) {}\n"
6865                "};",
6866                getGoogleStyle());
6867 
6868   FormatStyle NoColumnLimit = getLLVMStyle();
6869   NoColumnLimit.ColumnLimit = 0;
6870   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6871   EXPECT_EQ("class C {\n"
6872             "  A() : b(0) {}\n"
6873             "};",
6874             format("class C{A():b(0){}};", NoColumnLimit));
6875   EXPECT_EQ("A()\n"
6876             "    : b(0) {\n"
6877             "}",
6878             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6879 
6880   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6881   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6882       FormatStyle::SFS_None;
6883   EXPECT_EQ("A()\n"
6884             "    : b(0) {\n"
6885             "}",
6886             format("A():b(0){}", DoNotMergeNoColumnLimit));
6887   EXPECT_EQ("A()\n"
6888             "    : b(0) {\n"
6889             "}",
6890             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6891 
6892   verifyFormat("#define A          \\\n"
6893                "  void f() {       \\\n"
6894                "    int i;         \\\n"
6895                "  }",
6896                getLLVMStyleWithColumns(20));
6897   verifyFormat("#define A           \\\n"
6898                "  void f() { int i; }",
6899                getLLVMStyleWithColumns(21));
6900   verifyFormat("#define A            \\\n"
6901                "  void f() {         \\\n"
6902                "    int i;           \\\n"
6903                "  }                  \\\n"
6904                "  int j;",
6905                getLLVMStyleWithColumns(22));
6906   verifyFormat("#define A             \\\n"
6907                "  void f() { int i; } \\\n"
6908                "  int j;",
6909                getLLVMStyleWithColumns(23));
6910 }
6911 
6912 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6913   FormatStyle MergeInlineOnly = getLLVMStyle();
6914   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6915   verifyFormat("class C {\n"
6916                "  int f() { return 42; }\n"
6917                "};",
6918                MergeInlineOnly);
6919   verifyFormat("int f() {\n"
6920                "  return 42;\n"
6921                "}",
6922                MergeInlineOnly);
6923 }
6924 
6925 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6926   // Elaborate type variable declarations.
6927   verifyFormat("struct foo a = {bar};\nint n;");
6928   verifyFormat("class foo a = {bar};\nint n;");
6929   verifyFormat("union foo a = {bar};\nint n;");
6930 
6931   // Elaborate types inside function definitions.
6932   verifyFormat("struct foo f() {}\nint n;");
6933   verifyFormat("class foo f() {}\nint n;");
6934   verifyFormat("union foo f() {}\nint n;");
6935 
6936   // Templates.
6937   verifyFormat("template <class X> void f() {}\nint n;");
6938   verifyFormat("template <struct X> void f() {}\nint n;");
6939   verifyFormat("template <union X> void f() {}\nint n;");
6940 
6941   // Actual definitions...
6942   verifyFormat("struct {\n} n;");
6943   verifyFormat(
6944       "template <template <class T, class Y>, class Z> class X {\n} n;");
6945   verifyFormat("union Z {\n  int n;\n} x;");
6946   verifyFormat("class MACRO Z {\n} n;");
6947   verifyFormat("class MACRO(X) Z {\n} n;");
6948   verifyFormat("class __attribute__(X) Z {\n} n;");
6949   verifyFormat("class __declspec(X) Z {\n} n;");
6950   verifyFormat("class A##B##C {\n} n;");
6951   verifyFormat("class alignas(16) Z {\n} n;");
6952   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6953   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6954 
6955   // Redefinition from nested context:
6956   verifyFormat("class A::B::C {\n} n;");
6957 
6958   // Template definitions.
6959   verifyFormat(
6960       "template <typename F>\n"
6961       "Matcher(const Matcher<F> &Other,\n"
6962       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6963       "                             !is_same<F, T>::value>::type * = 0)\n"
6964       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6965 
6966   // FIXME: This is still incorrectly handled at the formatter side.
6967   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6968   verifyFormat("int i = SomeFunction(a<b, a> b);");
6969 
6970   // FIXME:
6971   // This now gets parsed incorrectly as class definition.
6972   // verifyFormat("class A<int> f() {\n}\nint n;");
6973 
6974   // Elaborate types where incorrectly parsing the structural element would
6975   // break the indent.
6976   verifyFormat("if (true)\n"
6977                "  class X x;\n"
6978                "else\n"
6979                "  f();\n");
6980 
6981   // This is simply incomplete. Formatting is not important, but must not crash.
6982   verifyFormat("class A:");
6983 }
6984 
6985 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6986   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6987             format("#error Leave     all         white!!!!! space* alone!\n"));
6988   EXPECT_EQ(
6989       "#warning Leave     all         white!!!!! space* alone!\n",
6990       format("#warning Leave     all         white!!!!! space* alone!\n"));
6991   EXPECT_EQ("#error 1", format("  #  error   1"));
6992   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6993 }
6994 
6995 TEST_F(FormatTest, FormatHashIfExpressions) {
6996   verifyFormat("#if AAAA && BBBB");
6997   verifyFormat("#if (AAAA && BBBB)");
6998   verifyFormat("#elif (AAAA && BBBB)");
6999   // FIXME: Come up with a better indentation for #elif.
7000   verifyFormat(
7001       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7002       "    defined(BBBBBBBB)\n"
7003       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7004       "    defined(BBBBBBBB)\n"
7005       "#endif",
7006       getLLVMStyleWithColumns(65));
7007 }
7008 
7009 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7010   FormatStyle AllowsMergedIf = getGoogleStyle();
7011   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7012   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7013   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7014   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7015   EXPECT_EQ("if (true) return 42;",
7016             format("if (true)\nreturn 42;", AllowsMergedIf));
7017   FormatStyle ShortMergedIf = AllowsMergedIf;
7018   ShortMergedIf.ColumnLimit = 25;
7019   verifyFormat("#define A \\\n"
7020                "  if (true) return 42;",
7021                ShortMergedIf);
7022   verifyFormat("#define A \\\n"
7023                "  f();    \\\n"
7024                "  if (true)\n"
7025                "#define B",
7026                ShortMergedIf);
7027   verifyFormat("#define A \\\n"
7028                "  f();    \\\n"
7029                "  if (true)\n"
7030                "g();",
7031                ShortMergedIf);
7032   verifyFormat("{\n"
7033                "#ifdef A\n"
7034                "  // Comment\n"
7035                "  if (true) continue;\n"
7036                "#endif\n"
7037                "  // Comment\n"
7038                "  if (true) continue;\n"
7039                "}",
7040                ShortMergedIf);
7041   ShortMergedIf.ColumnLimit = 29;
7042   verifyFormat("#define A                   \\\n"
7043                "  if (aaaaaaaaaa) return 1; \\\n"
7044                "  return 2;",
7045                ShortMergedIf);
7046   ShortMergedIf.ColumnLimit = 28;
7047   verifyFormat("#define A         \\\n"
7048                "  if (aaaaaaaaaa) \\\n"
7049                "    return 1;     \\\n"
7050                "  return 2;",
7051                ShortMergedIf);
7052 }
7053 
7054 TEST_F(FormatTest, BlockCommentsInControlLoops) {
7055   verifyFormat("if (0) /* a comment in a strange place */ {\n"
7056                "  f();\n"
7057                "}");
7058   verifyFormat("if (0) /* a comment in a strange place */ {\n"
7059                "  f();\n"
7060                "} /* another comment */ else /* comment #3 */ {\n"
7061                "  g();\n"
7062                "}");
7063   verifyFormat("while (0) /* a comment in a strange place */ {\n"
7064                "  f();\n"
7065                "}");
7066   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
7067                "  f();\n"
7068                "}");
7069   verifyFormat("do /* a comment in a strange place */ {\n"
7070                "  f();\n"
7071                "} /* another comment */ while (0);");
7072 }
7073 
7074 TEST_F(FormatTest, BlockComments) {
7075   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
7076             format("/* *//* */  /* */\n/* *//* */  /* */"));
7077   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
7078   EXPECT_EQ("#define A /*123*/ \\\n"
7079             "  b\n"
7080             "/* */\n"
7081             "someCall(\n"
7082             "    parameter);",
7083             format("#define A /*123*/ b\n"
7084                    "/* */\n"
7085                    "someCall(parameter);",
7086                    getLLVMStyleWithColumns(15)));
7087 
7088   EXPECT_EQ("#define A\n"
7089             "/* */ someCall(\n"
7090             "    parameter);",
7091             format("#define A\n"
7092                    "/* */someCall(parameter);",
7093                    getLLVMStyleWithColumns(15)));
7094   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
7095   EXPECT_EQ("/*\n"
7096             "*\n"
7097             " * aaaaaa\n"
7098             " * aaaaaa\n"
7099             "*/",
7100             format("/*\n"
7101                    "*\n"
7102                    " * aaaaaa aaaaaa\n"
7103                    "*/",
7104                    getLLVMStyleWithColumns(10)));
7105   EXPECT_EQ("/*\n"
7106             "**\n"
7107             "* aaaaaa\n"
7108             "*aaaaaa\n"
7109             "*/",
7110             format("/*\n"
7111                    "**\n"
7112                    "* aaaaaa aaaaaa\n"
7113                    "*/",
7114                    getLLVMStyleWithColumns(10)));
7115   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7116             "    /* line 1\n"
7117             "       bbbbbbbbbbbb */\n"
7118             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7119             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7120                    "    /* line 1\n"
7121                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7122             getLLVMStyleWithColumns(50)));
7123 
7124   FormatStyle NoBinPacking = getLLVMStyle();
7125   NoBinPacking.BinPackParameters = false;
7126   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
7127             "             2, /* comment 2 */\n"
7128             "             3, /* comment 3 */\n"
7129             "             aaaa,\n"
7130             "             bbbb);",
7131             format("someFunction (1,   /* comment 1 */\n"
7132                    "                2, /* comment 2 */  \n"
7133                    "               3,   /* comment 3 */\n"
7134                    "aaaa, bbbb );",
7135                    NoBinPacking));
7136   verifyFormat(
7137       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7138       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7139   EXPECT_EQ(
7140       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
7141       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7142       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
7143       format(
7144           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
7145           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
7146           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
7147   EXPECT_EQ(
7148       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7149       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
7150       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
7151       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7152              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
7153              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
7154 
7155   verifyFormat("void f(int * /* unused */) {}");
7156 
7157   EXPECT_EQ("/*\n"
7158             " **\n"
7159             " */",
7160             format("/*\n"
7161                    " **\n"
7162                    " */"));
7163   EXPECT_EQ("/*\n"
7164             " *q\n"
7165             " */",
7166             format("/*\n"
7167                    " *q\n"
7168                    " */"));
7169   EXPECT_EQ("/*\n"
7170             " * q\n"
7171             " */",
7172             format("/*\n"
7173                    " * q\n"
7174                    " */"));
7175   EXPECT_EQ("/*\n"
7176             " **/",
7177             format("/*\n"
7178                    " **/"));
7179   EXPECT_EQ("/*\n"
7180             " ***/",
7181             format("/*\n"
7182                    " ***/"));
7183 }
7184 
7185 TEST_F(FormatTest, BlockCommentsInMacros) {
7186   EXPECT_EQ("#define A          \\\n"
7187             "  {                \\\n"
7188             "    /* one line */ \\\n"
7189             "    someCall();",
7190             format("#define A {        \\\n"
7191                    "  /* one line */   \\\n"
7192                    "  someCall();",
7193                    getLLVMStyleWithColumns(20)));
7194   EXPECT_EQ("#define A          \\\n"
7195             "  {                \\\n"
7196             "    /* previous */ \\\n"
7197             "    /* one line */ \\\n"
7198             "    someCall();",
7199             format("#define A {        \\\n"
7200                    "  /* previous */   \\\n"
7201                    "  /* one line */   \\\n"
7202                    "  someCall();",
7203                    getLLVMStyleWithColumns(20)));
7204 }
7205 
7206 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7207   EXPECT_EQ("a = {\n"
7208             "    1111 /*    */\n"
7209             "};",
7210             format("a = {1111 /*    */\n"
7211                    "};",
7212                    getLLVMStyleWithColumns(15)));
7213   EXPECT_EQ("a = {\n"
7214             "    1111 /*      */\n"
7215             "};",
7216             format("a = {1111 /*      */\n"
7217                    "};",
7218                    getLLVMStyleWithColumns(15)));
7219 
7220   // FIXME: The formatting is still wrong here.
7221   EXPECT_EQ("a = {\n"
7222             "    1111 /*      a\n"
7223             "            */\n"
7224             "};",
7225             format("a = {1111 /*      a */\n"
7226                    "};",
7227                    getLLVMStyleWithColumns(15)));
7228 }
7229 
7230 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7231   verifyFormat("{\n"
7232                "  // a\n"
7233                "  // b");
7234 }
7235 
7236 TEST_F(FormatTest, FormatStarDependingOnContext) {
7237   verifyFormat("void f(int *a);");
7238   verifyFormat("void f() { f(fint * b); }");
7239   verifyFormat("class A {\n  void f(int *a);\n};");
7240   verifyFormat("class A {\n  int *a;\n};");
7241   verifyFormat("namespace a {\n"
7242                "namespace b {\n"
7243                "class A {\n"
7244                "  void f() {}\n"
7245                "  int *a;\n"
7246                "};\n"
7247                "}\n"
7248                "}");
7249 }
7250 
7251 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7252   verifyFormat("while");
7253   verifyFormat("operator");
7254 }
7255 
7256 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7257   // This code would be painfully slow to format if we didn't skip it.
7258   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
7259                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7260                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7261                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7262                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7263                    "A(1, 1)\n"
7264                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7265                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7266                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7267                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7268                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7269                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7270                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7271                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7272                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7273                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7274   // Deeply nested part is untouched, rest is formatted.
7275   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7276             format(std::string("int    i;\n") + Code + "int    j;\n",
7277                    getLLVMStyle(), IC_ExpectIncomplete));
7278 }
7279 
7280 //===----------------------------------------------------------------------===//
7281 // Objective-C tests.
7282 //===----------------------------------------------------------------------===//
7283 
7284 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7285   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7286   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7287             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7288   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7289   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7290   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7291             format("-(NSInteger)Method3:(id)anObject;"));
7292   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7293             format("-(NSInteger)Method4:(id)anObject;"));
7294   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7295             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7296   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7297             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7298   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7299             "forAllCells:(BOOL)flag;",
7300             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7301                    "forAllCells:(BOOL)flag;"));
7302 
7303   // Very long objectiveC method declaration.
7304   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7305                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7306   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7307                "                    inRange:(NSRange)range\n"
7308                "                   outRange:(NSRange)out_range\n"
7309                "                  outRange1:(NSRange)out_range1\n"
7310                "                  outRange2:(NSRange)out_range2\n"
7311                "                  outRange3:(NSRange)out_range3\n"
7312                "                  outRange4:(NSRange)out_range4\n"
7313                "                  outRange5:(NSRange)out_range5\n"
7314                "                  outRange6:(NSRange)out_range6\n"
7315                "                  outRange7:(NSRange)out_range7\n"
7316                "                  outRange8:(NSRange)out_range8\n"
7317                "                  outRange9:(NSRange)out_range9;");
7318 
7319   // When the function name has to be wrapped.
7320   FormatStyle Style = getLLVMStyle();
7321   Style.IndentWrappedFunctionNames = false;
7322   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7323                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7324                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7325                "}",
7326                Style);
7327   Style.IndentWrappedFunctionNames = true;
7328   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7329                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7330                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7331                "}",
7332                Style);
7333 
7334   verifyFormat("- (int)sum:(vector<int>)numbers;");
7335   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7336   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7337   // protocol lists (but not for template classes):
7338   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7339 
7340   verifyFormat("- (int (*)())foo:(int (*)())f;");
7341   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7342 
7343   // If there's no return type (very rare in practice!), LLVM and Google style
7344   // agree.
7345   verifyFormat("- foo;");
7346   verifyFormat("- foo:(int)f;");
7347   verifyGoogleFormat("- foo:(int)foo;");
7348 }
7349 
7350 
7351 TEST_F(FormatTest, BreaksStringLiterals) {
7352   EXPECT_EQ("\"some text \"\n"
7353             "\"other\";",
7354             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7355   EXPECT_EQ("\"some text \"\n"
7356             "\"other\";",
7357             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7358   EXPECT_EQ(
7359       "#define A  \\\n"
7360       "  \"some \"  \\\n"
7361       "  \"text \"  \\\n"
7362       "  \"other\";",
7363       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7364   EXPECT_EQ(
7365       "#define A  \\\n"
7366       "  \"so \"    \\\n"
7367       "  \"text \"  \\\n"
7368       "  \"other\";",
7369       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7370 
7371   EXPECT_EQ("\"some text\"",
7372             format("\"some text\"", getLLVMStyleWithColumns(1)));
7373   EXPECT_EQ("\"some text\"",
7374             format("\"some text\"", getLLVMStyleWithColumns(11)));
7375   EXPECT_EQ("\"some \"\n"
7376             "\"text\"",
7377             format("\"some text\"", getLLVMStyleWithColumns(10)));
7378   EXPECT_EQ("\"some \"\n"
7379             "\"text\"",
7380             format("\"some text\"", getLLVMStyleWithColumns(7)));
7381   EXPECT_EQ("\"some\"\n"
7382             "\" tex\"\n"
7383             "\"t\"",
7384             format("\"some text\"", getLLVMStyleWithColumns(6)));
7385   EXPECT_EQ("\"some\"\n"
7386             "\" tex\"\n"
7387             "\" and\"",
7388             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7389   EXPECT_EQ("\"some\"\n"
7390             "\"/tex\"\n"
7391             "\"/and\"",
7392             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7393 
7394   EXPECT_EQ("variable =\n"
7395             "    \"long string \"\n"
7396             "    \"literal\";",
7397             format("variable = \"long string literal\";",
7398                    getLLVMStyleWithColumns(20)));
7399 
7400   EXPECT_EQ("variable = f(\n"
7401             "    \"long string \"\n"
7402             "    \"literal\",\n"
7403             "    short,\n"
7404             "    loooooooooooooooooooong);",
7405             format("variable = f(\"long string literal\", short, "
7406                    "loooooooooooooooooooong);",
7407                    getLLVMStyleWithColumns(20)));
7408 
7409   EXPECT_EQ(
7410       "f(g(\"long string \"\n"
7411       "    \"literal\"),\n"
7412       "  b);",
7413       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7414   EXPECT_EQ("f(g(\"long string \"\n"
7415             "    \"literal\",\n"
7416             "    a),\n"
7417             "  b);",
7418             format("f(g(\"long string literal\", a), b);",
7419                    getLLVMStyleWithColumns(20)));
7420   EXPECT_EQ(
7421       "f(\"one two\".split(\n"
7422       "    variable));",
7423       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7424   EXPECT_EQ("f(\"one two three four five six \"\n"
7425             "  \"seven\".split(\n"
7426             "      really_looooong_variable));",
7427             format("f(\"one two three four five six seven\"."
7428                    "split(really_looooong_variable));",
7429                    getLLVMStyleWithColumns(33)));
7430 
7431   EXPECT_EQ("f(\"some \"\n"
7432             "  \"text\",\n"
7433             "  other);",
7434             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7435 
7436   // Only break as a last resort.
7437   verifyFormat(
7438       "aaaaaaaaaaaaaaaaaaaa(\n"
7439       "    aaaaaaaaaaaaaaaaaaaa,\n"
7440       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7441 
7442   EXPECT_EQ("\"splitmea\"\n"
7443             "\"trandomp\"\n"
7444             "\"oint\"",
7445             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7446 
7447   EXPECT_EQ("\"split/\"\n"
7448             "\"pathat/\"\n"
7449             "\"slashes\"",
7450             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7451 
7452   EXPECT_EQ("\"split/\"\n"
7453             "\"pathat/\"\n"
7454             "\"slashes\"",
7455             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7456   EXPECT_EQ("\"split at \"\n"
7457             "\"spaces/at/\"\n"
7458             "\"slashes.at.any$\"\n"
7459             "\"non-alphanumeric%\"\n"
7460             "\"1111111111characte\"\n"
7461             "\"rs\"",
7462             format("\"split at "
7463                    "spaces/at/"
7464                    "slashes.at."
7465                    "any$non-"
7466                    "alphanumeric%"
7467                    "1111111111characte"
7468                    "rs\"",
7469                    getLLVMStyleWithColumns(20)));
7470 
7471   // Verify that splitting the strings understands
7472   // Style::AlwaysBreakBeforeMultilineStrings.
7473   EXPECT_EQ(
7474       "aaaaaaaaaaaa(\n"
7475       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7476       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7477       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7478              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7479              "aaaaaaaaaaaaaaaaaaaaaa\");",
7480              getGoogleStyle()));
7481   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7482             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7483             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7484                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7485                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7486                    getGoogleStyle()));
7487   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7488             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7489             format("llvm::outs() << "
7490                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7491                    "aaaaaaaaaaaaaaaaaaa\";"));
7492   EXPECT_EQ("ffff(\n"
7493             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7494             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7495             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7496                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7497                    getGoogleStyle()));
7498 
7499   FormatStyle Style = getLLVMStyleWithColumns(12);
7500   Style.BreakStringLiterals = false;
7501   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7502 
7503   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7504   AlignLeft.AlignEscapedNewlinesLeft = true;
7505   EXPECT_EQ("#define A \\\n"
7506             "  \"some \" \\\n"
7507             "  \"text \" \\\n"
7508             "  \"other\";",
7509             format("#define A \"some text other\";", AlignLeft));
7510 }
7511 
7512 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7513   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7514   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7515   EXPECT_EQ("int i = a(b());",
7516             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7517 }
7518 
7519 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7520   EXPECT_EQ(
7521       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7522       "(\n"
7523       "    \"x\t\");",
7524       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7525              "aaaaaaa("
7526              "\"x\t\");"));
7527 }
7528 
7529 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7530   EXPECT_EQ(
7531       "u8\"utf8 string \"\n"
7532       "u8\"literal\";",
7533       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7534   EXPECT_EQ(
7535       "u\"utf16 string \"\n"
7536       "u\"literal\";",
7537       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7538   EXPECT_EQ(
7539       "U\"utf32 string \"\n"
7540       "U\"literal\";",
7541       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7542   EXPECT_EQ("L\"wide string \"\n"
7543             "L\"literal\";",
7544             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7545   EXPECT_EQ("@\"NSString \"\n"
7546             "@\"literal\";",
7547             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7548 
7549   // This input makes clang-format try to split the incomplete unicode escape
7550   // sequence, which used to lead to a crasher.
7551   verifyNoCrash(
7552       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7553       getLLVMStyleWithColumns(60));
7554 }
7555 
7556 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7557   FormatStyle Style = getGoogleStyleWithColumns(15);
7558   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7559   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7560   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7561   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7562   EXPECT_EQ("u8R\"x(raw literal)x\";",
7563             format("u8R\"x(raw literal)x\";", Style));
7564 }
7565 
7566 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7567   FormatStyle Style = getLLVMStyleWithColumns(20);
7568   EXPECT_EQ(
7569       "_T(\"aaaaaaaaaaaaaa\")\n"
7570       "_T(\"aaaaaaaaaaaaaa\")\n"
7571       "_T(\"aaaaaaaaaaaa\")",
7572       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7573   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7574             "     _T(\"aaaaaa\"),\n"
7575             "  z);",
7576             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7577 
7578   // FIXME: Handle embedded spaces in one iteration.
7579   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7580   //            "_T(\"aaaaaaaaaaaaa\")\n"
7581   //            "_T(\"aaaaaaaaaaaaa\")\n"
7582   //            "_T(\"a\")",
7583   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7584   //                   getLLVMStyleWithColumns(20)));
7585   EXPECT_EQ(
7586       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7587       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7588   EXPECT_EQ("f(\n"
7589             "#if !TEST\n"
7590             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7591             "#endif\n"
7592             "    );",
7593             format("f(\n"
7594                    "#if !TEST\n"
7595                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7596                    "#endif\n"
7597                    ");"));
7598   EXPECT_EQ("f(\n"
7599             "\n"
7600             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7601             format("f(\n"
7602                    "\n"
7603                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7604 }
7605 
7606 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7607   EXPECT_EQ(
7608       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7609       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7610       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7611       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7612              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7613              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7614 }
7615 
7616 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7617   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7618             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7619   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7620             "multiline raw string literal xxxxxxxxxxxxxx\n"
7621             ")x\",\n"
7622             "              a),\n"
7623             "            b);",
7624             format("fffffffffff(g(R\"x(\n"
7625                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7626                    ")x\", a), b);",
7627                    getGoogleStyleWithColumns(20)));
7628   EXPECT_EQ("fffffffffff(\n"
7629             "    g(R\"x(qqq\n"
7630             "multiline raw string literal xxxxxxxxxxxxxx\n"
7631             ")x\",\n"
7632             "      a),\n"
7633             "    b);",
7634             format("fffffffffff(g(R\"x(qqq\n"
7635                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7636                    ")x\", a), b);",
7637                    getGoogleStyleWithColumns(20)));
7638 
7639   EXPECT_EQ("fffffffffff(R\"x(\n"
7640             "multiline raw string literal xxxxxxxxxxxxxx\n"
7641             ")x\");",
7642             format("fffffffffff(R\"x(\n"
7643                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7644                    ")x\");",
7645                    getGoogleStyleWithColumns(20)));
7646   EXPECT_EQ("fffffffffff(R\"x(\n"
7647             "multiline raw string literal xxxxxxxxxxxxxx\n"
7648             ")x\" + bbbbbb);",
7649             format("fffffffffff(R\"x(\n"
7650                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7651                    ")x\" +   bbbbbb);",
7652                    getGoogleStyleWithColumns(20)));
7653   EXPECT_EQ("fffffffffff(\n"
7654             "    R\"x(\n"
7655             "multiline raw string literal xxxxxxxxxxxxxx\n"
7656             ")x\" +\n"
7657             "    bbbbbb);",
7658             format("fffffffffff(\n"
7659                    " R\"x(\n"
7660                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7661                    ")x\" + bbbbbb);",
7662                    getGoogleStyleWithColumns(20)));
7663 }
7664 
7665 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7666   verifyFormat("string a = \"unterminated;");
7667   EXPECT_EQ("function(\"unterminated,\n"
7668             "         OtherParameter);",
7669             format("function(  \"unterminated,\n"
7670                    "    OtherParameter);"));
7671 }
7672 
7673 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7674   FormatStyle Style = getLLVMStyle();
7675   Style.Standard = FormatStyle::LS_Cpp03;
7676   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7677             format("#define x(_a) printf(\"foo\"_a);", Style));
7678 }
7679 
7680 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7681 
7682 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7683   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7684             "             \"ddeeefff\");",
7685             format("someFunction(\"aaabbbcccdddeeefff\");",
7686                    getLLVMStyleWithColumns(25)));
7687   EXPECT_EQ("someFunction1234567890(\n"
7688             "    \"aaabbbcccdddeeefff\");",
7689             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7690                    getLLVMStyleWithColumns(26)));
7691   EXPECT_EQ("someFunction1234567890(\n"
7692             "    \"aaabbbcccdddeeeff\"\n"
7693             "    \"f\");",
7694             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7695                    getLLVMStyleWithColumns(25)));
7696   EXPECT_EQ("someFunction1234567890(\n"
7697             "    \"aaabbbcccdddeeeff\"\n"
7698             "    \"f\");",
7699             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7700                    getLLVMStyleWithColumns(24)));
7701   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7702             "             \"ddde \"\n"
7703             "             \"efff\");",
7704             format("someFunction(\"aaabbbcc ddde efff\");",
7705                    getLLVMStyleWithColumns(25)));
7706   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7707             "             \"ddeeefff\");",
7708             format("someFunction(\"aaabbbccc ddeeefff\");",
7709                    getLLVMStyleWithColumns(25)));
7710   EXPECT_EQ("someFunction1234567890(\n"
7711             "    \"aaabb \"\n"
7712             "    \"cccdddeeefff\");",
7713             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7714                    getLLVMStyleWithColumns(25)));
7715   EXPECT_EQ("#define A          \\\n"
7716             "  string s =       \\\n"
7717             "      \"123456789\"  \\\n"
7718             "      \"0\";         \\\n"
7719             "  int i;",
7720             format("#define A string s = \"1234567890\"; int i;",
7721                    getLLVMStyleWithColumns(20)));
7722   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7723   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7724             "             \"dddeeeff\"\n"
7725             "             \"f\");",
7726             format("someFunction(\"aaabbbcc dddeeefff\");",
7727                    getLLVMStyleWithColumns(25)));
7728 }
7729 
7730 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7731   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7732   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7733   EXPECT_EQ("\"test\"\n"
7734             "\"\\n\"",
7735             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7736   EXPECT_EQ("\"tes\\\\\"\n"
7737             "\"n\"",
7738             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7739   EXPECT_EQ("\"\\\\\\\\\"\n"
7740             "\"\\n\"",
7741             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7742   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7743   EXPECT_EQ("\"\\uff01\"\n"
7744             "\"test\"",
7745             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7746   EXPECT_EQ("\"\\Uff01ff02\"",
7747             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7748   EXPECT_EQ("\"\\x000000000001\"\n"
7749             "\"next\"",
7750             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7751   EXPECT_EQ("\"\\x000000000001next\"",
7752             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7753   EXPECT_EQ("\"\\x000000000001\"",
7754             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7755   EXPECT_EQ("\"test\"\n"
7756             "\"\\000000\"\n"
7757             "\"000001\"",
7758             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7759   EXPECT_EQ("\"test\\000\"\n"
7760             "\"00000000\"\n"
7761             "\"1\"",
7762             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7763 }
7764 
7765 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7766   verifyFormat("void f() {\n"
7767                "  return g() {}\n"
7768                "  void h() {}");
7769   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7770                "g();\n"
7771                "}");
7772 }
7773 
7774 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7775   verifyFormat(
7776       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7777 }
7778 
7779 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7780   verifyFormat("class X {\n"
7781                "  void f() {\n"
7782                "  }\n"
7783                "};",
7784                getLLVMStyleWithColumns(12));
7785 }
7786 
7787 TEST_F(FormatTest, ConfigurableIndentWidth) {
7788   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7789   EightIndent.IndentWidth = 8;
7790   EightIndent.ContinuationIndentWidth = 8;
7791   verifyFormat("void f() {\n"
7792                "        someFunction();\n"
7793                "        if (true) {\n"
7794                "                f();\n"
7795                "        }\n"
7796                "}",
7797                EightIndent);
7798   verifyFormat("class X {\n"
7799                "        void f() {\n"
7800                "        }\n"
7801                "};",
7802                EightIndent);
7803   verifyFormat("int x[] = {\n"
7804                "        call(),\n"
7805                "        call()};",
7806                EightIndent);
7807 }
7808 
7809 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7810   verifyFormat("double\n"
7811                "f();",
7812                getLLVMStyleWithColumns(8));
7813 }
7814 
7815 TEST_F(FormatTest, ConfigurableUseOfTab) {
7816   FormatStyle Tab = getLLVMStyleWithColumns(42);
7817   Tab.IndentWidth = 8;
7818   Tab.UseTab = FormatStyle::UT_Always;
7819   Tab.AlignEscapedNewlinesLeft = true;
7820 
7821   EXPECT_EQ("if (aaaaaaaa && // q\n"
7822             "    bb)\t\t// w\n"
7823             "\t;",
7824             format("if (aaaaaaaa &&// q\n"
7825                    "bb)// w\n"
7826                    ";",
7827                    Tab));
7828   EXPECT_EQ("if (aaa && bbb) // w\n"
7829             "\t;",
7830             format("if(aaa&&bbb)// w\n"
7831                    ";",
7832                    Tab));
7833 
7834   verifyFormat("class X {\n"
7835                "\tvoid f() {\n"
7836                "\t\tsomeFunction(parameter1,\n"
7837                "\t\t\t     parameter2);\n"
7838                "\t}\n"
7839                "};",
7840                Tab);
7841   verifyFormat("#define A                        \\\n"
7842                "\tvoid f() {               \\\n"
7843                "\t\tsomeFunction(    \\\n"
7844                "\t\t    parameter1,  \\\n"
7845                "\t\t    parameter2); \\\n"
7846                "\t}",
7847                Tab);
7848 
7849   Tab.TabWidth = 4;
7850   Tab.IndentWidth = 8;
7851   verifyFormat("class TabWidth4Indent8 {\n"
7852                "\t\tvoid f() {\n"
7853                "\t\t\t\tsomeFunction(parameter1,\n"
7854                "\t\t\t\t\t\t\t parameter2);\n"
7855                "\t\t}\n"
7856                "};",
7857                Tab);
7858 
7859   Tab.TabWidth = 4;
7860   Tab.IndentWidth = 4;
7861   verifyFormat("class TabWidth4Indent4 {\n"
7862                "\tvoid f() {\n"
7863                "\t\tsomeFunction(parameter1,\n"
7864                "\t\t\t\t\t parameter2);\n"
7865                "\t}\n"
7866                "};",
7867                Tab);
7868 
7869   Tab.TabWidth = 8;
7870   Tab.IndentWidth = 4;
7871   verifyFormat("class TabWidth8Indent4 {\n"
7872                "    void f() {\n"
7873                "\tsomeFunction(parameter1,\n"
7874                "\t\t     parameter2);\n"
7875                "    }\n"
7876                "};",
7877                Tab);
7878 
7879   Tab.TabWidth = 8;
7880   Tab.IndentWidth = 8;
7881   EXPECT_EQ("/*\n"
7882             "\t      a\t\tcomment\n"
7883             "\t      in multiple lines\n"
7884             "       */",
7885             format("   /*\t \t \n"
7886                    " \t \t a\t\tcomment\t \t\n"
7887                    " \t \t in multiple lines\t\n"
7888                    " \t  */",
7889                    Tab));
7890 
7891   Tab.UseTab = FormatStyle::UT_ForIndentation;
7892   verifyFormat("{\n"
7893                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7894                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7895                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7896                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7897                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7898                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7899                "};",
7900                Tab);
7901   verifyFormat("enum AA {\n"
7902                "\ta1, // Force multiple lines\n"
7903                "\ta2,\n"
7904                "\ta3\n"
7905                "};",
7906                Tab);
7907   EXPECT_EQ("if (aaaaaaaa && // q\n"
7908             "    bb)         // w\n"
7909             "\t;",
7910             format("if (aaaaaaaa &&// q\n"
7911                    "bb)// w\n"
7912                    ";",
7913                    Tab));
7914   verifyFormat("class X {\n"
7915                "\tvoid f() {\n"
7916                "\t\tsomeFunction(parameter1,\n"
7917                "\t\t             parameter2);\n"
7918                "\t}\n"
7919                "};",
7920                Tab);
7921   verifyFormat("{\n"
7922                "\tQ(\n"
7923                "\t    {\n"
7924                "\t\t    int a;\n"
7925                "\t\t    someFunction(aaaaaaaa,\n"
7926                "\t\t                 bbbbbbb);\n"
7927                "\t    },\n"
7928                "\t    p);\n"
7929                "}",
7930                Tab);
7931   EXPECT_EQ("{\n"
7932             "\t/* aaaa\n"
7933             "\t   bbbb */\n"
7934             "}",
7935             format("{\n"
7936                    "/* aaaa\n"
7937                    "   bbbb */\n"
7938                    "}",
7939                    Tab));
7940   EXPECT_EQ("{\n"
7941             "\t/*\n"
7942             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7943             "\t  bbbbbbbbbbbbb\n"
7944             "\t*/\n"
7945             "}",
7946             format("{\n"
7947                    "/*\n"
7948                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7949                    "*/\n"
7950                    "}",
7951                    Tab));
7952   EXPECT_EQ("{\n"
7953             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7954             "\t// bbbbbbbbbbbbb\n"
7955             "}",
7956             format("{\n"
7957                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7958                    "}",
7959                    Tab));
7960   EXPECT_EQ("{\n"
7961             "\t/*\n"
7962             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7963             "\t  bbbbbbbbbbbbb\n"
7964             "\t*/\n"
7965             "}",
7966             format("{\n"
7967                    "\t/*\n"
7968                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7969                    "\t*/\n"
7970                    "}",
7971                    Tab));
7972   EXPECT_EQ("{\n"
7973             "\t/*\n"
7974             "\n"
7975             "\t*/\n"
7976             "}",
7977             format("{\n"
7978                    "\t/*\n"
7979                    "\n"
7980                    "\t*/\n"
7981                    "}",
7982                    Tab));
7983   EXPECT_EQ("{\n"
7984             "\t/*\n"
7985             " asdf\n"
7986             "\t*/\n"
7987             "}",
7988             format("{\n"
7989                    "\t/*\n"
7990                    " asdf\n"
7991                    "\t*/\n"
7992                    "}",
7993                    Tab));
7994 
7995   Tab.UseTab = FormatStyle::UT_Never;
7996   EXPECT_EQ("/*\n"
7997             "              a\t\tcomment\n"
7998             "              in multiple lines\n"
7999             "       */",
8000             format("   /*\t \t \n"
8001                    " \t \t a\t\tcomment\t \t\n"
8002                    " \t \t in multiple lines\t\n"
8003                    " \t  */",
8004                    Tab));
8005   EXPECT_EQ("/* some\n"
8006             "   comment */",
8007             format(" \t \t /* some\n"
8008                    " \t \t    comment */",
8009                    Tab));
8010   EXPECT_EQ("int a; /* some\n"
8011             "   comment */",
8012             format(" \t \t int a; /* some\n"
8013                    " \t \t    comment */",
8014                    Tab));
8015 
8016   EXPECT_EQ("int a; /* some\n"
8017             "comment */",
8018             format(" \t \t int\ta; /* some\n"
8019                    " \t \t    comment */",
8020                    Tab));
8021   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8022             "    comment */",
8023             format(" \t \t f(\"\t\t\"); /* some\n"
8024                    " \t \t    comment */",
8025                    Tab));
8026   EXPECT_EQ("{\n"
8027             "  /*\n"
8028             "   * Comment\n"
8029             "   */\n"
8030             "  int i;\n"
8031             "}",
8032             format("{\n"
8033                    "\t/*\n"
8034                    "\t * Comment\n"
8035                    "\t */\n"
8036                    "\t int i;\n"
8037                    "}"));
8038 
8039   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8040   Tab.TabWidth = 8;
8041   Tab.IndentWidth = 8;
8042   EXPECT_EQ("if (aaaaaaaa && // q\n"
8043             "    bb)         // w\n"
8044             "\t;",
8045             format("if (aaaaaaaa &&// q\n"
8046                    "bb)// w\n"
8047                    ";",
8048                    Tab));
8049   EXPECT_EQ("if (aaa && bbb) // w\n"
8050             "\t;",
8051             format("if(aaa&&bbb)// w\n"
8052                    ";",
8053                    Tab));
8054   verifyFormat("class X {\n"
8055                "\tvoid f() {\n"
8056                "\t\tsomeFunction(parameter1,\n"
8057                "\t\t\t     parameter2);\n"
8058                "\t}\n"
8059                "};",
8060                Tab);
8061   verifyFormat("#define A                        \\\n"
8062                "\tvoid f() {               \\\n"
8063                "\t\tsomeFunction(    \\\n"
8064                "\t\t    parameter1,  \\\n"
8065                "\t\t    parameter2); \\\n"
8066                "\t}",
8067                Tab);
8068   Tab.TabWidth = 4;
8069   Tab.IndentWidth = 8;
8070   verifyFormat("class TabWidth4Indent8 {\n"
8071                "\t\tvoid f() {\n"
8072                "\t\t\t\tsomeFunction(parameter1,\n"
8073                "\t\t\t\t\t\t\t parameter2);\n"
8074                "\t\t}\n"
8075                "};",
8076                Tab);
8077   Tab.TabWidth = 4;
8078   Tab.IndentWidth = 4;
8079   verifyFormat("class TabWidth4Indent4 {\n"
8080                "\tvoid f() {\n"
8081                "\t\tsomeFunction(parameter1,\n"
8082                "\t\t\t\t\t parameter2);\n"
8083                "\t}\n"
8084                "};",
8085                Tab);
8086   Tab.TabWidth = 8;
8087   Tab.IndentWidth = 4;
8088   verifyFormat("class TabWidth8Indent4 {\n"
8089                "    void f() {\n"
8090                "\tsomeFunction(parameter1,\n"
8091                "\t\t     parameter2);\n"
8092                "    }\n"
8093                "};",
8094                Tab);
8095   Tab.TabWidth = 8;
8096   Tab.IndentWidth = 8;
8097   EXPECT_EQ("/*\n"
8098             "\t      a\t\tcomment\n"
8099             "\t      in multiple lines\n"
8100             "       */",
8101             format("   /*\t \t \n"
8102                    " \t \t a\t\tcomment\t \t\n"
8103                    " \t \t in multiple lines\t\n"
8104                    " \t  */",
8105                    Tab));
8106   verifyFormat("{\n"
8107                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8108                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8109                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8110                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8111                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8112                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8113                "};",
8114                Tab);
8115   verifyFormat("enum AA {\n"
8116                "\ta1, // Force multiple lines\n"
8117                "\ta2,\n"
8118                "\ta3\n"
8119                "};",
8120                Tab);
8121   EXPECT_EQ("if (aaaaaaaa && // q\n"
8122             "    bb)         // w\n"
8123             "\t;",
8124             format("if (aaaaaaaa &&// q\n"
8125                    "bb)// w\n"
8126                    ";",
8127                    Tab));
8128   verifyFormat("class X {\n"
8129                "\tvoid f() {\n"
8130                "\t\tsomeFunction(parameter1,\n"
8131                "\t\t\t     parameter2);\n"
8132                "\t}\n"
8133                "};",
8134                Tab);
8135   verifyFormat("{\n"
8136                "\tQ(\n"
8137                "\t    {\n"
8138                "\t\t    int a;\n"
8139                "\t\t    someFunction(aaaaaaaa,\n"
8140                "\t\t\t\t bbbbbbb);\n"
8141                "\t    },\n"
8142                "\t    p);\n"
8143                "}",
8144                Tab);
8145   EXPECT_EQ("{\n"
8146             "\t/* aaaa\n"
8147             "\t   bbbb */\n"
8148             "}",
8149             format("{\n"
8150                    "/* aaaa\n"
8151                    "   bbbb */\n"
8152                    "}",
8153                    Tab));
8154   EXPECT_EQ("{\n"
8155             "\t/*\n"
8156             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8157             "\t  bbbbbbbbbbbbb\n"
8158             "\t*/\n"
8159             "}",
8160             format("{\n"
8161                    "/*\n"
8162                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8163                    "*/\n"
8164                    "}",
8165                    Tab));
8166   EXPECT_EQ("{\n"
8167             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8168             "\t// bbbbbbbbbbbbb\n"
8169             "}",
8170             format("{\n"
8171                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8172                    "}",
8173                    Tab));
8174   EXPECT_EQ("{\n"
8175             "\t/*\n"
8176             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8177             "\t  bbbbbbbbbbbbb\n"
8178             "\t*/\n"
8179             "}",
8180             format("{\n"
8181                    "\t/*\n"
8182                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8183                    "\t*/\n"
8184                    "}",
8185                    Tab));
8186   EXPECT_EQ("{\n"
8187             "\t/*\n"
8188             "\n"
8189             "\t*/\n"
8190             "}",
8191             format("{\n"
8192                    "\t/*\n"
8193                    "\n"
8194                    "\t*/\n"
8195                    "}",
8196                    Tab));
8197   EXPECT_EQ("{\n"
8198             "\t/*\n"
8199             " asdf\n"
8200             "\t*/\n"
8201             "}",
8202             format("{\n"
8203                    "\t/*\n"
8204                    " asdf\n"
8205                    "\t*/\n"
8206                    "}",
8207                    Tab));
8208   EXPECT_EQ("/*\n"
8209             "\t      a\t\tcomment\n"
8210             "\t      in multiple lines\n"
8211             "       */",
8212             format("   /*\t \t \n"
8213                    " \t \t a\t\tcomment\t \t\n"
8214                    " \t \t in multiple lines\t\n"
8215                    " \t  */",
8216                    Tab));
8217   EXPECT_EQ("/* some\n"
8218             "   comment */",
8219             format(" \t \t /* some\n"
8220                    " \t \t    comment */",
8221                    Tab));
8222   EXPECT_EQ("int a; /* some\n"
8223             "   comment */",
8224             format(" \t \t int a; /* some\n"
8225                    " \t \t    comment */",
8226                    Tab));
8227   EXPECT_EQ("int a; /* some\n"
8228             "comment */",
8229             format(" \t \t int\ta; /* some\n"
8230                    " \t \t    comment */",
8231                    Tab));
8232   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8233             "    comment */",
8234             format(" \t \t f(\"\t\t\"); /* some\n"
8235                    " \t \t    comment */",
8236                    Tab));
8237   EXPECT_EQ("{\n"
8238             "  /*\n"
8239             "   * Comment\n"
8240             "   */\n"
8241             "  int i;\n"
8242             "}",
8243             format("{\n"
8244                    "\t/*\n"
8245                    "\t * Comment\n"
8246                    "\t */\n"
8247                    "\t int i;\n"
8248                    "}"));
8249   Tab.AlignConsecutiveAssignments = true;
8250   Tab.AlignConsecutiveDeclarations = true;
8251   Tab.TabWidth = 4;
8252   Tab.IndentWidth = 4;
8253   verifyFormat("class Assign {\n"
8254                "\tvoid f() {\n"
8255                "\t\tint         x      = 123;\n"
8256                "\t\tint         random = 4;\n"
8257                "\t\tstd::string alphabet =\n"
8258                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8259                "\t}\n"
8260                "};",
8261                Tab);
8262 }
8263 
8264 TEST_F(FormatTest, CalculatesOriginalColumn) {
8265   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8266             "q\"; /* some\n"
8267             "       comment */",
8268             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8269                    "q\"; /* some\n"
8270                    "       comment */",
8271                    getLLVMStyle()));
8272   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8273             "/* some\n"
8274             "   comment */",
8275             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8276                    " /* some\n"
8277                    "    comment */",
8278                    getLLVMStyle()));
8279   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8280             "qqq\n"
8281             "/* some\n"
8282             "   comment */",
8283             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8284                    "qqq\n"
8285                    " /* some\n"
8286                    "    comment */",
8287                    getLLVMStyle()));
8288   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8289             "wwww; /* some\n"
8290             "         comment */",
8291             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8292                    "wwww; /* some\n"
8293                    "         comment */",
8294                    getLLVMStyle()));
8295 }
8296 
8297 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8298   FormatStyle NoSpace = getLLVMStyle();
8299   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8300 
8301   verifyFormat("while(true)\n"
8302                "  continue;",
8303                NoSpace);
8304   verifyFormat("for(;;)\n"
8305                "  continue;",
8306                NoSpace);
8307   verifyFormat("if(true)\n"
8308                "  f();\n"
8309                "else if(true)\n"
8310                "  f();",
8311                NoSpace);
8312   verifyFormat("do {\n"
8313                "  do_something();\n"
8314                "} while(something());",
8315                NoSpace);
8316   verifyFormat("switch(x) {\n"
8317                "default:\n"
8318                "  break;\n"
8319                "}",
8320                NoSpace);
8321   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8322   verifyFormat("size_t x = sizeof(x);", NoSpace);
8323   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8324   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8325   verifyFormat("alignas(128) char a[128];", NoSpace);
8326   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8327   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8328   verifyFormat("int f() throw(Deprecated);", NoSpace);
8329   verifyFormat("typedef void (*cb)(int);", NoSpace);
8330   verifyFormat("T A::operator()();", NoSpace);
8331   verifyFormat("X A::operator++(T);", NoSpace);
8332 
8333   FormatStyle Space = getLLVMStyle();
8334   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8335 
8336   verifyFormat("int f ();", Space);
8337   verifyFormat("void f (int a, T b) {\n"
8338                "  while (true)\n"
8339                "    continue;\n"
8340                "}",
8341                Space);
8342   verifyFormat("if (true)\n"
8343                "  f ();\n"
8344                "else if (true)\n"
8345                "  f ();",
8346                Space);
8347   verifyFormat("do {\n"
8348                "  do_something ();\n"
8349                "} while (something ());",
8350                Space);
8351   verifyFormat("switch (x) {\n"
8352                "default:\n"
8353                "  break;\n"
8354                "}",
8355                Space);
8356   verifyFormat("A::A () : a (1) {}", Space);
8357   verifyFormat("void f () __attribute__ ((asdf));", Space);
8358   verifyFormat("*(&a + 1);\n"
8359                "&((&a)[1]);\n"
8360                "a[(b + c) * d];\n"
8361                "(((a + 1) * 2) + 3) * 4;",
8362                Space);
8363   verifyFormat("#define A(x) x", Space);
8364   verifyFormat("#define A (x) x", Space);
8365   verifyFormat("#if defined(x)\n"
8366                "#endif",
8367                Space);
8368   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8369   verifyFormat("size_t x = sizeof (x);", Space);
8370   verifyFormat("auto f (int x) -> decltype (x);", Space);
8371   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8372   verifyFormat("alignas (128) char a[128];", Space);
8373   verifyFormat("size_t x = alignof (MyType);", Space);
8374   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8375   verifyFormat("int f () throw (Deprecated);", Space);
8376   verifyFormat("typedef void (*cb) (int);", Space);
8377   verifyFormat("T A::operator() ();", Space);
8378   verifyFormat("X A::operator++ (T);", Space);
8379 }
8380 
8381 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8382   FormatStyle Spaces = getLLVMStyle();
8383 
8384   Spaces.SpacesInParentheses = true;
8385   verifyFormat("call( x, y, z );", Spaces);
8386   verifyFormat("call();", Spaces);
8387   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8388   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8389                Spaces);
8390   verifyFormat("while ( (bool)1 )\n"
8391                "  continue;",
8392                Spaces);
8393   verifyFormat("for ( ;; )\n"
8394                "  continue;",
8395                Spaces);
8396   verifyFormat("if ( true )\n"
8397                "  f();\n"
8398                "else if ( true )\n"
8399                "  f();",
8400                Spaces);
8401   verifyFormat("do {\n"
8402                "  do_something( (int)i );\n"
8403                "} while ( something() );",
8404                Spaces);
8405   verifyFormat("switch ( x ) {\n"
8406                "default:\n"
8407                "  break;\n"
8408                "}",
8409                Spaces);
8410 
8411   Spaces.SpacesInParentheses = false;
8412   Spaces.SpacesInCStyleCastParentheses = true;
8413   verifyFormat("Type *A = ( Type * )P;", Spaces);
8414   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8415   verifyFormat("x = ( int32 )y;", Spaces);
8416   verifyFormat("int a = ( int )(2.0f);", Spaces);
8417   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8418   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8419   verifyFormat("#define x (( int )-1)", Spaces);
8420 
8421   // Run the first set of tests again with:
8422   Spaces.SpacesInParentheses = false;
8423   Spaces.SpaceInEmptyParentheses = true;
8424   Spaces.SpacesInCStyleCastParentheses = true;
8425   verifyFormat("call(x, y, z);", Spaces);
8426   verifyFormat("call( );", Spaces);
8427   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8428   verifyFormat("while (( bool )1)\n"
8429                "  continue;",
8430                Spaces);
8431   verifyFormat("for (;;)\n"
8432                "  continue;",
8433                Spaces);
8434   verifyFormat("if (true)\n"
8435                "  f( );\n"
8436                "else if (true)\n"
8437                "  f( );",
8438                Spaces);
8439   verifyFormat("do {\n"
8440                "  do_something(( int )i);\n"
8441                "} while (something( ));",
8442                Spaces);
8443   verifyFormat("switch (x) {\n"
8444                "default:\n"
8445                "  break;\n"
8446                "}",
8447                Spaces);
8448 
8449   // Run the first set of tests again with:
8450   Spaces.SpaceAfterCStyleCast = true;
8451   verifyFormat("call(x, y, z);", Spaces);
8452   verifyFormat("call( );", Spaces);
8453   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8454   verifyFormat("while (( bool ) 1)\n"
8455                "  continue;",
8456                Spaces);
8457   verifyFormat("for (;;)\n"
8458                "  continue;",
8459                Spaces);
8460   verifyFormat("if (true)\n"
8461                "  f( );\n"
8462                "else if (true)\n"
8463                "  f( );",
8464                Spaces);
8465   verifyFormat("do {\n"
8466                "  do_something(( int ) i);\n"
8467                "} while (something( ));",
8468                Spaces);
8469   verifyFormat("switch (x) {\n"
8470                "default:\n"
8471                "  break;\n"
8472                "}",
8473                Spaces);
8474 
8475   // Run subset of tests again with:
8476   Spaces.SpacesInCStyleCastParentheses = false;
8477   Spaces.SpaceAfterCStyleCast = true;
8478   verifyFormat("while ((bool) 1)\n"
8479                "  continue;",
8480                Spaces);
8481   verifyFormat("do {\n"
8482                "  do_something((int) i);\n"
8483                "} while (something( ));",
8484                Spaces);
8485 }
8486 
8487 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8488   verifyFormat("int a[5];");
8489   verifyFormat("a[3] += 42;");
8490 
8491   FormatStyle Spaces = getLLVMStyle();
8492   Spaces.SpacesInSquareBrackets = true;
8493   // Lambdas unchanged.
8494   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8495   verifyFormat("return [i, args...] {};", Spaces);
8496 
8497   // Not lambdas.
8498   verifyFormat("int a[ 5 ];", Spaces);
8499   verifyFormat("a[ 3 ] += 42;", Spaces);
8500   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8501   verifyFormat("double &operator[](int i) { return 0; }\n"
8502                "int i;",
8503                Spaces);
8504   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8505   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8506   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8507 }
8508 
8509 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8510   verifyFormat("int a = 5;");
8511   verifyFormat("a += 42;");
8512   verifyFormat("a or_eq 8;");
8513 
8514   FormatStyle Spaces = getLLVMStyle();
8515   Spaces.SpaceBeforeAssignmentOperators = false;
8516   verifyFormat("int a= 5;", Spaces);
8517   verifyFormat("a+= 42;", Spaces);
8518   verifyFormat("a or_eq 8;", Spaces);
8519 }
8520 
8521 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8522   FormatStyle Alignment = getLLVMStyle();
8523   Alignment.AlignConsecutiveAssignments = false;
8524   verifyFormat("int a = 5;\n"
8525                "int oneTwoThree = 123;",
8526                Alignment);
8527   verifyFormat("int a = 5;\n"
8528                "int oneTwoThree = 123;",
8529                Alignment);
8530 
8531   Alignment.AlignConsecutiveAssignments = true;
8532   verifyFormat("int a           = 5;\n"
8533                "int oneTwoThree = 123;",
8534                Alignment);
8535   verifyFormat("int a           = method();\n"
8536                "int oneTwoThree = 133;",
8537                Alignment);
8538   verifyFormat("a &= 5;\n"
8539                "bcd *= 5;\n"
8540                "ghtyf += 5;\n"
8541                "dvfvdb -= 5;\n"
8542                "a /= 5;\n"
8543                "vdsvsv %= 5;\n"
8544                "sfdbddfbdfbb ^= 5;\n"
8545                "dvsdsv |= 5;\n"
8546                "int dsvvdvsdvvv = 123;",
8547                Alignment);
8548   verifyFormat("int i = 1, j = 10;\n"
8549                "something = 2000;",
8550                Alignment);
8551   verifyFormat("something = 2000;\n"
8552                "int i = 1, j = 10;\n",
8553                Alignment);
8554   verifyFormat("something = 2000;\n"
8555                "another   = 911;\n"
8556                "int i = 1, j = 10;\n"
8557                "oneMore = 1;\n"
8558                "i       = 2;",
8559                Alignment);
8560   verifyFormat("int a   = 5;\n"
8561                "int one = 1;\n"
8562                "method();\n"
8563                "int oneTwoThree = 123;\n"
8564                "int oneTwo      = 12;",
8565                Alignment);
8566   verifyFormat("int oneTwoThree = 123;\n"
8567                "int oneTwo      = 12;\n"
8568                "method();\n",
8569                Alignment);
8570   verifyFormat("int oneTwoThree = 123; // comment\n"
8571                "int oneTwo      = 12;  // comment",
8572                Alignment);
8573   EXPECT_EQ("int a = 5;\n"
8574             "\n"
8575             "int oneTwoThree = 123;",
8576             format("int a       = 5;\n"
8577                    "\n"
8578                    "int oneTwoThree= 123;",
8579                    Alignment));
8580   EXPECT_EQ("int a   = 5;\n"
8581             "int one = 1;\n"
8582             "\n"
8583             "int oneTwoThree = 123;",
8584             format("int a = 5;\n"
8585                    "int one = 1;\n"
8586                    "\n"
8587                    "int oneTwoThree = 123;",
8588                    Alignment));
8589   EXPECT_EQ("int a   = 5;\n"
8590             "int one = 1;\n"
8591             "\n"
8592             "int oneTwoThree = 123;\n"
8593             "int oneTwo      = 12;",
8594             format("int a = 5;\n"
8595                    "int one = 1;\n"
8596                    "\n"
8597                    "int oneTwoThree = 123;\n"
8598                    "int oneTwo = 12;",
8599                    Alignment));
8600   Alignment.AlignEscapedNewlinesLeft = true;
8601   verifyFormat("#define A               \\\n"
8602                "  int aaaa       = 12;  \\\n"
8603                "  int b          = 23;  \\\n"
8604                "  int ccc        = 234; \\\n"
8605                "  int dddddddddd = 2345;",
8606                Alignment);
8607   Alignment.AlignEscapedNewlinesLeft = false;
8608   verifyFormat("#define A                                                      "
8609                "                \\\n"
8610                "  int aaaa       = 12;                                         "
8611                "                \\\n"
8612                "  int b          = 23;                                         "
8613                "                \\\n"
8614                "  int ccc        = 234;                                        "
8615                "                \\\n"
8616                "  int dddddddddd = 2345;",
8617                Alignment);
8618   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8619                "k = 4, int l = 5,\n"
8620                "                  int m = 6) {\n"
8621                "  int j      = 10;\n"
8622                "  otherThing = 1;\n"
8623                "}",
8624                Alignment);
8625   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8626                "  int i   = 1;\n"
8627                "  int j   = 2;\n"
8628                "  int big = 10000;\n"
8629                "}",
8630                Alignment);
8631   verifyFormat("class C {\n"
8632                "public:\n"
8633                "  int i            = 1;\n"
8634                "  virtual void f() = 0;\n"
8635                "};",
8636                Alignment);
8637   verifyFormat("int i = 1;\n"
8638                "if (SomeType t = getSomething()) {\n"
8639                "}\n"
8640                "int j   = 2;\n"
8641                "int big = 10000;",
8642                Alignment);
8643   verifyFormat("int j = 7;\n"
8644                "for (int k = 0; k < N; ++k) {\n"
8645                "}\n"
8646                "int j   = 2;\n"
8647                "int big = 10000;\n"
8648                "}",
8649                Alignment);
8650   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8651   verifyFormat("int i = 1;\n"
8652                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8653                "    = someLooooooooooooooooongFunction();\n"
8654                "int j = 2;",
8655                Alignment);
8656   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8657   verifyFormat("int i = 1;\n"
8658                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8659                "    someLooooooooooooooooongFunction();\n"
8660                "int j = 2;",
8661                Alignment);
8662 
8663   verifyFormat("auto lambda = []() {\n"
8664                "  auto i = 0;\n"
8665                "  return 0;\n"
8666                "};\n"
8667                "int i  = 0;\n"
8668                "auto v = type{\n"
8669                "    i = 1,   //\n"
8670                "    (i = 2), //\n"
8671                "    i = 3    //\n"
8672                "};",
8673                Alignment);
8674 
8675   // FIXME: Should align all three assignments
8676   verifyFormat(
8677       "int i      = 1;\n"
8678       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8679       "                          loooooooooooooooooooooongParameterB);\n"
8680       "int j = 2;",
8681       Alignment);
8682 
8683   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8684                "          typename B   = very_long_type_name_1,\n"
8685                "          typename T_2 = very_long_type_name_2>\n"
8686                "auto foo() {}\n",
8687                Alignment);
8688   verifyFormat("int a, b = 1;\n"
8689                "int c  = 2;\n"
8690                "int dd = 3;\n",
8691                Alignment);
8692   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8693                "float b[1][] = {{3.f}};\n",
8694                Alignment);
8695 }
8696 
8697 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8698   FormatStyle Alignment = getLLVMStyle();
8699   Alignment.AlignConsecutiveDeclarations = false;
8700   verifyFormat("float const a = 5;\n"
8701                "int oneTwoThree = 123;",
8702                Alignment);
8703   verifyFormat("int a = 5;\n"
8704                "float const oneTwoThree = 123;",
8705                Alignment);
8706 
8707   Alignment.AlignConsecutiveDeclarations = true;
8708   verifyFormat("float const a = 5;\n"
8709                "int         oneTwoThree = 123;",
8710                Alignment);
8711   verifyFormat("int         a = method();\n"
8712                "float const oneTwoThree = 133;",
8713                Alignment);
8714   verifyFormat("int i = 1, j = 10;\n"
8715                "something = 2000;",
8716                Alignment);
8717   verifyFormat("something = 2000;\n"
8718                "int i = 1, j = 10;\n",
8719                Alignment);
8720   verifyFormat("float      something = 2000;\n"
8721                "double     another = 911;\n"
8722                "int        i = 1, j = 10;\n"
8723                "const int *oneMore = 1;\n"
8724                "unsigned   i = 2;",
8725                Alignment);
8726   verifyFormat("float a = 5;\n"
8727                "int   one = 1;\n"
8728                "method();\n"
8729                "const double       oneTwoThree = 123;\n"
8730                "const unsigned int oneTwo = 12;",
8731                Alignment);
8732   verifyFormat("int      oneTwoThree{0}; // comment\n"
8733                "unsigned oneTwo;         // comment",
8734                Alignment);
8735   EXPECT_EQ("float const a = 5;\n"
8736             "\n"
8737             "int oneTwoThree = 123;",
8738             format("float const   a = 5;\n"
8739                    "\n"
8740                    "int           oneTwoThree= 123;",
8741                    Alignment));
8742   EXPECT_EQ("float a = 5;\n"
8743             "int   one = 1;\n"
8744             "\n"
8745             "unsigned oneTwoThree = 123;",
8746             format("float    a = 5;\n"
8747                    "int      one = 1;\n"
8748                    "\n"
8749                    "unsigned oneTwoThree = 123;",
8750                    Alignment));
8751   EXPECT_EQ("float a = 5;\n"
8752             "int   one = 1;\n"
8753             "\n"
8754             "unsigned oneTwoThree = 123;\n"
8755             "int      oneTwo = 12;",
8756             format("float    a = 5;\n"
8757                    "int one = 1;\n"
8758                    "\n"
8759                    "unsigned oneTwoThree = 123;\n"
8760                    "int oneTwo = 12;",
8761                    Alignment));
8762   Alignment.AlignConsecutiveAssignments = true;
8763   verifyFormat("float      something = 2000;\n"
8764                "double     another   = 911;\n"
8765                "int        i = 1, j = 10;\n"
8766                "const int *oneMore = 1;\n"
8767                "unsigned   i       = 2;",
8768                Alignment);
8769   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8770                "unsigned oneTwo      = 0;   // comment",
8771                Alignment);
8772   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8773             "  int const i   = 1;\n"
8774             "  int *     j   = 2;\n"
8775             "  int       big = 10000;\n"
8776             "\n"
8777             "  unsigned oneTwoThree = 123;\n"
8778             "  int      oneTwo      = 12;\n"
8779             "  method();\n"
8780             "  float k  = 2;\n"
8781             "  int   ll = 10000;\n"
8782             "}",
8783             format("void SomeFunction(int parameter= 0) {\n"
8784                    " int const  i= 1;\n"
8785                    "  int *j=2;\n"
8786                    " int big  =  10000;\n"
8787                    "\n"
8788                    "unsigned oneTwoThree  =123;\n"
8789                    "int oneTwo = 12;\n"
8790                    "  method();\n"
8791                    "float k= 2;\n"
8792                    "int ll=10000;\n"
8793                    "}",
8794                    Alignment));
8795   Alignment.AlignConsecutiveAssignments = false;
8796   Alignment.AlignEscapedNewlinesLeft = true;
8797   verifyFormat("#define A              \\\n"
8798                "  int       aaaa = 12; \\\n"
8799                "  float     b = 23;    \\\n"
8800                "  const int ccc = 234; \\\n"
8801                "  unsigned  dddddddddd = 2345;",
8802                Alignment);
8803   Alignment.AlignEscapedNewlinesLeft = false;
8804   Alignment.ColumnLimit = 30;
8805   verifyFormat("#define A                    \\\n"
8806                "  int       aaaa = 12;       \\\n"
8807                "  float     b = 23;          \\\n"
8808                "  const int ccc = 234;       \\\n"
8809                "  int       dddddddddd = 2345;",
8810                Alignment);
8811   Alignment.ColumnLimit = 80;
8812   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8813                "k = 4, int l = 5,\n"
8814                "                  int m = 6) {\n"
8815                "  const int j = 10;\n"
8816                "  otherThing = 1;\n"
8817                "}",
8818                Alignment);
8819   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8820                "  int const i = 1;\n"
8821                "  int *     j = 2;\n"
8822                "  int       big = 10000;\n"
8823                "}",
8824                Alignment);
8825   verifyFormat("class C {\n"
8826                "public:\n"
8827                "  int          i = 1;\n"
8828                "  virtual void f() = 0;\n"
8829                "};",
8830                Alignment);
8831   verifyFormat("float i = 1;\n"
8832                "if (SomeType t = getSomething()) {\n"
8833                "}\n"
8834                "const unsigned j = 2;\n"
8835                "int            big = 10000;",
8836                Alignment);
8837   verifyFormat("float j = 7;\n"
8838                "for (int k = 0; k < N; ++k) {\n"
8839                "}\n"
8840                "unsigned j = 2;\n"
8841                "int      big = 10000;\n"
8842                "}",
8843                Alignment);
8844   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8845   verifyFormat("float              i = 1;\n"
8846                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8847                "    = someLooooooooooooooooongFunction();\n"
8848                "int j = 2;",
8849                Alignment);
8850   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8851   verifyFormat("int                i = 1;\n"
8852                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8853                "    someLooooooooooooooooongFunction();\n"
8854                "int j = 2;",
8855                Alignment);
8856 
8857   Alignment.AlignConsecutiveAssignments = true;
8858   verifyFormat("auto lambda = []() {\n"
8859                "  auto  ii = 0;\n"
8860                "  float j  = 0;\n"
8861                "  return 0;\n"
8862                "};\n"
8863                "int   i  = 0;\n"
8864                "float i2 = 0;\n"
8865                "auto  v  = type{\n"
8866                "    i = 1,   //\n"
8867                "    (i = 2), //\n"
8868                "    i = 3    //\n"
8869                "};",
8870                Alignment);
8871   Alignment.AlignConsecutiveAssignments = false;
8872 
8873   // FIXME: Should align all three declarations
8874   verifyFormat(
8875       "int      i = 1;\n"
8876       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8877       "                          loooooooooooooooooooooongParameterB);\n"
8878       "int j = 2;",
8879       Alignment);
8880 
8881   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8882   // We expect declarations and assignments to align, as long as it doesn't
8883   // exceed the column limit, starting a new alignemnt sequence whenever it
8884   // happens.
8885   Alignment.AlignConsecutiveAssignments = true;
8886   Alignment.ColumnLimit = 30;
8887   verifyFormat("float    ii              = 1;\n"
8888                "unsigned j               = 2;\n"
8889                "int someVerylongVariable = 1;\n"
8890                "AnotherLongType  ll = 123456;\n"
8891                "VeryVeryLongType k  = 2;\n"
8892                "int              myvar = 1;",
8893                Alignment);
8894   Alignment.ColumnLimit = 80;
8895   Alignment.AlignConsecutiveAssignments = false;
8896 
8897   verifyFormat(
8898       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8899       "          typename LongType, typename B>\n"
8900       "auto foo() {}\n",
8901       Alignment);
8902   verifyFormat("float a, b = 1;\n"
8903                "int   c = 2;\n"
8904                "int   dd = 3;\n",
8905                Alignment);
8906   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8907                "float b[1][] = {{3.f}};\n",
8908                Alignment);
8909   Alignment.AlignConsecutiveAssignments = true;
8910   verifyFormat("float a, b = 1;\n"
8911                "int   c  = 2;\n"
8912                "int   dd = 3;\n",
8913                Alignment);
8914   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8915                "float b[1][] = {{3.f}};\n",
8916                Alignment);
8917   Alignment.AlignConsecutiveAssignments = false;
8918 
8919   Alignment.ColumnLimit = 30;
8920   Alignment.BinPackParameters = false;
8921   verifyFormat("void foo(float     a,\n"
8922                "         float     b,\n"
8923                "         int       c,\n"
8924                "         uint32_t *d) {\n"
8925                "  int *  e = 0;\n"
8926                "  float  f = 0;\n"
8927                "  double g = 0;\n"
8928                "}\n"
8929                "void bar(ino_t     a,\n"
8930                "         int       b,\n"
8931                "         uint32_t *c,\n"
8932                "         bool      d) {}\n",
8933                Alignment);
8934   Alignment.BinPackParameters = true;
8935   Alignment.ColumnLimit = 80;
8936 }
8937 
8938 TEST_F(FormatTest, LinuxBraceBreaking) {
8939   FormatStyle LinuxBraceStyle = getLLVMStyle();
8940   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8941   verifyFormat("namespace a\n"
8942                "{\n"
8943                "class A\n"
8944                "{\n"
8945                "  void f()\n"
8946                "  {\n"
8947                "    if (true) {\n"
8948                "      a();\n"
8949                "      b();\n"
8950                "    } else {\n"
8951                "      a();\n"
8952                "    }\n"
8953                "  }\n"
8954                "  void g() { return; }\n"
8955                "};\n"
8956                "struct B {\n"
8957                "  int x;\n"
8958                "};\n"
8959                "}\n",
8960                LinuxBraceStyle);
8961   verifyFormat("enum X {\n"
8962                "  Y = 0,\n"
8963                "}\n",
8964                LinuxBraceStyle);
8965   verifyFormat("struct S {\n"
8966                "  int Type;\n"
8967                "  union {\n"
8968                "    int x;\n"
8969                "    double y;\n"
8970                "  } Value;\n"
8971                "  class C\n"
8972                "  {\n"
8973                "    MyFavoriteType Value;\n"
8974                "  } Class;\n"
8975                "}\n",
8976                LinuxBraceStyle);
8977 }
8978 
8979 TEST_F(FormatTest, MozillaBraceBreaking) {
8980   FormatStyle MozillaBraceStyle = getLLVMStyle();
8981   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8982   verifyFormat("namespace a {\n"
8983                "class A\n"
8984                "{\n"
8985                "  void f()\n"
8986                "  {\n"
8987                "    if (true) {\n"
8988                "      a();\n"
8989                "      b();\n"
8990                "    }\n"
8991                "  }\n"
8992                "  void g() { return; }\n"
8993                "};\n"
8994                "enum E\n"
8995                "{\n"
8996                "  A,\n"
8997                "  // foo\n"
8998                "  B,\n"
8999                "  C\n"
9000                "};\n"
9001                "struct B\n"
9002                "{\n"
9003                "  int x;\n"
9004                "};\n"
9005                "}\n",
9006                MozillaBraceStyle);
9007   verifyFormat("struct S\n"
9008                "{\n"
9009                "  int Type;\n"
9010                "  union\n"
9011                "  {\n"
9012                "    int x;\n"
9013                "    double y;\n"
9014                "  } Value;\n"
9015                "  class C\n"
9016                "  {\n"
9017                "    MyFavoriteType Value;\n"
9018                "  } Class;\n"
9019                "}\n",
9020                MozillaBraceStyle);
9021 }
9022 
9023 TEST_F(FormatTest, StroustrupBraceBreaking) {
9024   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9025   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9026   verifyFormat("namespace a {\n"
9027                "class A {\n"
9028                "  void f()\n"
9029                "  {\n"
9030                "    if (true) {\n"
9031                "      a();\n"
9032                "      b();\n"
9033                "    }\n"
9034                "  }\n"
9035                "  void g() { return; }\n"
9036                "};\n"
9037                "struct B {\n"
9038                "  int x;\n"
9039                "};\n"
9040                "}\n",
9041                StroustrupBraceStyle);
9042 
9043   verifyFormat("void foo()\n"
9044                "{\n"
9045                "  if (a) {\n"
9046                "    a();\n"
9047                "  }\n"
9048                "  else {\n"
9049                "    b();\n"
9050                "  }\n"
9051                "}\n",
9052                StroustrupBraceStyle);
9053 
9054   verifyFormat("#ifdef _DEBUG\n"
9055                "int foo(int i = 0)\n"
9056                "#else\n"
9057                "int foo(int i = 5)\n"
9058                "#endif\n"
9059                "{\n"
9060                "  return i;\n"
9061                "}",
9062                StroustrupBraceStyle);
9063 
9064   verifyFormat("void foo() {}\n"
9065                "void bar()\n"
9066                "#ifdef _DEBUG\n"
9067                "{\n"
9068                "  foo();\n"
9069                "}\n"
9070                "#else\n"
9071                "{\n"
9072                "}\n"
9073                "#endif",
9074                StroustrupBraceStyle);
9075 
9076   verifyFormat("void foobar() { int i = 5; }\n"
9077                "#ifdef _DEBUG\n"
9078                "void bar() {}\n"
9079                "#else\n"
9080                "void bar() { foobar(); }\n"
9081                "#endif",
9082                StroustrupBraceStyle);
9083 }
9084 
9085 TEST_F(FormatTest, AllmanBraceBreaking) {
9086   FormatStyle AllmanBraceStyle = getLLVMStyle();
9087   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9088   verifyFormat("namespace a\n"
9089                "{\n"
9090                "class A\n"
9091                "{\n"
9092                "  void f()\n"
9093                "  {\n"
9094                "    if (true)\n"
9095                "    {\n"
9096                "      a();\n"
9097                "      b();\n"
9098                "    }\n"
9099                "  }\n"
9100                "  void g() { return; }\n"
9101                "};\n"
9102                "struct B\n"
9103                "{\n"
9104                "  int x;\n"
9105                "};\n"
9106                "}",
9107                AllmanBraceStyle);
9108 
9109   verifyFormat("void f()\n"
9110                "{\n"
9111                "  if (true)\n"
9112                "  {\n"
9113                "    a();\n"
9114                "  }\n"
9115                "  else if (false)\n"
9116                "  {\n"
9117                "    b();\n"
9118                "  }\n"
9119                "  else\n"
9120                "  {\n"
9121                "    c();\n"
9122                "  }\n"
9123                "}\n",
9124                AllmanBraceStyle);
9125 
9126   verifyFormat("void f()\n"
9127                "{\n"
9128                "  for (int i = 0; i < 10; ++i)\n"
9129                "  {\n"
9130                "    a();\n"
9131                "  }\n"
9132                "  while (false)\n"
9133                "  {\n"
9134                "    b();\n"
9135                "  }\n"
9136                "  do\n"
9137                "  {\n"
9138                "    c();\n"
9139                "  } while (false)\n"
9140                "}\n",
9141                AllmanBraceStyle);
9142 
9143   verifyFormat("void f(int a)\n"
9144                "{\n"
9145                "  switch (a)\n"
9146                "  {\n"
9147                "  case 0:\n"
9148                "    break;\n"
9149                "  case 1:\n"
9150                "  {\n"
9151                "    break;\n"
9152                "  }\n"
9153                "  case 2:\n"
9154                "  {\n"
9155                "  }\n"
9156                "  break;\n"
9157                "  default:\n"
9158                "    break;\n"
9159                "  }\n"
9160                "}\n",
9161                AllmanBraceStyle);
9162 
9163   verifyFormat("enum X\n"
9164                "{\n"
9165                "  Y = 0,\n"
9166                "}\n",
9167                AllmanBraceStyle);
9168   verifyFormat("enum X\n"
9169                "{\n"
9170                "  Y = 0\n"
9171                "}\n",
9172                AllmanBraceStyle);
9173 
9174   verifyFormat("@interface BSApplicationController ()\n"
9175                "{\n"
9176                "@private\n"
9177                "  id _extraIvar;\n"
9178                "}\n"
9179                "@end\n",
9180                AllmanBraceStyle);
9181 
9182   verifyFormat("#ifdef _DEBUG\n"
9183                "int foo(int i = 0)\n"
9184                "#else\n"
9185                "int foo(int i = 5)\n"
9186                "#endif\n"
9187                "{\n"
9188                "  return i;\n"
9189                "}",
9190                AllmanBraceStyle);
9191 
9192   verifyFormat("void foo() {}\n"
9193                "void bar()\n"
9194                "#ifdef _DEBUG\n"
9195                "{\n"
9196                "  foo();\n"
9197                "}\n"
9198                "#else\n"
9199                "{\n"
9200                "}\n"
9201                "#endif",
9202                AllmanBraceStyle);
9203 
9204   verifyFormat("void foobar() { int i = 5; }\n"
9205                "#ifdef _DEBUG\n"
9206                "void bar() {}\n"
9207                "#else\n"
9208                "void bar() { foobar(); }\n"
9209                "#endif",
9210                AllmanBraceStyle);
9211 
9212   // This shouldn't affect ObjC blocks..
9213   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9214                "  // ...\n"
9215                "  int i;\n"
9216                "}];",
9217                AllmanBraceStyle);
9218   verifyFormat("void (^block)(void) = ^{\n"
9219                "  // ...\n"
9220                "  int i;\n"
9221                "};",
9222                AllmanBraceStyle);
9223   // .. or dict literals.
9224   verifyFormat("void f()\n"
9225                "{\n"
9226                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9227                "}",
9228                AllmanBraceStyle);
9229   verifyFormat("int f()\n"
9230                "{ // comment\n"
9231                "  return 42;\n"
9232                "}",
9233                AllmanBraceStyle);
9234 
9235   AllmanBraceStyle.ColumnLimit = 19;
9236   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9237   AllmanBraceStyle.ColumnLimit = 18;
9238   verifyFormat("void f()\n"
9239                "{\n"
9240                "  int i;\n"
9241                "}",
9242                AllmanBraceStyle);
9243   AllmanBraceStyle.ColumnLimit = 80;
9244 
9245   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9246   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9247   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9248   verifyFormat("void f(bool b)\n"
9249                "{\n"
9250                "  if (b)\n"
9251                "  {\n"
9252                "    return;\n"
9253                "  }\n"
9254                "}\n",
9255                BreakBeforeBraceShortIfs);
9256   verifyFormat("void f(bool b)\n"
9257                "{\n"
9258                "  if (b) return;\n"
9259                "}\n",
9260                BreakBeforeBraceShortIfs);
9261   verifyFormat("void f(bool b)\n"
9262                "{\n"
9263                "  while (b)\n"
9264                "  {\n"
9265                "    return;\n"
9266                "  }\n"
9267                "}\n",
9268                BreakBeforeBraceShortIfs);
9269 }
9270 
9271 TEST_F(FormatTest, GNUBraceBreaking) {
9272   FormatStyle GNUBraceStyle = getLLVMStyle();
9273   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9274   verifyFormat("namespace a\n"
9275                "{\n"
9276                "class A\n"
9277                "{\n"
9278                "  void f()\n"
9279                "  {\n"
9280                "    int a;\n"
9281                "    {\n"
9282                "      int b;\n"
9283                "    }\n"
9284                "    if (true)\n"
9285                "      {\n"
9286                "        a();\n"
9287                "        b();\n"
9288                "      }\n"
9289                "  }\n"
9290                "  void g() { return; }\n"
9291                "}\n"
9292                "}",
9293                GNUBraceStyle);
9294 
9295   verifyFormat("void f()\n"
9296                "{\n"
9297                "  if (true)\n"
9298                "    {\n"
9299                "      a();\n"
9300                "    }\n"
9301                "  else if (false)\n"
9302                "    {\n"
9303                "      b();\n"
9304                "    }\n"
9305                "  else\n"
9306                "    {\n"
9307                "      c();\n"
9308                "    }\n"
9309                "}\n",
9310                GNUBraceStyle);
9311 
9312   verifyFormat("void f()\n"
9313                "{\n"
9314                "  for (int i = 0; i < 10; ++i)\n"
9315                "    {\n"
9316                "      a();\n"
9317                "    }\n"
9318                "  while (false)\n"
9319                "    {\n"
9320                "      b();\n"
9321                "    }\n"
9322                "  do\n"
9323                "    {\n"
9324                "      c();\n"
9325                "    }\n"
9326                "  while (false);\n"
9327                "}\n",
9328                GNUBraceStyle);
9329 
9330   verifyFormat("void f(int a)\n"
9331                "{\n"
9332                "  switch (a)\n"
9333                "    {\n"
9334                "    case 0:\n"
9335                "      break;\n"
9336                "    case 1:\n"
9337                "      {\n"
9338                "        break;\n"
9339                "      }\n"
9340                "    case 2:\n"
9341                "      {\n"
9342                "      }\n"
9343                "      break;\n"
9344                "    default:\n"
9345                "      break;\n"
9346                "    }\n"
9347                "}\n",
9348                GNUBraceStyle);
9349 
9350   verifyFormat("enum X\n"
9351                "{\n"
9352                "  Y = 0,\n"
9353                "}\n",
9354                GNUBraceStyle);
9355 
9356   verifyFormat("@interface BSApplicationController ()\n"
9357                "{\n"
9358                "@private\n"
9359                "  id _extraIvar;\n"
9360                "}\n"
9361                "@end\n",
9362                GNUBraceStyle);
9363 
9364   verifyFormat("#ifdef _DEBUG\n"
9365                "int foo(int i = 0)\n"
9366                "#else\n"
9367                "int foo(int i = 5)\n"
9368                "#endif\n"
9369                "{\n"
9370                "  return i;\n"
9371                "}",
9372                GNUBraceStyle);
9373 
9374   verifyFormat("void foo() {}\n"
9375                "void bar()\n"
9376                "#ifdef _DEBUG\n"
9377                "{\n"
9378                "  foo();\n"
9379                "}\n"
9380                "#else\n"
9381                "{\n"
9382                "}\n"
9383                "#endif",
9384                GNUBraceStyle);
9385 
9386   verifyFormat("void foobar() { int i = 5; }\n"
9387                "#ifdef _DEBUG\n"
9388                "void bar() {}\n"
9389                "#else\n"
9390                "void bar() { foobar(); }\n"
9391                "#endif",
9392                GNUBraceStyle);
9393 }
9394 
9395 TEST_F(FormatTest, WebKitBraceBreaking) {
9396   FormatStyle WebKitBraceStyle = getLLVMStyle();
9397   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9398   verifyFormat("namespace a {\n"
9399                "class A {\n"
9400                "  void f()\n"
9401                "  {\n"
9402                "    if (true) {\n"
9403                "      a();\n"
9404                "      b();\n"
9405                "    }\n"
9406                "  }\n"
9407                "  void g() { return; }\n"
9408                "};\n"
9409                "enum E {\n"
9410                "  A,\n"
9411                "  // foo\n"
9412                "  B,\n"
9413                "  C\n"
9414                "};\n"
9415                "struct B {\n"
9416                "  int x;\n"
9417                "};\n"
9418                "}\n",
9419                WebKitBraceStyle);
9420   verifyFormat("struct S {\n"
9421                "  int Type;\n"
9422                "  union {\n"
9423                "    int x;\n"
9424                "    double y;\n"
9425                "  } Value;\n"
9426                "  class C {\n"
9427                "    MyFavoriteType Value;\n"
9428                "  } Class;\n"
9429                "};\n",
9430                WebKitBraceStyle);
9431 }
9432 
9433 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9434   verifyFormat("void f() {\n"
9435                "  try {\n"
9436                "  } catch (const Exception &e) {\n"
9437                "  }\n"
9438                "}\n",
9439                getLLVMStyle());
9440 }
9441 
9442 TEST_F(FormatTest, UnderstandsPragmas) {
9443   verifyFormat("#pragma omp reduction(| : var)");
9444   verifyFormat("#pragma omp reduction(+ : var)");
9445 
9446   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9447             "(including parentheses).",
9448             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9449                    "(including parentheses)."));
9450 }
9451 
9452 TEST_F(FormatTest, UnderstandPragmaOption) {
9453   verifyFormat("#pragma option -C -A");
9454 
9455   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9456 }
9457 
9458 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9459   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9460   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9461                                   << " differs from Style #0"
9462 
9463 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9464   SmallVector<FormatStyle, 3> Styles;
9465   Styles.resize(3);
9466 
9467   Styles[0] = getLLVMStyle();
9468   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9469   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9470   EXPECT_ALL_STYLES_EQUAL(Styles);
9471 
9472   Styles[0] = getGoogleStyle();
9473   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9474   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9475   EXPECT_ALL_STYLES_EQUAL(Styles);
9476 
9477   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9478   EXPECT_TRUE(
9479       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9480   EXPECT_TRUE(
9481       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9482   EXPECT_ALL_STYLES_EQUAL(Styles);
9483 
9484   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9485   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9486   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9487   EXPECT_ALL_STYLES_EQUAL(Styles);
9488 
9489   Styles[0] = getMozillaStyle();
9490   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9491   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9492   EXPECT_ALL_STYLES_EQUAL(Styles);
9493 
9494   Styles[0] = getWebKitStyle();
9495   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9496   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9497   EXPECT_ALL_STYLES_EQUAL(Styles);
9498 
9499   Styles[0] = getGNUStyle();
9500   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9501   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9502   EXPECT_ALL_STYLES_EQUAL(Styles);
9503 
9504   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9505 }
9506 
9507 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9508   SmallVector<FormatStyle, 8> Styles;
9509   Styles.resize(2);
9510 
9511   Styles[0] = getGoogleStyle();
9512   Styles[1] = getLLVMStyle();
9513   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9514   EXPECT_ALL_STYLES_EQUAL(Styles);
9515 
9516   Styles.resize(5);
9517   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9518   Styles[1] = getLLVMStyle();
9519   Styles[1].Language = FormatStyle::LK_JavaScript;
9520   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9521 
9522   Styles[2] = getLLVMStyle();
9523   Styles[2].Language = FormatStyle::LK_JavaScript;
9524   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9525                                   "BasedOnStyle: Google",
9526                                   &Styles[2])
9527                    .value());
9528 
9529   Styles[3] = getLLVMStyle();
9530   Styles[3].Language = FormatStyle::LK_JavaScript;
9531   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9532                                   "Language: JavaScript",
9533                                   &Styles[3])
9534                    .value());
9535 
9536   Styles[4] = getLLVMStyle();
9537   Styles[4].Language = FormatStyle::LK_JavaScript;
9538   EXPECT_EQ(0, parseConfiguration("---\n"
9539                                   "BasedOnStyle: LLVM\n"
9540                                   "IndentWidth: 123\n"
9541                                   "---\n"
9542                                   "BasedOnStyle: Google\n"
9543                                   "Language: JavaScript",
9544                                   &Styles[4])
9545                    .value());
9546   EXPECT_ALL_STYLES_EQUAL(Styles);
9547 }
9548 
9549 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9550   Style.FIELD = false;                                                         \
9551   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9552   EXPECT_TRUE(Style.FIELD);                                                    \
9553   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9554   EXPECT_FALSE(Style.FIELD);
9555 
9556 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9557 
9558 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9559   Style.STRUCT.FIELD = false;                                                  \
9560   EXPECT_EQ(0,                                                                 \
9561             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9562                 .value());                                                     \
9563   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9564   EXPECT_EQ(0,                                                                 \
9565             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9566                 .value());                                                     \
9567   EXPECT_FALSE(Style.STRUCT.FIELD);
9568 
9569 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9570   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9571 
9572 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9573   EXPECT_NE(VALUE, Style.FIELD);                                               \
9574   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9575   EXPECT_EQ(VALUE, Style.FIELD)
9576 
9577 TEST_F(FormatTest, ParsesConfigurationBools) {
9578   FormatStyle Style = {};
9579   Style.Language = FormatStyle::LK_Cpp;
9580   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9581   CHECK_PARSE_BOOL(AlignOperands);
9582   CHECK_PARSE_BOOL(AlignTrailingComments);
9583   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9584   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9585   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9586   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9587   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9588   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9589   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9590   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9591   CHECK_PARSE_BOOL(BinPackArguments);
9592   CHECK_PARSE_BOOL(BinPackParameters);
9593   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
9594   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9595   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9596   CHECK_PARSE_BOOL(BreakStringLiterals);
9597   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9598   CHECK_PARSE_BOOL(DerivePointerAlignment);
9599   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9600   CHECK_PARSE_BOOL(DisableFormat);
9601   CHECK_PARSE_BOOL(IndentCaseLabels);
9602   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9603   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9604   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9605   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9606   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9607   CHECK_PARSE_BOOL(ReflowComments);
9608   CHECK_PARSE_BOOL(SortIncludes);
9609   CHECK_PARSE_BOOL(SpacesInParentheses);
9610   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9611   CHECK_PARSE_BOOL(SpacesInAngles);
9612   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9613   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9614   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9615   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9616   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
9617   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9618 
9619   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9620   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9621   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9622   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9623   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9624   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9625   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9626   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9627   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9628   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9629   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9630 }
9631 
9632 #undef CHECK_PARSE_BOOL
9633 
9634 TEST_F(FormatTest, ParsesConfiguration) {
9635   FormatStyle Style = {};
9636   Style.Language = FormatStyle::LK_Cpp;
9637   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9638   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9639               ConstructorInitializerIndentWidth, 1234u);
9640   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9641   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9642   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9643   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9644               PenaltyBreakBeforeFirstCallParameter, 1234u);
9645   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9646   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9647               PenaltyReturnTypeOnItsOwnLine, 1234u);
9648   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9649               SpacesBeforeTrailingComments, 1234u);
9650   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9651   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9652   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
9653 
9654   Style.PointerAlignment = FormatStyle::PAS_Middle;
9655   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9656               FormatStyle::PAS_Left);
9657   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9658               FormatStyle::PAS_Right);
9659   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9660               FormatStyle::PAS_Middle);
9661   // For backward compatibility:
9662   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9663               FormatStyle::PAS_Left);
9664   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9665               FormatStyle::PAS_Right);
9666   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9667               FormatStyle::PAS_Middle);
9668 
9669   Style.Standard = FormatStyle::LS_Auto;
9670   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9671   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9672   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9673   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9674   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9675 
9676   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9677   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9678               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9679   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9680               FormatStyle::BOS_None);
9681   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9682               FormatStyle::BOS_All);
9683   // For backward compatibility:
9684   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9685               FormatStyle::BOS_None);
9686   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9687               FormatStyle::BOS_All);
9688 
9689   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9690   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9691               FormatStyle::BAS_Align);
9692   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9693               FormatStyle::BAS_DontAlign);
9694   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9695               FormatStyle::BAS_AlwaysBreak);
9696   // For backward compatibility:
9697   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9698               FormatStyle::BAS_DontAlign);
9699   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9700               FormatStyle::BAS_Align);
9701 
9702   Style.UseTab = FormatStyle::UT_ForIndentation;
9703   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9704   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9705   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9706   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9707               FormatStyle::UT_ForContinuationAndIndentation);
9708   // For backward compatibility:
9709   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9710   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9711 
9712   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9713   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9714               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9715   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9716               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9717   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9718               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9719   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9720               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9721   // For backward compatibility:
9722   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9723               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9724   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9725               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9726 
9727   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9728   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9729               FormatStyle::SBPO_Never);
9730   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9731               FormatStyle::SBPO_Always);
9732   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9733               FormatStyle::SBPO_ControlStatements);
9734   // For backward compatibility:
9735   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9736               FormatStyle::SBPO_Never);
9737   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9738               FormatStyle::SBPO_ControlStatements);
9739 
9740   Style.ColumnLimit = 123;
9741   FormatStyle BaseStyle = getLLVMStyle();
9742   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9743   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9744 
9745   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9746   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9747               FormatStyle::BS_Attach);
9748   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9749               FormatStyle::BS_Linux);
9750   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9751               FormatStyle::BS_Mozilla);
9752   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9753               FormatStyle::BS_Stroustrup);
9754   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9755               FormatStyle::BS_Allman);
9756   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9757   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9758               FormatStyle::BS_WebKit);
9759   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9760               FormatStyle::BS_Custom);
9761 
9762   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9763   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9764               FormatStyle::RTBS_None);
9765   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9766               FormatStyle::RTBS_All);
9767   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9768               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9769   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9770               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9771   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9772               AlwaysBreakAfterReturnType,
9773               FormatStyle::RTBS_TopLevelDefinitions);
9774 
9775   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9776   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9777               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9778   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9779               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9780   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9781               AlwaysBreakAfterDefinitionReturnType,
9782               FormatStyle::DRTBS_TopLevel);
9783 
9784   Style.NamespaceIndentation = FormatStyle::NI_All;
9785   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9786               FormatStyle::NI_None);
9787   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9788               FormatStyle::NI_Inner);
9789   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9790               FormatStyle::NI_All);
9791 
9792   // FIXME: This is required because parsing a configuration simply overwrites
9793   // the first N elements of the list instead of resetting it.
9794   Style.ForEachMacros.clear();
9795   std::vector<std::string> BoostForeach;
9796   BoostForeach.push_back("BOOST_FOREACH");
9797   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9798   std::vector<std::string> BoostAndQForeach;
9799   BoostAndQForeach.push_back("BOOST_FOREACH");
9800   BoostAndQForeach.push_back("Q_FOREACH");
9801   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9802               BoostAndQForeach);
9803 
9804   Style.IncludeCategories.clear();
9805   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9806                                                                   {".*", 1}};
9807   CHECK_PARSE("IncludeCategories:\n"
9808               "  - Regex: abc/.*\n"
9809               "    Priority: 2\n"
9810               "  - Regex: .*\n"
9811               "    Priority: 1",
9812               IncludeCategories, ExpectedCategories);
9813   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
9814 }
9815 
9816 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9817   FormatStyle Style = {};
9818   Style.Language = FormatStyle::LK_Cpp;
9819   CHECK_PARSE("Language: Cpp\n"
9820               "IndentWidth: 12",
9821               IndentWidth, 12u);
9822   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9823                                "IndentWidth: 34",
9824                                &Style),
9825             ParseError::Unsuitable);
9826   EXPECT_EQ(12u, Style.IndentWidth);
9827   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9828   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9829 
9830   Style.Language = FormatStyle::LK_JavaScript;
9831   CHECK_PARSE("Language: JavaScript\n"
9832               "IndentWidth: 12",
9833               IndentWidth, 12u);
9834   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9835   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9836                                "IndentWidth: 34",
9837                                &Style),
9838             ParseError::Unsuitable);
9839   EXPECT_EQ(23u, Style.IndentWidth);
9840   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9841   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9842 
9843   CHECK_PARSE("BasedOnStyle: LLVM\n"
9844               "IndentWidth: 67",
9845               IndentWidth, 67u);
9846 
9847   CHECK_PARSE("---\n"
9848               "Language: JavaScript\n"
9849               "IndentWidth: 12\n"
9850               "---\n"
9851               "Language: Cpp\n"
9852               "IndentWidth: 34\n"
9853               "...\n",
9854               IndentWidth, 12u);
9855 
9856   Style.Language = FormatStyle::LK_Cpp;
9857   CHECK_PARSE("---\n"
9858               "Language: JavaScript\n"
9859               "IndentWidth: 12\n"
9860               "---\n"
9861               "Language: Cpp\n"
9862               "IndentWidth: 34\n"
9863               "...\n",
9864               IndentWidth, 34u);
9865   CHECK_PARSE("---\n"
9866               "IndentWidth: 78\n"
9867               "---\n"
9868               "Language: JavaScript\n"
9869               "IndentWidth: 56\n"
9870               "...\n",
9871               IndentWidth, 78u);
9872 
9873   Style.ColumnLimit = 123;
9874   Style.IndentWidth = 234;
9875   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9876   Style.TabWidth = 345;
9877   EXPECT_FALSE(parseConfiguration("---\n"
9878                                   "IndentWidth: 456\n"
9879                                   "BreakBeforeBraces: Allman\n"
9880                                   "---\n"
9881                                   "Language: JavaScript\n"
9882                                   "IndentWidth: 111\n"
9883                                   "TabWidth: 111\n"
9884                                   "---\n"
9885                                   "Language: Cpp\n"
9886                                   "BreakBeforeBraces: Stroustrup\n"
9887                                   "TabWidth: 789\n"
9888                                   "...\n",
9889                                   &Style));
9890   EXPECT_EQ(123u, Style.ColumnLimit);
9891   EXPECT_EQ(456u, Style.IndentWidth);
9892   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9893   EXPECT_EQ(789u, Style.TabWidth);
9894 
9895   EXPECT_EQ(parseConfiguration("---\n"
9896                                "Language: JavaScript\n"
9897                                "IndentWidth: 56\n"
9898                                "---\n"
9899                                "IndentWidth: 78\n"
9900                                "...\n",
9901                                &Style),
9902             ParseError::Error);
9903   EXPECT_EQ(parseConfiguration("---\n"
9904                                "Language: JavaScript\n"
9905                                "IndentWidth: 56\n"
9906                                "---\n"
9907                                "Language: JavaScript\n"
9908                                "IndentWidth: 78\n"
9909                                "...\n",
9910                                &Style),
9911             ParseError::Error);
9912 
9913   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9914 }
9915 
9916 #undef CHECK_PARSE
9917 
9918 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9919   FormatStyle Style = {};
9920   Style.Language = FormatStyle::LK_JavaScript;
9921   Style.BreakBeforeTernaryOperators = true;
9922   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9923   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9924 
9925   Style.BreakBeforeTernaryOperators = true;
9926   EXPECT_EQ(0, parseConfiguration("---\n"
9927                                   "BasedOnStyle: Google\n"
9928                                   "---\n"
9929                                   "Language: JavaScript\n"
9930                                   "IndentWidth: 76\n"
9931                                   "...\n",
9932                                   &Style)
9933                    .value());
9934   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9935   EXPECT_EQ(76u, Style.IndentWidth);
9936   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9937 }
9938 
9939 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9940   FormatStyle Style = getLLVMStyle();
9941   std::string YAML = configurationAsText(Style);
9942   FormatStyle ParsedStyle = {};
9943   ParsedStyle.Language = FormatStyle::LK_Cpp;
9944   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9945   EXPECT_EQ(Style, ParsedStyle);
9946 }
9947 
9948 TEST_F(FormatTest, WorksFor8bitEncodings) {
9949   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9950             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9951             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9952             "\"\xef\xee\xf0\xf3...\"",
9953             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9954                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9955                    "\xef\xee\xf0\xf3...\"",
9956                    getLLVMStyleWithColumns(12)));
9957 }
9958 
9959 TEST_F(FormatTest, HandlesUTF8BOM) {
9960   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9961   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9962             format("\xef\xbb\xbf#include <iostream>"));
9963   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9964             format("\xef\xbb\xbf\n#include <iostream>"));
9965 }
9966 
9967 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9968 #if !defined(_MSC_VER)
9969 
9970 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9971   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9972                getLLVMStyleWithColumns(35));
9973   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9974                getLLVMStyleWithColumns(31));
9975   verifyFormat("// Однажды в студёную зимнюю пору...",
9976                getLLVMStyleWithColumns(36));
9977   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9978   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9979                getLLVMStyleWithColumns(39));
9980   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9981                getLLVMStyleWithColumns(35));
9982 }
9983 
9984 TEST_F(FormatTest, SplitsUTF8Strings) {
9985   // Non-printable characters' width is currently considered to be the length in
9986   // bytes in UTF8. The characters can be displayed in very different manner
9987   // (zero-width, single width with a substitution glyph, expanded to their code
9988   // (e.g. "<8d>"), so there's no single correct way to handle them.
9989   EXPECT_EQ("\"aaaaÄ\"\n"
9990             "\"\xc2\x8d\";",
9991             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9992   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9993             "\"\xc2\x8d\";",
9994             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9995   EXPECT_EQ("\"Однажды, в \"\n"
9996             "\"студёную \"\n"
9997             "\"зимнюю \"\n"
9998             "\"пору,\"",
9999             format("\"Однажды, в студёную зимнюю пору,\"",
10000                    getLLVMStyleWithColumns(13)));
10001   EXPECT_EQ(
10002       "\"一 二 三 \"\n"
10003       "\"四 五六 \"\n"
10004       "\"七 八 九 \"\n"
10005       "\"十\"",
10006       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10007   EXPECT_EQ("\"一\t二 \"\n"
10008             "\"\t三 \"\n"
10009             "\"四 五\t六 \"\n"
10010             "\"\t七 \"\n"
10011             "\"八九十\tqq\"",
10012             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10013                    getLLVMStyleWithColumns(11)));
10014 
10015   // UTF8 character in an escape sequence.
10016   EXPECT_EQ("\"aaaaaa\"\n"
10017             "\"\\\xC2\x8D\"",
10018             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10019 }
10020 
10021 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10022   EXPECT_EQ("const char *sssss =\n"
10023             "    \"一二三四五六七八\\\n"
10024             " 九 十\";",
10025             format("const char *sssss = \"一二三四五六七八\\\n"
10026                    " 九 十\";",
10027                    getLLVMStyleWithColumns(30)));
10028 }
10029 
10030 TEST_F(FormatTest, SplitsUTF8LineComments) {
10031   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10032             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10033   EXPECT_EQ("// Я из лесу\n"
10034             "// вышел; был\n"
10035             "// сильный\n"
10036             "// мороз.",
10037             format("// Я из лесу вышел; был сильный мороз.",
10038                    getLLVMStyleWithColumns(13)));
10039   EXPECT_EQ("// 一二三\n"
10040             "// 四五六七\n"
10041             "// 八  九\n"
10042             "// 十",
10043             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10044 }
10045 
10046 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10047   EXPECT_EQ("/* Гляжу,\n"
10048             " * поднимается\n"
10049             " * медленно в\n"
10050             " * гору\n"
10051             " * Лошадка,\n"
10052             " * везущая\n"
10053             " * хворосту\n"
10054             " * воз. */",
10055             format("/* Гляжу, поднимается медленно в гору\n"
10056                    " * Лошадка, везущая хворосту воз. */",
10057                    getLLVMStyleWithColumns(13)));
10058   EXPECT_EQ(
10059       "/* 一二三\n"
10060       " * 四五六七\n"
10061       " * 八  九\n"
10062       " * 十  */",
10063       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10064   EXPECT_EQ("/* �������� ��������\n"
10065             " * ��������\n"
10066             " * ������-�� */",
10067             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10068 }
10069 
10070 #endif // _MSC_VER
10071 
10072 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10073   FormatStyle Style = getLLVMStyle();
10074 
10075   Style.ConstructorInitializerIndentWidth = 4;
10076   verifyFormat(
10077       "SomeClass::Constructor()\n"
10078       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10079       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10080       Style);
10081 
10082   Style.ConstructorInitializerIndentWidth = 2;
10083   verifyFormat(
10084       "SomeClass::Constructor()\n"
10085       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10086       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10087       Style);
10088 
10089   Style.ConstructorInitializerIndentWidth = 0;
10090   verifyFormat(
10091       "SomeClass::Constructor()\n"
10092       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10093       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10094       Style);
10095   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10096   verifyFormat(
10097       "SomeLongTemplateVariableName<\n"
10098       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10099       Style);
10100   verifyFormat(
10101       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10102       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10103       Style);
10104 }
10105 
10106 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10107   FormatStyle Style = getLLVMStyle();
10108   Style.BreakConstructorInitializersBeforeComma = true;
10109   Style.ConstructorInitializerIndentWidth = 4;
10110   verifyFormat("SomeClass::Constructor()\n"
10111                "    : a(a)\n"
10112                "    , b(b)\n"
10113                "    , c(c) {}",
10114                Style);
10115   verifyFormat("SomeClass::Constructor()\n"
10116                "    : a(a) {}",
10117                Style);
10118 
10119   Style.ColumnLimit = 0;
10120   verifyFormat("SomeClass::Constructor()\n"
10121                "    : a(a) {}",
10122                Style);
10123   verifyFormat("SomeClass::Constructor() noexcept\n"
10124                "    : a(a) {}",
10125                Style);
10126   verifyFormat("SomeClass::Constructor()\n"
10127                "    : a(a)\n"
10128                "    , b(b)\n"
10129                "    , c(c) {}",
10130                Style);
10131   verifyFormat("SomeClass::Constructor()\n"
10132                "    : a(a) {\n"
10133                "  foo();\n"
10134                "  bar();\n"
10135                "}",
10136                Style);
10137 
10138   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10139   verifyFormat("SomeClass::Constructor()\n"
10140                "    : a(a)\n"
10141                "    , b(b)\n"
10142                "    , c(c) {\n}",
10143                Style);
10144   verifyFormat("SomeClass::Constructor()\n"
10145                "    : a(a) {\n}",
10146                Style);
10147 
10148   Style.ColumnLimit = 80;
10149   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10150   Style.ConstructorInitializerIndentWidth = 2;
10151   verifyFormat("SomeClass::Constructor()\n"
10152                "  : a(a)\n"
10153                "  , b(b)\n"
10154                "  , c(c) {}",
10155                Style);
10156 
10157   Style.ConstructorInitializerIndentWidth = 0;
10158   verifyFormat("SomeClass::Constructor()\n"
10159                ": a(a)\n"
10160                ", b(b)\n"
10161                ", c(c) {}",
10162                Style);
10163 
10164   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10165   Style.ConstructorInitializerIndentWidth = 4;
10166   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10167   verifyFormat(
10168       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10169       Style);
10170   verifyFormat(
10171       "SomeClass::Constructor()\n"
10172       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10173       Style);
10174   Style.ConstructorInitializerIndentWidth = 4;
10175   Style.ColumnLimit = 60;
10176   verifyFormat("SomeClass::Constructor()\n"
10177                "    : aaaaaaaa(aaaaaaaa)\n"
10178                "    , aaaaaaaa(aaaaaaaa)\n"
10179                "    , aaaaaaaa(aaaaaaaa) {}",
10180                Style);
10181 }
10182 
10183 TEST_F(FormatTest, Destructors) {
10184   verifyFormat("void F(int &i) { i.~int(); }");
10185   verifyFormat("void F(int &i) { i->~int(); }");
10186 }
10187 
10188 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10189   FormatStyle Style = getWebKitStyle();
10190 
10191   // Don't indent in outer namespaces.
10192   verifyFormat("namespace outer {\n"
10193                "int i;\n"
10194                "namespace inner {\n"
10195                "    int i;\n"
10196                "} // namespace inner\n"
10197                "} // namespace outer\n"
10198                "namespace other_outer {\n"
10199                "int i;\n"
10200                "}",
10201                Style);
10202 
10203   // Don't indent case labels.
10204   verifyFormat("switch (variable) {\n"
10205                "case 1:\n"
10206                "case 2:\n"
10207                "    doSomething();\n"
10208                "    break;\n"
10209                "default:\n"
10210                "    ++variable;\n"
10211                "}",
10212                Style);
10213 
10214   // Wrap before binary operators.
10215   EXPECT_EQ("void f()\n"
10216             "{\n"
10217             "    if (aaaaaaaaaaaaaaaa\n"
10218             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10219             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10220             "        return;\n"
10221             "}",
10222             format("void f() {\n"
10223                    "if (aaaaaaaaaaaaaaaa\n"
10224                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10225                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10226                    "return;\n"
10227                    "}",
10228                    Style));
10229 
10230   // Allow functions on a single line.
10231   verifyFormat("void f() { return; }", Style);
10232 
10233   // Constructor initializers are formatted one per line with the "," on the
10234   // new line.
10235   verifyFormat("Constructor()\n"
10236                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10237                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10238                "          aaaaaaaaaaaaaa)\n"
10239                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10240                "{\n"
10241                "}",
10242                Style);
10243   verifyFormat("SomeClass::Constructor()\n"
10244                "    : a(a)\n"
10245                "{\n"
10246                "}",
10247                Style);
10248   EXPECT_EQ("SomeClass::Constructor()\n"
10249             "    : a(a)\n"
10250             "{\n"
10251             "}",
10252             format("SomeClass::Constructor():a(a){}", Style));
10253   verifyFormat("SomeClass::Constructor()\n"
10254                "    : a(a)\n"
10255                "    , b(b)\n"
10256                "    , c(c)\n"
10257                "{\n"
10258                "}",
10259                Style);
10260   verifyFormat("SomeClass::Constructor()\n"
10261                "    : a(a)\n"
10262                "{\n"
10263                "    foo();\n"
10264                "    bar();\n"
10265                "}",
10266                Style);
10267 
10268   // Access specifiers should be aligned left.
10269   verifyFormat("class C {\n"
10270                "public:\n"
10271                "    int i;\n"
10272                "};",
10273                Style);
10274 
10275   // Do not align comments.
10276   verifyFormat("int a; // Do not\n"
10277                "double b; // align comments.",
10278                Style);
10279 
10280   // Do not align operands.
10281   EXPECT_EQ("ASSERT(aaaa\n"
10282             "    || bbbb);",
10283             format("ASSERT ( aaaa\n||bbbb);", Style));
10284 
10285   // Accept input's line breaks.
10286   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10287             "    || bbbbbbbbbbbbbbb) {\n"
10288             "    i++;\n"
10289             "}",
10290             format("if (aaaaaaaaaaaaaaa\n"
10291                    "|| bbbbbbbbbbbbbbb) { i++; }",
10292                    Style));
10293   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10294             "    i++;\n"
10295             "}",
10296             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10297 
10298   // Don't automatically break all macro definitions (llvm.org/PR17842).
10299   verifyFormat("#define aNumber 10", Style);
10300   // However, generally keep the line breaks that the user authored.
10301   EXPECT_EQ("#define aNumber \\\n"
10302             "    10",
10303             format("#define aNumber \\\n"
10304                    " 10",
10305                    Style));
10306 
10307   // Keep empty and one-element array literals on a single line.
10308   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10309             "                                  copyItems:YES];",
10310             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10311                    "copyItems:YES];",
10312                    Style));
10313   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10314             "                                  copyItems:YES];",
10315             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10316                    "             copyItems:YES];",
10317                    Style));
10318   // FIXME: This does not seem right, there should be more indentation before
10319   // the array literal's entries. Nested blocks have the same problem.
10320   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10321             "    @\"a\",\n"
10322             "    @\"a\"\n"
10323             "]\n"
10324             "                                  copyItems:YES];",
10325             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10326                    "     @\"a\",\n"
10327                    "     @\"a\"\n"
10328                    "     ]\n"
10329                    "       copyItems:YES];",
10330                    Style));
10331   EXPECT_EQ(
10332       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10333       "                                  copyItems:YES];",
10334       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10335              "   copyItems:YES];",
10336              Style));
10337 
10338   verifyFormat("[self.a b:c c:d];", Style);
10339   EXPECT_EQ("[self.a b:c\n"
10340             "        c:d];",
10341             format("[self.a b:c\n"
10342                    "c:d];",
10343                    Style));
10344 }
10345 
10346 TEST_F(FormatTest, FormatsLambdas) {
10347   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10348   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10349   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10350   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10351   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10352   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10353   verifyFormat("int x = f(*+[] {});");
10354   verifyFormat("void f() {\n"
10355                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10356                "}\n");
10357   verifyFormat("void f() {\n"
10358                "  other(x.begin(), //\n"
10359                "        x.end(),   //\n"
10360                "        [&](int, int) { return 1; });\n"
10361                "}\n");
10362   verifyFormat("SomeFunction([]() { // A cool function...\n"
10363                "  return 43;\n"
10364                "});");
10365   EXPECT_EQ("SomeFunction([]() {\n"
10366             "#define A a\n"
10367             "  return 43;\n"
10368             "});",
10369             format("SomeFunction([](){\n"
10370                    "#define A a\n"
10371                    "return 43;\n"
10372                    "});"));
10373   verifyFormat("void f() {\n"
10374                "  SomeFunction([](decltype(x), A *a) {});\n"
10375                "}");
10376   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10377                "    [](const aaaaaaaaaa &a) { return a; });");
10378   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10379                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10380                "});");
10381   verifyFormat("Constructor()\n"
10382                "    : Field([] { // comment\n"
10383                "        int i;\n"
10384                "      }) {}");
10385   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10386                "  return some_parameter.size();\n"
10387                "};");
10388   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10389                "    [](const string &s) { return s; };");
10390   verifyFormat("int i = aaaaaa ? 1 //\n"
10391                "               : [] {\n"
10392                "                   return 2; //\n"
10393                "                 }();");
10394   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10395                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10396                "                  return x == 2; // force break\n"
10397                "                });");
10398   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10399                "    int iiiiiiiiiiii) {\n"
10400                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10401                "});",
10402                getLLVMStyleWithColumns(60));
10403   verifyFormat("SomeFunction({[&] {\n"
10404                "                // comment\n"
10405                "              },\n"
10406                "              [&] {\n"
10407                "                // comment\n"
10408                "              }});");
10409   verifyFormat("SomeFunction({[&] {\n"
10410                "  // comment\n"
10411                "}});");
10412   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10413                "                             [&]() { return true; },\n"
10414                "                         aaaaa aaaaaaaaa);");
10415 
10416   // Lambdas with return types.
10417   verifyFormat("int c = []() -> int { return 2; }();\n");
10418   verifyFormat("int c = []() -> int * { return 2; }();\n");
10419   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10420   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10421   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10422   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10423   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10424   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10425   verifyFormat("[a, a]() -> a<1> {};");
10426   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10427                "                   int j) -> int {\n"
10428                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10429                "};");
10430   verifyFormat(
10431       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10432       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10433       "      return aaaaaaaaaaaaaaaaa;\n"
10434       "    });",
10435       getLLVMStyleWithColumns(70));
10436   verifyFormat("[]() //\n"
10437                "    -> int {\n"
10438                "  return 1; //\n"
10439                "};");
10440 
10441   // Multiple lambdas in the same parentheses change indentation rules.
10442   verifyFormat("SomeFunction(\n"
10443                "    []() {\n"
10444                "      int i = 42;\n"
10445                "      return i;\n"
10446                "    },\n"
10447                "    []() {\n"
10448                "      int j = 43;\n"
10449                "      return j;\n"
10450                "    });");
10451 
10452   // More complex introducers.
10453   verifyFormat("return [i, args...] {};");
10454 
10455   // Not lambdas.
10456   verifyFormat("constexpr char hello[]{\"hello\"};");
10457   verifyFormat("double &operator[](int i) { return 0; }\n"
10458                "int i;");
10459   verifyFormat("std::unique_ptr<int[]> foo() {}");
10460   verifyFormat("int i = a[a][a]->f();");
10461   verifyFormat("int i = (*b)[a]->f();");
10462 
10463   // Other corner cases.
10464   verifyFormat("void f() {\n"
10465                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10466                "      );\n"
10467                "}");
10468 
10469   // Lambdas created through weird macros.
10470   verifyFormat("void f() {\n"
10471                "  MACRO((const AA &a) { return 1; });\n"
10472                "  MACRO((AA &a) { return 1; });\n"
10473                "}");
10474 
10475   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10476                "      doo_dah();\n"
10477                "      doo_dah();\n"
10478                "    })) {\n"
10479                "}");
10480   verifyFormat("auto lambda = []() {\n"
10481                "  int a = 2\n"
10482                "#if A\n"
10483                "          + 2\n"
10484                "#endif\n"
10485                "      ;\n"
10486                "};");
10487 }
10488 
10489 TEST_F(FormatTest, FormatsBlocks) {
10490   FormatStyle ShortBlocks = getLLVMStyle();
10491   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10492   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10493   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10494   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10495   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10496   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10497   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10498 
10499   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10500   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10501   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10502 
10503   verifyFormat("[operation setCompletionBlock:^{\n"
10504                "  [self onOperationDone];\n"
10505                "}];");
10506   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10507                "  [self onOperationDone];\n"
10508                "}]};");
10509   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10510                "  f();\n"
10511                "}];");
10512   verifyFormat("int a = [operation block:^int(int *i) {\n"
10513                "  return 1;\n"
10514                "}];");
10515   verifyFormat("[myObject doSomethingWith:arg1\n"
10516                "                      aaa:^int(int *a) {\n"
10517                "                        return 1;\n"
10518                "                      }\n"
10519                "                      bbb:f(a * bbbbbbbb)];");
10520 
10521   verifyFormat("[operation setCompletionBlock:^{\n"
10522                "  [self.delegate newDataAvailable];\n"
10523                "}];",
10524                getLLVMStyleWithColumns(60));
10525   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10526                "  NSString *path = [self sessionFilePath];\n"
10527                "  if (path) {\n"
10528                "    // ...\n"
10529                "  }\n"
10530                "});");
10531   verifyFormat("[[SessionService sharedService]\n"
10532                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10533                "      if (window) {\n"
10534                "        [self windowDidLoad:window];\n"
10535                "      } else {\n"
10536                "        [self errorLoadingWindow];\n"
10537                "      }\n"
10538                "    }];");
10539   verifyFormat("void (^largeBlock)(void) = ^{\n"
10540                "  // ...\n"
10541                "};\n",
10542                getLLVMStyleWithColumns(40));
10543   verifyFormat("[[SessionService sharedService]\n"
10544                "    loadWindowWithCompletionBlock: //\n"
10545                "        ^(SessionWindow *window) {\n"
10546                "          if (window) {\n"
10547                "            [self windowDidLoad:window];\n"
10548                "          } else {\n"
10549                "            [self errorLoadingWindow];\n"
10550                "          }\n"
10551                "        }];",
10552                getLLVMStyleWithColumns(60));
10553   verifyFormat("[myObject doSomethingWith:arg1\n"
10554                "    firstBlock:^(Foo *a) {\n"
10555                "      // ...\n"
10556                "      int i;\n"
10557                "    }\n"
10558                "    secondBlock:^(Bar *b) {\n"
10559                "      // ...\n"
10560                "      int i;\n"
10561                "    }\n"
10562                "    thirdBlock:^Foo(Bar *b) {\n"
10563                "      // ...\n"
10564                "      int i;\n"
10565                "    }];");
10566   verifyFormat("[myObject doSomethingWith:arg1\n"
10567                "               firstBlock:-1\n"
10568                "              secondBlock:^(Bar *b) {\n"
10569                "                // ...\n"
10570                "                int i;\n"
10571                "              }];");
10572 
10573   verifyFormat("f(^{\n"
10574                "  @autoreleasepool {\n"
10575                "    if (a) {\n"
10576                "      g();\n"
10577                "    }\n"
10578                "  }\n"
10579                "});");
10580   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10581   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
10582                "};");
10583 
10584   FormatStyle FourIndent = getLLVMStyle();
10585   FourIndent.ObjCBlockIndentWidth = 4;
10586   verifyFormat("[operation setCompletionBlock:^{\n"
10587                "    [self onOperationDone];\n"
10588                "}];",
10589                FourIndent);
10590 }
10591 
10592 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10593   FormatStyle ZeroColumn = getLLVMStyle();
10594   ZeroColumn.ColumnLimit = 0;
10595 
10596   verifyFormat("[[SessionService sharedService] "
10597                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10598                "  if (window) {\n"
10599                "    [self windowDidLoad:window];\n"
10600                "  } else {\n"
10601                "    [self errorLoadingWindow];\n"
10602                "  }\n"
10603                "}];",
10604                ZeroColumn);
10605   EXPECT_EQ("[[SessionService sharedService]\n"
10606             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10607             "      if (window) {\n"
10608             "        [self windowDidLoad:window];\n"
10609             "      } else {\n"
10610             "        [self errorLoadingWindow];\n"
10611             "      }\n"
10612             "    }];",
10613             format("[[SessionService sharedService]\n"
10614                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10615                    "                if (window) {\n"
10616                    "    [self windowDidLoad:window];\n"
10617                    "  } else {\n"
10618                    "    [self errorLoadingWindow];\n"
10619                    "  }\n"
10620                    "}];",
10621                    ZeroColumn));
10622   verifyFormat("[myObject doSomethingWith:arg1\n"
10623                "    firstBlock:^(Foo *a) {\n"
10624                "      // ...\n"
10625                "      int i;\n"
10626                "    }\n"
10627                "    secondBlock:^(Bar *b) {\n"
10628                "      // ...\n"
10629                "      int i;\n"
10630                "    }\n"
10631                "    thirdBlock:^Foo(Bar *b) {\n"
10632                "      // ...\n"
10633                "      int i;\n"
10634                "    }];",
10635                ZeroColumn);
10636   verifyFormat("f(^{\n"
10637                "  @autoreleasepool {\n"
10638                "    if (a) {\n"
10639                "      g();\n"
10640                "    }\n"
10641                "  }\n"
10642                "});",
10643                ZeroColumn);
10644   verifyFormat("void (^largeBlock)(void) = ^{\n"
10645                "  // ...\n"
10646                "};",
10647                ZeroColumn);
10648 
10649   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10650   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10651             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10652   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10653   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10654             "  int i;\n"
10655             "};",
10656             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10657 }
10658 
10659 TEST_F(FormatTest, SupportsCRLF) {
10660   EXPECT_EQ("int a;\r\n"
10661             "int b;\r\n"
10662             "int c;\r\n",
10663             format("int a;\r\n"
10664                    "  int b;\r\n"
10665                    "    int c;\r\n",
10666                    getLLVMStyle()));
10667   EXPECT_EQ("int a;\r\n"
10668             "int b;\r\n"
10669             "int c;\r\n",
10670             format("int a;\r\n"
10671                    "  int b;\n"
10672                    "    int c;\r\n",
10673                    getLLVMStyle()));
10674   EXPECT_EQ("int a;\n"
10675             "int b;\n"
10676             "int c;\n",
10677             format("int a;\r\n"
10678                    "  int b;\n"
10679                    "    int c;\n",
10680                    getLLVMStyle()));
10681   EXPECT_EQ("\"aaaaaaa \"\r\n"
10682             "\"bbbbbbb\";\r\n",
10683             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10684   EXPECT_EQ("#define A \\\r\n"
10685             "  b;      \\\r\n"
10686             "  c;      \\\r\n"
10687             "  d;\r\n",
10688             format("#define A \\\r\n"
10689                    "  b; \\\r\n"
10690                    "  c; d; \r\n",
10691                    getGoogleStyle()));
10692 
10693   EXPECT_EQ("/*\r\n"
10694             "multi line block comments\r\n"
10695             "should not introduce\r\n"
10696             "an extra carriage return\r\n"
10697             "*/\r\n",
10698             format("/*\r\n"
10699                    "multi line block comments\r\n"
10700                    "should not introduce\r\n"
10701                    "an extra carriage return\r\n"
10702                    "*/\r\n"));
10703 }
10704 
10705 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10706   verifyFormat("MY_CLASS(C) {\n"
10707                "  int i;\n"
10708                "  int j;\n"
10709                "};");
10710 }
10711 
10712 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10713   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10714   TwoIndent.ContinuationIndentWidth = 2;
10715 
10716   EXPECT_EQ("int i =\n"
10717             "  longFunction(\n"
10718             "    arg);",
10719             format("int i = longFunction(arg);", TwoIndent));
10720 
10721   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10722   SixIndent.ContinuationIndentWidth = 6;
10723 
10724   EXPECT_EQ("int i =\n"
10725             "      longFunction(\n"
10726             "            arg);",
10727             format("int i = longFunction(arg);", SixIndent));
10728 }
10729 
10730 TEST_F(FormatTest, SpacesInAngles) {
10731   FormatStyle Spaces = getLLVMStyle();
10732   Spaces.SpacesInAngles = true;
10733 
10734   verifyFormat("static_cast< int >(arg);", Spaces);
10735   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10736   verifyFormat("f< int, float >();", Spaces);
10737   verifyFormat("template <> g() {}", Spaces);
10738   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10739   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10740   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10741                Spaces);
10742 
10743   Spaces.Standard = FormatStyle::LS_Cpp03;
10744   Spaces.SpacesInAngles = true;
10745   verifyFormat("A< A< int > >();", Spaces);
10746 
10747   Spaces.SpacesInAngles = false;
10748   verifyFormat("A<A<int> >();", Spaces);
10749 
10750   Spaces.Standard = FormatStyle::LS_Cpp11;
10751   Spaces.SpacesInAngles = true;
10752   verifyFormat("A< A< int > >();", Spaces);
10753 
10754   Spaces.SpacesInAngles = false;
10755   verifyFormat("A<A<int>>();", Spaces);
10756 }
10757 
10758 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
10759   FormatStyle Style = getLLVMStyle();
10760   Style.SpaceAfterTemplateKeyword = false;
10761   verifyFormat("template<int> void foo();", Style);
10762 }
10763 
10764 TEST_F(FormatTest, TripleAngleBrackets) {
10765   verifyFormat("f<<<1, 1>>>();");
10766   verifyFormat("f<<<1, 1, 1, s>>>();");
10767   verifyFormat("f<<<a, b, c, d>>>();");
10768   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10769   verifyFormat("f<param><<<1, 1>>>();");
10770   verifyFormat("f<1><<<1, 1>>>();");
10771   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10772   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10773                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10774   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
10775                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
10776 }
10777 
10778 TEST_F(FormatTest, MergeLessLessAtEnd) {
10779   verifyFormat("<<");
10780   EXPECT_EQ("< < <", format("\\\n<<<"));
10781   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10782                "aaallvm::outs() <<");
10783   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10784                "aaaallvm::outs()\n    <<");
10785 }
10786 
10787 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10788   std::string code = "#if A\n"
10789                      "#if B\n"
10790                      "a.\n"
10791                      "#endif\n"
10792                      "    a = 1;\n"
10793                      "#else\n"
10794                      "#endif\n"
10795                      "#if C\n"
10796                      "#else\n"
10797                      "#endif\n";
10798   EXPECT_EQ(code, format(code));
10799 }
10800 
10801 TEST_F(FormatTest, HandleConflictMarkers) {
10802   // Git/SVN conflict markers.
10803   EXPECT_EQ("int a;\n"
10804             "void f() {\n"
10805             "  callme(some(parameter1,\n"
10806             "<<<<<<< text by the vcs\n"
10807             "              parameter2),\n"
10808             "||||||| text by the vcs\n"
10809             "              parameter2),\n"
10810             "         parameter3,\n"
10811             "======= text by the vcs\n"
10812             "              parameter2, parameter3),\n"
10813             ">>>>>>> text by the vcs\n"
10814             "         otherparameter);\n",
10815             format("int a;\n"
10816                    "void f() {\n"
10817                    "  callme(some(parameter1,\n"
10818                    "<<<<<<< text by the vcs\n"
10819                    "  parameter2),\n"
10820                    "||||||| text by the vcs\n"
10821                    "  parameter2),\n"
10822                    "  parameter3,\n"
10823                    "======= text by the vcs\n"
10824                    "  parameter2,\n"
10825                    "  parameter3),\n"
10826                    ">>>>>>> text by the vcs\n"
10827                    "  otherparameter);\n"));
10828 
10829   // Perforce markers.
10830   EXPECT_EQ("void f() {\n"
10831             "  function(\n"
10832             ">>>> text by the vcs\n"
10833             "      parameter,\n"
10834             "==== text by the vcs\n"
10835             "      parameter,\n"
10836             "==== text by the vcs\n"
10837             "      parameter,\n"
10838             "<<<< text by the vcs\n"
10839             "      parameter);\n",
10840             format("void f() {\n"
10841                    "  function(\n"
10842                    ">>>> text by the vcs\n"
10843                    "  parameter,\n"
10844                    "==== text by the vcs\n"
10845                    "  parameter,\n"
10846                    "==== text by the vcs\n"
10847                    "  parameter,\n"
10848                    "<<<< text by the vcs\n"
10849                    "  parameter);\n"));
10850 
10851   EXPECT_EQ("<<<<<<<\n"
10852             "|||||||\n"
10853             "=======\n"
10854             ">>>>>>>",
10855             format("<<<<<<<\n"
10856                    "|||||||\n"
10857                    "=======\n"
10858                    ">>>>>>>"));
10859 
10860   EXPECT_EQ("<<<<<<<\n"
10861             "|||||||\n"
10862             "int i;\n"
10863             "=======\n"
10864             ">>>>>>>",
10865             format("<<<<<<<\n"
10866                    "|||||||\n"
10867                    "int i;\n"
10868                    "=======\n"
10869                    ">>>>>>>"));
10870 
10871   // FIXME: Handle parsing of macros around conflict markers correctly:
10872   EXPECT_EQ("#define Macro \\\n"
10873             "<<<<<<<\n"
10874             "Something \\\n"
10875             "|||||||\n"
10876             "Else \\\n"
10877             "=======\n"
10878             "Other \\\n"
10879             ">>>>>>>\n"
10880             "    End int i;\n",
10881             format("#define Macro \\\n"
10882                    "<<<<<<<\n"
10883                    "  Something \\\n"
10884                    "|||||||\n"
10885                    "  Else \\\n"
10886                    "=======\n"
10887                    "  Other \\\n"
10888                    ">>>>>>>\n"
10889                    "  End\n"
10890                    "int i;\n"));
10891 }
10892 
10893 TEST_F(FormatTest, DisableRegions) {
10894   EXPECT_EQ("int i;\n"
10895             "// clang-format off\n"
10896             "  int j;\n"
10897             "// clang-format on\n"
10898             "int k;",
10899             format(" int  i;\n"
10900                    "   // clang-format off\n"
10901                    "  int j;\n"
10902                    " // clang-format on\n"
10903                    "   int   k;"));
10904   EXPECT_EQ("int i;\n"
10905             "/* clang-format off */\n"
10906             "  int j;\n"
10907             "/* clang-format on */\n"
10908             "int k;",
10909             format(" int  i;\n"
10910                    "   /* clang-format off */\n"
10911                    "  int j;\n"
10912                    " /* clang-format on */\n"
10913                    "   int   k;"));
10914 }
10915 
10916 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10917   format("? ) =");
10918   verifyNoCrash("#define a\\\n /**/}");
10919 }
10920 
10921 TEST_F(FormatTest, FormatsTableGenCode) {
10922   FormatStyle Style = getLLVMStyle();
10923   Style.Language = FormatStyle::LK_TableGen;
10924   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10925 }
10926 
10927 TEST_F(FormatTest, ArrayOfTemplates) {
10928   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10929             format("auto a = new unique_ptr<int > [ 10];"));
10930 
10931   FormatStyle Spaces = getLLVMStyle();
10932   Spaces.SpacesInSquareBrackets = true;
10933   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10934             format("auto a = new unique_ptr<int > [10];", Spaces));
10935 }
10936 
10937 TEST_F(FormatTest, ArrayAsTemplateType) {
10938   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10939             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10940 
10941   FormatStyle Spaces = getLLVMStyle();
10942   Spaces.SpacesInSquareBrackets = true;
10943   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10944             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10945 }
10946 
10947 TEST(FormatStyle, GetStyleOfFile) {
10948   vfs::InMemoryFileSystem FS;
10949   // Test 1: format file in the same directory.
10950   ASSERT_TRUE(
10951       FS.addFile("/a/.clang-format", 0,
10952                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10953   ASSERT_TRUE(
10954       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10955   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10956   ASSERT_EQ(Style1, getLLVMStyle());
10957 
10958   // Test 2: fallback to default.
10959   ASSERT_TRUE(
10960       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10961   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10962   ASSERT_EQ(Style2, getMozillaStyle());
10963 
10964   // Test 3: format file in parent directory.
10965   ASSERT_TRUE(
10966       FS.addFile("/c/.clang-format", 0,
10967                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
10968   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
10969                          llvm::MemoryBuffer::getMemBuffer("int i;")));
10970   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
10971   ASSERT_EQ(Style3, getGoogleStyle());
10972 }
10973 
10974 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
10975   // Column limit is 20.
10976   std::string Code = "Type *a =\n"
10977                      "    new Type();\n"
10978                      "g(iiiii, 0, jjjjj,\n"
10979                      "  0, kkkkk, 0, mm);\n"
10980                      "int  bad     = format   ;";
10981   std::string Expected = "auto a = new Type();\n"
10982                          "g(iiiii, nullptr,\n"
10983                          "  jjjjj, nullptr,\n"
10984                          "  kkkkk, nullptr,\n"
10985                          "  mm);\n"
10986                          "int  bad     = format   ;";
10987   FileID ID = Context.createInMemoryFile("format.cpp", Code);
10988   tooling::Replacements Replaces = toReplacements(
10989       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
10990                             "auto "),
10991        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
10992                             "nullptr"),
10993        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
10994                             "nullptr"),
10995        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
10996                             "nullptr")});
10997 
10998   format::FormatStyle Style = format::getLLVMStyle();
10999   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11000   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11001   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11002       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11003   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11004   EXPECT_TRUE(static_cast<bool>(Result));
11005   EXPECT_EQ(Expected, *Result);
11006 }
11007 
11008 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11009   std::string Code = "#include \"a.h\"\n"
11010                      "#include \"c.h\"\n"
11011                      "\n"
11012                      "int main() {\n"
11013                      "  return 0;\n"
11014                      "}";
11015   std::string Expected = "#include \"a.h\"\n"
11016                          "#include \"b.h\"\n"
11017                          "#include \"c.h\"\n"
11018                          "\n"
11019                          "int main() {\n"
11020                          "  return 0;\n"
11021                          "}";
11022   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11023   tooling::Replacements Replaces = toReplacements(
11024       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11025                             "#include \"b.h\"\n")});
11026 
11027   format::FormatStyle Style = format::getLLVMStyle();
11028   Style.SortIncludes = true;
11029   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11030   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11031       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11032   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11033   EXPECT_TRUE(static_cast<bool>(Result));
11034   EXPECT_EQ(Expected, *Result);
11035 }
11036 
11037 TEST_F(FormatTest, AllignTrailingComments) {
11038   EXPECT_EQ("#define MACRO(V)                       \\\n"
11039             "  V(Rt2) /* one more char */           \\\n"
11040             "  V(Rs)  /* than here  */              \\\n"
11041             "/* comment 3 */\n",
11042             format("#define MACRO(V)\\\n"
11043                    "V(Rt2)  /* one more char */ \\\n"
11044                    "V(Rs) /* than here  */    \\\n"
11045                    "/* comment 3 */         \\\n",
11046                    getLLVMStyleWithColumns(40)));
11047 }
11048 } // end namespace
11049 } // end namespace format
11050 } // end namespace clang
11051