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