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 
281 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
282   verifyFormat("x = (a) and (b);");
283   verifyFormat("x = (a) or (b);");
284   verifyFormat("x = (a) bitand (b);");
285   verifyFormat("x = (a) bitor (b);");
286   verifyFormat("x = (a) not_eq (b);");
287   verifyFormat("x = (a) and_eq (b);");
288   verifyFormat("x = (a) or_eq (b);");
289   verifyFormat("x = (a) xor (b);");
290 }
291 
292 //===----------------------------------------------------------------------===//
293 // Tests for control statements.
294 //===----------------------------------------------------------------------===//
295 
296 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
297   verifyFormat("if (true)\n  f();\ng();");
298   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
299   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
300 
301   FormatStyle AllowsMergedIf = getLLVMStyle();
302   AllowsMergedIf.AlignEscapedNewlinesLeft = true;
303   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
304   verifyFormat("if (a)\n"
305                "  // comment\n"
306                "  f();",
307                AllowsMergedIf);
308   verifyFormat("{\n"
309                "  if (a)\n"
310                "  label:\n"
311                "    f();\n"
312                "}",
313                AllowsMergedIf);
314   verifyFormat("#define A \\\n"
315                "  if (a)  \\\n"
316                "  label:  \\\n"
317                "    f()",
318                AllowsMergedIf);
319   verifyFormat("if (a)\n"
320                "  ;",
321                AllowsMergedIf);
322   verifyFormat("if (a)\n"
323                "  if (b) return;",
324                AllowsMergedIf);
325 
326   verifyFormat("if (a) // Can't merge this\n"
327                "  f();\n",
328                AllowsMergedIf);
329   verifyFormat("if (a) /* still don't merge */\n"
330                "  f();",
331                AllowsMergedIf);
332   verifyFormat("if (a) { // Never merge this\n"
333                "  f();\n"
334                "}",
335                AllowsMergedIf);
336   verifyFormat("if (a) { /* Never merge this */\n"
337                "  f();\n"
338                "}",
339                AllowsMergedIf);
340 
341   AllowsMergedIf.ColumnLimit = 14;
342   verifyFormat("if (a) return;", AllowsMergedIf);
343   verifyFormat("if (aaaaaaaaa)\n"
344                "  return;",
345                AllowsMergedIf);
346 
347   AllowsMergedIf.ColumnLimit = 13;
348   verifyFormat("if (a)\n  return;", AllowsMergedIf);
349 }
350 
351 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
352   FormatStyle AllowsMergedLoops = getLLVMStyle();
353   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
354   verifyFormat("while (true) continue;", AllowsMergedLoops);
355   verifyFormat("for (;;) continue;", AllowsMergedLoops);
356   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
357   verifyFormat("while (true)\n"
358                "  ;",
359                AllowsMergedLoops);
360   verifyFormat("for (;;)\n"
361                "  ;",
362                AllowsMergedLoops);
363   verifyFormat("for (;;)\n"
364                "  for (;;) continue;",
365                AllowsMergedLoops);
366   verifyFormat("for (;;) // Can't merge this\n"
367                "  continue;",
368                AllowsMergedLoops);
369   verifyFormat("for (;;) /* still don't merge */\n"
370                "  continue;",
371                AllowsMergedLoops);
372 }
373 
374 TEST_F(FormatTest, FormatShortBracedStatements) {
375   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
376   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
377 
378   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
379   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
380 
381   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
382   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
383   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
384   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
385   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
386   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
387   verifyFormat("if (true) { //\n"
388                "  f();\n"
389                "}",
390                AllowSimpleBracedStatements);
391   verifyFormat("if (true) {\n"
392                "  f();\n"
393                "  f();\n"
394                "}",
395                AllowSimpleBracedStatements);
396   verifyFormat("if (true) {\n"
397                "  f();\n"
398                "} else {\n"
399                "  f();\n"
400                "}",
401                AllowSimpleBracedStatements);
402 
403   verifyFormat("template <int> struct A2 {\n"
404                "  struct B {};\n"
405                "};",
406                AllowSimpleBracedStatements);
407 
408   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
409   verifyFormat("if (true) {\n"
410                "  f();\n"
411                "}",
412                AllowSimpleBracedStatements);
413   verifyFormat("if (true) {\n"
414                "  f();\n"
415                "} else {\n"
416                "  f();\n"
417                "}",
418                AllowSimpleBracedStatements);
419 
420   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
421   verifyFormat("while (true) {\n"
422                "  f();\n"
423                "}",
424                AllowSimpleBracedStatements);
425   verifyFormat("for (;;) {\n"
426                "  f();\n"
427                "}",
428                AllowSimpleBracedStatements);
429 }
430 
431 TEST_F(FormatTest, ParseIfElse) {
432   verifyFormat("if (true)\n"
433                "  if (true)\n"
434                "    if (true)\n"
435                "      f();\n"
436                "    else\n"
437                "      g();\n"
438                "  else\n"
439                "    h();\n"
440                "else\n"
441                "  i();");
442   verifyFormat("if (true)\n"
443                "  if (true)\n"
444                "    if (true) {\n"
445                "      if (true)\n"
446                "        f();\n"
447                "    } else {\n"
448                "      g();\n"
449                "    }\n"
450                "  else\n"
451                "    h();\n"
452                "else {\n"
453                "  i();\n"
454                "}");
455   verifyFormat("void f() {\n"
456                "  if (a) {\n"
457                "  } else {\n"
458                "  }\n"
459                "}");
460 }
461 
462 TEST_F(FormatTest, ElseIf) {
463   verifyFormat("if (a) {\n} else if (b) {\n}");
464   verifyFormat("if (a)\n"
465                "  f();\n"
466                "else if (b)\n"
467                "  g();\n"
468                "else\n"
469                "  h();");
470   verifyFormat("if (a) {\n"
471                "  f();\n"
472                "}\n"
473                "// or else ..\n"
474                "else {\n"
475                "  g()\n"
476                "}");
477 
478   verifyFormat("if (a) {\n"
479                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
480                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
481                "}");
482   verifyFormat("if (a) {\n"
483                "} else if (\n"
484                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
485                "}",
486                getLLVMStyleWithColumns(62));
487 }
488 
489 TEST_F(FormatTest, FormatsForLoop) {
490   verifyFormat(
491       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
492       "     ++VeryVeryLongLoopVariable)\n"
493       "  ;");
494   verifyFormat("for (;;)\n"
495                "  f();");
496   verifyFormat("for (;;) {\n}");
497   verifyFormat("for (;;) {\n"
498                "  f();\n"
499                "}");
500   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
501 
502   verifyFormat(
503       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
504       "                                          E = UnwrappedLines.end();\n"
505       "     I != E; ++I) {\n}");
506 
507   verifyFormat(
508       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
509       "     ++IIIII) {\n}");
510   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
511                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
512                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
513   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
514                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
515                "         E = FD->getDeclsInPrototypeScope().end();\n"
516                "     I != E; ++I) {\n}");
517   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
518                "         I = Container.begin(),\n"
519                "         E = Container.end();\n"
520                "     I != E; ++I) {\n}",
521                getLLVMStyleWithColumns(76));
522 
523   verifyFormat(
524       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
525       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
526       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
527       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
528       "     ++aaaaaaaaaaa) {\n}");
529   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
530                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
531                "     ++i) {\n}");
532   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
533                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
534                "}");
535   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
536                "         aaaaaaaaaa);\n"
537                "     iter; ++iter) {\n"
538                "}");
539   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
540                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
541                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
542                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
543 
544   FormatStyle NoBinPacking = getLLVMStyle();
545   NoBinPacking.BinPackParameters = false;
546   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
547                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
548                "                                           aaaaaaaaaaaaaaaa,\n"
549                "                                           aaaaaaaaaaaaaaaa,\n"
550                "                                           aaaaaaaaaaaaaaaa);\n"
551                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
552                "}",
553                NoBinPacking);
554   verifyFormat(
555       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
556       "                                          E = UnwrappedLines.end();\n"
557       "     I != E;\n"
558       "     ++I) {\n}",
559       NoBinPacking);
560 }
561 
562 TEST_F(FormatTest, RangeBasedForLoops) {
563   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
564                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
565   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
566                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
567   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
568                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
569   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
570                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
571 }
572 
573 TEST_F(FormatTest, ForEachLoops) {
574   verifyFormat("void f() {\n"
575                "  foreach (Item *item, itemlist) {}\n"
576                "  Q_FOREACH (Item *item, itemlist) {}\n"
577                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
578                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
579                "}");
580 
581   // As function-like macros.
582   verifyFormat("#define foreach(x, y)\n"
583                "#define Q_FOREACH(x, y)\n"
584                "#define BOOST_FOREACH(x, y)\n"
585                "#define UNKNOWN_FOREACH(x, y)\n");
586 
587   // Not as function-like macros.
588   verifyFormat("#define foreach (x, y)\n"
589                "#define Q_FOREACH (x, y)\n"
590                "#define BOOST_FOREACH (x, y)\n"
591                "#define UNKNOWN_FOREACH (x, y)\n");
592 }
593 
594 TEST_F(FormatTest, FormatsWhileLoop) {
595   verifyFormat("while (true) {\n}");
596   verifyFormat("while (true)\n"
597                "  f();");
598   verifyFormat("while () {\n}");
599   verifyFormat("while () {\n"
600                "  f();\n"
601                "}");
602 }
603 
604 TEST_F(FormatTest, FormatsDoWhile) {
605   verifyFormat("do {\n"
606                "  do_something();\n"
607                "} while (something());");
608   verifyFormat("do\n"
609                "  do_something();\n"
610                "while (something());");
611 }
612 
613 TEST_F(FormatTest, FormatsSwitchStatement) {
614   verifyFormat("switch (x) {\n"
615                "case 1:\n"
616                "  f();\n"
617                "  break;\n"
618                "case kFoo:\n"
619                "case ns::kBar:\n"
620                "case kBaz:\n"
621                "  break;\n"
622                "default:\n"
623                "  g();\n"
624                "  break;\n"
625                "}");
626   verifyFormat("switch (x) {\n"
627                "case 1: {\n"
628                "  f();\n"
629                "  break;\n"
630                "}\n"
631                "case 2: {\n"
632                "  break;\n"
633                "}\n"
634                "}");
635   verifyFormat("switch (x) {\n"
636                "case 1: {\n"
637                "  f();\n"
638                "  {\n"
639                "    g();\n"
640                "    h();\n"
641                "  }\n"
642                "  break;\n"
643                "}\n"
644                "}");
645   verifyFormat("switch (x) {\n"
646                "case 1: {\n"
647                "  f();\n"
648                "  if (foo) {\n"
649                "    g();\n"
650                "    h();\n"
651                "  }\n"
652                "  break;\n"
653                "}\n"
654                "}");
655   verifyFormat("switch (x) {\n"
656                "case 1: {\n"
657                "  f();\n"
658                "  g();\n"
659                "} break;\n"
660                "}");
661   verifyFormat("switch (test)\n"
662                "  ;");
663   verifyFormat("switch (x) {\n"
664                "default: {\n"
665                "  // Do nothing.\n"
666                "}\n"
667                "}");
668   verifyFormat("switch (x) {\n"
669                "// comment\n"
670                "// if 1, do f()\n"
671                "case 1:\n"
672                "  f();\n"
673                "}");
674   verifyFormat("switch (x) {\n"
675                "case 1:\n"
676                "  // Do amazing stuff\n"
677                "  {\n"
678                "    f();\n"
679                "    g();\n"
680                "  }\n"
681                "  break;\n"
682                "}");
683   verifyFormat("#define A          \\\n"
684                "  switch (x) {     \\\n"
685                "  case a:          \\\n"
686                "    foo = b;       \\\n"
687                "  }",
688                getLLVMStyleWithColumns(20));
689   verifyFormat("#define OPERATION_CASE(name)           \\\n"
690                "  case OP_name:                        \\\n"
691                "    return operations::Operation##name\n",
692                getLLVMStyleWithColumns(40));
693   verifyFormat("switch (x) {\n"
694                "case 1:;\n"
695                "default:;\n"
696                "  int i;\n"
697                "}");
698 
699   verifyGoogleFormat("switch (x) {\n"
700                      "  case 1:\n"
701                      "    f();\n"
702                      "    break;\n"
703                      "  case kFoo:\n"
704                      "  case ns::kBar:\n"
705                      "  case kBaz:\n"
706                      "    break;\n"
707                      "  default:\n"
708                      "    g();\n"
709                      "    break;\n"
710                      "}");
711   verifyGoogleFormat("switch (x) {\n"
712                      "  case 1: {\n"
713                      "    f();\n"
714                      "    break;\n"
715                      "  }\n"
716                      "}");
717   verifyGoogleFormat("switch (test)\n"
718                      "  ;");
719 
720   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
721                      "  case OP_name:              \\\n"
722                      "    return operations::Operation##name\n");
723   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
724                      "  // Get the correction operation class.\n"
725                      "  switch (OpCode) {\n"
726                      "    CASE(Add);\n"
727                      "    CASE(Subtract);\n"
728                      "    default:\n"
729                      "      return operations::Unknown;\n"
730                      "  }\n"
731                      "#undef OPERATION_CASE\n"
732                      "}");
733   verifyFormat("DEBUG({\n"
734                "  switch (x) {\n"
735                "  case A:\n"
736                "    f();\n"
737                "    break;\n"
738                "  // On B:\n"
739                "  case B:\n"
740                "    g();\n"
741                "    break;\n"
742                "  }\n"
743                "});");
744   verifyFormat("switch (a) {\n"
745                "case (b):\n"
746                "  return;\n"
747                "}");
748 
749   verifyFormat("switch (a) {\n"
750                "case some_namespace::\n"
751                "    some_constant:\n"
752                "  return;\n"
753                "}",
754                getLLVMStyleWithColumns(34));
755 }
756 
757 TEST_F(FormatTest, CaseRanges) {
758   verifyFormat("switch (x) {\n"
759                "case 'A' ... 'Z':\n"
760                "case 1 ... 5:\n"
761                "case a ... b:\n"
762                "  break;\n"
763                "}");
764 }
765 
766 TEST_F(FormatTest, ShortCaseLabels) {
767   FormatStyle Style = getLLVMStyle();
768   Style.AllowShortCaseLabelsOnASingleLine = true;
769   verifyFormat("switch (a) {\n"
770                "case 1: x = 1; break;\n"
771                "case 2: return;\n"
772                "case 3:\n"
773                "case 4:\n"
774                "case 5: return;\n"
775                "case 6: // comment\n"
776                "  return;\n"
777                "case 7:\n"
778                "  // comment\n"
779                "  return;\n"
780                "case 8:\n"
781                "  x = 8; // comment\n"
782                "  break;\n"
783                "default: y = 1; break;\n"
784                "}",
785                Style);
786   verifyFormat("switch (a) {\n"
787                "#if FOO\n"
788                "case 0: return 0;\n"
789                "#endif\n"
790                "}",
791                Style);
792   verifyFormat("switch (a) {\n"
793                "case 1: {\n"
794                "}\n"
795                "case 2: {\n"
796                "  return;\n"
797                "}\n"
798                "case 3: {\n"
799                "  x = 1;\n"
800                "  return;\n"
801                "}\n"
802                "case 4:\n"
803                "  if (x)\n"
804                "    return;\n"
805                "}",
806                Style);
807   Style.ColumnLimit = 21;
808   verifyFormat("switch (a) {\n"
809                "case 1: x = 1; break;\n"
810                "case 2: return;\n"
811                "case 3:\n"
812                "case 4:\n"
813                "case 5: return;\n"
814                "default:\n"
815                "  y = 1;\n"
816                "  break;\n"
817                "}",
818                Style);
819 }
820 
821 TEST_F(FormatTest, FormatsLabels) {
822   verifyFormat("void f() {\n"
823                "  some_code();\n"
824                "test_label:\n"
825                "  some_other_code();\n"
826                "  {\n"
827                "    some_more_code();\n"
828                "  another_label:\n"
829                "    some_more_code();\n"
830                "  }\n"
831                "}");
832   verifyFormat("{\n"
833                "  some_code();\n"
834                "test_label:\n"
835                "  some_other_code();\n"
836                "}");
837   verifyFormat("{\n"
838                "  some_code();\n"
839                "test_label:;\n"
840                "  int i = 0;\n"
841                "}");
842 }
843 
844 //===----------------------------------------------------------------------===//
845 // Tests for comments.
846 //===----------------------------------------------------------------------===//
847 
848 TEST_F(FormatTest, UnderstandsSingleLineComments) {
849   verifyFormat("//* */");
850   verifyFormat("// line 1\n"
851                "// line 2\n"
852                "void f() {}\n");
853 
854   verifyFormat("void f() {\n"
855                "  // Doesn't do anything\n"
856                "}");
857   verifyFormat("SomeObject\n"
858                "    // Calling someFunction on SomeObject\n"
859                "    .someFunction();");
860   verifyFormat("auto result = SomeObject\n"
861                "                  // Calling someFunction on SomeObject\n"
862                "                  .someFunction();");
863   verifyFormat("void f(int i,  // some comment (probably for i)\n"
864                "       int j,  // some comment (probably for j)\n"
865                "       int k); // some comment (probably for k)");
866   verifyFormat("void f(int i,\n"
867                "       // some comment (probably for j)\n"
868                "       int j,\n"
869                "       // some comment (probably for k)\n"
870                "       int k);");
871 
872   verifyFormat("int i    // This is a fancy variable\n"
873                "    = 5; // with nicely aligned comment.");
874 
875   verifyFormat("// Leading comment.\n"
876                "int a; // Trailing comment.");
877   verifyFormat("int a; // Trailing comment\n"
878                "       // on 2\n"
879                "       // or 3 lines.\n"
880                "int b;");
881   verifyFormat("int a; // Trailing comment\n"
882                "\n"
883                "// Leading comment.\n"
884                "int b;");
885   verifyFormat("int a;    // Comment.\n"
886                "          // More details.\n"
887                "int bbbb; // Another comment.");
888   verifyFormat(
889       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
890       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
891       "int cccccccccccccccccccccccccccccc;       // comment\n"
892       "int ddd;                     // looooooooooooooooooooooooong comment\n"
893       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
894       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
895       "int ccccccccccccccccccc;     // comment");
896 
897   verifyFormat("#include \"a\"     // comment\n"
898                "#include \"a/b/c\" // comment");
899   verifyFormat("#include <a>     // comment\n"
900                "#include <a/b/c> // comment");
901   EXPECT_EQ("#include \"a\"     // comment\n"
902             "#include \"a/b/c\" // comment",
903             format("#include \\\n"
904                    "  \"a\" // comment\n"
905                    "#include \"a/b/c\" // comment"));
906 
907   verifyFormat("enum E {\n"
908                "  // comment\n"
909                "  VAL_A, // comment\n"
910                "  VAL_B\n"
911                "};");
912 
913   verifyFormat(
914       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
915       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
916   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
917                "    // Comment inside a statement.\n"
918                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
919   verifyFormat("SomeFunction(a,\n"
920                "             // comment\n"
921                "             b + x);");
922   verifyFormat("SomeFunction(a, a,\n"
923                "             // comment\n"
924                "             b + x);");
925   verifyFormat(
926       "bool aaaaaaaaaaaaa = // comment\n"
927       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
928       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
929 
930   verifyFormat("int aaaa; // aaaaa\n"
931                "int aa;   // aaaaaaa",
932                getLLVMStyleWithColumns(20));
933 
934   EXPECT_EQ("void f() { // This does something ..\n"
935             "}\n"
936             "int a; // This is unrelated",
937             format("void f()    {     // This does something ..\n"
938                    "  }\n"
939                    "int   a;     // This is unrelated"));
940   EXPECT_EQ("class C {\n"
941             "  void f() { // This does something ..\n"
942             "  }          // awesome..\n"
943             "\n"
944             "  int a; // This is unrelated\n"
945             "};",
946             format("class C{void f()    { // This does something ..\n"
947                    "      } // awesome..\n"
948                    " \n"
949                    "int a;    // This is unrelated\n"
950                    "};"));
951 
952   EXPECT_EQ("int i; // single line trailing comment",
953             format("int i;\\\n// single line trailing comment"));
954 
955   verifyGoogleFormat("int a;  // Trailing comment.");
956 
957   verifyFormat("someFunction(anotherFunction( // Force break.\n"
958                "    parameter));");
959 
960   verifyGoogleFormat("#endif  // HEADER_GUARD");
961 
962   verifyFormat("const char *test[] = {\n"
963                "    // A\n"
964                "    \"aaaa\",\n"
965                "    // B\n"
966                "    \"aaaaa\"};");
967   verifyGoogleFormat(
968       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
969       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
970   EXPECT_EQ("D(a, {\n"
971             "  // test\n"
972             "  int a;\n"
973             "});",
974             format("D(a, {\n"
975                    "// test\n"
976                    "int a;\n"
977                    "});"));
978 
979   EXPECT_EQ("lineWith(); // comment\n"
980             "// at start\n"
981             "otherLine();",
982             format("lineWith();   // comment\n"
983                    "// at start\n"
984                    "otherLine();"));
985   EXPECT_EQ("lineWith(); // comment\n"
986             "/*\n"
987             " * at start */\n"
988             "otherLine();",
989             format("lineWith();   // comment\n"
990                    "/*\n"
991                    " * at start */\n"
992                    "otherLine();"));
993   EXPECT_EQ("lineWith(); // comment\n"
994             "            // at start\n"
995             "otherLine();",
996             format("lineWith();   // comment\n"
997                    " // at start\n"
998                    "otherLine();"));
999 
1000   EXPECT_EQ("lineWith(); // comment\n"
1001             "// at start\n"
1002             "otherLine(); // comment",
1003             format("lineWith();   // comment\n"
1004                    "// at start\n"
1005                    "otherLine();   // comment"));
1006   EXPECT_EQ("lineWith();\n"
1007             "// at start\n"
1008             "otherLine(); // comment",
1009             format("lineWith();\n"
1010                    " // at start\n"
1011                    "otherLine();   // comment"));
1012   EXPECT_EQ("// first\n"
1013             "// at start\n"
1014             "otherLine(); // comment",
1015             format("// first\n"
1016                    " // at start\n"
1017                    "otherLine();   // comment"));
1018   EXPECT_EQ("f();\n"
1019             "// first\n"
1020             "// at start\n"
1021             "otherLine(); // comment",
1022             format("f();\n"
1023                    "// first\n"
1024                    " // at start\n"
1025                    "otherLine();   // comment"));
1026   verifyFormat("f(); // comment\n"
1027                "// first\n"
1028                "// at start\n"
1029                "otherLine();");
1030   EXPECT_EQ("f(); // comment\n"
1031             "// first\n"
1032             "// at start\n"
1033             "otherLine();",
1034             format("f();   // comment\n"
1035                    "// first\n"
1036                    " // at start\n"
1037                    "otherLine();"));
1038   EXPECT_EQ("f(); // comment\n"
1039             "     // first\n"
1040             "// at start\n"
1041             "otherLine();",
1042             format("f();   // comment\n"
1043                    " // first\n"
1044                    "// at start\n"
1045                    "otherLine();"));
1046   EXPECT_EQ("void f() {\n"
1047             "  lineWith(); // comment\n"
1048             "  // at start\n"
1049             "}",
1050             format("void              f() {\n"
1051                    "  lineWith(); // comment\n"
1052                    "  // at start\n"
1053                    "}"));
1054   EXPECT_EQ("int xy; // a\n"
1055             "int z;  // b",
1056             format("int xy;    // a\n"
1057                    "int z;    //b"));
1058   EXPECT_EQ("int xy; // a\n"
1059             "int z; // bb",
1060             format("int xy;    // a\n"
1061                    "int z;    //bb",
1062                    getLLVMStyleWithColumns(12)));
1063 
1064   verifyFormat("#define A                                                  \\\n"
1065                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1066                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1067                getLLVMStyleWithColumns(60));
1068   verifyFormat(
1069       "#define A                                                   \\\n"
1070       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1071       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1072       getLLVMStyleWithColumns(61));
1073 
1074   verifyFormat("if ( // This is some comment\n"
1075                "    x + 3) {\n"
1076                "}");
1077   EXPECT_EQ("if ( // This is some comment\n"
1078             "     // spanning two lines\n"
1079             "    x + 3) {\n"
1080             "}",
1081             format("if( // This is some comment\n"
1082                    "     // spanning two lines\n"
1083                    " x + 3) {\n"
1084                    "}"));
1085 
1086   verifyNoCrash("/\\\n/");
1087   verifyNoCrash("/\\\n* */");
1088   // The 0-character somehow makes the lexer return a proper comment.
1089   verifyNoCrash(StringRef("/*\\\0\n/", 6));
1090 }
1091 
1092 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1093   EXPECT_EQ("SomeFunction(a,\n"
1094             "             b, // comment\n"
1095             "             c);",
1096             format("SomeFunction(a,\n"
1097                    "          b, // comment\n"
1098                    "      c);"));
1099   EXPECT_EQ("SomeFunction(a, b,\n"
1100             "             // comment\n"
1101             "             c);",
1102             format("SomeFunction(a,\n"
1103                    "          b,\n"
1104                    "  // comment\n"
1105                    "      c);"));
1106   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1107             "             c);",
1108             format("SomeFunction(a, b, // comment (unclear relation)\n"
1109                    "      c);"));
1110   EXPECT_EQ("SomeFunction(a, // comment\n"
1111             "             b,\n"
1112             "             c); // comment",
1113             format("SomeFunction(a,     // comment\n"
1114                    "          b,\n"
1115                    "      c); // comment"));
1116 }
1117 
1118 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1119   EXPECT_EQ("// comment", format("// comment  "));
1120   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1121             format("int aaaaaaa, bbbbbbb; // comment                   ",
1122                    getLLVMStyleWithColumns(33)));
1123   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1124   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1125 }
1126 
1127 TEST_F(FormatTest, UnderstandsBlockComments) {
1128   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1129   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y, /*c=*/::c); }");
1130   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1131             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1132             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1133                    "/* Trailing comment for aa... */\n"
1134                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1135   EXPECT_EQ(
1136       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1137       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1138       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1139              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1140   EXPECT_EQ(
1141       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1142       "    aaaaaaaaaaaaaaaaaa,\n"
1143       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1144       "}",
1145       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1146              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1147              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1148              "}"));
1149   verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
1150                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
1151 
1152   FormatStyle NoBinPacking = getLLVMStyle();
1153   NoBinPacking.BinPackParameters = false;
1154   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1155                "         /* parameter 2 */ aaaaaa,\n"
1156                "         /* parameter 3 */ aaaaaa,\n"
1157                "         /* parameter 4 */ aaaaaa);",
1158                NoBinPacking);
1159 
1160   // Aligning block comments in macros.
1161   verifyGoogleFormat("#define A        \\\n"
1162                      "  int i;   /*a*/ \\\n"
1163                      "  int jjj; /*b*/");
1164 }
1165 
1166 TEST_F(FormatTest, AlignsBlockComments) {
1167   EXPECT_EQ("/*\n"
1168             " * Really multi-line\n"
1169             " * comment.\n"
1170             " */\n"
1171             "void f() {}",
1172             format("  /*\n"
1173                    "   * Really multi-line\n"
1174                    "   * comment.\n"
1175                    "   */\n"
1176                    "  void f() {}"));
1177   EXPECT_EQ("class C {\n"
1178             "  /*\n"
1179             "   * Another multi-line\n"
1180             "   * comment.\n"
1181             "   */\n"
1182             "  void f() {}\n"
1183             "};",
1184             format("class C {\n"
1185                    "/*\n"
1186                    " * Another multi-line\n"
1187                    " * comment.\n"
1188                    " */\n"
1189                    "void f() {}\n"
1190                    "};"));
1191   EXPECT_EQ("/*\n"
1192             "  1. This is a comment with non-trivial formatting.\n"
1193             "     1.1. We have to indent/outdent all lines equally\n"
1194             "         1.1.1. to keep the formatting.\n"
1195             " */",
1196             format("  /*\n"
1197                    "    1. This is a comment with non-trivial formatting.\n"
1198                    "       1.1. We have to indent/outdent all lines equally\n"
1199                    "           1.1.1. to keep the formatting.\n"
1200                    "   */"));
1201   EXPECT_EQ("/*\n"
1202             "Don't try to outdent if there's not enough indentation.\n"
1203             "*/",
1204             format("  /*\n"
1205                    " Don't try to outdent if there's not enough indentation.\n"
1206                    " */"));
1207 
1208   EXPECT_EQ("int i; /* Comment with empty...\n"
1209             "        *\n"
1210             "        * line. */",
1211             format("int i; /* Comment with empty...\n"
1212                    "        *\n"
1213                    "        * line. */"));
1214   EXPECT_EQ("int foobar = 0; /* comment */\n"
1215             "int bar = 0;    /* multiline\n"
1216             "                   comment 1 */\n"
1217             "int baz = 0;    /* multiline\n"
1218             "                   comment 2 */\n"
1219             "int bzz = 0;    /* multiline\n"
1220             "                   comment 3 */",
1221             format("int foobar = 0; /* comment */\n"
1222                    "int bar = 0;    /* multiline\n"
1223                    "                   comment 1 */\n"
1224                    "int baz = 0; /* multiline\n"
1225                    "                comment 2 */\n"
1226                    "int bzz = 0;         /* multiline\n"
1227                    "                        comment 3 */"));
1228   EXPECT_EQ("int foobar = 0; /* comment */\n"
1229             "int bar = 0;    /* multiline\n"
1230             "   comment */\n"
1231             "int baz = 0;    /* multiline\n"
1232             "comment */",
1233             format("int foobar = 0; /* comment */\n"
1234                    "int bar = 0; /* multiline\n"
1235                    "comment */\n"
1236                    "int baz = 0;        /* multiline\n"
1237                    "comment */"));
1238 }
1239 
1240 TEST_F(FormatTest, CommentReflowingCanBeTurnedOff) {
1241   FormatStyle Style = getLLVMStyleWithColumns(20);
1242   Style.ReflowComments = false;
1243   verifyFormat("// aaaaaaaaa aaaaaaaaaa aaaaaaaaaa", Style);
1244   verifyFormat("/* aaaaaaaaa aaaaaaaaaa aaaaaaaaaa */", Style);
1245 }
1246 
1247 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1248   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1249             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1250             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1251                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1252   EXPECT_EQ(
1253       "void ffffffffffff(\n"
1254       "    int aaaaaaaa, int bbbbbbbb,\n"
1255       "    int cccccccccccc) { /*\n"
1256       "                           aaaaaaaaaa\n"
1257       "                           aaaaaaaaaaaaa\n"
1258       "                           bbbbbbbbbbbbbb\n"
1259       "                           bbbbbbbbbb\n"
1260       "                         */\n"
1261       "}",
1262       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1263              "{ /*\n"
1264              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1265              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1266              "   */\n"
1267              "}",
1268              getLLVMStyleWithColumns(40)));
1269 }
1270 
1271 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1272   EXPECT_EQ("void ffffffffff(\n"
1273             "    int aaaaa /* test */);",
1274             format("void ffffffffff(int aaaaa /* test */);",
1275                    getLLVMStyleWithColumns(35)));
1276 }
1277 
1278 TEST_F(FormatTest, SplitsLongCxxComments) {
1279   EXPECT_EQ("// A comment that\n"
1280             "// doesn't fit on\n"
1281             "// one line",
1282             format("// A comment that doesn't fit on one line",
1283                    getLLVMStyleWithColumns(20)));
1284   EXPECT_EQ("/// A comment that\n"
1285             "/// doesn't fit on\n"
1286             "/// one line",
1287             format("/// A comment that doesn't fit on one line",
1288                    getLLVMStyleWithColumns(20)));
1289   EXPECT_EQ("//! A comment that\n"
1290             "//! doesn't fit on\n"
1291             "//! one line",
1292             format("//! A comment that doesn't fit on one line",
1293                    getLLVMStyleWithColumns(20)));
1294   EXPECT_EQ("// a b c d\n"
1295             "// e f  g\n"
1296             "// h i j k",
1297             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1298   EXPECT_EQ(
1299       "// a b c d\n"
1300       "// e f  g\n"
1301       "// h i j k",
1302       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1303   EXPECT_EQ("if (true) // A comment that\n"
1304             "          // doesn't fit on\n"
1305             "          // one line",
1306             format("if (true) // A comment that doesn't fit on one line   ",
1307                    getLLVMStyleWithColumns(30)));
1308   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1309             format("//    Don't_touch_leading_whitespace",
1310                    getLLVMStyleWithColumns(20)));
1311   EXPECT_EQ("// Add leading\n"
1312             "// whitespace",
1313             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1314   EXPECT_EQ("/// Add leading\n"
1315             "/// whitespace",
1316             format("///Add leading whitespace", getLLVMStyleWithColumns(20)));
1317   EXPECT_EQ("//! Add leading\n"
1318             "//! whitespace",
1319             format("//!Add leading whitespace", getLLVMStyleWithColumns(20)));
1320   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1321   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1322             "// limit",
1323             format("//Even if it makes the line exceed the column limit",
1324                    getLLVMStyleWithColumns(51)));
1325   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1326 
1327   EXPECT_EQ("// aa bb cc dd",
1328             format("// aa bb             cc dd                   ",
1329                    getLLVMStyleWithColumns(15)));
1330 
1331   EXPECT_EQ("// A comment before\n"
1332             "// a macro\n"
1333             "// definition\n"
1334             "#define a b",
1335             format("// A comment before a macro definition\n"
1336                    "#define a b",
1337                    getLLVMStyleWithColumns(20)));
1338   EXPECT_EQ("void ffffff(\n"
1339             "    int aaaaaaaaa,  // wwww\n"
1340             "    int bbbbbbbbbb, // xxxxxxx\n"
1341             "                    // yyyyyyyyyy\n"
1342             "    int c, int d, int e) {}",
1343             format("void ffffff(\n"
1344                    "    int aaaaaaaaa, // wwww\n"
1345                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1346                    "    int c, int d, int e) {}",
1347                    getLLVMStyleWithColumns(40)));
1348   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1349             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1350                    getLLVMStyleWithColumns(20)));
1351   EXPECT_EQ(
1352       "#define XXX // a b c d\n"
1353       "            // e f g h",
1354       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1355   EXPECT_EQ(
1356       "#define XXX // q w e r\n"
1357       "            // t y u i",
1358       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1359 }
1360 
1361 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1362   EXPECT_EQ("//     A comment\n"
1363             "//     that doesn't\n"
1364             "//     fit on one\n"
1365             "//     line",
1366             format("//     A comment that doesn't fit on one line",
1367                    getLLVMStyleWithColumns(20)));
1368   EXPECT_EQ("///     A comment\n"
1369             "///     that doesn't\n"
1370             "///     fit on one\n"
1371             "///     line",
1372             format("///     A comment that doesn't fit on one line",
1373                    getLLVMStyleWithColumns(20)));
1374 }
1375 
1376 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1377   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1378             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1379             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1380             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1381                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1382                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1383   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1384             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1385             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1386             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1387                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1388                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1389                    getLLVMStyleWithColumns(50)));
1390   // FIXME: One day we might want to implement adjustment of leading whitespace
1391   // of the consecutive lines in this kind of comment:
1392   EXPECT_EQ("double\n"
1393             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1394             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1395             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1396             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1397                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1398                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1399                    getLLVMStyleWithColumns(49)));
1400 }
1401 
1402 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1403   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1404   Pragmas.CommentPragmas = "^ IWYU pragma:";
1405   EXPECT_EQ(
1406       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1407       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1408   EXPECT_EQ(
1409       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1410       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1411 }
1412 
1413 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1414   EXPECT_EQ("if (xxx ==\n"
1415             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1416             "    zzz)\n"
1417             "  q();",
1418             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1419                    "    zzz) q();",
1420                    getLLVMStyleWithColumns(40)));
1421   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1422             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1423             "    zzz)\n"
1424             "  q();",
1425             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1426                    "    zzz) q();",
1427                    getLLVMStyleWithColumns(40)));
1428   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1429             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1430             "    zzz)\n"
1431             "  q();",
1432             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1433                    "    zzz) q();",
1434                    getLLVMStyleWithColumns(40)));
1435   EXPECT_EQ("fffffffff(\n"
1436             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1437             "    zzz);",
1438             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1439                    " zzz);",
1440                    getLLVMStyleWithColumns(40)));
1441 }
1442 
1443 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1444   EXPECT_EQ("#define A(x) /* \\\n"
1445             "  a comment     \\\n"
1446             "  inside */     \\\n"
1447             "  f();",
1448             format("#define A(x) /* \\\n"
1449                    "  a comment     \\\n"
1450                    "  inside */     \\\n"
1451                    "  f();",
1452                    getLLVMStyleWithColumns(17)));
1453   EXPECT_EQ("#define A(      \\\n"
1454             "    x) /*       \\\n"
1455             "  a comment     \\\n"
1456             "  inside */     \\\n"
1457             "  f();",
1458             format("#define A(      \\\n"
1459                    "    x) /*       \\\n"
1460                    "  a comment     \\\n"
1461                    "  inside */     \\\n"
1462                    "  f();",
1463                    getLLVMStyleWithColumns(17)));
1464 }
1465 
1466 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1467   EXPECT_EQ("namespace {}\n// Test\n#define A",
1468             format("namespace {}\n   // Test\n#define A"));
1469   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1470             format("namespace {}\n   /* Test */\n#define A"));
1471   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1472             format("namespace {}\n   /* Test */    #define A"));
1473 }
1474 
1475 TEST_F(FormatTest, SplitsLongLinesInComments) {
1476   EXPECT_EQ("/* This is a long\n"
1477             " * comment that\n"
1478             " * doesn't\n"
1479             " * fit on one line.\n"
1480             " */",
1481             format("/* "
1482                    "This is a long                                         "
1483                    "comment that "
1484                    "doesn't                                    "
1485                    "fit on one line.  */",
1486                    getLLVMStyleWithColumns(20)));
1487   EXPECT_EQ(
1488       "/* a b c d\n"
1489       " * e f  g\n"
1490       " * h i j k\n"
1491       " */",
1492       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1493   EXPECT_EQ(
1494       "/* a b c d\n"
1495       " * e f  g\n"
1496       " * h i j k\n"
1497       " */",
1498       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1499   EXPECT_EQ("/*\n"
1500             "This is a long\n"
1501             "comment that doesn't\n"
1502             "fit on one line.\n"
1503             "*/",
1504             format("/*\n"
1505                    "This is a long                                         "
1506                    "comment that doesn't                                    "
1507                    "fit on one line.                                      \n"
1508                    "*/",
1509                    getLLVMStyleWithColumns(20)));
1510   EXPECT_EQ("/*\n"
1511             " * This is a long\n"
1512             " * comment that\n"
1513             " * doesn't fit on\n"
1514             " * one line.\n"
1515             " */",
1516             format("/*      \n"
1517                    " * This is a long "
1518                    "   comment that     "
1519                    "   doesn't fit on   "
1520                    "   one line.                                            \n"
1521                    " */",
1522                    getLLVMStyleWithColumns(20)));
1523   EXPECT_EQ("/*\n"
1524             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1525             " * so_it_should_be_broken\n"
1526             " * wherever_a_space_occurs\n"
1527             " */",
1528             format("/*\n"
1529                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1530                    "   so_it_should_be_broken "
1531                    "   wherever_a_space_occurs                             \n"
1532                    " */",
1533                    getLLVMStyleWithColumns(20)));
1534   EXPECT_EQ("/*\n"
1535             " *    This_comment_can_not_be_broken_into_lines\n"
1536             " */",
1537             format("/*\n"
1538                    " *    This_comment_can_not_be_broken_into_lines\n"
1539                    " */",
1540                    getLLVMStyleWithColumns(20)));
1541   EXPECT_EQ("{\n"
1542             "  /*\n"
1543             "  This is another\n"
1544             "  long comment that\n"
1545             "  doesn't fit on one\n"
1546             "  line    1234567890\n"
1547             "  */\n"
1548             "}",
1549             format("{\n"
1550                    "/*\n"
1551                    "This is another     "
1552                    "  long comment that "
1553                    "  doesn't fit on one"
1554                    "  line    1234567890\n"
1555                    "*/\n"
1556                    "}",
1557                    getLLVMStyleWithColumns(20)));
1558   EXPECT_EQ("{\n"
1559             "  /*\n"
1560             "   * This        i s\n"
1561             "   * another comment\n"
1562             "   * t hat  doesn' t\n"
1563             "   * fit on one l i\n"
1564             "   * n e\n"
1565             "   */\n"
1566             "}",
1567             format("{\n"
1568                    "/*\n"
1569                    " * This        i s"
1570                    "   another comment"
1571                    "   t hat  doesn' t"
1572                    "   fit on one l i"
1573                    "   n e\n"
1574                    " */\n"
1575                    "}",
1576                    getLLVMStyleWithColumns(20)));
1577   EXPECT_EQ("/*\n"
1578             " * This is a long\n"
1579             " * comment that\n"
1580             " * doesn't fit on\n"
1581             " * one line\n"
1582             " */",
1583             format("   /*\n"
1584                    "    * This is a long comment that doesn't fit on one line\n"
1585                    "    */",
1586                    getLLVMStyleWithColumns(20)));
1587   EXPECT_EQ("{\n"
1588             "  if (something) /* This is a\n"
1589             "                    long\n"
1590             "                    comment */\n"
1591             "    ;\n"
1592             "}",
1593             format("{\n"
1594                    "  if (something) /* This is a long comment */\n"
1595                    "    ;\n"
1596                    "}",
1597                    getLLVMStyleWithColumns(30)));
1598 
1599   EXPECT_EQ("/* A comment before\n"
1600             " * a macro\n"
1601             " * definition */\n"
1602             "#define a b",
1603             format("/* A comment before a macro definition */\n"
1604                    "#define a b",
1605                    getLLVMStyleWithColumns(20)));
1606 
1607   EXPECT_EQ("/* some comment\n"
1608             "     *   a comment\n"
1609             "* that we break\n"
1610             " * another comment\n"
1611             "* we have to break\n"
1612             "* a left comment\n"
1613             " */",
1614             format("  /* some comment\n"
1615                    "       *   a comment that we break\n"
1616                    "   * another comment we have to break\n"
1617                    "* a left comment\n"
1618                    "   */",
1619                    getLLVMStyleWithColumns(20)));
1620 
1621   EXPECT_EQ("/**\n"
1622             " * multiline block\n"
1623             " * comment\n"
1624             " *\n"
1625             " */",
1626             format("/**\n"
1627                    " * multiline block comment\n"
1628                    " *\n"
1629                    " */",
1630                    getLLVMStyleWithColumns(20)));
1631 
1632   EXPECT_EQ("/*\n"
1633             "\n"
1634             "\n"
1635             "    */\n",
1636             format("  /*       \n"
1637                    "      \n"
1638                    "               \n"
1639                    "      */\n"));
1640 
1641   EXPECT_EQ("/* a a */",
1642             format("/* a a            */", getLLVMStyleWithColumns(15)));
1643   EXPECT_EQ("/* a a bc  */",
1644             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1645   EXPECT_EQ("/* aaa aaa\n"
1646             " * aaaaa */",
1647             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1648   EXPECT_EQ("/* aaa aaa\n"
1649             " * aaaaa     */",
1650             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1651 }
1652 
1653 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1654   EXPECT_EQ("#define X          \\\n"
1655             "  /*               \\\n"
1656             "   Test            \\\n"
1657             "   Macro comment   \\\n"
1658             "   with a long     \\\n"
1659             "   line            \\\n"
1660             "   */              \\\n"
1661             "  A + B",
1662             format("#define X \\\n"
1663                    "  /*\n"
1664                    "   Test\n"
1665                    "   Macro comment with a long  line\n"
1666                    "   */ \\\n"
1667                    "  A + B",
1668                    getLLVMStyleWithColumns(20)));
1669   EXPECT_EQ("#define X          \\\n"
1670             "  /* Macro comment \\\n"
1671             "     with a long   \\\n"
1672             "     line */       \\\n"
1673             "  A + B",
1674             format("#define X \\\n"
1675                    "  /* Macro comment with a long\n"
1676                    "     line */ \\\n"
1677                    "  A + B",
1678                    getLLVMStyleWithColumns(20)));
1679   EXPECT_EQ("#define X          \\\n"
1680             "  /* Macro comment \\\n"
1681             "   * with a long   \\\n"
1682             "   * line */       \\\n"
1683             "  A + B",
1684             format("#define X \\\n"
1685                    "  /* Macro comment with a long  line */ \\\n"
1686                    "  A + B",
1687                    getLLVMStyleWithColumns(20)));
1688 }
1689 
1690 TEST_F(FormatTest, CommentsInStaticInitializers) {
1691   EXPECT_EQ(
1692       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1693       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1694       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1695       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1696       "                        aaaaaaaaaaaaaaaaaaaa};",
1697       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1698              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1699              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1700              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1701              "                  aaaaaaaaaaaaaaaaaaaa };"));
1702   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1703                "                        bbbbbbbbbbb, ccccccccccc};");
1704   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1705                "                        // comment for bb....\n"
1706                "                        bbbbbbbbbbb, ccccccccccc};");
1707   verifyGoogleFormat(
1708       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1709       "                        bbbbbbbbbbb, ccccccccccc};");
1710   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1711                      "                        // comment for bb....\n"
1712                      "                        bbbbbbbbbbb, ccccccccccc};");
1713 
1714   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1715                "       {d, e, f},  // Group #2\n"
1716                "       {g, h, i}}; // Group #3");
1717   verifyFormat("S s = {{// Group #1\n"
1718                "        a, b, c},\n"
1719                "       {// Group #2\n"
1720                "        d, e, f},\n"
1721                "       {// Group #3\n"
1722                "        g, h, i}};");
1723 
1724   EXPECT_EQ("S s = {\n"
1725             "    // Some comment\n"
1726             "    a,\n"
1727             "\n"
1728             "    // Comment after empty line\n"
1729             "    b}",
1730             format("S s =    {\n"
1731                    "      // Some comment\n"
1732                    "  a,\n"
1733                    "  \n"
1734                    "     // Comment after empty line\n"
1735                    "      b\n"
1736                    "}"));
1737   EXPECT_EQ("S s = {\n"
1738             "    /* Some comment */\n"
1739             "    a,\n"
1740             "\n"
1741             "    /* Comment after empty line */\n"
1742             "    b}",
1743             format("S s =    {\n"
1744                    "      /* Some comment */\n"
1745                    "  a,\n"
1746                    "  \n"
1747                    "     /* Comment after empty line */\n"
1748                    "      b\n"
1749                    "}"));
1750   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1751                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1752                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1753                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1754 }
1755 
1756 TEST_F(FormatTest, IgnoresIf0Contents) {
1757   EXPECT_EQ("#if 0\n"
1758             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1759             "#endif\n"
1760             "void f() {}",
1761             format("#if 0\n"
1762                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1763                    "#endif\n"
1764                    "void f(  ) {  }"));
1765   EXPECT_EQ("#if false\n"
1766             "void f(  ) {  }\n"
1767             "#endif\n"
1768             "void g() {}\n",
1769             format("#if false\n"
1770                    "void f(  ) {  }\n"
1771                    "#endif\n"
1772                    "void g(  ) {  }\n"));
1773   EXPECT_EQ("enum E {\n"
1774             "  One,\n"
1775             "  Two,\n"
1776             "#if 0\n"
1777             "Three,\n"
1778             "      Four,\n"
1779             "#endif\n"
1780             "  Five\n"
1781             "};",
1782             format("enum E {\n"
1783                    "  One,Two,\n"
1784                    "#if 0\n"
1785                    "Three,\n"
1786                    "      Four,\n"
1787                    "#endif\n"
1788                    "  Five};"));
1789   EXPECT_EQ("enum F {\n"
1790             "  One,\n"
1791             "#if 1\n"
1792             "  Two,\n"
1793             "#if 0\n"
1794             "Three,\n"
1795             "      Four,\n"
1796             "#endif\n"
1797             "  Five\n"
1798             "#endif\n"
1799             "};",
1800             format("enum F {\n"
1801                    "One,\n"
1802                    "#if 1\n"
1803                    "Two,\n"
1804                    "#if 0\n"
1805                    "Three,\n"
1806                    "      Four,\n"
1807                    "#endif\n"
1808                    "Five\n"
1809                    "#endif\n"
1810                    "};"));
1811   EXPECT_EQ("enum G {\n"
1812             "  One,\n"
1813             "#if 0\n"
1814             "Two,\n"
1815             "#else\n"
1816             "  Three,\n"
1817             "#endif\n"
1818             "  Four\n"
1819             "};",
1820             format("enum G {\n"
1821                    "One,\n"
1822                    "#if 0\n"
1823                    "Two,\n"
1824                    "#else\n"
1825                    "Three,\n"
1826                    "#endif\n"
1827                    "Four\n"
1828                    "};"));
1829   EXPECT_EQ("enum H {\n"
1830             "  One,\n"
1831             "#if 0\n"
1832             "#ifdef Q\n"
1833             "Two,\n"
1834             "#else\n"
1835             "Three,\n"
1836             "#endif\n"
1837             "#endif\n"
1838             "  Four\n"
1839             "};",
1840             format("enum H {\n"
1841                    "One,\n"
1842                    "#if 0\n"
1843                    "#ifdef Q\n"
1844                    "Two,\n"
1845                    "#else\n"
1846                    "Three,\n"
1847                    "#endif\n"
1848                    "#endif\n"
1849                    "Four\n"
1850                    "};"));
1851   EXPECT_EQ("enum I {\n"
1852             "  One,\n"
1853             "#if /* test */ 0 || 1\n"
1854             "Two,\n"
1855             "Three,\n"
1856             "#endif\n"
1857             "  Four\n"
1858             "};",
1859             format("enum I {\n"
1860                    "One,\n"
1861                    "#if /* test */ 0 || 1\n"
1862                    "Two,\n"
1863                    "Three,\n"
1864                    "#endif\n"
1865                    "Four\n"
1866                    "};"));
1867   EXPECT_EQ("enum J {\n"
1868             "  One,\n"
1869             "#if 0\n"
1870             "#if 0\n"
1871             "Two,\n"
1872             "#else\n"
1873             "Three,\n"
1874             "#endif\n"
1875             "Four,\n"
1876             "#endif\n"
1877             "  Five\n"
1878             "};",
1879             format("enum J {\n"
1880                    "One,\n"
1881                    "#if 0\n"
1882                    "#if 0\n"
1883                    "Two,\n"
1884                    "#else\n"
1885                    "Three,\n"
1886                    "#endif\n"
1887                    "Four,\n"
1888                    "#endif\n"
1889                    "Five\n"
1890                    "};"));
1891 }
1892 
1893 //===----------------------------------------------------------------------===//
1894 // Tests for classes, namespaces, etc.
1895 //===----------------------------------------------------------------------===//
1896 
1897 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1898   verifyFormat("class A {};");
1899 }
1900 
1901 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1902   verifyFormat("class A {\n"
1903                "public:\n"
1904                "public: // comment\n"
1905                "protected:\n"
1906                "private:\n"
1907                "  void f() {}\n"
1908                "};");
1909   verifyGoogleFormat("class A {\n"
1910                      " public:\n"
1911                      " protected:\n"
1912                      " private:\n"
1913                      "  void f() {}\n"
1914                      "};");
1915   verifyFormat("class A {\n"
1916                "public slots:\n"
1917                "  void f1() {}\n"
1918                "public Q_SLOTS:\n"
1919                "  void f2() {}\n"
1920                "protected slots:\n"
1921                "  void f3() {}\n"
1922                "protected Q_SLOTS:\n"
1923                "  void f4() {}\n"
1924                "private slots:\n"
1925                "  void f5() {}\n"
1926                "private Q_SLOTS:\n"
1927                "  void f6() {}\n"
1928                "signals:\n"
1929                "  void g1();\n"
1930                "Q_SIGNALS:\n"
1931                "  void g2();\n"
1932                "};");
1933 
1934   // Don't interpret 'signals' the wrong way.
1935   verifyFormat("signals.set();");
1936   verifyFormat("for (Signals signals : f()) {\n}");
1937   verifyFormat("{\n"
1938                "  signals.set(); // This needs indentation.\n"
1939                "}");
1940   verifyFormat("void f() {\n"
1941                "label:\n"
1942                "  signals.baz();\n"
1943                "}");
1944 }
1945 
1946 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1947   EXPECT_EQ("class A {\n"
1948             "public:\n"
1949             "  void f();\n"
1950             "\n"
1951             "private:\n"
1952             "  void g() {}\n"
1953             "  // test\n"
1954             "protected:\n"
1955             "  int h;\n"
1956             "};",
1957             format("class A {\n"
1958                    "public:\n"
1959                    "void f();\n"
1960                    "private:\n"
1961                    "void g() {}\n"
1962                    "// test\n"
1963                    "protected:\n"
1964                    "int h;\n"
1965                    "};"));
1966   EXPECT_EQ("class A {\n"
1967             "protected:\n"
1968             "public:\n"
1969             "  void f();\n"
1970             "};",
1971             format("class A {\n"
1972                    "protected:\n"
1973                    "\n"
1974                    "public:\n"
1975                    "\n"
1976                    "  void f();\n"
1977                    "};"));
1978 
1979   // Even ensure proper spacing inside macros.
1980   EXPECT_EQ("#define B     \\\n"
1981             "  class A {   \\\n"
1982             "   protected: \\\n"
1983             "   public:    \\\n"
1984             "    void f(); \\\n"
1985             "  };",
1986             format("#define B     \\\n"
1987                    "  class A {   \\\n"
1988                    "   protected: \\\n"
1989                    "              \\\n"
1990                    "   public:    \\\n"
1991                    "              \\\n"
1992                    "    void f(); \\\n"
1993                    "  };",
1994                    getGoogleStyle()));
1995   // But don't remove empty lines after macros ending in access specifiers.
1996   EXPECT_EQ("#define A private:\n"
1997             "\n"
1998             "int i;",
1999             format("#define A         private:\n"
2000                    "\n"
2001                    "int              i;"));
2002 }
2003 
2004 TEST_F(FormatTest, FormatsClasses) {
2005   verifyFormat("class A : public B {};");
2006   verifyFormat("class A : public ::B {};");
2007 
2008   verifyFormat(
2009       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2010       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2011   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
2012                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
2013                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
2014   verifyFormat(
2015       "class A : public B, public C, public D, public E, public F {};");
2016   verifyFormat("class AAAAAAAAAAAA : public B,\n"
2017                "                     public C,\n"
2018                "                     public D,\n"
2019                "                     public E,\n"
2020                "                     public F,\n"
2021                "                     public G {};");
2022 
2023   verifyFormat("class\n"
2024                "    ReallyReallyLongClassName {\n"
2025                "  int i;\n"
2026                "};",
2027                getLLVMStyleWithColumns(32));
2028   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2029                "                           aaaaaaaaaaaaaaaa> {};");
2030   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2031                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2032                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2033   verifyFormat("template <class R, class C>\n"
2034                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2035                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2036   verifyFormat("class ::A::B {};");
2037 }
2038 
2039 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2040   verifyFormat("class A {\n} a, b;");
2041   verifyFormat("struct A {\n} a, b;");
2042   verifyFormat("union A {\n} a;");
2043 }
2044 
2045 TEST_F(FormatTest, FormatsEnum) {
2046   verifyFormat("enum {\n"
2047                "  Zero,\n"
2048                "  One = 1,\n"
2049                "  Two = One + 1,\n"
2050                "  Three = (One + Two),\n"
2051                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2052                "  Five = (One, Two, Three, Four, 5)\n"
2053                "};");
2054   verifyGoogleFormat("enum {\n"
2055                      "  Zero,\n"
2056                      "  One = 1,\n"
2057                      "  Two = One + 1,\n"
2058                      "  Three = (One + Two),\n"
2059                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2060                      "  Five = (One, Two, Three, Four, 5)\n"
2061                      "};");
2062   verifyFormat("enum Enum {};");
2063   verifyFormat("enum {};");
2064   verifyFormat("enum X E {} d;");
2065   verifyFormat("enum __attribute__((...)) E {} d;");
2066   verifyFormat("enum __declspec__((...)) E {} d;");
2067   verifyFormat("enum {\n"
2068                "  Bar = Foo<int, int>::value\n"
2069                "};",
2070                getLLVMStyleWithColumns(30));
2071 
2072   verifyFormat("enum ShortEnum { A, B, C };");
2073   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2074 
2075   EXPECT_EQ("enum KeepEmptyLines {\n"
2076             "  ONE,\n"
2077             "\n"
2078             "  TWO,\n"
2079             "\n"
2080             "  THREE\n"
2081             "}",
2082             format("enum KeepEmptyLines {\n"
2083                    "  ONE,\n"
2084                    "\n"
2085                    "  TWO,\n"
2086                    "\n"
2087                    "\n"
2088                    "  THREE\n"
2089                    "}"));
2090   verifyFormat("enum E { // comment\n"
2091                "  ONE,\n"
2092                "  TWO\n"
2093                "};\n"
2094                "int i;");
2095   // Not enums.
2096   verifyFormat("enum X f() {\n"
2097                "  a();\n"
2098                "  return 42;\n"
2099                "}");
2100   verifyFormat("enum X Type::f() {\n"
2101                "  a();\n"
2102                "  return 42;\n"
2103                "}");
2104   verifyFormat("enum ::X f() {\n"
2105                "  a();\n"
2106                "  return 42;\n"
2107                "}");
2108   verifyFormat("enum ns::X f() {\n"
2109                "  a();\n"
2110                "  return 42;\n"
2111                "}");
2112 }
2113 
2114 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2115   verifyFormat("enum Type {\n"
2116                "  One = 0; // These semicolons should be commas.\n"
2117                "  Two = 1;\n"
2118                "};");
2119   verifyFormat("namespace n {\n"
2120                "enum Type {\n"
2121                "  One,\n"
2122                "  Two, // missing };\n"
2123                "  int i;\n"
2124                "}\n"
2125                "void g() {}");
2126 }
2127 
2128 TEST_F(FormatTest, FormatsEnumStruct) {
2129   verifyFormat("enum struct {\n"
2130                "  Zero,\n"
2131                "  One = 1,\n"
2132                "  Two = One + 1,\n"
2133                "  Three = (One + Two),\n"
2134                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2135                "  Five = (One, Two, Three, Four, 5)\n"
2136                "};");
2137   verifyFormat("enum struct Enum {};");
2138   verifyFormat("enum struct {};");
2139   verifyFormat("enum struct X E {} d;");
2140   verifyFormat("enum struct __attribute__((...)) E {} d;");
2141   verifyFormat("enum struct __declspec__((...)) E {} d;");
2142   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2143 }
2144 
2145 TEST_F(FormatTest, FormatsEnumClass) {
2146   verifyFormat("enum class {\n"
2147                "  Zero,\n"
2148                "  One = 1,\n"
2149                "  Two = One + 1,\n"
2150                "  Three = (One + Two),\n"
2151                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2152                "  Five = (One, Two, Three, Four, 5)\n"
2153                "};");
2154   verifyFormat("enum class Enum {};");
2155   verifyFormat("enum class {};");
2156   verifyFormat("enum class X E {} d;");
2157   verifyFormat("enum class __attribute__((...)) E {} d;");
2158   verifyFormat("enum class __declspec__((...)) E {} d;");
2159   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2160 }
2161 
2162 TEST_F(FormatTest, FormatsEnumTypes) {
2163   verifyFormat("enum X : int {\n"
2164                "  A, // Force multiple lines.\n"
2165                "  B\n"
2166                "};");
2167   verifyFormat("enum X : int { A, B };");
2168   verifyFormat("enum X : std::uint32_t { A, B };");
2169 }
2170 
2171 TEST_F(FormatTest, FormatsNSEnums) {
2172   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2173   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2174                      "  // Information about someDecentlyLongValue.\n"
2175                      "  someDecentlyLongValue,\n"
2176                      "  // Information about anotherDecentlyLongValue.\n"
2177                      "  anotherDecentlyLongValue,\n"
2178                      "  // Information about aThirdDecentlyLongValue.\n"
2179                      "  aThirdDecentlyLongValue\n"
2180                      "};");
2181   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2182                      "  a = 1,\n"
2183                      "  b = 2,\n"
2184                      "  c = 3,\n"
2185                      "};");
2186   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2187                      "  a = 1,\n"
2188                      "  b = 2,\n"
2189                      "  c = 3,\n"
2190                      "};");
2191   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2192                      "  a = 1,\n"
2193                      "  b = 2,\n"
2194                      "  c = 3,\n"
2195                      "};");
2196 }
2197 
2198 TEST_F(FormatTest, FormatsBitfields) {
2199   verifyFormat("struct Bitfields {\n"
2200                "  unsigned sClass : 8;\n"
2201                "  unsigned ValueKind : 2;\n"
2202                "};");
2203   verifyFormat("struct A {\n"
2204                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2205                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2206                "};");
2207   verifyFormat("struct MyStruct {\n"
2208                "  uchar data;\n"
2209                "  uchar : 8;\n"
2210                "  uchar : 8;\n"
2211                "  uchar other;\n"
2212                "};");
2213 }
2214 
2215 TEST_F(FormatTest, FormatsNamespaces) {
2216   verifyFormat("namespace some_namespace {\n"
2217                "class A {};\n"
2218                "void f() { f(); }\n"
2219                "}");
2220   verifyFormat("namespace {\n"
2221                "class A {};\n"
2222                "void f() { f(); }\n"
2223                "}");
2224   verifyFormat("inline namespace X {\n"
2225                "class A {};\n"
2226                "void f() { f(); }\n"
2227                "}");
2228   verifyFormat("using namespace some_namespace;\n"
2229                "class A {};\n"
2230                "void f() { f(); }");
2231 
2232   // This code is more common than we thought; if we
2233   // layout this correctly the semicolon will go into
2234   // its own line, which is undesirable.
2235   verifyFormat("namespace {};");
2236   verifyFormat("namespace {\n"
2237                "class A {};\n"
2238                "};");
2239 
2240   verifyFormat("namespace {\n"
2241                "int SomeVariable = 0; // comment\n"
2242                "} // namespace");
2243   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2244             "#define HEADER_GUARD\n"
2245             "namespace my_namespace {\n"
2246             "int i;\n"
2247             "} // my_namespace\n"
2248             "#endif // HEADER_GUARD",
2249             format("#ifndef HEADER_GUARD\n"
2250                    " #define HEADER_GUARD\n"
2251                    "   namespace my_namespace {\n"
2252                    "int i;\n"
2253                    "}    // my_namespace\n"
2254                    "#endif    // HEADER_GUARD"));
2255 
2256   EXPECT_EQ("namespace A::B {\n"
2257             "class C {};\n"
2258             "}",
2259             format("namespace A::B {\n"
2260                    "class C {};\n"
2261                    "}"));
2262 
2263   FormatStyle Style = getLLVMStyle();
2264   Style.NamespaceIndentation = FormatStyle::NI_All;
2265   EXPECT_EQ("namespace out {\n"
2266             "  int i;\n"
2267             "  namespace in {\n"
2268             "    int i;\n"
2269             "  } // namespace\n"
2270             "} // namespace",
2271             format("namespace out {\n"
2272                    "int i;\n"
2273                    "namespace in {\n"
2274                    "int i;\n"
2275                    "} // namespace\n"
2276                    "} // namespace",
2277                    Style));
2278 
2279   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2280   EXPECT_EQ("namespace out {\n"
2281             "int i;\n"
2282             "namespace in {\n"
2283             "  int i;\n"
2284             "} // namespace\n"
2285             "} // namespace",
2286             format("namespace out {\n"
2287                    "int i;\n"
2288                    "namespace in {\n"
2289                    "int i;\n"
2290                    "} // namespace\n"
2291                    "} // namespace",
2292                    Style));
2293 }
2294 
2295 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2296 
2297 TEST_F(FormatTest, FormatsInlineASM) {
2298   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2299   verifyFormat("asm(\"nop\" ::: \"memory\");");
2300   verifyFormat(
2301       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2302       "    \"cpuid\\n\\t\"\n"
2303       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2304       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2305       "    : \"a\"(value));");
2306   EXPECT_EQ(
2307       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2308       "  __asm {\n"
2309       "        mov     edx,[that] // vtable in edx\n"
2310       "        mov     eax,methodIndex\n"
2311       "        call    [edx][eax*4] // stdcall\n"
2312       "  }\n"
2313       "}",
2314       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2315              "    __asm {\n"
2316              "        mov     edx,[that] // vtable in edx\n"
2317              "        mov     eax,methodIndex\n"
2318              "        call    [edx][eax*4] // stdcall\n"
2319              "    }\n"
2320              "}"));
2321   EXPECT_EQ("_asm {\n"
2322             "  xor eax, eax;\n"
2323             "  cpuid;\n"
2324             "}",
2325             format("_asm {\n"
2326                    "  xor eax, eax;\n"
2327                    "  cpuid;\n"
2328                    "}"));
2329   verifyFormat("void function() {\n"
2330                "  // comment\n"
2331                "  asm(\"\");\n"
2332                "}");
2333   EXPECT_EQ("__asm {\n"
2334             "}\n"
2335             "int i;",
2336             format("__asm   {\n"
2337                    "}\n"
2338                    "int   i;"));
2339 }
2340 
2341 TEST_F(FormatTest, FormatTryCatch) {
2342   verifyFormat("try {\n"
2343                "  throw a * b;\n"
2344                "} catch (int a) {\n"
2345                "  // Do nothing.\n"
2346                "} catch (...) {\n"
2347                "  exit(42);\n"
2348                "}");
2349 
2350   // Function-level try statements.
2351   verifyFormat("int f() try { return 4; } catch (...) {\n"
2352                "  return 5;\n"
2353                "}");
2354   verifyFormat("class A {\n"
2355                "  int a;\n"
2356                "  A() try : a(0) {\n"
2357                "  } catch (...) {\n"
2358                "    throw;\n"
2359                "  }\n"
2360                "};\n");
2361 
2362   // Incomplete try-catch blocks.
2363   verifyIncompleteFormat("try {} catch (");
2364 }
2365 
2366 TEST_F(FormatTest, FormatSEHTryCatch) {
2367   verifyFormat("__try {\n"
2368                "  int a = b * c;\n"
2369                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2370                "  // Do nothing.\n"
2371                "}");
2372 
2373   verifyFormat("__try {\n"
2374                "  int a = b * c;\n"
2375                "} __finally {\n"
2376                "  // Do nothing.\n"
2377                "}");
2378 
2379   verifyFormat("DEBUG({\n"
2380                "  __try {\n"
2381                "  } __finally {\n"
2382                "  }\n"
2383                "});\n");
2384 }
2385 
2386 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2387   verifyFormat("try {\n"
2388                "  f();\n"
2389                "} catch {\n"
2390                "  g();\n"
2391                "}");
2392   verifyFormat("try {\n"
2393                "  f();\n"
2394                "} catch (A a) MACRO(x) {\n"
2395                "  g();\n"
2396                "} catch (B b) MACRO(x) {\n"
2397                "  g();\n"
2398                "}");
2399 }
2400 
2401 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2402   FormatStyle Style = getLLVMStyle();
2403   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
2404                           FormatStyle::BS_WebKit}) {
2405     Style.BreakBeforeBraces = BraceStyle;
2406     verifyFormat("try {\n"
2407                  "  // something\n"
2408                  "} catch (...) {\n"
2409                  "  // something\n"
2410                  "}",
2411                  Style);
2412   }
2413   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2414   verifyFormat("try {\n"
2415                "  // something\n"
2416                "}\n"
2417                "catch (...) {\n"
2418                "  // something\n"
2419                "}",
2420                Style);
2421   verifyFormat("__try {\n"
2422                "  // something\n"
2423                "}\n"
2424                "__finally {\n"
2425                "  // something\n"
2426                "}",
2427                Style);
2428   verifyFormat("@try {\n"
2429                "  // something\n"
2430                "}\n"
2431                "@finally {\n"
2432                "  // something\n"
2433                "}",
2434                Style);
2435   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2436   verifyFormat("try\n"
2437                "{\n"
2438                "  // something\n"
2439                "}\n"
2440                "catch (...)\n"
2441                "{\n"
2442                "  // something\n"
2443                "}",
2444                Style);
2445   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2446   verifyFormat("try\n"
2447                "  {\n"
2448                "    // something\n"
2449                "  }\n"
2450                "catch (...)\n"
2451                "  {\n"
2452                "    // something\n"
2453                "  }",
2454                Style);
2455   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
2456   Style.BraceWrapping.BeforeCatch = true;
2457   verifyFormat("try {\n"
2458                "  // something\n"
2459                "}\n"
2460                "catch (...) {\n"
2461                "  // something\n"
2462                "}",
2463                Style);
2464 }
2465 
2466 TEST_F(FormatTest, FormatObjCTryCatch) {
2467   verifyFormat("@try {\n"
2468                "  f();\n"
2469                "} @catch (NSException e) {\n"
2470                "  @throw;\n"
2471                "} @finally {\n"
2472                "  exit(42);\n"
2473                "}");
2474   verifyFormat("DEBUG({\n"
2475                "  @try {\n"
2476                "  } @finally {\n"
2477                "  }\n"
2478                "});\n");
2479 }
2480 
2481 TEST_F(FormatTest, FormatObjCAutoreleasepool) {
2482   FormatStyle Style = getLLVMStyle();
2483   verifyFormat("@autoreleasepool {\n"
2484                "  f();\n"
2485                "}\n"
2486                "@autoreleasepool {\n"
2487                "  f();\n"
2488                "}\n",
2489                Style);
2490   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2491   verifyFormat("@autoreleasepool\n"
2492                "{\n"
2493                "  f();\n"
2494                "}\n"
2495                "@autoreleasepool\n"
2496                "{\n"
2497                "  f();\n"
2498                "}\n",
2499                Style);
2500 }
2501 
2502 TEST_F(FormatTest, StaticInitializers) {
2503   verifyFormat("static SomeClass SC = {1, 'a'};");
2504 
2505   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2506                "    100000000, "
2507                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2508 
2509   // Here, everything other than the "}" would fit on a line.
2510   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2511                "    10000000000000000000000000};");
2512   EXPECT_EQ("S s = {a,\n"
2513             "\n"
2514             "       b};",
2515             format("S s = {\n"
2516                    "  a,\n"
2517                    "\n"
2518                    "  b\n"
2519                    "};"));
2520 
2521   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2522   // line. However, the formatting looks a bit off and this probably doesn't
2523   // happen often in practice.
2524   verifyFormat("static int Variable[1] = {\n"
2525                "    {1000000000000000000000000000000000000}};",
2526                getLLVMStyleWithColumns(40));
2527 }
2528 
2529 TEST_F(FormatTest, DesignatedInitializers) {
2530   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2531   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2532                "                    .bbbbbbbbbb = 2,\n"
2533                "                    .cccccccccc = 3,\n"
2534                "                    .dddddddddd = 4,\n"
2535                "                    .eeeeeeeeee = 5};");
2536   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2537                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2538                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2539                "    .ccccccccccccccccccccccccccc = 3,\n"
2540                "    .ddddddddddddddddddddddddddd = 4,\n"
2541                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2542 
2543   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2544 }
2545 
2546 TEST_F(FormatTest, NestedStaticInitializers) {
2547   verifyFormat("static A x = {{{}}};\n");
2548   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2549                "               {init1, init2, init3, init4}}};",
2550                getLLVMStyleWithColumns(50));
2551 
2552   verifyFormat("somes Status::global_reps[3] = {\n"
2553                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2554                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2555                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2556                getLLVMStyleWithColumns(60));
2557   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2558                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2559                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2560                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2561   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2562                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2563                "rect.fTop}};");
2564 
2565   verifyFormat(
2566       "SomeArrayOfSomeType a = {\n"
2567       "    {{1, 2, 3},\n"
2568       "     {1, 2, 3},\n"
2569       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2570       "      333333333333333333333333333333},\n"
2571       "     {1, 2, 3},\n"
2572       "     {1, 2, 3}}};");
2573   verifyFormat(
2574       "SomeArrayOfSomeType a = {\n"
2575       "    {{1, 2, 3}},\n"
2576       "    {{1, 2, 3}},\n"
2577       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2578       "      333333333333333333333333333333}},\n"
2579       "    {{1, 2, 3}},\n"
2580       "    {{1, 2, 3}}};");
2581 
2582   verifyFormat("struct {\n"
2583                "  unsigned bit;\n"
2584                "  const char *const name;\n"
2585                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2586                "                 {kOsWin, \"Windows\"},\n"
2587                "                 {kOsLinux, \"Linux\"},\n"
2588                "                 {kOsCrOS, \"Chrome OS\"}};");
2589   verifyFormat("struct {\n"
2590                "  unsigned bit;\n"
2591                "  const char *const name;\n"
2592                "} kBitsToOs[] = {\n"
2593                "    {kOsMac, \"Mac\"},\n"
2594                "    {kOsWin, \"Windows\"},\n"
2595                "    {kOsLinux, \"Linux\"},\n"
2596                "    {kOsCrOS, \"Chrome OS\"},\n"
2597                "};");
2598 }
2599 
2600 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2601   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2602                "                      \\\n"
2603                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2604 }
2605 
2606 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2607   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2608                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2609 
2610   // Do break defaulted and deleted functions.
2611   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2612                "    default;",
2613                getLLVMStyleWithColumns(40));
2614   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2615                "    delete;",
2616                getLLVMStyleWithColumns(40));
2617 }
2618 
2619 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2620   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2621                getLLVMStyleWithColumns(40));
2622   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2623                getLLVMStyleWithColumns(40));
2624   EXPECT_EQ("#define Q                              \\\n"
2625             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2626             "  \"aaaaaaaa.cpp\"",
2627             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2628                    getLLVMStyleWithColumns(40)));
2629 }
2630 
2631 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2632   EXPECT_EQ("# 123 \"A string literal\"",
2633             format("   #     123    \"A string literal\""));
2634 }
2635 
2636 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2637   EXPECT_EQ("#;", format("#;"));
2638   verifyFormat("#\n;\n;\n;");
2639 }
2640 
2641 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2642   EXPECT_EQ("#line 42 \"test\"\n",
2643             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2644   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2645                                     getLLVMStyleWithColumns(12)));
2646 }
2647 
2648 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2649   EXPECT_EQ("#line 42 \"test\"",
2650             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2651   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2652 }
2653 
2654 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2655   verifyFormat("#define A \\x20");
2656   verifyFormat("#define A \\ x20");
2657   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2658   verifyFormat("#define A ''");
2659   verifyFormat("#define A ''qqq");
2660   verifyFormat("#define A `qqq");
2661   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2662   EXPECT_EQ("const char *c = STRINGIFY(\n"
2663             "\\na : b);",
2664             format("const char * c = STRINGIFY(\n"
2665                    "\\na : b);"));
2666 
2667   verifyFormat("a\r\\");
2668   verifyFormat("a\v\\");
2669   verifyFormat("a\f\\");
2670 }
2671 
2672 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2673   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2674   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2675   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2676   // FIXME: We never break before the macro name.
2677   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2678 
2679   verifyFormat("#define A A\n#define A A");
2680   verifyFormat("#define A(X) A\n#define A A");
2681 
2682   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2683   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2684 }
2685 
2686 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2687   EXPECT_EQ("// somecomment\n"
2688             "#include \"a.h\"\n"
2689             "#define A(  \\\n"
2690             "    A, B)\n"
2691             "#include \"b.h\"\n"
2692             "// somecomment\n",
2693             format("  // somecomment\n"
2694                    "  #include \"a.h\"\n"
2695                    "#define A(A,\\\n"
2696                    "    B)\n"
2697                    "    #include \"b.h\"\n"
2698                    " // somecomment\n",
2699                    getLLVMStyleWithColumns(13)));
2700 }
2701 
2702 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2703 
2704 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2705   EXPECT_EQ("#define A    \\\n"
2706             "  c;         \\\n"
2707             "  e;\n"
2708             "f;",
2709             format("#define A c; e;\n"
2710                    "f;",
2711                    getLLVMStyleWithColumns(14)));
2712 }
2713 
2714 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2715 
2716 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2717   EXPECT_EQ("int x,\n"
2718             "#define A\n"
2719             "    y;",
2720             format("int x,\n#define A\ny;"));
2721 }
2722 
2723 TEST_F(FormatTest, HashInMacroDefinition) {
2724   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2725   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2726   verifyFormat("#define A  \\\n"
2727                "  {        \\\n"
2728                "    f(#c); \\\n"
2729                "  }",
2730                getLLVMStyleWithColumns(11));
2731 
2732   verifyFormat("#define A(X)         \\\n"
2733                "  void function##X()",
2734                getLLVMStyleWithColumns(22));
2735 
2736   verifyFormat("#define A(a, b, c)   \\\n"
2737                "  void a##b##c()",
2738                getLLVMStyleWithColumns(22));
2739 
2740   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2741 }
2742 
2743 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2744   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2745   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2746 }
2747 
2748 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2749   EXPECT_EQ("#define A b;", format("#define A \\\n"
2750                                    "          \\\n"
2751                                    "  b;",
2752                                    getLLVMStyleWithColumns(25)));
2753   EXPECT_EQ("#define A \\\n"
2754             "          \\\n"
2755             "  a;      \\\n"
2756             "  b;",
2757             format("#define A \\\n"
2758                    "          \\\n"
2759                    "  a;      \\\n"
2760                    "  b;",
2761                    getLLVMStyleWithColumns(11)));
2762   EXPECT_EQ("#define A \\\n"
2763             "  a;      \\\n"
2764             "          \\\n"
2765             "  b;",
2766             format("#define A \\\n"
2767                    "  a;      \\\n"
2768                    "          \\\n"
2769                    "  b;",
2770                    getLLVMStyleWithColumns(11)));
2771 }
2772 
2773 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2774   verifyIncompleteFormat("#define A :");
2775   verifyFormat("#define SOMECASES  \\\n"
2776                "  case 1:          \\\n"
2777                "  case 2\n",
2778                getLLVMStyleWithColumns(20));
2779   verifyFormat("#define MACRO(a) \\\n"
2780                "  if (a)         \\\n"
2781                "    f();         \\\n"
2782                "  else           \\\n"
2783                "    g()",
2784                getLLVMStyleWithColumns(18));
2785   verifyFormat("#define A template <typename T>");
2786   verifyIncompleteFormat("#define STR(x) #x\n"
2787                          "f(STR(this_is_a_string_literal{));");
2788   verifyFormat("#pragma omp threadprivate( \\\n"
2789                "    y)), // expected-warning",
2790                getLLVMStyleWithColumns(28));
2791   verifyFormat("#d, = };");
2792   verifyFormat("#if \"a");
2793   verifyIncompleteFormat("({\n"
2794                          "#define b     \\\n"
2795                          "  }           \\\n"
2796                          "  a\n"
2797                          "a",
2798                          getLLVMStyleWithColumns(15));
2799   verifyFormat("#define A     \\\n"
2800                "  {           \\\n"
2801                "    {\n"
2802                "#define B     \\\n"
2803                "  }           \\\n"
2804                "  }",
2805                getLLVMStyleWithColumns(15));
2806   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2807   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2808   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2809   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2810 }
2811 
2812 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2813   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2814   EXPECT_EQ("class A : public QObject {\n"
2815             "  Q_OBJECT\n"
2816             "\n"
2817             "  A() {}\n"
2818             "};",
2819             format("class A  :  public QObject {\n"
2820                    "     Q_OBJECT\n"
2821                    "\n"
2822                    "  A() {\n}\n"
2823                    "}  ;"));
2824   EXPECT_EQ("MACRO\n"
2825             "/*static*/ int i;",
2826             format("MACRO\n"
2827                    " /*static*/ int   i;"));
2828   EXPECT_EQ("SOME_MACRO\n"
2829             "namespace {\n"
2830             "void f();\n"
2831             "}",
2832             format("SOME_MACRO\n"
2833                    "  namespace    {\n"
2834                    "void   f(  );\n"
2835                    "}"));
2836   // Only if the identifier contains at least 5 characters.
2837   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2838   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2839   // Only if everything is upper case.
2840   EXPECT_EQ("class A : public QObject {\n"
2841             "  Q_Object A() {}\n"
2842             "};",
2843             format("class A  :  public QObject {\n"
2844                    "     Q_Object\n"
2845                    "  A() {\n}\n"
2846                    "}  ;"));
2847 
2848   // Only if the next line can actually start an unwrapped line.
2849   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2850             format("SOME_WEIRD_LOG_MACRO\n"
2851                    "<< SomeThing;"));
2852 
2853   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2854                "(n, buffers))\n",
2855                getChromiumStyle(FormatStyle::LK_Cpp));
2856 }
2857 
2858 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2859   EXPECT_EQ("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             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2866                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2867                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2868                    "  class X {};\n"
2869                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2870                    "  int *createScopDetectionPass() { return 0; }"));
2871   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2872   // braces, so that inner block is indented one level more.
2873   EXPECT_EQ("int q() {\n"
2874             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2875             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2876             "  IPC_END_MESSAGE_MAP()\n"
2877             "}",
2878             format("int q() {\n"
2879                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2880                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2881                    "  IPC_END_MESSAGE_MAP()\n"
2882                    "}"));
2883 
2884   // Same inside macros.
2885   EXPECT_EQ("#define LIST(L) \\\n"
2886             "  L(A)          \\\n"
2887             "  L(B)          \\\n"
2888             "  L(C)",
2889             format("#define LIST(L) \\\n"
2890                    "  L(A) \\\n"
2891                    "  L(B) \\\n"
2892                    "  L(C)",
2893                    getGoogleStyle()));
2894 
2895   // These must not be recognized as macros.
2896   EXPECT_EQ("int q() {\n"
2897             "  f(x);\n"
2898             "  f(x) {}\n"
2899             "  f(x)->g();\n"
2900             "  f(x)->*g();\n"
2901             "  f(x).g();\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) %= x;\n"
2908             "  f(x) &= x;\n"
2909             "  f(x) |= x;\n"
2910             "  f(x) ^= x;\n"
2911             "  f(x) >>= x;\n"
2912             "  f(x) <<= x;\n"
2913             "  f(x)[y].z();\n"
2914             "  LOG(INFO) << x;\n"
2915             "  ifstream(x) >> x;\n"
2916             "}\n",
2917             format("int q() {\n"
2918                    "  f(x)\n;\n"
2919                    "  f(x)\n {}\n"
2920                    "  f(x)\n->g();\n"
2921                    "  f(x)\n->*g();\n"
2922                    "  f(x)\n.g();\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 %= x;\n"
2929                    "  f(x)\n &= x;\n"
2930                    "  f(x)\n |= x;\n"
2931                    "  f(x)\n ^= x;\n"
2932                    "  f(x)\n >>= x;\n"
2933                    "  f(x)\n <<= x;\n"
2934                    "  f(x)\n[y].z();\n"
2935                    "  LOG(INFO)\n << x;\n"
2936                    "  ifstream(x)\n >> x;\n"
2937                    "}\n"));
2938   EXPECT_EQ("int q() {\n"
2939             "  F(x)\n"
2940             "  if (1) {\n"
2941             "  }\n"
2942             "  F(x)\n"
2943             "  while (1) {\n"
2944             "  }\n"
2945             "  F(x)\n"
2946             "  G(x);\n"
2947             "  F(x)\n"
2948             "  try {\n"
2949             "    Q();\n"
2950             "  } catch (...) {\n"
2951             "  }\n"
2952             "}\n",
2953             format("int q() {\n"
2954                    "F(x)\n"
2955                    "if (1) {}\n"
2956                    "F(x)\n"
2957                    "while (1) {}\n"
2958                    "F(x)\n"
2959                    "G(x);\n"
2960                    "F(x)\n"
2961                    "try { Q(); } catch (...) {}\n"
2962                    "}\n"));
2963   EXPECT_EQ("class A {\n"
2964             "  A() : t(0) {}\n"
2965             "  A(int i) noexcept() : {}\n"
2966             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2967             "  try : t(0) {\n"
2968             "  } catch (...) {\n"
2969             "  }\n"
2970             "};",
2971             format("class A {\n"
2972                    "  A()\n : t(0) {}\n"
2973                    "  A(int i)\n noexcept() : {}\n"
2974                    "  A(X x)\n"
2975                    "  try : t(0) {} catch (...) {}\n"
2976                    "};"));
2977   EXPECT_EQ("class SomeClass {\n"
2978             "public:\n"
2979             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2980             "};",
2981             format("class SomeClass {\n"
2982                    "public:\n"
2983                    "  SomeClass()\n"
2984                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2985                    "};"));
2986   EXPECT_EQ("class SomeClass {\n"
2987             "public:\n"
2988             "  SomeClass()\n"
2989             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2990             "};",
2991             format("class SomeClass {\n"
2992                    "public:\n"
2993                    "  SomeClass()\n"
2994                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2995                    "};",
2996                    getLLVMStyleWithColumns(40)));
2997 
2998   verifyFormat("MACRO(>)");
2999 }
3000 
3001 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
3002   verifyFormat("#define A \\\n"
3003                "  f({     \\\n"
3004                "    g();  \\\n"
3005                "  });",
3006                getLLVMStyleWithColumns(11));
3007 }
3008 
3009 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
3010   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
3011 }
3012 
3013 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
3014   verifyFormat("{\n  { a #c; }\n}");
3015 }
3016 
3017 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
3018   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
3019             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
3020   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
3021             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
3022 }
3023 
3024 TEST_F(FormatTest, EscapedNewlines) {
3025   EXPECT_EQ(
3026       "#define A \\\n  int i;  \\\n  int j;",
3027       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
3028   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
3029   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
3030   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
3031   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
3032 }
3033 
3034 TEST_F(FormatTest, DontCrashOnBlockComments) {
3035   EXPECT_EQ(
3036       "int xxxxxxxxx; /* "
3037       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
3038       "zzzzzz\n"
3039       "0*/",
3040       format("int xxxxxxxxx;                          /* "
3041              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
3042              "0*/"));
3043 }
3044 
3045 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
3046   verifyFormat("#define A \\\n"
3047                "  int v(  \\\n"
3048                "      a); \\\n"
3049                "  int i;",
3050                getLLVMStyleWithColumns(11));
3051 }
3052 
3053 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
3054   EXPECT_EQ(
3055       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
3056       "                      \\\n"
3057       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3058       "\n"
3059       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3060       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
3061       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
3062              "\\\n"
3063              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
3064              "  \n"
3065              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
3066              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
3067 }
3068 
3069 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
3070   EXPECT_EQ("int\n"
3071             "#define A\n"
3072             "    a;",
3073             format("int\n#define A\na;"));
3074   verifyFormat("functionCallTo(\n"
3075                "    someOtherFunction(\n"
3076                "        withSomeParameters, whichInSequence,\n"
3077                "        areLongerThanALine(andAnotherCall,\n"
3078                "#define A B\n"
3079                "                           withMoreParamters,\n"
3080                "                           whichStronglyInfluenceTheLayout),\n"
3081                "        andMoreParameters),\n"
3082                "    trailing);",
3083                getLLVMStyleWithColumns(69));
3084   verifyFormat("Foo::Foo()\n"
3085                "#ifdef BAR\n"
3086                "    : baz(0)\n"
3087                "#endif\n"
3088                "{\n"
3089                "}");
3090   verifyFormat("void f() {\n"
3091                "  if (true)\n"
3092                "#ifdef A\n"
3093                "    f(42);\n"
3094                "  x();\n"
3095                "#else\n"
3096                "    g();\n"
3097                "  x();\n"
3098                "#endif\n"
3099                "}");
3100   verifyFormat("void f(param1, param2,\n"
3101                "       param3,\n"
3102                "#ifdef A\n"
3103                "       param4(param5,\n"
3104                "#ifdef A1\n"
3105                "              param6,\n"
3106                "#ifdef A2\n"
3107                "              param7),\n"
3108                "#else\n"
3109                "              param8),\n"
3110                "       param9,\n"
3111                "#endif\n"
3112                "       param10,\n"
3113                "#endif\n"
3114                "       param11)\n"
3115                "#else\n"
3116                "       param12)\n"
3117                "#endif\n"
3118                "{\n"
3119                "  x();\n"
3120                "}",
3121                getLLVMStyleWithColumns(28));
3122   verifyFormat("#if 1\n"
3123                "int i;");
3124   verifyFormat("#if 1\n"
3125                "#endif\n"
3126                "#if 1\n"
3127                "#else\n"
3128                "#endif\n");
3129   verifyFormat("DEBUG({\n"
3130                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3131                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3132                "});\n"
3133                "#if a\n"
3134                "#else\n"
3135                "#endif");
3136 
3137   verifyIncompleteFormat("void f(\n"
3138                          "#if A\n"
3139                          "    );\n"
3140                          "#else\n"
3141                          "#endif");
3142 }
3143 
3144 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3145   verifyFormat("#endif\n"
3146                "#if B");
3147 }
3148 
3149 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3150   FormatStyle SingleLine = getLLVMStyle();
3151   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3152   verifyFormat("#if 0\n"
3153                "#elif 1\n"
3154                "#endif\n"
3155                "void foo() {\n"
3156                "  if (test) foo2();\n"
3157                "}",
3158                SingleLine);
3159 }
3160 
3161 TEST_F(FormatTest, LayoutBlockInsideParens) {
3162   verifyFormat("functionCall({ int i; });");
3163   verifyFormat("functionCall({\n"
3164                "  int i;\n"
3165                "  int j;\n"
3166                "});");
3167   verifyFormat("functionCall(\n"
3168                "    {\n"
3169                "      int i;\n"
3170                "      int j;\n"
3171                "    },\n"
3172                "    aaaa, bbbb, cccc);");
3173   verifyFormat("functionA(functionB({\n"
3174                "            int i;\n"
3175                "            int j;\n"
3176                "          }),\n"
3177                "          aaaa, bbbb, cccc);");
3178   verifyFormat("functionCall(\n"
3179                "    {\n"
3180                "      int i;\n"
3181                "      int j;\n"
3182                "    },\n"
3183                "    aaaa, bbbb, // comment\n"
3184                "    cccc);");
3185   verifyFormat("functionA(functionB({\n"
3186                "            int i;\n"
3187                "            int j;\n"
3188                "          }),\n"
3189                "          aaaa, bbbb, // comment\n"
3190                "          cccc);");
3191   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3192   verifyFormat("functionCall(aaaa, bbbb, {\n"
3193                "  int i;\n"
3194                "  int j;\n"
3195                "});");
3196   verifyFormat(
3197       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3198       "    {\n"
3199       "      int i; // break\n"
3200       "    },\n"
3201       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3202       "                                     ccccccccccccccccc));");
3203   verifyFormat("DEBUG({\n"
3204                "  if (a)\n"
3205                "    f();\n"
3206                "});");
3207 }
3208 
3209 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3210   EXPECT_EQ("SOME_MACRO { int i; }\n"
3211             "int i;",
3212             format("  SOME_MACRO  {int i;}  int i;"));
3213 }
3214 
3215 TEST_F(FormatTest, LayoutNestedBlocks) {
3216   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3217                "  struct s {\n"
3218                "    int i;\n"
3219                "  };\n"
3220                "  s kBitsToOs[] = {{10}};\n"
3221                "  for (int i = 0; i < 10; ++i)\n"
3222                "    return;\n"
3223                "}");
3224   verifyFormat("call(parameter, {\n"
3225                "  something();\n"
3226                "  // Comment using all columns.\n"
3227                "  somethingelse();\n"
3228                "});",
3229                getLLVMStyleWithColumns(40));
3230   verifyFormat("DEBUG( //\n"
3231                "    { f(); }, a);");
3232   verifyFormat("DEBUG( //\n"
3233                "    {\n"
3234                "      f(); //\n"
3235                "    },\n"
3236                "    a);");
3237 
3238   EXPECT_EQ("call(parameter, {\n"
3239             "  something();\n"
3240             "  // Comment too\n"
3241             "  // looooooooooong.\n"
3242             "  somethingElse();\n"
3243             "});",
3244             format("call(parameter, {\n"
3245                    "  something();\n"
3246                    "  // Comment too looooooooooong.\n"
3247                    "  somethingElse();\n"
3248                    "});",
3249                    getLLVMStyleWithColumns(29)));
3250   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3251   EXPECT_EQ("DEBUG({ // comment\n"
3252             "  int i;\n"
3253             "});",
3254             format("DEBUG({ // comment\n"
3255                    "int  i;\n"
3256                    "});"));
3257   EXPECT_EQ("DEBUG({\n"
3258             "  int i;\n"
3259             "\n"
3260             "  // comment\n"
3261             "  int j;\n"
3262             "});",
3263             format("DEBUG({\n"
3264                    "  int  i;\n"
3265                    "\n"
3266                    "  // comment\n"
3267                    "  int  j;\n"
3268                    "});"));
3269 
3270   verifyFormat("DEBUG({\n"
3271                "  if (a)\n"
3272                "    return;\n"
3273                "});");
3274   verifyGoogleFormat("DEBUG({\n"
3275                      "  if (a) return;\n"
3276                      "});");
3277   FormatStyle Style = getGoogleStyle();
3278   Style.ColumnLimit = 45;
3279   verifyFormat("Debug(aaaaa,\n"
3280                "      {\n"
3281                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3282                "      },\n"
3283                "      a);",
3284                Style);
3285 
3286   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
3287 
3288   verifyNoCrash("^{v^{a}}");
3289 }
3290 
3291 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
3292   EXPECT_EQ("#define MACRO()                     \\\n"
3293             "  Debug(aaa, /* force line break */ \\\n"
3294             "        {                           \\\n"
3295             "          int i;                    \\\n"
3296             "          int j;                    \\\n"
3297             "        })",
3298             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
3299                    "          {  int   i;  int  j;   })",
3300                    getGoogleStyle()));
3301 
3302   EXPECT_EQ("#define A                                       \\\n"
3303             "  [] {                                          \\\n"
3304             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
3305             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
3306             "  }",
3307             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
3308                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
3309                    getGoogleStyle()));
3310 }
3311 
3312 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3313   EXPECT_EQ("{}", format("{}"));
3314   verifyFormat("enum E {};");
3315   verifyFormat("enum E {}");
3316 }
3317 
3318 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
3319   FormatStyle Style = getLLVMStyle();
3320   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
3321   Style.MacroBlockEnd = "^[A-Z_]+_END$";
3322   verifyFormat("FOO_BEGIN\n"
3323                "  FOO_ENTRY\n"
3324                "FOO_END", Style);
3325   verifyFormat("FOO_BEGIN\n"
3326                "  NESTED_FOO_BEGIN\n"
3327                "    NESTED_FOO_ENTRY\n"
3328                "  NESTED_FOO_END\n"
3329                "FOO_END", Style);
3330   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
3331                "  int x;\n"
3332                "  x = 1;\n"
3333                "FOO_END(Baz)", Style);
3334 }
3335 
3336 //===----------------------------------------------------------------------===//
3337 // Line break tests.
3338 //===----------------------------------------------------------------------===//
3339 
3340 TEST_F(FormatTest, PreventConfusingIndents) {
3341   verifyFormat(
3342       "void f() {\n"
3343       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3344       "                         parameter, parameter, parameter)),\n"
3345       "                     SecondLongCall(parameter));\n"
3346       "}");
3347   verifyFormat(
3348       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3349       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3350       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3351       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3352   verifyFormat(
3353       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3354       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3355       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3356       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3357   verifyFormat(
3358       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3359       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3360       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3361       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3362   verifyFormat("int a = bbbb && ccc && fffff(\n"
3363                "#define A Just forcing a new line\n"
3364                "                           ddd);");
3365 }
3366 
3367 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3368   verifyFormat(
3369       "bool aaaaaaa =\n"
3370       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3371       "    bbbbbbbb();");
3372   verifyFormat(
3373       "bool aaaaaaa =\n"
3374       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3375       "    bbbbbbbb();");
3376 
3377   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3378                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3379                "    ccccccccc == ddddddddddd;");
3380   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3381                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3382                "    ccccccccc == ddddddddddd;");
3383   verifyFormat(
3384       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3385       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3386       "    ccccccccc == ddddddddddd;");
3387 
3388   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3389                "                 aaaaaa) &&\n"
3390                "         bbbbbb && cccccc;");
3391   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3392                "                 aaaaaa) >>\n"
3393                "         bbbbbb;");
3394   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3395                "    SourceMgr.getSpellingColumnNumber(\n"
3396                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3397                "    1);");
3398 
3399   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3400                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3401                "    cccccc) {\n}");
3402   verifyFormat("b = a &&\n"
3403                "    // Comment\n"
3404                "    b.c && d;");
3405 
3406   // If the LHS of a comparison is not a binary expression itself, the
3407   // additional linebreak confuses many people.
3408   verifyFormat(
3409       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3410       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3411       "}");
3412   verifyFormat(
3413       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3414       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3415       "}");
3416   verifyFormat(
3417       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3418       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3419       "}");
3420   // Even explicit parentheses stress the precedence enough to make the
3421   // additional break unnecessary.
3422   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3423                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3424                "}");
3425   // This cases is borderline, but with the indentation it is still readable.
3426   verifyFormat(
3427       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3428       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3429       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3430       "}",
3431       getLLVMStyleWithColumns(75));
3432 
3433   // If the LHS is a binary expression, we should still use the additional break
3434   // as otherwise the formatting hides the operator precedence.
3435   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3436                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3437                "    5) {\n"
3438                "}");
3439 
3440   FormatStyle OnePerLine = getLLVMStyle();
3441   OnePerLine.BinPackParameters = false;
3442   verifyFormat(
3443       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3444       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3445       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3446       OnePerLine);
3447 }
3448 
3449 TEST_F(FormatTest, ExpressionIndentation) {
3450   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3451                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3452                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3453                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3454                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3455                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3456                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3457                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3458                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3459   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3460                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3461                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3462                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3463   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3464                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3465                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3466                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3467   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3468                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3469                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3470                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3471   verifyFormat("if () {\n"
3472                "} else if (aaaaa &&\n"
3473                "           bbbbb > // break\n"
3474                "               ccccc) {\n"
3475                "}");
3476 
3477   // Presence of a trailing comment used to change indentation of b.
3478   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3479                "       b;\n"
3480                "return aaaaaaaaaaaaaaaaaaa +\n"
3481                "       b; //",
3482                getLLVMStyleWithColumns(30));
3483 }
3484 
3485 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3486   // Not sure what the best system is here. Like this, the LHS can be found
3487   // immediately above an operator (everything with the same or a higher
3488   // indent). The RHS is aligned right of the operator and so compasses
3489   // everything until something with the same indent as the operator is found.
3490   // FIXME: Is this a good system?
3491   FormatStyle Style = getLLVMStyle();
3492   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3493   verifyFormat(
3494       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3495       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3496       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3497       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3498       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3499       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3500       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3501       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3502       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3503       Style);
3504   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3505                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3506                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3507                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3508                Style);
3509   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3510                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3511                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3512                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3513                Style);
3514   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3515                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3516                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3517                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3518                Style);
3519   verifyFormat("if () {\n"
3520                "} else if (aaaaa\n"
3521                "           && bbbbb // break\n"
3522                "                  > ccccc) {\n"
3523                "}",
3524                Style);
3525   verifyFormat("return (a)\n"
3526                "       // comment\n"
3527                "       + b;",
3528                Style);
3529   verifyFormat(
3530       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3531       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3532       "             + cc;",
3533       Style);
3534 
3535   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3536                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3537                Style);
3538 
3539   // Forced by comments.
3540   verifyFormat(
3541       "unsigned ContentSize =\n"
3542       "    sizeof(int16_t)   // DWARF ARange version number\n"
3543       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3544       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3545       "    + sizeof(int8_t); // Segment Size (in bytes)");
3546 
3547   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3548                "       == boost::fusion::at_c<1>(iiii).second;",
3549                Style);
3550 
3551   Style.ColumnLimit = 60;
3552   verifyFormat("zzzzzzzzzz\n"
3553                "    = bbbbbbbbbbbbbbbbb\n"
3554                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3555                Style);
3556 }
3557 
3558 TEST_F(FormatTest, NoOperandAlignment) {
3559   FormatStyle Style = getLLVMStyle();
3560   Style.AlignOperands = false;
3561   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3562   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3563                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3564                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3565                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3566                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3567                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3568                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3569                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3570                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3571                Style);
3572 
3573   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3574                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3575                "    + cc;",
3576                Style);
3577   verifyFormat("int a = aa\n"
3578                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3579                "        * cccccccccccccccccccccccccccccccccccc;",
3580                Style);
3581 
3582   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3583   verifyFormat("return (a > b\n"
3584                "    // comment1\n"
3585                "    // comment2\n"
3586                "    || c);",
3587                Style);
3588 }
3589 
3590 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3591   FormatStyle Style = getLLVMStyle();
3592   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3593   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3594                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3595                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3596                Style);
3597 }
3598 
3599 TEST_F(FormatTest, ConstructorInitializers) {
3600   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3601   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3602                getLLVMStyleWithColumns(45));
3603   verifyFormat("Constructor()\n"
3604                "    : Inttializer(FitsOnTheLine) {}",
3605                getLLVMStyleWithColumns(44));
3606   verifyFormat("Constructor()\n"
3607                "    : Inttializer(FitsOnTheLine) {}",
3608                getLLVMStyleWithColumns(43));
3609 
3610   verifyFormat("template <typename T>\n"
3611                "Constructor() : Initializer(FitsOnTheLine) {}",
3612                getLLVMStyleWithColumns(45));
3613 
3614   verifyFormat(
3615       "SomeClass::Constructor()\n"
3616       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3617 
3618   verifyFormat(
3619       "SomeClass::Constructor()\n"
3620       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3621       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3622   verifyFormat(
3623       "SomeClass::Constructor()\n"
3624       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3625       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3626   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3627                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3628                "    : aaaaaaaaaa(aaaaaa) {}");
3629 
3630   verifyFormat("Constructor()\n"
3631                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3632                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3633                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3634                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3635 
3636   verifyFormat("Constructor()\n"
3637                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3638                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3639 
3640   verifyFormat("Constructor(int Parameter = 0)\n"
3641                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3642                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3643   verifyFormat("Constructor()\n"
3644                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3645                "}",
3646                getLLVMStyleWithColumns(60));
3647   verifyFormat("Constructor()\n"
3648                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3649                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3650 
3651   // Here a line could be saved by splitting the second initializer onto two
3652   // lines, but that is not desirable.
3653   verifyFormat("Constructor()\n"
3654                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3655                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3656                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3657 
3658   FormatStyle OnePerLine = getLLVMStyle();
3659   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3660   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3661   verifyFormat("SomeClass::Constructor()\n"
3662                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3663                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3664                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3665                OnePerLine);
3666   verifyFormat("SomeClass::Constructor()\n"
3667                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3668                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3669                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3670                OnePerLine);
3671   verifyFormat("MyClass::MyClass(int var)\n"
3672                "    : some_var_(var),            // 4 space indent\n"
3673                "      some_other_var_(var + 1) { // lined up\n"
3674                "}",
3675                OnePerLine);
3676   verifyFormat("Constructor()\n"
3677                "    : aaaaa(aaaaaa),\n"
3678                "      aaaaa(aaaaaa),\n"
3679                "      aaaaa(aaaaaa),\n"
3680                "      aaaaa(aaaaaa),\n"
3681                "      aaaaa(aaaaaa) {}",
3682                OnePerLine);
3683   verifyFormat("Constructor()\n"
3684                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3685                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3686                OnePerLine);
3687   OnePerLine.BinPackParameters = false;
3688   verifyFormat(
3689       "Constructor()\n"
3690       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3691       "          aaaaaaaaaaa().aaa(),\n"
3692       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3693       OnePerLine);
3694   OnePerLine.ColumnLimit = 60;
3695   verifyFormat("Constructor()\n"
3696                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3697                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3698                OnePerLine);
3699 
3700   EXPECT_EQ("Constructor()\n"
3701             "    : // Comment forcing unwanted break.\n"
3702             "      aaaa(aaaa) {}",
3703             format("Constructor() :\n"
3704                    "    // Comment forcing unwanted break.\n"
3705                    "    aaaa(aaaa) {}"));
3706 }
3707 
3708 TEST_F(FormatTest, MemoizationTests) {
3709   // This breaks if the memoization lookup does not take \c Indent and
3710   // \c LastSpace into account.
3711   verifyFormat(
3712       "extern CFRunLoopTimerRef\n"
3713       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3714       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3715       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3716       "                     CFRunLoopTimerContext *context) {}");
3717 
3718   // Deep nesting somewhat works around our memoization.
3719   verifyFormat(
3720       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3721       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3722       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3723       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3724       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3725       getLLVMStyleWithColumns(65));
3726   verifyFormat(
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(\n"
3746       "                                        aaaaa,\n"
3747       "                                        aaaaa(\n"
3748       "                                            aaaaa,\n"
3749       "                                            aaaaa(\n"
3750       "                                                aaaaa,\n"
3751       "                                                aaaaa))))))))))));",
3752       getLLVMStyleWithColumns(65));
3753   verifyFormat(
3754       "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"
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),\n"
3766       "            a),\n"
3767       "          a),\n"
3768       "        a),\n"
3769       "      a),\n"
3770       "    a),\n"
3771       "  a)",
3772       getLLVMStyleWithColumns(65));
3773 
3774   // This test takes VERY long when memoization is broken.
3775   FormatStyle OnePerLine = getLLVMStyle();
3776   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3777   OnePerLine.BinPackParameters = false;
3778   std::string input = "Constructor()\n"
3779                       "    : aaaa(a,\n";
3780   for (unsigned i = 0, e = 80; i != e; ++i) {
3781     input += "           a,\n";
3782   }
3783   input += "           a) {}";
3784   verifyFormat(input, OnePerLine);
3785 }
3786 
3787 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3788   verifyFormat(
3789       "void f() {\n"
3790       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3791       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3792       "    f();\n"
3793       "}");
3794   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3795                "    Intervals[i - 1].getRange().getLast()) {\n}");
3796 }
3797 
3798 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3799   // Principially, we break function declarations in a certain order:
3800   // 1) break amongst arguments.
3801   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3802                "                              Cccccccccccccc cccccccccccccc);");
3803   verifyFormat("template <class TemplateIt>\n"
3804                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3805                "                            TemplateIt *stop) {}");
3806 
3807   // 2) break after return type.
3808   verifyFormat(
3809       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3810       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3811       getGoogleStyle());
3812 
3813   // 3) break after (.
3814   verifyFormat(
3815       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3816       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3817       getGoogleStyle());
3818 
3819   // 4) break before after nested name specifiers.
3820   verifyFormat(
3821       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3822       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3823       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3824       getGoogleStyle());
3825 
3826   // However, there are exceptions, if a sufficient amount of lines can be
3827   // saved.
3828   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3829   // more adjusting.
3830   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3831                "                                  Cccccccccccccc cccccccccc,\n"
3832                "                                  Cccccccccccccc cccccccccc,\n"
3833                "                                  Cccccccccccccc cccccccccc,\n"
3834                "                                  Cccccccccccccc cccccccccc);");
3835   verifyFormat(
3836       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3837       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3838       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3839       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3840       getGoogleStyle());
3841   verifyFormat(
3842       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3843       "                                          Cccccccccccccc cccccccccc,\n"
3844       "                                          Cccccccccccccc cccccccccc,\n"
3845       "                                          Cccccccccccccc cccccccccc,\n"
3846       "                                          Cccccccccccccc cccccccccc,\n"
3847       "                                          Cccccccccccccc cccccccccc,\n"
3848       "                                          Cccccccccccccc cccccccccc);");
3849   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3850                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3851                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3852                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3853                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3854 
3855   // Break after multi-line parameters.
3856   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3857                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3858                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3859                "    bbbb bbbb);");
3860   verifyFormat("void SomeLoooooooooooongFunction(\n"
3861                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3862                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3863                "    int bbbbbbbbbbbbb);");
3864 
3865   // Treat overloaded operators like other functions.
3866   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3867                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3868   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3869                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3870   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3871                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3872   verifyGoogleFormat(
3873       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3874       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3875   verifyGoogleFormat(
3876       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3877       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3878   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3879                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3880   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3881                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3882   verifyGoogleFormat(
3883       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3884       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3885       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3886   verifyGoogleFormat(
3887       "template <typename T>\n"
3888       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3889       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3890       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3891 
3892   FormatStyle Style = getLLVMStyle();
3893   Style.PointerAlignment = FormatStyle::PAS_Left;
3894   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3895                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3896                Style);
3897   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3898                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3899                Style);
3900 }
3901 
3902 TEST_F(FormatTest, TrailingReturnType) {
3903   verifyFormat("auto foo() -> int;\n");
3904   verifyFormat("struct S {\n"
3905                "  auto bar() const -> int;\n"
3906                "};");
3907   verifyFormat("template <size_t Order, typename T>\n"
3908                "auto load_img(const std::string &filename)\n"
3909                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3910   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3911                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3912   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3913   verifyFormat("template <typename T>\n"
3914                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3915                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3916 
3917   // Not trailing return types.
3918   verifyFormat("void f() { auto a = b->c(); }");
3919 }
3920 
3921 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3922   // Avoid breaking before trailing 'const' or other trailing annotations, if
3923   // they are not function-like.
3924   FormatStyle Style = getGoogleStyle();
3925   Style.ColumnLimit = 47;
3926   verifyFormat("void someLongFunction(\n"
3927                "    int someLoooooooooooooongParameter) const {\n}",
3928                getLLVMStyleWithColumns(47));
3929   verifyFormat("LoooooongReturnType\n"
3930                "someLoooooooongFunction() const {}",
3931                getLLVMStyleWithColumns(47));
3932   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3933                "    const {}",
3934                Style);
3935   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3936                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3937   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3938                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3939   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3940                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3941   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3942                "                   aaaaaaaaaaa aaaaa) const override;");
3943   verifyGoogleFormat(
3944       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3945       "    const override;");
3946 
3947   // Even if the first parameter has to be wrapped.
3948   verifyFormat("void someLongFunction(\n"
3949                "    int someLongParameter) const {}",
3950                getLLVMStyleWithColumns(46));
3951   verifyFormat("void someLongFunction(\n"
3952                "    int someLongParameter) const {}",
3953                Style);
3954   verifyFormat("void someLongFunction(\n"
3955                "    int someLongParameter) override {}",
3956                Style);
3957   verifyFormat("void someLongFunction(\n"
3958                "    int someLongParameter) OVERRIDE {}",
3959                Style);
3960   verifyFormat("void someLongFunction(\n"
3961                "    int someLongParameter) final {}",
3962                Style);
3963   verifyFormat("void someLongFunction(\n"
3964                "    int someLongParameter) FINAL {}",
3965                Style);
3966   verifyFormat("void someLongFunction(\n"
3967                "    int parameter) const override {}",
3968                Style);
3969 
3970   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3971   verifyFormat("void someLongFunction(\n"
3972                "    int someLongParameter) const\n"
3973                "{\n"
3974                "}",
3975                Style);
3976 
3977   // Unless these are unknown annotations.
3978   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3979                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3980                "    LONG_AND_UGLY_ANNOTATION;");
3981 
3982   // Breaking before function-like trailing annotations is fine to keep them
3983   // close to their arguments.
3984   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3985                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3986   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3987                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3988   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3989                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3990   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3991                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3992   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3993 
3994   verifyFormat(
3995       "void aaaaaaaaaaaaaaaaaa()\n"
3996       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3997       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3998   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3999                "    __attribute__((unused));");
4000   verifyGoogleFormat(
4001       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4002       "    GUARDED_BY(aaaaaaaaaaaa);");
4003   verifyGoogleFormat(
4004       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4005       "    GUARDED_BY(aaaaaaaaaaaa);");
4006   verifyGoogleFormat(
4007       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4008       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4009   verifyGoogleFormat(
4010       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
4011       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
4012 }
4013 
4014 TEST_F(FormatTest, FunctionAnnotations) {
4015   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4016                "int OldFunction(const string &parameter) {}");
4017   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4018                "string OldFunction(const string &parameter) {}");
4019   verifyFormat("template <typename T>\n"
4020                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
4021                "string OldFunction(const string &parameter) {}");
4022 
4023   // Not function annotations.
4024   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4025                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
4026   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
4027                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
4028   verifyFormat("MACRO(abc).function() // wrap\n"
4029                "    << abc;");
4030   verifyFormat("MACRO(abc)->function() // wrap\n"
4031                "    << abc;");
4032   verifyFormat("MACRO(abc)::function() // wrap\n"
4033                "    << abc;");
4034 }
4035 
4036 TEST_F(FormatTest, BreaksDesireably) {
4037   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4038                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
4039                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
4040   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4041                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
4042                "}");
4043 
4044   verifyFormat(
4045       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4046       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
4047 
4048   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4049                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4050                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4051 
4052   verifyFormat(
4053       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4054       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4055       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4056       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
4057 
4058   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4059                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4060 
4061   verifyFormat(
4062       "void f() {\n"
4063       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
4064       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4065       "}");
4066   verifyFormat(
4067       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4068       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4069   verifyFormat(
4070       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4071       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4072   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4073                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4074                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4075 
4076   // Indent consistently independent of call expression and unary operator.
4077   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4078                "    dddddddddddddddddddddddddddddd));");
4079   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
4080                "    dddddddddddddddddddddddddddddd));");
4081   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
4082                "    dddddddddddddddddddddddddddddd));");
4083 
4084   // This test case breaks on an incorrect memoization, i.e. an optimization not
4085   // taking into account the StopAt value.
4086   verifyFormat(
4087       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4088       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4089       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
4090       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4091 
4092   verifyFormat("{\n  {\n    {\n"
4093                "      Annotation.SpaceRequiredBefore =\n"
4094                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
4095                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
4096                "    }\n  }\n}");
4097 
4098   // Break on an outer level if there was a break on an inner level.
4099   EXPECT_EQ("f(g(h(a, // comment\n"
4100             "      b, c),\n"
4101             "    d, e),\n"
4102             "  x, y);",
4103             format("f(g(h(a, // comment\n"
4104                    "    b, c), d, e), x, y);"));
4105 
4106   // Prefer breaking similar line breaks.
4107   verifyFormat(
4108       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4109       "                             NSTrackingMouseEnteredAndExited |\n"
4110       "                             NSTrackingActiveAlways;");
4111 }
4112 
4113 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4114   FormatStyle NoBinPacking = getGoogleStyle();
4115   NoBinPacking.BinPackParameters = false;
4116   NoBinPacking.BinPackArguments = true;
4117   verifyFormat("void f() {\n"
4118                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4119                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4120                "}",
4121                NoBinPacking);
4122   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4123                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4124                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4125                NoBinPacking);
4126 
4127   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4128   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4129                "                        vector<int> bbbbbbbbbbbbbbb);",
4130                NoBinPacking);
4131   // FIXME: This behavior difference is probably not wanted. However, currently
4132   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4133   // template arguments from BreakBeforeParameter being set because of the
4134   // one-per-line formatting.
4135   verifyFormat(
4136       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4137       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4138       NoBinPacking);
4139   verifyFormat(
4140       "void fffffffffff(\n"
4141       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4142       "        aaaaaaaaaa);");
4143 }
4144 
4145 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4146   FormatStyle NoBinPacking = getGoogleStyle();
4147   NoBinPacking.BinPackParameters = false;
4148   NoBinPacking.BinPackArguments = false;
4149   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4150                "  aaaaaaaaaaaaaaaaaaaa,\n"
4151                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4152                NoBinPacking);
4153   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4154                "        aaaaaaaaaaaaa,\n"
4155                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4156                NoBinPacking);
4157   verifyFormat(
4158       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4159       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4160       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4161       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4162       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4163       NoBinPacking);
4164   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4165                "    .aaaaaaaaaaaaaaaaaa();",
4166                NoBinPacking);
4167   verifyFormat("void f() {\n"
4168                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4169                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4170                "}",
4171                NoBinPacking);
4172 
4173   verifyFormat(
4174       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4175       "             aaaaaaaaaaaa,\n"
4176       "             aaaaaaaaaaaa);",
4177       NoBinPacking);
4178   verifyFormat(
4179       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4180       "                               ddddddddddddddddddddddddddddd),\n"
4181       "             test);",
4182       NoBinPacking);
4183 
4184   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4185                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4186                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4187                "    aaaaaaaaaaaaaaaaaa;",
4188                NoBinPacking);
4189   verifyFormat("a(\"a\"\n"
4190                "  \"a\",\n"
4191                "  a);");
4192 
4193   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4194   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4195                "                aaaaaaaaa,\n"
4196                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4197                NoBinPacking);
4198   verifyFormat(
4199       "void f() {\n"
4200       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4201       "      .aaaaaaa();\n"
4202       "}",
4203       NoBinPacking);
4204   verifyFormat(
4205       "template <class SomeType, class SomeOtherType>\n"
4206       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4207       NoBinPacking);
4208 }
4209 
4210 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4211   FormatStyle Style = getLLVMStyleWithColumns(15);
4212   Style.ExperimentalAutoDetectBinPacking = true;
4213   EXPECT_EQ("aaa(aaaa,\n"
4214             "    aaaa,\n"
4215             "    aaaa);\n"
4216             "aaa(aaaa,\n"
4217             "    aaaa,\n"
4218             "    aaaa);",
4219             format("aaa(aaaa,\n" // one-per-line
4220                    "  aaaa,\n"
4221                    "    aaaa  );\n"
4222                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4223                    Style));
4224   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4225             "    aaaa);\n"
4226             "aaa(aaaa, aaaa,\n"
4227             "    aaaa);",
4228             format("aaa(aaaa,  aaaa,\n" // bin-packed
4229                    "    aaaa  );\n"
4230                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4231                    Style));
4232 }
4233 
4234 TEST_F(FormatTest, FormatsBuilderPattern) {
4235   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4236                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4237                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4238                "    .StartsWith(\".init\", ORDER_INIT)\n"
4239                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4240                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4241                "    .Default(ORDER_TEXT);\n");
4242 
4243   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4244                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4245   verifyFormat(
4246       "aaaaaaa->aaaaaaa\n"
4247       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4248       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4249       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4250   verifyFormat(
4251       "aaaaaaa->aaaaaaa\n"
4252       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4253       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4254   verifyFormat(
4255       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4256       "    aaaaaaaaaaaaaa);");
4257   verifyFormat(
4258       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4259       "    aaaaaa->aaaaaaaaaaaa()\n"
4260       "        ->aaaaaaaaaaaaaaaa(\n"
4261       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4262       "        ->aaaaaaaaaaaaaaaaa();");
4263   verifyGoogleFormat(
4264       "void f() {\n"
4265       "  someo->Add((new util::filetools::Handler(dir))\n"
4266       "                 ->OnEvent1(NewPermanentCallback(\n"
4267       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4268       "                 ->OnEvent2(NewPermanentCallback(\n"
4269       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4270       "                 ->OnEvent3(NewPermanentCallback(\n"
4271       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4272       "                 ->OnEvent5(NewPermanentCallback(\n"
4273       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4274       "                 ->OnEvent6(NewPermanentCallback(\n"
4275       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4276       "}");
4277 
4278   verifyFormat(
4279       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4280   verifyFormat("aaaaaaaaaaaaaaa()\n"
4281                "    .aaaaaaaaaaaaaaa()\n"
4282                "    .aaaaaaaaaaaaaaa()\n"
4283                "    .aaaaaaaaaaaaaaa()\n"
4284                "    .aaaaaaaaaaaaaaa();");
4285   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4286                "    .aaaaaaaaaaaaaaa()\n"
4287                "    .aaaaaaaaaaaaaaa()\n"
4288                "    .aaaaaaaaaaaaaaa();");
4289   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4290                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4291                "    .aaaaaaaaaaaaaaa();");
4292   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4293                "    ->aaaaaaaaaaaaaae(0)\n"
4294                "    ->aaaaaaaaaaaaaaa();");
4295 
4296   // Don't linewrap after very short segments.
4297   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4298                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4299                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4300   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4301                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4302                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4303   verifyFormat("aaa()\n"
4304                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4305                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4306                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4307 
4308   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4309                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4310                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4311   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4312                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4313                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4314 
4315   // Prefer not to break after empty parentheses.
4316   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4317                "    First->LastNewlineOffset);");
4318 
4319   // Prefer not to create "hanging" indents.
4320   verifyFormat(
4321       "return !soooooooooooooome_map\n"
4322       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4323       "            .second;");
4324   verifyFormat(
4325       "return aaaaaaaaaaaaaaaa\n"
4326       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4327       "    .aaaa(aaaaaaaaaaaaaa);");
4328   // No hanging indent here.
4329   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4330                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4331   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4332                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4333   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4334                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4335                getLLVMStyleWithColumns(60));
4336   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4337                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4338                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4339                getLLVMStyleWithColumns(59));
4340   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4341                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4342                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4343 }
4344 
4345 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4346   verifyFormat(
4347       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4348       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4349   verifyFormat(
4350       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4351       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4352 
4353   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4354                "    ccccccccccccccccccccccccc) {\n}");
4355   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4356                "    ccccccccccccccccccccccccc) {\n}");
4357 
4358   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4359                "    ccccccccccccccccccccccccc) {\n}");
4360   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4361                "    ccccccccccccccccccccccccc) {\n}");
4362 
4363   verifyFormat(
4364       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4365       "    ccccccccccccccccccccccccc) {\n}");
4366   verifyFormat(
4367       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4368       "    ccccccccccccccccccccccccc) {\n}");
4369 
4370   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4371                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4372                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4373                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4374   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4375                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4376                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4377                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4378 
4379   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4380                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4381                "    aaaaaaaaaaaaaaa != aa) {\n}");
4382   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4383                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4384                "    aaaaaaaaaaaaaaa != aa) {\n}");
4385 }
4386 
4387 TEST_F(FormatTest, BreaksAfterAssignments) {
4388   verifyFormat(
4389       "unsigned Cost =\n"
4390       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4391       "                        SI->getPointerAddressSpaceee());\n");
4392   verifyFormat(
4393       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4394       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4395 
4396   verifyFormat(
4397       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4398       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4399   verifyFormat("unsigned OriginalStartColumn =\n"
4400                "    SourceMgr.getSpellingColumnNumber(\n"
4401                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4402                "    1;");
4403 }
4404 
4405 TEST_F(FormatTest, AlignsAfterAssignments) {
4406   verifyFormat(
4407       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4408       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4409   verifyFormat(
4410       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4411       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4412   verifyFormat(
4413       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4414       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4415   verifyFormat(
4416       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4417       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4418   verifyFormat(
4419       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4420       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4421       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4422 }
4423 
4424 TEST_F(FormatTest, AlignsAfterReturn) {
4425   verifyFormat(
4426       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4427       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4428   verifyFormat(
4429       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4430       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4431   verifyFormat(
4432       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4433       "       aaaaaaaaaaaaaaaaaaaaaa();");
4434   verifyFormat(
4435       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4436       "        aaaaaaaaaaaaaaaaaaaaaa());");
4437   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4438                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4439   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4440                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4441                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4442   verifyFormat("return\n"
4443                "    // true if code is one of a or b.\n"
4444                "    code == a || code == b;");
4445 }
4446 
4447 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4448   verifyFormat(
4449       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4450       "                                                aaaaaaaaa aaaaaaa) {}");
4451   verifyFormat(
4452       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4453       "                                               aaaaaaaaaaa aaaaaaaaa);");
4454   verifyFormat(
4455       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4456       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4457   FormatStyle Style = getLLVMStyle();
4458   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4459   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4460                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4461                Style);
4462   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4463                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4464                Style);
4465   verifyFormat("SomeLongVariableName->someFunction(\n"
4466                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4467                Style);
4468   verifyFormat(
4469       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4470       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4471       Style);
4472   verifyFormat(
4473       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4474       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4475       Style);
4476   verifyFormat(
4477       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4478       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4479       Style);
4480 
4481   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4482                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4483                "        b));",
4484                Style);
4485 
4486   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4487   Style.BinPackArguments = false;
4488   Style.BinPackParameters = false;
4489   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4490                "    aaaaaaaaaaa aaaaaaaa,\n"
4491                "    aaaaaaaaa aaaaaaa,\n"
4492                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4493                Style);
4494   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4495                "    aaaaaaaaaaa aaaaaaaaa,\n"
4496                "    aaaaaaaaaaa aaaaaaaaa,\n"
4497                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4498                Style);
4499   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4500                "    aaaaaaaaaaaaaaa,\n"
4501                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4502                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4503                Style);
4504   verifyFormat(
4505       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4506       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4507       Style);
4508   verifyFormat(
4509       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4510       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4511       Style);
4512   verifyFormat(
4513       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4514       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4515       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4516       "    aaaaaaaaaaaaaaaa);",
4517       Style);
4518   verifyFormat(
4519       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4520       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4521       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4522       "    aaaaaaaaaaaaaaaa);",
4523       Style);
4524 }
4525 
4526 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4527   FormatStyle Style = getLLVMStyleWithColumns(40);
4528   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4529                "          bbbbbbbbbbbbbbbbbbbbbb);",
4530                Style);
4531   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4532   Style.AlignOperands = false;
4533   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4534                "          bbbbbbbbbbbbbbbbbbbbbb);",
4535                Style);
4536   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4537   Style.AlignOperands = true;
4538   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4539                "          bbbbbbbbbbbbbbbbbbbbbb);",
4540                Style);
4541   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4542   Style.AlignOperands = false;
4543   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4544                "    bbbbbbbbbbbbbbbbbbbbbb);",
4545                Style);
4546 }
4547 
4548 TEST_F(FormatTest, BreaksConditionalExpressions) {
4549   verifyFormat(
4550       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4551       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4552       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4553   verifyFormat(
4554       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4555       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4556   verifyFormat(
4557       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4558       "                                                    : aaaaaaaaaaaaa);");
4559   verifyFormat(
4560       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4561       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4562       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4563       "                   aaaaaaaaaaaaa);");
4564   verifyFormat(
4565       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4566       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4567       "                   aaaaaaaaaaaaa);");
4568   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4569                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4570                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4571                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4572                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4573   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4574                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4575                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4576                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4577                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4578                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4579                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4580   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4581                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4582                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4583                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4584                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4585   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4586                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4587                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4588   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4589                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4590                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4591                "        : aaaaaaaaaaaaaaaa;");
4592   verifyFormat(
4593       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4594       "    ? aaaaaaaaaaaaaaa\n"
4595       "    : aaaaaaaaaaaaaaa;");
4596   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4597                "          aaaaaaaaa\n"
4598                "      ? b\n"
4599                "      : c);");
4600   verifyFormat("return aaaa == bbbb\n"
4601                "           // comment\n"
4602                "           ? aaaa\n"
4603                "           : bbbb;");
4604   verifyFormat("unsigned Indent =\n"
4605                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4606                "                              ? IndentForLevel[TheLine.Level]\n"
4607                "                              : TheLine * 2,\n"
4608                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4609                getLLVMStyleWithColumns(70));
4610   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4611                "                  ? aaaaaaaaaaaaaaa\n"
4612                "                  : bbbbbbbbbbbbbbb //\n"
4613                "                        ? ccccccccccccccc\n"
4614                "                        : ddddddddddddddd;");
4615   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4616                "                  ? aaaaaaaaaaaaaaa\n"
4617                "                  : (bbbbbbbbbbbbbbb //\n"
4618                "                         ? ccccccccccccccc\n"
4619                "                         : ddddddddddddddd);");
4620   verifyFormat(
4621       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4622       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4623       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4624       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4625       "                                      : aaaaaaaaaa;");
4626   verifyFormat(
4627       "aaaaaa = aaaaaaaaaaaa\n"
4628       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4629       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4630       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4631 
4632   FormatStyle NoBinPacking = getLLVMStyle();
4633   NoBinPacking.BinPackArguments = false;
4634   verifyFormat(
4635       "void f() {\n"
4636       "  g(aaa,\n"
4637       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4638       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4639       "        ? aaaaaaaaaaaaaaa\n"
4640       "        : aaaaaaaaaaaaaaa);\n"
4641       "}",
4642       NoBinPacking);
4643   verifyFormat(
4644       "void f() {\n"
4645       "  g(aaa,\n"
4646       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4648       "        ?: aaaaaaaaaaaaaaa);\n"
4649       "}",
4650       NoBinPacking);
4651 
4652   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4653                "             // comment.\n"
4654                "             ccccccccccccccccccccccccccccccccccccccc\n"
4655                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4656                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4657 
4658   // Assignments in conditional expressions. Apparently not uncommon :-(.
4659   verifyFormat("return a != b\n"
4660                "           // comment\n"
4661                "           ? a = b\n"
4662                "           : a = b;");
4663   verifyFormat("return a != b\n"
4664                "           // comment\n"
4665                "           ? a = a != b\n"
4666                "                     // comment\n"
4667                "                     ? a = b\n"
4668                "                     : a\n"
4669                "           : a;\n");
4670   verifyFormat("return a != b\n"
4671                "           // comment\n"
4672                "           ? a\n"
4673                "           : a = a != b\n"
4674                "                     // comment\n"
4675                "                     ? a = b\n"
4676                "                     : a;");
4677 }
4678 
4679 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4680   FormatStyle Style = getLLVMStyle();
4681   Style.BreakBeforeTernaryOperators = false;
4682   Style.ColumnLimit = 70;
4683   verifyFormat(
4684       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4685       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4686       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4687       Style);
4688   verifyFormat(
4689       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4690       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4691       Style);
4692   verifyFormat(
4693       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4694       "                                                      aaaaaaaaaaaaa);",
4695       Style);
4696   verifyFormat(
4697       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4698       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4699       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4700       "                   aaaaaaaaaaaaa);",
4701       Style);
4702   verifyFormat(
4703       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4704       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4705       "                   aaaaaaaaaaaaa);",
4706       Style);
4707   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4708                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4709                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4710                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4711                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4712                Style);
4713   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4714                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4715                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4716                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4717                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4718                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4719                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4720                Style);
4721   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4722                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4723                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4724                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4725                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4726                Style);
4727   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4728                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4729                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4730                Style);
4731   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4732                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4733                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4734                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4735                Style);
4736   verifyFormat(
4737       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4738       "    aaaaaaaaaaaaaaa :\n"
4739       "    aaaaaaaaaaaaaaa;",
4740       Style);
4741   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4742                "          aaaaaaaaa ?\n"
4743                "      b :\n"
4744                "      c);",
4745                Style);
4746   verifyFormat(
4747       "unsigned Indent =\n"
4748       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4749       "                              IndentForLevel[TheLine.Level] :\n"
4750       "                              TheLine * 2,\n"
4751       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4752       Style);
4753   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4754                "                  aaaaaaaaaaaaaaa :\n"
4755                "                  bbbbbbbbbbbbbbb ? //\n"
4756                "                      ccccccccccccccc :\n"
4757                "                      ddddddddddddddd;",
4758                Style);
4759   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4760                "                  aaaaaaaaaaaaaaa :\n"
4761                "                  (bbbbbbbbbbbbbbb ? //\n"
4762                "                       ccccccccccccccc :\n"
4763                "                       ddddddddddddddd);",
4764                Style);
4765   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4766                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4767                "            ccccccccccccccccccccccccccc;",
4768                Style);
4769   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4770                "           aaaaa :\n"
4771                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4772                Style);
4773 }
4774 
4775 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4776   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4777                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4778   verifyFormat("bool a = true, b = false;");
4779 
4780   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4781                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4782                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4783                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4784   verifyFormat(
4785       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4786       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4787       "     d = e && f;");
4788   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4789                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4790   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4791                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4792   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4793                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4794 
4795   FormatStyle Style = getGoogleStyle();
4796   Style.PointerAlignment = FormatStyle::PAS_Left;
4797   Style.DerivePointerAlignment = false;
4798   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4799                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4800                "    *b = bbbbbbbbbbbbbbbbbbb;",
4801                Style);
4802   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4803                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4804                Style);
4805 }
4806 
4807 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4808   verifyFormat("arr[foo ? bar : baz];");
4809   verifyFormat("f()[foo ? bar : baz];");
4810   verifyFormat("(a + b)[foo ? bar : baz];");
4811   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4812 }
4813 
4814 TEST_F(FormatTest, AlignsStringLiterals) {
4815   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4816                "                                      \"short literal\");");
4817   verifyFormat(
4818       "looooooooooooooooooooooooongFunction(\n"
4819       "    \"short literal\"\n"
4820       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4821   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4822                "             \" string literals\",\n"
4823                "             and, other, parameters);");
4824   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4825             "      \"5678\";",
4826             format("fun + \"1243\" /* comment */\n"
4827                    "      \"5678\";",
4828                    getLLVMStyleWithColumns(28)));
4829   EXPECT_EQ(
4830       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4831       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4832       "         \"aaaaaaaaaaaaaaaa\";",
4833       format("aaaaaa ="
4834              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4835              "aaaaaaaaaaaaaaaaaaaaa\" "
4836              "\"aaaaaaaaaaaaaaaa\";"));
4837   verifyFormat("a = a + \"a\"\n"
4838                "        \"a\"\n"
4839                "        \"a\";");
4840   verifyFormat("f(\"a\", \"b\"\n"
4841                "       \"c\");");
4842 
4843   verifyFormat(
4844       "#define LL_FORMAT \"ll\"\n"
4845       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4846       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4847 
4848   verifyFormat("#define A(X)          \\\n"
4849                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4850                "  \"ccccc\"",
4851                getLLVMStyleWithColumns(23));
4852   verifyFormat("#define A \"def\"\n"
4853                "f(\"abc\" A \"ghi\"\n"
4854                "  \"jkl\");");
4855 
4856   verifyFormat("f(L\"a\"\n"
4857                "  L\"b\");");
4858   verifyFormat("#define A(X)            \\\n"
4859                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4860                "  L\"ccccc\"",
4861                getLLVMStyleWithColumns(25));
4862 
4863   verifyFormat("f(@\"a\"\n"
4864                "  @\"b\");");
4865   verifyFormat("NSString s = @\"a\"\n"
4866                "             @\"b\"\n"
4867                "             @\"c\";");
4868   verifyFormat("NSString s = @\"a\"\n"
4869                "              \"b\"\n"
4870                "              \"c\";");
4871 }
4872 
4873 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4874   FormatStyle Style = getLLVMStyle();
4875   // No declarations or definitions should be moved to own line.
4876   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4877   verifyFormat("class A {\n"
4878                "  int f() { return 1; }\n"
4879                "  int g();\n"
4880                "};\n"
4881                "int f() { return 1; }\n"
4882                "int g();\n",
4883                Style);
4884 
4885   // All declarations and definitions should have the return type moved to its
4886   // own
4887   // line.
4888   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4889   verifyFormat("class E {\n"
4890                "  int\n"
4891                "  f() {\n"
4892                "    return 1;\n"
4893                "  }\n"
4894                "  int\n"
4895                "  g();\n"
4896                "};\n"
4897                "int\n"
4898                "f() {\n"
4899                "  return 1;\n"
4900                "}\n"
4901                "int\n"
4902                "g();\n",
4903                Style);
4904 
4905   // Top-level definitions, and no kinds of declarations should have the
4906   // return type moved to its own line.
4907   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4908   verifyFormat("class B {\n"
4909                "  int f() { return 1; }\n"
4910                "  int g();\n"
4911                "};\n"
4912                "int\n"
4913                "f() {\n"
4914                "  return 1;\n"
4915                "}\n"
4916                "int g();\n",
4917                Style);
4918 
4919   // Top-level definitions and declarations should have the return type moved
4920   // to its own line.
4921   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4922   verifyFormat("class C {\n"
4923                "  int f() { return 1; }\n"
4924                "  int g();\n"
4925                "};\n"
4926                "int\n"
4927                "f() {\n"
4928                "  return 1;\n"
4929                "}\n"
4930                "int\n"
4931                "g();\n",
4932                Style);
4933 
4934   // All definitions should have the return type moved to its own line, but no
4935   // kinds of declarations.
4936   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4937   verifyFormat("class D {\n"
4938                "  int\n"
4939                "  f() {\n"
4940                "    return 1;\n"
4941                "  }\n"
4942                "  int g();\n"
4943                "};\n"
4944                "int\n"
4945                "f() {\n"
4946                "  return 1;\n"
4947                "}\n"
4948                "int g();\n",
4949                Style);
4950   verifyFormat("const char *\n"
4951                "f(void) {\n" // Break here.
4952                "  return \"\";\n"
4953                "}\n"
4954                "const char *bar(void);\n", // No break here.
4955                Style);
4956   verifyFormat("template <class T>\n"
4957                "T *\n"
4958                "f(T &c) {\n" // Break here.
4959                "  return NULL;\n"
4960                "}\n"
4961                "template <class T> T *f(T &c);\n", // No break here.
4962                Style);
4963   verifyFormat("class C {\n"
4964                "  int\n"
4965                "  operator+() {\n"
4966                "    return 1;\n"
4967                "  }\n"
4968                "  int\n"
4969                "  operator()() {\n"
4970                "    return 1;\n"
4971                "  }\n"
4972                "};\n",
4973                Style);
4974   verifyFormat("void\n"
4975                "A::operator()() {}\n"
4976                "void\n"
4977                "A::operator>>() {}\n"
4978                "void\n"
4979                "A::operator+() {}\n",
4980                Style);
4981   verifyFormat("void *operator new(std::size_t s);", // No break here.
4982                Style);
4983   verifyFormat("void *\n"
4984                "operator new(std::size_t s) {}",
4985                Style);
4986   verifyFormat("void *\n"
4987                "operator delete[](void *ptr) {}",
4988                Style);
4989   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4990   verifyFormat("const char *\n"
4991                "f(void)\n" // Break here.
4992                "{\n"
4993                "  return \"\";\n"
4994                "}\n"
4995                "const char *bar(void);\n", // No break here.
4996                Style);
4997   verifyFormat("template <class T>\n"
4998                "T *\n"     // Problem here: no line break
4999                "f(T &c)\n" // Break here.
5000                "{\n"
5001                "  return NULL;\n"
5002                "}\n"
5003                "template <class T> T *f(T &c);\n", // No break here.
5004                Style);
5005 }
5006 
5007 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
5008   FormatStyle NoBreak = getLLVMStyle();
5009   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
5010   FormatStyle Break = getLLVMStyle();
5011   Break.AlwaysBreakBeforeMultilineStrings = true;
5012   verifyFormat("aaaa = \"bbbb\"\n"
5013                "       \"cccc\";",
5014                NoBreak);
5015   verifyFormat("aaaa =\n"
5016                "    \"bbbb\"\n"
5017                "    \"cccc\";",
5018                Break);
5019   verifyFormat("aaaa(\"bbbb\"\n"
5020                "     \"cccc\");",
5021                NoBreak);
5022   verifyFormat("aaaa(\n"
5023                "    \"bbbb\"\n"
5024                "    \"cccc\");",
5025                Break);
5026   verifyFormat("aaaa(qqq, \"bbbb\"\n"
5027                "          \"cccc\");",
5028                NoBreak);
5029   verifyFormat("aaaa(qqq,\n"
5030                "     \"bbbb\"\n"
5031                "     \"cccc\");",
5032                Break);
5033   verifyFormat("aaaa(qqq,\n"
5034                "     L\"bbbb\"\n"
5035                "     L\"cccc\");",
5036                Break);
5037   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
5038                "                      \"bbbb\"));",
5039                Break);
5040   verifyFormat("string s = someFunction(\n"
5041                "    \"abc\"\n"
5042                "    \"abc\");",
5043                Break);
5044 
5045   // As we break before unary operators, breaking right after them is bad.
5046   verifyFormat("string foo = abc ? \"x\"\n"
5047                "                   \"blah blah blah blah blah blah\"\n"
5048                "                 : \"y\";",
5049                Break);
5050 
5051   // Don't break if there is no column gain.
5052   verifyFormat("f(\"aaaa\"\n"
5053                "  \"bbbb\");",
5054                Break);
5055 
5056   // Treat literals with escaped newlines like multi-line string literals.
5057   EXPECT_EQ("x = \"a\\\n"
5058             "b\\\n"
5059             "c\";",
5060             format("x = \"a\\\n"
5061                    "b\\\n"
5062                    "c\";",
5063                    NoBreak));
5064   EXPECT_EQ("xxxx =\n"
5065             "    \"a\\\n"
5066             "b\\\n"
5067             "c\";",
5068             format("xxxx = \"a\\\n"
5069                    "b\\\n"
5070                    "c\";",
5071                    Break));
5072 
5073   // Exempt ObjC strings for now.
5074   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
5075             "                          @\"bbbb\";",
5076             format("NSString *const kString = @\"aaaa\"\n"
5077                    "@\"bbbb\";",
5078                    Break));
5079 
5080   Break.ColumnLimit = 0;
5081   verifyFormat("const char *hello = \"hello llvm\";", Break);
5082 }
5083 
5084 TEST_F(FormatTest, AlignsPipes) {
5085   verifyFormat(
5086       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5087       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5088       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5089   verifyFormat(
5090       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5091       "                     << aaaaaaaaaaaaaaaaaaaa;");
5092   verifyFormat(
5093       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5094       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5095   verifyFormat(
5096       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5097       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5098       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5099   verifyFormat(
5100       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5101       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5102       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5103   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5104                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5105                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5106                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5107   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5108                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5109   verifyFormat(
5110       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5111       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5112 
5113   verifyFormat("return out << \"somepacket = {\\n\"\n"
5114                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5115                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5116                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5117                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5118                "           << \"}\";");
5119 
5120   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5121                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5122                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5123   verifyFormat(
5124       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5125       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5126       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5127       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5128       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5129   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5130                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5131   verifyFormat(
5132       "void f() {\n"
5133       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5134       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5135       "}");
5136   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5137                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5138   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5139                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5140                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5141                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5142   verifyFormat("LOG_IF(aaa == //\n"
5143                "       bbb)\n"
5144                "    << a << b;");
5145 
5146   // Breaking before the first "<<" is generally not desirable.
5147   verifyFormat(
5148       "llvm::errs()\n"
5149       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5150       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5151       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5152       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5153       getLLVMStyleWithColumns(70));
5154   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5155                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5156                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5157                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5158                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5159                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5160                getLLVMStyleWithColumns(70));
5161 
5162   // But sometimes, breaking before the first "<<" is desirable.
5163   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5164                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5165   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5166                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5167                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5168   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5169                "    << BEF << IsTemplate << Description << E->getType();");
5170   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5171                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5172                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5173   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5174                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5175                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5176                "    << aaa;");
5177 
5178   verifyFormat(
5179       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5180       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5181 
5182   // Incomplete string literal.
5183   EXPECT_EQ("llvm::errs() << \"\n"
5184             "             << a;",
5185             format("llvm::errs() << \"\n<<a;"));
5186 
5187   verifyFormat("void f() {\n"
5188                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5189                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5190                "}");
5191 
5192   // Handle 'endl'.
5193   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5194                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5195   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5196 
5197   // Handle '\n'.
5198   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5199                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5200   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5201                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5202   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5203                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5204   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5205 }
5206 
5207 TEST_F(FormatTest, UnderstandsEquals) {
5208   verifyFormat(
5209       "aaaaaaaaaaaaaaaaa =\n"
5210       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5211   verifyFormat(
5212       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5213       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5214   verifyFormat(
5215       "if (a) {\n"
5216       "  f();\n"
5217       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5218       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5219       "}");
5220 
5221   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5222                "        100000000 + 10000000) {\n}");
5223 }
5224 
5225 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5226   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5227                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5228 
5229   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5230                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5231 
5232   verifyFormat(
5233       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5234       "                                                          Parameter2);");
5235 
5236   verifyFormat(
5237       "ShortObject->shortFunction(\n"
5238       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5239       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5240 
5241   verifyFormat("loooooooooooooongFunction(\n"
5242                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5243 
5244   verifyFormat(
5245       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5246       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5247 
5248   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5249                "    .WillRepeatedly(Return(SomeValue));");
5250   verifyFormat("void f() {\n"
5251                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5252                "      .Times(2)\n"
5253                "      .WillRepeatedly(Return(SomeValue));\n"
5254                "}");
5255   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5256                "    ccccccccccccccccccccccc);");
5257   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5258                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5259                "          .aaaaa(aaaaa),\n"
5260                "      aaaaaaaaaaaaaaaaaaaaa);");
5261   verifyFormat("void f() {\n"
5262                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5263                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5264                "}");
5265   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5266                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5267                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5268                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5269                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5270   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5271                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5272                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5273                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5274                "}");
5275 
5276   // Here, it is not necessary to wrap at "." or "->".
5277   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5278                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5279   verifyFormat(
5280       "aaaaaaaaaaa->aaaaaaaaa(\n"
5281       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5282       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5283 
5284   verifyFormat(
5285       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5286       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5287   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5288                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5289   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5290                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5291 
5292   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5293                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5294                "    .a();");
5295 
5296   FormatStyle NoBinPacking = getLLVMStyle();
5297   NoBinPacking.BinPackParameters = false;
5298   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5299                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5300                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5301                "                         aaaaaaaaaaaaaaaaaaa,\n"
5302                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5303                NoBinPacking);
5304 
5305   // If there is a subsequent call, change to hanging indentation.
5306   verifyFormat(
5307       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5308       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5309       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5310   verifyFormat(
5311       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5312       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5313   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5314                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5315                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5316   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5317                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5318                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5319 }
5320 
5321 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5322   verifyFormat("template <typename T>\n"
5323                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5324   verifyFormat("template <typename T>\n"
5325                "// T should be one of {A, B}.\n"
5326                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5327   verifyFormat(
5328       "template <typename T>\n"
5329       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5330   verifyFormat("template <typename T>\n"
5331                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5332                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5333   verifyFormat(
5334       "template <typename T>\n"
5335       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5336       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5337   verifyFormat(
5338       "template <typename T>\n"
5339       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5340       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5341       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5342   verifyFormat("template <typename T>\n"
5343                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5344                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5345   verifyFormat(
5346       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5347       "          typename T4 = char>\n"
5348       "void f();");
5349   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5350                "          template <typename> class cccccccccccccccccccccc,\n"
5351                "          typename ddddddddddddd>\n"
5352                "class C {};");
5353   verifyFormat(
5354       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5355       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5356 
5357   verifyFormat("void f() {\n"
5358                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5359                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5360                "}");
5361 
5362   verifyFormat("template <typename T> class C {};");
5363   verifyFormat("template <typename T> void f();");
5364   verifyFormat("template <typename T> void f() {}");
5365   verifyFormat(
5366       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5367       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5368       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5369       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5370       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5371       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5372       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5373       getLLVMStyleWithColumns(72));
5374   EXPECT_EQ("static_cast<A< //\n"
5375             "    B> *>(\n"
5376             "\n"
5377             "    );",
5378             format("static_cast<A<//\n"
5379                    "    B>*>(\n"
5380                    "\n"
5381                    "    );"));
5382   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5383                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5384 
5385   FormatStyle AlwaysBreak = getLLVMStyle();
5386   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5387   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5388   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5389   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5390   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5391                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5392                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5393   verifyFormat("template <template <typename> class Fooooooo,\n"
5394                "          template <typename> class Baaaaaaar>\n"
5395                "struct C {};",
5396                AlwaysBreak);
5397   verifyFormat("template <typename T> // T can be A, B or C.\n"
5398                "struct C {};",
5399                AlwaysBreak);
5400   verifyFormat("template <enum E> class A {\n"
5401                "public:\n"
5402                "  E *f();\n"
5403                "};");
5404 }
5405 
5406 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5407   verifyFormat(
5408       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5409       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5410   verifyFormat(
5411       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5413       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5414 
5415   // FIXME: Should we have the extra indent after the second break?
5416   verifyFormat(
5417       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5418       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5419       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5420 
5421   verifyFormat(
5422       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5423       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5424 
5425   // Breaking at nested name specifiers is generally not desirable.
5426   verifyFormat(
5427       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5428       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5429 
5430   verifyFormat(
5431       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5432       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5433       "                   aaaaaaaaaaaaaaaaaaaaa);",
5434       getLLVMStyleWithColumns(74));
5435 
5436   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5437                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5438                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5439 }
5440 
5441 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5442   verifyFormat("A<int> a;");
5443   verifyFormat("A<A<A<int>>> a;");
5444   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5445   verifyFormat("bool x = a < 1 || 2 > a;");
5446   verifyFormat("bool x = 5 < f<int>();");
5447   verifyFormat("bool x = f<int>() > 5;");
5448   verifyFormat("bool x = 5 < a<int>::x;");
5449   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5450   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5451 
5452   verifyGoogleFormat("A<A<int>> a;");
5453   verifyGoogleFormat("A<A<A<int>>> a;");
5454   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5455   verifyGoogleFormat("A<A<int> > a;");
5456   verifyGoogleFormat("A<A<A<int> > > a;");
5457   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5458   verifyGoogleFormat("A<::A<int>> a;");
5459   verifyGoogleFormat("A<::A> a;");
5460   verifyGoogleFormat("A< ::A> a;");
5461   verifyGoogleFormat("A< ::A<int> > a;");
5462   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5463   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5464   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5465   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5466   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5467             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5468 
5469   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5470 
5471   verifyFormat("test >> a >> b;");
5472   verifyFormat("test << a >> b;");
5473 
5474   verifyFormat("f<int>();");
5475   verifyFormat("template <typename T> void f() {}");
5476   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5477   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5478                "sizeof(char)>::type>;");
5479   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5480   verifyFormat("f(a.operator()<A>());");
5481   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5482                "      .template operator()<A>());",
5483                getLLVMStyleWithColumns(35));
5484 
5485   // Not template parameters.
5486   verifyFormat("return a < b && c > d;");
5487   verifyFormat("void f() {\n"
5488                "  while (a < b && c > d) {\n"
5489                "  }\n"
5490                "}");
5491   verifyFormat("template <typename... Types>\n"
5492                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5493 
5494   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5495                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5496                getLLVMStyleWithColumns(60));
5497   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5498   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5499   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5500 }
5501 
5502 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5503   verifyFormat("COMPARE(a, ==, b);");
5504   verifyFormat("auto s = sizeof...(Ts) - 1;");
5505 }
5506 
5507 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5508   verifyFormat("int A::*x;");
5509   verifyFormat("int (S::*func)(void *);");
5510   verifyFormat("void f() { int (S::*func)(void *); }");
5511   verifyFormat("typedef bool *(Class::*Member)() const;");
5512   verifyFormat("void f() {\n"
5513                "  (a->*f)();\n"
5514                "  a->*x;\n"
5515                "  (a.*f)();\n"
5516                "  ((*a).*f)();\n"
5517                "  a.*x;\n"
5518                "}");
5519   verifyFormat("void f() {\n"
5520                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5521                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5522                "}");
5523   verifyFormat(
5524       "(aaaaaaaaaa->*bbbbbbb)(\n"
5525       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5526   FormatStyle Style = getLLVMStyle();
5527   Style.PointerAlignment = FormatStyle::PAS_Left;
5528   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5529 }
5530 
5531 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5532   verifyFormat("int a = -2;");
5533   verifyFormat("f(-1, -2, -3);");
5534   verifyFormat("a[-1] = 5;");
5535   verifyFormat("int a = 5 + -2;");
5536   verifyFormat("if (i == -1) {\n}");
5537   verifyFormat("if (i != -1) {\n}");
5538   verifyFormat("if (i > -1) {\n}");
5539   verifyFormat("if (i < -1) {\n}");
5540   verifyFormat("++(a->f());");
5541   verifyFormat("--(a->f());");
5542   verifyFormat("(a->f())++;");
5543   verifyFormat("a[42]++;");
5544   verifyFormat("if (!(a->f())) {\n}");
5545 
5546   verifyFormat("a-- > b;");
5547   verifyFormat("b ? -a : c;");
5548   verifyFormat("n * sizeof char16;");
5549   verifyFormat("n * alignof char16;", getGoogleStyle());
5550   verifyFormat("sizeof(char);");
5551   verifyFormat("alignof(char);", getGoogleStyle());
5552 
5553   verifyFormat("return -1;");
5554   verifyFormat("switch (a) {\n"
5555                "case -1:\n"
5556                "  break;\n"
5557                "}");
5558   verifyFormat("#define X -1");
5559   verifyFormat("#define X -kConstant");
5560 
5561   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5562   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5563 
5564   verifyFormat("int a = /* confusing comment */ -1;");
5565   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5566   verifyFormat("int a = i /* confusing comment */++;");
5567 }
5568 
5569 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5570   verifyFormat("if (!aaaaaaaaaa( // break\n"
5571                "        aaaaa)) {\n"
5572                "}");
5573   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5574                "    aaaaa));");
5575   verifyFormat("*aaa = aaaaaaa( // break\n"
5576                "    bbbbbb);");
5577 }
5578 
5579 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5580   verifyFormat("bool operator<();");
5581   verifyFormat("bool operator>();");
5582   verifyFormat("bool operator=();");
5583   verifyFormat("bool operator==();");
5584   verifyFormat("bool operator!=();");
5585   verifyFormat("int operator+();");
5586   verifyFormat("int operator++();");
5587   verifyFormat("bool operator,();");
5588   verifyFormat("bool operator();");
5589   verifyFormat("bool operator()();");
5590   verifyFormat("bool operator[]();");
5591   verifyFormat("operator bool();");
5592   verifyFormat("operator int();");
5593   verifyFormat("operator void *();");
5594   verifyFormat("operator SomeType<int>();");
5595   verifyFormat("operator SomeType<int, int>();");
5596   verifyFormat("operator SomeType<SomeType<int>>();");
5597   verifyFormat("void *operator new(std::size_t size);");
5598   verifyFormat("void *operator new[](std::size_t size);");
5599   verifyFormat("void operator delete(void *ptr);");
5600   verifyFormat("void operator delete[](void *ptr);");
5601   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5602                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5603   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5604                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5605 
5606   verifyFormat(
5607       "ostream &operator<<(ostream &OutputStream,\n"
5608       "                    SomeReallyLongType WithSomeReallyLongValue);");
5609   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5610                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5611                "  return left.group < right.group;\n"
5612                "}");
5613   verifyFormat("SomeType &operator=(const SomeType &S);");
5614   verifyFormat("f.template operator()<int>();");
5615 
5616   verifyGoogleFormat("operator void*();");
5617   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5618   verifyGoogleFormat("operator ::A();");
5619 
5620   verifyFormat("using A::operator+;");
5621   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5622                "int i;");
5623 }
5624 
5625 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5626   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5627   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5628   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5629   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5630   verifyFormat("Deleted &operator=(const Deleted &) &;");
5631   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5632   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5633   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5634   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5635   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5636   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5637   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
5638 
5639   FormatStyle AlignLeft = getLLVMStyle();
5640   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5641   verifyFormat("void A::b() && {}", AlignLeft);
5642   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5643   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5644                AlignLeft);
5645   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5646   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5647   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5648   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5649   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5650   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5651   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
5652 
5653   FormatStyle Spaces = getLLVMStyle();
5654   Spaces.SpacesInCStyleCastParentheses = true;
5655   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5656   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5657   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5658   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5659 
5660   Spaces.SpacesInCStyleCastParentheses = false;
5661   Spaces.SpacesInParentheses = true;
5662   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5663   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5664   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5665   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5666 }
5667 
5668 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5669   verifyFormat("void f() {\n"
5670                "  A *a = new A;\n"
5671                "  A *a = new (placement) A;\n"
5672                "  delete a;\n"
5673                "  delete (A *)a;\n"
5674                "}");
5675   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5676                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5677   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5678                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5679                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5680   verifyFormat("delete[] h->p;");
5681 }
5682 
5683 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5684   verifyFormat("int *f(int *a) {}");
5685   verifyFormat("int main(int argc, char **argv) {}");
5686   verifyFormat("Test::Test(int b) : a(b * b) {}");
5687   verifyIndependentOfContext("f(a, *a);");
5688   verifyFormat("void g() { f(*a); }");
5689   verifyIndependentOfContext("int a = b * 10;");
5690   verifyIndependentOfContext("int a = 10 * b;");
5691   verifyIndependentOfContext("int a = b * c;");
5692   verifyIndependentOfContext("int a += b * c;");
5693   verifyIndependentOfContext("int a -= b * c;");
5694   verifyIndependentOfContext("int a *= b * c;");
5695   verifyIndependentOfContext("int a /= b * c;");
5696   verifyIndependentOfContext("int a = *b;");
5697   verifyIndependentOfContext("int a = *b * c;");
5698   verifyIndependentOfContext("int a = b * *c;");
5699   verifyIndependentOfContext("int a = b * (10);");
5700   verifyIndependentOfContext("S << b * (10);");
5701   verifyIndependentOfContext("return 10 * b;");
5702   verifyIndependentOfContext("return *b * *c;");
5703   verifyIndependentOfContext("return a & ~b;");
5704   verifyIndependentOfContext("f(b ? *c : *d);");
5705   verifyIndependentOfContext("int a = b ? *c : *d;");
5706   verifyIndependentOfContext("*b = a;");
5707   verifyIndependentOfContext("a * ~b;");
5708   verifyIndependentOfContext("a * !b;");
5709   verifyIndependentOfContext("a * +b;");
5710   verifyIndependentOfContext("a * -b;");
5711   verifyIndependentOfContext("a * ++b;");
5712   verifyIndependentOfContext("a * --b;");
5713   verifyIndependentOfContext("a[4] * b;");
5714   verifyIndependentOfContext("a[a * a] = 1;");
5715   verifyIndependentOfContext("f() * b;");
5716   verifyIndependentOfContext("a * [self dostuff];");
5717   verifyIndependentOfContext("int x = a * (a + b);");
5718   verifyIndependentOfContext("(a *)(a + b);");
5719   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5720   verifyIndependentOfContext("int *pa = (int *)&a;");
5721   verifyIndependentOfContext("return sizeof(int **);");
5722   verifyIndependentOfContext("return sizeof(int ******);");
5723   verifyIndependentOfContext("return (int **&)a;");
5724   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5725   verifyFormat("void f(Type (*parameter)[10]) {}");
5726   verifyFormat("void f(Type (&parameter)[10]) {}");
5727   verifyGoogleFormat("return sizeof(int**);");
5728   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5729   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5730   verifyFormat("auto a = [](int **&, int ***) {};");
5731   verifyFormat("auto PointerBinding = [](const char *S) {};");
5732   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5733   verifyFormat("[](const decltype(*a) &value) {}");
5734   verifyFormat("decltype(a * b) F();");
5735   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5736   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5737   verifyIndependentOfContext("typedef void (*f)(int *a);");
5738   verifyIndependentOfContext("int i{a * b};");
5739   verifyIndependentOfContext("aaa && aaa->f();");
5740   verifyIndependentOfContext("int x = ~*p;");
5741   verifyFormat("Constructor() : a(a), area(width * height) {}");
5742   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5743   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5744   verifyFormat("void f() { f(a, c * d); }");
5745   verifyFormat("void f() { f(new a(), c * d); }");
5746 
5747   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5748 
5749   verifyIndependentOfContext("A<int *> a;");
5750   verifyIndependentOfContext("A<int **> a;");
5751   verifyIndependentOfContext("A<int *, int *> a;");
5752   verifyIndependentOfContext("A<int *[]> a;");
5753   verifyIndependentOfContext(
5754       "const char *const p = reinterpret_cast<const char *const>(q);");
5755   verifyIndependentOfContext("A<int **, int **> a;");
5756   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5757   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5758   verifyFormat("for (; a && b;) {\n}");
5759   verifyFormat("bool foo = true && [] { return false; }();");
5760 
5761   verifyFormat(
5762       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5763       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5764 
5765   verifyGoogleFormat("int const* a = &b;");
5766   verifyGoogleFormat("**outparam = 1;");
5767   verifyGoogleFormat("*outparam = a * b;");
5768   verifyGoogleFormat("int main(int argc, char** argv) {}");
5769   verifyGoogleFormat("A<int*> a;");
5770   verifyGoogleFormat("A<int**> a;");
5771   verifyGoogleFormat("A<int*, int*> a;");
5772   verifyGoogleFormat("A<int**, int**> a;");
5773   verifyGoogleFormat("f(b ? *c : *d);");
5774   verifyGoogleFormat("int a = b ? *c : *d;");
5775   verifyGoogleFormat("Type* t = **x;");
5776   verifyGoogleFormat("Type* t = *++*x;");
5777   verifyGoogleFormat("*++*x;");
5778   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5779   verifyGoogleFormat("Type* t = x++ * y;");
5780   verifyGoogleFormat(
5781       "const char* const p = reinterpret_cast<const char* const>(q);");
5782   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5783   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5784   verifyGoogleFormat("template <typename T>\n"
5785                      "void f(int i = 0, SomeType** temps = NULL);");
5786 
5787   FormatStyle Left = getLLVMStyle();
5788   Left.PointerAlignment = FormatStyle::PAS_Left;
5789   verifyFormat("x = *a(x) = *a(y);", Left);
5790   verifyFormat("for (;; * = b) {\n}", Left);
5791   verifyFormat("return *this += 1;", Left);
5792 
5793   verifyIndependentOfContext("a = *(x + y);");
5794   verifyIndependentOfContext("a = &(x + y);");
5795   verifyIndependentOfContext("*(x + y).call();");
5796   verifyIndependentOfContext("&(x + y)->call();");
5797   verifyFormat("void f() { &(*I).first; }");
5798 
5799   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5800   verifyFormat(
5801       "int *MyValues = {\n"
5802       "    *A, // Operator detection might be confused by the '{'\n"
5803       "    *BB // Operator detection might be confused by previous comment\n"
5804       "};");
5805 
5806   verifyIndependentOfContext("if (int *a = &b)");
5807   verifyIndependentOfContext("if (int &a = *b)");
5808   verifyIndependentOfContext("if (a & b[i])");
5809   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5810   verifyIndependentOfContext("if (*b[i])");
5811   verifyIndependentOfContext("if (int *a = (&b))");
5812   verifyIndependentOfContext("while (int *a = &b)");
5813   verifyIndependentOfContext("size = sizeof *a;");
5814   verifyIndependentOfContext("if (a && (b = c))");
5815   verifyFormat("void f() {\n"
5816                "  for (const int &v : Values) {\n"
5817                "  }\n"
5818                "}");
5819   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5820   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5821   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5822 
5823   verifyFormat("#define A (!a * b)");
5824   verifyFormat("#define MACRO     \\\n"
5825                "  int *i = a * b; \\\n"
5826                "  void f(a *b);",
5827                getLLVMStyleWithColumns(19));
5828 
5829   verifyIndependentOfContext("A = new SomeType *[Length];");
5830   verifyIndependentOfContext("A = new SomeType *[Length]();");
5831   verifyIndependentOfContext("T **t = new T *;");
5832   verifyIndependentOfContext("T **t = new T *();");
5833   verifyGoogleFormat("A = new SomeType*[Length]();");
5834   verifyGoogleFormat("A = new SomeType*[Length];");
5835   verifyGoogleFormat("T** t = new T*;");
5836   verifyGoogleFormat("T** t = new T*();");
5837 
5838   FormatStyle PointerLeft = getLLVMStyle();
5839   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5840   verifyFormat("delete *x;", PointerLeft);
5841   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5842   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5843   verifyFormat("template <bool a, bool b> "
5844                "typename t::if<x && y>::type f() {}");
5845   verifyFormat("template <int *y> f() {}");
5846   verifyFormat("vector<int *> v;");
5847   verifyFormat("vector<int *const> v;");
5848   verifyFormat("vector<int *const **const *> v;");
5849   verifyFormat("vector<int *volatile> v;");
5850   verifyFormat("vector<a * b> v;");
5851   verifyFormat("foo<b && false>();");
5852   verifyFormat("foo<b & 1>();");
5853   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5854   verifyFormat(
5855       "template <class T, class = typename std::enable_if<\n"
5856       "                       std::is_integral<T>::value &&\n"
5857       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5858       "void F();",
5859       getLLVMStyleWithColumns(76));
5860   verifyFormat(
5861       "template <class T,\n"
5862       "          class = typename ::std::enable_if<\n"
5863       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5864       "void F();",
5865       getGoogleStyleWithColumns(68));
5866 
5867   verifyIndependentOfContext("MACRO(int *i);");
5868   verifyIndependentOfContext("MACRO(auto *a);");
5869   verifyIndependentOfContext("MACRO(const A *a);");
5870   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5871   // FIXME: Is there a way to make this work?
5872   // verifyIndependentOfContext("MACRO(A *a);");
5873 
5874   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5875   verifyFormat("return options != nullptr && operator==(*options);");
5876 
5877   EXPECT_EQ("#define OP(x)                                    \\\n"
5878             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5879             "    return s << a.DebugString();                 \\\n"
5880             "  }",
5881             format("#define OP(x) \\\n"
5882                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5883                    "    return s << a.DebugString(); \\\n"
5884                    "  }",
5885                    getLLVMStyleWithColumns(50)));
5886 
5887   // FIXME: We cannot handle this case yet; we might be able to figure out that
5888   // foo<x> d > v; doesn't make sense.
5889   verifyFormat("foo<a<b && c> d> v;");
5890 
5891   FormatStyle PointerMiddle = getLLVMStyle();
5892   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5893   verifyFormat("delete *x;", PointerMiddle);
5894   verifyFormat("int * x;", PointerMiddle);
5895   verifyFormat("template <int * y> f() {}", PointerMiddle);
5896   verifyFormat("int * f(int * a) {}", PointerMiddle);
5897   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5898   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5899   verifyFormat("A<int *> a;", PointerMiddle);
5900   verifyFormat("A<int **> a;", PointerMiddle);
5901   verifyFormat("A<int *, int *> a;", PointerMiddle);
5902   verifyFormat("A<int * []> a;", PointerMiddle);
5903   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5904   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5905   verifyFormat("T ** t = new T *;", PointerMiddle);
5906 
5907   // Member function reference qualifiers aren't binary operators.
5908   verifyFormat("string // break\n"
5909                "operator()() & {}");
5910   verifyFormat("string // break\n"
5911                "operator()() && {}");
5912   verifyGoogleFormat("template <typename T>\n"
5913                      "auto x() & -> int {}");
5914 }
5915 
5916 TEST_F(FormatTest, UnderstandsAttributes) {
5917   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5918   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5919                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5920   FormatStyle AfterType = getLLVMStyle();
5921   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5922   verifyFormat("__attribute__((nodebug)) void\n"
5923                "foo() {}\n",
5924                AfterType);
5925 }
5926 
5927 TEST_F(FormatTest, UnderstandsEllipsis) {
5928   verifyFormat("int printf(const char *fmt, ...);");
5929   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5930   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5931 
5932   FormatStyle PointersLeft = getLLVMStyle();
5933   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5934   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5935 }
5936 
5937 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5938   EXPECT_EQ("int *a;\n"
5939             "int *a;\n"
5940             "int *a;",
5941             format("int *a;\n"
5942                    "int* a;\n"
5943                    "int *a;",
5944                    getGoogleStyle()));
5945   EXPECT_EQ("int* a;\n"
5946             "int* a;\n"
5947             "int* a;",
5948             format("int* a;\n"
5949                    "int* a;\n"
5950                    "int *a;",
5951                    getGoogleStyle()));
5952   EXPECT_EQ("int *a;\n"
5953             "int *a;\n"
5954             "int *a;",
5955             format("int *a;\n"
5956                    "int * a;\n"
5957                    "int *  a;",
5958                    getGoogleStyle()));
5959   EXPECT_EQ("auto x = [] {\n"
5960             "  int *a;\n"
5961             "  int *a;\n"
5962             "  int *a;\n"
5963             "};",
5964             format("auto x=[]{int *a;\n"
5965                    "int * a;\n"
5966                    "int *  a;};",
5967                    getGoogleStyle()));
5968 }
5969 
5970 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5971   verifyFormat("int f(int &&a) {}");
5972   verifyFormat("int f(int a, char &&b) {}");
5973   verifyFormat("void f() { int &&a = b; }");
5974   verifyGoogleFormat("int f(int a, char&& b) {}");
5975   verifyGoogleFormat("void f() { int&& a = b; }");
5976 
5977   verifyIndependentOfContext("A<int &&> a;");
5978   verifyIndependentOfContext("A<int &&, int &&> a;");
5979   verifyGoogleFormat("A<int&&> a;");
5980   verifyGoogleFormat("A<int&&, int&&> a;");
5981 
5982   // Not rvalue references:
5983   verifyFormat("template <bool B, bool C> class A {\n"
5984                "  static_assert(B && C, \"Something is wrong\");\n"
5985                "};");
5986   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5987   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5988   verifyFormat("#define A(a, b) (a && b)");
5989 }
5990 
5991 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5992   verifyFormat("void f() {\n"
5993                "  x[aaaaaaaaa -\n"
5994                "    b] = 23;\n"
5995                "}",
5996                getLLVMStyleWithColumns(15));
5997 }
5998 
5999 TEST_F(FormatTest, FormatsCasts) {
6000   verifyFormat("Type *A = static_cast<Type *>(P);");
6001   verifyFormat("Type *A = (Type *)P;");
6002   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6003   verifyFormat("int a = (int)(2.0f);");
6004   verifyFormat("int a = (int)2.0f;");
6005   verifyFormat("x[(int32)y];");
6006   verifyFormat("x = (int32)y;");
6007   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6008   verifyFormat("int a = (int)*b;");
6009   verifyFormat("int a = (int)2.0f;");
6010   verifyFormat("int a = (int)~0;");
6011   verifyFormat("int a = (int)++a;");
6012   verifyFormat("int a = (int)sizeof(int);");
6013   verifyFormat("int a = (int)+2;");
6014   verifyFormat("my_int a = (my_int)2.0f;");
6015   verifyFormat("my_int a = (my_int)sizeof(int);");
6016   verifyFormat("return (my_int)aaa;");
6017   verifyFormat("#define x ((int)-1)");
6018   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6019   verifyFormat("#define p(q) ((int *)&q)");
6020   verifyFormat("fn(a)(b) + 1;");
6021 
6022   verifyFormat("void f() { my_int a = (my_int)*b; }");
6023   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6024   verifyFormat("my_int a = (my_int)~0;");
6025   verifyFormat("my_int a = (my_int)++a;");
6026   verifyFormat("my_int a = (my_int)-2;");
6027   verifyFormat("my_int a = (my_int)1;");
6028   verifyFormat("my_int a = (my_int *)1;");
6029   verifyFormat("my_int a = (const my_int)-1;");
6030   verifyFormat("my_int a = (const my_int *)-1;");
6031   verifyFormat("my_int a = (my_int)(my_int)-1;");
6032   verifyFormat("my_int a = (ns::my_int)-2;");
6033   verifyFormat("case (my_int)ONE:");
6034   verifyFormat("auto x = (X)this;");
6035 
6036   // FIXME: single value wrapped with paren will be treated as cast.
6037   verifyFormat("void f(int i = (kValue)*kMask) {}");
6038 
6039   verifyFormat("{ (void)F; }");
6040 
6041   // Don't break after a cast's
6042   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6043                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6044                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6045 
6046   // These are not casts.
6047   verifyFormat("void f(int *) {}");
6048   verifyFormat("f(foo)->b;");
6049   verifyFormat("f(foo).b;");
6050   verifyFormat("f(foo)(b);");
6051   verifyFormat("f(foo)[b];");
6052   verifyFormat("[](foo) { return 4; }(bar);");
6053   verifyFormat("(*funptr)(foo)[4];");
6054   verifyFormat("funptrs[4](foo)[4];");
6055   verifyFormat("void f(int *);");
6056   verifyFormat("void f(int *) = 0;");
6057   verifyFormat("void f(SmallVector<int>) {}");
6058   verifyFormat("void f(SmallVector<int>);");
6059   verifyFormat("void f(SmallVector<int>) = 0;");
6060   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6061   verifyFormat("int a = sizeof(int) * b;");
6062   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6063   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6064   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6065   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6066 
6067   // These are not casts, but at some point were confused with casts.
6068   verifyFormat("virtual void foo(int *) override;");
6069   verifyFormat("virtual void foo(char &) const;");
6070   verifyFormat("virtual void foo(int *a, char *) const;");
6071   verifyFormat("int a = sizeof(int *) + b;");
6072   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6073   verifyFormat("bool b = f(g<int>) && c;");
6074   verifyFormat("typedef void (*f)(int i) func;");
6075 
6076   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6077                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6078   // FIXME: The indentation here is not ideal.
6079   verifyFormat(
6080       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6081       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6082       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6083 }
6084 
6085 TEST_F(FormatTest, FormatsFunctionTypes) {
6086   verifyFormat("A<bool()> a;");
6087   verifyFormat("A<SomeType()> a;");
6088   verifyFormat("A<void (*)(int, std::string)> a;");
6089   verifyFormat("A<void *(int)>;");
6090   verifyFormat("void *(*a)(int *, SomeType *);");
6091   verifyFormat("int (*func)(void *);");
6092   verifyFormat("void f() { int (*func)(void *); }");
6093   verifyFormat("template <class CallbackClass>\n"
6094                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6095 
6096   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6097   verifyGoogleFormat("void* (*a)(int);");
6098   verifyGoogleFormat(
6099       "template <class CallbackClass>\n"
6100       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6101 
6102   // Other constructs can look somewhat like function types:
6103   verifyFormat("A<sizeof(*x)> a;");
6104   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6105   verifyFormat("some_var = function(*some_pointer_var)[0];");
6106   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6107   verifyFormat("int x = f(&h)();");
6108   verifyFormat("returnsFunction(&param1, &param2)(param);");
6109 }
6110 
6111 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6112   verifyFormat("A (*foo_)[6];");
6113   verifyFormat("vector<int> (*foo_)[6];");
6114 }
6115 
6116 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6117   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6118                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6119   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6120                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6121   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6122                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6123 
6124   // Different ways of ()-initializiation.
6125   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6126                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6127   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6128                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6129   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6130                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6131   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6132                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6133 }
6134 
6135 TEST_F(FormatTest, BreaksLongDeclarations) {
6136   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6137                "    AnotherNameForTheLongType;");
6138   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6139                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6140   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6141                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6142   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6143                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6144   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6145                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6146   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6147                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6148   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6149                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6150   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6151                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6152   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6153                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6154   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6155                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6156   FormatStyle Indented = getLLVMStyle();
6157   Indented.IndentWrappedFunctionNames = true;
6158   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6159                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6160                Indented);
6161   verifyFormat(
6162       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6163       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6164       Indented);
6165   verifyFormat(
6166       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6167       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6168       Indented);
6169   verifyFormat(
6170       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6171       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6172       Indented);
6173 
6174   // FIXME: Without the comment, this breaks after "(".
6175   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6176                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6177                getGoogleStyle());
6178 
6179   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6180                "                  int LoooooooooooooooooooongParam2) {}");
6181   verifyFormat(
6182       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6183       "                                   SourceLocation L, IdentifierIn *II,\n"
6184       "                                   Type *T) {}");
6185   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6186                "ReallyReaaallyLongFunctionName(\n"
6187                "    const std::string &SomeParameter,\n"
6188                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6189                "        &ReallyReallyLongParameterName,\n"
6190                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6191                "        &AnotherLongParameterName) {}");
6192   verifyFormat("template <typename A>\n"
6193                "SomeLoooooooooooooooooooooongType<\n"
6194                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6195                "Function() {}");
6196 
6197   verifyGoogleFormat(
6198       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6199       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6200   verifyGoogleFormat(
6201       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6202       "                                   SourceLocation L) {}");
6203   verifyGoogleFormat(
6204       "some_namespace::LongReturnType\n"
6205       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6206       "    int first_long_parameter, int second_parameter) {}");
6207 
6208   verifyGoogleFormat("template <typename T>\n"
6209                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6210                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6211   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6212                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6213 
6214   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6215                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6216                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6217   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6218                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6219                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6220   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6221                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6222                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6223                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6224 }
6225 
6226 TEST_F(FormatTest, FormatsArrays) {
6227   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6228                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6229   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6230                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6231   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6232                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6233   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6234                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6235   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6236                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6237   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6238                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6239                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6240   verifyFormat(
6241       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6242       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6243       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6244   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6245                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6246 
6247   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6248                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6249   verifyFormat(
6250       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6251       "                                  .aaaaaaa[0]\n"
6252       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6253   verifyFormat("a[::b::c];");
6254 
6255   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6256 
6257   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6258   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6259 }
6260 
6261 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6262   verifyFormat("(a)->b();");
6263   verifyFormat("--a;");
6264 }
6265 
6266 TEST_F(FormatTest, HandlesIncludeDirectives) {
6267   verifyFormat("#include <string>\n"
6268                "#include <a/b/c.h>\n"
6269                "#include \"a/b/string\"\n"
6270                "#include \"string.h\"\n"
6271                "#include \"string.h\"\n"
6272                "#include <a-a>\n"
6273                "#include < path with space >\n"
6274                "#include_next <test.h>"
6275                "#include \"abc.h\" // this is included for ABC\n"
6276                "#include \"some long include\" // with a comment\n"
6277                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6278                getLLVMStyleWithColumns(35));
6279   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6280   EXPECT_EQ("#include <a>", format("#include<a>"));
6281 
6282   verifyFormat("#import <string>");
6283   verifyFormat("#import <a/b/c.h>");
6284   verifyFormat("#import \"a/b/string\"");
6285   verifyFormat("#import \"string.h\"");
6286   verifyFormat("#import \"string.h\"");
6287   verifyFormat("#if __has_include(<strstream>)\n"
6288                "#include <strstream>\n"
6289                "#endif");
6290 
6291   verifyFormat("#define MY_IMPORT <a/b>");
6292 
6293   // Protocol buffer definition or missing "#".
6294   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6295                getLLVMStyleWithColumns(30));
6296 
6297   FormatStyle Style = getLLVMStyle();
6298   Style.AlwaysBreakBeforeMultilineStrings = true;
6299   Style.ColumnLimit = 0;
6300   verifyFormat("#import \"abc.h\"", Style);
6301 
6302   // But 'import' might also be a regular C++ namespace.
6303   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6304                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6305 }
6306 
6307 //===----------------------------------------------------------------------===//
6308 // Error recovery tests.
6309 //===----------------------------------------------------------------------===//
6310 
6311 TEST_F(FormatTest, IncompleteParameterLists) {
6312   FormatStyle NoBinPacking = getLLVMStyle();
6313   NoBinPacking.BinPackParameters = false;
6314   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6315                "                        double *min_x,\n"
6316                "                        double *max_x,\n"
6317                "                        double *min_y,\n"
6318                "                        double *max_y,\n"
6319                "                        double *min_z,\n"
6320                "                        double *max_z, ) {}",
6321                NoBinPacking);
6322 }
6323 
6324 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6325   verifyFormat("void f() { return; }\n42");
6326   verifyFormat("void f() {\n"
6327                "  if (0)\n"
6328                "    return;\n"
6329                "}\n"
6330                "42");
6331   verifyFormat("void f() { return }\n42");
6332   verifyFormat("void f() {\n"
6333                "  if (0)\n"
6334                "    return\n"
6335                "}\n"
6336                "42");
6337 }
6338 
6339 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6340   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6341   EXPECT_EQ("void f() {\n"
6342             "  if (a)\n"
6343             "    return\n"
6344             "}",
6345             format("void  f  (  )  {  if  ( a )  return  }"));
6346   EXPECT_EQ("namespace N {\n"
6347             "void f()\n"
6348             "}",
6349             format("namespace  N  {  void f()  }"));
6350   EXPECT_EQ("namespace N {\n"
6351             "void f() {}\n"
6352             "void g()\n"
6353             "}",
6354             format("namespace N  { void f( ) { } void g( ) }"));
6355 }
6356 
6357 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6358   verifyFormat("int aaaaaaaa =\n"
6359                "    // Overlylongcomment\n"
6360                "    b;",
6361                getLLVMStyleWithColumns(20));
6362   verifyFormat("function(\n"
6363                "    ShortArgument,\n"
6364                "    LoooooooooooongArgument);\n",
6365                getLLVMStyleWithColumns(20));
6366 }
6367 
6368 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6369   verifyFormat("public:");
6370   verifyFormat("class A {\n"
6371                "public\n"
6372                "  void f() {}\n"
6373                "};");
6374   verifyFormat("public\n"
6375                "int qwerty;");
6376   verifyFormat("public\n"
6377                "B {}");
6378   verifyFormat("public\n"
6379                "{}");
6380   verifyFormat("public\n"
6381                "B { int x; }");
6382 }
6383 
6384 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6385   verifyFormat("{");
6386   verifyFormat("#})");
6387   verifyNoCrash("(/**/[:!] ?[).");
6388 }
6389 
6390 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6391   verifyFormat("do {\n}");
6392   verifyFormat("do {\n}\n"
6393                "f();");
6394   verifyFormat("do {\n}\n"
6395                "wheeee(fun);");
6396   verifyFormat("do {\n"
6397                "  f();\n"
6398                "}");
6399 }
6400 
6401 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6402   verifyFormat("if {\n  foo;\n  foo();\n}");
6403   verifyFormat("switch {\n  foo;\n  foo();\n}");
6404   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6405   verifyFormat("while {\n  foo;\n  foo();\n}");
6406   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6407 }
6408 
6409 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6410   verifyIncompleteFormat("namespace {\n"
6411                          "class Foo { Foo (\n"
6412                          "};\n"
6413                          "} // comment");
6414 }
6415 
6416 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6417   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6418   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6419   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6420   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6421 
6422   EXPECT_EQ("{\n"
6423             "  {\n"
6424             "    breakme(\n"
6425             "        qwe);\n"
6426             "  }\n",
6427             format("{\n"
6428                    "    {\n"
6429                    " breakme(qwe);\n"
6430                    "}\n",
6431                    getLLVMStyleWithColumns(10)));
6432 }
6433 
6434 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6435   verifyFormat("int x = {\n"
6436                "    avariable,\n"
6437                "    b(alongervariable)};",
6438                getLLVMStyleWithColumns(25));
6439 }
6440 
6441 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6442   verifyFormat("return (a)(b){1, 2, 3};");
6443 }
6444 
6445 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6446   verifyFormat("vector<int> x{1, 2, 3, 4};");
6447   verifyFormat("vector<int> x{\n"
6448                "    1, 2, 3, 4,\n"
6449                "};");
6450   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6451   verifyFormat("f({1, 2});");
6452   verifyFormat("auto v = Foo{-1};");
6453   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6454   verifyFormat("Class::Class : member{1, 2, 3} {}");
6455   verifyFormat("new vector<int>{1, 2, 3};");
6456   verifyFormat("new int[3]{1, 2, 3};");
6457   verifyFormat("new int{1};");
6458   verifyFormat("return {arg1, arg2};");
6459   verifyFormat("return {arg1, SomeType{parameter}};");
6460   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6461   verifyFormat("new T{arg1, arg2};");
6462   verifyFormat("f(MyMap[{composite, key}]);");
6463   verifyFormat("class Class {\n"
6464                "  T member = {arg1, arg2};\n"
6465                "};");
6466   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6467   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6468   verifyFormat("int a = std::is_integral<int>{} + 0;");
6469 
6470   verifyFormat("int foo(int i) { return fo1{}(i); }");
6471   verifyFormat("int foo(int i) { return fo1{}(i); }");
6472   verifyFormat("auto i = decltype(x){};");
6473   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6474   verifyFormat("Node n{1, Node{1000}, //\n"
6475                "       2};");
6476   verifyFormat("Aaaa aaaaaaa{\n"
6477                "    {\n"
6478                "        aaaa,\n"
6479                "    },\n"
6480                "};");
6481   verifyFormat("class C : public D {\n"
6482                "  SomeClass SC{2};\n"
6483                "};");
6484   verifyFormat("class C : public A {\n"
6485                "  class D : public B {\n"
6486                "    void f() { int i{2}; }\n"
6487                "  };\n"
6488                "};");
6489   verifyFormat("#define A {a, a},");
6490 
6491   // In combination with BinPackArguments = false.
6492   FormatStyle NoBinPacking = getLLVMStyle();
6493   NoBinPacking.BinPackArguments = false;
6494   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6495                "                      bbbbb,\n"
6496                "                      ccccc,\n"
6497                "                      ddddd,\n"
6498                "                      eeeee,\n"
6499                "                      ffffff,\n"
6500                "                      ggggg,\n"
6501                "                      hhhhhh,\n"
6502                "                      iiiiii,\n"
6503                "                      jjjjjj,\n"
6504                "                      kkkkkk};",
6505                NoBinPacking);
6506   verifyFormat("const Aaaaaa aaaaa = {\n"
6507                "    aaaaa,\n"
6508                "    bbbbb,\n"
6509                "    ccccc,\n"
6510                "    ddddd,\n"
6511                "    eeeee,\n"
6512                "    ffffff,\n"
6513                "    ggggg,\n"
6514                "    hhhhhh,\n"
6515                "    iiiiii,\n"
6516                "    jjjjjj,\n"
6517                "    kkkkkk,\n"
6518                "};",
6519                NoBinPacking);
6520   verifyFormat(
6521       "const Aaaaaa aaaaa = {\n"
6522       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6523       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6524       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6525       "};",
6526       NoBinPacking);
6527 
6528   // FIXME: The alignment of these trailing comments might be bad. Then again,
6529   // this might be utterly useless in real code.
6530   verifyFormat("Constructor::Constructor()\n"
6531                "    : some_value{         //\n"
6532                "                 aaaaaaa, //\n"
6533                "                 bbbbbbb} {}");
6534 
6535   // In braced lists, the first comment is always assumed to belong to the
6536   // first element. Thus, it can be moved to the next or previous line as
6537   // appropriate.
6538   EXPECT_EQ("function({// First element:\n"
6539             "          1,\n"
6540             "          // Second element:\n"
6541             "          2});",
6542             format("function({\n"
6543                    "    // First element:\n"
6544                    "    1,\n"
6545                    "    // Second element:\n"
6546                    "    2});"));
6547   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6548             "    // First element:\n"
6549             "    1,\n"
6550             "    // Second element:\n"
6551             "    2};",
6552             format("std::vector<int> MyNumbers{// First element:\n"
6553                    "                           1,\n"
6554                    "                           // Second element:\n"
6555                    "                           2};",
6556                    getLLVMStyleWithColumns(30)));
6557   // A trailing comma should still lead to an enforced line break.
6558   EXPECT_EQ("vector<int> SomeVector = {\n"
6559             "    // aaa\n"
6560             "    1, 2,\n"
6561             "};",
6562             format("vector<int> SomeVector = { // aaa\n"
6563                    "    1, 2, };"));
6564 
6565   FormatStyle ExtraSpaces = getLLVMStyle();
6566   ExtraSpaces.Cpp11BracedListStyle = false;
6567   ExtraSpaces.ColumnLimit = 75;
6568   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6569   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6570   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6571   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6572   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6573   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6574   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6575   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6576   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6577   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6578   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6579   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6580   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6581   verifyFormat("class Class {\n"
6582                "  T member = { arg1, arg2 };\n"
6583                "};",
6584                ExtraSpaces);
6585   verifyFormat(
6586       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6587       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6588       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6589       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6590       ExtraSpaces);
6591   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6592   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6593                ExtraSpaces);
6594   verifyFormat(
6595       "someFunction(OtherParam,\n"
6596       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6597       "                         param1, param2,\n"
6598       "                         // comment 2\n"
6599       "                         param3, param4 });",
6600       ExtraSpaces);
6601   verifyFormat(
6602       "std::this_thread::sleep_for(\n"
6603       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6604       ExtraSpaces);
6605   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6606                "    aaaaaaa,\n"
6607                "    aaaaaaaaaa,\n"
6608                "    aaaaa,\n"
6609                "    aaaaaaaaaaaaaaa,\n"
6610                "    aaa,\n"
6611                "    aaaaaaaaaa,\n"
6612                "    a,\n"
6613                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6614                "    aaaaaaaaaaaa,\n"
6615                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6616                "    aaaaaaa,\n"
6617                "    a};");
6618   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6619 }
6620 
6621 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6622   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6623                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6624                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6625                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6626                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6627                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6628   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6629                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6630                "                 1, 22, 333, 4444, 55555, //\n"
6631                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6632                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6633   verifyFormat(
6634       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6635       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6636       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6637       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6638       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6639       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6640       "                 7777777};");
6641   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6642                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6643                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6644   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6645                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6646                "    // Separating comment.\n"
6647                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6648   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6649                "    // Leading comment\n"
6650                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6651                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6652   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6653                "                 1, 1, 1, 1};",
6654                getLLVMStyleWithColumns(39));
6655   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6656                "                 1, 1, 1, 1};",
6657                getLLVMStyleWithColumns(38));
6658   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6659                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6660                getLLVMStyleWithColumns(43));
6661   verifyFormat(
6662       "static unsigned SomeValues[10][3] = {\n"
6663       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6664       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6665   verifyFormat("static auto fields = new vector<string>{\n"
6666                "    \"aaaaaaaaaaaaa\",\n"
6667                "    \"aaaaaaaaaaaaa\",\n"
6668                "    \"aaaaaaaaaaaa\",\n"
6669                "    \"aaaaaaaaaaaaaa\",\n"
6670                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6671                "    \"aaaaaaaaaaaa\",\n"
6672                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6673                "};");
6674   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6675   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6676                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6677                "                 3, cccccccccccccccccccccc};",
6678                getLLVMStyleWithColumns(60));
6679 
6680   // Trailing commas.
6681   verifyFormat("vector<int> x = {\n"
6682                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6683                "};",
6684                getLLVMStyleWithColumns(39));
6685   verifyFormat("vector<int> x = {\n"
6686                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6687                "};",
6688                getLLVMStyleWithColumns(39));
6689   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6690                "                 1, 1, 1, 1,\n"
6691                "                 /**/ /**/};",
6692                getLLVMStyleWithColumns(39));
6693 
6694   // Trailing comment in the first line.
6695   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6696                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6697                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6698                "    11111111,   22222222,   333333333,   44444444};");
6699   // Trailing comment in the last line.
6700   verifyFormat("int aaaaa[] = {\n"
6701                "    1, 2, 3, // comment\n"
6702                "    4, 5, 6  // comment\n"
6703                "};");
6704 
6705   // With nested lists, we should either format one item per line or all nested
6706   // lists one on line.
6707   // FIXME: For some nested lists, we can do better.
6708   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6709                "        {aaaaaaaaaaaaaaaaaaa},\n"
6710                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6711                "        {aaaaaaaaaaaaaaaaa}};",
6712                getLLVMStyleWithColumns(60));
6713   verifyFormat(
6714       "SomeStruct my_struct_array = {\n"
6715       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6716       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6717       "    {aaa, aaa},\n"
6718       "    {aaa, aaa},\n"
6719       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6720       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6721       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6722 
6723   // No column layout should be used here.
6724   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6725                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6726 
6727   verifyNoCrash("a<,");
6728 
6729   // No braced initializer here.
6730   verifyFormat("void f() {\n"
6731                "  struct Dummy {};\n"
6732                "  f(v);\n"
6733                "}");
6734 
6735   // Long lists should be formatted in columns even if they are nested.
6736   verifyFormat(
6737       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6738       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6739       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6740       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6741       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6742       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6743 }
6744 
6745 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6746   FormatStyle DoNotMerge = getLLVMStyle();
6747   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6748 
6749   verifyFormat("void f() { return 42; }");
6750   verifyFormat("void f() {\n"
6751                "  return 42;\n"
6752                "}",
6753                DoNotMerge);
6754   verifyFormat("void f() {\n"
6755                "  // Comment\n"
6756                "}");
6757   verifyFormat("{\n"
6758                "#error {\n"
6759                "  int a;\n"
6760                "}");
6761   verifyFormat("{\n"
6762                "  int a;\n"
6763                "#error {\n"
6764                "}");
6765   verifyFormat("void f() {} // comment");
6766   verifyFormat("void f() { int a; } // comment");
6767   verifyFormat("void f() {\n"
6768                "} // comment",
6769                DoNotMerge);
6770   verifyFormat("void f() {\n"
6771                "  int a;\n"
6772                "} // comment",
6773                DoNotMerge);
6774   verifyFormat("void f() {\n"
6775                "} // comment",
6776                getLLVMStyleWithColumns(15));
6777 
6778   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6779   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6780 
6781   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6782   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6783   verifyFormat("class C {\n"
6784                "  C()\n"
6785                "      : iiiiiiii(nullptr),\n"
6786                "        kkkkkkk(nullptr),\n"
6787                "        mmmmmmm(nullptr),\n"
6788                "        nnnnnnn(nullptr) {}\n"
6789                "};",
6790                getGoogleStyle());
6791 
6792   FormatStyle NoColumnLimit = getLLVMStyle();
6793   NoColumnLimit.ColumnLimit = 0;
6794   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6795   EXPECT_EQ("class C {\n"
6796             "  A() : b(0) {}\n"
6797             "};",
6798             format("class C{A():b(0){}};", NoColumnLimit));
6799   EXPECT_EQ("A()\n"
6800             "    : b(0) {\n"
6801             "}",
6802             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6803 
6804   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6805   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6806       FormatStyle::SFS_None;
6807   EXPECT_EQ("A()\n"
6808             "    : b(0) {\n"
6809             "}",
6810             format("A():b(0){}", DoNotMergeNoColumnLimit));
6811   EXPECT_EQ("A()\n"
6812             "    : b(0) {\n"
6813             "}",
6814             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6815 
6816   verifyFormat("#define A          \\\n"
6817                "  void f() {       \\\n"
6818                "    int i;         \\\n"
6819                "  }",
6820                getLLVMStyleWithColumns(20));
6821   verifyFormat("#define A           \\\n"
6822                "  void f() { int i; }",
6823                getLLVMStyleWithColumns(21));
6824   verifyFormat("#define A            \\\n"
6825                "  void f() {         \\\n"
6826                "    int i;           \\\n"
6827                "  }                  \\\n"
6828                "  int j;",
6829                getLLVMStyleWithColumns(22));
6830   verifyFormat("#define A             \\\n"
6831                "  void f() { int i; } \\\n"
6832                "  int j;",
6833                getLLVMStyleWithColumns(23));
6834 }
6835 
6836 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6837   FormatStyle MergeInlineOnly = getLLVMStyle();
6838   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6839   verifyFormat("class C {\n"
6840                "  int f() { return 42; }\n"
6841                "};",
6842                MergeInlineOnly);
6843   verifyFormat("int f() {\n"
6844                "  return 42;\n"
6845                "}",
6846                MergeInlineOnly);
6847 }
6848 
6849 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6850   // Elaborate type variable declarations.
6851   verifyFormat("struct foo a = {bar};\nint n;");
6852   verifyFormat("class foo a = {bar};\nint n;");
6853   verifyFormat("union foo a = {bar};\nint n;");
6854 
6855   // Elaborate types inside function definitions.
6856   verifyFormat("struct foo f() {}\nint n;");
6857   verifyFormat("class foo f() {}\nint n;");
6858   verifyFormat("union foo f() {}\nint n;");
6859 
6860   // Templates.
6861   verifyFormat("template <class X> void f() {}\nint n;");
6862   verifyFormat("template <struct X> void f() {}\nint n;");
6863   verifyFormat("template <union X> void f() {}\nint n;");
6864 
6865   // Actual definitions...
6866   verifyFormat("struct {\n} n;");
6867   verifyFormat(
6868       "template <template <class T, class Y>, class Z> class X {\n} n;");
6869   verifyFormat("union Z {\n  int n;\n} x;");
6870   verifyFormat("class MACRO Z {\n} n;");
6871   verifyFormat("class MACRO(X) Z {\n} n;");
6872   verifyFormat("class __attribute__(X) Z {\n} n;");
6873   verifyFormat("class __declspec(X) Z {\n} n;");
6874   verifyFormat("class A##B##C {\n} n;");
6875   verifyFormat("class alignas(16) Z {\n} n;");
6876   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6877   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6878 
6879   // Redefinition from nested context:
6880   verifyFormat("class A::B::C {\n} n;");
6881 
6882   // Template definitions.
6883   verifyFormat(
6884       "template <typename F>\n"
6885       "Matcher(const Matcher<F> &Other,\n"
6886       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6887       "                             !is_same<F, T>::value>::type * = 0)\n"
6888       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6889 
6890   // FIXME: This is still incorrectly handled at the formatter side.
6891   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6892   verifyFormat("int i = SomeFunction(a<b, a> b);");
6893 
6894   // FIXME:
6895   // This now gets parsed incorrectly as class definition.
6896   // verifyFormat("class A<int> f() {\n}\nint n;");
6897 
6898   // Elaborate types where incorrectly parsing the structural element would
6899   // break the indent.
6900   verifyFormat("if (true)\n"
6901                "  class X x;\n"
6902                "else\n"
6903                "  f();\n");
6904 
6905   // This is simply incomplete. Formatting is not important, but must not crash.
6906   verifyFormat("class A:");
6907 }
6908 
6909 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6910   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6911             format("#error Leave     all         white!!!!! space* alone!\n"));
6912   EXPECT_EQ(
6913       "#warning Leave     all         white!!!!! space* alone!\n",
6914       format("#warning Leave     all         white!!!!! space* alone!\n"));
6915   EXPECT_EQ("#error 1", format("  #  error   1"));
6916   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6917 }
6918 
6919 TEST_F(FormatTest, FormatHashIfExpressions) {
6920   verifyFormat("#if AAAA && BBBB");
6921   verifyFormat("#if (AAAA && BBBB)");
6922   verifyFormat("#elif (AAAA && BBBB)");
6923   // FIXME: Come up with a better indentation for #elif.
6924   verifyFormat(
6925       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6926       "    defined(BBBBBBBB)\n"
6927       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6928       "    defined(BBBBBBBB)\n"
6929       "#endif",
6930       getLLVMStyleWithColumns(65));
6931 }
6932 
6933 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6934   FormatStyle AllowsMergedIf = getGoogleStyle();
6935   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6936   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6937   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6938   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6939   EXPECT_EQ("if (true) return 42;",
6940             format("if (true)\nreturn 42;", AllowsMergedIf));
6941   FormatStyle ShortMergedIf = AllowsMergedIf;
6942   ShortMergedIf.ColumnLimit = 25;
6943   verifyFormat("#define A \\\n"
6944                "  if (true) return 42;",
6945                ShortMergedIf);
6946   verifyFormat("#define A \\\n"
6947                "  f();    \\\n"
6948                "  if (true)\n"
6949                "#define B",
6950                ShortMergedIf);
6951   verifyFormat("#define A \\\n"
6952                "  f();    \\\n"
6953                "  if (true)\n"
6954                "g();",
6955                ShortMergedIf);
6956   verifyFormat("{\n"
6957                "#ifdef A\n"
6958                "  // Comment\n"
6959                "  if (true) continue;\n"
6960                "#endif\n"
6961                "  // Comment\n"
6962                "  if (true) continue;\n"
6963                "}",
6964                ShortMergedIf);
6965   ShortMergedIf.ColumnLimit = 29;
6966   verifyFormat("#define A                   \\\n"
6967                "  if (aaaaaaaaaa) return 1; \\\n"
6968                "  return 2;",
6969                ShortMergedIf);
6970   ShortMergedIf.ColumnLimit = 28;
6971   verifyFormat("#define A         \\\n"
6972                "  if (aaaaaaaaaa) \\\n"
6973                "    return 1;     \\\n"
6974                "  return 2;",
6975                ShortMergedIf);
6976 }
6977 
6978 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6979   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6980                "  f();\n"
6981                "}");
6982   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6983                "  f();\n"
6984                "} /* another comment */ else /* comment #3 */ {\n"
6985                "  g();\n"
6986                "}");
6987   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6988                "  f();\n"
6989                "}");
6990   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6991                "  f();\n"
6992                "}");
6993   verifyFormat("do /* a comment in a strange place */ {\n"
6994                "  f();\n"
6995                "} /* another comment */ while (0);");
6996 }
6997 
6998 TEST_F(FormatTest, BlockComments) {
6999   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
7000             format("/* *//* */  /* */\n/* *//* */  /* */"));
7001   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
7002   EXPECT_EQ("#define A /*123*/ \\\n"
7003             "  b\n"
7004             "/* */\n"
7005             "someCall(\n"
7006             "    parameter);",
7007             format("#define A /*123*/ b\n"
7008                    "/* */\n"
7009                    "someCall(parameter);",
7010                    getLLVMStyleWithColumns(15)));
7011 
7012   EXPECT_EQ("#define A\n"
7013             "/* */ someCall(\n"
7014             "    parameter);",
7015             format("#define A\n"
7016                    "/* */someCall(parameter);",
7017                    getLLVMStyleWithColumns(15)));
7018   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
7019   EXPECT_EQ("/*\n"
7020             "*\n"
7021             " * aaaaaa\n"
7022             " * aaaaaa\n"
7023             "*/",
7024             format("/*\n"
7025                    "*\n"
7026                    " * aaaaaa aaaaaa\n"
7027                    "*/",
7028                    getLLVMStyleWithColumns(10)));
7029   EXPECT_EQ("/*\n"
7030             "**\n"
7031             "* aaaaaa\n"
7032             "*aaaaaa\n"
7033             "*/",
7034             format("/*\n"
7035                    "**\n"
7036                    "* aaaaaa aaaaaa\n"
7037                    "*/",
7038                    getLLVMStyleWithColumns(10)));
7039   EXPECT_EQ("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7040             "    /* line 1\n"
7041             "       bbbbbbbbbbbb */\n"
7042             "    bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7043             format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
7044                    "    /* line 1\n"
7045                    "       bbbbbbbbbbbb */ bbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
7046             getLLVMStyleWithColumns(50)));
7047 
7048   FormatStyle NoBinPacking = getLLVMStyle();
7049   NoBinPacking.BinPackParameters = false;
7050   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
7051             "             2, /* comment 2 */\n"
7052             "             3, /* comment 3 */\n"
7053             "             aaaa,\n"
7054             "             bbbb);",
7055             format("someFunction (1,   /* comment 1 */\n"
7056                    "                2, /* comment 2 */  \n"
7057                    "               3,   /* comment 3 */\n"
7058                    "aaaa, bbbb );",
7059                    NoBinPacking));
7060   verifyFormat(
7061       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7062       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
7063   EXPECT_EQ(
7064       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
7065       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
7066       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
7067       format(
7068           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
7069           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
7070           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
7071   EXPECT_EQ(
7072       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7073       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
7074       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
7075       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
7076              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
7077              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
7078 
7079   verifyFormat("void f(int * /* unused */) {}");
7080 
7081   EXPECT_EQ("/*\n"
7082             " **\n"
7083             " */",
7084             format("/*\n"
7085                    " **\n"
7086                    " */"));
7087   EXPECT_EQ("/*\n"
7088             " *q\n"
7089             " */",
7090             format("/*\n"
7091                    " *q\n"
7092                    " */"));
7093   EXPECT_EQ("/*\n"
7094             " * q\n"
7095             " */",
7096             format("/*\n"
7097                    " * q\n"
7098                    " */"));
7099   EXPECT_EQ("/*\n"
7100             " **/",
7101             format("/*\n"
7102                    " **/"));
7103   EXPECT_EQ("/*\n"
7104             " ***/",
7105             format("/*\n"
7106                    " ***/"));
7107 }
7108 
7109 TEST_F(FormatTest, BlockCommentsInMacros) {
7110   EXPECT_EQ("#define A          \\\n"
7111             "  {                \\\n"
7112             "    /* one line */ \\\n"
7113             "    someCall();",
7114             format("#define A {        \\\n"
7115                    "  /* one line */   \\\n"
7116                    "  someCall();",
7117                    getLLVMStyleWithColumns(20)));
7118   EXPECT_EQ("#define A          \\\n"
7119             "  {                \\\n"
7120             "    /* previous */ \\\n"
7121             "    /* one line */ \\\n"
7122             "    someCall();",
7123             format("#define A {        \\\n"
7124                    "  /* previous */   \\\n"
7125                    "  /* one line */   \\\n"
7126                    "  someCall();",
7127                    getLLVMStyleWithColumns(20)));
7128 }
7129 
7130 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
7131   EXPECT_EQ("a = {\n"
7132             "    1111 /*    */\n"
7133             "};",
7134             format("a = {1111 /*    */\n"
7135                    "};",
7136                    getLLVMStyleWithColumns(15)));
7137   EXPECT_EQ("a = {\n"
7138             "    1111 /*      */\n"
7139             "};",
7140             format("a = {1111 /*      */\n"
7141                    "};",
7142                    getLLVMStyleWithColumns(15)));
7143 
7144   // FIXME: The formatting is still wrong here.
7145   EXPECT_EQ("a = {\n"
7146             "    1111 /*      a\n"
7147             "            */\n"
7148             "};",
7149             format("a = {1111 /*      a */\n"
7150                    "};",
7151                    getLLVMStyleWithColumns(15)));
7152 }
7153 
7154 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
7155   verifyFormat("{\n"
7156                "  // a\n"
7157                "  // b");
7158 }
7159 
7160 TEST_F(FormatTest, FormatStarDependingOnContext) {
7161   verifyFormat("void f(int *a);");
7162   verifyFormat("void f() { f(fint * b); }");
7163   verifyFormat("class A {\n  void f(int *a);\n};");
7164   verifyFormat("class A {\n  int *a;\n};");
7165   verifyFormat("namespace a {\n"
7166                "namespace b {\n"
7167                "class A {\n"
7168                "  void f() {}\n"
7169                "  int *a;\n"
7170                "};\n"
7171                "}\n"
7172                "}");
7173 }
7174 
7175 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7176   verifyFormat("while");
7177   verifyFormat("operator");
7178 }
7179 
7180 //===----------------------------------------------------------------------===//
7181 // Objective-C tests.
7182 //===----------------------------------------------------------------------===//
7183 
7184 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7185   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7186   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7187             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7188   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7189   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7190   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7191             format("-(NSInteger)Method3:(id)anObject;"));
7192   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7193             format("-(NSInteger)Method4:(id)anObject;"));
7194   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7195             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7196   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7197             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7198   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7199             "forAllCells:(BOOL)flag;",
7200             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7201                    "forAllCells:(BOOL)flag;"));
7202 
7203   // Very long objectiveC method declaration.
7204   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7205                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7206   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7207                "                    inRange:(NSRange)range\n"
7208                "                   outRange:(NSRange)out_range\n"
7209                "                  outRange1:(NSRange)out_range1\n"
7210                "                  outRange2:(NSRange)out_range2\n"
7211                "                  outRange3:(NSRange)out_range3\n"
7212                "                  outRange4:(NSRange)out_range4\n"
7213                "                  outRange5:(NSRange)out_range5\n"
7214                "                  outRange6:(NSRange)out_range6\n"
7215                "                  outRange7:(NSRange)out_range7\n"
7216                "                  outRange8:(NSRange)out_range8\n"
7217                "                  outRange9:(NSRange)out_range9;");
7218 
7219   // When the function name has to be wrapped.
7220   FormatStyle Style = getLLVMStyle();
7221   Style.IndentWrappedFunctionNames = false;
7222   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7223                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7224                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7225                "}",
7226                Style);
7227   Style.IndentWrappedFunctionNames = true;
7228   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7229                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7230                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7231                "}",
7232                Style);
7233 
7234   verifyFormat("- (int)sum:(vector<int>)numbers;");
7235   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7236   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7237   // protocol lists (but not for template classes):
7238   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7239 
7240   verifyFormat("- (int (*)())foo:(int (*)())f;");
7241   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7242 
7243   // If there's no return type (very rare in practice!), LLVM and Google style
7244   // agree.
7245   verifyFormat("- foo;");
7246   verifyFormat("- foo:(int)f;");
7247   verifyGoogleFormat("- foo:(int)foo;");
7248 }
7249 
7250 TEST_F(FormatTest, FormatObjCInterface) {
7251   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
7252                "@public\n"
7253                "  int field1;\n"
7254                "@protected\n"
7255                "  int field2;\n"
7256                "@private\n"
7257                "  int field3;\n"
7258                "@package\n"
7259                "  int field4;\n"
7260                "}\n"
7261                "+ (id)init;\n"
7262                "@end");
7263 
7264   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
7265                      " @public\n"
7266                      "  int field1;\n"
7267                      " @protected\n"
7268                      "  int field2;\n"
7269                      " @private\n"
7270                      "  int field3;\n"
7271                      " @package\n"
7272                      "  int field4;\n"
7273                      "}\n"
7274                      "+ (id)init;\n"
7275                      "@end");
7276 
7277   verifyFormat("@interface /* wait for it */ Foo\n"
7278                "+ (id)init;\n"
7279                "// Look, a comment!\n"
7280                "- (int)answerWith:(int)i;\n"
7281                "@end");
7282 
7283   verifyFormat("@interface Foo\n"
7284                "@end\n"
7285                "@interface Bar\n"
7286                "@end");
7287 
7288   verifyFormat("@interface Foo : Bar\n"
7289                "+ (id)init;\n"
7290                "@end");
7291 
7292   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
7293                "+ (id)init;\n"
7294                "@end");
7295 
7296   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
7297                      "+ (id)init;\n"
7298                      "@end");
7299 
7300   verifyFormat("@interface Foo (HackStuff)\n"
7301                "+ (id)init;\n"
7302                "@end");
7303 
7304   verifyFormat("@interface Foo ()\n"
7305                "+ (id)init;\n"
7306                "@end");
7307 
7308   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
7309                "+ (id)init;\n"
7310                "@end");
7311 
7312   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
7313                      "+ (id)init;\n"
7314                      "@end");
7315 
7316   verifyFormat("@interface Foo {\n"
7317                "  int _i;\n"
7318                "}\n"
7319                "+ (id)init;\n"
7320                "@end");
7321 
7322   verifyFormat("@interface Foo : Bar {\n"
7323                "  int _i;\n"
7324                "}\n"
7325                "+ (id)init;\n"
7326                "@end");
7327 
7328   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
7329                "  int _i;\n"
7330                "}\n"
7331                "+ (id)init;\n"
7332                "@end");
7333 
7334   verifyFormat("@interface Foo (HackStuff) {\n"
7335                "  int _i;\n"
7336                "}\n"
7337                "+ (id)init;\n"
7338                "@end");
7339 
7340   verifyFormat("@interface Foo () {\n"
7341                "  int _i;\n"
7342                "}\n"
7343                "+ (id)init;\n"
7344                "@end");
7345 
7346   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
7347                "  int _i;\n"
7348                "}\n"
7349                "+ (id)init;\n"
7350                "@end");
7351 
7352   FormatStyle OnePerLine = getGoogleStyle();
7353   OnePerLine.BinPackParameters = false;
7354   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
7355                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7356                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7357                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
7358                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
7359                "}",
7360                OnePerLine);
7361 }
7362 
7363 TEST_F(FormatTest, FormatObjCImplementation) {
7364   verifyFormat("@implementation Foo : NSObject {\n"
7365                "@public\n"
7366                "  int field1;\n"
7367                "@protected\n"
7368                "  int field2;\n"
7369                "@private\n"
7370                "  int field3;\n"
7371                "@package\n"
7372                "  int field4;\n"
7373                "}\n"
7374                "+ (id)init {\n}\n"
7375                "@end");
7376 
7377   verifyGoogleFormat("@implementation Foo : NSObject {\n"
7378                      " @public\n"
7379                      "  int field1;\n"
7380                      " @protected\n"
7381                      "  int field2;\n"
7382                      " @private\n"
7383                      "  int field3;\n"
7384                      " @package\n"
7385                      "  int field4;\n"
7386                      "}\n"
7387                      "+ (id)init {\n}\n"
7388                      "@end");
7389 
7390   verifyFormat("@implementation Foo\n"
7391                "+ (id)init {\n"
7392                "  if (true)\n"
7393                "    return nil;\n"
7394                "}\n"
7395                "// Look, a comment!\n"
7396                "- (int)answerWith:(int)i {\n"
7397                "  return i;\n"
7398                "}\n"
7399                "+ (int)answerWith:(int)i {\n"
7400                "  return i;\n"
7401                "}\n"
7402                "@end");
7403 
7404   verifyFormat("@implementation Foo\n"
7405                "@end\n"
7406                "@implementation Bar\n"
7407                "@end");
7408 
7409   EXPECT_EQ("@implementation Foo : Bar\n"
7410             "+ (id)init {\n}\n"
7411             "- (void)foo {\n}\n"
7412             "@end",
7413             format("@implementation Foo : Bar\n"
7414                    "+(id)init{}\n"
7415                    "-(void)foo{}\n"
7416                    "@end"));
7417 
7418   verifyFormat("@implementation Foo {\n"
7419                "  int _i;\n"
7420                "}\n"
7421                "+ (id)init {\n}\n"
7422                "@end");
7423 
7424   verifyFormat("@implementation Foo : Bar {\n"
7425                "  int _i;\n"
7426                "}\n"
7427                "+ (id)init {\n}\n"
7428                "@end");
7429 
7430   verifyFormat("@implementation Foo (HackStuff)\n"
7431                "+ (id)init {\n}\n"
7432                "@end");
7433   verifyFormat("@implementation ObjcClass\n"
7434                "- (void)method;\n"
7435                "{}\n"
7436                "@end");
7437 }
7438 
7439 TEST_F(FormatTest, FormatObjCProtocol) {
7440   verifyFormat("@protocol Foo\n"
7441                "@property(weak) id delegate;\n"
7442                "- (NSUInteger)numberOfThings;\n"
7443                "@end");
7444 
7445   verifyFormat("@protocol MyProtocol <NSObject>\n"
7446                "- (NSUInteger)numberOfThings;\n"
7447                "@end");
7448 
7449   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
7450                      "- (NSUInteger)numberOfThings;\n"
7451                      "@end");
7452 
7453   verifyFormat("@protocol Foo;\n"
7454                "@protocol Bar;\n");
7455 
7456   verifyFormat("@protocol Foo\n"
7457                "@end\n"
7458                "@protocol Bar\n"
7459                "@end");
7460 
7461   verifyFormat("@protocol myProtocol\n"
7462                "- (void)mandatoryWithInt:(int)i;\n"
7463                "@optional\n"
7464                "- (void)optional;\n"
7465                "@required\n"
7466                "- (void)required;\n"
7467                "@optional\n"
7468                "@property(assign) int madProp;\n"
7469                "@end\n");
7470 
7471   verifyFormat("@property(nonatomic, assign, readonly)\n"
7472                "    int *looooooooooooooooooooooooooooongNumber;\n"
7473                "@property(nonatomic, assign, readonly)\n"
7474                "    NSString *looooooooooooooooooooooooooooongName;");
7475 
7476   verifyFormat("@implementation PR18406\n"
7477                "}\n"
7478                "@end");
7479 }
7480 
7481 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
7482   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
7483                "                   rect:(NSRect)theRect\n"
7484                "               interval:(float)theInterval {\n"
7485                "}");
7486   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7487                "      longKeyword:(NSRect)theRect\n"
7488                "    longerKeyword:(float)theInterval\n"
7489                "            error:(NSError **)theError {\n"
7490                "}");
7491   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7492                "          longKeyword:(NSRect)theRect\n"
7493                "    evenLongerKeyword:(float)theInterval\n"
7494                "                error:(NSError **)theError {\n"
7495                "}");
7496   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
7497                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
7498                "    NS_DESIGNATED_INITIALIZER;",
7499                getLLVMStyleWithColumns(60));
7500 
7501   // Continuation indent width should win over aligning colons if the function
7502   // name is long.
7503   FormatStyle continuationStyle = getGoogleStyle();
7504   continuationStyle.ColumnLimit = 40;
7505   continuationStyle.IndentWrappedFunctionNames = true;
7506   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7507                "    dontAlignNamef:(NSRect)theRect {\n"
7508                "}",
7509                continuationStyle);
7510 
7511   // Make sure we don't break aligning for short parameter names.
7512   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
7513                "       aShortf:(NSRect)theRect {\n"
7514                "}",
7515                continuationStyle);
7516 }
7517 
7518 TEST_F(FormatTest, FormatObjCMethodExpr) {
7519   verifyFormat("[foo bar:baz];");
7520   verifyFormat("return [foo bar:baz];");
7521   verifyFormat("return (a)[foo bar:baz];");
7522   verifyFormat("f([foo bar:baz]);");
7523   verifyFormat("f(2, [foo bar:baz]);");
7524   verifyFormat("f(2, a ? b : c);");
7525   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
7526 
7527   // Unary operators.
7528   verifyFormat("int a = +[foo bar:baz];");
7529   verifyFormat("int a = -[foo bar:baz];");
7530   verifyFormat("int a = ![foo bar:baz];");
7531   verifyFormat("int a = ~[foo bar:baz];");
7532   verifyFormat("int a = ++[foo bar:baz];");
7533   verifyFormat("int a = --[foo bar:baz];");
7534   verifyFormat("int a = sizeof [foo bar:baz];");
7535   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
7536   verifyFormat("int a = &[foo bar:baz];");
7537   verifyFormat("int a = *[foo bar:baz];");
7538   // FIXME: Make casts work, without breaking f()[4].
7539   // verifyFormat("int a = (int)[foo bar:baz];");
7540   // verifyFormat("return (int)[foo bar:baz];");
7541   // verifyFormat("(void)[foo bar:baz];");
7542   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7543 
7544   // Binary operators.
7545   verifyFormat("[foo bar:baz], [foo bar:baz];");
7546   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7547   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7548   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7549   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7550   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7551   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7552   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7553   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7554   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7555   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7556   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7557   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7558   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7559   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7560   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7561   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7562   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7563   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7564   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7565   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7566   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7567   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7568   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7569   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7570   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7571   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7572   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7573   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7574   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7575   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7576   // Whew!
7577 
7578   verifyFormat("return in[42];");
7579   verifyFormat("for (auto v : in[1]) {\n}");
7580   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7581   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7582   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7583   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7584   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7585   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7586                "}");
7587   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7588   verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
7589   verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
7590 
7591   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7592   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7593   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7594   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7595   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7596   verifyFormat("[button setAction:@selector(zoomOut:)];");
7597   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7598 
7599   verifyFormat("arr[[self indexForFoo:a]];");
7600   verifyFormat("throw [self errorFor:a];");
7601   verifyFormat("@throw [self errorFor:a];");
7602 
7603   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7604   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7605   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7606 
7607   // This tests that the formatter doesn't break after "backing" but before ":",
7608   // which would be at 80 columns.
7609   verifyFormat(
7610       "void f() {\n"
7611       "  if ((self = [super initWithContentRect:contentRect\n"
7612       "                               styleMask:styleMask ?: otherMask\n"
7613       "                                 backing:NSBackingStoreBuffered\n"
7614       "                                   defer:YES]))");
7615 
7616   verifyFormat(
7617       "[foo checkThatBreakingAfterColonWorksOk:\n"
7618       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7619 
7620   verifyFormat("[myObj short:arg1 // Force line break\n"
7621                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7622                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7623                "                error:arg4];");
7624   verifyFormat(
7625       "void f() {\n"
7626       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7627       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7628       "                                     pos.width(), pos.height())\n"
7629       "                styleMask:NSBorderlessWindowMask\n"
7630       "                  backing:NSBackingStoreBuffered\n"
7631       "                    defer:NO]);\n"
7632       "}");
7633   verifyFormat(
7634       "void f() {\n"
7635       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7636       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7637       "                                 pos.width(), pos.height())\n"
7638       "                syeMask:NSBorderlessWindowMask\n"
7639       "                  bking:NSBackingStoreBuffered\n"
7640       "                    der:NO]);\n"
7641       "}",
7642       getLLVMStyleWithColumns(70));
7643   verifyFormat(
7644       "void f() {\n"
7645       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7646       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7647       "                                     pos.width(), pos.height())\n"
7648       "                styleMask:NSBorderlessWindowMask\n"
7649       "                  backing:NSBackingStoreBuffered\n"
7650       "                    defer:NO]);\n"
7651       "}",
7652       getChromiumStyle(FormatStyle::LK_Cpp));
7653   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7654                "                             with:contentsNativeView];");
7655 
7656   verifyFormat(
7657       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7658       "           owner:nillllll];");
7659 
7660   verifyFormat(
7661       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7662       "        forType:kBookmarkButtonDragType];");
7663 
7664   verifyFormat("[defaultCenter addObserver:self\n"
7665                "                  selector:@selector(willEnterFullscreen)\n"
7666                "                      name:kWillEnterFullscreenNotification\n"
7667                "                    object:nil];");
7668   verifyFormat("[image_rep drawInRect:drawRect\n"
7669                "             fromRect:NSZeroRect\n"
7670                "            operation:NSCompositeCopy\n"
7671                "             fraction:1.0\n"
7672                "       respectFlipped:NO\n"
7673                "                hints:nil];");
7674   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7675                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7676   verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
7677                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
7678   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
7679                "    aaaaaaaaaaaaaaaaaaaaaa];");
7680   verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
7681                "        .aaaaaaaa];", // FIXME: Indentation seems off.
7682                getLLVMStyleWithColumns(60));
7683 
7684   verifyFormat(
7685       "scoped_nsobject<NSTextField> message(\n"
7686       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7687       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7688   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7689                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7690                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7691                "          aaaa:bbb];");
7692   verifyFormat("[self param:function( //\n"
7693                "                parameter)]");
7694   verifyFormat(
7695       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7696       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7697       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7698 
7699   // FIXME: This violates the column limit.
7700   verifyFormat(
7701       "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
7702       "    aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
7703       "                  aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];",
7704       getLLVMStyleWithColumns(60));
7705 
7706   // Variadic parameters.
7707   verifyFormat(
7708       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7709   verifyFormat(
7710       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7711       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7712       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7713   verifyFormat("[self // break\n"
7714                "      a:a\n"
7715                "    aaa:aaa];");
7716   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7717                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7718 }
7719 
7720 TEST_F(FormatTest, ObjCAt) {
7721   verifyFormat("@autoreleasepool");
7722   verifyFormat("@catch");
7723   verifyFormat("@class");
7724   verifyFormat("@compatibility_alias");
7725   verifyFormat("@defs");
7726   verifyFormat("@dynamic");
7727   verifyFormat("@encode");
7728   verifyFormat("@end");
7729   verifyFormat("@finally");
7730   verifyFormat("@implementation");
7731   verifyFormat("@import");
7732   verifyFormat("@interface");
7733   verifyFormat("@optional");
7734   verifyFormat("@package");
7735   verifyFormat("@private");
7736   verifyFormat("@property");
7737   verifyFormat("@protected");
7738   verifyFormat("@protocol");
7739   verifyFormat("@public");
7740   verifyFormat("@required");
7741   verifyFormat("@selector");
7742   verifyFormat("@synchronized");
7743   verifyFormat("@synthesize");
7744   verifyFormat("@throw");
7745   verifyFormat("@try");
7746 
7747   EXPECT_EQ("@interface", format("@ interface"));
7748 
7749   // The precise formatting of this doesn't matter, nobody writes code like
7750   // this.
7751   verifyFormat("@ /*foo*/ interface");
7752 }
7753 
7754 TEST_F(FormatTest, ObjCSnippets) {
7755   verifyFormat("@autoreleasepool {\n"
7756                "  foo();\n"
7757                "}");
7758   verifyFormat("@class Foo, Bar;");
7759   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7760   verifyFormat("@dynamic textColor;");
7761   verifyFormat("char *buf1 = @encode(int *);");
7762   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7763   verifyFormat("char *buf1 = @encode(int **);");
7764   verifyFormat("Protocol *proto = @protocol(p1);");
7765   verifyFormat("SEL s = @selector(foo:);");
7766   verifyFormat("@synchronized(self) {\n"
7767                "  f();\n"
7768                "}");
7769 
7770   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7771   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7772 
7773   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7774   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7775   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7776   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7777                getMozillaStyle());
7778   verifyFormat("@property BOOL editable;", getMozillaStyle());
7779   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7780                getWebKitStyle());
7781   verifyFormat("@property BOOL editable;", getWebKitStyle());
7782 
7783   verifyFormat("@import foo.bar;\n"
7784                "@import baz;");
7785 }
7786 
7787 TEST_F(FormatTest, ObjCForIn) {
7788   verifyFormat("- (void)test {\n"
7789                "  for (NSString *n in arrayOfStrings) {\n"
7790                "    foo(n);\n"
7791                "  }\n"
7792                "}");
7793   verifyFormat("- (void)test {\n"
7794                "  for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
7795                "    foo(n);\n"
7796                "  }\n"
7797                "}");
7798 }
7799 
7800 TEST_F(FormatTest, ObjCLiterals) {
7801   verifyFormat("@\"String\"");
7802   verifyFormat("@1");
7803   verifyFormat("@+4.8");
7804   verifyFormat("@-4");
7805   verifyFormat("@1LL");
7806   verifyFormat("@.5");
7807   verifyFormat("@'c'");
7808   verifyFormat("@true");
7809 
7810   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7811   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7812   verifyFormat("NSNumber *favoriteColor = @(Green);");
7813   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7814 
7815   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7816 }
7817 
7818 TEST_F(FormatTest, ObjCDictLiterals) {
7819   verifyFormat("@{");
7820   verifyFormat("@{}");
7821   verifyFormat("@{@\"one\" : @1}");
7822   verifyFormat("return @{@\"one\" : @1;");
7823   verifyFormat("@{@\"one\" : @1}");
7824 
7825   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7826   verifyFormat("@{\n"
7827                "  @\"one\" : @{@2 : @1},\n"
7828                "}");
7829 
7830   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7831   verifyIncompleteFormat("[self setDict:@{}");
7832   verifyIncompleteFormat("[self setDict:@{@1 : @2}");
7833   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7834   verifyFormat(
7835       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7836   verifyFormat(
7837       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7838 
7839   verifyFormat("NSDictionary *d = @{\n"
7840                "  @\"nam\" : NSUserNam(),\n"
7841                "  @\"dte\" : [NSDate date],\n"
7842                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7843                "};");
7844   verifyFormat(
7845       "@{\n"
7846       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7847       "regularFont,\n"
7848       "};");
7849   verifyGoogleFormat(
7850       "@{\n"
7851       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7852       "regularFont,\n"
7853       "};");
7854   verifyFormat(
7855       "@{\n"
7856       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7857       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7858       "};");
7859 
7860   // We should try to be robust in case someone forgets the "@".
7861   verifyFormat("NSDictionary *d = {\n"
7862                "  @\"nam\" : NSUserNam(),\n"
7863                "  @\"dte\" : [NSDate date],\n"
7864                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7865                "};");
7866   verifyFormat("NSMutableDictionary *dictionary =\n"
7867                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7868                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7869                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7870                "      cccccccccccccccc : ccccccccccccccc\n"
7871                "    }];");
7872 
7873   // Ensure that casts before the key are kept on the same line as the key.
7874   verifyFormat(
7875       "NSDictionary *d = @{\n"
7876       "  (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
7877       "  (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
7878       "};");
7879 }
7880 
7881 TEST_F(FormatTest, ObjCArrayLiterals) {
7882   verifyIncompleteFormat("@[");
7883   verifyFormat("@[]");
7884   verifyFormat(
7885       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7886   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7887   verifyFormat("NSArray *array = @[ [foo description] ];");
7888 
7889   verifyFormat(
7890       "NSArray *some_variable = @[\n"
7891       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7892       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7893       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7894       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7895       "];");
7896   verifyFormat(
7897       "NSArray *some_variable = @[\n"
7898       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7899       "  @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
7900       "];");
7901   verifyFormat("NSArray *some_variable = @[\n"
7902                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7903                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7904                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7905                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7906                "];");
7907   verifyFormat("NSArray *array = @[\n"
7908                "  @\"a\",\n"
7909                "  @\"a\",\n" // Trailing comma -> one per line.
7910                "];");
7911 
7912   // We should try to be robust in case someone forgets the "@".
7913   verifyFormat("NSArray *some_variable = [\n"
7914                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7915                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7916                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7917                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7918                "];");
7919   verifyFormat(
7920       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7921       "                                             index:(NSUInteger)index\n"
7922       "                                nonDigitAttributes:\n"
7923       "                                    (NSDictionary *)noDigitAttributes;");
7924   verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
7925                "  NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
7926                "]];");
7927 }
7928 
7929 TEST_F(FormatTest, BreaksStringLiterals) {
7930   EXPECT_EQ("\"some text \"\n"
7931             "\"other\";",
7932             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7933   EXPECT_EQ("\"some text \"\n"
7934             "\"other\";",
7935             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7936   EXPECT_EQ(
7937       "#define A  \\\n"
7938       "  \"some \"  \\\n"
7939       "  \"text \"  \\\n"
7940       "  \"other\";",
7941       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7942   EXPECT_EQ(
7943       "#define A  \\\n"
7944       "  \"so \"    \\\n"
7945       "  \"text \"  \\\n"
7946       "  \"other\";",
7947       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7948 
7949   EXPECT_EQ("\"some text\"",
7950             format("\"some text\"", getLLVMStyleWithColumns(1)));
7951   EXPECT_EQ("\"some text\"",
7952             format("\"some text\"", getLLVMStyleWithColumns(11)));
7953   EXPECT_EQ("\"some \"\n"
7954             "\"text\"",
7955             format("\"some text\"", getLLVMStyleWithColumns(10)));
7956   EXPECT_EQ("\"some \"\n"
7957             "\"text\"",
7958             format("\"some text\"", getLLVMStyleWithColumns(7)));
7959   EXPECT_EQ("\"some\"\n"
7960             "\" tex\"\n"
7961             "\"t\"",
7962             format("\"some text\"", getLLVMStyleWithColumns(6)));
7963   EXPECT_EQ("\"some\"\n"
7964             "\" tex\"\n"
7965             "\" and\"",
7966             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7967   EXPECT_EQ("\"some\"\n"
7968             "\"/tex\"\n"
7969             "\"/and\"",
7970             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7971 
7972   EXPECT_EQ("variable =\n"
7973             "    \"long string \"\n"
7974             "    \"literal\";",
7975             format("variable = \"long string literal\";",
7976                    getLLVMStyleWithColumns(20)));
7977 
7978   EXPECT_EQ("variable = f(\n"
7979             "    \"long string \"\n"
7980             "    \"literal\",\n"
7981             "    short,\n"
7982             "    loooooooooooooooooooong);",
7983             format("variable = f(\"long string literal\", short, "
7984                    "loooooooooooooooooooong);",
7985                    getLLVMStyleWithColumns(20)));
7986 
7987   EXPECT_EQ(
7988       "f(g(\"long string \"\n"
7989       "    \"literal\"),\n"
7990       "  b);",
7991       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7992   EXPECT_EQ("f(g(\"long string \"\n"
7993             "    \"literal\",\n"
7994             "    a),\n"
7995             "  b);",
7996             format("f(g(\"long string literal\", a), b);",
7997                    getLLVMStyleWithColumns(20)));
7998   EXPECT_EQ(
7999       "f(\"one two\".split(\n"
8000       "    variable));",
8001       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
8002   EXPECT_EQ("f(\"one two three four five six \"\n"
8003             "  \"seven\".split(\n"
8004             "      really_looooong_variable));",
8005             format("f(\"one two three four five six seven\"."
8006                    "split(really_looooong_variable));",
8007                    getLLVMStyleWithColumns(33)));
8008 
8009   EXPECT_EQ("f(\"some \"\n"
8010             "  \"text\",\n"
8011             "  other);",
8012             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
8013 
8014   // Only break as a last resort.
8015   verifyFormat(
8016       "aaaaaaaaaaaaaaaaaaaa(\n"
8017       "    aaaaaaaaaaaaaaaaaaaa,\n"
8018       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
8019 
8020   EXPECT_EQ("\"splitmea\"\n"
8021             "\"trandomp\"\n"
8022             "\"oint\"",
8023             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
8024 
8025   EXPECT_EQ("\"split/\"\n"
8026             "\"pathat/\"\n"
8027             "\"slashes\"",
8028             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8029 
8030   EXPECT_EQ("\"split/\"\n"
8031             "\"pathat/\"\n"
8032             "\"slashes\"",
8033             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
8034   EXPECT_EQ("\"split at \"\n"
8035             "\"spaces/at/\"\n"
8036             "\"slashes.at.any$\"\n"
8037             "\"non-alphanumeric%\"\n"
8038             "\"1111111111characte\"\n"
8039             "\"rs\"",
8040             format("\"split at "
8041                    "spaces/at/"
8042                    "slashes.at."
8043                    "any$non-"
8044                    "alphanumeric%"
8045                    "1111111111characte"
8046                    "rs\"",
8047                    getLLVMStyleWithColumns(20)));
8048 
8049   // Verify that splitting the strings understands
8050   // Style::AlwaysBreakBeforeMultilineStrings.
8051   EXPECT_EQ(
8052       "aaaaaaaaaaaa(\n"
8053       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
8054       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
8055       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
8056              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8057              "aaaaaaaaaaaaaaaaaaaaaa\");",
8058              getGoogleStyle()));
8059   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8060             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
8061             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
8062                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
8063                    "aaaaaaaaaaaaaaaaaaaaaa\";",
8064                    getGoogleStyle()));
8065   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8066             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8067             format("llvm::outs() << "
8068                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
8069                    "aaaaaaaaaaaaaaaaaaa\";"));
8070   EXPECT_EQ("ffff(\n"
8071             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
8072             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8073             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
8074                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
8075                    getGoogleStyle()));
8076 
8077   FormatStyle Style = getLLVMStyleWithColumns(12);
8078   Style.BreakStringLiterals = false;
8079   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
8080 
8081   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
8082   AlignLeft.AlignEscapedNewlinesLeft = true;
8083   EXPECT_EQ("#define A \\\n"
8084             "  \"some \" \\\n"
8085             "  \"text \" \\\n"
8086             "  \"other\";",
8087             format("#define A \"some text other\";", AlignLeft));
8088 }
8089 
8090 TEST_F(FormatTest, FullyRemoveEmptyLines) {
8091   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
8092   NoEmptyLines.MaxEmptyLinesToKeep = 0;
8093   EXPECT_EQ("int i = a(b());",
8094             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
8095 }
8096 
8097 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
8098   EXPECT_EQ(
8099       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8100       "(\n"
8101       "    \"x\t\");",
8102       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
8103              "aaaaaaa("
8104              "\"x\t\");"));
8105 }
8106 
8107 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
8108   EXPECT_EQ(
8109       "u8\"utf8 string \"\n"
8110       "u8\"literal\";",
8111       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
8112   EXPECT_EQ(
8113       "u\"utf16 string \"\n"
8114       "u\"literal\";",
8115       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
8116   EXPECT_EQ(
8117       "U\"utf32 string \"\n"
8118       "U\"literal\";",
8119       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
8120   EXPECT_EQ("L\"wide string \"\n"
8121             "L\"literal\";",
8122             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
8123   EXPECT_EQ("@\"NSString \"\n"
8124             "@\"literal\";",
8125             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
8126 
8127   // This input makes clang-format try to split the incomplete unicode escape
8128   // sequence, which used to lead to a crasher.
8129   verifyNoCrash(
8130       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
8131       getLLVMStyleWithColumns(60));
8132 }
8133 
8134 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
8135   FormatStyle Style = getGoogleStyleWithColumns(15);
8136   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
8137   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
8138   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
8139   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
8140   EXPECT_EQ("u8R\"x(raw literal)x\";",
8141             format("u8R\"x(raw literal)x\";", Style));
8142 }
8143 
8144 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
8145   FormatStyle Style = getLLVMStyleWithColumns(20);
8146   EXPECT_EQ(
8147       "_T(\"aaaaaaaaaaaaaa\")\n"
8148       "_T(\"aaaaaaaaaaaaaa\")\n"
8149       "_T(\"aaaaaaaaaaaa\")",
8150       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
8151   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
8152             "     _T(\"aaaaaa\"),\n"
8153             "  z);",
8154             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
8155 
8156   // FIXME: Handle embedded spaces in one iteration.
8157   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
8158   //            "_T(\"aaaaaaaaaaaaa\")\n"
8159   //            "_T(\"aaaaaaaaaaaaa\")\n"
8160   //            "_T(\"a\")",
8161   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8162   //                   getLLVMStyleWithColumns(20)));
8163   EXPECT_EQ(
8164       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
8165       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
8166   EXPECT_EQ("f(\n"
8167             "#if !TEST\n"
8168             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8169             "#endif\n"
8170             "    );",
8171             format("f(\n"
8172                    "#if !TEST\n"
8173                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
8174                    "#endif\n"
8175                    ");"));
8176   EXPECT_EQ("f(\n"
8177             "\n"
8178             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
8179             format("f(\n"
8180                    "\n"
8181                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
8182 }
8183 
8184 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
8185   EXPECT_EQ(
8186       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8187       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8188       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
8189       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8190              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
8191              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
8192 }
8193 
8194 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
8195   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
8196             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
8197   EXPECT_EQ("fffffffffff(g(R\"x(\n"
8198             "multiline raw string literal xxxxxxxxxxxxxx\n"
8199             ")x\",\n"
8200             "              a),\n"
8201             "            b);",
8202             format("fffffffffff(g(R\"x(\n"
8203                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8204                    ")x\", a), b);",
8205                    getGoogleStyleWithColumns(20)));
8206   EXPECT_EQ("fffffffffff(\n"
8207             "    g(R\"x(qqq\n"
8208             "multiline raw string literal xxxxxxxxxxxxxx\n"
8209             ")x\",\n"
8210             "      a),\n"
8211             "    b);",
8212             format("fffffffffff(g(R\"x(qqq\n"
8213                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8214                    ")x\", a), b);",
8215                    getGoogleStyleWithColumns(20)));
8216 
8217   EXPECT_EQ("fffffffffff(R\"x(\n"
8218             "multiline raw string literal xxxxxxxxxxxxxx\n"
8219             ")x\");",
8220             format("fffffffffff(R\"x(\n"
8221                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8222                    ")x\");",
8223                    getGoogleStyleWithColumns(20)));
8224   EXPECT_EQ("fffffffffff(R\"x(\n"
8225             "multiline raw string literal xxxxxxxxxxxxxx\n"
8226             ")x\" + bbbbbb);",
8227             format("fffffffffff(R\"x(\n"
8228                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8229                    ")x\" +   bbbbbb);",
8230                    getGoogleStyleWithColumns(20)));
8231   EXPECT_EQ("fffffffffff(\n"
8232             "    R\"x(\n"
8233             "multiline raw string literal xxxxxxxxxxxxxx\n"
8234             ")x\" +\n"
8235             "    bbbbbb);",
8236             format("fffffffffff(\n"
8237                    " R\"x(\n"
8238                    "multiline raw string literal xxxxxxxxxxxxxx\n"
8239                    ")x\" + bbbbbb);",
8240                    getGoogleStyleWithColumns(20)));
8241 }
8242 
8243 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
8244   verifyFormat("string a = \"unterminated;");
8245   EXPECT_EQ("function(\"unterminated,\n"
8246             "         OtherParameter);",
8247             format("function(  \"unterminated,\n"
8248                    "    OtherParameter);"));
8249 }
8250 
8251 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
8252   FormatStyle Style = getLLVMStyle();
8253   Style.Standard = FormatStyle::LS_Cpp03;
8254   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
8255             format("#define x(_a) printf(\"foo\"_a);", Style));
8256 }
8257 
8258 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
8259 
8260 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
8261   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
8262             "             \"ddeeefff\");",
8263             format("someFunction(\"aaabbbcccdddeeefff\");",
8264                    getLLVMStyleWithColumns(25)));
8265   EXPECT_EQ("someFunction1234567890(\n"
8266             "    \"aaabbbcccdddeeefff\");",
8267             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8268                    getLLVMStyleWithColumns(26)));
8269   EXPECT_EQ("someFunction1234567890(\n"
8270             "    \"aaabbbcccdddeeeff\"\n"
8271             "    \"f\");",
8272             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8273                    getLLVMStyleWithColumns(25)));
8274   EXPECT_EQ("someFunction1234567890(\n"
8275             "    \"aaabbbcccdddeeeff\"\n"
8276             "    \"f\");",
8277             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8278                    getLLVMStyleWithColumns(24)));
8279   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8280             "             \"ddde \"\n"
8281             "             \"efff\");",
8282             format("someFunction(\"aaabbbcc ddde efff\");",
8283                    getLLVMStyleWithColumns(25)));
8284   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8285             "             \"ddeeefff\");",
8286             format("someFunction(\"aaabbbccc ddeeefff\");",
8287                    getLLVMStyleWithColumns(25)));
8288   EXPECT_EQ("someFunction1234567890(\n"
8289             "    \"aaabb \"\n"
8290             "    \"cccdddeeefff\");",
8291             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8292                    getLLVMStyleWithColumns(25)));
8293   EXPECT_EQ("#define A          \\\n"
8294             "  string s =       \\\n"
8295             "      \"123456789\"  \\\n"
8296             "      \"0\";         \\\n"
8297             "  int i;",
8298             format("#define A string s = \"1234567890\"; int i;",
8299                    getLLVMStyleWithColumns(20)));
8300   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8301   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8302             "             \"dddeeeff\"\n"
8303             "             \"f\");",
8304             format("someFunction(\"aaabbbcc dddeeefff\");",
8305                    getLLVMStyleWithColumns(25)));
8306 }
8307 
8308 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8309   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8310   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8311   EXPECT_EQ("\"test\"\n"
8312             "\"\\n\"",
8313             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8314   EXPECT_EQ("\"tes\\\\\"\n"
8315             "\"n\"",
8316             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8317   EXPECT_EQ("\"\\\\\\\\\"\n"
8318             "\"\\n\"",
8319             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8320   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8321   EXPECT_EQ("\"\\uff01\"\n"
8322             "\"test\"",
8323             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8324   EXPECT_EQ("\"\\Uff01ff02\"",
8325             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8326   EXPECT_EQ("\"\\x000000000001\"\n"
8327             "\"next\"",
8328             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8329   EXPECT_EQ("\"\\x000000000001next\"",
8330             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8331   EXPECT_EQ("\"\\x000000000001\"",
8332             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8333   EXPECT_EQ("\"test\"\n"
8334             "\"\\000000\"\n"
8335             "\"000001\"",
8336             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8337   EXPECT_EQ("\"test\\000\"\n"
8338             "\"00000000\"\n"
8339             "\"1\"",
8340             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8341 }
8342 
8343 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8344   verifyFormat("void f() {\n"
8345                "  return g() {}\n"
8346                "  void h() {}");
8347   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8348                "g();\n"
8349                "}");
8350 }
8351 
8352 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8353   verifyFormat(
8354       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8355 }
8356 
8357 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8358   verifyFormat("class X {\n"
8359                "  void f() {\n"
8360                "  }\n"
8361                "};",
8362                getLLVMStyleWithColumns(12));
8363 }
8364 
8365 TEST_F(FormatTest, ConfigurableIndentWidth) {
8366   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8367   EightIndent.IndentWidth = 8;
8368   EightIndent.ContinuationIndentWidth = 8;
8369   verifyFormat("void f() {\n"
8370                "        someFunction();\n"
8371                "        if (true) {\n"
8372                "                f();\n"
8373                "        }\n"
8374                "}",
8375                EightIndent);
8376   verifyFormat("class X {\n"
8377                "        void f() {\n"
8378                "        }\n"
8379                "};",
8380                EightIndent);
8381   verifyFormat("int x[] = {\n"
8382                "        call(),\n"
8383                "        call()};",
8384                EightIndent);
8385 }
8386 
8387 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8388   verifyFormat("double\n"
8389                "f();",
8390                getLLVMStyleWithColumns(8));
8391 }
8392 
8393 TEST_F(FormatTest, ConfigurableUseOfTab) {
8394   FormatStyle Tab = getLLVMStyleWithColumns(42);
8395   Tab.IndentWidth = 8;
8396   Tab.UseTab = FormatStyle::UT_Always;
8397   Tab.AlignEscapedNewlinesLeft = true;
8398 
8399   EXPECT_EQ("if (aaaaaaaa && // q\n"
8400             "    bb)\t\t// w\n"
8401             "\t;",
8402             format("if (aaaaaaaa &&// q\n"
8403                    "bb)// w\n"
8404                    ";",
8405                    Tab));
8406   EXPECT_EQ("if (aaa && bbb) // w\n"
8407             "\t;",
8408             format("if(aaa&&bbb)// w\n"
8409                    ";",
8410                    Tab));
8411 
8412   verifyFormat("class X {\n"
8413                "\tvoid f() {\n"
8414                "\t\tsomeFunction(parameter1,\n"
8415                "\t\t\t     parameter2);\n"
8416                "\t}\n"
8417                "};",
8418                Tab);
8419   verifyFormat("#define A                        \\\n"
8420                "\tvoid f() {               \\\n"
8421                "\t\tsomeFunction(    \\\n"
8422                "\t\t    parameter1,  \\\n"
8423                "\t\t    parameter2); \\\n"
8424                "\t}",
8425                Tab);
8426 
8427   Tab.TabWidth = 4;
8428   Tab.IndentWidth = 8;
8429   verifyFormat("class TabWidth4Indent8 {\n"
8430                "\t\tvoid f() {\n"
8431                "\t\t\t\tsomeFunction(parameter1,\n"
8432                "\t\t\t\t\t\t\t parameter2);\n"
8433                "\t\t}\n"
8434                "};",
8435                Tab);
8436 
8437   Tab.TabWidth = 4;
8438   Tab.IndentWidth = 4;
8439   verifyFormat("class TabWidth4Indent4 {\n"
8440                "\tvoid f() {\n"
8441                "\t\tsomeFunction(parameter1,\n"
8442                "\t\t\t\t\t parameter2);\n"
8443                "\t}\n"
8444                "};",
8445                Tab);
8446 
8447   Tab.TabWidth = 8;
8448   Tab.IndentWidth = 4;
8449   verifyFormat("class TabWidth8Indent4 {\n"
8450                "    void f() {\n"
8451                "\tsomeFunction(parameter1,\n"
8452                "\t\t     parameter2);\n"
8453                "    }\n"
8454                "};",
8455                Tab);
8456 
8457   Tab.TabWidth = 8;
8458   Tab.IndentWidth = 8;
8459   EXPECT_EQ("/*\n"
8460             "\t      a\t\tcomment\n"
8461             "\t      in multiple lines\n"
8462             "       */",
8463             format("   /*\t \t \n"
8464                    " \t \t a\t\tcomment\t \t\n"
8465                    " \t \t in multiple lines\t\n"
8466                    " \t  */",
8467                    Tab));
8468 
8469   Tab.UseTab = FormatStyle::UT_ForIndentation;
8470   verifyFormat("{\n"
8471                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8472                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8473                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8474                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8475                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8476                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8477                "};",
8478                Tab);
8479   verifyFormat("enum AA {\n"
8480                "\ta1, // Force multiple lines\n"
8481                "\ta2,\n"
8482                "\ta3\n"
8483                "};",
8484                Tab);
8485   EXPECT_EQ("if (aaaaaaaa && // q\n"
8486             "    bb)         // w\n"
8487             "\t;",
8488             format("if (aaaaaaaa &&// q\n"
8489                    "bb)// w\n"
8490                    ";",
8491                    Tab));
8492   verifyFormat("class X {\n"
8493                "\tvoid f() {\n"
8494                "\t\tsomeFunction(parameter1,\n"
8495                "\t\t             parameter2);\n"
8496                "\t}\n"
8497                "};",
8498                Tab);
8499   verifyFormat("{\n"
8500                "\tQ(\n"
8501                "\t    {\n"
8502                "\t\t    int a;\n"
8503                "\t\t    someFunction(aaaaaaaa,\n"
8504                "\t\t                 bbbbbbb);\n"
8505                "\t    },\n"
8506                "\t    p);\n"
8507                "}",
8508                Tab);
8509   EXPECT_EQ("{\n"
8510             "\t/* aaaa\n"
8511             "\t   bbbb */\n"
8512             "}",
8513             format("{\n"
8514                    "/* aaaa\n"
8515                    "   bbbb */\n"
8516                    "}",
8517                    Tab));
8518   EXPECT_EQ("{\n"
8519             "\t/*\n"
8520             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8521             "\t  bbbbbbbbbbbbb\n"
8522             "\t*/\n"
8523             "}",
8524             format("{\n"
8525                    "/*\n"
8526                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8527                    "*/\n"
8528                    "}",
8529                    Tab));
8530   EXPECT_EQ("{\n"
8531             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8532             "\t// bbbbbbbbbbbbb\n"
8533             "}",
8534             format("{\n"
8535                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8536                    "}",
8537                    Tab));
8538   EXPECT_EQ("{\n"
8539             "\t/*\n"
8540             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8541             "\t  bbbbbbbbbbbbb\n"
8542             "\t*/\n"
8543             "}",
8544             format("{\n"
8545                    "\t/*\n"
8546                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8547                    "\t*/\n"
8548                    "}",
8549                    Tab));
8550   EXPECT_EQ("{\n"
8551             "\t/*\n"
8552             "\n"
8553             "\t*/\n"
8554             "}",
8555             format("{\n"
8556                    "\t/*\n"
8557                    "\n"
8558                    "\t*/\n"
8559                    "}",
8560                    Tab));
8561   EXPECT_EQ("{\n"
8562             "\t/*\n"
8563             " asdf\n"
8564             "\t*/\n"
8565             "}",
8566             format("{\n"
8567                    "\t/*\n"
8568                    " asdf\n"
8569                    "\t*/\n"
8570                    "}",
8571                    Tab));
8572 
8573   Tab.UseTab = FormatStyle::UT_Never;
8574   EXPECT_EQ("/*\n"
8575             "              a\t\tcomment\n"
8576             "              in multiple lines\n"
8577             "       */",
8578             format("   /*\t \t \n"
8579                    " \t \t a\t\tcomment\t \t\n"
8580                    " \t \t in multiple lines\t\n"
8581                    " \t  */",
8582                    Tab));
8583   EXPECT_EQ("/* some\n"
8584             "   comment */",
8585             format(" \t \t /* some\n"
8586                    " \t \t    comment */",
8587                    Tab));
8588   EXPECT_EQ("int a; /* some\n"
8589             "   comment */",
8590             format(" \t \t int a; /* some\n"
8591                    " \t \t    comment */",
8592                    Tab));
8593 
8594   EXPECT_EQ("int a; /* some\n"
8595             "comment */",
8596             format(" \t \t int\ta; /* some\n"
8597                    " \t \t    comment */",
8598                    Tab));
8599   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8600             "    comment */",
8601             format(" \t \t f(\"\t\t\"); /* some\n"
8602                    " \t \t    comment */",
8603                    Tab));
8604   EXPECT_EQ("{\n"
8605             "  /*\n"
8606             "   * Comment\n"
8607             "   */\n"
8608             "  int i;\n"
8609             "}",
8610             format("{\n"
8611                    "\t/*\n"
8612                    "\t * Comment\n"
8613                    "\t */\n"
8614                    "\t int i;\n"
8615                    "}"));
8616 
8617   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8618   Tab.TabWidth = 8;
8619   Tab.IndentWidth = 8;
8620   EXPECT_EQ("if (aaaaaaaa && // q\n"
8621             "    bb)         // w\n"
8622             "\t;",
8623             format("if (aaaaaaaa &&// q\n"
8624                    "bb)// w\n"
8625                    ";",
8626                    Tab));
8627   EXPECT_EQ("if (aaa && bbb) // w\n"
8628             "\t;",
8629             format("if(aaa&&bbb)// w\n"
8630                    ";",
8631                    Tab));
8632   verifyFormat("class X {\n"
8633                "\tvoid f() {\n"
8634                "\t\tsomeFunction(parameter1,\n"
8635                "\t\t\t     parameter2);\n"
8636                "\t}\n"
8637                "};",
8638                Tab);
8639   verifyFormat("#define A                        \\\n"
8640                "\tvoid f() {               \\\n"
8641                "\t\tsomeFunction(    \\\n"
8642                "\t\t    parameter1,  \\\n"
8643                "\t\t    parameter2); \\\n"
8644                "\t}",
8645                Tab);
8646   Tab.TabWidth = 4;
8647   Tab.IndentWidth = 8;
8648   verifyFormat("class TabWidth4Indent8 {\n"
8649                "\t\tvoid f() {\n"
8650                "\t\t\t\tsomeFunction(parameter1,\n"
8651                "\t\t\t\t\t\t\t parameter2);\n"
8652                "\t\t}\n"
8653                "};",
8654                Tab);
8655   Tab.TabWidth = 4;
8656   Tab.IndentWidth = 4;
8657   verifyFormat("class TabWidth4Indent4 {\n"
8658                "\tvoid f() {\n"
8659                "\t\tsomeFunction(parameter1,\n"
8660                "\t\t\t\t\t parameter2);\n"
8661                "\t}\n"
8662                "};",
8663                Tab);
8664   Tab.TabWidth = 8;
8665   Tab.IndentWidth = 4;
8666   verifyFormat("class TabWidth8Indent4 {\n"
8667                "    void f() {\n"
8668                "\tsomeFunction(parameter1,\n"
8669                "\t\t     parameter2);\n"
8670                "    }\n"
8671                "};",
8672                Tab);
8673   Tab.TabWidth = 8;
8674   Tab.IndentWidth = 8;
8675   EXPECT_EQ("/*\n"
8676             "\t      a\t\tcomment\n"
8677             "\t      in multiple lines\n"
8678             "       */",
8679             format("   /*\t \t \n"
8680                    " \t \t a\t\tcomment\t \t\n"
8681                    " \t \t in multiple lines\t\n"
8682                    " \t  */",
8683                    Tab));
8684   verifyFormat("{\n"
8685                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8686                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8687                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8688                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8689                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8690                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8691                "};",
8692                Tab);
8693   verifyFormat("enum AA {\n"
8694                "\ta1, // Force multiple lines\n"
8695                "\ta2,\n"
8696                "\ta3\n"
8697                "};",
8698                Tab);
8699   EXPECT_EQ("if (aaaaaaaa && // q\n"
8700             "    bb)         // w\n"
8701             "\t;",
8702             format("if (aaaaaaaa &&// q\n"
8703                    "bb)// w\n"
8704                    ";",
8705                    Tab));
8706   verifyFormat("class X {\n"
8707                "\tvoid f() {\n"
8708                "\t\tsomeFunction(parameter1,\n"
8709                "\t\t\t     parameter2);\n"
8710                "\t}\n"
8711                "};",
8712                Tab);
8713   verifyFormat("{\n"
8714                "\tQ(\n"
8715                "\t    {\n"
8716                "\t\t    int a;\n"
8717                "\t\t    someFunction(aaaaaaaa,\n"
8718                "\t\t\t\t bbbbbbb);\n"
8719                "\t    },\n"
8720                "\t    p);\n"
8721                "}",
8722                Tab);
8723   EXPECT_EQ("{\n"
8724             "\t/* aaaa\n"
8725             "\t   bbbb */\n"
8726             "}",
8727             format("{\n"
8728                    "/* aaaa\n"
8729                    "   bbbb */\n"
8730                    "}",
8731                    Tab));
8732   EXPECT_EQ("{\n"
8733             "\t/*\n"
8734             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8735             "\t  bbbbbbbbbbbbb\n"
8736             "\t*/\n"
8737             "}",
8738             format("{\n"
8739                    "/*\n"
8740                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8741                    "*/\n"
8742                    "}",
8743                    Tab));
8744   EXPECT_EQ("{\n"
8745             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8746             "\t// bbbbbbbbbbbbb\n"
8747             "}",
8748             format("{\n"
8749                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8750                    "}",
8751                    Tab));
8752   EXPECT_EQ("{\n"
8753             "\t/*\n"
8754             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8755             "\t  bbbbbbbbbbbbb\n"
8756             "\t*/\n"
8757             "}",
8758             format("{\n"
8759                    "\t/*\n"
8760                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8761                    "\t*/\n"
8762                    "}",
8763                    Tab));
8764   EXPECT_EQ("{\n"
8765             "\t/*\n"
8766             "\n"
8767             "\t*/\n"
8768             "}",
8769             format("{\n"
8770                    "\t/*\n"
8771                    "\n"
8772                    "\t*/\n"
8773                    "}",
8774                    Tab));
8775   EXPECT_EQ("{\n"
8776             "\t/*\n"
8777             " asdf\n"
8778             "\t*/\n"
8779             "}",
8780             format("{\n"
8781                    "\t/*\n"
8782                    " asdf\n"
8783                    "\t*/\n"
8784                    "}",
8785                    Tab));
8786   EXPECT_EQ("/*\n"
8787             "\t      a\t\tcomment\n"
8788             "\t      in multiple lines\n"
8789             "       */",
8790             format("   /*\t \t \n"
8791                    " \t \t a\t\tcomment\t \t\n"
8792                    " \t \t in multiple lines\t\n"
8793                    " \t  */",
8794                    Tab));
8795   EXPECT_EQ("/* some\n"
8796             "   comment */",
8797             format(" \t \t /* some\n"
8798                    " \t \t    comment */",
8799                    Tab));
8800   EXPECT_EQ("int a; /* some\n"
8801             "   comment */",
8802             format(" \t \t int a; /* some\n"
8803                    " \t \t    comment */",
8804                    Tab));
8805   EXPECT_EQ("int a; /* some\n"
8806             "comment */",
8807             format(" \t \t int\ta; /* some\n"
8808                    " \t \t    comment */",
8809                    Tab));
8810   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8811             "    comment */",
8812             format(" \t \t f(\"\t\t\"); /* some\n"
8813                    " \t \t    comment */",
8814                    Tab));
8815   EXPECT_EQ("{\n"
8816             "  /*\n"
8817             "   * Comment\n"
8818             "   */\n"
8819             "  int i;\n"
8820             "}",
8821             format("{\n"
8822                    "\t/*\n"
8823                    "\t * Comment\n"
8824                    "\t */\n"
8825                    "\t int i;\n"
8826                    "}"));
8827   Tab.AlignConsecutiveAssignments = true;
8828   Tab.AlignConsecutiveDeclarations = true;
8829   Tab.TabWidth = 4;
8830   Tab.IndentWidth = 4;
8831   verifyFormat("class Assign {\n"
8832                "\tvoid f() {\n"
8833                "\t\tint         x      = 123;\n"
8834                "\t\tint         random = 4;\n"
8835                "\t\tstd::string alphabet =\n"
8836                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8837                "\t}\n"
8838                "};",
8839                Tab);
8840 }
8841 
8842 TEST_F(FormatTest, CalculatesOriginalColumn) {
8843   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8844             "q\"; /* some\n"
8845             "       comment */",
8846             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8847                    "q\"; /* some\n"
8848                    "       comment */",
8849                    getLLVMStyle()));
8850   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8851             "/* some\n"
8852             "   comment */",
8853             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8854                    " /* some\n"
8855                    "    comment */",
8856                    getLLVMStyle()));
8857   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8858             "qqq\n"
8859             "/* some\n"
8860             "   comment */",
8861             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8862                    "qqq\n"
8863                    " /* some\n"
8864                    "    comment */",
8865                    getLLVMStyle()));
8866   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8867             "wwww; /* some\n"
8868             "         comment */",
8869             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8870                    "wwww; /* some\n"
8871                    "         comment */",
8872                    getLLVMStyle()));
8873 }
8874 
8875 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8876   FormatStyle NoSpace = getLLVMStyle();
8877   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8878 
8879   verifyFormat("while(true)\n"
8880                "  continue;",
8881                NoSpace);
8882   verifyFormat("for(;;)\n"
8883                "  continue;",
8884                NoSpace);
8885   verifyFormat("if(true)\n"
8886                "  f();\n"
8887                "else if(true)\n"
8888                "  f();",
8889                NoSpace);
8890   verifyFormat("do {\n"
8891                "  do_something();\n"
8892                "} while(something());",
8893                NoSpace);
8894   verifyFormat("switch(x) {\n"
8895                "default:\n"
8896                "  break;\n"
8897                "}",
8898                NoSpace);
8899   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8900   verifyFormat("size_t x = sizeof(x);", NoSpace);
8901   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8902   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8903   verifyFormat("alignas(128) char a[128];", NoSpace);
8904   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8905   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8906   verifyFormat("int f() throw(Deprecated);", NoSpace);
8907   verifyFormat("typedef void (*cb)(int);", NoSpace);
8908   verifyFormat("T A::operator()();", NoSpace);
8909   verifyFormat("X A::operator++(T);", NoSpace);
8910 
8911   FormatStyle Space = getLLVMStyle();
8912   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8913 
8914   verifyFormat("int f ();", Space);
8915   verifyFormat("void f (int a, T b) {\n"
8916                "  while (true)\n"
8917                "    continue;\n"
8918                "}",
8919                Space);
8920   verifyFormat("if (true)\n"
8921                "  f ();\n"
8922                "else if (true)\n"
8923                "  f ();",
8924                Space);
8925   verifyFormat("do {\n"
8926                "  do_something ();\n"
8927                "} while (something ());",
8928                Space);
8929   verifyFormat("switch (x) {\n"
8930                "default:\n"
8931                "  break;\n"
8932                "}",
8933                Space);
8934   verifyFormat("A::A () : a (1) {}", Space);
8935   verifyFormat("void f () __attribute__ ((asdf));", Space);
8936   verifyFormat("*(&a + 1);\n"
8937                "&((&a)[1]);\n"
8938                "a[(b + c) * d];\n"
8939                "(((a + 1) * 2) + 3) * 4;",
8940                Space);
8941   verifyFormat("#define A(x) x", Space);
8942   verifyFormat("#define A (x) x", Space);
8943   verifyFormat("#if defined(x)\n"
8944                "#endif",
8945                Space);
8946   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8947   verifyFormat("size_t x = sizeof (x);", Space);
8948   verifyFormat("auto f (int x) -> decltype (x);", Space);
8949   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8950   verifyFormat("alignas (128) char a[128];", Space);
8951   verifyFormat("size_t x = alignof (MyType);", Space);
8952   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8953   verifyFormat("int f () throw (Deprecated);", Space);
8954   verifyFormat("typedef void (*cb) (int);", Space);
8955   verifyFormat("T A::operator() ();", Space);
8956   verifyFormat("X A::operator++ (T);", Space);
8957 }
8958 
8959 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8960   FormatStyle Spaces = getLLVMStyle();
8961 
8962   Spaces.SpacesInParentheses = true;
8963   verifyFormat("call( x, y, z );", Spaces);
8964   verifyFormat("call();", Spaces);
8965   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8966   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8967                Spaces);
8968   verifyFormat("while ( (bool)1 )\n"
8969                "  continue;",
8970                Spaces);
8971   verifyFormat("for ( ;; )\n"
8972                "  continue;",
8973                Spaces);
8974   verifyFormat("if ( true )\n"
8975                "  f();\n"
8976                "else if ( true )\n"
8977                "  f();",
8978                Spaces);
8979   verifyFormat("do {\n"
8980                "  do_something( (int)i );\n"
8981                "} while ( something() );",
8982                Spaces);
8983   verifyFormat("switch ( x ) {\n"
8984                "default:\n"
8985                "  break;\n"
8986                "}",
8987                Spaces);
8988 
8989   Spaces.SpacesInParentheses = false;
8990   Spaces.SpacesInCStyleCastParentheses = true;
8991   verifyFormat("Type *A = ( Type * )P;", Spaces);
8992   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8993   verifyFormat("x = ( int32 )y;", Spaces);
8994   verifyFormat("int a = ( int )(2.0f);", Spaces);
8995   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8996   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8997   verifyFormat("#define x (( int )-1)", Spaces);
8998 
8999   // Run the first set of tests again with:
9000   Spaces.SpacesInParentheses = false;
9001   Spaces.SpaceInEmptyParentheses = true;
9002   Spaces.SpacesInCStyleCastParentheses = true;
9003   verifyFormat("call(x, y, z);", Spaces);
9004   verifyFormat("call( );", Spaces);
9005   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9006   verifyFormat("while (( bool )1)\n"
9007                "  continue;",
9008                Spaces);
9009   verifyFormat("for (;;)\n"
9010                "  continue;",
9011                Spaces);
9012   verifyFormat("if (true)\n"
9013                "  f( );\n"
9014                "else if (true)\n"
9015                "  f( );",
9016                Spaces);
9017   verifyFormat("do {\n"
9018                "  do_something(( int )i);\n"
9019                "} while (something( ));",
9020                Spaces);
9021   verifyFormat("switch (x) {\n"
9022                "default:\n"
9023                "  break;\n"
9024                "}",
9025                Spaces);
9026 
9027   // Run the first set of tests again with:
9028   Spaces.SpaceAfterCStyleCast = true;
9029   verifyFormat("call(x, y, z);", Spaces);
9030   verifyFormat("call( );", Spaces);
9031   verifyFormat("std::function<void(int, int)> callback;", Spaces);
9032   verifyFormat("while (( bool ) 1)\n"
9033                "  continue;",
9034                Spaces);
9035   verifyFormat("for (;;)\n"
9036                "  continue;",
9037                Spaces);
9038   verifyFormat("if (true)\n"
9039                "  f( );\n"
9040                "else if (true)\n"
9041                "  f( );",
9042                Spaces);
9043   verifyFormat("do {\n"
9044                "  do_something(( int ) i);\n"
9045                "} while (something( ));",
9046                Spaces);
9047   verifyFormat("switch (x) {\n"
9048                "default:\n"
9049                "  break;\n"
9050                "}",
9051                Spaces);
9052 
9053   // Run subset of tests again with:
9054   Spaces.SpacesInCStyleCastParentheses = false;
9055   Spaces.SpaceAfterCStyleCast = true;
9056   verifyFormat("while ((bool) 1)\n"
9057                "  continue;",
9058                Spaces);
9059   verifyFormat("do {\n"
9060                "  do_something((int) i);\n"
9061                "} while (something( ));",
9062                Spaces);
9063 }
9064 
9065 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
9066   verifyFormat("int a[5];");
9067   verifyFormat("a[3] += 42;");
9068 
9069   FormatStyle Spaces = getLLVMStyle();
9070   Spaces.SpacesInSquareBrackets = true;
9071   // Lambdas unchanged.
9072   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
9073   verifyFormat("return [i, args...] {};", Spaces);
9074 
9075   // Not lambdas.
9076   verifyFormat("int a[ 5 ];", Spaces);
9077   verifyFormat("a[ 3 ] += 42;", Spaces);
9078   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
9079   verifyFormat("double &operator[](int i) { return 0; }\n"
9080                "int i;",
9081                Spaces);
9082   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
9083   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
9084   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
9085 }
9086 
9087 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
9088   verifyFormat("int a = 5;");
9089   verifyFormat("a += 42;");
9090   verifyFormat("a or_eq 8;");
9091 
9092   FormatStyle Spaces = getLLVMStyle();
9093   Spaces.SpaceBeforeAssignmentOperators = false;
9094   verifyFormat("int a= 5;", Spaces);
9095   verifyFormat("a+= 42;", Spaces);
9096   verifyFormat("a or_eq 8;", Spaces);
9097 }
9098 
9099 TEST_F(FormatTest, AlignConsecutiveAssignments) {
9100   FormatStyle Alignment = getLLVMStyle();
9101   Alignment.AlignConsecutiveAssignments = false;
9102   verifyFormat("int a = 5;\n"
9103                "int oneTwoThree = 123;",
9104                Alignment);
9105   verifyFormat("int a = 5;\n"
9106                "int oneTwoThree = 123;",
9107                Alignment);
9108 
9109   Alignment.AlignConsecutiveAssignments = true;
9110   verifyFormat("int a           = 5;\n"
9111                "int oneTwoThree = 123;",
9112                Alignment);
9113   verifyFormat("int a           = method();\n"
9114                "int oneTwoThree = 133;",
9115                Alignment);
9116   verifyFormat("a &= 5;\n"
9117                "bcd *= 5;\n"
9118                "ghtyf += 5;\n"
9119                "dvfvdb -= 5;\n"
9120                "a /= 5;\n"
9121                "vdsvsv %= 5;\n"
9122                "sfdbddfbdfbb ^= 5;\n"
9123                "dvsdsv |= 5;\n"
9124                "int dsvvdvsdvvv = 123;",
9125                Alignment);
9126   verifyFormat("int i = 1, j = 10;\n"
9127                "something = 2000;",
9128                Alignment);
9129   verifyFormat("something = 2000;\n"
9130                "int i = 1, j = 10;\n",
9131                Alignment);
9132   verifyFormat("something = 2000;\n"
9133                "another   = 911;\n"
9134                "int i = 1, j = 10;\n"
9135                "oneMore = 1;\n"
9136                "i       = 2;",
9137                Alignment);
9138   verifyFormat("int a   = 5;\n"
9139                "int one = 1;\n"
9140                "method();\n"
9141                "int oneTwoThree = 123;\n"
9142                "int oneTwo      = 12;",
9143                Alignment);
9144   verifyFormat("int oneTwoThree = 123;\n"
9145                "int oneTwo      = 12;\n"
9146                "method();\n",
9147                Alignment);
9148   verifyFormat("int oneTwoThree = 123; // comment\n"
9149                "int oneTwo      = 12;  // comment",
9150                Alignment);
9151   EXPECT_EQ("int a = 5;\n"
9152             "\n"
9153             "int oneTwoThree = 123;",
9154             format("int a       = 5;\n"
9155                    "\n"
9156                    "int oneTwoThree= 123;",
9157                    Alignment));
9158   EXPECT_EQ("int a   = 5;\n"
9159             "int one = 1;\n"
9160             "\n"
9161             "int oneTwoThree = 123;",
9162             format("int a = 5;\n"
9163                    "int one = 1;\n"
9164                    "\n"
9165                    "int oneTwoThree = 123;",
9166                    Alignment));
9167   EXPECT_EQ("int a   = 5;\n"
9168             "int one = 1;\n"
9169             "\n"
9170             "int oneTwoThree = 123;\n"
9171             "int oneTwo      = 12;",
9172             format("int a = 5;\n"
9173                    "int one = 1;\n"
9174                    "\n"
9175                    "int oneTwoThree = 123;\n"
9176                    "int oneTwo = 12;",
9177                    Alignment));
9178   Alignment.AlignEscapedNewlinesLeft = true;
9179   verifyFormat("#define A               \\\n"
9180                "  int aaaa       = 12;  \\\n"
9181                "  int b          = 23;  \\\n"
9182                "  int ccc        = 234; \\\n"
9183                "  int dddddddddd = 2345;",
9184                Alignment);
9185   Alignment.AlignEscapedNewlinesLeft = false;
9186   verifyFormat("#define A                                                      "
9187                "                \\\n"
9188                "  int aaaa       = 12;                                         "
9189                "                \\\n"
9190                "  int b          = 23;                                         "
9191                "                \\\n"
9192                "  int ccc        = 234;                                        "
9193                "                \\\n"
9194                "  int dddddddddd = 2345;",
9195                Alignment);
9196   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9197                "k = 4, int l = 5,\n"
9198                "                  int m = 6) {\n"
9199                "  int j      = 10;\n"
9200                "  otherThing = 1;\n"
9201                "}",
9202                Alignment);
9203   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9204                "  int i   = 1;\n"
9205                "  int j   = 2;\n"
9206                "  int big = 10000;\n"
9207                "}",
9208                Alignment);
9209   verifyFormat("class C {\n"
9210                "public:\n"
9211                "  int i            = 1;\n"
9212                "  virtual void f() = 0;\n"
9213                "};",
9214                Alignment);
9215   verifyFormat("int i = 1;\n"
9216                "if (SomeType t = getSomething()) {\n"
9217                "}\n"
9218                "int j   = 2;\n"
9219                "int big = 10000;",
9220                Alignment);
9221   verifyFormat("int j = 7;\n"
9222                "for (int k = 0; k < N; ++k) {\n"
9223                "}\n"
9224                "int j   = 2;\n"
9225                "int big = 10000;\n"
9226                "}",
9227                Alignment);
9228   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9229   verifyFormat("int i = 1;\n"
9230                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9231                "    = someLooooooooooooooooongFunction();\n"
9232                "int j = 2;",
9233                Alignment);
9234   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9235   verifyFormat("int i = 1;\n"
9236                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9237                "    someLooooooooooooooooongFunction();\n"
9238                "int j = 2;",
9239                Alignment);
9240 
9241   verifyFormat("auto lambda = []() {\n"
9242                "  auto i = 0;\n"
9243                "  return 0;\n"
9244                "};\n"
9245                "int i  = 0;\n"
9246                "auto v = type{\n"
9247                "    i = 1,   //\n"
9248                "    (i = 2), //\n"
9249                "    i = 3    //\n"
9250                "};",
9251                Alignment);
9252 
9253   // FIXME: Should align all three assignments
9254   verifyFormat(
9255       "int i      = 1;\n"
9256       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9257       "                          loooooooooooooooooooooongParameterB);\n"
9258       "int j = 2;",
9259       Alignment);
9260 
9261   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
9262                "          typename B   = very_long_type_name_1,\n"
9263                "          typename T_2 = very_long_type_name_2>\n"
9264                "auto foo() {}\n",
9265                Alignment);
9266   verifyFormat("int a, b = 1;\n"
9267                "int c  = 2;\n"
9268                "int dd = 3;\n",
9269                Alignment);
9270   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9271                "float b[1][] = {{3.f}};\n",
9272                Alignment);
9273 }
9274 
9275 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9276   FormatStyle Alignment = getLLVMStyle();
9277   Alignment.AlignConsecutiveDeclarations = false;
9278   verifyFormat("float const a = 5;\n"
9279                "int oneTwoThree = 123;",
9280                Alignment);
9281   verifyFormat("int a = 5;\n"
9282                "float const oneTwoThree = 123;",
9283                Alignment);
9284 
9285   Alignment.AlignConsecutiveDeclarations = true;
9286   verifyFormat("float const a = 5;\n"
9287                "int         oneTwoThree = 123;",
9288                Alignment);
9289   verifyFormat("int         a = method();\n"
9290                "float const oneTwoThree = 133;",
9291                Alignment);
9292   verifyFormat("int i = 1, j = 10;\n"
9293                "something = 2000;",
9294                Alignment);
9295   verifyFormat("something = 2000;\n"
9296                "int i = 1, j = 10;\n",
9297                Alignment);
9298   verifyFormat("float      something = 2000;\n"
9299                "double     another = 911;\n"
9300                "int        i = 1, j = 10;\n"
9301                "const int *oneMore = 1;\n"
9302                "unsigned   i = 2;",
9303                Alignment);
9304   verifyFormat("float a = 5;\n"
9305                "int   one = 1;\n"
9306                "method();\n"
9307                "const double       oneTwoThree = 123;\n"
9308                "const unsigned int oneTwo = 12;",
9309                Alignment);
9310   verifyFormat("int      oneTwoThree{0}; // comment\n"
9311                "unsigned oneTwo;         // comment",
9312                Alignment);
9313   EXPECT_EQ("float const a = 5;\n"
9314             "\n"
9315             "int oneTwoThree = 123;",
9316             format("float const   a = 5;\n"
9317                    "\n"
9318                    "int           oneTwoThree= 123;",
9319                    Alignment));
9320   EXPECT_EQ("float a = 5;\n"
9321             "int   one = 1;\n"
9322             "\n"
9323             "unsigned oneTwoThree = 123;",
9324             format("float    a = 5;\n"
9325                    "int      one = 1;\n"
9326                    "\n"
9327                    "unsigned oneTwoThree = 123;",
9328                    Alignment));
9329   EXPECT_EQ("float a = 5;\n"
9330             "int   one = 1;\n"
9331             "\n"
9332             "unsigned oneTwoThree = 123;\n"
9333             "int      oneTwo = 12;",
9334             format("float    a = 5;\n"
9335                    "int one = 1;\n"
9336                    "\n"
9337                    "unsigned oneTwoThree = 123;\n"
9338                    "int oneTwo = 12;",
9339                    Alignment));
9340   Alignment.AlignConsecutiveAssignments = true;
9341   verifyFormat("float      something = 2000;\n"
9342                "double     another   = 911;\n"
9343                "int        i = 1, j = 10;\n"
9344                "const int *oneMore = 1;\n"
9345                "unsigned   i       = 2;",
9346                Alignment);
9347   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9348                "unsigned oneTwo      = 0;   // comment",
9349                Alignment);
9350   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9351             "  int const i   = 1;\n"
9352             "  int *     j   = 2;\n"
9353             "  int       big = 10000;\n"
9354             "\n"
9355             "  unsigned oneTwoThree = 123;\n"
9356             "  int      oneTwo      = 12;\n"
9357             "  method();\n"
9358             "  float k  = 2;\n"
9359             "  int   ll = 10000;\n"
9360             "}",
9361             format("void SomeFunction(int parameter= 0) {\n"
9362                    " int const  i= 1;\n"
9363                    "  int *j=2;\n"
9364                    " int big  =  10000;\n"
9365                    "\n"
9366                    "unsigned oneTwoThree  =123;\n"
9367                    "int oneTwo = 12;\n"
9368                    "  method();\n"
9369                    "float k= 2;\n"
9370                    "int ll=10000;\n"
9371                    "}",
9372                    Alignment));
9373   Alignment.AlignConsecutiveAssignments = false;
9374   Alignment.AlignEscapedNewlinesLeft = true;
9375   verifyFormat("#define A              \\\n"
9376                "  int       aaaa = 12; \\\n"
9377                "  float     b = 23;    \\\n"
9378                "  const int ccc = 234; \\\n"
9379                "  unsigned  dddddddddd = 2345;",
9380                Alignment);
9381   Alignment.AlignEscapedNewlinesLeft = false;
9382   Alignment.ColumnLimit = 30;
9383   verifyFormat("#define A                    \\\n"
9384                "  int       aaaa = 12;       \\\n"
9385                "  float     b = 23;          \\\n"
9386                "  const int ccc = 234;       \\\n"
9387                "  int       dddddddddd = 2345;",
9388                Alignment);
9389   Alignment.ColumnLimit = 80;
9390   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9391                "k = 4, int l = 5,\n"
9392                "                  int m = 6) {\n"
9393                "  const int j = 10;\n"
9394                "  otherThing = 1;\n"
9395                "}",
9396                Alignment);
9397   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9398                "  int const i = 1;\n"
9399                "  int *     j = 2;\n"
9400                "  int       big = 10000;\n"
9401                "}",
9402                Alignment);
9403   verifyFormat("class C {\n"
9404                "public:\n"
9405                "  int          i = 1;\n"
9406                "  virtual void f() = 0;\n"
9407                "};",
9408                Alignment);
9409   verifyFormat("float i = 1;\n"
9410                "if (SomeType t = getSomething()) {\n"
9411                "}\n"
9412                "const unsigned j = 2;\n"
9413                "int            big = 10000;",
9414                Alignment);
9415   verifyFormat("float j = 7;\n"
9416                "for (int k = 0; k < N; ++k) {\n"
9417                "}\n"
9418                "unsigned j = 2;\n"
9419                "int      big = 10000;\n"
9420                "}",
9421                Alignment);
9422   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9423   verifyFormat("float              i = 1;\n"
9424                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9425                "    = someLooooooooooooooooongFunction();\n"
9426                "int j = 2;",
9427                Alignment);
9428   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9429   verifyFormat("int                i = 1;\n"
9430                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9431                "    someLooooooooooooooooongFunction();\n"
9432                "int j = 2;",
9433                Alignment);
9434 
9435   Alignment.AlignConsecutiveAssignments = true;
9436   verifyFormat("auto lambda = []() {\n"
9437                "  auto  ii = 0;\n"
9438                "  float j  = 0;\n"
9439                "  return 0;\n"
9440                "};\n"
9441                "int   i  = 0;\n"
9442                "float i2 = 0;\n"
9443                "auto  v  = type{\n"
9444                "    i = 1,   //\n"
9445                "    (i = 2), //\n"
9446                "    i = 3    //\n"
9447                "};",
9448                Alignment);
9449   Alignment.AlignConsecutiveAssignments = false;
9450 
9451   // FIXME: Should align all three declarations
9452   verifyFormat(
9453       "int      i = 1;\n"
9454       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9455       "                          loooooooooooooooooooooongParameterB);\n"
9456       "int j = 2;",
9457       Alignment);
9458 
9459   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9460   // We expect declarations and assignments to align, as long as it doesn't
9461   // exceed the column limit, starting a new alignemnt sequence whenever it
9462   // happens.
9463   Alignment.AlignConsecutiveAssignments = true;
9464   Alignment.ColumnLimit = 30;
9465   verifyFormat("float    ii              = 1;\n"
9466                "unsigned j               = 2;\n"
9467                "int someVerylongVariable = 1;\n"
9468                "AnotherLongType  ll = 123456;\n"
9469                "VeryVeryLongType k  = 2;\n"
9470                "int              myvar = 1;",
9471                Alignment);
9472   Alignment.ColumnLimit = 80;
9473   Alignment.AlignConsecutiveAssignments = false;
9474 
9475   verifyFormat(
9476       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9477       "          typename LongType, typename B>\n"
9478       "auto foo() {}\n",
9479       Alignment);
9480   verifyFormat("float a, b = 1;\n"
9481                "int   c = 2;\n"
9482                "int   dd = 3;\n",
9483                Alignment);
9484   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9485                "float b[1][] = {{3.f}};\n",
9486                Alignment);
9487   Alignment.AlignConsecutiveAssignments = true;
9488   verifyFormat("float a, b = 1;\n"
9489                "int   c  = 2;\n"
9490                "int   dd = 3;\n",
9491                Alignment);
9492   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9493                "float b[1][] = {{3.f}};\n",
9494                Alignment);
9495   Alignment.AlignConsecutiveAssignments = false;
9496 
9497   Alignment.ColumnLimit = 30;
9498   Alignment.BinPackParameters = false;
9499   verifyFormat("void foo(float     a,\n"
9500                "         float     b,\n"
9501                "         int       c,\n"
9502                "         uint32_t *d) {\n"
9503                "  int *  e = 0;\n"
9504                "  float  f = 0;\n"
9505                "  double g = 0;\n"
9506                "}\n"
9507                "void bar(ino_t     a,\n"
9508                "         int       b,\n"
9509                "         uint32_t *c,\n"
9510                "         bool      d) {}\n",
9511                Alignment);
9512   Alignment.BinPackParameters = true;
9513   Alignment.ColumnLimit = 80;
9514 }
9515 
9516 TEST_F(FormatTest, LinuxBraceBreaking) {
9517   FormatStyle LinuxBraceStyle = getLLVMStyle();
9518   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9519   verifyFormat("namespace a\n"
9520                "{\n"
9521                "class A\n"
9522                "{\n"
9523                "  void f()\n"
9524                "  {\n"
9525                "    if (true) {\n"
9526                "      a();\n"
9527                "      b();\n"
9528                "    } else {\n"
9529                "      a();\n"
9530                "    }\n"
9531                "  }\n"
9532                "  void g() { return; }\n"
9533                "};\n"
9534                "struct B {\n"
9535                "  int x;\n"
9536                "};\n"
9537                "}\n",
9538                LinuxBraceStyle);
9539   verifyFormat("enum X {\n"
9540                "  Y = 0,\n"
9541                "}\n",
9542                LinuxBraceStyle);
9543   verifyFormat("struct S {\n"
9544                "  int Type;\n"
9545                "  union {\n"
9546                "    int x;\n"
9547                "    double y;\n"
9548                "  } Value;\n"
9549                "  class C\n"
9550                "  {\n"
9551                "    MyFavoriteType Value;\n"
9552                "  } Class;\n"
9553                "}\n",
9554                LinuxBraceStyle);
9555 }
9556 
9557 TEST_F(FormatTest, MozillaBraceBreaking) {
9558   FormatStyle MozillaBraceStyle = getLLVMStyle();
9559   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9560   verifyFormat("namespace a {\n"
9561                "class A\n"
9562                "{\n"
9563                "  void f()\n"
9564                "  {\n"
9565                "    if (true) {\n"
9566                "      a();\n"
9567                "      b();\n"
9568                "    }\n"
9569                "  }\n"
9570                "  void g() { return; }\n"
9571                "};\n"
9572                "enum E\n"
9573                "{\n"
9574                "  A,\n"
9575                "  // foo\n"
9576                "  B,\n"
9577                "  C\n"
9578                "};\n"
9579                "struct B\n"
9580                "{\n"
9581                "  int x;\n"
9582                "};\n"
9583                "}\n",
9584                MozillaBraceStyle);
9585   verifyFormat("struct S\n"
9586                "{\n"
9587                "  int Type;\n"
9588                "  union\n"
9589                "  {\n"
9590                "    int x;\n"
9591                "    double y;\n"
9592                "  } Value;\n"
9593                "  class C\n"
9594                "  {\n"
9595                "    MyFavoriteType Value;\n"
9596                "  } Class;\n"
9597                "}\n",
9598                MozillaBraceStyle);
9599 }
9600 
9601 TEST_F(FormatTest, StroustrupBraceBreaking) {
9602   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9603   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9604   verifyFormat("namespace a {\n"
9605                "class A {\n"
9606                "  void f()\n"
9607                "  {\n"
9608                "    if (true) {\n"
9609                "      a();\n"
9610                "      b();\n"
9611                "    }\n"
9612                "  }\n"
9613                "  void g() { return; }\n"
9614                "};\n"
9615                "struct B {\n"
9616                "  int x;\n"
9617                "};\n"
9618                "}\n",
9619                StroustrupBraceStyle);
9620 
9621   verifyFormat("void foo()\n"
9622                "{\n"
9623                "  if (a) {\n"
9624                "    a();\n"
9625                "  }\n"
9626                "  else {\n"
9627                "    b();\n"
9628                "  }\n"
9629                "}\n",
9630                StroustrupBraceStyle);
9631 
9632   verifyFormat("#ifdef _DEBUG\n"
9633                "int foo(int i = 0)\n"
9634                "#else\n"
9635                "int foo(int i = 5)\n"
9636                "#endif\n"
9637                "{\n"
9638                "  return i;\n"
9639                "}",
9640                StroustrupBraceStyle);
9641 
9642   verifyFormat("void foo() {}\n"
9643                "void bar()\n"
9644                "#ifdef _DEBUG\n"
9645                "{\n"
9646                "  foo();\n"
9647                "}\n"
9648                "#else\n"
9649                "{\n"
9650                "}\n"
9651                "#endif",
9652                StroustrupBraceStyle);
9653 
9654   verifyFormat("void foobar() { int i = 5; }\n"
9655                "#ifdef _DEBUG\n"
9656                "void bar() {}\n"
9657                "#else\n"
9658                "void bar() { foobar(); }\n"
9659                "#endif",
9660                StroustrupBraceStyle);
9661 }
9662 
9663 TEST_F(FormatTest, AllmanBraceBreaking) {
9664   FormatStyle AllmanBraceStyle = getLLVMStyle();
9665   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9666   verifyFormat("namespace a\n"
9667                "{\n"
9668                "class A\n"
9669                "{\n"
9670                "  void f()\n"
9671                "  {\n"
9672                "    if (true)\n"
9673                "    {\n"
9674                "      a();\n"
9675                "      b();\n"
9676                "    }\n"
9677                "  }\n"
9678                "  void g() { return; }\n"
9679                "};\n"
9680                "struct B\n"
9681                "{\n"
9682                "  int x;\n"
9683                "};\n"
9684                "}",
9685                AllmanBraceStyle);
9686 
9687   verifyFormat("void f()\n"
9688                "{\n"
9689                "  if (true)\n"
9690                "  {\n"
9691                "    a();\n"
9692                "  }\n"
9693                "  else if (false)\n"
9694                "  {\n"
9695                "    b();\n"
9696                "  }\n"
9697                "  else\n"
9698                "  {\n"
9699                "    c();\n"
9700                "  }\n"
9701                "}\n",
9702                AllmanBraceStyle);
9703 
9704   verifyFormat("void f()\n"
9705                "{\n"
9706                "  for (int i = 0; i < 10; ++i)\n"
9707                "  {\n"
9708                "    a();\n"
9709                "  }\n"
9710                "  while (false)\n"
9711                "  {\n"
9712                "    b();\n"
9713                "  }\n"
9714                "  do\n"
9715                "  {\n"
9716                "    c();\n"
9717                "  } while (false)\n"
9718                "}\n",
9719                AllmanBraceStyle);
9720 
9721   verifyFormat("void f(int a)\n"
9722                "{\n"
9723                "  switch (a)\n"
9724                "  {\n"
9725                "  case 0:\n"
9726                "    break;\n"
9727                "  case 1:\n"
9728                "  {\n"
9729                "    break;\n"
9730                "  }\n"
9731                "  case 2:\n"
9732                "  {\n"
9733                "  }\n"
9734                "  break;\n"
9735                "  default:\n"
9736                "    break;\n"
9737                "  }\n"
9738                "}\n",
9739                AllmanBraceStyle);
9740 
9741   verifyFormat("enum X\n"
9742                "{\n"
9743                "  Y = 0,\n"
9744                "}\n",
9745                AllmanBraceStyle);
9746   verifyFormat("enum X\n"
9747                "{\n"
9748                "  Y = 0\n"
9749                "}\n",
9750                AllmanBraceStyle);
9751 
9752   verifyFormat("@interface BSApplicationController ()\n"
9753                "{\n"
9754                "@private\n"
9755                "  id _extraIvar;\n"
9756                "}\n"
9757                "@end\n",
9758                AllmanBraceStyle);
9759 
9760   verifyFormat("#ifdef _DEBUG\n"
9761                "int foo(int i = 0)\n"
9762                "#else\n"
9763                "int foo(int i = 5)\n"
9764                "#endif\n"
9765                "{\n"
9766                "  return i;\n"
9767                "}",
9768                AllmanBraceStyle);
9769 
9770   verifyFormat("void foo() {}\n"
9771                "void bar()\n"
9772                "#ifdef _DEBUG\n"
9773                "{\n"
9774                "  foo();\n"
9775                "}\n"
9776                "#else\n"
9777                "{\n"
9778                "}\n"
9779                "#endif",
9780                AllmanBraceStyle);
9781 
9782   verifyFormat("void foobar() { int i = 5; }\n"
9783                "#ifdef _DEBUG\n"
9784                "void bar() {}\n"
9785                "#else\n"
9786                "void bar() { foobar(); }\n"
9787                "#endif",
9788                AllmanBraceStyle);
9789 
9790   // This shouldn't affect ObjC blocks..
9791   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9792                "  // ...\n"
9793                "  int i;\n"
9794                "}];",
9795                AllmanBraceStyle);
9796   verifyFormat("void (^block)(void) = ^{\n"
9797                "  // ...\n"
9798                "  int i;\n"
9799                "};",
9800                AllmanBraceStyle);
9801   // .. or dict literals.
9802   verifyFormat("void f()\n"
9803                "{\n"
9804                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
9805                "}",
9806                AllmanBraceStyle);
9807   verifyFormat("int f()\n"
9808                "{ // comment\n"
9809                "  return 42;\n"
9810                "}",
9811                AllmanBraceStyle);
9812 
9813   AllmanBraceStyle.ColumnLimit = 19;
9814   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9815   AllmanBraceStyle.ColumnLimit = 18;
9816   verifyFormat("void f()\n"
9817                "{\n"
9818                "  int i;\n"
9819                "}",
9820                AllmanBraceStyle);
9821   AllmanBraceStyle.ColumnLimit = 80;
9822 
9823   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9824   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9825   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9826   verifyFormat("void f(bool b)\n"
9827                "{\n"
9828                "  if (b)\n"
9829                "  {\n"
9830                "    return;\n"
9831                "  }\n"
9832                "}\n",
9833                BreakBeforeBraceShortIfs);
9834   verifyFormat("void f(bool b)\n"
9835                "{\n"
9836                "  if (b) return;\n"
9837                "}\n",
9838                BreakBeforeBraceShortIfs);
9839   verifyFormat("void f(bool b)\n"
9840                "{\n"
9841                "  while (b)\n"
9842                "  {\n"
9843                "    return;\n"
9844                "  }\n"
9845                "}\n",
9846                BreakBeforeBraceShortIfs);
9847 }
9848 
9849 TEST_F(FormatTest, GNUBraceBreaking) {
9850   FormatStyle GNUBraceStyle = getLLVMStyle();
9851   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9852   verifyFormat("namespace a\n"
9853                "{\n"
9854                "class A\n"
9855                "{\n"
9856                "  void f()\n"
9857                "  {\n"
9858                "    int a;\n"
9859                "    {\n"
9860                "      int b;\n"
9861                "    }\n"
9862                "    if (true)\n"
9863                "      {\n"
9864                "        a();\n"
9865                "        b();\n"
9866                "      }\n"
9867                "  }\n"
9868                "  void g() { return; }\n"
9869                "}\n"
9870                "}",
9871                GNUBraceStyle);
9872 
9873   verifyFormat("void f()\n"
9874                "{\n"
9875                "  if (true)\n"
9876                "    {\n"
9877                "      a();\n"
9878                "    }\n"
9879                "  else if (false)\n"
9880                "    {\n"
9881                "      b();\n"
9882                "    }\n"
9883                "  else\n"
9884                "    {\n"
9885                "      c();\n"
9886                "    }\n"
9887                "}\n",
9888                GNUBraceStyle);
9889 
9890   verifyFormat("void f()\n"
9891                "{\n"
9892                "  for (int i = 0; i < 10; ++i)\n"
9893                "    {\n"
9894                "      a();\n"
9895                "    }\n"
9896                "  while (false)\n"
9897                "    {\n"
9898                "      b();\n"
9899                "    }\n"
9900                "  do\n"
9901                "    {\n"
9902                "      c();\n"
9903                "    }\n"
9904                "  while (false);\n"
9905                "}\n",
9906                GNUBraceStyle);
9907 
9908   verifyFormat("void f(int a)\n"
9909                "{\n"
9910                "  switch (a)\n"
9911                "    {\n"
9912                "    case 0:\n"
9913                "      break;\n"
9914                "    case 1:\n"
9915                "      {\n"
9916                "        break;\n"
9917                "      }\n"
9918                "    case 2:\n"
9919                "      {\n"
9920                "      }\n"
9921                "      break;\n"
9922                "    default:\n"
9923                "      break;\n"
9924                "    }\n"
9925                "}\n",
9926                GNUBraceStyle);
9927 
9928   verifyFormat("enum X\n"
9929                "{\n"
9930                "  Y = 0,\n"
9931                "}\n",
9932                GNUBraceStyle);
9933 
9934   verifyFormat("@interface BSApplicationController ()\n"
9935                "{\n"
9936                "@private\n"
9937                "  id _extraIvar;\n"
9938                "}\n"
9939                "@end\n",
9940                GNUBraceStyle);
9941 
9942   verifyFormat("#ifdef _DEBUG\n"
9943                "int foo(int i = 0)\n"
9944                "#else\n"
9945                "int foo(int i = 5)\n"
9946                "#endif\n"
9947                "{\n"
9948                "  return i;\n"
9949                "}",
9950                GNUBraceStyle);
9951 
9952   verifyFormat("void foo() {}\n"
9953                "void bar()\n"
9954                "#ifdef _DEBUG\n"
9955                "{\n"
9956                "  foo();\n"
9957                "}\n"
9958                "#else\n"
9959                "{\n"
9960                "}\n"
9961                "#endif",
9962                GNUBraceStyle);
9963 
9964   verifyFormat("void foobar() { int i = 5; }\n"
9965                "#ifdef _DEBUG\n"
9966                "void bar() {}\n"
9967                "#else\n"
9968                "void bar() { foobar(); }\n"
9969                "#endif",
9970                GNUBraceStyle);
9971 }
9972 
9973 TEST_F(FormatTest, WebKitBraceBreaking) {
9974   FormatStyle WebKitBraceStyle = getLLVMStyle();
9975   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9976   verifyFormat("namespace a {\n"
9977                "class A {\n"
9978                "  void f()\n"
9979                "  {\n"
9980                "    if (true) {\n"
9981                "      a();\n"
9982                "      b();\n"
9983                "    }\n"
9984                "  }\n"
9985                "  void g() { return; }\n"
9986                "};\n"
9987                "enum E {\n"
9988                "  A,\n"
9989                "  // foo\n"
9990                "  B,\n"
9991                "  C\n"
9992                "};\n"
9993                "struct B {\n"
9994                "  int x;\n"
9995                "};\n"
9996                "}\n",
9997                WebKitBraceStyle);
9998   verifyFormat("struct S {\n"
9999                "  int Type;\n"
10000                "  union {\n"
10001                "    int x;\n"
10002                "    double y;\n"
10003                "  } Value;\n"
10004                "  class C {\n"
10005                "    MyFavoriteType Value;\n"
10006                "  } Class;\n"
10007                "};\n",
10008                WebKitBraceStyle);
10009 }
10010 
10011 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
10012   verifyFormat("void f() {\n"
10013                "  try {\n"
10014                "  } catch (const Exception &e) {\n"
10015                "  }\n"
10016                "}\n",
10017                getLLVMStyle());
10018 }
10019 
10020 TEST_F(FormatTest, UnderstandsPragmas) {
10021   verifyFormat("#pragma omp reduction(| : var)");
10022   verifyFormat("#pragma omp reduction(+ : var)");
10023 
10024   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
10025             "(including parentheses).",
10026             format("#pragma    mark   Any non-hyphenated or hyphenated string "
10027                    "(including parentheses)."));
10028 }
10029 
10030 TEST_F(FormatTest, UnderstandPragmaOption) {
10031   verifyFormat("#pragma option -C -A");
10032 
10033   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
10034 }
10035 
10036 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
10037   for (size_t i = 1; i < Styles.size(); ++i)                                   \
10038   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
10039                                   << " differs from Style #0"
10040 
10041 TEST_F(FormatTest, GetsPredefinedStyleByName) {
10042   SmallVector<FormatStyle, 3> Styles;
10043   Styles.resize(3);
10044 
10045   Styles[0] = getLLVMStyle();
10046   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
10047   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
10048   EXPECT_ALL_STYLES_EQUAL(Styles);
10049 
10050   Styles[0] = getGoogleStyle();
10051   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
10052   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
10053   EXPECT_ALL_STYLES_EQUAL(Styles);
10054 
10055   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10056   EXPECT_TRUE(
10057       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
10058   EXPECT_TRUE(
10059       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
10060   EXPECT_ALL_STYLES_EQUAL(Styles);
10061 
10062   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
10063   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
10064   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
10065   EXPECT_ALL_STYLES_EQUAL(Styles);
10066 
10067   Styles[0] = getMozillaStyle();
10068   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
10069   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
10070   EXPECT_ALL_STYLES_EQUAL(Styles);
10071 
10072   Styles[0] = getWebKitStyle();
10073   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
10074   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
10075   EXPECT_ALL_STYLES_EQUAL(Styles);
10076 
10077   Styles[0] = getGNUStyle();
10078   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
10079   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
10080   EXPECT_ALL_STYLES_EQUAL(Styles);
10081 
10082   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
10083 }
10084 
10085 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
10086   SmallVector<FormatStyle, 8> Styles;
10087   Styles.resize(2);
10088 
10089   Styles[0] = getGoogleStyle();
10090   Styles[1] = getLLVMStyle();
10091   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10092   EXPECT_ALL_STYLES_EQUAL(Styles);
10093 
10094   Styles.resize(5);
10095   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
10096   Styles[1] = getLLVMStyle();
10097   Styles[1].Language = FormatStyle::LK_JavaScript;
10098   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
10099 
10100   Styles[2] = getLLVMStyle();
10101   Styles[2].Language = FormatStyle::LK_JavaScript;
10102   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
10103                                   "BasedOnStyle: Google",
10104                                   &Styles[2])
10105                    .value());
10106 
10107   Styles[3] = getLLVMStyle();
10108   Styles[3].Language = FormatStyle::LK_JavaScript;
10109   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
10110                                   "Language: JavaScript",
10111                                   &Styles[3])
10112                    .value());
10113 
10114   Styles[4] = getLLVMStyle();
10115   Styles[4].Language = FormatStyle::LK_JavaScript;
10116   EXPECT_EQ(0, parseConfiguration("---\n"
10117                                   "BasedOnStyle: LLVM\n"
10118                                   "IndentWidth: 123\n"
10119                                   "---\n"
10120                                   "BasedOnStyle: Google\n"
10121                                   "Language: JavaScript",
10122                                   &Styles[4])
10123                    .value());
10124   EXPECT_ALL_STYLES_EQUAL(Styles);
10125 }
10126 
10127 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
10128   Style.FIELD = false;                                                         \
10129   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
10130   EXPECT_TRUE(Style.FIELD);                                                    \
10131   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
10132   EXPECT_FALSE(Style.FIELD);
10133 
10134 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
10135 
10136 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
10137   Style.STRUCT.FIELD = false;                                                  \
10138   EXPECT_EQ(0,                                                                 \
10139             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10140                 .value());                                                     \
10141   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10142   EXPECT_EQ(0,                                                                 \
10143             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10144                 .value());                                                     \
10145   EXPECT_FALSE(Style.STRUCT.FIELD);
10146 
10147 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10148   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10149 
10150 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10151   EXPECT_NE(VALUE, Style.FIELD);                                               \
10152   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10153   EXPECT_EQ(VALUE, Style.FIELD)
10154 
10155 TEST_F(FormatTest, ParsesConfigurationBools) {
10156   FormatStyle Style = {};
10157   Style.Language = FormatStyle::LK_Cpp;
10158   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
10159   CHECK_PARSE_BOOL(AlignOperands);
10160   CHECK_PARSE_BOOL(AlignTrailingComments);
10161   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10162   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10163   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10164   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10165   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10166   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10167   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10168   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10169   CHECK_PARSE_BOOL(BinPackArguments);
10170   CHECK_PARSE_BOOL(BinPackParameters);
10171   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10172   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10173   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
10174   CHECK_PARSE_BOOL(BreakStringLiterals);
10175   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10176   CHECK_PARSE_BOOL(DerivePointerAlignment);
10177   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10178   CHECK_PARSE_BOOL(DisableFormat);
10179   CHECK_PARSE_BOOL(IndentCaseLabels);
10180   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10181   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10182   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10183   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10184   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10185   CHECK_PARSE_BOOL(ReflowComments);
10186   CHECK_PARSE_BOOL(SortIncludes);
10187   CHECK_PARSE_BOOL(SpacesInParentheses);
10188   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10189   CHECK_PARSE_BOOL(SpacesInAngles);
10190   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10191   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10192   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10193   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10194   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10195 
10196   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10197   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10198   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10199   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10200   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10201   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10202   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10203   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10204   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10205   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10206   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10207 }
10208 
10209 #undef CHECK_PARSE_BOOL
10210 
10211 TEST_F(FormatTest, ParsesConfiguration) {
10212   FormatStyle Style = {};
10213   Style.Language = FormatStyle::LK_Cpp;
10214   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10215   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10216               ConstructorInitializerIndentWidth, 1234u);
10217   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10218   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10219   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10220   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10221               PenaltyBreakBeforeFirstCallParameter, 1234u);
10222   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10223   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10224               PenaltyReturnTypeOnItsOwnLine, 1234u);
10225   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10226               SpacesBeforeTrailingComments, 1234u);
10227   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10228   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10229   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10230 
10231   Style.PointerAlignment = FormatStyle::PAS_Middle;
10232   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10233               FormatStyle::PAS_Left);
10234   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10235               FormatStyle::PAS_Right);
10236   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10237               FormatStyle::PAS_Middle);
10238   // For backward compatibility:
10239   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10240               FormatStyle::PAS_Left);
10241   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10242               FormatStyle::PAS_Right);
10243   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10244               FormatStyle::PAS_Middle);
10245 
10246   Style.Standard = FormatStyle::LS_Auto;
10247   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10248   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10249   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10250   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10251   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10252 
10253   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10254   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10255               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10256   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10257               FormatStyle::BOS_None);
10258   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10259               FormatStyle::BOS_All);
10260   // For backward compatibility:
10261   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10262               FormatStyle::BOS_None);
10263   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10264               FormatStyle::BOS_All);
10265 
10266   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10267   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10268               FormatStyle::BAS_Align);
10269   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10270               FormatStyle::BAS_DontAlign);
10271   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10272               FormatStyle::BAS_AlwaysBreak);
10273   // For backward compatibility:
10274   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10275               FormatStyle::BAS_DontAlign);
10276   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10277               FormatStyle::BAS_Align);
10278 
10279   Style.UseTab = FormatStyle::UT_ForIndentation;
10280   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10281   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10282   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10283   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10284               FormatStyle::UT_ForContinuationAndIndentation);
10285   // For backward compatibility:
10286   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10287   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10288 
10289   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10290   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10291               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10292   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10293               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10294   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10295               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10296   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10297               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10298   // For backward compatibility:
10299   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10300               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10301   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10302               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10303 
10304   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10305   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10306               FormatStyle::SBPO_Never);
10307   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10308               FormatStyle::SBPO_Always);
10309   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10310               FormatStyle::SBPO_ControlStatements);
10311   // For backward compatibility:
10312   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10313               FormatStyle::SBPO_Never);
10314   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10315               FormatStyle::SBPO_ControlStatements);
10316 
10317   Style.ColumnLimit = 123;
10318   FormatStyle BaseStyle = getLLVMStyle();
10319   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10320   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10321 
10322   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10323   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10324               FormatStyle::BS_Attach);
10325   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10326               FormatStyle::BS_Linux);
10327   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10328               FormatStyle::BS_Mozilla);
10329   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10330               FormatStyle::BS_Stroustrup);
10331   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10332               FormatStyle::BS_Allman);
10333   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10334   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10335               FormatStyle::BS_WebKit);
10336   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10337               FormatStyle::BS_Custom);
10338 
10339   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10340   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10341               FormatStyle::RTBS_None);
10342   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10343               FormatStyle::RTBS_All);
10344   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10345               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10346   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10347               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10348   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10349               AlwaysBreakAfterReturnType,
10350               FormatStyle::RTBS_TopLevelDefinitions);
10351 
10352   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10353   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10354               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10355   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10356               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10357   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10358               AlwaysBreakAfterDefinitionReturnType,
10359               FormatStyle::DRTBS_TopLevel);
10360 
10361   Style.NamespaceIndentation = FormatStyle::NI_All;
10362   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10363               FormatStyle::NI_None);
10364   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10365               FormatStyle::NI_Inner);
10366   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10367               FormatStyle::NI_All);
10368 
10369   // FIXME: This is required because parsing a configuration simply overwrites
10370   // the first N elements of the list instead of resetting it.
10371   Style.ForEachMacros.clear();
10372   std::vector<std::string> BoostForeach;
10373   BoostForeach.push_back("BOOST_FOREACH");
10374   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10375   std::vector<std::string> BoostAndQForeach;
10376   BoostAndQForeach.push_back("BOOST_FOREACH");
10377   BoostAndQForeach.push_back("Q_FOREACH");
10378   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10379               BoostAndQForeach);
10380 
10381   Style.IncludeCategories.clear();
10382   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10383                                                                   {".*", 1}};
10384   CHECK_PARSE("IncludeCategories:\n"
10385               "  - Regex: abc/.*\n"
10386               "    Priority: 2\n"
10387               "  - Regex: .*\n"
10388               "    Priority: 1",
10389               IncludeCategories, ExpectedCategories);
10390   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10391 }
10392 
10393 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10394   FormatStyle Style = {};
10395   Style.Language = FormatStyle::LK_Cpp;
10396   CHECK_PARSE("Language: Cpp\n"
10397               "IndentWidth: 12",
10398               IndentWidth, 12u);
10399   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10400                                "IndentWidth: 34",
10401                                &Style),
10402             ParseError::Unsuitable);
10403   EXPECT_EQ(12u, Style.IndentWidth);
10404   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10405   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10406 
10407   Style.Language = FormatStyle::LK_JavaScript;
10408   CHECK_PARSE("Language: JavaScript\n"
10409               "IndentWidth: 12",
10410               IndentWidth, 12u);
10411   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10412   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10413                                "IndentWidth: 34",
10414                                &Style),
10415             ParseError::Unsuitable);
10416   EXPECT_EQ(23u, Style.IndentWidth);
10417   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10418   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10419 
10420   CHECK_PARSE("BasedOnStyle: LLVM\n"
10421               "IndentWidth: 67",
10422               IndentWidth, 67u);
10423 
10424   CHECK_PARSE("---\n"
10425               "Language: JavaScript\n"
10426               "IndentWidth: 12\n"
10427               "---\n"
10428               "Language: Cpp\n"
10429               "IndentWidth: 34\n"
10430               "...\n",
10431               IndentWidth, 12u);
10432 
10433   Style.Language = FormatStyle::LK_Cpp;
10434   CHECK_PARSE("---\n"
10435               "Language: JavaScript\n"
10436               "IndentWidth: 12\n"
10437               "---\n"
10438               "Language: Cpp\n"
10439               "IndentWidth: 34\n"
10440               "...\n",
10441               IndentWidth, 34u);
10442   CHECK_PARSE("---\n"
10443               "IndentWidth: 78\n"
10444               "---\n"
10445               "Language: JavaScript\n"
10446               "IndentWidth: 56\n"
10447               "...\n",
10448               IndentWidth, 78u);
10449 
10450   Style.ColumnLimit = 123;
10451   Style.IndentWidth = 234;
10452   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10453   Style.TabWidth = 345;
10454   EXPECT_FALSE(parseConfiguration("---\n"
10455                                   "IndentWidth: 456\n"
10456                                   "BreakBeforeBraces: Allman\n"
10457                                   "---\n"
10458                                   "Language: JavaScript\n"
10459                                   "IndentWidth: 111\n"
10460                                   "TabWidth: 111\n"
10461                                   "---\n"
10462                                   "Language: Cpp\n"
10463                                   "BreakBeforeBraces: Stroustrup\n"
10464                                   "TabWidth: 789\n"
10465                                   "...\n",
10466                                   &Style));
10467   EXPECT_EQ(123u, Style.ColumnLimit);
10468   EXPECT_EQ(456u, Style.IndentWidth);
10469   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10470   EXPECT_EQ(789u, Style.TabWidth);
10471 
10472   EXPECT_EQ(parseConfiguration("---\n"
10473                                "Language: JavaScript\n"
10474                                "IndentWidth: 56\n"
10475                                "---\n"
10476                                "IndentWidth: 78\n"
10477                                "...\n",
10478                                &Style),
10479             ParseError::Error);
10480   EXPECT_EQ(parseConfiguration("---\n"
10481                                "Language: JavaScript\n"
10482                                "IndentWidth: 56\n"
10483                                "---\n"
10484                                "Language: JavaScript\n"
10485                                "IndentWidth: 78\n"
10486                                "...\n",
10487                                &Style),
10488             ParseError::Error);
10489 
10490   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10491 }
10492 
10493 #undef CHECK_PARSE
10494 
10495 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10496   FormatStyle Style = {};
10497   Style.Language = FormatStyle::LK_JavaScript;
10498   Style.BreakBeforeTernaryOperators = true;
10499   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10500   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10501 
10502   Style.BreakBeforeTernaryOperators = true;
10503   EXPECT_EQ(0, parseConfiguration("---\n"
10504                                   "BasedOnStyle: Google\n"
10505                                   "---\n"
10506                                   "Language: JavaScript\n"
10507                                   "IndentWidth: 76\n"
10508                                   "...\n",
10509                                   &Style)
10510                    .value());
10511   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10512   EXPECT_EQ(76u, Style.IndentWidth);
10513   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10514 }
10515 
10516 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10517   FormatStyle Style = getLLVMStyle();
10518   std::string YAML = configurationAsText(Style);
10519   FormatStyle ParsedStyle = {};
10520   ParsedStyle.Language = FormatStyle::LK_Cpp;
10521   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10522   EXPECT_EQ(Style, ParsedStyle);
10523 }
10524 
10525 TEST_F(FormatTest, WorksFor8bitEncodings) {
10526   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10527             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10528             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10529             "\"\xef\xee\xf0\xf3...\"",
10530             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10531                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10532                    "\xef\xee\xf0\xf3...\"",
10533                    getLLVMStyleWithColumns(12)));
10534 }
10535 
10536 TEST_F(FormatTest, HandlesUTF8BOM) {
10537   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10538   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10539             format("\xef\xbb\xbf#include <iostream>"));
10540   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10541             format("\xef\xbb\xbf\n#include <iostream>"));
10542 }
10543 
10544 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10545 #if !defined(_MSC_VER)
10546 
10547 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10548   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10549                getLLVMStyleWithColumns(35));
10550   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10551                getLLVMStyleWithColumns(31));
10552   verifyFormat("// Однажды в студёную зимнюю пору...",
10553                getLLVMStyleWithColumns(36));
10554   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10555   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10556                getLLVMStyleWithColumns(39));
10557   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10558                getLLVMStyleWithColumns(35));
10559 }
10560 
10561 TEST_F(FormatTest, SplitsUTF8Strings) {
10562   // Non-printable characters' width is currently considered to be the length in
10563   // bytes in UTF8. The characters can be displayed in very different manner
10564   // (zero-width, single width with a substitution glyph, expanded to their code
10565   // (e.g. "<8d>"), so there's no single correct way to handle them.
10566   EXPECT_EQ("\"aaaaÄ\"\n"
10567             "\"\xc2\x8d\";",
10568             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10569   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10570             "\"\xc2\x8d\";",
10571             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10572   EXPECT_EQ("\"Однажды, в \"\n"
10573             "\"студёную \"\n"
10574             "\"зимнюю \"\n"
10575             "\"пору,\"",
10576             format("\"Однажды, в студёную зимнюю пору,\"",
10577                    getLLVMStyleWithColumns(13)));
10578   EXPECT_EQ(
10579       "\"一 二 三 \"\n"
10580       "\"四 五六 \"\n"
10581       "\"七 八 九 \"\n"
10582       "\"十\"",
10583       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10584   EXPECT_EQ("\"一\t二 \"\n"
10585             "\"\t三 \"\n"
10586             "\"四 五\t六 \"\n"
10587             "\"\t七 \"\n"
10588             "\"八九十\tqq\"",
10589             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10590                    getLLVMStyleWithColumns(11)));
10591 
10592   // UTF8 character in an escape sequence.
10593   EXPECT_EQ("\"aaaaaa\"\n"
10594             "\"\\\xC2\x8D\"",
10595             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10596 }
10597 
10598 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10599   EXPECT_EQ("const char *sssss =\n"
10600             "    \"一二三四五六七八\\\n"
10601             " 九 十\";",
10602             format("const char *sssss = \"一二三四五六七八\\\n"
10603                    " 九 十\";",
10604                    getLLVMStyleWithColumns(30)));
10605 }
10606 
10607 TEST_F(FormatTest, SplitsUTF8LineComments) {
10608   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10609             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10610   EXPECT_EQ("// Я из лесу\n"
10611             "// вышел; был\n"
10612             "// сильный\n"
10613             "// мороз.",
10614             format("// Я из лесу вышел; был сильный мороз.",
10615                    getLLVMStyleWithColumns(13)));
10616   EXPECT_EQ("// 一二三\n"
10617             "// 四五六七\n"
10618             "// 八  九\n"
10619             "// 十",
10620             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10621 }
10622 
10623 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10624   EXPECT_EQ("/* Гляжу,\n"
10625             " * поднимается\n"
10626             " * медленно в\n"
10627             " * гору\n"
10628             " * Лошадка,\n"
10629             " * везущая\n"
10630             " * хворосту\n"
10631             " * воз. */",
10632             format("/* Гляжу, поднимается медленно в гору\n"
10633                    " * Лошадка, везущая хворосту воз. */",
10634                    getLLVMStyleWithColumns(13)));
10635   EXPECT_EQ(
10636       "/* 一二三\n"
10637       " * 四五六七\n"
10638       " * 八  九\n"
10639       " * 十  */",
10640       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10641   EXPECT_EQ("/* �������� ��������\n"
10642             " * ��������\n"
10643             " * ������-�� */",
10644             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10645 }
10646 
10647 #endif // _MSC_VER
10648 
10649 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10650   FormatStyle Style = getLLVMStyle();
10651 
10652   Style.ConstructorInitializerIndentWidth = 4;
10653   verifyFormat(
10654       "SomeClass::Constructor()\n"
10655       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10656       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10657       Style);
10658 
10659   Style.ConstructorInitializerIndentWidth = 2;
10660   verifyFormat(
10661       "SomeClass::Constructor()\n"
10662       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10663       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10664       Style);
10665 
10666   Style.ConstructorInitializerIndentWidth = 0;
10667   verifyFormat(
10668       "SomeClass::Constructor()\n"
10669       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10670       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10671       Style);
10672   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10673   verifyFormat(
10674       "SomeLongTemplateVariableName<\n"
10675       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10676       Style);
10677   verifyFormat(
10678       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10679       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10680       Style);
10681 }
10682 
10683 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10684   FormatStyle Style = getLLVMStyle();
10685   Style.BreakConstructorInitializersBeforeComma = true;
10686   Style.ConstructorInitializerIndentWidth = 4;
10687   verifyFormat("SomeClass::Constructor()\n"
10688                "    : a(a)\n"
10689                "    , b(b)\n"
10690                "    , c(c) {}",
10691                Style);
10692   verifyFormat("SomeClass::Constructor()\n"
10693                "    : a(a) {}",
10694                Style);
10695 
10696   Style.ColumnLimit = 0;
10697   verifyFormat("SomeClass::Constructor()\n"
10698                "    : a(a) {}",
10699                Style);
10700   verifyFormat("SomeClass::Constructor() noexcept\n"
10701                "    : a(a) {}",
10702                Style);
10703   verifyFormat("SomeClass::Constructor()\n"
10704                "    : a(a)\n"
10705                "    , b(b)\n"
10706                "    , c(c) {}",
10707                Style);
10708   verifyFormat("SomeClass::Constructor()\n"
10709                "    : a(a) {\n"
10710                "  foo();\n"
10711                "  bar();\n"
10712                "}",
10713                Style);
10714 
10715   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10716   verifyFormat("SomeClass::Constructor()\n"
10717                "    : a(a)\n"
10718                "    , b(b)\n"
10719                "    , c(c) {\n}",
10720                Style);
10721   verifyFormat("SomeClass::Constructor()\n"
10722                "    : a(a) {\n}",
10723                Style);
10724 
10725   Style.ColumnLimit = 80;
10726   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10727   Style.ConstructorInitializerIndentWidth = 2;
10728   verifyFormat("SomeClass::Constructor()\n"
10729                "  : a(a)\n"
10730                "  , b(b)\n"
10731                "  , c(c) {}",
10732                Style);
10733 
10734   Style.ConstructorInitializerIndentWidth = 0;
10735   verifyFormat("SomeClass::Constructor()\n"
10736                ": a(a)\n"
10737                ", b(b)\n"
10738                ", c(c) {}",
10739                Style);
10740 
10741   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10742   Style.ConstructorInitializerIndentWidth = 4;
10743   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10744   verifyFormat(
10745       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10746       Style);
10747   verifyFormat(
10748       "SomeClass::Constructor()\n"
10749       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10750       Style);
10751   Style.ConstructorInitializerIndentWidth = 4;
10752   Style.ColumnLimit = 60;
10753   verifyFormat("SomeClass::Constructor()\n"
10754                "    : aaaaaaaa(aaaaaaaa)\n"
10755                "    , aaaaaaaa(aaaaaaaa)\n"
10756                "    , aaaaaaaa(aaaaaaaa) {}",
10757                Style);
10758 }
10759 
10760 TEST_F(FormatTest, Destructors) {
10761   verifyFormat("void F(int &i) { i.~int(); }");
10762   verifyFormat("void F(int &i) { i->~int(); }");
10763 }
10764 
10765 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10766   FormatStyle Style = getWebKitStyle();
10767 
10768   // Don't indent in outer namespaces.
10769   verifyFormat("namespace outer {\n"
10770                "int i;\n"
10771                "namespace inner {\n"
10772                "    int i;\n"
10773                "} // namespace inner\n"
10774                "} // namespace outer\n"
10775                "namespace other_outer {\n"
10776                "int i;\n"
10777                "}",
10778                Style);
10779 
10780   // Don't indent case labels.
10781   verifyFormat("switch (variable) {\n"
10782                "case 1:\n"
10783                "case 2:\n"
10784                "    doSomething();\n"
10785                "    break;\n"
10786                "default:\n"
10787                "    ++variable;\n"
10788                "}",
10789                Style);
10790 
10791   // Wrap before binary operators.
10792   EXPECT_EQ("void f()\n"
10793             "{\n"
10794             "    if (aaaaaaaaaaaaaaaa\n"
10795             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10796             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10797             "        return;\n"
10798             "}",
10799             format("void f() {\n"
10800                    "if (aaaaaaaaaaaaaaaa\n"
10801                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10802                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10803                    "return;\n"
10804                    "}",
10805                    Style));
10806 
10807   // Allow functions on a single line.
10808   verifyFormat("void f() { return; }", Style);
10809 
10810   // Constructor initializers are formatted one per line with the "," on the
10811   // new line.
10812   verifyFormat("Constructor()\n"
10813                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10814                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10815                "          aaaaaaaaaaaaaa)\n"
10816                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10817                "{\n"
10818                "}",
10819                Style);
10820   verifyFormat("SomeClass::Constructor()\n"
10821                "    : a(a)\n"
10822                "{\n"
10823                "}",
10824                Style);
10825   EXPECT_EQ("SomeClass::Constructor()\n"
10826             "    : a(a)\n"
10827             "{\n"
10828             "}",
10829             format("SomeClass::Constructor():a(a){}", Style));
10830   verifyFormat("SomeClass::Constructor()\n"
10831                "    : a(a)\n"
10832                "    , b(b)\n"
10833                "    , c(c)\n"
10834                "{\n"
10835                "}",
10836                Style);
10837   verifyFormat("SomeClass::Constructor()\n"
10838                "    : a(a)\n"
10839                "{\n"
10840                "    foo();\n"
10841                "    bar();\n"
10842                "}",
10843                Style);
10844 
10845   // Access specifiers should be aligned left.
10846   verifyFormat("class C {\n"
10847                "public:\n"
10848                "    int i;\n"
10849                "};",
10850                Style);
10851 
10852   // Do not align comments.
10853   verifyFormat("int a; // Do not\n"
10854                "double b; // align comments.",
10855                Style);
10856 
10857   // Do not align operands.
10858   EXPECT_EQ("ASSERT(aaaa\n"
10859             "    || bbbb);",
10860             format("ASSERT ( aaaa\n||bbbb);", Style));
10861 
10862   // Accept input's line breaks.
10863   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10864             "    || bbbbbbbbbbbbbbb) {\n"
10865             "    i++;\n"
10866             "}",
10867             format("if (aaaaaaaaaaaaaaa\n"
10868                    "|| bbbbbbbbbbbbbbb) { i++; }",
10869                    Style));
10870   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10871             "    i++;\n"
10872             "}",
10873             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10874 
10875   // Don't automatically break all macro definitions (llvm.org/PR17842).
10876   verifyFormat("#define aNumber 10", Style);
10877   // However, generally keep the line breaks that the user authored.
10878   EXPECT_EQ("#define aNumber \\\n"
10879             "    10",
10880             format("#define aNumber \\\n"
10881                    " 10",
10882                    Style));
10883 
10884   // Keep empty and one-element array literals on a single line.
10885   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10886             "                                  copyItems:YES];",
10887             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10888                    "copyItems:YES];",
10889                    Style));
10890   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10891             "                                  copyItems:YES];",
10892             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10893                    "             copyItems:YES];",
10894                    Style));
10895   // FIXME: This does not seem right, there should be more indentation before
10896   // the array literal's entries. Nested blocks have the same problem.
10897   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10898             "    @\"a\",\n"
10899             "    @\"a\"\n"
10900             "]\n"
10901             "                                  copyItems:YES];",
10902             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10903                    "     @\"a\",\n"
10904                    "     @\"a\"\n"
10905                    "     ]\n"
10906                    "       copyItems:YES];",
10907                    Style));
10908   EXPECT_EQ(
10909       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10910       "                                  copyItems:YES];",
10911       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10912              "   copyItems:YES];",
10913              Style));
10914 
10915   verifyFormat("[self.a b:c c:d];", Style);
10916   EXPECT_EQ("[self.a b:c\n"
10917             "        c:d];",
10918             format("[self.a b:c\n"
10919                    "c:d];",
10920                    Style));
10921 }
10922 
10923 TEST_F(FormatTest, FormatsLambdas) {
10924   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10925   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10926   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10927   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10928   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10929   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10930   verifyFormat("void f() {\n"
10931                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10932                "}\n");
10933   verifyFormat("void f() {\n"
10934                "  other(x.begin(), //\n"
10935                "        x.end(),   //\n"
10936                "        [&](int, int) { return 1; });\n"
10937                "}\n");
10938   verifyFormat("SomeFunction([]() { // A cool function...\n"
10939                "  return 43;\n"
10940                "});");
10941   EXPECT_EQ("SomeFunction([]() {\n"
10942             "#define A a\n"
10943             "  return 43;\n"
10944             "});",
10945             format("SomeFunction([](){\n"
10946                    "#define A a\n"
10947                    "return 43;\n"
10948                    "});"));
10949   verifyFormat("void f() {\n"
10950                "  SomeFunction([](decltype(x), A *a) {});\n"
10951                "}");
10952   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10953                "    [](const aaaaaaaaaa &a) { return a; });");
10954   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10955                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10956                "});");
10957   verifyFormat("Constructor()\n"
10958                "    : Field([] { // comment\n"
10959                "        int i;\n"
10960                "      }) {}");
10961   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10962                "  return some_parameter.size();\n"
10963                "};");
10964   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10965                "    [](const string &s) { return s; };");
10966   verifyFormat("int i = aaaaaa ? 1 //\n"
10967                "               : [] {\n"
10968                "                   return 2; //\n"
10969                "                 }();");
10970   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10971                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10972                "                  return x == 2; // force break\n"
10973                "                });");
10974   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa([=](\n"
10975                "    int iiiiiiiiiiii) {\n"
10976                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
10977                "});",
10978                getLLVMStyleWithColumns(60));
10979   verifyFormat("SomeFunction({[&] {\n"
10980                "                // comment\n"
10981                "              },\n"
10982                "              [&] {\n"
10983                "                // comment\n"
10984                "              }});");
10985   verifyFormat("SomeFunction({[&] {\n"
10986                "  // comment\n"
10987                "}});");
10988   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10989                "                             [&]() { return true; },\n"
10990                "                         aaaaa aaaaaaaaa);");
10991 
10992   // Lambdas with return types.
10993   verifyFormat("int c = []() -> int { return 2; }();\n");
10994   verifyFormat("int c = []() -> int * { return 2; }();\n");
10995   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10996   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10997   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10998   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10999   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
11000   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
11001   verifyFormat("[a, a]() -> a<1> {};");
11002   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
11003                "                   int j) -> int {\n"
11004                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
11005                "};");
11006   verifyFormat(
11007       "aaaaaaaaaaaaaaaaaaaaaa(\n"
11008       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
11009       "      return aaaaaaaaaaaaaaaaa;\n"
11010       "    });",
11011       getLLVMStyleWithColumns(70));
11012   verifyFormat("[]() //\n"
11013                "    -> int {\n"
11014                "  return 1; //\n"
11015                "};");
11016 
11017   // Multiple lambdas in the same parentheses change indentation rules.
11018   verifyFormat("SomeFunction(\n"
11019                "    []() {\n"
11020                "      int i = 42;\n"
11021                "      return i;\n"
11022                "    },\n"
11023                "    []() {\n"
11024                "      int j = 43;\n"
11025                "      return j;\n"
11026                "    });");
11027 
11028   // More complex introducers.
11029   verifyFormat("return [i, args...] {};");
11030 
11031   // Not lambdas.
11032   verifyFormat("constexpr char hello[]{\"hello\"};");
11033   verifyFormat("double &operator[](int i) { return 0; }\n"
11034                "int i;");
11035   verifyFormat("std::unique_ptr<int[]> foo() {}");
11036   verifyFormat("int i = a[a][a]->f();");
11037   verifyFormat("int i = (*b)[a]->f();");
11038 
11039   // Other corner cases.
11040   verifyFormat("void f() {\n"
11041                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
11042                "      );\n"
11043                "}");
11044 
11045   // Lambdas created through weird macros.
11046   verifyFormat("void f() {\n"
11047                "  MACRO((const AA &a) { return 1; });\n"
11048                "  MACRO((AA &a) { return 1; });\n"
11049                "}");
11050 
11051   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
11052                "      doo_dah();\n"
11053                "      doo_dah();\n"
11054                "    })) {\n"
11055                "}");
11056   verifyFormat("auto lambda = []() {\n"
11057                "  int a = 2\n"
11058                "#if A\n"
11059                "          + 2\n"
11060                "#endif\n"
11061                "      ;\n"
11062                "};");
11063 }
11064 
11065 TEST_F(FormatTest, FormatsBlocks) {
11066   FormatStyle ShortBlocks = getLLVMStyle();
11067   ShortBlocks.AllowShortBlocksOnASingleLine = true;
11068   verifyFormat("int (^Block)(int, int);", ShortBlocks);
11069   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
11070   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
11071   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
11072   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
11073   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
11074 
11075   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11076   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11077   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11078 
11079   verifyFormat("[operation setCompletionBlock:^{\n"
11080                "  [self onOperationDone];\n"
11081                "}];");
11082   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11083                "  [self onOperationDone];\n"
11084                "}]};");
11085   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11086                "  f();\n"
11087                "}];");
11088   verifyFormat("int a = [operation block:^int(int *i) {\n"
11089                "  return 1;\n"
11090                "}];");
11091   verifyFormat("[myObject doSomethingWith:arg1\n"
11092                "                      aaa:^int(int *a) {\n"
11093                "                        return 1;\n"
11094                "                      }\n"
11095                "                      bbb:f(a * bbbbbbbb)];");
11096 
11097   verifyFormat("[operation setCompletionBlock:^{\n"
11098                "  [self.delegate newDataAvailable];\n"
11099                "}];",
11100                getLLVMStyleWithColumns(60));
11101   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11102                "  NSString *path = [self sessionFilePath];\n"
11103                "  if (path) {\n"
11104                "    // ...\n"
11105                "  }\n"
11106                "});");
11107   verifyFormat("[[SessionService sharedService]\n"
11108                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11109                "      if (window) {\n"
11110                "        [self windowDidLoad:window];\n"
11111                "      } else {\n"
11112                "        [self errorLoadingWindow];\n"
11113                "      }\n"
11114                "    }];");
11115   verifyFormat("void (^largeBlock)(void) = ^{\n"
11116                "  // ...\n"
11117                "};\n",
11118                getLLVMStyleWithColumns(40));
11119   verifyFormat("[[SessionService sharedService]\n"
11120                "    loadWindowWithCompletionBlock: //\n"
11121                "        ^(SessionWindow *window) {\n"
11122                "          if (window) {\n"
11123                "            [self windowDidLoad:window];\n"
11124                "          } else {\n"
11125                "            [self errorLoadingWindow];\n"
11126                "          }\n"
11127                "        }];",
11128                getLLVMStyleWithColumns(60));
11129   verifyFormat("[myObject doSomethingWith:arg1\n"
11130                "    firstBlock:^(Foo *a) {\n"
11131                "      // ...\n"
11132                "      int i;\n"
11133                "    }\n"
11134                "    secondBlock:^(Bar *b) {\n"
11135                "      // ...\n"
11136                "      int i;\n"
11137                "    }\n"
11138                "    thirdBlock:^Foo(Bar *b) {\n"
11139                "      // ...\n"
11140                "      int i;\n"
11141                "    }];");
11142   verifyFormat("[myObject doSomethingWith:arg1\n"
11143                "               firstBlock:-1\n"
11144                "              secondBlock:^(Bar *b) {\n"
11145                "                // ...\n"
11146                "                int i;\n"
11147                "              }];");
11148 
11149   verifyFormat("f(^{\n"
11150                "  @autoreleasepool {\n"
11151                "    if (a) {\n"
11152                "      g();\n"
11153                "    }\n"
11154                "  }\n"
11155                "});");
11156   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11157 
11158   FormatStyle FourIndent = getLLVMStyle();
11159   FourIndent.ObjCBlockIndentWidth = 4;
11160   verifyFormat("[operation setCompletionBlock:^{\n"
11161                "    [self onOperationDone];\n"
11162                "}];",
11163                FourIndent);
11164 }
11165 
11166 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11167   FormatStyle ZeroColumn = getLLVMStyle();
11168   ZeroColumn.ColumnLimit = 0;
11169 
11170   verifyFormat("[[SessionService sharedService] "
11171                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11172                "  if (window) {\n"
11173                "    [self windowDidLoad:window];\n"
11174                "  } else {\n"
11175                "    [self errorLoadingWindow];\n"
11176                "  }\n"
11177                "}];",
11178                ZeroColumn);
11179   EXPECT_EQ("[[SessionService sharedService]\n"
11180             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11181             "      if (window) {\n"
11182             "        [self windowDidLoad:window];\n"
11183             "      } else {\n"
11184             "        [self errorLoadingWindow];\n"
11185             "      }\n"
11186             "    }];",
11187             format("[[SessionService sharedService]\n"
11188                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11189                    "                if (window) {\n"
11190                    "    [self windowDidLoad:window];\n"
11191                    "  } else {\n"
11192                    "    [self errorLoadingWindow];\n"
11193                    "  }\n"
11194                    "}];",
11195                    ZeroColumn));
11196   verifyFormat("[myObject doSomethingWith:arg1\n"
11197                "    firstBlock:^(Foo *a) {\n"
11198                "      // ...\n"
11199                "      int i;\n"
11200                "    }\n"
11201                "    secondBlock:^(Bar *b) {\n"
11202                "      // ...\n"
11203                "      int i;\n"
11204                "    }\n"
11205                "    thirdBlock:^Foo(Bar *b) {\n"
11206                "      // ...\n"
11207                "      int i;\n"
11208                "    }];",
11209                ZeroColumn);
11210   verifyFormat("f(^{\n"
11211                "  @autoreleasepool {\n"
11212                "    if (a) {\n"
11213                "      g();\n"
11214                "    }\n"
11215                "  }\n"
11216                "});",
11217                ZeroColumn);
11218   verifyFormat("void (^largeBlock)(void) = ^{\n"
11219                "  // ...\n"
11220                "};",
11221                ZeroColumn);
11222 
11223   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11224   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11225             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11226   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11227   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11228             "  int i;\n"
11229             "};",
11230             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11231 }
11232 
11233 TEST_F(FormatTest, SupportsCRLF) {
11234   EXPECT_EQ("int a;\r\n"
11235             "int b;\r\n"
11236             "int c;\r\n",
11237             format("int a;\r\n"
11238                    "  int b;\r\n"
11239                    "    int c;\r\n",
11240                    getLLVMStyle()));
11241   EXPECT_EQ("int a;\r\n"
11242             "int b;\r\n"
11243             "int c;\r\n",
11244             format("int a;\r\n"
11245                    "  int b;\n"
11246                    "    int c;\r\n",
11247                    getLLVMStyle()));
11248   EXPECT_EQ("int a;\n"
11249             "int b;\n"
11250             "int c;\n",
11251             format("int a;\r\n"
11252                    "  int b;\n"
11253                    "    int c;\n",
11254                    getLLVMStyle()));
11255   EXPECT_EQ("\"aaaaaaa \"\r\n"
11256             "\"bbbbbbb\";\r\n",
11257             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11258   EXPECT_EQ("#define A \\\r\n"
11259             "  b;      \\\r\n"
11260             "  c;      \\\r\n"
11261             "  d;\r\n",
11262             format("#define A \\\r\n"
11263                    "  b; \\\r\n"
11264                    "  c; d; \r\n",
11265                    getGoogleStyle()));
11266 
11267   EXPECT_EQ("/*\r\n"
11268             "multi line block comments\r\n"
11269             "should not introduce\r\n"
11270             "an extra carriage return\r\n"
11271             "*/\r\n",
11272             format("/*\r\n"
11273                    "multi line block comments\r\n"
11274                    "should not introduce\r\n"
11275                    "an extra carriage return\r\n"
11276                    "*/\r\n"));
11277 }
11278 
11279 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11280   verifyFormat("MY_CLASS(C) {\n"
11281                "  int i;\n"
11282                "  int j;\n"
11283                "};");
11284 }
11285 
11286 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11287   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11288   TwoIndent.ContinuationIndentWidth = 2;
11289 
11290   EXPECT_EQ("int i =\n"
11291             "  longFunction(\n"
11292             "    arg);",
11293             format("int i = longFunction(arg);", TwoIndent));
11294 
11295   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11296   SixIndent.ContinuationIndentWidth = 6;
11297 
11298   EXPECT_EQ("int i =\n"
11299             "      longFunction(\n"
11300             "            arg);",
11301             format("int i = longFunction(arg);", SixIndent));
11302 }
11303 
11304 TEST_F(FormatTest, SpacesInAngles) {
11305   FormatStyle Spaces = getLLVMStyle();
11306   Spaces.SpacesInAngles = true;
11307 
11308   verifyFormat("static_cast< int >(arg);", Spaces);
11309   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11310   verifyFormat("f< int, float >();", Spaces);
11311   verifyFormat("template <> g() {}", Spaces);
11312   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11313   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11314   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11315                Spaces);
11316 
11317   Spaces.Standard = FormatStyle::LS_Cpp03;
11318   Spaces.SpacesInAngles = true;
11319   verifyFormat("A< A< int > >();", Spaces);
11320 
11321   Spaces.SpacesInAngles = false;
11322   verifyFormat("A<A<int> >();", Spaces);
11323 
11324   Spaces.Standard = FormatStyle::LS_Cpp11;
11325   Spaces.SpacesInAngles = true;
11326   verifyFormat("A< A< int > >();", Spaces);
11327 
11328   Spaces.SpacesInAngles = false;
11329   verifyFormat("A<A<int>>();", Spaces);
11330 }
11331 
11332 TEST_F(FormatTest, TripleAngleBrackets) {
11333   verifyFormat("f<<<1, 1>>>();");
11334   verifyFormat("f<<<1, 1, 1, s>>>();");
11335   verifyFormat("f<<<a, b, c, d>>>();");
11336   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11337   verifyFormat("f<param><<<1, 1>>>();");
11338   verifyFormat("f<1><<<1, 1>>>();");
11339   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11340   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11341                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11342 }
11343 
11344 TEST_F(FormatTest, MergeLessLessAtEnd) {
11345   verifyFormat("<<");
11346   EXPECT_EQ("< < <", format("\\\n<<<"));
11347   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11348                "aaallvm::outs() <<");
11349   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11350                "aaaallvm::outs()\n    <<");
11351 }
11352 
11353 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11354   std::string code = "#if A\n"
11355                      "#if B\n"
11356                      "a.\n"
11357                      "#endif\n"
11358                      "    a = 1;\n"
11359                      "#else\n"
11360                      "#endif\n"
11361                      "#if C\n"
11362                      "#else\n"
11363                      "#endif\n";
11364   EXPECT_EQ(code, format(code));
11365 }
11366 
11367 TEST_F(FormatTest, HandleConflictMarkers) {
11368   // Git/SVN conflict markers.
11369   EXPECT_EQ("int a;\n"
11370             "void f() {\n"
11371             "  callme(some(parameter1,\n"
11372             "<<<<<<< text by the vcs\n"
11373             "              parameter2),\n"
11374             "||||||| text by the vcs\n"
11375             "              parameter2),\n"
11376             "         parameter3,\n"
11377             "======= text by the vcs\n"
11378             "              parameter2, parameter3),\n"
11379             ">>>>>>> text by the vcs\n"
11380             "         otherparameter);\n",
11381             format("int a;\n"
11382                    "void f() {\n"
11383                    "  callme(some(parameter1,\n"
11384                    "<<<<<<< text by the vcs\n"
11385                    "  parameter2),\n"
11386                    "||||||| text by the vcs\n"
11387                    "  parameter2),\n"
11388                    "  parameter3,\n"
11389                    "======= text by the vcs\n"
11390                    "  parameter2,\n"
11391                    "  parameter3),\n"
11392                    ">>>>>>> text by the vcs\n"
11393                    "  otherparameter);\n"));
11394 
11395   // Perforce markers.
11396   EXPECT_EQ("void f() {\n"
11397             "  function(\n"
11398             ">>>> text by the vcs\n"
11399             "      parameter,\n"
11400             "==== text by the vcs\n"
11401             "      parameter,\n"
11402             "==== text by the vcs\n"
11403             "      parameter,\n"
11404             "<<<< text by the vcs\n"
11405             "      parameter);\n",
11406             format("void f() {\n"
11407                    "  function(\n"
11408                    ">>>> text by the vcs\n"
11409                    "  parameter,\n"
11410                    "==== text by the vcs\n"
11411                    "  parameter,\n"
11412                    "==== text by the vcs\n"
11413                    "  parameter,\n"
11414                    "<<<< text by the vcs\n"
11415                    "  parameter);\n"));
11416 
11417   EXPECT_EQ("<<<<<<<\n"
11418             "|||||||\n"
11419             "=======\n"
11420             ">>>>>>>",
11421             format("<<<<<<<\n"
11422                    "|||||||\n"
11423                    "=======\n"
11424                    ">>>>>>>"));
11425 
11426   EXPECT_EQ("<<<<<<<\n"
11427             "|||||||\n"
11428             "int i;\n"
11429             "=======\n"
11430             ">>>>>>>",
11431             format("<<<<<<<\n"
11432                    "|||||||\n"
11433                    "int i;\n"
11434                    "=======\n"
11435                    ">>>>>>>"));
11436 
11437   // FIXME: Handle parsing of macros around conflict markers correctly:
11438   EXPECT_EQ("#define Macro \\\n"
11439             "<<<<<<<\n"
11440             "Something \\\n"
11441             "|||||||\n"
11442             "Else \\\n"
11443             "=======\n"
11444             "Other \\\n"
11445             ">>>>>>>\n"
11446             "    End int i;\n",
11447             format("#define Macro \\\n"
11448                    "<<<<<<<\n"
11449                    "  Something \\\n"
11450                    "|||||||\n"
11451                    "  Else \\\n"
11452                    "=======\n"
11453                    "  Other \\\n"
11454                    ">>>>>>>\n"
11455                    "  End\n"
11456                    "int i;\n"));
11457 }
11458 
11459 TEST_F(FormatTest, DisableRegions) {
11460   EXPECT_EQ("int i;\n"
11461             "// clang-format off\n"
11462             "  int j;\n"
11463             "// clang-format on\n"
11464             "int k;",
11465             format(" int  i;\n"
11466                    "   // clang-format off\n"
11467                    "  int j;\n"
11468                    " // clang-format on\n"
11469                    "   int   k;"));
11470   EXPECT_EQ("int i;\n"
11471             "/* clang-format off */\n"
11472             "  int j;\n"
11473             "/* clang-format on */\n"
11474             "int k;",
11475             format(" int  i;\n"
11476                    "   /* clang-format off */\n"
11477                    "  int j;\n"
11478                    " /* clang-format on */\n"
11479                    "   int   k;"));
11480 }
11481 
11482 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11483   format("? ) =");
11484   verifyNoCrash("#define a\\\n /**/}");
11485 }
11486 
11487 TEST_F(FormatTest, FormatsTableGenCode) {
11488   FormatStyle Style = getLLVMStyle();
11489   Style.Language = FormatStyle::LK_TableGen;
11490   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11491 }
11492 
11493 // Since this test case uses UNIX-style file path. We disable it for MS
11494 // compiler.
11495 #if !defined(_MSC_VER) && !defined(__MINGW32__)
11496 
11497 TEST(FormatStyle, GetStyleOfFile) {
11498   vfs::InMemoryFileSystem FS;
11499   // Test 1: format file in the same directory.
11500   ASSERT_TRUE(
11501       FS.addFile("/a/.clang-format", 0,
11502                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11503   ASSERT_TRUE(
11504       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11505   auto Style1 = getStyle("file", "/a/.clang-format", "Google", &FS);
11506   ASSERT_EQ(Style1, getLLVMStyle());
11507 
11508   // Test 2: fallback to default.
11509   ASSERT_TRUE(
11510       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11511   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", &FS);
11512   ASSERT_EQ(Style2, getMozillaStyle());
11513 
11514   // Test 3: format file in parent directory.
11515   ASSERT_TRUE(
11516       FS.addFile("/c/.clang-format", 0,
11517                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11518   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11519                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11520   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", &FS);
11521   ASSERT_EQ(Style3, getGoogleStyle());
11522 }
11523 
11524 #endif // _MSC_VER
11525 
11526 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11527   // Column limit is 20.
11528   std::string Code = "Type *a =\n"
11529                      "    new Type();\n"
11530                      "g(iiiii, 0, jjjjj,\n"
11531                      "  0, kkkkk, 0, mm);\n"
11532                      "int  bad     = format   ;";
11533   std::string Expected = "auto a = new Type();\n"
11534                          "g(iiiii, nullptr,\n"
11535                          "  jjjjj, nullptr,\n"
11536                          "  kkkkk, nullptr,\n"
11537                          "  mm);\n"
11538                          "int  bad     = format   ;";
11539   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11540   tooling::Replacements Replaces = toReplacements(
11541       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11542                             "auto "),
11543        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11544                             "nullptr"),
11545        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11546                             "nullptr"),
11547        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11548                             "nullptr")});
11549 
11550   format::FormatStyle Style = format::getLLVMStyle();
11551   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11552   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11553   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11554       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11555   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11556   EXPECT_TRUE(static_cast<bool>(Result));
11557   EXPECT_EQ(Expected, *Result);
11558 }
11559 
11560 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11561   std::string Code = "#include \"a.h\"\n"
11562                      "#include \"c.h\"\n"
11563                      "\n"
11564                      "int main() {\n"
11565                      "  return 0;\n"
11566                      "}";
11567   std::string Expected = "#include \"a.h\"\n"
11568                          "#include \"b.h\"\n"
11569                          "#include \"c.h\"\n"
11570                          "\n"
11571                          "int main() {\n"
11572                          "  return 0;\n"
11573                          "}";
11574   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11575   tooling::Replacements Replaces = toReplacements(
11576       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11577                             "#include \"b.h\"\n")});
11578 
11579   format::FormatStyle Style = format::getLLVMStyle();
11580   Style.SortIncludes = true;
11581   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11582   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11583       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11584   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11585   EXPECT_TRUE(static_cast<bool>(Result));
11586   EXPECT_EQ(Expected, *Result);
11587 }
11588 
11589 } // end namespace
11590 } // end namespace format
11591 } // end namespace clang
11592