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