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 StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_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     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
66     return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
67   }
68 
69   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
70     return getStyleWithColumns(getGoogleStyle(), ColumnLimit);
71   }
72 
73   void verifyFormat(llvm::StringRef Code,
74                     const FormatStyle &Style = getLLVMStyle()) {
75     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
76     if (Style.Language == FormatStyle::LK_Cpp) {
77       // Objective-C++ is a superset of C++, so everything checked for C++
78       // needs to be checked for Objective-C++ as well.
79       FormatStyle ObjCStyle = Style;
80       ObjCStyle.Language = FormatStyle::LK_ObjC;
81       EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle));
82     }
83   }
84 
85   void verifyIncompleteFormat(llvm::StringRef Code,
86                               const FormatStyle &Style = getLLVMStyle()) {
87     EXPECT_EQ(Code.str(),
88               format(test::messUp(Code), Style, SC_ExpectIncomplete));
89   }
90 
91   void verifyGoogleFormat(llvm::StringRef Code) {
92     verifyFormat(Code, getGoogleStyle());
93   }
94 
95   void verifyIndependentOfContext(llvm::StringRef text) {
96     verifyFormat(text);
97     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
98   }
99 
100   /// \brief Verify that clang-format does not crash on the given input.
101   void verifyNoCrash(llvm::StringRef Code,
102                      const FormatStyle &Style = getLLVMStyle()) {
103     format(Code, Style, SC_DoNotCheck);
104   }
105 
106   int ReplacementCount;
107 };
108 
109 TEST_F(FormatTest, MessUp) {
110   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
111   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
112   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
113   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
114   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // Basic function tests.
119 //===----------------------------------------------------------------------===//
120 
121 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
122   EXPECT_EQ(";", format(";"));
123 }
124 
125 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
126   EXPECT_EQ("int i;", format("  int i;"));
127   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
128   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
129   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
130 }
131 
132 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
133   EXPECT_EQ("int i;", format("int\ni;"));
134 }
135 
136 TEST_F(FormatTest, FormatsNestedBlockStatements) {
137   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
138 }
139 
140 TEST_F(FormatTest, FormatsNestedCall) {
141   verifyFormat("Method(f1, f2(f3));");
142   verifyFormat("Method(f1(f2, f3()));");
143   verifyFormat("Method(f1(f2, (f3())));");
144 }
145 
146 TEST_F(FormatTest, NestedNameSpecifiers) {
147   verifyFormat("vector<::Type> v;");
148   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
149   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
150   verifyFormat("bool a = 2 < ::SomeFunction();");
151   verifyFormat("ALWAYS_INLINE ::std::string getName();");
152   verifyFormat("some::string getName();");
153 }
154 
155 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
156   EXPECT_EQ("if (a) {\n"
157             "  f();\n"
158             "}",
159             format("if(a){f();}"));
160   EXPECT_EQ(4, ReplacementCount);
161   EXPECT_EQ("if (a) {\n"
162             "  f();\n"
163             "}",
164             format("if (a) {\n"
165                    "  f();\n"
166                    "}"));
167   EXPECT_EQ(0, ReplacementCount);
168   EXPECT_EQ("/*\r\n"
169             "\r\n"
170             "*/\r\n",
171             format("/*\r\n"
172                    "\r\n"
173                    "*/\r\n"));
174   EXPECT_EQ(0, ReplacementCount);
175 }
176 
177 TEST_F(FormatTest, RemovesEmptyLines) {
178   EXPECT_EQ("class C {\n"
179             "  int i;\n"
180             "};",
181             format("class C {\n"
182                    " int i;\n"
183                    "\n"
184                    "};"));
185 
186   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
187   EXPECT_EQ("namespace N {\n"
188             "\n"
189             "int i;\n"
190             "}",
191             format("namespace N {\n"
192                    "\n"
193                    "int    i;\n"
194                    "}",
195                    getGoogleStyle()));
196   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
197             "\n"
198             "int i;\n"
199             "}",
200             format("extern /**/ \"C\" /**/ {\n"
201                    "\n"
202                    "int    i;\n"
203                    "}",
204                    getGoogleStyle()));
205 
206   // ...but do keep inlining and removing empty lines for non-block extern "C"
207   // functions.
208   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
209   EXPECT_EQ("extern \"C\" int f() {\n"
210             "  int i = 42;\n"
211             "  return i;\n"
212             "}",
213             format("extern \"C\" int f() {\n"
214                    "\n"
215                    "  int i = 42;\n"
216                    "  return i;\n"
217                    "}",
218                    getGoogleStyle()));
219 
220   // Remove empty lines at the beginning and end of blocks.
221   EXPECT_EQ("void f() {\n"
222             "\n"
223             "  if (a) {\n"
224             "\n"
225             "    f();\n"
226             "  }\n"
227             "}",
228             format("void f() {\n"
229                    "\n"
230                    "  if (a) {\n"
231                    "\n"
232                    "    f();\n"
233                    "\n"
234                    "  }\n"
235                    "\n"
236                    "}",
237                    getLLVMStyle()));
238   EXPECT_EQ("void f() {\n"
239             "  if (a) {\n"
240             "    f();\n"
241             "  }\n"
242             "}",
243             format("void f() {\n"
244                    "\n"
245                    "  if (a) {\n"
246                    "\n"
247                    "    f();\n"
248                    "\n"
249                    "  }\n"
250                    "\n"
251                    "}",
252                    getGoogleStyle()));
253 
254   // Don't remove empty lines in more complex control statements.
255   EXPECT_EQ("void f() {\n"
256             "  if (a) {\n"
257             "    f();\n"
258             "\n"
259             "  } else if (b) {\n"
260             "    f();\n"
261             "  }\n"
262             "}",
263             format("void f() {\n"
264                    "  if (a) {\n"
265                    "    f();\n"
266                    "\n"
267                    "  } else if (b) {\n"
268                    "    f();\n"
269                    "\n"
270                    "  }\n"
271                    "\n"
272                    "}"));
273 
274   // FIXME: This is slightly inconsistent.
275   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
276   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
277   EXPECT_EQ("namespace {\n"
278             "int i;\n"
279             "}",
280             format("namespace {\n"
281                    "int i;\n"
282                    "\n"
283                    "}", LLVMWithNoNamespaceFix));
284   EXPECT_EQ("namespace {\n"
285             "int i;\n"
286             "}",
287             format("namespace {\n"
288                    "int i;\n"
289                    "\n"
290                    "}"));
291   EXPECT_EQ("namespace {\n"
292             "int i;\n"
293             "\n"
294             "} // namespace",
295             format("namespace {\n"
296                    "int i;\n"
297                    "\n"
298                    "}  // namespace"));
299 
300   FormatStyle Style = getLLVMStyle();
301   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
302   Style.MaxEmptyLinesToKeep = 2;
303   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
304   Style.BraceWrapping.AfterClass = true;
305   Style.BraceWrapping.AfterFunction = true;
306   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
307 
308   EXPECT_EQ("class Foo\n"
309             "{\n"
310             "  Foo() {}\n"
311             "\n"
312             "  void funk() {}\n"
313             "};",
314             format("class Foo\n"
315                    "{\n"
316                    "  Foo()\n"
317                    "  {\n"
318                    "  }\n"
319                    "\n"
320                    "  void funk() {}\n"
321                    "};",
322                    Style));
323 }
324 
325 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
326   verifyFormat("x = (a) and (b);");
327   verifyFormat("x = (a) or (b);");
328   verifyFormat("x = (a) bitand (b);");
329   verifyFormat("x = (a) bitor (b);");
330   verifyFormat("x = (a) not_eq (b);");
331   verifyFormat("x = (a) and_eq (b);");
332   verifyFormat("x = (a) or_eq (b);");
333   verifyFormat("x = (a) xor (b);");
334 }
335 
336 TEST_F(FormatTest, RecognizesUnaryOperatorKeywords) {
337   verifyFormat("x = compl(a);");
338   verifyFormat("x = not(a);");
339   verifyFormat("x = bitand(a);");
340   // Unary operator must not be merged with the next identifier
341   verifyFormat("x = compl a;");
342   verifyFormat("x = not a;");
343   verifyFormat("x = bitand a;");
344 }
345 
346 //===----------------------------------------------------------------------===//
347 // Tests for control statements.
348 //===----------------------------------------------------------------------===//
349 
350 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
351   verifyFormat("if (true)\n  f();\ng();");
352   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
353   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
354   verifyFormat("if constexpr (true)\n"
355                "  f();\ng();");
356   verifyFormat("if constexpr (a)\n"
357                "  if constexpr (b)\n"
358                "    if constexpr (c)\n"
359                "      g();\n"
360                "h();");
361   verifyFormat("if constexpr (a)\n"
362                "  if constexpr (b) {\n"
363                "    f();\n"
364                "  }\n"
365                "g();");
366 
367   FormatStyle AllowsMergedIf = getLLVMStyle();
368   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
369   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
370   verifyFormat("if (a)\n"
371                "  // comment\n"
372                "  f();",
373                AllowsMergedIf);
374   verifyFormat("{\n"
375                "  if (a)\n"
376                "  label:\n"
377                "    f();\n"
378                "}",
379                AllowsMergedIf);
380   verifyFormat("#define A \\\n"
381                "  if (a)  \\\n"
382                "  label:  \\\n"
383                "    f()",
384                AllowsMergedIf);
385   verifyFormat("if (a)\n"
386                "  ;",
387                AllowsMergedIf);
388   verifyFormat("if (a)\n"
389                "  if (b) return;",
390                AllowsMergedIf);
391 
392   verifyFormat("if (a) // Can't merge this\n"
393                "  f();\n",
394                AllowsMergedIf);
395   verifyFormat("if (a) /* still don't merge */\n"
396                "  f();",
397                AllowsMergedIf);
398   verifyFormat("if (a) { // Never merge this\n"
399                "  f();\n"
400                "}",
401                AllowsMergedIf);
402   verifyFormat("if (a) { /* Never merge this */\n"
403                "  f();\n"
404                "}",
405                AllowsMergedIf);
406 
407   AllowsMergedIf.ColumnLimit = 14;
408   verifyFormat("if (a) return;", AllowsMergedIf);
409   verifyFormat("if (aaaaaaaaa)\n"
410                "  return;",
411                AllowsMergedIf);
412 
413   AllowsMergedIf.ColumnLimit = 13;
414   verifyFormat("if (a)\n  return;", AllowsMergedIf);
415 }
416 
417 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
418   FormatStyle AllowsMergedLoops = getLLVMStyle();
419   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
420   verifyFormat("while (true) continue;", AllowsMergedLoops);
421   verifyFormat("for (;;) continue;", AllowsMergedLoops);
422   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
423   verifyFormat("while (true)\n"
424                "  ;",
425                AllowsMergedLoops);
426   verifyFormat("for (;;)\n"
427                "  ;",
428                AllowsMergedLoops);
429   verifyFormat("for (;;)\n"
430                "  for (;;) continue;",
431                AllowsMergedLoops);
432   verifyFormat("for (;;) // Can't merge this\n"
433                "  continue;",
434                AllowsMergedLoops);
435   verifyFormat("for (;;) /* still don't merge */\n"
436                "  continue;",
437                AllowsMergedLoops);
438 }
439 
440 TEST_F(FormatTest, FormatShortBracedStatements) {
441   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
442   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
443 
444   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
445   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
446 
447   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
448   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
449   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
450   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
451   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
452   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
453   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
454   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
455   verifyFormat("if (true) { //\n"
456                "  f();\n"
457                "}",
458                AllowSimpleBracedStatements);
459   verifyFormat("if (true) {\n"
460                "  f();\n"
461                "  f();\n"
462                "}",
463                AllowSimpleBracedStatements);
464   verifyFormat("if (true) {\n"
465                "  f();\n"
466                "} else {\n"
467                "  f();\n"
468                "}",
469                AllowSimpleBracedStatements);
470 
471   verifyFormat("struct A2 {\n"
472                "  int X;\n"
473                "};",
474                AllowSimpleBracedStatements);
475   verifyFormat("typedef struct A2 {\n"
476                "  int X;\n"
477                "} A2_t;",
478                AllowSimpleBracedStatements);
479   verifyFormat("template <int> struct A2 {\n"
480                "  struct B {};\n"
481                "};",
482                AllowSimpleBracedStatements);
483 
484   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
485   verifyFormat("if (true) {\n"
486                "  f();\n"
487                "}",
488                AllowSimpleBracedStatements);
489   verifyFormat("if (true) {\n"
490                "  f();\n"
491                "} else {\n"
492                "  f();\n"
493                "}",
494                AllowSimpleBracedStatements);
495 
496   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
497   verifyFormat("while (true) {\n"
498                "  f();\n"
499                "}",
500                AllowSimpleBracedStatements);
501   verifyFormat("for (;;) {\n"
502                "  f();\n"
503                "}",
504                AllowSimpleBracedStatements);
505 }
506 
507 TEST_F(FormatTest, ParseIfElse) {
508   verifyFormat("if (true)\n"
509                "  if (true)\n"
510                "    if (true)\n"
511                "      f();\n"
512                "    else\n"
513                "      g();\n"
514                "  else\n"
515                "    h();\n"
516                "else\n"
517                "  i();");
518   verifyFormat("if (true)\n"
519                "  if (true)\n"
520                "    if (true) {\n"
521                "      if (true)\n"
522                "        f();\n"
523                "    } else {\n"
524                "      g();\n"
525                "    }\n"
526                "  else\n"
527                "    h();\n"
528                "else {\n"
529                "  i();\n"
530                "}");
531   verifyFormat("if (true)\n"
532                "  if constexpr (true)\n"
533                "    if (true) {\n"
534                "      if constexpr (true)\n"
535                "        f();\n"
536                "    } else {\n"
537                "      g();\n"
538                "    }\n"
539                "  else\n"
540                "    h();\n"
541                "else {\n"
542                "  i();\n"
543                "}");
544   verifyFormat("void f() {\n"
545                "  if (a) {\n"
546                "  } else {\n"
547                "  }\n"
548                "}");
549 }
550 
551 TEST_F(FormatTest, ElseIf) {
552   verifyFormat("if (a) {\n} else if (b) {\n}");
553   verifyFormat("if (a)\n"
554                "  f();\n"
555                "else if (b)\n"
556                "  g();\n"
557                "else\n"
558                "  h();");
559   verifyFormat("if constexpr (a)\n"
560                "  f();\n"
561                "else if constexpr (b)\n"
562                "  g();\n"
563                "else\n"
564                "  h();");
565   verifyFormat("if (a) {\n"
566                "  f();\n"
567                "}\n"
568                "// or else ..\n"
569                "else {\n"
570                "  g()\n"
571                "}");
572 
573   verifyFormat("if (a) {\n"
574                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
575                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
576                "}");
577   verifyFormat("if (a) {\n"
578                "} else if (\n"
579                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
580                "}",
581                getLLVMStyleWithColumns(62));
582   verifyFormat("if (a) {\n"
583                "} else if constexpr (\n"
584                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
585                "}",
586                getLLVMStyleWithColumns(62));
587 }
588 
589 TEST_F(FormatTest, FormatsForLoop) {
590   verifyFormat(
591       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
592       "     ++VeryVeryLongLoopVariable)\n"
593       "  ;");
594   verifyFormat("for (;;)\n"
595                "  f();");
596   verifyFormat("for (;;) {\n}");
597   verifyFormat("for (;;) {\n"
598                "  f();\n"
599                "}");
600   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
601 
602   verifyFormat(
603       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
604       "                                          E = UnwrappedLines.end();\n"
605       "     I != E; ++I) {\n}");
606 
607   verifyFormat(
608       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
609       "     ++IIIII) {\n}");
610   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
611                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
612                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
613   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
614                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
615                "         E = FD->getDeclsInPrototypeScope().end();\n"
616                "     I != E; ++I) {\n}");
617   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
618                "         I = Container.begin(),\n"
619                "         E = Container.end();\n"
620                "     I != E; ++I) {\n}",
621                getLLVMStyleWithColumns(76));
622 
623   verifyFormat(
624       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
625       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
626       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
627       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
628       "     ++aaaaaaaaaaa) {\n}");
629   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
630                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
631                "     ++i) {\n}");
632   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
633                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
634                "}");
635   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
636                "         aaaaaaaaaa);\n"
637                "     iter; ++iter) {\n"
638                "}");
639   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
640                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
641                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
642                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
643 
644   FormatStyle NoBinPacking = getLLVMStyle();
645   NoBinPacking.BinPackParameters = false;
646   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
647                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
648                "                                           aaaaaaaaaaaaaaaa,\n"
649                "                                           aaaaaaaaaaaaaaaa,\n"
650                "                                           aaaaaaaaaaaaaaaa);\n"
651                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
652                "}",
653                NoBinPacking);
654   verifyFormat(
655       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
656       "                                          E = UnwrappedLines.end();\n"
657       "     I != E;\n"
658       "     ++I) {\n}",
659       NoBinPacking);
660 }
661 
662 TEST_F(FormatTest, RangeBasedForLoops) {
663   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
664                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
665   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
666                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
667   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
668                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
669   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
670                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
671 }
672 
673 TEST_F(FormatTest, ForEachLoops) {
674   verifyFormat("void f() {\n"
675                "  foreach (Item *item, itemlist) {}\n"
676                "  Q_FOREACH (Item *item, itemlist) {}\n"
677                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
678                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
679                "}");
680 
681   // As function-like macros.
682   verifyFormat("#define foreach(x, y)\n"
683                "#define Q_FOREACH(x, y)\n"
684                "#define BOOST_FOREACH(x, y)\n"
685                "#define UNKNOWN_FOREACH(x, y)\n");
686 
687   // Not as function-like macros.
688   verifyFormat("#define foreach (x, y)\n"
689                "#define Q_FOREACH (x, y)\n"
690                "#define BOOST_FOREACH (x, y)\n"
691                "#define UNKNOWN_FOREACH (x, y)\n");
692 }
693 
694 TEST_F(FormatTest, FormatsWhileLoop) {
695   verifyFormat("while (true) {\n}");
696   verifyFormat("while (true)\n"
697                "  f();");
698   verifyFormat("while () {\n}");
699   verifyFormat("while () {\n"
700                "  f();\n"
701                "}");
702 }
703 
704 TEST_F(FormatTest, FormatsDoWhile) {
705   verifyFormat("do {\n"
706                "  do_something();\n"
707                "} while (something());");
708   verifyFormat("do\n"
709                "  do_something();\n"
710                "while (something());");
711 }
712 
713 TEST_F(FormatTest, FormatsSwitchStatement) {
714   verifyFormat("switch (x) {\n"
715                "case 1:\n"
716                "  f();\n"
717                "  break;\n"
718                "case kFoo:\n"
719                "case ns::kBar:\n"
720                "case kBaz:\n"
721                "  break;\n"
722                "default:\n"
723                "  g();\n"
724                "  break;\n"
725                "}");
726   verifyFormat("switch (x) {\n"
727                "case 1: {\n"
728                "  f();\n"
729                "  break;\n"
730                "}\n"
731                "case 2: {\n"
732                "  break;\n"
733                "}\n"
734                "}");
735   verifyFormat("switch (x) {\n"
736                "case 1: {\n"
737                "  f();\n"
738                "  {\n"
739                "    g();\n"
740                "    h();\n"
741                "  }\n"
742                "  break;\n"
743                "}\n"
744                "}");
745   verifyFormat("switch (x) {\n"
746                "case 1: {\n"
747                "  f();\n"
748                "  if (foo) {\n"
749                "    g();\n"
750                "    h();\n"
751                "  }\n"
752                "  break;\n"
753                "}\n"
754                "}");
755   verifyFormat("switch (x) {\n"
756                "case 1: {\n"
757                "  f();\n"
758                "  g();\n"
759                "} break;\n"
760                "}");
761   verifyFormat("switch (test)\n"
762                "  ;");
763   verifyFormat("switch (x) {\n"
764                "default: {\n"
765                "  // Do nothing.\n"
766                "}\n"
767                "}");
768   verifyFormat("switch (x) {\n"
769                "// comment\n"
770                "// if 1, do f()\n"
771                "case 1:\n"
772                "  f();\n"
773                "}");
774   verifyFormat("switch (x) {\n"
775                "case 1:\n"
776                "  // Do amazing stuff\n"
777                "  {\n"
778                "    f();\n"
779                "    g();\n"
780                "  }\n"
781                "  break;\n"
782                "}");
783   verifyFormat("#define A          \\\n"
784                "  switch (x) {     \\\n"
785                "  case a:          \\\n"
786                "    foo = b;       \\\n"
787                "  }",
788                getLLVMStyleWithColumns(20));
789   verifyFormat("#define OPERATION_CASE(name)           \\\n"
790                "  case OP_name:                        \\\n"
791                "    return operations::Operation##name\n",
792                getLLVMStyleWithColumns(40));
793   verifyFormat("switch (x) {\n"
794                "case 1:;\n"
795                "default:;\n"
796                "  int i;\n"
797                "}");
798 
799   verifyGoogleFormat("switch (x) {\n"
800                      "  case 1:\n"
801                      "    f();\n"
802                      "    break;\n"
803                      "  case kFoo:\n"
804                      "  case ns::kBar:\n"
805                      "  case kBaz:\n"
806                      "    break;\n"
807                      "  default:\n"
808                      "    g();\n"
809                      "    break;\n"
810                      "}");
811   verifyGoogleFormat("switch (x) {\n"
812                      "  case 1: {\n"
813                      "    f();\n"
814                      "    break;\n"
815                      "  }\n"
816                      "}");
817   verifyGoogleFormat("switch (test)\n"
818                      "  ;");
819 
820   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
821                      "  case OP_name:              \\\n"
822                      "    return operations::Operation##name\n");
823   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
824                      "  // Get the correction operation class.\n"
825                      "  switch (OpCode) {\n"
826                      "    CASE(Add);\n"
827                      "    CASE(Subtract);\n"
828                      "    default:\n"
829                      "      return operations::Unknown;\n"
830                      "  }\n"
831                      "#undef OPERATION_CASE\n"
832                      "}");
833   verifyFormat("DEBUG({\n"
834                "  switch (x) {\n"
835                "  case A:\n"
836                "    f();\n"
837                "    break;\n"
838                "    // fallthrough\n"
839                "  case B:\n"
840                "    g();\n"
841                "    break;\n"
842                "  }\n"
843                "});");
844   EXPECT_EQ("DEBUG({\n"
845             "  switch (x) {\n"
846             "  case A:\n"
847             "    f();\n"
848             "    break;\n"
849             "  // On B:\n"
850             "  case B:\n"
851             "    g();\n"
852             "    break;\n"
853             "  }\n"
854             "});",
855             format("DEBUG({\n"
856                    "  switch (x) {\n"
857                    "  case A:\n"
858                    "    f();\n"
859                    "    break;\n"
860                    "  // On B:\n"
861                    "  case B:\n"
862                    "    g();\n"
863                    "    break;\n"
864                    "  }\n"
865                    "});",
866                    getLLVMStyle()));
867   verifyFormat("switch (a) {\n"
868                "case (b):\n"
869                "  return;\n"
870                "}");
871 
872   verifyFormat("switch (a) {\n"
873                "case some_namespace::\n"
874                "    some_constant:\n"
875                "  return;\n"
876                "}",
877                getLLVMStyleWithColumns(34));
878 }
879 
880 TEST_F(FormatTest, CaseRanges) {
881   verifyFormat("switch (x) {\n"
882                "case 'A' ... 'Z':\n"
883                "case 1 ... 5:\n"
884                "case a ... b:\n"
885                "  break;\n"
886                "}");
887 }
888 
889 TEST_F(FormatTest, ShortCaseLabels) {
890   FormatStyle Style = getLLVMStyle();
891   Style.AllowShortCaseLabelsOnASingleLine = true;
892   verifyFormat("switch (a) {\n"
893                "case 1: x = 1; break;\n"
894                "case 2: return;\n"
895                "case 3:\n"
896                "case 4:\n"
897                "case 5: return;\n"
898                "case 6: // comment\n"
899                "  return;\n"
900                "case 7:\n"
901                "  // comment\n"
902                "  return;\n"
903                "case 8:\n"
904                "  x = 8; // comment\n"
905                "  break;\n"
906                "default: y = 1; break;\n"
907                "}",
908                Style);
909   verifyFormat("switch (a) {\n"
910                "#if FOO\n"
911                "case 0: return 0;\n"
912                "#endif\n"
913                "}",
914                Style);
915   verifyFormat("switch (a) {\n"
916                "case 1: {\n"
917                "}\n"
918                "case 2: {\n"
919                "  return;\n"
920                "}\n"
921                "case 3: {\n"
922                "  x = 1;\n"
923                "  return;\n"
924                "}\n"
925                "case 4:\n"
926                "  if (x)\n"
927                "    return;\n"
928                "}",
929                Style);
930   Style.ColumnLimit = 21;
931   verifyFormat("switch (a) {\n"
932                "case 1: x = 1; break;\n"
933                "case 2: return;\n"
934                "case 3:\n"
935                "case 4:\n"
936                "case 5: return;\n"
937                "default:\n"
938                "  y = 1;\n"
939                "  break;\n"
940                "}",
941                Style);
942 }
943 
944 TEST_F(FormatTest, FormatsLabels) {
945   verifyFormat("void f() {\n"
946                "  some_code();\n"
947                "test_label:\n"
948                "  some_other_code();\n"
949                "  {\n"
950                "    some_more_code();\n"
951                "  another_label:\n"
952                "    some_more_code();\n"
953                "  }\n"
954                "}");
955   verifyFormat("{\n"
956                "  some_code();\n"
957                "test_label:\n"
958                "  some_other_code();\n"
959                "}");
960   verifyFormat("{\n"
961                "  some_code();\n"
962                "test_label:;\n"
963                "  int i = 0;\n"
964                "}");
965 }
966 
967 //===----------------------------------------------------------------------===//
968 // Tests for classes, namespaces, etc.
969 //===----------------------------------------------------------------------===//
970 
971 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
972   verifyFormat("class A {};");
973 }
974 
975 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
976   verifyFormat("class A {\n"
977                "public:\n"
978                "public: // comment\n"
979                "protected:\n"
980                "private:\n"
981                "  void f() {}\n"
982                "};");
983   verifyGoogleFormat("class A {\n"
984                      " public:\n"
985                      " protected:\n"
986                      " private:\n"
987                      "  void f() {}\n"
988                      "};");
989   verifyFormat("class A {\n"
990                "public slots:\n"
991                "  void f1() {}\n"
992                "public Q_SLOTS:\n"
993                "  void f2() {}\n"
994                "protected slots:\n"
995                "  void f3() {}\n"
996                "protected Q_SLOTS:\n"
997                "  void f4() {}\n"
998                "private slots:\n"
999                "  void f5() {}\n"
1000                "private Q_SLOTS:\n"
1001                "  void f6() {}\n"
1002                "signals:\n"
1003                "  void g1();\n"
1004                "Q_SIGNALS:\n"
1005                "  void g2();\n"
1006                "};");
1007 
1008   // Don't interpret 'signals' the wrong way.
1009   verifyFormat("signals.set();");
1010   verifyFormat("for (Signals signals : f()) {\n}");
1011   verifyFormat("{\n"
1012                "  signals.set(); // This needs indentation.\n"
1013                "}");
1014   verifyFormat("void f() {\n"
1015                "label:\n"
1016                "  signals.baz();\n"
1017                "}");
1018 }
1019 
1020 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1021   EXPECT_EQ("class A {\n"
1022             "public:\n"
1023             "  void f();\n"
1024             "\n"
1025             "private:\n"
1026             "  void g() {}\n"
1027             "  // test\n"
1028             "protected:\n"
1029             "  int h;\n"
1030             "};",
1031             format("class A {\n"
1032                    "public:\n"
1033                    "void f();\n"
1034                    "private:\n"
1035                    "void g() {}\n"
1036                    "// test\n"
1037                    "protected:\n"
1038                    "int h;\n"
1039                    "};"));
1040   EXPECT_EQ("class A {\n"
1041             "protected:\n"
1042             "public:\n"
1043             "  void f();\n"
1044             "};",
1045             format("class A {\n"
1046                    "protected:\n"
1047                    "\n"
1048                    "public:\n"
1049                    "\n"
1050                    "  void f();\n"
1051                    "};"));
1052 
1053   // Even ensure proper spacing inside macros.
1054   EXPECT_EQ("#define B     \\\n"
1055             "  class A {   \\\n"
1056             "   protected: \\\n"
1057             "   public:    \\\n"
1058             "    void f(); \\\n"
1059             "  };",
1060             format("#define B     \\\n"
1061                    "  class A {   \\\n"
1062                    "   protected: \\\n"
1063                    "              \\\n"
1064                    "   public:    \\\n"
1065                    "              \\\n"
1066                    "    void f(); \\\n"
1067                    "  };",
1068                    getGoogleStyle()));
1069   // But don't remove empty lines after macros ending in access specifiers.
1070   EXPECT_EQ("#define A private:\n"
1071             "\n"
1072             "int i;",
1073             format("#define A         private:\n"
1074                    "\n"
1075                    "int              i;"));
1076 }
1077 
1078 TEST_F(FormatTest, FormatsClasses) {
1079   verifyFormat("class A : public B {};");
1080   verifyFormat("class A : public ::B {};");
1081 
1082   verifyFormat(
1083       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1084       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1085   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1086                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1087                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1088   verifyFormat(
1089       "class A : public B, public C, public D, public E, public F {};");
1090   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1091                "                     public C,\n"
1092                "                     public D,\n"
1093                "                     public E,\n"
1094                "                     public F,\n"
1095                "                     public G {};");
1096 
1097   verifyFormat("class\n"
1098                "    ReallyReallyLongClassName {\n"
1099                "  int i;\n"
1100                "};",
1101                getLLVMStyleWithColumns(32));
1102   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1103                "                           aaaaaaaaaaaaaaaa> {};");
1104   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1105                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1106                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1107   verifyFormat("template <class R, class C>\n"
1108                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1109                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1110   verifyFormat("class ::A::B {};");
1111 }
1112 
1113 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1114   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1115   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1116 
1117   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1118   verifyFormat("class MyClass\n"
1119                "    : public X\n"
1120                "    , public Y {};",
1121                StyleWithInheritanceBreak);
1122 }
1123 
1124 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1125   verifyFormat("class A {\n} a, b;");
1126   verifyFormat("struct A {\n} a, b;");
1127   verifyFormat("union A {\n} a;");
1128 }
1129 
1130 TEST_F(FormatTest, FormatsEnum) {
1131   verifyFormat("enum {\n"
1132                "  Zero,\n"
1133                "  One = 1,\n"
1134                "  Two = One + 1,\n"
1135                "  Three = (One + Two),\n"
1136                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1137                "  Five = (One, Two, Three, Four, 5)\n"
1138                "};");
1139   verifyGoogleFormat("enum {\n"
1140                      "  Zero,\n"
1141                      "  One = 1,\n"
1142                      "  Two = One + 1,\n"
1143                      "  Three = (One + Two),\n"
1144                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1145                      "  Five = (One, Two, Three, Four, 5)\n"
1146                      "};");
1147   verifyFormat("enum Enum {};");
1148   verifyFormat("enum {};");
1149   verifyFormat("enum X E {} d;");
1150   verifyFormat("enum __attribute__((...)) E {} d;");
1151   verifyFormat("enum __declspec__((...)) E {} d;");
1152   verifyFormat("enum {\n"
1153                "  Bar = Foo<int, int>::value\n"
1154                "};",
1155                getLLVMStyleWithColumns(30));
1156 
1157   verifyFormat("enum ShortEnum { A, B, C };");
1158   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1159 
1160   EXPECT_EQ("enum KeepEmptyLines {\n"
1161             "  ONE,\n"
1162             "\n"
1163             "  TWO,\n"
1164             "\n"
1165             "  THREE\n"
1166             "}",
1167             format("enum KeepEmptyLines {\n"
1168                    "  ONE,\n"
1169                    "\n"
1170                    "  TWO,\n"
1171                    "\n"
1172                    "\n"
1173                    "  THREE\n"
1174                    "}"));
1175   verifyFormat("enum E { // comment\n"
1176                "  ONE,\n"
1177                "  TWO\n"
1178                "};\n"
1179                "int i;");
1180   // Not enums.
1181   verifyFormat("enum X f() {\n"
1182                "  a();\n"
1183                "  return 42;\n"
1184                "}");
1185   verifyFormat("enum X Type::f() {\n"
1186                "  a();\n"
1187                "  return 42;\n"
1188                "}");
1189   verifyFormat("enum ::X f() {\n"
1190                "  a();\n"
1191                "  return 42;\n"
1192                "}");
1193   verifyFormat("enum ns::X f() {\n"
1194                "  a();\n"
1195                "  return 42;\n"
1196                "}");
1197 }
1198 
1199 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1200   verifyFormat("enum Type {\n"
1201                "  One = 0; // These semicolons should be commas.\n"
1202                "  Two = 1;\n"
1203                "};");
1204   verifyFormat("namespace n {\n"
1205                "enum Type {\n"
1206                "  One,\n"
1207                "  Two, // missing };\n"
1208                "  int i;\n"
1209                "}\n"
1210                "void g() {}");
1211 }
1212 
1213 TEST_F(FormatTest, FormatsEnumStruct) {
1214   verifyFormat("enum struct {\n"
1215                "  Zero,\n"
1216                "  One = 1,\n"
1217                "  Two = One + 1,\n"
1218                "  Three = (One + Two),\n"
1219                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1220                "  Five = (One, Two, Three, Four, 5)\n"
1221                "};");
1222   verifyFormat("enum struct Enum {};");
1223   verifyFormat("enum struct {};");
1224   verifyFormat("enum struct X E {} d;");
1225   verifyFormat("enum struct __attribute__((...)) E {} d;");
1226   verifyFormat("enum struct __declspec__((...)) E {} d;");
1227   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1228 }
1229 
1230 TEST_F(FormatTest, FormatsEnumClass) {
1231   verifyFormat("enum class {\n"
1232                "  Zero,\n"
1233                "  One = 1,\n"
1234                "  Two = One + 1,\n"
1235                "  Three = (One + Two),\n"
1236                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1237                "  Five = (One, Two, Three, Four, 5)\n"
1238                "};");
1239   verifyFormat("enum class Enum {};");
1240   verifyFormat("enum class {};");
1241   verifyFormat("enum class X E {} d;");
1242   verifyFormat("enum class __attribute__((...)) E {} d;");
1243   verifyFormat("enum class __declspec__((...)) E {} d;");
1244   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1245 }
1246 
1247 TEST_F(FormatTest, FormatsEnumTypes) {
1248   verifyFormat("enum X : int {\n"
1249                "  A, // Force multiple lines.\n"
1250                "  B\n"
1251                "};");
1252   verifyFormat("enum X : int { A, B };");
1253   verifyFormat("enum X : std::uint32_t { A, B };");
1254 }
1255 
1256 TEST_F(FormatTest, FormatsNSEnums) {
1257   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1258   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1259                      "  // Information about someDecentlyLongValue.\n"
1260                      "  someDecentlyLongValue,\n"
1261                      "  // Information about anotherDecentlyLongValue.\n"
1262                      "  anotherDecentlyLongValue,\n"
1263                      "  // Information about aThirdDecentlyLongValue.\n"
1264                      "  aThirdDecentlyLongValue\n"
1265                      "};");
1266   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1267                      "  a = 1,\n"
1268                      "  b = 2,\n"
1269                      "  c = 3,\n"
1270                      "};");
1271   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1272                      "  a = 1,\n"
1273                      "  b = 2,\n"
1274                      "  c = 3,\n"
1275                      "};");
1276   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1277                      "  a = 1,\n"
1278                      "  b = 2,\n"
1279                      "  c = 3,\n"
1280                      "};");
1281 }
1282 
1283 TEST_F(FormatTest, FormatsBitfields) {
1284   verifyFormat("struct Bitfields {\n"
1285                "  unsigned sClass : 8;\n"
1286                "  unsigned ValueKind : 2;\n"
1287                "};");
1288   verifyFormat("struct A {\n"
1289                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1290                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1291                "};");
1292   verifyFormat("struct MyStruct {\n"
1293                "  uchar data;\n"
1294                "  uchar : 8;\n"
1295                "  uchar : 8;\n"
1296                "  uchar other;\n"
1297                "};");
1298 }
1299 
1300 TEST_F(FormatTest, FormatsNamespaces) {
1301   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1302   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1303 
1304   verifyFormat("namespace some_namespace {\n"
1305                "class A {};\n"
1306                "void f() { f(); }\n"
1307                "}",
1308                LLVMWithNoNamespaceFix);
1309   verifyFormat("namespace {\n"
1310                "class A {};\n"
1311                "void f() { f(); }\n"
1312                "}",
1313                LLVMWithNoNamespaceFix);
1314   verifyFormat("inline namespace X {\n"
1315                "class A {};\n"
1316                "void f() { f(); }\n"
1317                "}",
1318                LLVMWithNoNamespaceFix);
1319   verifyFormat("using namespace some_namespace;\n"
1320                "class A {};\n"
1321                "void f() { f(); }",
1322                LLVMWithNoNamespaceFix);
1323 
1324   // This code is more common than we thought; if we
1325   // layout this correctly the semicolon will go into
1326   // its own line, which is undesirable.
1327   verifyFormat("namespace {};",
1328                LLVMWithNoNamespaceFix);
1329   verifyFormat("namespace {\n"
1330                "class A {};\n"
1331                "};",
1332                LLVMWithNoNamespaceFix);
1333 
1334   verifyFormat("namespace {\n"
1335                "int SomeVariable = 0; // comment\n"
1336                "} // namespace",
1337                LLVMWithNoNamespaceFix);
1338   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1339             "#define HEADER_GUARD\n"
1340             "namespace my_namespace {\n"
1341             "int i;\n"
1342             "} // my_namespace\n"
1343             "#endif // HEADER_GUARD",
1344             format("#ifndef HEADER_GUARD\n"
1345                    " #define HEADER_GUARD\n"
1346                    "   namespace my_namespace {\n"
1347                    "int i;\n"
1348                    "}    // my_namespace\n"
1349                    "#endif    // HEADER_GUARD",
1350                    LLVMWithNoNamespaceFix));
1351 
1352   EXPECT_EQ("namespace A::B {\n"
1353             "class C {};\n"
1354             "}",
1355             format("namespace A::B {\n"
1356                    "class C {};\n"
1357                    "}",
1358                    LLVMWithNoNamespaceFix));
1359 
1360   FormatStyle Style = getLLVMStyle();
1361   Style.NamespaceIndentation = FormatStyle::NI_All;
1362   EXPECT_EQ("namespace out {\n"
1363             "  int i;\n"
1364             "  namespace in {\n"
1365             "    int i;\n"
1366             "  } // namespace in\n"
1367             "} // namespace out",
1368             format("namespace out {\n"
1369                    "int i;\n"
1370                    "namespace in {\n"
1371                    "int i;\n"
1372                    "} // namespace in\n"
1373                    "} // namespace out",
1374                    Style));
1375 
1376   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1377   EXPECT_EQ("namespace out {\n"
1378             "int i;\n"
1379             "namespace in {\n"
1380             "  int i;\n"
1381             "} // namespace in\n"
1382             "} // namespace out",
1383             format("namespace out {\n"
1384                    "int i;\n"
1385                    "namespace in {\n"
1386                    "int i;\n"
1387                    "} // namespace in\n"
1388                    "} // namespace out",
1389                    Style));
1390 }
1391 
1392 TEST_F(FormatTest, FormatsCompactNamespaces) {
1393   FormatStyle Style = getLLVMStyle();
1394   Style.CompactNamespaces = true;
1395 
1396   verifyFormat("namespace A { namespace B {\n"
1397 			   "}} // namespace A::B",
1398 			   Style);
1399 
1400   EXPECT_EQ("namespace out { namespace in {\n"
1401             "}} // namespace out::in",
1402             format("namespace out {\n"
1403                    "namespace in {\n"
1404                    "} // namespace in\n"
1405                    "} // namespace out",
1406                    Style));
1407 
1408   // Only namespaces which have both consecutive opening and end get compacted
1409   EXPECT_EQ("namespace out {\n"
1410             "namespace in1 {\n"
1411             "} // namespace in1\n"
1412             "namespace in2 {\n"
1413             "} // namespace in2\n"
1414             "} // namespace out",
1415             format("namespace out {\n"
1416                    "namespace in1 {\n"
1417                    "} // namespace in1\n"
1418                    "namespace in2 {\n"
1419                    "} // namespace in2\n"
1420                    "} // namespace out",
1421                    Style));
1422 
1423   EXPECT_EQ("namespace out {\n"
1424             "int i;\n"
1425             "namespace in {\n"
1426             "int j;\n"
1427             "} // namespace in\n"
1428             "int k;\n"
1429             "} // namespace out",
1430             format("namespace out { int i;\n"
1431                    "namespace in { int j; } // namespace in\n"
1432                    "int k; } // namespace out",
1433                    Style));
1434 
1435   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1436             "}}} // namespace A::B::C\n",
1437             format("namespace A { namespace B {\n"
1438                    "namespace C {\n"
1439                    "}} // namespace B::C\n"
1440                    "} // namespace A\n",
1441                    Style));
1442 
1443   Style.ColumnLimit = 40;
1444   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1445             "namespace bbbbbbbbbb {\n"
1446             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1447             format("namespace aaaaaaaaaa {\n"
1448                    "namespace bbbbbbbbbb {\n"
1449                    "} // namespace bbbbbbbbbb\n"
1450                    "} // namespace aaaaaaaaaa",
1451                    Style));
1452 
1453   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1454             "namespace cccccc {\n"
1455             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1456             format("namespace aaaaaa {\n"
1457                    "namespace bbbbbb {\n"
1458                    "namespace cccccc {\n"
1459                    "} // namespace cccccc\n"
1460                    "} // namespace bbbbbb\n"
1461                    "} // namespace aaaaaa",
1462                    Style));
1463   Style.ColumnLimit = 80;
1464 
1465   // Extra semicolon after 'inner' closing brace prevents merging
1466   EXPECT_EQ("namespace out { namespace in {\n"
1467             "}; } // namespace out::in",
1468             format("namespace out {\n"
1469                    "namespace in {\n"
1470                    "}; // namespace in\n"
1471                    "} // namespace out",
1472                    Style));
1473 
1474   // Extra semicolon after 'outer' closing brace is conserved
1475   EXPECT_EQ("namespace out { namespace in {\n"
1476             "}}; // namespace out::in",
1477             format("namespace out {\n"
1478                    "namespace in {\n"
1479                    "} // namespace in\n"
1480                    "}; // namespace out",
1481                    Style));
1482 
1483   Style.NamespaceIndentation = FormatStyle::NI_All;
1484   EXPECT_EQ("namespace out { namespace in {\n"
1485             "  int i;\n"
1486             "}} // namespace out::in",
1487             format("namespace out {\n"
1488                    "namespace in {\n"
1489                    "int i;\n"
1490                    "} // namespace in\n"
1491                    "} // namespace out",
1492                    Style));
1493   EXPECT_EQ("namespace out { namespace mid {\n"
1494             "  namespace in {\n"
1495             "    int j;\n"
1496             "  } // namespace in\n"
1497             "  int k;\n"
1498             "}} // namespace out::mid",
1499             format("namespace out { namespace mid {\n"
1500                    "namespace in { int j; } // namespace in\n"
1501                    "int k; }} // namespace out::mid",
1502                    Style));
1503 
1504   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1505   EXPECT_EQ("namespace out { namespace in {\n"
1506             "  int i;\n"
1507             "}} // namespace out::in",
1508             format("namespace out {\n"
1509                    "namespace in {\n"
1510                    "int i;\n"
1511                    "} // namespace in\n"
1512                    "} // namespace out",
1513                    Style));
1514   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1515             "  int i;\n"
1516             "}}} // namespace out::mid::in",
1517             format("namespace out {\n"
1518                    "namespace mid {\n"
1519                    "namespace in {\n"
1520                    "int i;\n"
1521                    "} // namespace in\n"
1522                    "} // namespace mid\n"
1523                    "} // namespace out",
1524                    Style));
1525 }
1526 
1527 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1528 
1529 TEST_F(FormatTest, FormatsInlineASM) {
1530   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1531   verifyFormat("asm(\"nop\" ::: \"memory\");");
1532   verifyFormat(
1533       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1534       "    \"cpuid\\n\\t\"\n"
1535       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1536       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1537       "    : \"a\"(value));");
1538   EXPECT_EQ(
1539       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1540       "  __asm {\n"
1541       "        mov     edx,[that] // vtable in edx\n"
1542       "        mov     eax,methodIndex\n"
1543       "        call    [edx][eax*4] // stdcall\n"
1544       "  }\n"
1545       "}",
1546       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1547              "    __asm {\n"
1548              "        mov     edx,[that] // vtable in edx\n"
1549              "        mov     eax,methodIndex\n"
1550              "        call    [edx][eax*4] // stdcall\n"
1551              "    }\n"
1552              "}"));
1553   EXPECT_EQ("_asm {\n"
1554             "  xor eax, eax;\n"
1555             "  cpuid;\n"
1556             "}",
1557             format("_asm {\n"
1558                    "  xor eax, eax;\n"
1559                    "  cpuid;\n"
1560                    "}"));
1561   verifyFormat("void function() {\n"
1562                "  // comment\n"
1563                "  asm(\"\");\n"
1564                "}");
1565   EXPECT_EQ("__asm {\n"
1566             "}\n"
1567             "int i;",
1568             format("__asm   {\n"
1569                    "}\n"
1570                    "int   i;"));
1571 }
1572 
1573 TEST_F(FormatTest, FormatTryCatch) {
1574   verifyFormat("try {\n"
1575                "  throw a * b;\n"
1576                "} catch (int a) {\n"
1577                "  // Do nothing.\n"
1578                "} catch (...) {\n"
1579                "  exit(42);\n"
1580                "}");
1581 
1582   // Function-level try statements.
1583   verifyFormat("int f() try { return 4; } catch (...) {\n"
1584                "  return 5;\n"
1585                "}");
1586   verifyFormat("class A {\n"
1587                "  int a;\n"
1588                "  A() try : a(0) {\n"
1589                "  } catch (...) {\n"
1590                "    throw;\n"
1591                "  }\n"
1592                "};\n");
1593 
1594   // Incomplete try-catch blocks.
1595   verifyIncompleteFormat("try {} catch (");
1596 }
1597 
1598 TEST_F(FormatTest, FormatSEHTryCatch) {
1599   verifyFormat("__try {\n"
1600                "  int a = b * c;\n"
1601                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1602                "  // Do nothing.\n"
1603                "}");
1604 
1605   verifyFormat("__try {\n"
1606                "  int a = b * c;\n"
1607                "} __finally {\n"
1608                "  // Do nothing.\n"
1609                "}");
1610 
1611   verifyFormat("DEBUG({\n"
1612                "  __try {\n"
1613                "  } __finally {\n"
1614                "  }\n"
1615                "});\n");
1616 }
1617 
1618 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1619   verifyFormat("try {\n"
1620                "  f();\n"
1621                "} catch {\n"
1622                "  g();\n"
1623                "}");
1624   verifyFormat("try {\n"
1625                "  f();\n"
1626                "} catch (A a) MACRO(x) {\n"
1627                "  g();\n"
1628                "} catch (B b) MACRO(x) {\n"
1629                "  g();\n"
1630                "}");
1631 }
1632 
1633 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1634   FormatStyle Style = getLLVMStyle();
1635   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1636                           FormatStyle::BS_WebKit}) {
1637     Style.BreakBeforeBraces = BraceStyle;
1638     verifyFormat("try {\n"
1639                  "  // something\n"
1640                  "} catch (...) {\n"
1641                  "  // something\n"
1642                  "}",
1643                  Style);
1644   }
1645   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1646   verifyFormat("try {\n"
1647                "  // something\n"
1648                "}\n"
1649                "catch (...) {\n"
1650                "  // something\n"
1651                "}",
1652                Style);
1653   verifyFormat("__try {\n"
1654                "  // something\n"
1655                "}\n"
1656                "__finally {\n"
1657                "  // something\n"
1658                "}",
1659                Style);
1660   verifyFormat("@try {\n"
1661                "  // something\n"
1662                "}\n"
1663                "@finally {\n"
1664                "  // something\n"
1665                "}",
1666                Style);
1667   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1668   verifyFormat("try\n"
1669                "{\n"
1670                "  // something\n"
1671                "}\n"
1672                "catch (...)\n"
1673                "{\n"
1674                "  // something\n"
1675                "}",
1676                Style);
1677   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1678   verifyFormat("try\n"
1679                "  {\n"
1680                "    // something\n"
1681                "  }\n"
1682                "catch (...)\n"
1683                "  {\n"
1684                "    // something\n"
1685                "  }",
1686                Style);
1687   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1688   Style.BraceWrapping.BeforeCatch = true;
1689   verifyFormat("try {\n"
1690                "  // something\n"
1691                "}\n"
1692                "catch (...) {\n"
1693                "  // something\n"
1694                "}",
1695                Style);
1696 }
1697 
1698 TEST_F(FormatTest, StaticInitializers) {
1699   verifyFormat("static SomeClass SC = {1, 'a'};");
1700 
1701   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1702                "    100000000, "
1703                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1704 
1705   // Here, everything other than the "}" would fit on a line.
1706   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1707                "    10000000000000000000000000};");
1708   EXPECT_EQ("S s = {a,\n"
1709             "\n"
1710             "       b};",
1711             format("S s = {\n"
1712                    "  a,\n"
1713                    "\n"
1714                    "  b\n"
1715                    "};"));
1716 
1717   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1718   // line. However, the formatting looks a bit off and this probably doesn't
1719   // happen often in practice.
1720   verifyFormat("static int Variable[1] = {\n"
1721                "    {1000000000000000000000000000000000000}};",
1722                getLLVMStyleWithColumns(40));
1723 }
1724 
1725 TEST_F(FormatTest, DesignatedInitializers) {
1726   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1727   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1728                "                    .bbbbbbbbbb = 2,\n"
1729                "                    .cccccccccc = 3,\n"
1730                "                    .dddddddddd = 4,\n"
1731                "                    .eeeeeeeeee = 5};");
1732   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1733                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1734                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1735                "    .ccccccccccccccccccccccccccc = 3,\n"
1736                "    .ddddddddddddddddddddddddddd = 4,\n"
1737                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1738 
1739   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1740 
1741   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1742   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1743                "                    [2] = bbbbbbbbbb,\n"
1744                "                    [3] = cccccccccc,\n"
1745                "                    [4] = dddddddddd,\n"
1746                "                    [5] = eeeeeeeeee};");
1747   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1748                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1749                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1750                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1751                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1752                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1753 }
1754 
1755 TEST_F(FormatTest, NestedStaticInitializers) {
1756   verifyFormat("static A x = {{{}}};\n");
1757   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1758                "               {init1, init2, init3, init4}}};",
1759                getLLVMStyleWithColumns(50));
1760 
1761   verifyFormat("somes Status::global_reps[3] = {\n"
1762                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1763                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1764                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1765                getLLVMStyleWithColumns(60));
1766   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1767                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1768                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1769                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1770   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1771                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1772                "rect.fTop}};");
1773 
1774   verifyFormat(
1775       "SomeArrayOfSomeType a = {\n"
1776       "    {{1, 2, 3},\n"
1777       "     {1, 2, 3},\n"
1778       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1779       "      333333333333333333333333333333},\n"
1780       "     {1, 2, 3},\n"
1781       "     {1, 2, 3}}};");
1782   verifyFormat(
1783       "SomeArrayOfSomeType a = {\n"
1784       "    {{1, 2, 3}},\n"
1785       "    {{1, 2, 3}},\n"
1786       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1787       "      333333333333333333333333333333}},\n"
1788       "    {{1, 2, 3}},\n"
1789       "    {{1, 2, 3}}};");
1790 
1791   verifyFormat("struct {\n"
1792                "  unsigned bit;\n"
1793                "  const char *const name;\n"
1794                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1795                "                 {kOsWin, \"Windows\"},\n"
1796                "                 {kOsLinux, \"Linux\"},\n"
1797                "                 {kOsCrOS, \"Chrome OS\"}};");
1798   verifyFormat("struct {\n"
1799                "  unsigned bit;\n"
1800                "  const char *const name;\n"
1801                "} kBitsToOs[] = {\n"
1802                "    {kOsMac, \"Mac\"},\n"
1803                "    {kOsWin, \"Windows\"},\n"
1804                "    {kOsLinux, \"Linux\"},\n"
1805                "    {kOsCrOS, \"Chrome OS\"},\n"
1806                "};");
1807 }
1808 
1809 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1810   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1811                "                      \\\n"
1812                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1813 }
1814 
1815 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1816   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1817                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1818 
1819   // Do break defaulted and deleted functions.
1820   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1821                "    default;",
1822                getLLVMStyleWithColumns(40));
1823   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1824                "    delete;",
1825                getLLVMStyleWithColumns(40));
1826 }
1827 
1828 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1829   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1830                getLLVMStyleWithColumns(40));
1831   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1832                getLLVMStyleWithColumns(40));
1833   EXPECT_EQ("#define Q                              \\\n"
1834             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1835             "  \"aaaaaaaa.cpp\"",
1836             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1837                    getLLVMStyleWithColumns(40)));
1838 }
1839 
1840 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1841   EXPECT_EQ("# 123 \"A string literal\"",
1842             format("   #     123    \"A string literal\""));
1843 }
1844 
1845 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1846   EXPECT_EQ("#;", format("#;"));
1847   verifyFormat("#\n;\n;\n;");
1848 }
1849 
1850 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1851   EXPECT_EQ("#line 42 \"test\"\n",
1852             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1853   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1854                                     getLLVMStyleWithColumns(12)));
1855 }
1856 
1857 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1858   EXPECT_EQ("#line 42 \"test\"",
1859             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1860   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1861 }
1862 
1863 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1864   verifyFormat("#define A \\x20");
1865   verifyFormat("#define A \\ x20");
1866   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1867   verifyFormat("#define A ''");
1868   verifyFormat("#define A ''qqq");
1869   verifyFormat("#define A `qqq");
1870   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1871   EXPECT_EQ("const char *c = STRINGIFY(\n"
1872             "\\na : b);",
1873             format("const char * c = STRINGIFY(\n"
1874                    "\\na : b);"));
1875 
1876   verifyFormat("a\r\\");
1877   verifyFormat("a\v\\");
1878   verifyFormat("a\f\\");
1879 }
1880 
1881 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1882   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1883   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1884   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1885   // FIXME: We never break before the macro name.
1886   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1887 
1888   verifyFormat("#define A A\n#define A A");
1889   verifyFormat("#define A(X) A\n#define A A");
1890 
1891   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1892   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1893 }
1894 
1895 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1896   EXPECT_EQ("// somecomment\n"
1897             "#include \"a.h\"\n"
1898             "#define A(  \\\n"
1899             "    A, B)\n"
1900             "#include \"b.h\"\n"
1901             "// somecomment\n",
1902             format("  // somecomment\n"
1903                    "  #include \"a.h\"\n"
1904                    "#define A(A,\\\n"
1905                    "    B)\n"
1906                    "    #include \"b.h\"\n"
1907                    " // somecomment\n",
1908                    getLLVMStyleWithColumns(13)));
1909 }
1910 
1911 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1912 
1913 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1914   EXPECT_EQ("#define A    \\\n"
1915             "  c;         \\\n"
1916             "  e;\n"
1917             "f;",
1918             format("#define A c; e;\n"
1919                    "f;",
1920                    getLLVMStyleWithColumns(14)));
1921 }
1922 
1923 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1924 
1925 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1926   EXPECT_EQ("int x,\n"
1927             "#define A\n"
1928             "    y;",
1929             format("int x,\n#define A\ny;"));
1930 }
1931 
1932 TEST_F(FormatTest, HashInMacroDefinition) {
1933   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
1934   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1935   verifyFormat("#define A  \\\n"
1936                "  {        \\\n"
1937                "    f(#c); \\\n"
1938                "  }",
1939                getLLVMStyleWithColumns(11));
1940 
1941   verifyFormat("#define A(X)         \\\n"
1942                "  void function##X()",
1943                getLLVMStyleWithColumns(22));
1944 
1945   verifyFormat("#define A(a, b, c)   \\\n"
1946                "  void a##b##c()",
1947                getLLVMStyleWithColumns(22));
1948 
1949   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1950 }
1951 
1952 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1953   EXPECT_EQ("#define A (x)", format("#define A (x)"));
1954   EXPECT_EQ("#define A(x)", format("#define A(x)"));
1955 }
1956 
1957 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1958   EXPECT_EQ("#define A b;", format("#define A \\\n"
1959                                    "          \\\n"
1960                                    "  b;",
1961                                    getLLVMStyleWithColumns(25)));
1962   EXPECT_EQ("#define A \\\n"
1963             "          \\\n"
1964             "  a;      \\\n"
1965             "  b;",
1966             format("#define A \\\n"
1967                    "          \\\n"
1968                    "  a;      \\\n"
1969                    "  b;",
1970                    getLLVMStyleWithColumns(11)));
1971   EXPECT_EQ("#define A \\\n"
1972             "  a;      \\\n"
1973             "          \\\n"
1974             "  b;",
1975             format("#define A \\\n"
1976                    "  a;      \\\n"
1977                    "          \\\n"
1978                    "  b;",
1979                    getLLVMStyleWithColumns(11)));
1980 }
1981 
1982 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1983   verifyIncompleteFormat("#define A :");
1984   verifyFormat("#define SOMECASES  \\\n"
1985                "  case 1:          \\\n"
1986                "  case 2\n",
1987                getLLVMStyleWithColumns(20));
1988   verifyFormat("#define MACRO(a) \\\n"
1989                "  if (a)         \\\n"
1990                "    f();         \\\n"
1991                "  else           \\\n"
1992                "    g()",
1993                getLLVMStyleWithColumns(18));
1994   verifyFormat("#define A template <typename T>");
1995   verifyIncompleteFormat("#define STR(x) #x\n"
1996                          "f(STR(this_is_a_string_literal{));");
1997   verifyFormat("#pragma omp threadprivate( \\\n"
1998                "    y)), // expected-warning",
1999                getLLVMStyleWithColumns(28));
2000   verifyFormat("#d, = };");
2001   verifyFormat("#if \"a");
2002   verifyIncompleteFormat("({\n"
2003                          "#define b     \\\n"
2004                          "  }           \\\n"
2005                          "  a\n"
2006                          "a",
2007                          getLLVMStyleWithColumns(15));
2008   verifyFormat("#define A     \\\n"
2009                "  {           \\\n"
2010                "    {\n"
2011                "#define B     \\\n"
2012                "  }           \\\n"
2013                "  }",
2014                getLLVMStyleWithColumns(15));
2015   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2016   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2017   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2018   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2019 }
2020 
2021 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2022   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2023   EXPECT_EQ("class A : public QObject {\n"
2024             "  Q_OBJECT\n"
2025             "\n"
2026             "  A() {}\n"
2027             "};",
2028             format("class A  :  public QObject {\n"
2029                    "     Q_OBJECT\n"
2030                    "\n"
2031                    "  A() {\n}\n"
2032                    "}  ;"));
2033   EXPECT_EQ("MACRO\n"
2034             "/*static*/ int i;",
2035             format("MACRO\n"
2036                    " /*static*/ int   i;"));
2037   EXPECT_EQ("SOME_MACRO\n"
2038             "namespace {\n"
2039             "void f();\n"
2040             "} // namespace",
2041             format("SOME_MACRO\n"
2042                    "  namespace    {\n"
2043                    "void   f(  );\n"
2044                    "} // namespace"));
2045   // Only if the identifier contains at least 5 characters.
2046   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2047   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2048   // Only if everything is upper case.
2049   EXPECT_EQ("class A : public QObject {\n"
2050             "  Q_Object A() {}\n"
2051             "};",
2052             format("class A  :  public QObject {\n"
2053                    "     Q_Object\n"
2054                    "  A() {\n}\n"
2055                    "}  ;"));
2056 
2057   // Only if the next line can actually start an unwrapped line.
2058   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2059             format("SOME_WEIRD_LOG_MACRO\n"
2060                    "<< SomeThing;"));
2061 
2062   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2063                "(n, buffers))\n",
2064                getChromiumStyle(FormatStyle::LK_Cpp));
2065 }
2066 
2067 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2068   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2069             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2070             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2071             "class X {};\n"
2072             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2073             "int *createScopDetectionPass() { return 0; }",
2074             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2075                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2076                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2077                    "  class X {};\n"
2078                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2079                    "  int *createScopDetectionPass() { return 0; }"));
2080   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2081   // braces, so that inner block is indented one level more.
2082   EXPECT_EQ("int q() {\n"
2083             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2084             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2085             "  IPC_END_MESSAGE_MAP()\n"
2086             "}",
2087             format("int q() {\n"
2088                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2089                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2090                    "  IPC_END_MESSAGE_MAP()\n"
2091                    "}"));
2092 
2093   // Same inside macros.
2094   EXPECT_EQ("#define LIST(L) \\\n"
2095             "  L(A)          \\\n"
2096             "  L(B)          \\\n"
2097             "  L(C)",
2098             format("#define LIST(L) \\\n"
2099                    "  L(A) \\\n"
2100                    "  L(B) \\\n"
2101                    "  L(C)",
2102                    getGoogleStyle()));
2103 
2104   // These must not be recognized as macros.
2105   EXPECT_EQ("int q() {\n"
2106             "  f(x);\n"
2107             "  f(x) {}\n"
2108             "  f(x)->g();\n"
2109             "  f(x)->*g();\n"
2110             "  f(x).g();\n"
2111             "  f(x) = x;\n"
2112             "  f(x) += x;\n"
2113             "  f(x) -= x;\n"
2114             "  f(x) *= x;\n"
2115             "  f(x) /= x;\n"
2116             "  f(x) %= x;\n"
2117             "  f(x) &= x;\n"
2118             "  f(x) |= x;\n"
2119             "  f(x) ^= x;\n"
2120             "  f(x) >>= x;\n"
2121             "  f(x) <<= x;\n"
2122             "  f(x)[y].z();\n"
2123             "  LOG(INFO) << x;\n"
2124             "  ifstream(x) >> x;\n"
2125             "}\n",
2126             format("int q() {\n"
2127                    "  f(x)\n;\n"
2128                    "  f(x)\n {}\n"
2129                    "  f(x)\n->g();\n"
2130                    "  f(x)\n->*g();\n"
2131                    "  f(x)\n.g();\n"
2132                    "  f(x)\n = x;\n"
2133                    "  f(x)\n += x;\n"
2134                    "  f(x)\n -= x;\n"
2135                    "  f(x)\n *= x;\n"
2136                    "  f(x)\n /= x;\n"
2137                    "  f(x)\n %= x;\n"
2138                    "  f(x)\n &= x;\n"
2139                    "  f(x)\n |= x;\n"
2140                    "  f(x)\n ^= x;\n"
2141                    "  f(x)\n >>= x;\n"
2142                    "  f(x)\n <<= x;\n"
2143                    "  f(x)\n[y].z();\n"
2144                    "  LOG(INFO)\n << x;\n"
2145                    "  ifstream(x)\n >> x;\n"
2146                    "}\n"));
2147   EXPECT_EQ("int q() {\n"
2148             "  F(x)\n"
2149             "  if (1) {\n"
2150             "  }\n"
2151             "  F(x)\n"
2152             "  while (1) {\n"
2153             "  }\n"
2154             "  F(x)\n"
2155             "  G(x);\n"
2156             "  F(x)\n"
2157             "  try {\n"
2158             "    Q();\n"
2159             "  } catch (...) {\n"
2160             "  }\n"
2161             "}\n",
2162             format("int q() {\n"
2163                    "F(x)\n"
2164                    "if (1) {}\n"
2165                    "F(x)\n"
2166                    "while (1) {}\n"
2167                    "F(x)\n"
2168                    "G(x);\n"
2169                    "F(x)\n"
2170                    "try { Q(); } catch (...) {}\n"
2171                    "}\n"));
2172   EXPECT_EQ("class A {\n"
2173             "  A() : t(0) {}\n"
2174             "  A(int i) noexcept() : {}\n"
2175             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2176             "  try : t(0) {\n"
2177             "  } catch (...) {\n"
2178             "  }\n"
2179             "};",
2180             format("class A {\n"
2181                    "  A()\n : t(0) {}\n"
2182                    "  A(int i)\n noexcept() : {}\n"
2183                    "  A(X x)\n"
2184                    "  try : t(0) {} catch (...) {}\n"
2185                    "};"));
2186   EXPECT_EQ("class SomeClass {\n"
2187             "public:\n"
2188             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2189             "};",
2190             format("class SomeClass {\n"
2191                    "public:\n"
2192                    "  SomeClass()\n"
2193                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2194                    "};"));
2195   EXPECT_EQ("class SomeClass {\n"
2196             "public:\n"
2197             "  SomeClass()\n"
2198             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2199             "};",
2200             format("class SomeClass {\n"
2201                    "public:\n"
2202                    "  SomeClass()\n"
2203                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2204                    "};",
2205                    getLLVMStyleWithColumns(40)));
2206 
2207   verifyFormat("MACRO(>)");
2208 }
2209 
2210 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2211   verifyFormat("#define A \\\n"
2212                "  f({     \\\n"
2213                "    g();  \\\n"
2214                "  });",
2215                getLLVMStyleWithColumns(11));
2216 }
2217 
2218 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2219   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2220 }
2221 
2222 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2223   verifyFormat("{\n  { a #c; }\n}");
2224 }
2225 
2226 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2227   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2228             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2229   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2230             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2231 }
2232 
2233 TEST_F(FormatTest, EscapedNewlines) {
2234   EXPECT_EQ(
2235       "#define A \\\n  int i;  \\\n  int j;",
2236       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2237   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2238   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2239   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2240   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2241 }
2242 
2243 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2244   verifyFormat("#define A \\\n"
2245                "  int v(  \\\n"
2246                "      a); \\\n"
2247                "  int i;",
2248                getLLVMStyleWithColumns(11));
2249 }
2250 
2251 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2252   EXPECT_EQ(
2253       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2254       "                      \\\n"
2255       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2256       "\n"
2257       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2258       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2259       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2260              "\\\n"
2261              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2262              "  \n"
2263              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2264              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2265 }
2266 
2267 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2268   EXPECT_EQ("int\n"
2269             "#define A\n"
2270             "    a;",
2271             format("int\n#define A\na;"));
2272   verifyFormat("functionCallTo(\n"
2273                "    someOtherFunction(\n"
2274                "        withSomeParameters, whichInSequence,\n"
2275                "        areLongerThanALine(andAnotherCall,\n"
2276                "#define A B\n"
2277                "                           withMoreParamters,\n"
2278                "                           whichStronglyInfluenceTheLayout),\n"
2279                "        andMoreParameters),\n"
2280                "    trailing);",
2281                getLLVMStyleWithColumns(69));
2282   verifyFormat("Foo::Foo()\n"
2283                "#ifdef BAR\n"
2284                "    : baz(0)\n"
2285                "#endif\n"
2286                "{\n"
2287                "}");
2288   verifyFormat("void f() {\n"
2289                "  if (true)\n"
2290                "#ifdef A\n"
2291                "    f(42);\n"
2292                "  x();\n"
2293                "#else\n"
2294                "    g();\n"
2295                "  x();\n"
2296                "#endif\n"
2297                "}");
2298   verifyFormat("void f(param1, param2,\n"
2299                "       param3,\n"
2300                "#ifdef A\n"
2301                "       param4(param5,\n"
2302                "#ifdef A1\n"
2303                "              param6,\n"
2304                "#ifdef A2\n"
2305                "              param7),\n"
2306                "#else\n"
2307                "              param8),\n"
2308                "       param9,\n"
2309                "#endif\n"
2310                "       param10,\n"
2311                "#endif\n"
2312                "       param11)\n"
2313                "#else\n"
2314                "       param12)\n"
2315                "#endif\n"
2316                "{\n"
2317                "  x();\n"
2318                "}",
2319                getLLVMStyleWithColumns(28));
2320   verifyFormat("#if 1\n"
2321                "int i;");
2322   verifyFormat("#if 1\n"
2323                "#endif\n"
2324                "#if 1\n"
2325                "#else\n"
2326                "#endif\n");
2327   verifyFormat("DEBUG({\n"
2328                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2329                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2330                "});\n"
2331                "#if a\n"
2332                "#else\n"
2333                "#endif");
2334 
2335   verifyIncompleteFormat("void f(\n"
2336                          "#if A\n"
2337                          ");\n"
2338                          "#else\n"
2339                          "#endif");
2340 }
2341 
2342 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2343   verifyFormat("#endif\n"
2344                "#if B");
2345 }
2346 
2347 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2348   FormatStyle SingleLine = getLLVMStyle();
2349   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2350   verifyFormat("#if 0\n"
2351                "#elif 1\n"
2352                "#endif\n"
2353                "void foo() {\n"
2354                "  if (test) foo2();\n"
2355                "}",
2356                SingleLine);
2357 }
2358 
2359 TEST_F(FormatTest, LayoutBlockInsideParens) {
2360   verifyFormat("functionCall({ int i; });");
2361   verifyFormat("functionCall({\n"
2362                "  int i;\n"
2363                "  int j;\n"
2364                "});");
2365   verifyFormat("functionCall(\n"
2366                "    {\n"
2367                "      int i;\n"
2368                "      int j;\n"
2369                "    },\n"
2370                "    aaaa, bbbb, cccc);");
2371   verifyFormat("functionA(functionB({\n"
2372                "            int i;\n"
2373                "            int j;\n"
2374                "          }),\n"
2375                "          aaaa, bbbb, cccc);");
2376   verifyFormat("functionCall(\n"
2377                "    {\n"
2378                "      int i;\n"
2379                "      int j;\n"
2380                "    },\n"
2381                "    aaaa, bbbb, // comment\n"
2382                "    cccc);");
2383   verifyFormat("functionA(functionB({\n"
2384                "            int i;\n"
2385                "            int j;\n"
2386                "          }),\n"
2387                "          aaaa, bbbb, // comment\n"
2388                "          cccc);");
2389   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2390   verifyFormat("functionCall(aaaa, bbbb, {\n"
2391                "  int i;\n"
2392                "  int j;\n"
2393                "});");
2394   verifyFormat(
2395       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2396       "    {\n"
2397       "      int i; // break\n"
2398       "    },\n"
2399       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2400       "                                     ccccccccccccccccc));");
2401   verifyFormat("DEBUG({\n"
2402                "  if (a)\n"
2403                "    f();\n"
2404                "});");
2405 }
2406 
2407 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2408   EXPECT_EQ("SOME_MACRO { int i; }\n"
2409             "int i;",
2410             format("  SOME_MACRO  {int i;}  int i;"));
2411 }
2412 
2413 TEST_F(FormatTest, LayoutNestedBlocks) {
2414   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2415                "  struct s {\n"
2416                "    int i;\n"
2417                "  };\n"
2418                "  s kBitsToOs[] = {{10}};\n"
2419                "  for (int i = 0; i < 10; ++i)\n"
2420                "    return;\n"
2421                "}");
2422   verifyFormat("call(parameter, {\n"
2423                "  something();\n"
2424                "  // Comment using all columns.\n"
2425                "  somethingelse();\n"
2426                "});",
2427                getLLVMStyleWithColumns(40));
2428   verifyFormat("DEBUG( //\n"
2429                "    { f(); }, a);");
2430   verifyFormat("DEBUG( //\n"
2431                "    {\n"
2432                "      f(); //\n"
2433                "    },\n"
2434                "    a);");
2435 
2436   EXPECT_EQ("call(parameter, {\n"
2437             "  something();\n"
2438             "  // Comment too\n"
2439             "  // looooooooooong.\n"
2440             "  somethingElse();\n"
2441             "});",
2442             format("call(parameter, {\n"
2443                    "  something();\n"
2444                    "  // Comment too looooooooooong.\n"
2445                    "  somethingElse();\n"
2446                    "});",
2447                    getLLVMStyleWithColumns(29)));
2448   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2449   EXPECT_EQ("DEBUG({ // comment\n"
2450             "  int i;\n"
2451             "});",
2452             format("DEBUG({ // comment\n"
2453                    "int  i;\n"
2454                    "});"));
2455   EXPECT_EQ("DEBUG({\n"
2456             "  int i;\n"
2457             "\n"
2458             "  // comment\n"
2459             "  int j;\n"
2460             "});",
2461             format("DEBUG({\n"
2462                    "  int  i;\n"
2463                    "\n"
2464                    "  // comment\n"
2465                    "  int  j;\n"
2466                    "});"));
2467 
2468   verifyFormat("DEBUG({\n"
2469                "  if (a)\n"
2470                "    return;\n"
2471                "});");
2472   verifyGoogleFormat("DEBUG({\n"
2473                      "  if (a) return;\n"
2474                      "});");
2475   FormatStyle Style = getGoogleStyle();
2476   Style.ColumnLimit = 45;
2477   verifyFormat("Debug(aaaaa,\n"
2478                "      {\n"
2479                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2480                "      },\n"
2481                "      a);",
2482                Style);
2483 
2484   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2485 
2486   verifyNoCrash("^{v^{a}}");
2487 }
2488 
2489 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2490   EXPECT_EQ("#define MACRO()                     \\\n"
2491             "  Debug(aaa, /* force line break */ \\\n"
2492             "        {                           \\\n"
2493             "          int i;                    \\\n"
2494             "          int j;                    \\\n"
2495             "        })",
2496             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2497                    "          {  int   i;  int  j;   })",
2498                    getGoogleStyle()));
2499 
2500   EXPECT_EQ("#define A                                       \\\n"
2501             "  [] {                                          \\\n"
2502             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2503             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2504             "  }",
2505             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2506                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2507                    getGoogleStyle()));
2508 }
2509 
2510 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2511   EXPECT_EQ("{}", format("{}"));
2512   verifyFormat("enum E {};");
2513   verifyFormat("enum E {}");
2514 }
2515 
2516 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2517   FormatStyle Style = getLLVMStyle();
2518   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2519   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2520   verifyFormat("FOO_BEGIN\n"
2521                "  FOO_ENTRY\n"
2522                "FOO_END", Style);
2523   verifyFormat("FOO_BEGIN\n"
2524                "  NESTED_FOO_BEGIN\n"
2525                "    NESTED_FOO_ENTRY\n"
2526                "  NESTED_FOO_END\n"
2527                "FOO_END", Style);
2528   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2529                "  int x;\n"
2530                "  x = 1;\n"
2531                "FOO_END(Baz)", Style);
2532 }
2533 
2534 //===----------------------------------------------------------------------===//
2535 // Line break tests.
2536 //===----------------------------------------------------------------------===//
2537 
2538 TEST_F(FormatTest, PreventConfusingIndents) {
2539   verifyFormat(
2540       "void f() {\n"
2541       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2542       "                         parameter, parameter, parameter)),\n"
2543       "                     SecondLongCall(parameter));\n"
2544       "}");
2545   verifyFormat(
2546       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2547       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2548       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2549       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2550   verifyFormat(
2551       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2552       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2553       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2554       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2555   verifyFormat(
2556       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2557       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2558       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2559       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2560   verifyFormat("int a = bbbb && ccc &&\n"
2561                "        fffff(\n"
2562                "#define A Just forcing a new line\n"
2563                "            ddd);");
2564 }
2565 
2566 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2567   verifyFormat(
2568       "bool aaaaaaa =\n"
2569       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2570       "    bbbbbbbb();");
2571   verifyFormat(
2572       "bool aaaaaaa =\n"
2573       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2574       "    bbbbbbbb();");
2575 
2576   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2577                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2578                "    ccccccccc == ddddddddddd;");
2579   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2580                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2581                "    ccccccccc == ddddddddddd;");
2582   verifyFormat(
2583       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2584       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2585       "    ccccccccc == ddddddddddd;");
2586 
2587   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2588                "                 aaaaaa) &&\n"
2589                "         bbbbbb && cccccc;");
2590   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2591                "                 aaaaaa) >>\n"
2592                "         bbbbbb;");
2593   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2594                "    SourceMgr.getSpellingColumnNumber(\n"
2595                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2596                "    1);");
2597 
2598   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2599                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2600                "    cccccc) {\n}");
2601   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2602                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
2603                "              cccccc) {\n}");
2604   verifyFormat("b = a &&\n"
2605                "    // Comment\n"
2606                "    b.c && d;");
2607 
2608   // If the LHS of a comparison is not a binary expression itself, the
2609   // additional linebreak confuses many people.
2610   verifyFormat(
2611       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2612       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2613       "}");
2614   verifyFormat(
2615       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2616       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2617       "}");
2618   verifyFormat(
2619       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2620       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2621       "}");
2622   // Even explicit parentheses stress the precedence enough to make the
2623   // additional break unnecessary.
2624   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2625                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2626                "}");
2627   // This cases is borderline, but with the indentation it is still readable.
2628   verifyFormat(
2629       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2630       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2631       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2632       "}",
2633       getLLVMStyleWithColumns(75));
2634 
2635   // If the LHS is a binary expression, we should still use the additional break
2636   // as otherwise the formatting hides the operator precedence.
2637   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2638                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2639                "    5) {\n"
2640                "}");
2641 
2642   FormatStyle OnePerLine = getLLVMStyle();
2643   OnePerLine.BinPackParameters = false;
2644   verifyFormat(
2645       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2646       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2648       OnePerLine);
2649 
2650   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2651                "                .aaa(aaaaaaaaaaaaa) *\n"
2652                "            aaaaaaa +\n"
2653                "        aaaaaaa;",
2654                getLLVMStyleWithColumns(40));
2655 }
2656 
2657 TEST_F(FormatTest, ExpressionIndentation) {
2658   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2659                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2660                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2661                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2662                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2663                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2664                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2665                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2666                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2667   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2668                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2669                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2670                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2671   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2672                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2673                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2674                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2675   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2676                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2677                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2678                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2679   verifyFormat("if () {\n"
2680                "} else if (aaaaa && bbbbb > // break\n"
2681                "                        ccccc) {\n"
2682                "}");
2683   verifyFormat("if () {\n"
2684                "} else if (aaaaa &&\n"
2685                "           bbbbb > // break\n"
2686                "               ccccc &&\n"
2687                "           ddddd) {\n"
2688                "}");
2689 
2690   // Presence of a trailing comment used to change indentation of b.
2691   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2692                "       b;\n"
2693                "return aaaaaaaaaaaaaaaaaaa +\n"
2694                "       b; //",
2695                getLLVMStyleWithColumns(30));
2696 }
2697 
2698 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2699   // Not sure what the best system is here. Like this, the LHS can be found
2700   // immediately above an operator (everything with the same or a higher
2701   // indent). The RHS is aligned right of the operator and so compasses
2702   // everything until something with the same indent as the operator is found.
2703   // FIXME: Is this a good system?
2704   FormatStyle Style = getLLVMStyle();
2705   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
2706   verifyFormat(
2707       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2708       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2709       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2710       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2711       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2712       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2713       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2714       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2715       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
2716       Style);
2717   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2718                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2719                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2720                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2721                Style);
2722   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2723                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2724                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2725                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2726                Style);
2727   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2728                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2729                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2730                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2731                Style);
2732   verifyFormat("if () {\n"
2733                "} else if (aaaaa\n"
2734                "           && bbbbb // break\n"
2735                "                  > ccccc) {\n"
2736                "}",
2737                Style);
2738   verifyFormat("return (a)\n"
2739                "       // comment\n"
2740                "       + b;",
2741                Style);
2742   verifyFormat(
2743       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2744       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2745       "             + cc;",
2746       Style);
2747 
2748   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2749                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
2750                Style);
2751 
2752   // Forced by comments.
2753   verifyFormat(
2754       "unsigned ContentSize =\n"
2755       "    sizeof(int16_t)   // DWARF ARange version number\n"
2756       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2757       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2758       "    + sizeof(int8_t); // Segment Size (in bytes)");
2759 
2760   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2761                "       == boost::fusion::at_c<1>(iiii).second;",
2762                Style);
2763 
2764   Style.ColumnLimit = 60;
2765   verifyFormat("zzzzzzzzzz\n"
2766                "    = bbbbbbbbbbbbbbbbb\n"
2767                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
2768                Style);
2769 }
2770 
2771 TEST_F(FormatTest, EnforcedOperatorWraps) {
2772   // Here we'd like to wrap after the || operators, but a comment is forcing an
2773   // earlier wrap.
2774   verifyFormat("bool x = aaaaa //\n"
2775                "         || bbbbb\n"
2776                "         //\n"
2777                "         || cccc;");
2778 }
2779 
2780 TEST_F(FormatTest, NoOperandAlignment) {
2781   FormatStyle Style = getLLVMStyle();
2782   Style.AlignOperands = false;
2783   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
2784                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2785                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2786                Style);
2787   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2788   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2789                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2790                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2791                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2792                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2793                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2794                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2795                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2796                "        > ccccccccccccccccccccccccccccccccccccccccc;",
2797                Style);
2798 
2799   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2800                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2801                "    + cc;",
2802                Style);
2803   verifyFormat("int a = aa\n"
2804                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2805                "        * cccccccccccccccccccccccccccccccccccc;\n",
2806                Style);
2807 
2808   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
2809   verifyFormat("return (a > b\n"
2810                "    // comment1\n"
2811                "    // comment2\n"
2812                "    || c);",
2813                Style);
2814 }
2815 
2816 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
2817   FormatStyle Style = getLLVMStyle();
2818   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2819   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2820                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2821                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2822                Style);
2823 }
2824 
2825 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
2826   FormatStyle Style = getLLVMStyle();
2827   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2828   Style.BinPackArguments = false;
2829   Style.ColumnLimit = 40;
2830   verifyFormat("void test() {\n"
2831                "  someFunction(\n"
2832                "      this + argument + is + quite\n"
2833                "      + long + so + it + gets + wrapped\n"
2834                "      + but + remains + bin - packed);\n"
2835                "}",
2836                Style);
2837   verifyFormat("void test() {\n"
2838                "  someFunction(arg1,\n"
2839                "               this + argument + is\n"
2840                "                   + quite + long + so\n"
2841                "                   + it + gets + wrapped\n"
2842                "                   + but + remains + bin\n"
2843                "                   - packed,\n"
2844                "               arg3);\n"
2845                "}",
2846                Style);
2847   verifyFormat("void test() {\n"
2848                "  someFunction(\n"
2849                "      arg1,\n"
2850                "      this + argument + has\n"
2851                "          + anotherFunc(nested,\n"
2852                "                        calls + whose\n"
2853                "                            + arguments\n"
2854                "                            + are + also\n"
2855                "                            + wrapped,\n"
2856                "                        in + addition)\n"
2857                "          + to + being + bin - packed,\n"
2858                "      arg3);\n"
2859                "}",
2860                Style);
2861 
2862   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
2863   verifyFormat("void test() {\n"
2864                "  someFunction(\n"
2865                "      arg1,\n"
2866                "      this + argument + has +\n"
2867                "          anotherFunc(nested,\n"
2868                "                      calls + whose +\n"
2869                "                          arguments +\n"
2870                "                          are + also +\n"
2871                "                          wrapped,\n"
2872                "                      in + addition) +\n"
2873                "          to + being + bin - packed,\n"
2874                "      arg3);\n"
2875                "}",
2876                Style);
2877 }
2878 
2879 TEST_F(FormatTest, ConstructorInitializers) {
2880   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2881   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2882                getLLVMStyleWithColumns(45));
2883   verifyFormat("Constructor()\n"
2884                "    : Inttializer(FitsOnTheLine) {}",
2885                getLLVMStyleWithColumns(44));
2886   verifyFormat("Constructor()\n"
2887                "    : Inttializer(FitsOnTheLine) {}",
2888                getLLVMStyleWithColumns(43));
2889 
2890   verifyFormat("template <typename T>\n"
2891                "Constructor() : Initializer(FitsOnTheLine) {}",
2892                getLLVMStyleWithColumns(45));
2893 
2894   verifyFormat(
2895       "SomeClass::Constructor()\n"
2896       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2897 
2898   verifyFormat(
2899       "SomeClass::Constructor()\n"
2900       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2901       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2902   verifyFormat(
2903       "SomeClass::Constructor()\n"
2904       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2905       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2906   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2907                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2908                "    : aaaaaaaaaa(aaaaaa) {}");
2909 
2910   verifyFormat("Constructor()\n"
2911                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2912                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2913                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2914                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2915 
2916   verifyFormat("Constructor()\n"
2917                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2918                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2919 
2920   verifyFormat("Constructor(int Parameter = 0)\n"
2921                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2922                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2923   verifyFormat("Constructor()\n"
2924                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2925                "}",
2926                getLLVMStyleWithColumns(60));
2927   verifyFormat("Constructor()\n"
2928                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2929                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
2930 
2931   // Here a line could be saved by splitting the second initializer onto two
2932   // lines, but that is not desirable.
2933   verifyFormat("Constructor()\n"
2934                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2935                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2936                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2937 
2938   FormatStyle OnePerLine = getLLVMStyle();
2939   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2940   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2941   verifyFormat("SomeClass::Constructor()\n"
2942                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2943                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2944                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2945                OnePerLine);
2946   verifyFormat("SomeClass::Constructor()\n"
2947                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2948                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2949                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2950                OnePerLine);
2951   verifyFormat("MyClass::MyClass(int var)\n"
2952                "    : some_var_(var),            // 4 space indent\n"
2953                "      some_other_var_(var + 1) { // lined up\n"
2954                "}",
2955                OnePerLine);
2956   verifyFormat("Constructor()\n"
2957                "    : aaaaa(aaaaaa),\n"
2958                "      aaaaa(aaaaaa),\n"
2959                "      aaaaa(aaaaaa),\n"
2960                "      aaaaa(aaaaaa),\n"
2961                "      aaaaa(aaaaaa) {}",
2962                OnePerLine);
2963   verifyFormat("Constructor()\n"
2964                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2965                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2966                OnePerLine);
2967   OnePerLine.BinPackParameters = false;
2968   verifyFormat(
2969       "Constructor()\n"
2970       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2971       "          aaaaaaaaaaa().aaa(),\n"
2972       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2973       OnePerLine);
2974   OnePerLine.ColumnLimit = 60;
2975   verifyFormat("Constructor()\n"
2976                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
2977                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2978                OnePerLine);
2979 
2980   EXPECT_EQ("Constructor()\n"
2981             "    : // Comment forcing unwanted break.\n"
2982             "      aaaa(aaaa) {}",
2983             format("Constructor() :\n"
2984                    "    // Comment forcing unwanted break.\n"
2985                    "    aaaa(aaaa) {}"));
2986 }
2987 
2988 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
2989   FormatStyle Style = getLLVMStyle();
2990   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
2991 
2992   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2993   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
2994                getStyleWithColumns(Style, 45));
2995   verifyFormat("Constructor() :\n"
2996                "    Initializer(FitsOnTheLine) {}",
2997                getStyleWithColumns(Style, 44));
2998   verifyFormat("Constructor() :\n"
2999                "    Initializer(FitsOnTheLine) {}",
3000                getStyleWithColumns(Style, 43));
3001 
3002   verifyFormat("template <typename T>\n"
3003                "Constructor() : Initializer(FitsOnTheLine) {}",
3004                getStyleWithColumns(Style, 50));
3005 
3006   verifyFormat(
3007       "SomeClass::Constructor() :\n"
3008       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3009 	  Style);
3010 
3011   verifyFormat(
3012       "SomeClass::Constructor() :\n"
3013       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3014       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3015 	  Style);
3016   verifyFormat(
3017       "SomeClass::Constructor() :\n"
3018       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3019       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3020 	  Style);
3021   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3022                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3023                "    aaaaaaaaaa(aaaaaa) {}",
3024 			   Style);
3025 
3026   verifyFormat("Constructor() :\n"
3027                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3028                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3029                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3030                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3031 			   Style);
3032 
3033   verifyFormat("Constructor() :\n"
3034                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3035                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3036 			   Style);
3037 
3038   verifyFormat("Constructor(int Parameter = 0) :\n"
3039                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3040                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3041 			   Style);
3042   verifyFormat("Constructor() :\n"
3043                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3044                "}",
3045                getStyleWithColumns(Style, 60));
3046   verifyFormat("Constructor() :\n"
3047                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3048                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3049 			   Style);
3050 
3051   // Here a line could be saved by splitting the second initializer onto two
3052   // lines, but that is not desirable.
3053   verifyFormat("Constructor() :\n"
3054                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3055                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3056                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3057 			   Style);
3058 
3059   FormatStyle OnePerLine = Style;
3060   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3061   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3062   verifyFormat("SomeClass::Constructor() :\n"
3063                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3064                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3065                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3066                OnePerLine);
3067   verifyFormat("SomeClass::Constructor() :\n"
3068                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3069                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3070                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3071                OnePerLine);
3072   verifyFormat("MyClass::MyClass(int var) :\n"
3073                "    some_var_(var),            // 4 space indent\n"
3074                "    some_other_var_(var + 1) { // lined up\n"
3075                "}",
3076                OnePerLine);
3077   verifyFormat("Constructor() :\n"
3078                "    aaaaa(aaaaaa),\n"
3079                "    aaaaa(aaaaaa),\n"
3080                "    aaaaa(aaaaaa),\n"
3081                "    aaaaa(aaaaaa),\n"
3082                "    aaaaa(aaaaaa) {}",
3083                OnePerLine);
3084   verifyFormat("Constructor() :\n"
3085                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3086                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3087                OnePerLine);
3088   OnePerLine.BinPackParameters = false;
3089   verifyFormat(
3090       "Constructor() :\n"
3091       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3092       "        aaaaaaaaaaa().aaa(),\n"
3093       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3094       OnePerLine);
3095   OnePerLine.ColumnLimit = 60;
3096   verifyFormat("Constructor() :\n"
3097                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3098                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3099                OnePerLine);
3100 
3101   EXPECT_EQ("Constructor() :\n"
3102             "    // Comment forcing unwanted break.\n"
3103             "    aaaa(aaaa) {}",
3104             format("Constructor() :\n"
3105                    "    // Comment forcing unwanted break.\n"
3106                    "    aaaa(aaaa) {}",
3107 				   Style));
3108 
3109   Style.ColumnLimit = 0;
3110   verifyFormat("SomeClass::Constructor() :\n"
3111                "    a(a) {}",
3112                Style);
3113   verifyFormat("SomeClass::Constructor() noexcept :\n"
3114                "    a(a) {}",
3115                Style);
3116   verifyFormat("SomeClass::Constructor() :\n"
3117 			   "    a(a), b(b), c(c) {}",
3118                Style);
3119   verifyFormat("SomeClass::Constructor() :\n"
3120                "    a(a) {\n"
3121                "  foo();\n"
3122                "  bar();\n"
3123                "}",
3124                Style);
3125 
3126   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3127   verifyFormat("SomeClass::Constructor() :\n"
3128 			   "    a(a), b(b), c(c) {\n"
3129 			   "}",
3130                Style);
3131   verifyFormat("SomeClass::Constructor() :\n"
3132                "    a(a) {\n"
3133 			   "}",
3134                Style);
3135 
3136   Style.ColumnLimit = 80;
3137   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3138   Style.ConstructorInitializerIndentWidth = 2;
3139   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3140                Style);
3141   verifyFormat("SomeClass::Constructor() :\n"
3142                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3143                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3144                Style);
3145 }
3146 
3147 TEST_F(FormatTest, MemoizationTests) {
3148   // This breaks if the memoization lookup does not take \c Indent and
3149   // \c LastSpace into account.
3150   verifyFormat(
3151       "extern CFRunLoopTimerRef\n"
3152       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3153       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3154       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3155       "                     CFRunLoopTimerContext *context) {}");
3156 
3157   // Deep nesting somewhat works around our memoization.
3158   verifyFormat(
3159       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3160       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3161       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3162       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3163       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3164       getLLVMStyleWithColumns(65));
3165   verifyFormat(
3166       "aaaaa(\n"
3167       "    aaaaa,\n"
3168       "    aaaaa(\n"
3169       "        aaaaa,\n"
3170       "        aaaaa(\n"
3171       "            aaaaa,\n"
3172       "            aaaaa(\n"
3173       "                aaaaa,\n"
3174       "                aaaaa(\n"
3175       "                    aaaaa,\n"
3176       "                    aaaaa(\n"
3177       "                        aaaaa,\n"
3178       "                        aaaaa(\n"
3179       "                            aaaaa,\n"
3180       "                            aaaaa(\n"
3181       "                                aaaaa,\n"
3182       "                                aaaaa(\n"
3183       "                                    aaaaa,\n"
3184       "                                    aaaaa(\n"
3185       "                                        aaaaa,\n"
3186       "                                        aaaaa(\n"
3187       "                                            aaaaa,\n"
3188       "                                            aaaaa(\n"
3189       "                                                aaaaa,\n"
3190       "                                                aaaaa))))))))))));",
3191       getLLVMStyleWithColumns(65));
3192   verifyFormat(
3193       "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"
3194       "                                  a),\n"
3195       "                                a),\n"
3196       "                              a),\n"
3197       "                            a),\n"
3198       "                          a),\n"
3199       "                        a),\n"
3200       "                      a),\n"
3201       "                    a),\n"
3202       "                  a),\n"
3203       "                a),\n"
3204       "              a),\n"
3205       "            a),\n"
3206       "          a),\n"
3207       "        a),\n"
3208       "      a),\n"
3209       "    a),\n"
3210       "  a)",
3211       getLLVMStyleWithColumns(65));
3212 
3213   // This test takes VERY long when memoization is broken.
3214   FormatStyle OnePerLine = getLLVMStyle();
3215   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3216   OnePerLine.BinPackParameters = false;
3217   std::string input = "Constructor()\n"
3218                       "    : aaaa(a,\n";
3219   for (unsigned i = 0, e = 80; i != e; ++i) {
3220     input += "           a,\n";
3221   }
3222   input += "           a) {}";
3223   verifyFormat(input, OnePerLine);
3224 }
3225 
3226 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3227   verifyFormat(
3228       "void f() {\n"
3229       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3230       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3231       "    f();\n"
3232       "}");
3233   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3234                "    Intervals[i - 1].getRange().getLast()) {\n}");
3235 }
3236 
3237 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3238   // Principially, we break function declarations in a certain order:
3239   // 1) break amongst arguments.
3240   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3241                "                              Cccccccccccccc cccccccccccccc);");
3242   verifyFormat("template <class TemplateIt>\n"
3243                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3244                "                            TemplateIt *stop) {}");
3245 
3246   // 2) break after return type.
3247   verifyFormat(
3248       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3249       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3250       getGoogleStyle());
3251 
3252   // 3) break after (.
3253   verifyFormat(
3254       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3255       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3256       getGoogleStyle());
3257 
3258   // 4) break before after nested name specifiers.
3259   verifyFormat(
3260       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3261       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3262       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3263       getGoogleStyle());
3264 
3265   // However, there are exceptions, if a sufficient amount of lines can be
3266   // saved.
3267   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3268   // more adjusting.
3269   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3270                "                                  Cccccccccccccc cccccccccc,\n"
3271                "                                  Cccccccccccccc cccccccccc,\n"
3272                "                                  Cccccccccccccc cccccccccc,\n"
3273                "                                  Cccccccccccccc cccccccccc);");
3274   verifyFormat(
3275       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3276       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3277       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3278       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3279       getGoogleStyle());
3280   verifyFormat(
3281       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3282       "                                          Cccccccccccccc cccccccccc,\n"
3283       "                                          Cccccccccccccc cccccccccc,\n"
3284       "                                          Cccccccccccccc cccccccccc,\n"
3285       "                                          Cccccccccccccc cccccccccc,\n"
3286       "                                          Cccccccccccccc cccccccccc,\n"
3287       "                                          Cccccccccccccc cccccccccc);");
3288   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3289                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3290                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3291                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3292                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3293 
3294   // Break after multi-line parameters.
3295   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3296                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3297                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3298                "    bbbb bbbb);");
3299   verifyFormat("void SomeLoooooooooooongFunction(\n"
3300                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3301                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3302                "    int bbbbbbbbbbbbb);");
3303 
3304   // Treat overloaded operators like other functions.
3305   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3306                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3307   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3308                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3309   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3310                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3311   verifyGoogleFormat(
3312       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3313       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3314   verifyGoogleFormat(
3315       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3316       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3317   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3318                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3319   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3320                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3321   verifyGoogleFormat(
3322       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3323       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3324       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3325   verifyGoogleFormat(
3326       "template <typename T>\n"
3327       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3328       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3329       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3330 
3331   FormatStyle Style = getLLVMStyle();
3332   Style.PointerAlignment = FormatStyle::PAS_Left;
3333   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3334                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3335                Style);
3336   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3337                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3338                Style);
3339 }
3340 
3341 TEST_F(FormatTest, TrailingReturnType) {
3342   verifyFormat("auto foo() -> int;\n");
3343   verifyFormat("struct S {\n"
3344                "  auto bar() const -> int;\n"
3345                "};");
3346   verifyFormat("template <size_t Order, typename T>\n"
3347                "auto load_img(const std::string &filename)\n"
3348                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3349   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3350                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3351   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3352   verifyFormat("template <typename T>\n"
3353                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3354                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3355 
3356   // Not trailing return types.
3357   verifyFormat("void f() { auto a = b->c(); }");
3358 }
3359 
3360 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3361   // Avoid breaking before trailing 'const' or other trailing annotations, if
3362   // they are not function-like.
3363   FormatStyle Style = getGoogleStyle();
3364   Style.ColumnLimit = 47;
3365   verifyFormat("void someLongFunction(\n"
3366                "    int someLoooooooooooooongParameter) const {\n}",
3367                getLLVMStyleWithColumns(47));
3368   verifyFormat("LoooooongReturnType\n"
3369                "someLoooooooongFunction() const {}",
3370                getLLVMStyleWithColumns(47));
3371   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3372                "    const {}",
3373                Style);
3374   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3375                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3376   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3377                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3378   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3379                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3380   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3381                "                   aaaaaaaaaaa aaaaa) const override;");
3382   verifyGoogleFormat(
3383       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3384       "    const override;");
3385 
3386   // Even if the first parameter has to be wrapped.
3387   verifyFormat("void someLongFunction(\n"
3388                "    int someLongParameter) const {}",
3389                getLLVMStyleWithColumns(46));
3390   verifyFormat("void someLongFunction(\n"
3391                "    int someLongParameter) const {}",
3392                Style);
3393   verifyFormat("void someLongFunction(\n"
3394                "    int someLongParameter) override {}",
3395                Style);
3396   verifyFormat("void someLongFunction(\n"
3397                "    int someLongParameter) OVERRIDE {}",
3398                Style);
3399   verifyFormat("void someLongFunction(\n"
3400                "    int someLongParameter) final {}",
3401                Style);
3402   verifyFormat("void someLongFunction(\n"
3403                "    int someLongParameter) FINAL {}",
3404                Style);
3405   verifyFormat("void someLongFunction(\n"
3406                "    int parameter) const override {}",
3407                Style);
3408 
3409   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3410   verifyFormat("void someLongFunction(\n"
3411                "    int someLongParameter) const\n"
3412                "{\n"
3413                "}",
3414                Style);
3415 
3416   // Unless these are unknown annotations.
3417   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3418                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3419                "    LONG_AND_UGLY_ANNOTATION;");
3420 
3421   // Breaking before function-like trailing annotations is fine to keep them
3422   // close to their arguments.
3423   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3424                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3425   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3426                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3427   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3428                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3429   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3430                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3431   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3432 
3433   verifyFormat(
3434       "void aaaaaaaaaaaaaaaaaa()\n"
3435       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3436       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3437   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3438                "    __attribute__((unused));");
3439   verifyGoogleFormat(
3440       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3441       "    GUARDED_BY(aaaaaaaaaaaa);");
3442   verifyGoogleFormat(
3443       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3444       "    GUARDED_BY(aaaaaaaaaaaa);");
3445   verifyGoogleFormat(
3446       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3447       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3448   verifyGoogleFormat(
3449       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3450       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3451 }
3452 
3453 TEST_F(FormatTest, FunctionAnnotations) {
3454   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3455                "int OldFunction(const string &parameter) {}");
3456   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3457                "string OldFunction(const string &parameter) {}");
3458   verifyFormat("template <typename T>\n"
3459                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3460                "string OldFunction(const string &parameter) {}");
3461 
3462   // Not function annotations.
3463   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3464                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3465   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3466                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3467   verifyFormat("MACRO(abc).function() // wrap\n"
3468                "    << abc;");
3469   verifyFormat("MACRO(abc)->function() // wrap\n"
3470                "    << abc;");
3471   verifyFormat("MACRO(abc)::function() // wrap\n"
3472                "    << abc;");
3473 }
3474 
3475 TEST_F(FormatTest, BreaksDesireably) {
3476   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3477                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3478                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3479   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3480                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3481                "}");
3482 
3483   verifyFormat(
3484       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3485       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3486 
3487   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3488                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3489                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3490 
3491   verifyFormat(
3492       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3493       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3494       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3495       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3496       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3497 
3498   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3499                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3500 
3501   verifyFormat(
3502       "void f() {\n"
3503       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3504       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3505       "}");
3506   verifyFormat(
3507       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3508       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3509   verifyFormat(
3510       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3511       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3512   verifyFormat(
3513       "aaaaaa(aaa,\n"
3514       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3515       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3516       "       aaaa);");
3517   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3518                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3519                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3520 
3521   // Indent consistently independent of call expression and unary operator.
3522   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3523                "    dddddddddddddddddddddddddddddd));");
3524   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3525                "    dddddddddddddddddddddddddddddd));");
3526   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3527                "    dddddddddddddddddddddddddddddd));");
3528 
3529   // This test case breaks on an incorrect memoization, i.e. an optimization not
3530   // taking into account the StopAt value.
3531   verifyFormat(
3532       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3533       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3534       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3535       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3536 
3537   verifyFormat("{\n  {\n    {\n"
3538                "      Annotation.SpaceRequiredBefore =\n"
3539                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3540                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3541                "    }\n  }\n}");
3542 
3543   // Break on an outer level if there was a break on an inner level.
3544   EXPECT_EQ("f(g(h(a, // comment\n"
3545             "      b, c),\n"
3546             "    d, e),\n"
3547             "  x, y);",
3548             format("f(g(h(a, // comment\n"
3549                    "    b, c), d, e), x, y);"));
3550 
3551   // Prefer breaking similar line breaks.
3552   verifyFormat(
3553       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3554       "                             NSTrackingMouseEnteredAndExited |\n"
3555       "                             NSTrackingActiveAlways;");
3556 }
3557 
3558 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3559   FormatStyle NoBinPacking = getGoogleStyle();
3560   NoBinPacking.BinPackParameters = false;
3561   NoBinPacking.BinPackArguments = true;
3562   verifyFormat("void f() {\n"
3563                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3564                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3565                "}",
3566                NoBinPacking);
3567   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3568                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3569                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3570                NoBinPacking);
3571 
3572   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3573   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3574                "                        vector<int> bbbbbbbbbbbbbbb);",
3575                NoBinPacking);
3576   // FIXME: This behavior difference is probably not wanted. However, currently
3577   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3578   // template arguments from BreakBeforeParameter being set because of the
3579   // one-per-line formatting.
3580   verifyFormat(
3581       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3582       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3583       NoBinPacking);
3584   verifyFormat(
3585       "void fffffffffff(\n"
3586       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3587       "        aaaaaaaaaa);");
3588 }
3589 
3590 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3591   FormatStyle NoBinPacking = getGoogleStyle();
3592   NoBinPacking.BinPackParameters = false;
3593   NoBinPacking.BinPackArguments = false;
3594   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3595                "  aaaaaaaaaaaaaaaaaaaa,\n"
3596                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3597                NoBinPacking);
3598   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3599                "        aaaaaaaaaaaaa,\n"
3600                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3601                NoBinPacking);
3602   verifyFormat(
3603       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3604       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3605       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3606       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3607       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3608       NoBinPacking);
3609   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3610                "    .aaaaaaaaaaaaaaaaaa();",
3611                NoBinPacking);
3612   verifyFormat("void f() {\n"
3613                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3614                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3615                "}",
3616                NoBinPacking);
3617 
3618   verifyFormat(
3619       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3620       "             aaaaaaaaaaaa,\n"
3621       "             aaaaaaaaaaaa);",
3622       NoBinPacking);
3623   verifyFormat(
3624       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3625       "                               ddddddddddddddddddddddddddddd),\n"
3626       "             test);",
3627       NoBinPacking);
3628 
3629   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3630                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3631                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3632                "    aaaaaaaaaaaaaaaaaa;",
3633                NoBinPacking);
3634   verifyFormat("a(\"a\"\n"
3635                "  \"a\",\n"
3636                "  a);");
3637 
3638   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3639   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3640                "                aaaaaaaaa,\n"
3641                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3642                NoBinPacking);
3643   verifyFormat(
3644       "void f() {\n"
3645       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3646       "      .aaaaaaa();\n"
3647       "}",
3648       NoBinPacking);
3649   verifyFormat(
3650       "template <class SomeType, class SomeOtherType>\n"
3651       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3652       NoBinPacking);
3653 }
3654 
3655 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3656   FormatStyle Style = getLLVMStyleWithColumns(15);
3657   Style.ExperimentalAutoDetectBinPacking = true;
3658   EXPECT_EQ("aaa(aaaa,\n"
3659             "    aaaa,\n"
3660             "    aaaa);\n"
3661             "aaa(aaaa,\n"
3662             "    aaaa,\n"
3663             "    aaaa);",
3664             format("aaa(aaaa,\n" // one-per-line
3665                    "  aaaa,\n"
3666                    "    aaaa  );\n"
3667                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3668                    Style));
3669   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3670             "    aaaa);\n"
3671             "aaa(aaaa, aaaa,\n"
3672             "    aaaa);",
3673             format("aaa(aaaa,  aaaa,\n" // bin-packed
3674                    "    aaaa  );\n"
3675                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3676                    Style));
3677 }
3678 
3679 TEST_F(FormatTest, FormatsBuilderPattern) {
3680   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3681                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3682                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3683                "    .StartsWith(\".init\", ORDER_INIT)\n"
3684                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3685                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3686                "    .Default(ORDER_TEXT);\n");
3687 
3688   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3689                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3690   verifyFormat(
3691       "aaaaaaa->aaaaaaa\n"
3692       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3693       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3694       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3695   verifyFormat(
3696       "aaaaaaa->aaaaaaa\n"
3697       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3698       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3699   verifyFormat(
3700       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3701       "    aaaaaaaaaaaaaa);");
3702   verifyFormat(
3703       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3704       "    aaaaaa->aaaaaaaaaaaa()\n"
3705       "        ->aaaaaaaaaaaaaaaa(\n"
3706       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3707       "        ->aaaaaaaaaaaaaaaaa();");
3708   verifyGoogleFormat(
3709       "void f() {\n"
3710       "  someo->Add((new util::filetools::Handler(dir))\n"
3711       "                 ->OnEvent1(NewPermanentCallback(\n"
3712       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3713       "                 ->OnEvent2(NewPermanentCallback(\n"
3714       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3715       "                 ->OnEvent3(NewPermanentCallback(\n"
3716       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3717       "                 ->OnEvent5(NewPermanentCallback(\n"
3718       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3719       "                 ->OnEvent6(NewPermanentCallback(\n"
3720       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3721       "}");
3722 
3723   verifyFormat(
3724       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3725   verifyFormat("aaaaaaaaaaaaaaa()\n"
3726                "    .aaaaaaaaaaaaaaa()\n"
3727                "    .aaaaaaaaaaaaaaa()\n"
3728                "    .aaaaaaaaaaaaaaa()\n"
3729                "    .aaaaaaaaaaaaaaa();");
3730   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3731                "    .aaaaaaaaaaaaaaa()\n"
3732                "    .aaaaaaaaaaaaaaa()\n"
3733                "    .aaaaaaaaaaaaaaa();");
3734   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3735                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3736                "    .aaaaaaaaaaaaaaa();");
3737   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3738                "    ->aaaaaaaaaaaaaae(0)\n"
3739                "    ->aaaaaaaaaaaaaaa();");
3740 
3741   // Don't linewrap after very short segments.
3742   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3743                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3744                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3745   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3746                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3747                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3748   verifyFormat("aaa()\n"
3749                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3750                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3751                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3752 
3753   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3754                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3755                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3756   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3757                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3758                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3759 
3760   // Prefer not to break after empty parentheses.
3761   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3762                "    First->LastNewlineOffset);");
3763 
3764   // Prefer not to create "hanging" indents.
3765   verifyFormat(
3766       "return !soooooooooooooome_map\n"
3767       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3768       "            .second;");
3769   verifyFormat(
3770       "return aaaaaaaaaaaaaaaa\n"
3771       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
3772       "    .aaaa(aaaaaaaaaaaaaa);");
3773   // No hanging indent here.
3774   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
3775                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3776   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
3777                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3778   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3779                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3780                getLLVMStyleWithColumns(60));
3781   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
3782                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3783                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3784                getLLVMStyleWithColumns(59));
3785   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3786                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3787                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3788 }
3789 
3790 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3791   verifyFormat(
3792       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3793       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3794   verifyFormat(
3795       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3796       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3797 
3798   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3799                "    ccccccccccccccccccccccccc) {\n}");
3800   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3801                "    ccccccccccccccccccccccccc) {\n}");
3802 
3803   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3804                "    ccccccccccccccccccccccccc) {\n}");
3805   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3806                "    ccccccccccccccccccccccccc) {\n}");
3807 
3808   verifyFormat(
3809       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3810       "    ccccccccccccccccccccccccc) {\n}");
3811   verifyFormat(
3812       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3813       "    ccccccccccccccccccccccccc) {\n}");
3814 
3815   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3816                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3817                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3818                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3819   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3820                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3821                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3822                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3823 
3824   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3825                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3826                "    aaaaaaaaaaaaaaa != aa) {\n}");
3827   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3828                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3829                "    aaaaaaaaaaaaaaa != aa) {\n}");
3830 }
3831 
3832 TEST_F(FormatTest, BreaksAfterAssignments) {
3833   verifyFormat(
3834       "unsigned Cost =\n"
3835       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3836       "                        SI->getPointerAddressSpaceee());\n");
3837   verifyFormat(
3838       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3839       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3840 
3841   verifyFormat(
3842       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3843       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3844   verifyFormat("unsigned OriginalStartColumn =\n"
3845                "    SourceMgr.getSpellingColumnNumber(\n"
3846                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3847                "    1;");
3848 }
3849 
3850 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
3851   FormatStyle Style = getLLVMStyle();
3852   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3853                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
3854                Style);
3855 
3856   Style.PenaltyBreakAssignment = 20;
3857   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3858                "                                 cccccccccccccccccccccccccc;",
3859                Style);
3860 }
3861 
3862 TEST_F(FormatTest, AlignsAfterAssignments) {
3863   verifyFormat(
3864       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3865       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3866   verifyFormat(
3867       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3868       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3869   verifyFormat(
3870       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3871       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3872   verifyFormat(
3873       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3874       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3875   verifyFormat(
3876       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3877       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3878       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3879 }
3880 
3881 TEST_F(FormatTest, AlignsAfterReturn) {
3882   verifyFormat(
3883       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3884       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3885   verifyFormat(
3886       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3887       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3888   verifyFormat(
3889       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3890       "       aaaaaaaaaaaaaaaaaaaaaa();");
3891   verifyFormat(
3892       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3893       "        aaaaaaaaaaaaaaaaaaaaaa());");
3894   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3895                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3896   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3897                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3898                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3899   verifyFormat("return\n"
3900                "    // true if code is one of a or b.\n"
3901                "    code == a || code == b;");
3902 }
3903 
3904 TEST_F(FormatTest, AlignsAfterOpenBracket) {
3905   verifyFormat(
3906       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3907       "                                                aaaaaaaaa aaaaaaa) {}");
3908   verifyFormat(
3909       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3910       "                                               aaaaaaaaaaa aaaaaaaaa);");
3911   verifyFormat(
3912       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3913       "                                             aaaaaaaaaaaaaaaaaaaaa));");
3914   FormatStyle Style = getLLVMStyle();
3915   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3916   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3917                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
3918                Style);
3919   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3920                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
3921                Style);
3922   verifyFormat("SomeLongVariableName->someFunction(\n"
3923                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
3924                Style);
3925   verifyFormat(
3926       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3927       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3928       Style);
3929   verifyFormat(
3930       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3931       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3932       Style);
3933   verifyFormat(
3934       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3935       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3936       Style);
3937 
3938   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
3939                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
3940                "        b));",
3941                Style);
3942 
3943   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
3944   Style.BinPackArguments = false;
3945   Style.BinPackParameters = false;
3946   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3947                "    aaaaaaaaaaa aaaaaaaa,\n"
3948                "    aaaaaaaaa aaaaaaa,\n"
3949                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3950                Style);
3951   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3952                "    aaaaaaaaaaa aaaaaaaaa,\n"
3953                "    aaaaaaaaaaa aaaaaaaaa,\n"
3954                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3955                Style);
3956   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
3957                "    aaaaaaaaaaaaaaa,\n"
3958                "    aaaaaaaaaaaaaaaaaaaaa,\n"
3959                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3960                Style);
3961   verifyFormat(
3962       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
3963       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3964       Style);
3965   verifyFormat(
3966       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
3967       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3968       Style);
3969   verifyFormat(
3970       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3971       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3972       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
3973       "    aaaaaaaaaaaaaaaa);",
3974       Style);
3975   verifyFormat(
3976       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3977       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3978       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
3979       "    aaaaaaaaaaaaaaaa);",
3980       Style);
3981 }
3982 
3983 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
3984   FormatStyle Style = getLLVMStyleWithColumns(40);
3985   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3986                "          bbbbbbbbbbbbbbbbbbbbbb);",
3987                Style);
3988   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
3989   Style.AlignOperands = false;
3990   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3991                "          bbbbbbbbbbbbbbbbbbbbbb);",
3992                Style);
3993   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3994   Style.AlignOperands = true;
3995   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3996                "          bbbbbbbbbbbbbbbbbbbbbb);",
3997                Style);
3998   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3999   Style.AlignOperands = false;
4000   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4001                "    bbbbbbbbbbbbbbbbbbbbbb);",
4002                Style);
4003 }
4004 
4005 TEST_F(FormatTest, BreaksConditionalExpressions) {
4006   verifyFormat(
4007       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4008       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4009       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4010   verifyFormat(
4011       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4012       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4013       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4014   verifyFormat(
4015       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4016       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4017   verifyFormat(
4018       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4019       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4020       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4021   verifyFormat(
4022       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4023       "                                                    : aaaaaaaaaaaaa);");
4024   verifyFormat(
4025       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4026       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4027       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4028       "                   aaaaaaaaaaaaa);");
4029   verifyFormat(
4030       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4031       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4032       "                   aaaaaaaaaaaaa);");
4033   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4034                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4035                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4036                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4037                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4038   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4039                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4040                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4041                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4042                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4043                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4044                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4045   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4046                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4047                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4048                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4049                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4050   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4051                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4052                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4053   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4054                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4055                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4056                "        : aaaaaaaaaaaaaaaa;");
4057   verifyFormat(
4058       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4059       "    ? aaaaaaaaaaaaaaa\n"
4060       "    : aaaaaaaaaaaaaaa;");
4061   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4062                "          aaaaaaaaa\n"
4063                "      ? b\n"
4064                "      : c);");
4065   verifyFormat("return aaaa == bbbb\n"
4066                "           // comment\n"
4067                "           ? aaaa\n"
4068                "           : bbbb;");
4069   verifyFormat("unsigned Indent =\n"
4070                "    format(TheLine.First,\n"
4071                "           IndentForLevel[TheLine.Level] >= 0\n"
4072                "               ? IndentForLevel[TheLine.Level]\n"
4073                "               : TheLine * 2,\n"
4074                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4075                getLLVMStyleWithColumns(60));
4076   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4077                "                  ? aaaaaaaaaaaaaaa\n"
4078                "                  : bbbbbbbbbbbbbbb //\n"
4079                "                        ? ccccccccccccccc\n"
4080                "                        : ddddddddddddddd;");
4081   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4082                "                  ? aaaaaaaaaaaaaaa\n"
4083                "                  : (bbbbbbbbbbbbbbb //\n"
4084                "                         ? ccccccccccccccc\n"
4085                "                         : ddddddddddddddd);");
4086   verifyFormat(
4087       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4088       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4089       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4090       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4091       "                                      : aaaaaaaaaa;");
4092   verifyFormat(
4093       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4094       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4095       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4096 
4097   FormatStyle NoBinPacking = getLLVMStyle();
4098   NoBinPacking.BinPackArguments = false;
4099   verifyFormat(
4100       "void f() {\n"
4101       "  g(aaa,\n"
4102       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4103       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4104       "        ? aaaaaaaaaaaaaaa\n"
4105       "        : aaaaaaaaaaaaaaa);\n"
4106       "}",
4107       NoBinPacking);
4108   verifyFormat(
4109       "void f() {\n"
4110       "  g(aaa,\n"
4111       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4112       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4113       "        ?: aaaaaaaaaaaaaaa);\n"
4114       "}",
4115       NoBinPacking);
4116 
4117   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4118                "             // comment.\n"
4119                "             ccccccccccccccccccccccccccccccccccccccc\n"
4120                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4121                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4122 
4123   // Assignments in conditional expressions. Apparently not uncommon :-(.
4124   verifyFormat("return a != b\n"
4125                "           // comment\n"
4126                "           ? a = b\n"
4127                "           : a = b;");
4128   verifyFormat("return a != b\n"
4129                "           // comment\n"
4130                "           ? a = a != b\n"
4131                "                     // comment\n"
4132                "                     ? a = b\n"
4133                "                     : a\n"
4134                "           : a;\n");
4135   verifyFormat("return a != b\n"
4136                "           // comment\n"
4137                "           ? a\n"
4138                "           : a = a != b\n"
4139                "                     // comment\n"
4140                "                     ? a = b\n"
4141                "                     : a;");
4142 }
4143 
4144 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4145   FormatStyle Style = getLLVMStyle();
4146   Style.BreakBeforeTernaryOperators = false;
4147   Style.ColumnLimit = 70;
4148   verifyFormat(
4149       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4150       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4151       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4152       Style);
4153   verifyFormat(
4154       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4155       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4156       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4157       Style);
4158   verifyFormat(
4159       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4160       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4161       Style);
4162   verifyFormat(
4163       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4164       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4165       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4166       Style);
4167   verifyFormat(
4168       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4169       "                                                      aaaaaaaaaaaaa);",
4170       Style);
4171   verifyFormat(
4172       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4173       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4174       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4175       "                   aaaaaaaaaaaaa);",
4176       Style);
4177   verifyFormat(
4178       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4179       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4180       "                   aaaaaaaaaaaaa);",
4181       Style);
4182   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4183                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4184                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4185                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4186                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4187                Style);
4188   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4189                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4190                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4191                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4192                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4193                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4194                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4195                Style);
4196   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4197                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4198                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4199                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4200                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4201                Style);
4202   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4203                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4204                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4205                Style);
4206   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4207                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4208                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4209                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4210                Style);
4211   verifyFormat(
4212       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4213       "    aaaaaaaaaaaaaaa :\n"
4214       "    aaaaaaaaaaaaaaa;",
4215       Style);
4216   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4217                "          aaaaaaaaa ?\n"
4218                "      b :\n"
4219                "      c);",
4220                Style);
4221   verifyFormat("unsigned Indent =\n"
4222                "    format(TheLine.First,\n"
4223                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4224                "               IndentForLevel[TheLine.Level] :\n"
4225                "               TheLine * 2,\n"
4226                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4227                Style);
4228   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4229                "                  aaaaaaaaaaaaaaa :\n"
4230                "                  bbbbbbbbbbbbbbb ? //\n"
4231                "                      ccccccccccccccc :\n"
4232                "                      ddddddddddddddd;",
4233                Style);
4234   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4235                "                  aaaaaaaaaaaaaaa :\n"
4236                "                  (bbbbbbbbbbbbbbb ? //\n"
4237                "                       ccccccccccccccc :\n"
4238                "                       ddddddddddddddd);",
4239                Style);
4240   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4241                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4242                "            ccccccccccccccccccccccccccc;",
4243                Style);
4244   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4245                "           aaaaa :\n"
4246                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4247                Style);
4248 }
4249 
4250 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4251   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4252                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4253   verifyFormat("bool a = true, b = false;");
4254 
4255   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4256                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4257                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4258                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4259   verifyFormat(
4260       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4261       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4262       "     d = e && f;");
4263   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4264                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4265   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4266                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4267   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4268                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4269 
4270   FormatStyle Style = getGoogleStyle();
4271   Style.PointerAlignment = FormatStyle::PAS_Left;
4272   Style.DerivePointerAlignment = false;
4273   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4274                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4275                "    *b = bbbbbbbbbbbbbbbbbbb;",
4276                Style);
4277   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4278                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4279                Style);
4280   verifyFormat("vector<int*> a, b;", Style);
4281   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4282 }
4283 
4284 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4285   verifyFormat("arr[foo ? bar : baz];");
4286   verifyFormat("f()[foo ? bar : baz];");
4287   verifyFormat("(a + b)[foo ? bar : baz];");
4288   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4289 }
4290 
4291 TEST_F(FormatTest, AlignsStringLiterals) {
4292   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4293                "                                      \"short literal\");");
4294   verifyFormat(
4295       "looooooooooooooooooooooooongFunction(\n"
4296       "    \"short literal\"\n"
4297       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4298   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4299                "             \" string literals\",\n"
4300                "             and, other, parameters);");
4301   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4302             "      \"5678\";",
4303             format("fun + \"1243\" /* comment */\n"
4304                    "    \"5678\";",
4305                    getLLVMStyleWithColumns(28)));
4306   EXPECT_EQ(
4307       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4308       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4309       "         \"aaaaaaaaaaaaaaaa\";",
4310       format("aaaaaa ="
4311              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4312              "aaaaaaaaaaaaaaaaaaaaa\" "
4313              "\"aaaaaaaaaaaaaaaa\";"));
4314   verifyFormat("a = a + \"a\"\n"
4315                "        \"a\"\n"
4316                "        \"a\";");
4317   verifyFormat("f(\"a\", \"b\"\n"
4318                "       \"c\");");
4319 
4320   verifyFormat(
4321       "#define LL_FORMAT \"ll\"\n"
4322       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4323       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4324 
4325   verifyFormat("#define A(X)          \\\n"
4326                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4327                "  \"ccccc\"",
4328                getLLVMStyleWithColumns(23));
4329   verifyFormat("#define A \"def\"\n"
4330                "f(\"abc\" A \"ghi\"\n"
4331                "  \"jkl\");");
4332 
4333   verifyFormat("f(L\"a\"\n"
4334                "  L\"b\");");
4335   verifyFormat("#define A(X)            \\\n"
4336                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4337                "  L\"ccccc\"",
4338                getLLVMStyleWithColumns(25));
4339 
4340   verifyFormat("f(@\"a\"\n"
4341                "  @\"b\");");
4342   verifyFormat("NSString s = @\"a\"\n"
4343                "             @\"b\"\n"
4344                "             @\"c\";");
4345   verifyFormat("NSString s = @\"a\"\n"
4346                "              \"b\"\n"
4347                "              \"c\";");
4348 }
4349 
4350 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4351   FormatStyle Style = getLLVMStyle();
4352   // No declarations or definitions should be moved to own line.
4353   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4354   verifyFormat("class A {\n"
4355                "  int f() { return 1; }\n"
4356                "  int g();\n"
4357                "};\n"
4358                "int f() { return 1; }\n"
4359                "int g();\n",
4360                Style);
4361 
4362   // All declarations and definitions should have the return type moved to its
4363   // own
4364   // line.
4365   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4366   verifyFormat("class E {\n"
4367                "  int\n"
4368                "  f() {\n"
4369                "    return 1;\n"
4370                "  }\n"
4371                "  int\n"
4372                "  g();\n"
4373                "};\n"
4374                "int\n"
4375                "f() {\n"
4376                "  return 1;\n"
4377                "}\n"
4378                "int\n"
4379                "g();\n",
4380                Style);
4381 
4382   // Top-level definitions, and no kinds of declarations should have the
4383   // return type moved to its own line.
4384   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4385   verifyFormat("class B {\n"
4386                "  int f() { return 1; }\n"
4387                "  int g();\n"
4388                "};\n"
4389                "int\n"
4390                "f() {\n"
4391                "  return 1;\n"
4392                "}\n"
4393                "int g();\n",
4394                Style);
4395 
4396   // Top-level definitions and declarations should have the return type moved
4397   // to its own line.
4398   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4399   verifyFormat("class C {\n"
4400                "  int f() { return 1; }\n"
4401                "  int g();\n"
4402                "};\n"
4403                "int\n"
4404                "f() {\n"
4405                "  return 1;\n"
4406                "}\n"
4407                "int\n"
4408                "g();\n",
4409                Style);
4410 
4411   // All definitions should have the return type moved to its own line, but no
4412   // kinds of declarations.
4413   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4414   verifyFormat("class D {\n"
4415                "  int\n"
4416                "  f() {\n"
4417                "    return 1;\n"
4418                "  }\n"
4419                "  int g();\n"
4420                "};\n"
4421                "int\n"
4422                "f() {\n"
4423                "  return 1;\n"
4424                "}\n"
4425                "int g();\n",
4426                Style);
4427   verifyFormat("const char *\n"
4428                "f(void) {\n" // Break here.
4429                "  return \"\";\n"
4430                "}\n"
4431                "const char *bar(void);\n", // No break here.
4432                Style);
4433   verifyFormat("template <class T>\n"
4434                "T *\n"
4435                "f(T &c) {\n" // Break here.
4436                "  return NULL;\n"
4437                "}\n"
4438                "template <class T> T *f(T &c);\n", // No break here.
4439                Style);
4440   verifyFormat("class C {\n"
4441                "  int\n"
4442                "  operator+() {\n"
4443                "    return 1;\n"
4444                "  }\n"
4445                "  int\n"
4446                "  operator()() {\n"
4447                "    return 1;\n"
4448                "  }\n"
4449                "};\n",
4450                Style);
4451   verifyFormat("void\n"
4452                "A::operator()() {}\n"
4453                "void\n"
4454                "A::operator>>() {}\n"
4455                "void\n"
4456                "A::operator+() {}\n",
4457                Style);
4458   verifyFormat("void *operator new(std::size_t s);", // No break here.
4459                Style);
4460   verifyFormat("void *\n"
4461                "operator new(std::size_t s) {}",
4462                Style);
4463   verifyFormat("void *\n"
4464                "operator delete[](void *ptr) {}",
4465                Style);
4466   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4467   verifyFormat("const char *\n"
4468                "f(void)\n" // Break here.
4469                "{\n"
4470                "  return \"\";\n"
4471                "}\n"
4472                "const char *bar(void);\n", // No break here.
4473                Style);
4474   verifyFormat("template <class T>\n"
4475                "T *\n"     // Problem here: no line break
4476                "f(T &c)\n" // Break here.
4477                "{\n"
4478                "  return NULL;\n"
4479                "}\n"
4480                "template <class T> T *f(T &c);\n", // No break here.
4481                Style);
4482 }
4483 
4484 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4485   FormatStyle NoBreak = getLLVMStyle();
4486   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4487   FormatStyle Break = getLLVMStyle();
4488   Break.AlwaysBreakBeforeMultilineStrings = true;
4489   verifyFormat("aaaa = \"bbbb\"\n"
4490                "       \"cccc\";",
4491                NoBreak);
4492   verifyFormat("aaaa =\n"
4493                "    \"bbbb\"\n"
4494                "    \"cccc\";",
4495                Break);
4496   verifyFormat("aaaa(\"bbbb\"\n"
4497                "     \"cccc\");",
4498                NoBreak);
4499   verifyFormat("aaaa(\n"
4500                "    \"bbbb\"\n"
4501                "    \"cccc\");",
4502                Break);
4503   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4504                "          \"cccc\");",
4505                NoBreak);
4506   verifyFormat("aaaa(qqq,\n"
4507                "     \"bbbb\"\n"
4508                "     \"cccc\");",
4509                Break);
4510   verifyFormat("aaaa(qqq,\n"
4511                "     L\"bbbb\"\n"
4512                "     L\"cccc\");",
4513                Break);
4514   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4515                "                      \"bbbb\"));",
4516                Break);
4517   verifyFormat("string s = someFunction(\n"
4518                "    \"abc\"\n"
4519                "    \"abc\");",
4520                Break);
4521 
4522   // As we break before unary operators, breaking right after them is bad.
4523   verifyFormat("string foo = abc ? \"x\"\n"
4524                "                   \"blah blah blah blah blah blah\"\n"
4525                "                 : \"y\";",
4526                Break);
4527 
4528   // Don't break if there is no column gain.
4529   verifyFormat("f(\"aaaa\"\n"
4530                "  \"bbbb\");",
4531                Break);
4532 
4533   // Treat literals with escaped newlines like multi-line string literals.
4534   EXPECT_EQ("x = \"a\\\n"
4535             "b\\\n"
4536             "c\";",
4537             format("x = \"a\\\n"
4538                    "b\\\n"
4539                    "c\";",
4540                    NoBreak));
4541   EXPECT_EQ("xxxx =\n"
4542             "    \"a\\\n"
4543             "b\\\n"
4544             "c\";",
4545             format("xxxx = \"a\\\n"
4546                    "b\\\n"
4547                    "c\";",
4548                    Break));
4549 
4550   EXPECT_EQ("NSString *const kString =\n"
4551             "    @\"aaaa\"\n"
4552             "    @\"bbbb\";",
4553             format("NSString *const kString = @\"aaaa\"\n"
4554                    "@\"bbbb\";",
4555                    Break));
4556 
4557   Break.ColumnLimit = 0;
4558   verifyFormat("const char *hello = \"hello llvm\";", Break);
4559 }
4560 
4561 TEST_F(FormatTest, AlignsPipes) {
4562   verifyFormat(
4563       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4564       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4565       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4566   verifyFormat(
4567       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4568       "                     << aaaaaaaaaaaaaaaaaaaa;");
4569   verifyFormat(
4570       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4571       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4572   verifyFormat(
4573       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4574       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4575   verifyFormat(
4576       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4577       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4578       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4579   verifyFormat(
4580       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4581       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4582       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4583   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4584                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4585                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4586                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4587   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4588                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4589   verifyFormat(
4590       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4591       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4592   verifyFormat(
4593       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
4594       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
4595 
4596   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4597                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4598   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4599                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4600                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4601                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4602   verifyFormat("LOG_IF(aaa == //\n"
4603                "       bbb)\n"
4604                "    << a << b;");
4605 
4606   // But sometimes, breaking before the first "<<" is desirable.
4607   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4608                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4609   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4610                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4611                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4612   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4613                "    << BEF << IsTemplate << Description << E->getType();");
4614   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4615                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4616                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4617   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4618                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4619                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4620                "    << aaa;");
4621 
4622   verifyFormat(
4623       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4624       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4625 
4626   // Incomplete string literal.
4627   EXPECT_EQ("llvm::errs() << \"\n"
4628             "             << a;",
4629             format("llvm::errs() << \"\n<<a;"));
4630 
4631   verifyFormat("void f() {\n"
4632                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4633                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4634                "}");
4635 
4636   // Handle 'endl'.
4637   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4638                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4639   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4640 
4641   // Handle '\n'.
4642   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4643                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4644   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4645                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4646   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4647                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4648   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4649 }
4650 
4651 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4652   verifyFormat("return out << \"somepacket = {\\n\"\n"
4653                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4654                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4655                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4656                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4657                "           << \"}\";");
4658 
4659   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4660                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4661                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4662   verifyFormat(
4663       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4664       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4665       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4666       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4667       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4668   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4669                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4670   verifyFormat(
4671       "void f() {\n"
4672       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4673       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4674       "}");
4675 
4676   // Breaking before the first "<<" is generally not desirable.
4677   verifyFormat(
4678       "llvm::errs()\n"
4679       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4680       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4681       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4682       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4683       getLLVMStyleWithColumns(70));
4684   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4685                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4686                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4687                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4688                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4689                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4690                getLLVMStyleWithColumns(70));
4691 
4692   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4693                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4694                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4695   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4696                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4697                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4698   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4699                "           (aaaa + aaaa);",
4700                getLLVMStyleWithColumns(40));
4701   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4702                "                  (aaaaaaa + aaaaa));",
4703                getLLVMStyleWithColumns(40));
4704   verifyFormat(
4705       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
4706       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
4707       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
4708 }
4709 
4710 TEST_F(FormatTest, UnderstandsEquals) {
4711   verifyFormat(
4712       "aaaaaaaaaaaaaaaaa =\n"
4713       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4714   verifyFormat(
4715       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4716       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4717   verifyFormat(
4718       "if (a) {\n"
4719       "  f();\n"
4720       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4721       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4722       "}");
4723 
4724   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4725                "        100000000 + 10000000) {\n}");
4726 }
4727 
4728 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4729   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4730                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4731 
4732   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4733                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4734 
4735   verifyFormat(
4736       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4737       "                                                          Parameter2);");
4738 
4739   verifyFormat(
4740       "ShortObject->shortFunction(\n"
4741       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4742       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4743 
4744   verifyFormat("loooooooooooooongFunction(\n"
4745                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4746 
4747   verifyFormat(
4748       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4749       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4750 
4751   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4752                "    .WillRepeatedly(Return(SomeValue));");
4753   verifyFormat("void f() {\n"
4754                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4755                "      .Times(2)\n"
4756                "      .WillRepeatedly(Return(SomeValue));\n"
4757                "}");
4758   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4759                "    ccccccccccccccccccccccc);");
4760   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4761                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4762                "          .aaaaa(aaaaa),\n"
4763                "      aaaaaaaaaaaaaaaaaaaaa);");
4764   verifyFormat("void f() {\n"
4765                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4766                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4767                "}");
4768   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4769                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4770                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4771                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4772                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4773   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4774                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4775                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4776                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4777                "}");
4778 
4779   // Here, it is not necessary to wrap at "." or "->".
4780   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4781                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4782   verifyFormat(
4783       "aaaaaaaaaaa->aaaaaaaaa(\n"
4784       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4785       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4786 
4787   verifyFormat(
4788       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4789       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4790   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4791                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4792   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4793                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4794 
4795   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4796                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4797                "    .a();");
4798 
4799   FormatStyle NoBinPacking = getLLVMStyle();
4800   NoBinPacking.BinPackParameters = false;
4801   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4802                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4803                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4804                "                         aaaaaaaaaaaaaaaaaaa,\n"
4805                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4806                NoBinPacking);
4807 
4808   // If there is a subsequent call, change to hanging indentation.
4809   verifyFormat(
4810       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4811       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4812       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4813   verifyFormat(
4814       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4815       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4816   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4817                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4818                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4819   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4820                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4821                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4822 }
4823 
4824 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4825   verifyFormat("template <typename T>\n"
4826                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4827   verifyFormat("template <typename T>\n"
4828                "// T should be one of {A, B}.\n"
4829                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4830   verifyFormat(
4831       "template <typename T>\n"
4832       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4833   verifyFormat("template <typename T>\n"
4834                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4835                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4836   verifyFormat(
4837       "template <typename T>\n"
4838       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4839       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4840   verifyFormat(
4841       "template <typename T>\n"
4842       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4843       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4844       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4845   verifyFormat("template <typename T>\n"
4846                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4847                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4848   verifyFormat(
4849       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4850       "          typename T4 = char>\n"
4851       "void f();");
4852   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4853                "          template <typename> class cccccccccccccccccccccc,\n"
4854                "          typename ddddddddddddd>\n"
4855                "class C {};");
4856   verifyFormat(
4857       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4858       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4859 
4860   verifyFormat("void f() {\n"
4861                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4862                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4863                "}");
4864 
4865   verifyFormat("template <typename T> class C {};");
4866   verifyFormat("template <typename T> void f();");
4867   verifyFormat("template <typename T> void f() {}");
4868   verifyFormat(
4869       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4870       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4871       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4872       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4873       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4874       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4875       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4876       getLLVMStyleWithColumns(72));
4877   EXPECT_EQ("static_cast<A< //\n"
4878             "    B> *>(\n"
4879             "\n"
4880             ");",
4881             format("static_cast<A<//\n"
4882                    "    B>*>(\n"
4883                    "\n"
4884                    "    );"));
4885   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4886                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4887 
4888   FormatStyle AlwaysBreak = getLLVMStyle();
4889   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4890   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4891   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4892   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4893   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4894                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4895                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4896   verifyFormat("template <template <typename> class Fooooooo,\n"
4897                "          template <typename> class Baaaaaaar>\n"
4898                "struct C {};",
4899                AlwaysBreak);
4900   verifyFormat("template <typename T> // T can be A, B or C.\n"
4901                "struct C {};",
4902                AlwaysBreak);
4903   verifyFormat("template <enum E> class A {\n"
4904                "public:\n"
4905                "  E *f();\n"
4906                "};");
4907 }
4908 
4909 TEST_F(FormatTest, WrapsTemplateParameters) {
4910   FormatStyle Style = getLLVMStyle();
4911   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4912   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4913   verifyFormat(
4914       "template <typename... a> struct q {};\n"
4915       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4916       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4917       "    y;",
4918       Style);
4919   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4920   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4921   verifyFormat(
4922       "template <typename... a> struct r {};\n"
4923       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4924       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4925       "    y;",
4926       Style);
4927   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4928   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4929   verifyFormat(
4930       "template <typename... a> struct s {};\n"
4931       "extern s<\n"
4932       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4933       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4934       "    y;",
4935       Style);
4936   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4937   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4938   verifyFormat(
4939       "template <typename... a> struct t {};\n"
4940       "extern t<\n"
4941       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4942       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4943       "    y;",
4944       Style);
4945 }
4946 
4947 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4948   verifyFormat(
4949       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4950       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4951   verifyFormat(
4952       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4953       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4954       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4955 
4956   // FIXME: Should we have the extra indent after the second break?
4957   verifyFormat(
4958       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4959       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4960       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4961 
4962   verifyFormat(
4963       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4964       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4965 
4966   // Breaking at nested name specifiers is generally not desirable.
4967   verifyFormat(
4968       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4969       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4970 
4971   verifyFormat(
4972       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
4973       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4974       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4975       "                   aaaaaaaaaaaaaaaaaaaaa);",
4976       getLLVMStyleWithColumns(74));
4977 
4978   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4979                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4980                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4981 }
4982 
4983 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4984   verifyFormat("A<int> a;");
4985   verifyFormat("A<A<A<int>>> a;");
4986   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4987   verifyFormat("bool x = a < 1 || 2 > a;");
4988   verifyFormat("bool x = 5 < f<int>();");
4989   verifyFormat("bool x = f<int>() > 5;");
4990   verifyFormat("bool x = 5 < a<int>::x;");
4991   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4992   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4993 
4994   verifyGoogleFormat("A<A<int>> a;");
4995   verifyGoogleFormat("A<A<A<int>>> a;");
4996   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4997   verifyGoogleFormat("A<A<int> > a;");
4998   verifyGoogleFormat("A<A<A<int> > > a;");
4999   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5000   verifyGoogleFormat("A<::A<int>> a;");
5001   verifyGoogleFormat("A<::A> a;");
5002   verifyGoogleFormat("A< ::A> a;");
5003   verifyGoogleFormat("A< ::A<int> > a;");
5004   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5005   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5006   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5007   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5008   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5009             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5010 
5011   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5012 
5013   verifyFormat("test >> a >> b;");
5014   verifyFormat("test << a >> b;");
5015 
5016   verifyFormat("f<int>();");
5017   verifyFormat("template <typename T> void f() {}");
5018   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5019   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5020                "sizeof(char)>::type>;");
5021   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5022   verifyFormat("f(a.operator()<A>());");
5023   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5024                "      .template operator()<A>());",
5025                getLLVMStyleWithColumns(35));
5026 
5027   // Not template parameters.
5028   verifyFormat("return a < b && c > d;");
5029   verifyFormat("void f() {\n"
5030                "  while (a < b && c > d) {\n"
5031                "  }\n"
5032                "}");
5033   verifyFormat("template <typename... Types>\n"
5034                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5035 
5036   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5037                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5038                getLLVMStyleWithColumns(60));
5039   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5040   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5041   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5042 }
5043 
5044 TEST_F(FormatTest, BitshiftOperatorWidth) {
5045   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5046             "                   bar */",
5047             format("int    a=1<<2;  /* foo\n"
5048                    "                   bar */"));
5049 
5050   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5051             "                     bar */",
5052             format("int  b  =256>>1 ;  /* foo\n"
5053                    "                      bar */"));
5054 }
5055 
5056 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5057   verifyFormat("COMPARE(a, ==, b);");
5058   verifyFormat("auto s = sizeof...(Ts) - 1;");
5059 }
5060 
5061 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5062   verifyFormat("int A::*x;");
5063   verifyFormat("int (S::*func)(void *);");
5064   verifyFormat("void f() { int (S::*func)(void *); }");
5065   verifyFormat("typedef bool *(Class::*Member)() const;");
5066   verifyFormat("void f() {\n"
5067                "  (a->*f)();\n"
5068                "  a->*x;\n"
5069                "  (a.*f)();\n"
5070                "  ((*a).*f)();\n"
5071                "  a.*x;\n"
5072                "}");
5073   verifyFormat("void f() {\n"
5074                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5075                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5076                "}");
5077   verifyFormat(
5078       "(aaaaaaaaaa->*bbbbbbb)(\n"
5079       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5080   FormatStyle Style = getLLVMStyle();
5081   Style.PointerAlignment = FormatStyle::PAS_Left;
5082   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5083 }
5084 
5085 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5086   verifyFormat("int a = -2;");
5087   verifyFormat("f(-1, -2, -3);");
5088   verifyFormat("a[-1] = 5;");
5089   verifyFormat("int a = 5 + -2;");
5090   verifyFormat("if (i == -1) {\n}");
5091   verifyFormat("if (i != -1) {\n}");
5092   verifyFormat("if (i > -1) {\n}");
5093   verifyFormat("if (i < -1) {\n}");
5094   verifyFormat("++(a->f());");
5095   verifyFormat("--(a->f());");
5096   verifyFormat("(a->f())++;");
5097   verifyFormat("a[42]++;");
5098   verifyFormat("if (!(a->f())) {\n}");
5099 
5100   verifyFormat("a-- > b;");
5101   verifyFormat("b ? -a : c;");
5102   verifyFormat("n * sizeof char16;");
5103   verifyFormat("n * alignof char16;", getGoogleStyle());
5104   verifyFormat("sizeof(char);");
5105   verifyFormat("alignof(char);", getGoogleStyle());
5106 
5107   verifyFormat("return -1;");
5108   verifyFormat("switch (a) {\n"
5109                "case -1:\n"
5110                "  break;\n"
5111                "}");
5112   verifyFormat("#define X -1");
5113   verifyFormat("#define X -kConstant");
5114 
5115   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5116   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5117 
5118   verifyFormat("int a = /* confusing comment */ -1;");
5119   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5120   verifyFormat("int a = i /* confusing comment */++;");
5121 }
5122 
5123 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5124   verifyFormat("if (!aaaaaaaaaa( // break\n"
5125                "        aaaaa)) {\n"
5126                "}");
5127   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5128                "    aaaaa));");
5129   verifyFormat("*aaa = aaaaaaa( // break\n"
5130                "    bbbbbb);");
5131 }
5132 
5133 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5134   verifyFormat("bool operator<();");
5135   verifyFormat("bool operator>();");
5136   verifyFormat("bool operator=();");
5137   verifyFormat("bool operator==();");
5138   verifyFormat("bool operator!=();");
5139   verifyFormat("int operator+();");
5140   verifyFormat("int operator++();");
5141   verifyFormat("bool operator,();");
5142   verifyFormat("bool operator();");
5143   verifyFormat("bool operator()();");
5144   verifyFormat("bool operator[]();");
5145   verifyFormat("operator bool();");
5146   verifyFormat("operator int();");
5147   verifyFormat("operator void *();");
5148   verifyFormat("operator SomeType<int>();");
5149   verifyFormat("operator SomeType<int, int>();");
5150   verifyFormat("operator SomeType<SomeType<int>>();");
5151   verifyFormat("void *operator new(std::size_t size);");
5152   verifyFormat("void *operator new[](std::size_t size);");
5153   verifyFormat("void operator delete(void *ptr);");
5154   verifyFormat("void operator delete[](void *ptr);");
5155   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5156                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5157   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5158                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5159 
5160   verifyFormat(
5161       "ostream &operator<<(ostream &OutputStream,\n"
5162       "                    SomeReallyLongType WithSomeReallyLongValue);");
5163   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5164                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5165                "  return left.group < right.group;\n"
5166                "}");
5167   verifyFormat("SomeType &operator=(const SomeType &S);");
5168   verifyFormat("f.template operator()<int>();");
5169 
5170   verifyGoogleFormat("operator void*();");
5171   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5172   verifyGoogleFormat("operator ::A();");
5173 
5174   verifyFormat("using A::operator+;");
5175   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5176                "int i;");
5177 }
5178 
5179 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5180   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5181   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5182   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5183   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5184   verifyFormat("Deleted &operator=(const Deleted &) &;");
5185   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5186   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5187   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5188   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5189   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5190   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5191   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
5192   verifyFormat("template <typename T>\n"
5193                "void F(T) && = delete;",
5194                getGoogleStyle());
5195 
5196   FormatStyle AlignLeft = getLLVMStyle();
5197   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5198   verifyFormat("void A::b() && {}", AlignLeft);
5199   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5200   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5201                AlignLeft);
5202   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5203   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5204   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5205   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5206   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5207   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5208   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
5209 
5210   FormatStyle Spaces = getLLVMStyle();
5211   Spaces.SpacesInCStyleCastParentheses = true;
5212   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5213   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5214   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5215   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5216 
5217   Spaces.SpacesInCStyleCastParentheses = false;
5218   Spaces.SpacesInParentheses = true;
5219   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5220   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5221   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5222   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5223 }
5224 
5225 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5226   verifyFormat("void f() {\n"
5227                "  A *a = new A;\n"
5228                "  A *a = new (placement) A;\n"
5229                "  delete a;\n"
5230                "  delete (A *)a;\n"
5231                "}");
5232   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5233                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5234   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5235                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5236                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5237   verifyFormat("delete[] h->p;");
5238 }
5239 
5240 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5241   verifyFormat("int *f(int *a) {}");
5242   verifyFormat("int main(int argc, char **argv) {}");
5243   verifyFormat("Test::Test(int b) : a(b * b) {}");
5244   verifyIndependentOfContext("f(a, *a);");
5245   verifyFormat("void g() { f(*a); }");
5246   verifyIndependentOfContext("int a = b * 10;");
5247   verifyIndependentOfContext("int a = 10 * b;");
5248   verifyIndependentOfContext("int a = b * c;");
5249   verifyIndependentOfContext("int a += b * c;");
5250   verifyIndependentOfContext("int a -= b * c;");
5251   verifyIndependentOfContext("int a *= b * c;");
5252   verifyIndependentOfContext("int a /= b * c;");
5253   verifyIndependentOfContext("int a = *b;");
5254   verifyIndependentOfContext("int a = *b * c;");
5255   verifyIndependentOfContext("int a = b * *c;");
5256   verifyIndependentOfContext("int a = b * (10);");
5257   verifyIndependentOfContext("S << b * (10);");
5258   verifyIndependentOfContext("return 10 * b;");
5259   verifyIndependentOfContext("return *b * *c;");
5260   verifyIndependentOfContext("return a & ~b;");
5261   verifyIndependentOfContext("f(b ? *c : *d);");
5262   verifyIndependentOfContext("int a = b ? *c : *d;");
5263   verifyIndependentOfContext("*b = a;");
5264   verifyIndependentOfContext("a * ~b;");
5265   verifyIndependentOfContext("a * !b;");
5266   verifyIndependentOfContext("a * +b;");
5267   verifyIndependentOfContext("a * -b;");
5268   verifyIndependentOfContext("a * ++b;");
5269   verifyIndependentOfContext("a * --b;");
5270   verifyIndependentOfContext("a[4] * b;");
5271   verifyIndependentOfContext("a[a * a] = 1;");
5272   verifyIndependentOfContext("f() * b;");
5273   verifyIndependentOfContext("a * [self dostuff];");
5274   verifyIndependentOfContext("int x = a * (a + b);");
5275   verifyIndependentOfContext("(a *)(a + b);");
5276   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5277   verifyIndependentOfContext("int *pa = (int *)&a;");
5278   verifyIndependentOfContext("return sizeof(int **);");
5279   verifyIndependentOfContext("return sizeof(int ******);");
5280   verifyIndependentOfContext("return (int **&)a;");
5281   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5282   verifyFormat("void f(Type (*parameter)[10]) {}");
5283   verifyFormat("void f(Type (&parameter)[10]) {}");
5284   verifyGoogleFormat("return sizeof(int**);");
5285   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5286   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5287   verifyFormat("auto a = [](int **&, int ***) {};");
5288   verifyFormat("auto PointerBinding = [](const char *S) {};");
5289   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5290   verifyFormat("[](const decltype(*a) &value) {}");
5291   verifyFormat("decltype(a * b) F();");
5292   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5293   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5294   verifyIndependentOfContext("typedef void (*f)(int *a);");
5295   verifyIndependentOfContext("int i{a * b};");
5296   verifyIndependentOfContext("aaa && aaa->f();");
5297   verifyIndependentOfContext("int x = ~*p;");
5298   verifyFormat("Constructor() : a(a), area(width * height) {}");
5299   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5300   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5301   verifyFormat("void f() { f(a, c * d); }");
5302   verifyFormat("void f() { f(new a(), c * d); }");
5303   verifyFormat("void f(const MyOverride &override);");
5304   verifyFormat("void f(const MyFinal &final);");
5305   verifyIndependentOfContext("bool a = f() && override.f();");
5306   verifyIndependentOfContext("bool a = f() && final.f();");
5307 
5308   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5309 
5310   verifyIndependentOfContext("A<int *> a;");
5311   verifyIndependentOfContext("A<int **> a;");
5312   verifyIndependentOfContext("A<int *, int *> a;");
5313   verifyIndependentOfContext("A<int *[]> a;");
5314   verifyIndependentOfContext(
5315       "const char *const p = reinterpret_cast<const char *const>(q);");
5316   verifyIndependentOfContext("A<int **, int **> a;");
5317   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5318   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5319   verifyFormat("for (; a && b;) {\n}");
5320   verifyFormat("bool foo = true && [] { return false; }();");
5321 
5322   verifyFormat(
5323       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5324       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5325 
5326   verifyGoogleFormat("int const* a = &b;");
5327   verifyGoogleFormat("**outparam = 1;");
5328   verifyGoogleFormat("*outparam = a * b;");
5329   verifyGoogleFormat("int main(int argc, char** argv) {}");
5330   verifyGoogleFormat("A<int*> a;");
5331   verifyGoogleFormat("A<int**> a;");
5332   verifyGoogleFormat("A<int*, int*> a;");
5333   verifyGoogleFormat("A<int**, int**> a;");
5334   verifyGoogleFormat("f(b ? *c : *d);");
5335   verifyGoogleFormat("int a = b ? *c : *d;");
5336   verifyGoogleFormat("Type* t = **x;");
5337   verifyGoogleFormat("Type* t = *++*x;");
5338   verifyGoogleFormat("*++*x;");
5339   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5340   verifyGoogleFormat("Type* t = x++ * y;");
5341   verifyGoogleFormat(
5342       "const char* const p = reinterpret_cast<const char* const>(q);");
5343   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5344   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5345   verifyGoogleFormat("template <typename T>\n"
5346                      "void f(int i = 0, SomeType** temps = NULL);");
5347 
5348   FormatStyle Left = getLLVMStyle();
5349   Left.PointerAlignment = FormatStyle::PAS_Left;
5350   verifyFormat("x = *a(x) = *a(y);", Left);
5351   verifyFormat("for (;; *a = b) {\n}", Left);
5352   verifyFormat("return *this += 1;", Left);
5353   verifyFormat("throw *x;", Left);
5354 
5355   verifyIndependentOfContext("a = *(x + y);");
5356   verifyIndependentOfContext("a = &(x + y);");
5357   verifyIndependentOfContext("*(x + y).call();");
5358   verifyIndependentOfContext("&(x + y)->call();");
5359   verifyFormat("void f() { &(*I).first; }");
5360 
5361   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5362   verifyFormat(
5363       "int *MyValues = {\n"
5364       "    *A, // Operator detection might be confused by the '{'\n"
5365       "    *BB // Operator detection might be confused by previous comment\n"
5366       "};");
5367 
5368   verifyIndependentOfContext("if (int *a = &b)");
5369   verifyIndependentOfContext("if (int &a = *b)");
5370   verifyIndependentOfContext("if (a & b[i])");
5371   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5372   verifyIndependentOfContext("if (*b[i])");
5373   verifyIndependentOfContext("if (int *a = (&b))");
5374   verifyIndependentOfContext("while (int *a = &b)");
5375   verifyIndependentOfContext("size = sizeof *a;");
5376   verifyIndependentOfContext("if (a && (b = c))");
5377   verifyFormat("void f() {\n"
5378                "  for (const int &v : Values) {\n"
5379                "  }\n"
5380                "}");
5381   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5382   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5383   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5384 
5385   verifyFormat("#define A (!a * b)");
5386   verifyFormat("#define MACRO     \\\n"
5387                "  int *i = a * b; \\\n"
5388                "  void f(a *b);",
5389                getLLVMStyleWithColumns(19));
5390 
5391   verifyIndependentOfContext("A = new SomeType *[Length];");
5392   verifyIndependentOfContext("A = new SomeType *[Length]();");
5393   verifyIndependentOfContext("T **t = new T *;");
5394   verifyIndependentOfContext("T **t = new T *();");
5395   verifyGoogleFormat("A = new SomeType*[Length]();");
5396   verifyGoogleFormat("A = new SomeType*[Length];");
5397   verifyGoogleFormat("T** t = new T*;");
5398   verifyGoogleFormat("T** t = new T*();");
5399 
5400   FormatStyle PointerLeft = getLLVMStyle();
5401   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5402   verifyFormat("delete *x;", PointerLeft);
5403   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5404   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5405   verifyFormat("template <bool a, bool b> "
5406                "typename t::if<x && y>::type f() {}");
5407   verifyFormat("template <int *y> f() {}");
5408   verifyFormat("vector<int *> v;");
5409   verifyFormat("vector<int *const> v;");
5410   verifyFormat("vector<int *const **const *> v;");
5411   verifyFormat("vector<int *volatile> v;");
5412   verifyFormat("vector<a * b> v;");
5413   verifyFormat("foo<b && false>();");
5414   verifyFormat("foo<b & 1>();");
5415   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5416   verifyFormat(
5417       "template <class T, class = typename std::enable_if<\n"
5418       "                       std::is_integral<T>::value &&\n"
5419       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5420       "void F();",
5421       getLLVMStyleWithColumns(70));
5422   verifyFormat(
5423       "template <class T,\n"
5424       "          class = typename std::enable_if<\n"
5425       "              std::is_integral<T>::value &&\n"
5426       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5427       "          class U>\n"
5428       "void F();",
5429       getLLVMStyleWithColumns(70));
5430   verifyFormat(
5431       "template <class T,\n"
5432       "          class = typename ::std::enable_if<\n"
5433       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5434       "void F();",
5435       getGoogleStyleWithColumns(68));
5436 
5437   verifyIndependentOfContext("MACRO(int *i);");
5438   verifyIndependentOfContext("MACRO(auto *a);");
5439   verifyIndependentOfContext("MACRO(const A *a);");
5440   verifyIndependentOfContext("MACRO(A *const a);");
5441   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5442   verifyFormat("void f() { f(float{1}, a * a); }");
5443   // FIXME: Is there a way to make this work?
5444   // verifyIndependentOfContext("MACRO(A *a);");
5445 
5446   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5447   verifyFormat("return options != nullptr && operator==(*options);");
5448 
5449   EXPECT_EQ("#define OP(x)                                    \\\n"
5450             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5451             "    return s << a.DebugString();                 \\\n"
5452             "  }",
5453             format("#define OP(x) \\\n"
5454                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5455                    "    return s << a.DebugString(); \\\n"
5456                    "  }",
5457                    getLLVMStyleWithColumns(50)));
5458 
5459   // FIXME: We cannot handle this case yet; we might be able to figure out that
5460   // foo<x> d > v; doesn't make sense.
5461   verifyFormat("foo<a<b && c> d> v;");
5462 
5463   FormatStyle PointerMiddle = getLLVMStyle();
5464   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5465   verifyFormat("delete *x;", PointerMiddle);
5466   verifyFormat("int * x;", PointerMiddle);
5467   verifyFormat("template <int * y> f() {}", PointerMiddle);
5468   verifyFormat("int * f(int * a) {}", PointerMiddle);
5469   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5470   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5471   verifyFormat("A<int *> a;", PointerMiddle);
5472   verifyFormat("A<int **> a;", PointerMiddle);
5473   verifyFormat("A<int *, int *> a;", PointerMiddle);
5474   verifyFormat("A<int * []> a;", PointerMiddle);
5475   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5476   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5477   verifyFormat("T ** t = new T *;", PointerMiddle);
5478 
5479   // Member function reference qualifiers aren't binary operators.
5480   verifyFormat("string // break\n"
5481                "operator()() & {}");
5482   verifyFormat("string // break\n"
5483                "operator()() && {}");
5484   verifyGoogleFormat("template <typename T>\n"
5485                      "auto x() & -> int {}");
5486 }
5487 
5488 TEST_F(FormatTest, UnderstandsAttributes) {
5489   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5490   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5491                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5492   FormatStyle AfterType = getLLVMStyle();
5493   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5494   verifyFormat("__attribute__((nodebug)) void\n"
5495                "foo() {}\n",
5496                AfterType);
5497 }
5498 
5499 TEST_F(FormatTest, UnderstandsEllipsis) {
5500   verifyFormat("int printf(const char *fmt, ...);");
5501   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5502   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5503 
5504   FormatStyle PointersLeft = getLLVMStyle();
5505   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5506   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5507 }
5508 
5509 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5510   EXPECT_EQ("int *a;\n"
5511             "int *a;\n"
5512             "int *a;",
5513             format("int *a;\n"
5514                    "int* a;\n"
5515                    "int *a;",
5516                    getGoogleStyle()));
5517   EXPECT_EQ("int* a;\n"
5518             "int* a;\n"
5519             "int* a;",
5520             format("int* a;\n"
5521                    "int* a;\n"
5522                    "int *a;",
5523                    getGoogleStyle()));
5524   EXPECT_EQ("int *a;\n"
5525             "int *a;\n"
5526             "int *a;",
5527             format("int *a;\n"
5528                    "int * a;\n"
5529                    "int *  a;",
5530                    getGoogleStyle()));
5531   EXPECT_EQ("auto x = [] {\n"
5532             "  int *a;\n"
5533             "  int *a;\n"
5534             "  int *a;\n"
5535             "};",
5536             format("auto x=[]{int *a;\n"
5537                    "int * a;\n"
5538                    "int *  a;};",
5539                    getGoogleStyle()));
5540 }
5541 
5542 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5543   verifyFormat("int f(int &&a) {}");
5544   verifyFormat("int f(int a, char &&b) {}");
5545   verifyFormat("void f() { int &&a = b; }");
5546   verifyGoogleFormat("int f(int a, char&& b) {}");
5547   verifyGoogleFormat("void f() { int&& a = b; }");
5548 
5549   verifyIndependentOfContext("A<int &&> a;");
5550   verifyIndependentOfContext("A<int &&, int &&> a;");
5551   verifyGoogleFormat("A<int&&> a;");
5552   verifyGoogleFormat("A<int&&, int&&> a;");
5553 
5554   // Not rvalue references:
5555   verifyFormat("template <bool B, bool C> class A {\n"
5556                "  static_assert(B && C, \"Something is wrong\");\n"
5557                "};");
5558   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5559   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5560   verifyFormat("#define A(a, b) (a && b)");
5561 }
5562 
5563 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5564   verifyFormat("void f() {\n"
5565                "  x[aaaaaaaaa -\n"
5566                "    b] = 23;\n"
5567                "}",
5568                getLLVMStyleWithColumns(15));
5569 }
5570 
5571 TEST_F(FormatTest, FormatsCasts) {
5572   verifyFormat("Type *A = static_cast<Type *>(P);");
5573   verifyFormat("Type *A = (Type *)P;");
5574   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5575   verifyFormat("int a = (int)(2.0f);");
5576   verifyFormat("int a = (int)2.0f;");
5577   verifyFormat("x[(int32)y];");
5578   verifyFormat("x = (int32)y;");
5579   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5580   verifyFormat("int a = (int)*b;");
5581   verifyFormat("int a = (int)2.0f;");
5582   verifyFormat("int a = (int)~0;");
5583   verifyFormat("int a = (int)++a;");
5584   verifyFormat("int a = (int)sizeof(int);");
5585   verifyFormat("int a = (int)+2;");
5586   verifyFormat("my_int a = (my_int)2.0f;");
5587   verifyFormat("my_int a = (my_int)sizeof(int);");
5588   verifyFormat("return (my_int)aaa;");
5589   verifyFormat("#define x ((int)-1)");
5590   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5591   verifyFormat("#define p(q) ((int *)&q)");
5592   verifyFormat("fn(a)(b) + 1;");
5593 
5594   verifyFormat("void f() { my_int a = (my_int)*b; }");
5595   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5596   verifyFormat("my_int a = (my_int)~0;");
5597   verifyFormat("my_int a = (my_int)++a;");
5598   verifyFormat("my_int a = (my_int)-2;");
5599   verifyFormat("my_int a = (my_int)1;");
5600   verifyFormat("my_int a = (my_int *)1;");
5601   verifyFormat("my_int a = (const my_int)-1;");
5602   verifyFormat("my_int a = (const my_int *)-1;");
5603   verifyFormat("my_int a = (my_int)(my_int)-1;");
5604   verifyFormat("my_int a = (ns::my_int)-2;");
5605   verifyFormat("case (my_int)ONE:");
5606   verifyFormat("auto x = (X)this;");
5607 
5608   // FIXME: single value wrapped with paren will be treated as cast.
5609   verifyFormat("void f(int i = (kValue)*kMask) {}");
5610 
5611   verifyFormat("{ (void)F; }");
5612 
5613   // Don't break after a cast's
5614   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5615                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5616                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5617 
5618   // These are not casts.
5619   verifyFormat("void f(int *) {}");
5620   verifyFormat("f(foo)->b;");
5621   verifyFormat("f(foo).b;");
5622   verifyFormat("f(foo)(b);");
5623   verifyFormat("f(foo)[b];");
5624   verifyFormat("[](foo) { return 4; }(bar);");
5625   verifyFormat("(*funptr)(foo)[4];");
5626   verifyFormat("funptrs[4](foo)[4];");
5627   verifyFormat("void f(int *);");
5628   verifyFormat("void f(int *) = 0;");
5629   verifyFormat("void f(SmallVector<int>) {}");
5630   verifyFormat("void f(SmallVector<int>);");
5631   verifyFormat("void f(SmallVector<int>) = 0;");
5632   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5633   verifyFormat("int a = sizeof(int) * b;");
5634   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5635   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5636   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5637   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5638 
5639   // These are not casts, but at some point were confused with casts.
5640   verifyFormat("virtual void foo(int *) override;");
5641   verifyFormat("virtual void foo(char &) const;");
5642   verifyFormat("virtual void foo(int *a, char *) const;");
5643   verifyFormat("int a = sizeof(int *) + b;");
5644   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5645   verifyFormat("bool b = f(g<int>) && c;");
5646   verifyFormat("typedef void (*f)(int i) func;");
5647 
5648   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5649                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5650   // FIXME: The indentation here is not ideal.
5651   verifyFormat(
5652       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5653       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5654       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5655 }
5656 
5657 TEST_F(FormatTest, FormatsFunctionTypes) {
5658   verifyFormat("A<bool()> a;");
5659   verifyFormat("A<SomeType()> a;");
5660   verifyFormat("A<void (*)(int, std::string)> a;");
5661   verifyFormat("A<void *(int)>;");
5662   verifyFormat("void *(*a)(int *, SomeType *);");
5663   verifyFormat("int (*func)(void *);");
5664   verifyFormat("void f() { int (*func)(void *); }");
5665   verifyFormat("template <class CallbackClass>\n"
5666                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5667 
5668   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5669   verifyGoogleFormat("void* (*a)(int);");
5670   verifyGoogleFormat(
5671       "template <class CallbackClass>\n"
5672       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5673 
5674   // Other constructs can look somewhat like function types:
5675   verifyFormat("A<sizeof(*x)> a;");
5676   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5677   verifyFormat("some_var = function(*some_pointer_var)[0];");
5678   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5679   verifyFormat("int x = f(&h)();");
5680   verifyFormat("returnsFunction(&param1, &param2)(param);");
5681   verifyFormat("std::function<\n"
5682                "    LooooooooooongTemplatedType<\n"
5683                "        SomeType>*(\n"
5684                "        LooooooooooooooooongType type)>\n"
5685                "    function;",
5686                getGoogleStyleWithColumns(40));
5687 }
5688 
5689 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5690   verifyFormat("A (*foo_)[6];");
5691   verifyFormat("vector<int> (*foo_)[6];");
5692 }
5693 
5694 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5695   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5696                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5697   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5698                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5699   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5700                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5701 
5702   // Different ways of ()-initializiation.
5703   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5704                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5705   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5706                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5707   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5708                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5709   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5710                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5711 
5712   // Lambdas should not confuse the variable declaration heuristic.
5713   verifyFormat("LooooooooooooooooongType\n"
5714                "    variable(nullptr, [](A *a) {});",
5715                getLLVMStyleWithColumns(40));
5716 }
5717 
5718 TEST_F(FormatTest, BreaksLongDeclarations) {
5719   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5720                "    AnotherNameForTheLongType;");
5721   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5722                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5723   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5724                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5725   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5726                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5727   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5728                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5729   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5730                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5731   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5732                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5733   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5734                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5735   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5736                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
5737   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5738                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
5739   FormatStyle Indented = getLLVMStyle();
5740   Indented.IndentWrappedFunctionNames = true;
5741   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5742                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5743                Indented);
5744   verifyFormat(
5745       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5746       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5747       Indented);
5748   verifyFormat(
5749       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5750       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5751       Indented);
5752   verifyFormat(
5753       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5754       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5755       Indented);
5756 
5757   // FIXME: Without the comment, this breaks after "(".
5758   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5759                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5760                getGoogleStyle());
5761 
5762   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5763                "                  int LoooooooooooooooooooongParam2) {}");
5764   verifyFormat(
5765       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5766       "                                   SourceLocation L, IdentifierIn *II,\n"
5767       "                                   Type *T) {}");
5768   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5769                "ReallyReaaallyLongFunctionName(\n"
5770                "    const std::string &SomeParameter,\n"
5771                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5772                "        &ReallyReallyLongParameterName,\n"
5773                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5774                "        &AnotherLongParameterName) {}");
5775   verifyFormat("template <typename A>\n"
5776                "SomeLoooooooooooooooooooooongType<\n"
5777                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5778                "Function() {}");
5779 
5780   verifyGoogleFormat(
5781       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5782       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5783   verifyGoogleFormat(
5784       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5785       "                                   SourceLocation L) {}");
5786   verifyGoogleFormat(
5787       "some_namespace::LongReturnType\n"
5788       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5789       "    int first_long_parameter, int second_parameter) {}");
5790 
5791   verifyGoogleFormat("template <typename T>\n"
5792                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5793                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5794   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5795                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5796 
5797   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5798                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5799                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5800   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5801                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5802                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5803   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5804                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5805                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5806                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5807 
5808   verifyFormat("template <typename T> // Templates on own line.\n"
5809                "static int            // Some comment.\n"
5810                "MyFunction(int a);",
5811                getLLVMStyle());
5812 }
5813 
5814 TEST_F(FormatTest, FormatsArrays) {
5815   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5816                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5817   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
5818                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
5819   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5820                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
5821   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5822                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5823   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5824                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5825   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5826                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5827                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5828   verifyFormat(
5829       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5830       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5831       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5832   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
5833                "    .aaaaaaaaaaaaaaaaaaaaaa();");
5834 
5835   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5836                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5837   verifyFormat(
5838       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5839       "                                  .aaaaaaa[0]\n"
5840       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5841   verifyFormat("a[::b::c];");
5842 
5843   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5844 
5845   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
5846   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
5847 }
5848 
5849 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5850   verifyFormat("(a)->b();");
5851   verifyFormat("--a;");
5852 }
5853 
5854 TEST_F(FormatTest, HandlesIncludeDirectives) {
5855   verifyFormat("#include <string>\n"
5856                "#include <a/b/c.h>\n"
5857                "#include \"a/b/string\"\n"
5858                "#include \"string.h\"\n"
5859                "#include \"string.h\"\n"
5860                "#include <a-a>\n"
5861                "#include < path with space >\n"
5862                "#include_next <test.h>"
5863                "#include \"abc.h\" // this is included for ABC\n"
5864                "#include \"some long include\" // with a comment\n"
5865                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5866                getLLVMStyleWithColumns(35));
5867   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5868   EXPECT_EQ("#include <a>", format("#include<a>"));
5869 
5870   verifyFormat("#import <string>");
5871   verifyFormat("#import <a/b/c.h>");
5872   verifyFormat("#import \"a/b/string\"");
5873   verifyFormat("#import \"string.h\"");
5874   verifyFormat("#import \"string.h\"");
5875   verifyFormat("#if __has_include(<strstream>)\n"
5876                "#include <strstream>\n"
5877                "#endif");
5878 
5879   verifyFormat("#define MY_IMPORT <a/b>");
5880 
5881   verifyFormat("#if __has_include(<a/b>)");
5882   verifyFormat("#if __has_include_next(<a/b>)");
5883   verifyFormat("#define F __has_include(<a/b>)");
5884   verifyFormat("#define F __has_include_next(<a/b>)");
5885 
5886   // Protocol buffer definition or missing "#".
5887   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5888                getLLVMStyleWithColumns(30));
5889 
5890   FormatStyle Style = getLLVMStyle();
5891   Style.AlwaysBreakBeforeMultilineStrings = true;
5892   Style.ColumnLimit = 0;
5893   verifyFormat("#import \"abc.h\"", Style);
5894 
5895   // But 'import' might also be a regular C++ namespace.
5896   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5897                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5898 }
5899 
5900 //===----------------------------------------------------------------------===//
5901 // Error recovery tests.
5902 //===----------------------------------------------------------------------===//
5903 
5904 TEST_F(FormatTest, IncompleteParameterLists) {
5905   FormatStyle NoBinPacking = getLLVMStyle();
5906   NoBinPacking.BinPackParameters = false;
5907   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5908                "                        double *min_x,\n"
5909                "                        double *max_x,\n"
5910                "                        double *min_y,\n"
5911                "                        double *max_y,\n"
5912                "                        double *min_z,\n"
5913                "                        double *max_z, ) {}",
5914                NoBinPacking);
5915 }
5916 
5917 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5918   verifyFormat("void f() { return; }\n42");
5919   verifyFormat("void f() {\n"
5920                "  if (0)\n"
5921                "    return;\n"
5922                "}\n"
5923                "42");
5924   verifyFormat("void f() { return }\n42");
5925   verifyFormat("void f() {\n"
5926                "  if (0)\n"
5927                "    return\n"
5928                "}\n"
5929                "42");
5930 }
5931 
5932 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5933   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5934   EXPECT_EQ("void f() {\n"
5935             "  if (a)\n"
5936             "    return\n"
5937             "}",
5938             format("void  f  (  )  {  if  ( a )  return  }"));
5939   EXPECT_EQ("namespace N {\n"
5940             "void f()\n"
5941             "}",
5942             format("namespace  N  {  void f()  }"));
5943   EXPECT_EQ("namespace N {\n"
5944             "void f() {}\n"
5945             "void g()\n"
5946             "} // namespace N",
5947             format("namespace N  { void f( ) { } void g( ) }"));
5948 }
5949 
5950 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5951   verifyFormat("int aaaaaaaa =\n"
5952                "    // Overlylongcomment\n"
5953                "    b;",
5954                getLLVMStyleWithColumns(20));
5955   verifyFormat("function(\n"
5956                "    ShortArgument,\n"
5957                "    LoooooooooooongArgument);\n",
5958                getLLVMStyleWithColumns(20));
5959 }
5960 
5961 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5962   verifyFormat("public:");
5963   verifyFormat("class A {\n"
5964                "public\n"
5965                "  void f() {}\n"
5966                "};");
5967   verifyFormat("public\n"
5968                "int qwerty;");
5969   verifyFormat("public\n"
5970                "B {}");
5971   verifyFormat("public\n"
5972                "{}");
5973   verifyFormat("public\n"
5974                "B { int x; }");
5975 }
5976 
5977 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5978   verifyFormat("{");
5979   verifyFormat("#})");
5980   verifyNoCrash("(/**/[:!] ?[).");
5981 }
5982 
5983 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5984   verifyFormat("do {\n}");
5985   verifyFormat("do {\n}\n"
5986                "f();");
5987   verifyFormat("do {\n}\n"
5988                "wheeee(fun);");
5989   verifyFormat("do {\n"
5990                "  f();\n"
5991                "}");
5992 }
5993 
5994 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5995   verifyFormat("if {\n  foo;\n  foo();\n}");
5996   verifyFormat("switch {\n  foo;\n  foo();\n}");
5997   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
5998   verifyFormat("while {\n  foo;\n  foo();\n}");
5999   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6000 }
6001 
6002 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6003   verifyIncompleteFormat("namespace {\n"
6004                          "class Foo { Foo (\n"
6005                          "};\n"
6006                          "} // namespace");
6007 }
6008 
6009 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6010   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6011   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6012   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6013   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6014 
6015   EXPECT_EQ("{\n"
6016             "  {\n"
6017             "    breakme(\n"
6018             "        qwe);\n"
6019             "  }\n",
6020             format("{\n"
6021                    "    {\n"
6022                    " breakme(qwe);\n"
6023                    "}\n",
6024                    getLLVMStyleWithColumns(10)));
6025 }
6026 
6027 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6028   verifyFormat("int x = {\n"
6029                "    avariable,\n"
6030                "    b(alongervariable)};",
6031                getLLVMStyleWithColumns(25));
6032 }
6033 
6034 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6035   verifyFormat("return (a)(b){1, 2, 3};");
6036 }
6037 
6038 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6039   verifyFormat("vector<int> x{1, 2, 3, 4};");
6040   verifyFormat("vector<int> x{\n"
6041                "    1,\n"
6042                "    2,\n"
6043                "    3,\n"
6044                "    4,\n"
6045                "};");
6046   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6047   verifyFormat("f({1, 2});");
6048   verifyFormat("auto v = Foo{-1};");
6049   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6050   verifyFormat("Class::Class : member{1, 2, 3} {}");
6051   verifyFormat("new vector<int>{1, 2, 3};");
6052   verifyFormat("new int[3]{1, 2, 3};");
6053   verifyFormat("new int{1};");
6054   verifyFormat("return {arg1, arg2};");
6055   verifyFormat("return {arg1, SomeType{parameter}};");
6056   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6057   verifyFormat("new T{arg1, arg2};");
6058   verifyFormat("f(MyMap[{composite, key}]);");
6059   verifyFormat("class Class {\n"
6060                "  T member = {arg1, arg2};\n"
6061                "};");
6062   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6063   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6064   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6065   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6066   verifyFormat("int a = std::is_integral<int>{} + 0;");
6067 
6068   verifyFormat("int foo(int i) { return fo1{}(i); }");
6069   verifyFormat("int foo(int i) { return fo1{}(i); }");
6070   verifyFormat("auto i = decltype(x){};");
6071   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6072   verifyFormat("Node n{1, Node{1000}, //\n"
6073                "       2};");
6074   verifyFormat("Aaaa aaaaaaa{\n"
6075                "    {\n"
6076                "        aaaa,\n"
6077                "    },\n"
6078                "};");
6079   verifyFormat("class C : public D {\n"
6080                "  SomeClass SC{2};\n"
6081                "};");
6082   verifyFormat("class C : public A {\n"
6083                "  class D : public B {\n"
6084                "    void f() { int i{2}; }\n"
6085                "  };\n"
6086                "};");
6087   verifyFormat("#define A {a, a},");
6088 
6089   // Binpacking only if there is no trailing comma
6090   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6091                "                      cccccccccc, dddddddddd};",
6092 			   getLLVMStyleWithColumns(50));
6093   verifyFormat("const Aaaaaa aaaaa = {\n"
6094                "    aaaaaaaaaaa,\n"
6095                "    bbbbbbbbbbb,\n"
6096                "    ccccccccccc,\n"
6097                "    ddddddddddd,\n"
6098                "};", getLLVMStyleWithColumns(50));
6099 
6100   // Cases where distinguising braced lists and blocks is hard.
6101   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6102   verifyFormat("void f() {\n"
6103                "  return; // comment\n"
6104                "}\n"
6105                "SomeType t;");
6106   verifyFormat("void f() {\n"
6107                "  if (a) {\n"
6108                "    f();\n"
6109                "  }\n"
6110                "}\n"
6111                "SomeType t;");
6112 
6113   // In combination with BinPackArguments = false.
6114   FormatStyle NoBinPacking = getLLVMStyle();
6115   NoBinPacking.BinPackArguments = false;
6116   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6117                "                      bbbbb,\n"
6118                "                      ccccc,\n"
6119                "                      ddddd,\n"
6120                "                      eeeee,\n"
6121                "                      ffffff,\n"
6122                "                      ggggg,\n"
6123                "                      hhhhhh,\n"
6124                "                      iiiiii,\n"
6125                "                      jjjjjj,\n"
6126                "                      kkkkkk};",
6127                NoBinPacking);
6128   verifyFormat("const Aaaaaa aaaaa = {\n"
6129                "    aaaaa,\n"
6130                "    bbbbb,\n"
6131                "    ccccc,\n"
6132                "    ddddd,\n"
6133                "    eeeee,\n"
6134                "    ffffff,\n"
6135                "    ggggg,\n"
6136                "    hhhhhh,\n"
6137                "    iiiiii,\n"
6138                "    jjjjjj,\n"
6139                "    kkkkkk,\n"
6140                "};",
6141                NoBinPacking);
6142   verifyFormat(
6143       "const Aaaaaa aaaaa = {\n"
6144       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6145       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6146       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6147       "};",
6148       NoBinPacking);
6149 
6150   // FIXME: The alignment of these trailing comments might be bad. Then again,
6151   // this might be utterly useless in real code.
6152   verifyFormat("Constructor::Constructor()\n"
6153                "    : some_value{         //\n"
6154                "                 aaaaaaa, //\n"
6155                "                 bbbbbbb} {}");
6156 
6157   // In braced lists, the first comment is always assumed to belong to the
6158   // first element. Thus, it can be moved to the next or previous line as
6159   // appropriate.
6160   EXPECT_EQ("function({// First element:\n"
6161             "          1,\n"
6162             "          // Second element:\n"
6163             "          2});",
6164             format("function({\n"
6165                    "    // First element:\n"
6166                    "    1,\n"
6167                    "    // Second element:\n"
6168                    "    2});"));
6169   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6170             "    // First element:\n"
6171             "    1,\n"
6172             "    // Second element:\n"
6173             "    2};",
6174             format("std::vector<int> MyNumbers{// First element:\n"
6175                    "                           1,\n"
6176                    "                           // Second element:\n"
6177                    "                           2};",
6178                    getLLVMStyleWithColumns(30)));
6179   // A trailing comma should still lead to an enforced line break and no
6180   // binpacking.
6181   EXPECT_EQ("vector<int> SomeVector = {\n"
6182             "    // aaa\n"
6183             "    1,\n"
6184             "    2,\n"
6185             "};",
6186             format("vector<int> SomeVector = { // aaa\n"
6187                    "    1, 2, };"));
6188 
6189   FormatStyle ExtraSpaces = getLLVMStyle();
6190   ExtraSpaces.Cpp11BracedListStyle = false;
6191   ExtraSpaces.ColumnLimit = 75;
6192   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6193   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6194   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6195   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6196   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6197   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6198   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6199   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6200   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6201   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6202   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6203   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6204   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6205   verifyFormat("class Class {\n"
6206                "  T member = { arg1, arg2 };\n"
6207                "};",
6208                ExtraSpaces);
6209   verifyFormat(
6210       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6211       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6212       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6213       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6214       ExtraSpaces);
6215   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6216   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6217                ExtraSpaces);
6218   verifyFormat(
6219       "someFunction(OtherParam,\n"
6220       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6221       "                         param1, param2,\n"
6222       "                         // comment 2\n"
6223       "                         param3, param4 });",
6224       ExtraSpaces);
6225   verifyFormat(
6226       "std::this_thread::sleep_for(\n"
6227       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6228       ExtraSpaces);
6229   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6230                "    aaaaaaa,\n"
6231                "    aaaaaaaaaa,\n"
6232                "    aaaaa,\n"
6233                "    aaaaaaaaaaaaaaa,\n"
6234                "    aaa,\n"
6235                "    aaaaaaaaaa,\n"
6236                "    a,\n"
6237                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6238                "    aaaaaaaaaaaa,\n"
6239                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6240                "    aaaaaaa,\n"
6241                "    a};");
6242   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6243   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6244   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6245 }
6246 
6247 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6248   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6249                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6250                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6251                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6252                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6253                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6254   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6255                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6256                "                 1, 22, 333, 4444, 55555, //\n"
6257                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6258                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6259   verifyFormat(
6260       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6261       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6262       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6263       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6264       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6265       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6266       "                 7777777};");
6267   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6268                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6269                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6270   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6271                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6272                "    // Separating comment.\n"
6273                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6274   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6275                "    // Leading comment\n"
6276                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6277                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6278   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6279                "                 1, 1, 1, 1};",
6280                getLLVMStyleWithColumns(39));
6281   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6282                "                 1, 1, 1, 1};",
6283                getLLVMStyleWithColumns(38));
6284   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6285                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6286                getLLVMStyleWithColumns(43));
6287   verifyFormat(
6288       "static unsigned SomeValues[10][3] = {\n"
6289       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6290       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6291   verifyFormat("static auto fields = new vector<string>{\n"
6292                "    \"aaaaaaaaaaaaa\",\n"
6293                "    \"aaaaaaaaaaaaa\",\n"
6294                "    \"aaaaaaaaaaaa\",\n"
6295                "    \"aaaaaaaaaaaaaa\",\n"
6296                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6297                "    \"aaaaaaaaaaaa\",\n"
6298                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6299                "};");
6300   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6301   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6302                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6303                "                 3, cccccccccccccccccccccc};",
6304                getLLVMStyleWithColumns(60));
6305 
6306   // Trailing commas.
6307   verifyFormat("vector<int> x = {\n"
6308                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6309                "};",
6310                getLLVMStyleWithColumns(39));
6311   verifyFormat("vector<int> x = {\n"
6312                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6313                "};",
6314                getLLVMStyleWithColumns(39));
6315   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6316                "                 1, 1, 1, 1,\n"
6317                "                 /**/ /**/};",
6318                getLLVMStyleWithColumns(39));
6319 
6320   // Trailing comment in the first line.
6321   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6322                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6323                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6324                "    11111111,   22222222,   333333333,   44444444};");
6325   // Trailing comment in the last line.
6326   verifyFormat("int aaaaa[] = {\n"
6327                "    1, 2, 3, // comment\n"
6328                "    4, 5, 6  // comment\n"
6329                "};");
6330 
6331   // With nested lists, we should either format one item per line or all nested
6332   // lists one on line.
6333   // FIXME: For some nested lists, we can do better.
6334   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6335                "        {aaaaaaaaaaaaaaaaaaa},\n"
6336                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6337                "        {aaaaaaaaaaaaaaaaa}};",
6338                getLLVMStyleWithColumns(60));
6339   verifyFormat(
6340       "SomeStruct my_struct_array = {\n"
6341       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6342       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6343       "    {aaa, aaa},\n"
6344       "    {aaa, aaa},\n"
6345       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6346       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6347       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6348 
6349   // No column layout should be used here.
6350   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6351                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6352 
6353   verifyNoCrash("a<,");
6354 
6355   // No braced initializer here.
6356   verifyFormat("void f() {\n"
6357                "  struct Dummy {};\n"
6358                "  f(v);\n"
6359                "}");
6360 
6361   // Long lists should be formatted in columns even if they are nested.
6362   verifyFormat(
6363       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6364       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6365       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6366       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6367       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6368       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6369 
6370   // Allow "single-column" layout even if that violates the column limit. There
6371   // isn't going to be a better way.
6372   verifyFormat("std::vector<int> a = {\n"
6373                "    aaaaaaaa,\n"
6374                "    aaaaaaaa,\n"
6375                "    aaaaaaaa,\n"
6376                "    aaaaaaaa,\n"
6377                "    aaaaaaaaaa,\n"
6378                "    aaaaaaaa,\n"
6379                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6380                getLLVMStyleWithColumns(30));
6381   verifyFormat("vector<int> aaaa = {\n"
6382                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6383                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6384                "    aaaaaa.aaaaaaa,\n"
6385                "    aaaaaa.aaaaaaa,\n"
6386                "    aaaaaa.aaaaaaa,\n"
6387                "    aaaaaa.aaaaaaa,\n"
6388                "};");
6389 
6390   // Don't create hanging lists.
6391   verifyFormat("someFunction(Param, {List1, List2,\n"
6392                "                     List3});",
6393                getLLVMStyleWithColumns(35));
6394   verifyFormat("someFunction(Param, Param,\n"
6395                "             {List1, List2,\n"
6396                "              List3});",
6397                getLLVMStyleWithColumns(35));
6398   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6399                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6400 }
6401 
6402 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6403   FormatStyle DoNotMerge = getLLVMStyle();
6404   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6405 
6406   verifyFormat("void f() { return 42; }");
6407   verifyFormat("void f() {\n"
6408                "  return 42;\n"
6409                "}",
6410                DoNotMerge);
6411   verifyFormat("void f() {\n"
6412                "  // Comment\n"
6413                "}");
6414   verifyFormat("{\n"
6415                "#error {\n"
6416                "  int a;\n"
6417                "}");
6418   verifyFormat("{\n"
6419                "  int a;\n"
6420                "#error {\n"
6421                "}");
6422   verifyFormat("void f() {} // comment");
6423   verifyFormat("void f() { int a; } // comment");
6424   verifyFormat("void f() {\n"
6425                "} // comment",
6426                DoNotMerge);
6427   verifyFormat("void f() {\n"
6428                "  int a;\n"
6429                "} // comment",
6430                DoNotMerge);
6431   verifyFormat("void f() {\n"
6432                "} // comment",
6433                getLLVMStyleWithColumns(15));
6434 
6435   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6436   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6437 
6438   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6439   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6440   verifyFormat("class C {\n"
6441                "  C()\n"
6442                "      : iiiiiiii(nullptr),\n"
6443                "        kkkkkkk(nullptr),\n"
6444                "        mmmmmmm(nullptr),\n"
6445                "        nnnnnnn(nullptr) {}\n"
6446                "};",
6447                getGoogleStyle());
6448 
6449   FormatStyle NoColumnLimit = getLLVMStyle();
6450   NoColumnLimit.ColumnLimit = 0;
6451   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6452   EXPECT_EQ("class C {\n"
6453             "  A() : b(0) {}\n"
6454             "};",
6455             format("class C{A():b(0){}};", NoColumnLimit));
6456   EXPECT_EQ("A()\n"
6457             "    : b(0) {\n"
6458             "}",
6459             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6460 
6461   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6462   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6463       FormatStyle::SFS_None;
6464   EXPECT_EQ("A()\n"
6465             "    : b(0) {\n"
6466             "}",
6467             format("A():b(0){}", DoNotMergeNoColumnLimit));
6468   EXPECT_EQ("A()\n"
6469             "    : b(0) {\n"
6470             "}",
6471             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6472 
6473   verifyFormat("#define A          \\\n"
6474                "  void f() {       \\\n"
6475                "    int i;         \\\n"
6476                "  }",
6477                getLLVMStyleWithColumns(20));
6478   verifyFormat("#define A           \\\n"
6479                "  void f() { int i; }",
6480                getLLVMStyleWithColumns(21));
6481   verifyFormat("#define A            \\\n"
6482                "  void f() {         \\\n"
6483                "    int i;           \\\n"
6484                "  }                  \\\n"
6485                "  int j;",
6486                getLLVMStyleWithColumns(22));
6487   verifyFormat("#define A             \\\n"
6488                "  void f() { int i; } \\\n"
6489                "  int j;",
6490                getLLVMStyleWithColumns(23));
6491 }
6492 
6493 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
6494   FormatStyle MergeEmptyOnly = getLLVMStyle();
6495   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6496   verifyFormat("class C {\n"
6497                "  int f() {}\n"
6498                "};",
6499                MergeEmptyOnly);
6500   verifyFormat("class C {\n"
6501                "  int f() {\n"
6502                "    return 42;\n"
6503                "  }\n"
6504                "};",
6505                MergeEmptyOnly);
6506   verifyFormat("int f() {}", MergeEmptyOnly);
6507   verifyFormat("int f() {\n"
6508                "  return 42;\n"
6509                "}",
6510                MergeEmptyOnly);
6511 
6512   // Also verify behavior when BraceWrapping.AfterFunction = true
6513   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6514   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
6515   verifyFormat("int f() {}", MergeEmptyOnly);
6516   verifyFormat("class C {\n"
6517                "  int f() {}\n"
6518                "};",
6519                MergeEmptyOnly);
6520 }
6521 
6522 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6523   FormatStyle MergeInlineOnly = getLLVMStyle();
6524   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6525   verifyFormat("class C {\n"
6526                "  int f() { return 42; }\n"
6527                "};",
6528                MergeInlineOnly);
6529   verifyFormat("int f() {\n"
6530                "  return 42;\n"
6531                "}",
6532                MergeInlineOnly);
6533 
6534   // SFS_Inline implies SFS_Empty
6535   verifyFormat("class C {\n"
6536                "  int f() {}\n"
6537                "};",
6538                MergeInlineOnly);
6539   verifyFormat("int f() {}", MergeInlineOnly);
6540 
6541   // Also verify behavior when BraceWrapping.AfterFunction = true
6542   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6543   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6544   verifyFormat("class C {\n"
6545                "  int f() { return 42; }\n"
6546                "};",
6547                MergeInlineOnly);
6548   verifyFormat("int f()\n"
6549                "{\n"
6550                "  return 42;\n"
6551                "}",
6552                MergeInlineOnly);
6553 
6554   // SFS_Inline implies SFS_Empty
6555   verifyFormat("int f() {}", MergeInlineOnly);
6556   verifyFormat("class C {\n"
6557                "  int f() {}\n"
6558                "};",
6559                MergeInlineOnly);
6560 }
6561 
6562 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
6563   FormatStyle MergeInlineOnly = getLLVMStyle();
6564   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
6565       FormatStyle::SFS_InlineOnly;
6566   verifyFormat("class C {\n"
6567                "  int f() { return 42; }\n"
6568                "};",
6569                MergeInlineOnly);
6570   verifyFormat("int f() {\n"
6571                "  return 42;\n"
6572                "}",
6573                MergeInlineOnly);
6574 
6575   // SFS_InlineOnly does not imply SFS_Empty
6576   verifyFormat("class C {\n"
6577                "  int f() {}\n"
6578                "};",
6579                MergeInlineOnly);
6580   verifyFormat("int f() {\n"
6581                "}",
6582                MergeInlineOnly);
6583 
6584   // Also verify behavior when BraceWrapping.AfterFunction = true
6585   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6586   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6587   verifyFormat("class C {\n"
6588                "  int f() { return 42; }\n"
6589                "};",
6590                MergeInlineOnly);
6591   verifyFormat("int f()\n"
6592                "{\n"
6593                "  return 42;\n"
6594                "}",
6595                MergeInlineOnly);
6596 
6597   // SFS_InlineOnly does not imply SFS_Empty
6598   verifyFormat("int f()\n"
6599                "{\n"
6600                "}",
6601                MergeInlineOnly);
6602   verifyFormat("class C {\n"
6603                "  int f() {}\n"
6604                "};",
6605                MergeInlineOnly);
6606 }
6607 
6608 TEST_F(FormatTest, SplitEmptyFunction) {
6609   FormatStyle Style = getLLVMStyle();
6610   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6611   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6612   Style.BraceWrapping.AfterFunction = true;
6613   Style.BraceWrapping.SplitEmptyFunction = false;
6614   Style.ColumnLimit = 40;
6615 
6616   verifyFormat("int f()\n"
6617                "{}",
6618                Style);
6619   verifyFormat("int f()\n"
6620                "{\n"
6621                "  return 42;\n"
6622                "}",
6623                Style);
6624   verifyFormat("int f()\n"
6625                "{\n"
6626                "  // some comment\n"
6627                "}",
6628                Style);
6629 
6630   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6631   verifyFormat("int f() {}", Style);
6632   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6633                "{}",
6634                Style);
6635   verifyFormat("int f()\n"
6636                "{\n"
6637                "  return 0;\n"
6638                "}",
6639                Style);
6640 
6641   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6642   verifyFormat("class Foo {\n"
6643                "  int f() {}\n"
6644                "};\n",
6645                Style);
6646   verifyFormat("class Foo {\n"
6647                "  int f() { return 0; }\n"
6648                "};\n",
6649                Style);
6650   verifyFormat("class Foo {\n"
6651                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6652                "  {}\n"
6653                "};\n",
6654                Style);
6655   verifyFormat("class Foo {\n"
6656                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6657                "  {\n"
6658                "    return 0;\n"
6659                "  }\n"
6660                "};\n",
6661                Style);
6662 
6663   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6664   verifyFormat("int f() {}", Style);
6665   verifyFormat("int f() { return 0; }", Style);
6666   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6667                "{}",
6668                Style);
6669   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6670                "{\n"
6671                "  return 0;\n"
6672                "}",
6673                Style);
6674 }
6675 
6676 TEST_F(FormatTest, SplitEmptyClass) {
6677   FormatStyle Style = getLLVMStyle();
6678   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6679   Style.BraceWrapping.AfterClass = true;
6680   Style.BraceWrapping.SplitEmptyRecord = false;
6681 
6682   verifyFormat("class Foo\n"
6683                "{};",
6684                Style);
6685   verifyFormat("/* something */ class Foo\n"
6686                "{};",
6687                Style);
6688   verifyFormat("template <typename X> class Foo\n"
6689                "{};",
6690                Style);
6691   verifyFormat("class Foo\n"
6692                "{\n"
6693                "  Foo();\n"
6694                "};",
6695                Style);
6696   verifyFormat("typedef class Foo\n"
6697                "{\n"
6698                "} Foo_t;",
6699                Style);
6700 }
6701 
6702 TEST_F(FormatTest, SplitEmptyStruct) {
6703   FormatStyle Style = getLLVMStyle();
6704   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6705   Style.BraceWrapping.AfterStruct = true;
6706   Style.BraceWrapping.SplitEmptyRecord = false;
6707 
6708   verifyFormat("struct Foo\n"
6709                "{};",
6710                Style);
6711   verifyFormat("/* something */ struct Foo\n"
6712                "{};",
6713                Style);
6714   verifyFormat("template <typename X> struct Foo\n"
6715                "{};",
6716                Style);
6717   verifyFormat("struct Foo\n"
6718                "{\n"
6719                "  Foo();\n"
6720                "};",
6721                Style);
6722   verifyFormat("typedef struct Foo\n"
6723                "{\n"
6724                "} Foo_t;",
6725                Style);
6726   //typedef struct Bar {} Bar_t;
6727 }
6728 
6729 TEST_F(FormatTest, SplitEmptyUnion) {
6730   FormatStyle Style = getLLVMStyle();
6731   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6732   Style.BraceWrapping.AfterUnion = true;
6733   Style.BraceWrapping.SplitEmptyRecord = false;
6734 
6735   verifyFormat("union Foo\n"
6736                "{};",
6737                Style);
6738   verifyFormat("/* something */ union Foo\n"
6739                "{};",
6740                Style);
6741   verifyFormat("union Foo\n"
6742                "{\n"
6743                "  A,\n"
6744                "};",
6745                Style);
6746   verifyFormat("typedef union Foo\n"
6747                "{\n"
6748                "} Foo_t;",
6749                Style);
6750 }
6751 
6752 TEST_F(FormatTest, SplitEmptyNamespace) {
6753   FormatStyle Style = getLLVMStyle();
6754   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6755   Style.BraceWrapping.AfterNamespace = true;
6756   Style.BraceWrapping.SplitEmptyNamespace = false;
6757 
6758   verifyFormat("namespace Foo\n"
6759                "{};",
6760                Style);
6761   verifyFormat("/* something */ namespace Foo\n"
6762                "{};",
6763                Style);
6764   verifyFormat("inline namespace Foo\n"
6765                "{};",
6766                Style);
6767   verifyFormat("namespace Foo\n"
6768                "{\n"
6769                "void Bar();\n"
6770                "};",
6771                Style);
6772 }
6773 
6774 TEST_F(FormatTest, NeverMergeShortRecords) {
6775   FormatStyle Style = getLLVMStyle();
6776 
6777   verifyFormat("class Foo {\n"
6778                "  Foo();\n"
6779                "};",
6780                Style);
6781   verifyFormat("typedef class Foo {\n"
6782                "  Foo();\n"
6783                "} Foo_t;",
6784                Style);
6785   verifyFormat("struct Foo {\n"
6786                "  Foo();\n"
6787                "};",
6788                Style);
6789   verifyFormat("typedef struct Foo {\n"
6790                "  Foo();\n"
6791                "} Foo_t;",
6792                Style);
6793   verifyFormat("union Foo {\n"
6794                "  A,\n"
6795                "};",
6796                Style);
6797   verifyFormat("typedef union Foo {\n"
6798                "  A,\n"
6799                "} Foo_t;",
6800                Style);
6801   verifyFormat("namespace Foo {\n"
6802                "void Bar();\n"
6803                "};",
6804                Style);
6805 
6806   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6807   Style.BraceWrapping.AfterClass = true;
6808   Style.BraceWrapping.AfterStruct = true;
6809   Style.BraceWrapping.AfterUnion = true;
6810   Style.BraceWrapping.AfterNamespace = true;
6811   verifyFormat("class Foo\n"
6812                "{\n"
6813                "  Foo();\n"
6814                "};",
6815                Style);
6816   verifyFormat("typedef class Foo\n"
6817                "{\n"
6818                "  Foo();\n"
6819                "} Foo_t;",
6820                Style);
6821   verifyFormat("struct Foo\n"
6822                "{\n"
6823                "  Foo();\n"
6824                "};",
6825                Style);
6826   verifyFormat("typedef struct Foo\n"
6827                "{\n"
6828                "  Foo();\n"
6829                "} Foo_t;",
6830                Style);
6831   verifyFormat("union Foo\n"
6832                "{\n"
6833                "  A,\n"
6834                "};",
6835                Style);
6836   verifyFormat("typedef union Foo\n"
6837                "{\n"
6838                "  A,\n"
6839                "} Foo_t;",
6840                Style);
6841   verifyFormat("namespace Foo\n"
6842                "{\n"
6843                "void Bar();\n"
6844                "};",
6845                Style);
6846 }
6847 
6848 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6849   // Elaborate type variable declarations.
6850   verifyFormat("struct foo a = {bar};\nint n;");
6851   verifyFormat("class foo a = {bar};\nint n;");
6852   verifyFormat("union foo a = {bar};\nint n;");
6853 
6854   // Elaborate types inside function definitions.
6855   verifyFormat("struct foo f() {}\nint n;");
6856   verifyFormat("class foo f() {}\nint n;");
6857   verifyFormat("union foo f() {}\nint n;");
6858 
6859   // Templates.
6860   verifyFormat("template <class X> void f() {}\nint n;");
6861   verifyFormat("template <struct X> void f() {}\nint n;");
6862   verifyFormat("template <union X> void f() {}\nint n;");
6863 
6864   // Actual definitions...
6865   verifyFormat("struct {\n} n;");
6866   verifyFormat(
6867       "template <template <class T, class Y>, class Z> class X {\n} n;");
6868   verifyFormat("union Z {\n  int n;\n} x;");
6869   verifyFormat("class MACRO Z {\n} n;");
6870   verifyFormat("class MACRO(X) Z {\n} n;");
6871   verifyFormat("class __attribute__(X) Z {\n} n;");
6872   verifyFormat("class __declspec(X) Z {\n} n;");
6873   verifyFormat("class A##B##C {\n} n;");
6874   verifyFormat("class alignas(16) Z {\n} n;");
6875   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6876   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6877 
6878   // Redefinition from nested context:
6879   verifyFormat("class A::B::C {\n} n;");
6880 
6881   // Template definitions.
6882   verifyFormat(
6883       "template <typename F>\n"
6884       "Matcher(const Matcher<F> &Other,\n"
6885       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6886       "                             !is_same<F, T>::value>::type * = 0)\n"
6887       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6888 
6889   // FIXME: This is still incorrectly handled at the formatter side.
6890   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6891   verifyFormat("int i = SomeFunction(a<b, a> b);");
6892 
6893   // FIXME:
6894   // This now gets parsed incorrectly as class definition.
6895   // verifyFormat("class A<int> f() {\n}\nint n;");
6896 
6897   // Elaborate types where incorrectly parsing the structural element would
6898   // break the indent.
6899   verifyFormat("if (true)\n"
6900                "  class X x;\n"
6901                "else\n"
6902                "  f();\n");
6903 
6904   // This is simply incomplete. Formatting is not important, but must not crash.
6905   verifyFormat("class A:");
6906 }
6907 
6908 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6909   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6910             format("#error Leave     all         white!!!!! space* alone!\n"));
6911   EXPECT_EQ(
6912       "#warning Leave     all         white!!!!! space* alone!\n",
6913       format("#warning Leave     all         white!!!!! space* alone!\n"));
6914   EXPECT_EQ("#error 1", format("  #  error   1"));
6915   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6916 }
6917 
6918 TEST_F(FormatTest, FormatHashIfExpressions) {
6919   verifyFormat("#if AAAA && BBBB");
6920   verifyFormat("#if (AAAA && BBBB)");
6921   verifyFormat("#elif (AAAA && BBBB)");
6922   // FIXME: Come up with a better indentation for #elif.
6923   verifyFormat(
6924       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6925       "    defined(BBBBBBBB)\n"
6926       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6927       "    defined(BBBBBBBB)\n"
6928       "#endif",
6929       getLLVMStyleWithColumns(65));
6930 }
6931 
6932 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6933   FormatStyle AllowsMergedIf = getGoogleStyle();
6934   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6935   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6936   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6937   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6938   EXPECT_EQ("if (true) return 42;",
6939             format("if (true)\nreturn 42;", AllowsMergedIf));
6940   FormatStyle ShortMergedIf = AllowsMergedIf;
6941   ShortMergedIf.ColumnLimit = 25;
6942   verifyFormat("#define A \\\n"
6943                "  if (true) return 42;",
6944                ShortMergedIf);
6945   verifyFormat("#define A \\\n"
6946                "  f();    \\\n"
6947                "  if (true)\n"
6948                "#define B",
6949                ShortMergedIf);
6950   verifyFormat("#define A \\\n"
6951                "  f();    \\\n"
6952                "  if (true)\n"
6953                "g();",
6954                ShortMergedIf);
6955   verifyFormat("{\n"
6956                "#ifdef A\n"
6957                "  // Comment\n"
6958                "  if (true) continue;\n"
6959                "#endif\n"
6960                "  // Comment\n"
6961                "  if (true) continue;\n"
6962                "}",
6963                ShortMergedIf);
6964   ShortMergedIf.ColumnLimit = 33;
6965   verifyFormat("#define A \\\n"
6966                "  if constexpr (true) return 42;",
6967                ShortMergedIf);
6968   ShortMergedIf.ColumnLimit = 29;
6969   verifyFormat("#define A                   \\\n"
6970                "  if (aaaaaaaaaa) return 1; \\\n"
6971                "  return 2;",
6972                ShortMergedIf);
6973   ShortMergedIf.ColumnLimit = 28;
6974   verifyFormat("#define A         \\\n"
6975                "  if (aaaaaaaaaa) \\\n"
6976                "    return 1;     \\\n"
6977                "  return 2;",
6978                ShortMergedIf);
6979   verifyFormat("#define A                \\\n"
6980                "  if constexpr (aaaaaaa) \\\n"
6981                "    return 1;            \\\n"
6982                "  return 2;",
6983                ShortMergedIf);
6984 }
6985 
6986 TEST_F(FormatTest, FormatStarDependingOnContext) {
6987   verifyFormat("void f(int *a);");
6988   verifyFormat("void f() { f(fint * b); }");
6989   verifyFormat("class A {\n  void f(int *a);\n};");
6990   verifyFormat("class A {\n  int *a;\n};");
6991   verifyFormat("namespace a {\n"
6992                "namespace b {\n"
6993                "class A {\n"
6994                "  void f() {}\n"
6995                "  int *a;\n"
6996                "};\n"
6997                "} // namespace b\n"
6998                "} // namespace a");
6999 }
7000 
7001 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7002   verifyFormat("while");
7003   verifyFormat("operator");
7004 }
7005 
7006 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7007   // This code would be painfully slow to format if we didn't skip it.
7008   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
7009                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7010                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7011                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7012                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7013                    "A(1, 1)\n"
7014                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7015                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7016                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7017                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7018                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7019                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7020                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7021                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7022                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7023                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7024   // Deeply nested part is untouched, rest is formatted.
7025   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7026             format(std::string("int    i;\n") + Code + "int    j;\n",
7027                    getLLVMStyle(), SC_ExpectIncomplete));
7028 }
7029 
7030 //===----------------------------------------------------------------------===//
7031 // Objective-C tests.
7032 //===----------------------------------------------------------------------===//
7033 
7034 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7035   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7036   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7037             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7038   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7039   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7040   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7041             format("-(NSInteger)Method3:(id)anObject;"));
7042   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7043             format("-(NSInteger)Method4:(id)anObject;"));
7044   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7045             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7046   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7047             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7048   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7049             "forAllCells:(BOOL)flag;",
7050             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7051                    "forAllCells:(BOOL)flag;"));
7052 
7053   // Very long objectiveC method declaration.
7054   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7055                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7056   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7057                "                    inRange:(NSRange)range\n"
7058                "                   outRange:(NSRange)out_range\n"
7059                "                  outRange1:(NSRange)out_range1\n"
7060                "                  outRange2:(NSRange)out_range2\n"
7061                "                  outRange3:(NSRange)out_range3\n"
7062                "                  outRange4:(NSRange)out_range4\n"
7063                "                  outRange5:(NSRange)out_range5\n"
7064                "                  outRange6:(NSRange)out_range6\n"
7065                "                  outRange7:(NSRange)out_range7\n"
7066                "                  outRange8:(NSRange)out_range8\n"
7067                "                  outRange9:(NSRange)out_range9;");
7068 
7069   // When the function name has to be wrapped.
7070   FormatStyle Style = getLLVMStyle();
7071   Style.IndentWrappedFunctionNames = false;
7072   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7073                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7074                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7075                "}",
7076                Style);
7077   Style.IndentWrappedFunctionNames = true;
7078   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7079                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7080                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7081                "}",
7082                Style);
7083 
7084   verifyFormat("- (int)sum:(vector<int>)numbers;");
7085   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7086   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7087   // protocol lists (but not for template classes):
7088   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7089 
7090   verifyFormat("- (int (*)())foo:(int (*)())f;");
7091   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7092 
7093   // If there's no return type (very rare in practice!), LLVM and Google style
7094   // agree.
7095   verifyFormat("- foo;");
7096   verifyFormat("- foo:(int)f;");
7097   verifyGoogleFormat("- foo:(int)foo;");
7098 }
7099 
7100 
7101 TEST_F(FormatTest, BreaksStringLiterals) {
7102   EXPECT_EQ("\"some text \"\n"
7103             "\"other\";",
7104             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7105   EXPECT_EQ("\"some text \"\n"
7106             "\"other\";",
7107             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7108   EXPECT_EQ(
7109       "#define A  \\\n"
7110       "  \"some \"  \\\n"
7111       "  \"text \"  \\\n"
7112       "  \"other\";",
7113       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7114   EXPECT_EQ(
7115       "#define A  \\\n"
7116       "  \"so \"    \\\n"
7117       "  \"text \"  \\\n"
7118       "  \"other\";",
7119       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7120 
7121   EXPECT_EQ("\"some text\"",
7122             format("\"some text\"", getLLVMStyleWithColumns(1)));
7123   EXPECT_EQ("\"some text\"",
7124             format("\"some text\"", getLLVMStyleWithColumns(11)));
7125   EXPECT_EQ("\"some \"\n"
7126             "\"text\"",
7127             format("\"some text\"", getLLVMStyleWithColumns(10)));
7128   EXPECT_EQ("\"some \"\n"
7129             "\"text\"",
7130             format("\"some text\"", getLLVMStyleWithColumns(7)));
7131   EXPECT_EQ("\"some\"\n"
7132             "\" tex\"\n"
7133             "\"t\"",
7134             format("\"some text\"", getLLVMStyleWithColumns(6)));
7135   EXPECT_EQ("\"some\"\n"
7136             "\" tex\"\n"
7137             "\" and\"",
7138             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7139   EXPECT_EQ("\"some\"\n"
7140             "\"/tex\"\n"
7141             "\"/and\"",
7142             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7143 
7144   EXPECT_EQ("variable =\n"
7145             "    \"long string \"\n"
7146             "    \"literal\";",
7147             format("variable = \"long string literal\";",
7148                    getLLVMStyleWithColumns(20)));
7149 
7150   EXPECT_EQ("variable = f(\n"
7151             "    \"long string \"\n"
7152             "    \"literal\",\n"
7153             "    short,\n"
7154             "    loooooooooooooooooooong);",
7155             format("variable = f(\"long string literal\", short, "
7156                    "loooooooooooooooooooong);",
7157                    getLLVMStyleWithColumns(20)));
7158 
7159   EXPECT_EQ(
7160       "f(g(\"long string \"\n"
7161       "    \"literal\"),\n"
7162       "  b);",
7163       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7164   EXPECT_EQ("f(g(\"long string \"\n"
7165             "    \"literal\",\n"
7166             "    a),\n"
7167             "  b);",
7168             format("f(g(\"long string literal\", a), b);",
7169                    getLLVMStyleWithColumns(20)));
7170   EXPECT_EQ(
7171       "f(\"one two\".split(\n"
7172       "    variable));",
7173       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7174   EXPECT_EQ("f(\"one two three four five six \"\n"
7175             "  \"seven\".split(\n"
7176             "      really_looooong_variable));",
7177             format("f(\"one two three four five six seven\"."
7178                    "split(really_looooong_variable));",
7179                    getLLVMStyleWithColumns(33)));
7180 
7181   EXPECT_EQ("f(\"some \"\n"
7182             "  \"text\",\n"
7183             "  other);",
7184             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7185 
7186   // Only break as a last resort.
7187   verifyFormat(
7188       "aaaaaaaaaaaaaaaaaaaa(\n"
7189       "    aaaaaaaaaaaaaaaaaaaa,\n"
7190       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7191 
7192   EXPECT_EQ("\"splitmea\"\n"
7193             "\"trandomp\"\n"
7194             "\"oint\"",
7195             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7196 
7197   EXPECT_EQ("\"split/\"\n"
7198             "\"pathat/\"\n"
7199             "\"slashes\"",
7200             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7201 
7202   EXPECT_EQ("\"split/\"\n"
7203             "\"pathat/\"\n"
7204             "\"slashes\"",
7205             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7206   EXPECT_EQ("\"split at \"\n"
7207             "\"spaces/at/\"\n"
7208             "\"slashes.at.any$\"\n"
7209             "\"non-alphanumeric%\"\n"
7210             "\"1111111111characte\"\n"
7211             "\"rs\"",
7212             format("\"split at "
7213                    "spaces/at/"
7214                    "slashes.at."
7215                    "any$non-"
7216                    "alphanumeric%"
7217                    "1111111111characte"
7218                    "rs\"",
7219                    getLLVMStyleWithColumns(20)));
7220 
7221   // Verify that splitting the strings understands
7222   // Style::AlwaysBreakBeforeMultilineStrings.
7223   EXPECT_EQ(
7224       "aaaaaaaaaaaa(\n"
7225       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7226       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7227       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7228              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7229              "aaaaaaaaaaaaaaaaaaaaaa\");",
7230              getGoogleStyle()));
7231   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7232             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7233             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7234                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7235                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7236                    getGoogleStyle()));
7237   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7238             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7239             format("llvm::outs() << "
7240                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7241                    "aaaaaaaaaaaaaaaaaaa\";"));
7242   EXPECT_EQ("ffff(\n"
7243             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7244             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7245             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7246                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7247                    getGoogleStyle()));
7248 
7249   FormatStyle Style = getLLVMStyleWithColumns(12);
7250   Style.BreakStringLiterals = false;
7251   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7252 
7253   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7254   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7255   EXPECT_EQ("#define A \\\n"
7256             "  \"some \" \\\n"
7257             "  \"text \" \\\n"
7258             "  \"other\";",
7259             format("#define A \"some text other\";", AlignLeft));
7260 }
7261 
7262 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7263   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7264   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7265   EXPECT_EQ("int i = a(b());",
7266             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7267 }
7268 
7269 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7270   EXPECT_EQ(
7271       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7272       "(\n"
7273       "    \"x\t\");",
7274       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7275              "aaaaaaa("
7276              "\"x\t\");"));
7277 }
7278 
7279 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7280   EXPECT_EQ(
7281       "u8\"utf8 string \"\n"
7282       "u8\"literal\";",
7283       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7284   EXPECT_EQ(
7285       "u\"utf16 string \"\n"
7286       "u\"literal\";",
7287       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7288   EXPECT_EQ(
7289       "U\"utf32 string \"\n"
7290       "U\"literal\";",
7291       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7292   EXPECT_EQ("L\"wide string \"\n"
7293             "L\"literal\";",
7294             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7295   EXPECT_EQ("@\"NSString \"\n"
7296             "@\"literal\";",
7297             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7298   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7299 
7300   // This input makes clang-format try to split the incomplete unicode escape
7301   // sequence, which used to lead to a crasher.
7302   verifyNoCrash(
7303       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7304       getLLVMStyleWithColumns(60));
7305 }
7306 
7307 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7308   FormatStyle Style = getGoogleStyleWithColumns(15);
7309   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7310   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7311   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7312   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7313   EXPECT_EQ("u8R\"x(raw literal)x\";",
7314             format("u8R\"x(raw literal)x\";", Style));
7315 }
7316 
7317 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7318   FormatStyle Style = getLLVMStyleWithColumns(20);
7319   EXPECT_EQ(
7320       "_T(\"aaaaaaaaaaaaaa\")\n"
7321       "_T(\"aaaaaaaaaaaaaa\")\n"
7322       "_T(\"aaaaaaaaaaaa\")",
7323       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7324   EXPECT_EQ("f(x,\n"
7325             "  _T(\"aaaaaaaaaaaa\")\n"
7326             "  _T(\"aaa\"),\n"
7327             "  z);",
7328             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7329 
7330   // FIXME: Handle embedded spaces in one iteration.
7331   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7332   //            "_T(\"aaaaaaaaaaaaa\")\n"
7333   //            "_T(\"aaaaaaaaaaaaa\")\n"
7334   //            "_T(\"a\")",
7335   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7336   //                   getLLVMStyleWithColumns(20)));
7337   EXPECT_EQ(
7338       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7339       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7340   EXPECT_EQ("f(\n"
7341             "#if !TEST\n"
7342             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7343             "#endif\n"
7344             ");",
7345             format("f(\n"
7346                    "#if !TEST\n"
7347                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7348                    "#endif\n"
7349                    ");"));
7350   EXPECT_EQ("f(\n"
7351             "\n"
7352             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7353             format("f(\n"
7354                    "\n"
7355                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7356 }
7357 
7358 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7359   // In a function call with two operands, the second can be broken with no line
7360   // break before it.
7361   EXPECT_EQ("func(a, \"long long \"\n"
7362             "        \"long long\");",
7363             format("func(a, \"long long long long\");",
7364                    getLLVMStyleWithColumns(24)));
7365   // In a function call with three operands, the second must be broken with a
7366   // line break before it.
7367   EXPECT_EQ("func(a,\n"
7368             "     \"long long long \"\n"
7369             "     \"long\",\n"
7370             "     c);",
7371             format("func(a, \"long long long long\", c);",
7372                    getLLVMStyleWithColumns(24)));
7373   // In a function call with three operands, the third must be broken with a
7374   // line break before it.
7375   EXPECT_EQ("func(a, b,\n"
7376             "     \"long long long \"\n"
7377             "     \"long\");",
7378             format("func(a, b, \"long long long long\");",
7379                    getLLVMStyleWithColumns(24)));
7380   // In a function call with three operands, both the second and the third must
7381   // be broken with a line break before them.
7382   EXPECT_EQ("func(a,\n"
7383             "     \"long long long \"\n"
7384             "     \"long\",\n"
7385             "     \"long long long \"\n"
7386             "     \"long\");",
7387             format("func(a, \"long long long long\", \"long long long long\");",
7388                    getLLVMStyleWithColumns(24)));
7389   // In a chain of << with two operands, the second can be broken with no line
7390   // break before it.
7391   EXPECT_EQ("a << \"line line \"\n"
7392             "     \"line\";",
7393             format("a << \"line line line\";",
7394                    getLLVMStyleWithColumns(20)));
7395   // In a chain of << with three operands, the second can be broken with no line
7396   // break before it.
7397   EXPECT_EQ("abcde << \"line \"\n"
7398             "         \"line line\"\n"
7399             "      << c;",
7400             format("abcde << \"line line line\" << c;",
7401                    getLLVMStyleWithColumns(20)));
7402   // In a chain of << with three operands, the third must be broken with a line
7403   // break before it.
7404   EXPECT_EQ("a << b\n"
7405             "  << \"line line \"\n"
7406             "     \"line\";",
7407             format("a << b << \"line line line\";",
7408                    getLLVMStyleWithColumns(20)));
7409   // In a chain of << with three operands, the second can be broken with no line
7410   // break before it and the third must be broken with a line break before it.
7411   EXPECT_EQ("abcd << \"line line \"\n"
7412             "        \"line\"\n"
7413             "     << \"line line \"\n"
7414             "        \"line\";",
7415             format("abcd << \"line line line\" << \"line line line\";",
7416                    getLLVMStyleWithColumns(20)));
7417   // In a chain of binary operators with two operands, the second can be broken
7418   // with no line break before it.
7419   EXPECT_EQ("abcd + \"line line \"\n"
7420             "       \"line line\";",
7421             format("abcd + \"line line line line\";",
7422                    getLLVMStyleWithColumns(20)));
7423   // In a chain of binary operators with three operands, the second must be
7424   // broken with a line break before it.
7425   EXPECT_EQ("abcd +\n"
7426             "    \"line line \"\n"
7427             "    \"line line\" +\n"
7428             "    e;",
7429             format("abcd + \"line line line line\" + e;",
7430                    getLLVMStyleWithColumns(20)));
7431   // In a function call with two operands, with AlignAfterOpenBracket enabled,
7432   // the first must be broken with a line break before it.
7433   FormatStyle Style = getLLVMStyleWithColumns(25);
7434   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7435   EXPECT_EQ("someFunction(\n"
7436             "    \"long long long \"\n"
7437             "    \"long\",\n"
7438             "    a);",
7439             format("someFunction(\"long long long long\", a);", Style));
7440 }
7441 
7442 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7443   EXPECT_EQ(
7444       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7445       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7446       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7447       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7448              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7449              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7450 }
7451 
7452 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7453   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7454             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7455   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7456             "multiline raw string literal xxxxxxxxxxxxxx\n"
7457             ")x\",\n"
7458             "              a),\n"
7459             "            b);",
7460             format("fffffffffff(g(R\"x(\n"
7461                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7462                    ")x\", a), b);",
7463                    getGoogleStyleWithColumns(20)));
7464   EXPECT_EQ("fffffffffff(\n"
7465             "    g(R\"x(qqq\n"
7466             "multiline raw string literal xxxxxxxxxxxxxx\n"
7467             ")x\",\n"
7468             "      a),\n"
7469             "    b);",
7470             format("fffffffffff(g(R\"x(qqq\n"
7471                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7472                    ")x\", a), b);",
7473                    getGoogleStyleWithColumns(20)));
7474 
7475   EXPECT_EQ("fffffffffff(R\"x(\n"
7476             "multiline raw string literal xxxxxxxxxxxxxx\n"
7477             ")x\");",
7478             format("fffffffffff(R\"x(\n"
7479                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7480                    ")x\");",
7481                    getGoogleStyleWithColumns(20)));
7482   EXPECT_EQ("fffffffffff(R\"x(\n"
7483             "multiline raw string literal xxxxxxxxxxxxxx\n"
7484             ")x\" + bbbbbb);",
7485             format("fffffffffff(R\"x(\n"
7486                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7487                    ")x\" +   bbbbbb);",
7488                    getGoogleStyleWithColumns(20)));
7489   EXPECT_EQ("fffffffffff(\n"
7490             "    R\"x(\n"
7491             "multiline raw string literal xxxxxxxxxxxxxx\n"
7492             ")x\" +\n"
7493             "    bbbbbb);",
7494             format("fffffffffff(\n"
7495                    " R\"x(\n"
7496                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7497                    ")x\" + bbbbbb);",
7498                    getGoogleStyleWithColumns(20)));
7499 }
7500 
7501 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7502   verifyFormat("string a = \"unterminated;");
7503   EXPECT_EQ("function(\"unterminated,\n"
7504             "         OtherParameter);",
7505             format("function(  \"unterminated,\n"
7506                    "    OtherParameter);"));
7507 }
7508 
7509 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7510   FormatStyle Style = getLLVMStyle();
7511   Style.Standard = FormatStyle::LS_Cpp03;
7512   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7513             format("#define x(_a) printf(\"foo\"_a);", Style));
7514 }
7515 
7516 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7517 
7518 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7519   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7520             "             \"ddeeefff\");",
7521             format("someFunction(\"aaabbbcccdddeeefff\");",
7522                    getLLVMStyleWithColumns(25)));
7523   EXPECT_EQ("someFunction1234567890(\n"
7524             "    \"aaabbbcccdddeeefff\");",
7525             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7526                    getLLVMStyleWithColumns(26)));
7527   EXPECT_EQ("someFunction1234567890(\n"
7528             "    \"aaabbbcccdddeeeff\"\n"
7529             "    \"f\");",
7530             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7531                    getLLVMStyleWithColumns(25)));
7532   EXPECT_EQ("someFunction1234567890(\n"
7533             "    \"aaabbbcccdddeeeff\"\n"
7534             "    \"f\");",
7535             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7536                    getLLVMStyleWithColumns(24)));
7537   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7538             "             \"ddde \"\n"
7539             "             \"efff\");",
7540             format("someFunction(\"aaabbbcc ddde efff\");",
7541                    getLLVMStyleWithColumns(25)));
7542   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7543             "             \"ddeeefff\");",
7544             format("someFunction(\"aaabbbccc ddeeefff\");",
7545                    getLLVMStyleWithColumns(25)));
7546   EXPECT_EQ("someFunction1234567890(\n"
7547             "    \"aaabb \"\n"
7548             "    \"cccdddeeefff\");",
7549             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7550                    getLLVMStyleWithColumns(25)));
7551   EXPECT_EQ("#define A          \\\n"
7552             "  string s =       \\\n"
7553             "      \"123456789\"  \\\n"
7554             "      \"0\";         \\\n"
7555             "  int i;",
7556             format("#define A string s = \"1234567890\"; int i;",
7557                    getLLVMStyleWithColumns(20)));
7558   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7559   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7560             "             \"dddeeeff\"\n"
7561             "             \"f\");",
7562             format("someFunction(\"aaabbbcc dddeeefff\");",
7563                    getLLVMStyleWithColumns(25)));
7564 }
7565 
7566 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7567   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7568   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7569   EXPECT_EQ("\"test\"\n"
7570             "\"\\n\"",
7571             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7572   EXPECT_EQ("\"tes\\\\\"\n"
7573             "\"n\"",
7574             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7575   EXPECT_EQ("\"\\\\\\\\\"\n"
7576             "\"\\n\"",
7577             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7578   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7579   EXPECT_EQ("\"\\uff01\"\n"
7580             "\"test\"",
7581             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7582   EXPECT_EQ("\"\\Uff01ff02\"",
7583             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7584   EXPECT_EQ("\"\\x000000000001\"\n"
7585             "\"next\"",
7586             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7587   EXPECT_EQ("\"\\x000000000001next\"",
7588             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7589   EXPECT_EQ("\"\\x000000000001\"",
7590             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7591   EXPECT_EQ("\"test\"\n"
7592             "\"\\000000\"\n"
7593             "\"000001\"",
7594             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7595   EXPECT_EQ("\"test\\000\"\n"
7596             "\"00000000\"\n"
7597             "\"1\"",
7598             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7599 }
7600 
7601 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7602   verifyFormat("void f() {\n"
7603                "  return g() {}\n"
7604                "  void h() {}");
7605   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7606                "g();\n"
7607                "}");
7608 }
7609 
7610 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7611   verifyFormat(
7612       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7613 }
7614 
7615 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7616   verifyFormat("class X {\n"
7617                "  void f() {\n"
7618                "  }\n"
7619                "};",
7620                getLLVMStyleWithColumns(12));
7621 }
7622 
7623 TEST_F(FormatTest, ConfigurableIndentWidth) {
7624   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7625   EightIndent.IndentWidth = 8;
7626   EightIndent.ContinuationIndentWidth = 8;
7627   verifyFormat("void f() {\n"
7628                "        someFunction();\n"
7629                "        if (true) {\n"
7630                "                f();\n"
7631                "        }\n"
7632                "}",
7633                EightIndent);
7634   verifyFormat("class X {\n"
7635                "        void f() {\n"
7636                "        }\n"
7637                "};",
7638                EightIndent);
7639   verifyFormat("int x[] = {\n"
7640                "        call(),\n"
7641                "        call()};",
7642                EightIndent);
7643 }
7644 
7645 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7646   verifyFormat("double\n"
7647                "f();",
7648                getLLVMStyleWithColumns(8));
7649 }
7650 
7651 TEST_F(FormatTest, ConfigurableUseOfTab) {
7652   FormatStyle Tab = getLLVMStyleWithColumns(42);
7653   Tab.IndentWidth = 8;
7654   Tab.UseTab = FormatStyle::UT_Always;
7655   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7656 
7657   EXPECT_EQ("if (aaaaaaaa && // q\n"
7658             "    bb)\t\t// w\n"
7659             "\t;",
7660             format("if (aaaaaaaa &&// q\n"
7661                    "bb)// w\n"
7662                    ";",
7663                    Tab));
7664   EXPECT_EQ("if (aaa && bbb) // w\n"
7665             "\t;",
7666             format("if(aaa&&bbb)// w\n"
7667                    ";",
7668                    Tab));
7669 
7670   verifyFormat("class X {\n"
7671                "\tvoid f() {\n"
7672                "\t\tsomeFunction(parameter1,\n"
7673                "\t\t\t     parameter2);\n"
7674                "\t}\n"
7675                "};",
7676                Tab);
7677   verifyFormat("#define A                        \\\n"
7678                "\tvoid f() {               \\\n"
7679                "\t\tsomeFunction(    \\\n"
7680                "\t\t    parameter1,  \\\n"
7681                "\t\t    parameter2); \\\n"
7682                "\t}",
7683                Tab);
7684 
7685   Tab.TabWidth = 4;
7686   Tab.IndentWidth = 8;
7687   verifyFormat("class TabWidth4Indent8 {\n"
7688                "\t\tvoid f() {\n"
7689                "\t\t\t\tsomeFunction(parameter1,\n"
7690                "\t\t\t\t\t\t\t parameter2);\n"
7691                "\t\t}\n"
7692                "};",
7693                Tab);
7694 
7695   Tab.TabWidth = 4;
7696   Tab.IndentWidth = 4;
7697   verifyFormat("class TabWidth4Indent4 {\n"
7698                "\tvoid f() {\n"
7699                "\t\tsomeFunction(parameter1,\n"
7700                "\t\t\t\t\t parameter2);\n"
7701                "\t}\n"
7702                "};",
7703                Tab);
7704 
7705   Tab.TabWidth = 8;
7706   Tab.IndentWidth = 4;
7707   verifyFormat("class TabWidth8Indent4 {\n"
7708                "    void f() {\n"
7709                "\tsomeFunction(parameter1,\n"
7710                "\t\t     parameter2);\n"
7711                "    }\n"
7712                "};",
7713                Tab);
7714 
7715   Tab.TabWidth = 8;
7716   Tab.IndentWidth = 8;
7717   EXPECT_EQ("/*\n"
7718             "\t      a\t\tcomment\n"
7719             "\t      in multiple lines\n"
7720             "       */",
7721             format("   /*\t \t \n"
7722                    " \t \t a\t\tcomment\t \t\n"
7723                    " \t \t in multiple lines\t\n"
7724                    " \t  */",
7725                    Tab));
7726 
7727   Tab.UseTab = FormatStyle::UT_ForIndentation;
7728   verifyFormat("{\n"
7729                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7730                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7731                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7732                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7733                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7734                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7735                "};",
7736                Tab);
7737   verifyFormat("enum AA {\n"
7738                "\ta1, // Force multiple lines\n"
7739                "\ta2,\n"
7740                "\ta3\n"
7741                "};",
7742                Tab);
7743   EXPECT_EQ("if (aaaaaaaa && // q\n"
7744             "    bb)         // w\n"
7745             "\t;",
7746             format("if (aaaaaaaa &&// q\n"
7747                    "bb)// w\n"
7748                    ";",
7749                    Tab));
7750   verifyFormat("class X {\n"
7751                "\tvoid f() {\n"
7752                "\t\tsomeFunction(parameter1,\n"
7753                "\t\t             parameter2);\n"
7754                "\t}\n"
7755                "};",
7756                Tab);
7757   verifyFormat("{\n"
7758                "\tQ(\n"
7759                "\t    {\n"
7760                "\t\t    int a;\n"
7761                "\t\t    someFunction(aaaaaaaa,\n"
7762                "\t\t                 bbbbbbb);\n"
7763                "\t    },\n"
7764                "\t    p);\n"
7765                "}",
7766                Tab);
7767   EXPECT_EQ("{\n"
7768             "\t/* aaaa\n"
7769             "\t   bbbb */\n"
7770             "}",
7771             format("{\n"
7772                    "/* aaaa\n"
7773                    "   bbbb */\n"
7774                    "}",
7775                    Tab));
7776   EXPECT_EQ("{\n"
7777             "\t/*\n"
7778             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7779             "\t  bbbbbbbbbbbbb\n"
7780             "\t*/\n"
7781             "}",
7782             format("{\n"
7783                    "/*\n"
7784                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7785                    "*/\n"
7786                    "}",
7787                    Tab));
7788   EXPECT_EQ("{\n"
7789             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7790             "\t// bbbbbbbbbbbbb\n"
7791             "}",
7792             format("{\n"
7793                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7794                    "}",
7795                    Tab));
7796   EXPECT_EQ("{\n"
7797             "\t/*\n"
7798             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7799             "\t  bbbbbbbbbbbbb\n"
7800             "\t*/\n"
7801             "}",
7802             format("{\n"
7803                    "\t/*\n"
7804                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7805                    "\t*/\n"
7806                    "}",
7807                    Tab));
7808   EXPECT_EQ("{\n"
7809             "\t/*\n"
7810             "\n"
7811             "\t*/\n"
7812             "}",
7813             format("{\n"
7814                    "\t/*\n"
7815                    "\n"
7816                    "\t*/\n"
7817                    "}",
7818                    Tab));
7819   EXPECT_EQ("{\n"
7820             "\t/*\n"
7821             " asdf\n"
7822             "\t*/\n"
7823             "}",
7824             format("{\n"
7825                    "\t/*\n"
7826                    " asdf\n"
7827                    "\t*/\n"
7828                    "}",
7829                    Tab));
7830 
7831   Tab.UseTab = FormatStyle::UT_Never;
7832   EXPECT_EQ("/*\n"
7833             "              a\t\tcomment\n"
7834             "              in multiple lines\n"
7835             "       */",
7836             format("   /*\t \t \n"
7837                    " \t \t a\t\tcomment\t \t\n"
7838                    " \t \t in multiple lines\t\n"
7839                    " \t  */",
7840                    Tab));
7841   EXPECT_EQ("/* some\n"
7842             "   comment */",
7843             format(" \t \t /* some\n"
7844                    " \t \t    comment */",
7845                    Tab));
7846   EXPECT_EQ("int a; /* some\n"
7847             "   comment */",
7848             format(" \t \t int a; /* some\n"
7849                    " \t \t    comment */",
7850                    Tab));
7851 
7852   EXPECT_EQ("int a; /* some\n"
7853             "comment */",
7854             format(" \t \t int\ta; /* some\n"
7855                    " \t \t    comment */",
7856                    Tab));
7857   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7858             "    comment */",
7859             format(" \t \t f(\"\t\t\"); /* some\n"
7860                    " \t \t    comment */",
7861                    Tab));
7862   EXPECT_EQ("{\n"
7863             "  /*\n"
7864             "   * Comment\n"
7865             "   */\n"
7866             "  int i;\n"
7867             "}",
7868             format("{\n"
7869                    "\t/*\n"
7870                    "\t * Comment\n"
7871                    "\t */\n"
7872                    "\t int i;\n"
7873                    "}"));
7874 
7875   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
7876   Tab.TabWidth = 8;
7877   Tab.IndentWidth = 8;
7878   EXPECT_EQ("if (aaaaaaaa && // q\n"
7879             "    bb)         // w\n"
7880             "\t;",
7881             format("if (aaaaaaaa &&// q\n"
7882                    "bb)// w\n"
7883                    ";",
7884                    Tab));
7885   EXPECT_EQ("if (aaa && bbb) // w\n"
7886             "\t;",
7887             format("if(aaa&&bbb)// w\n"
7888                    ";",
7889                    Tab));
7890   verifyFormat("class X {\n"
7891                "\tvoid f() {\n"
7892                "\t\tsomeFunction(parameter1,\n"
7893                "\t\t\t     parameter2);\n"
7894                "\t}\n"
7895                "};",
7896                Tab);
7897   verifyFormat("#define A                        \\\n"
7898                "\tvoid f() {               \\\n"
7899                "\t\tsomeFunction(    \\\n"
7900                "\t\t    parameter1,  \\\n"
7901                "\t\t    parameter2); \\\n"
7902                "\t}",
7903                Tab);
7904   Tab.TabWidth = 4;
7905   Tab.IndentWidth = 8;
7906   verifyFormat("class TabWidth4Indent8 {\n"
7907                "\t\tvoid f() {\n"
7908                "\t\t\t\tsomeFunction(parameter1,\n"
7909                "\t\t\t\t\t\t\t parameter2);\n"
7910                "\t\t}\n"
7911                "};",
7912                Tab);
7913   Tab.TabWidth = 4;
7914   Tab.IndentWidth = 4;
7915   verifyFormat("class TabWidth4Indent4 {\n"
7916                "\tvoid f() {\n"
7917                "\t\tsomeFunction(parameter1,\n"
7918                "\t\t\t\t\t parameter2);\n"
7919                "\t}\n"
7920                "};",
7921                Tab);
7922   Tab.TabWidth = 8;
7923   Tab.IndentWidth = 4;
7924   verifyFormat("class TabWidth8Indent4 {\n"
7925                "    void f() {\n"
7926                "\tsomeFunction(parameter1,\n"
7927                "\t\t     parameter2);\n"
7928                "    }\n"
7929                "};",
7930                Tab);
7931   Tab.TabWidth = 8;
7932   Tab.IndentWidth = 8;
7933   EXPECT_EQ("/*\n"
7934             "\t      a\t\tcomment\n"
7935             "\t      in multiple lines\n"
7936             "       */",
7937             format("   /*\t \t \n"
7938                    " \t \t a\t\tcomment\t \t\n"
7939                    " \t \t in multiple lines\t\n"
7940                    " \t  */",
7941                    Tab));
7942   verifyFormat("{\n"
7943                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7944                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7945                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7946                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7947                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7948                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7949                "};",
7950                Tab);
7951   verifyFormat("enum AA {\n"
7952                "\ta1, // Force multiple lines\n"
7953                "\ta2,\n"
7954                "\ta3\n"
7955                "};",
7956                Tab);
7957   EXPECT_EQ("if (aaaaaaaa && // q\n"
7958             "    bb)         // w\n"
7959             "\t;",
7960             format("if (aaaaaaaa &&// q\n"
7961                    "bb)// w\n"
7962                    ";",
7963                    Tab));
7964   verifyFormat("class X {\n"
7965                "\tvoid f() {\n"
7966                "\t\tsomeFunction(parameter1,\n"
7967                "\t\t\t     parameter2);\n"
7968                "\t}\n"
7969                "};",
7970                Tab);
7971   verifyFormat("{\n"
7972                "\tQ(\n"
7973                "\t    {\n"
7974                "\t\t    int a;\n"
7975                "\t\t    someFunction(aaaaaaaa,\n"
7976                "\t\t\t\t bbbbbbb);\n"
7977                "\t    },\n"
7978                "\t    p);\n"
7979                "}",
7980                Tab);
7981   EXPECT_EQ("{\n"
7982             "\t/* aaaa\n"
7983             "\t   bbbb */\n"
7984             "}",
7985             format("{\n"
7986                    "/* aaaa\n"
7987                    "   bbbb */\n"
7988                    "}",
7989                    Tab));
7990   EXPECT_EQ("{\n"
7991             "\t/*\n"
7992             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7993             "\t  bbbbbbbbbbbbb\n"
7994             "\t*/\n"
7995             "}",
7996             format("{\n"
7997                    "/*\n"
7998                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7999                    "*/\n"
8000                    "}",
8001                    Tab));
8002   EXPECT_EQ("{\n"
8003             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8004             "\t// bbbbbbbbbbbbb\n"
8005             "}",
8006             format("{\n"
8007                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8008                    "}",
8009                    Tab));
8010   EXPECT_EQ("{\n"
8011             "\t/*\n"
8012             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8013             "\t  bbbbbbbbbbbbb\n"
8014             "\t*/\n"
8015             "}",
8016             format("{\n"
8017                    "\t/*\n"
8018                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8019                    "\t*/\n"
8020                    "}",
8021                    Tab));
8022   EXPECT_EQ("{\n"
8023             "\t/*\n"
8024             "\n"
8025             "\t*/\n"
8026             "}",
8027             format("{\n"
8028                    "\t/*\n"
8029                    "\n"
8030                    "\t*/\n"
8031                    "}",
8032                    Tab));
8033   EXPECT_EQ("{\n"
8034             "\t/*\n"
8035             " asdf\n"
8036             "\t*/\n"
8037             "}",
8038             format("{\n"
8039                    "\t/*\n"
8040                    " asdf\n"
8041                    "\t*/\n"
8042                    "}",
8043                    Tab));
8044   EXPECT_EQ("/*\n"
8045             "\t      a\t\tcomment\n"
8046             "\t      in multiple lines\n"
8047             "       */",
8048             format("   /*\t \t \n"
8049                    " \t \t a\t\tcomment\t \t\n"
8050                    " \t \t in multiple lines\t\n"
8051                    " \t  */",
8052                    Tab));
8053   EXPECT_EQ("/* some\n"
8054             "   comment */",
8055             format(" \t \t /* some\n"
8056                    " \t \t    comment */",
8057                    Tab));
8058   EXPECT_EQ("int a; /* some\n"
8059             "   comment */",
8060             format(" \t \t int a; /* some\n"
8061                    " \t \t    comment */",
8062                    Tab));
8063   EXPECT_EQ("int a; /* some\n"
8064             "comment */",
8065             format(" \t \t int\ta; /* some\n"
8066                    " \t \t    comment */",
8067                    Tab));
8068   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8069             "    comment */",
8070             format(" \t \t f(\"\t\t\"); /* some\n"
8071                    " \t \t    comment */",
8072                    Tab));
8073   EXPECT_EQ("{\n"
8074             "  /*\n"
8075             "   * Comment\n"
8076             "   */\n"
8077             "  int i;\n"
8078             "}",
8079             format("{\n"
8080                    "\t/*\n"
8081                    "\t * Comment\n"
8082                    "\t */\n"
8083                    "\t int i;\n"
8084                    "}"));
8085   Tab.AlignConsecutiveAssignments = true;
8086   Tab.AlignConsecutiveDeclarations = true;
8087   Tab.TabWidth = 4;
8088   Tab.IndentWidth = 4;
8089   verifyFormat("class Assign {\n"
8090                "\tvoid f() {\n"
8091                "\t\tint         x      = 123;\n"
8092                "\t\tint         random = 4;\n"
8093                "\t\tstd::string alphabet =\n"
8094                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8095                "\t}\n"
8096                "};",
8097                Tab);
8098 }
8099 
8100 TEST_F(FormatTest, CalculatesOriginalColumn) {
8101   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8102             "q\"; /* some\n"
8103             "       comment */",
8104             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8105                    "q\"; /* some\n"
8106                    "       comment */",
8107                    getLLVMStyle()));
8108   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8109             "/* some\n"
8110             "   comment */",
8111             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8112                    " /* some\n"
8113                    "    comment */",
8114                    getLLVMStyle()));
8115   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8116             "qqq\n"
8117             "/* some\n"
8118             "   comment */",
8119             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8120                    "qqq\n"
8121                    " /* some\n"
8122                    "    comment */",
8123                    getLLVMStyle()));
8124   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8125             "wwww; /* some\n"
8126             "         comment */",
8127             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8128                    "wwww; /* some\n"
8129                    "         comment */",
8130                    getLLVMStyle()));
8131 }
8132 
8133 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8134   FormatStyle NoSpace = getLLVMStyle();
8135   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8136 
8137   verifyFormat("while(true)\n"
8138                "  continue;",
8139                NoSpace);
8140   verifyFormat("for(;;)\n"
8141                "  continue;",
8142                NoSpace);
8143   verifyFormat("if(true)\n"
8144                "  f();\n"
8145                "else if(true)\n"
8146                "  f();",
8147                NoSpace);
8148   verifyFormat("do {\n"
8149                "  do_something();\n"
8150                "} while(something());",
8151                NoSpace);
8152   verifyFormat("switch(x) {\n"
8153                "default:\n"
8154                "  break;\n"
8155                "}",
8156                NoSpace);
8157   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8158   verifyFormat("size_t x = sizeof(x);", NoSpace);
8159   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8160   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8161   verifyFormat("alignas(128) char a[128];", NoSpace);
8162   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8163   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8164   verifyFormat("int f() throw(Deprecated);", NoSpace);
8165   verifyFormat("typedef void (*cb)(int);", NoSpace);
8166   verifyFormat("T A::operator()();", NoSpace);
8167   verifyFormat("X A::operator++(T);", NoSpace);
8168 
8169   FormatStyle Space = getLLVMStyle();
8170   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8171 
8172   verifyFormat("int f ();", Space);
8173   verifyFormat("void f (int a, T b) {\n"
8174                "  while (true)\n"
8175                "    continue;\n"
8176                "}",
8177                Space);
8178   verifyFormat("if (true)\n"
8179                "  f ();\n"
8180                "else if (true)\n"
8181                "  f ();",
8182                Space);
8183   verifyFormat("do {\n"
8184                "  do_something ();\n"
8185                "} while (something ());",
8186                Space);
8187   verifyFormat("switch (x) {\n"
8188                "default:\n"
8189                "  break;\n"
8190                "}",
8191                Space);
8192   verifyFormat("A::A () : a (1) {}", Space);
8193   verifyFormat("void f () __attribute__ ((asdf));", Space);
8194   verifyFormat("*(&a + 1);\n"
8195                "&((&a)[1]);\n"
8196                "a[(b + c) * d];\n"
8197                "(((a + 1) * 2) + 3) * 4;",
8198                Space);
8199   verifyFormat("#define A(x) x", Space);
8200   verifyFormat("#define A (x) x", Space);
8201   verifyFormat("#if defined(x)\n"
8202                "#endif",
8203                Space);
8204   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8205   verifyFormat("size_t x = sizeof (x);", Space);
8206   verifyFormat("auto f (int x) -> decltype (x);", Space);
8207   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8208   verifyFormat("alignas (128) char a[128];", Space);
8209   verifyFormat("size_t x = alignof (MyType);", Space);
8210   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8211   verifyFormat("int f () throw (Deprecated);", Space);
8212   verifyFormat("typedef void (*cb) (int);", Space);
8213   verifyFormat("T A::operator() ();", Space);
8214   verifyFormat("X A::operator++ (T);", Space);
8215 }
8216 
8217 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8218   FormatStyle Spaces = getLLVMStyle();
8219 
8220   Spaces.SpacesInParentheses = true;
8221   verifyFormat("call( x, y, z );", Spaces);
8222   verifyFormat("call();", Spaces);
8223   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8224   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8225                Spaces);
8226   verifyFormat("while ( (bool)1 )\n"
8227                "  continue;",
8228                Spaces);
8229   verifyFormat("for ( ;; )\n"
8230                "  continue;",
8231                Spaces);
8232   verifyFormat("if ( true )\n"
8233                "  f();\n"
8234                "else if ( true )\n"
8235                "  f();",
8236                Spaces);
8237   verifyFormat("do {\n"
8238                "  do_something( (int)i );\n"
8239                "} while ( something() );",
8240                Spaces);
8241   verifyFormat("switch ( x ) {\n"
8242                "default:\n"
8243                "  break;\n"
8244                "}",
8245                Spaces);
8246 
8247   Spaces.SpacesInParentheses = false;
8248   Spaces.SpacesInCStyleCastParentheses = true;
8249   verifyFormat("Type *A = ( Type * )P;", Spaces);
8250   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8251   verifyFormat("x = ( int32 )y;", Spaces);
8252   verifyFormat("int a = ( int )(2.0f);", Spaces);
8253   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8254   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8255   verifyFormat("#define x (( int )-1)", Spaces);
8256 
8257   // Run the first set of tests again with:
8258   Spaces.SpacesInParentheses = false;
8259   Spaces.SpaceInEmptyParentheses = true;
8260   Spaces.SpacesInCStyleCastParentheses = true;
8261   verifyFormat("call(x, y, z);", Spaces);
8262   verifyFormat("call( );", Spaces);
8263   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8264   verifyFormat("while (( bool )1)\n"
8265                "  continue;",
8266                Spaces);
8267   verifyFormat("for (;;)\n"
8268                "  continue;",
8269                Spaces);
8270   verifyFormat("if (true)\n"
8271                "  f( );\n"
8272                "else if (true)\n"
8273                "  f( );",
8274                Spaces);
8275   verifyFormat("do {\n"
8276                "  do_something(( int )i);\n"
8277                "} while (something( ));",
8278                Spaces);
8279   verifyFormat("switch (x) {\n"
8280                "default:\n"
8281                "  break;\n"
8282                "}",
8283                Spaces);
8284 
8285   // Run the first set of tests again with:
8286   Spaces.SpaceAfterCStyleCast = true;
8287   verifyFormat("call(x, y, z);", Spaces);
8288   verifyFormat("call( );", Spaces);
8289   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8290   verifyFormat("while (( bool ) 1)\n"
8291                "  continue;",
8292                Spaces);
8293   verifyFormat("for (;;)\n"
8294                "  continue;",
8295                Spaces);
8296   verifyFormat("if (true)\n"
8297                "  f( );\n"
8298                "else if (true)\n"
8299                "  f( );",
8300                Spaces);
8301   verifyFormat("do {\n"
8302                "  do_something(( int ) i);\n"
8303                "} while (something( ));",
8304                Spaces);
8305   verifyFormat("switch (x) {\n"
8306                "default:\n"
8307                "  break;\n"
8308                "}",
8309                Spaces);
8310 
8311   // Run subset of tests again with:
8312   Spaces.SpacesInCStyleCastParentheses = false;
8313   Spaces.SpaceAfterCStyleCast = true;
8314   verifyFormat("while ((bool) 1)\n"
8315                "  continue;",
8316                Spaces);
8317   verifyFormat("do {\n"
8318                "  do_something((int) i);\n"
8319                "} while (something( ));",
8320                Spaces);
8321 }
8322 
8323 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8324   verifyFormat("int a[5];");
8325   verifyFormat("a[3] += 42;");
8326 
8327   FormatStyle Spaces = getLLVMStyle();
8328   Spaces.SpacesInSquareBrackets = true;
8329   // Lambdas unchanged.
8330   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8331   verifyFormat("return [i, args...] {};", Spaces);
8332 
8333   // Not lambdas.
8334   verifyFormat("int a[ 5 ];", Spaces);
8335   verifyFormat("a[ 3 ] += 42;", Spaces);
8336   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8337   verifyFormat("double &operator[](int i) { return 0; }\n"
8338                "int i;",
8339                Spaces);
8340   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8341   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8342   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8343 }
8344 
8345 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8346   verifyFormat("int a = 5;");
8347   verifyFormat("a += 42;");
8348   verifyFormat("a or_eq 8;");
8349 
8350   FormatStyle Spaces = getLLVMStyle();
8351   Spaces.SpaceBeforeAssignmentOperators = false;
8352   verifyFormat("int a= 5;", Spaces);
8353   verifyFormat("a+= 42;", Spaces);
8354   verifyFormat("a or_eq 8;", Spaces);
8355 }
8356 
8357 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8358   FormatStyle Alignment = getLLVMStyle();
8359   Alignment.AlignConsecutiveAssignments = false;
8360   verifyFormat("int a = 5;\n"
8361                "int oneTwoThree = 123;",
8362                Alignment);
8363   verifyFormat("int a = 5;\n"
8364                "int oneTwoThree = 123;",
8365                Alignment);
8366 
8367   Alignment.AlignConsecutiveAssignments = true;
8368   verifyFormat("int a           = 5;\n"
8369                "int oneTwoThree = 123;",
8370                Alignment);
8371   verifyFormat("int a           = method();\n"
8372                "int oneTwoThree = 133;",
8373                Alignment);
8374   verifyFormat("a &= 5;\n"
8375                "bcd *= 5;\n"
8376                "ghtyf += 5;\n"
8377                "dvfvdb -= 5;\n"
8378                "a /= 5;\n"
8379                "vdsvsv %= 5;\n"
8380                "sfdbddfbdfbb ^= 5;\n"
8381                "dvsdsv |= 5;\n"
8382                "int dsvvdvsdvvv = 123;",
8383                Alignment);
8384   verifyFormat("int i = 1, j = 10;\n"
8385                "something = 2000;",
8386                Alignment);
8387   verifyFormat("something = 2000;\n"
8388                "int i = 1, j = 10;\n",
8389                Alignment);
8390   verifyFormat("something = 2000;\n"
8391                "another   = 911;\n"
8392                "int i = 1, j = 10;\n"
8393                "oneMore = 1;\n"
8394                "i       = 2;",
8395                Alignment);
8396   verifyFormat("int a   = 5;\n"
8397                "int one = 1;\n"
8398                "method();\n"
8399                "int oneTwoThree = 123;\n"
8400                "int oneTwo      = 12;",
8401                Alignment);
8402   verifyFormat("int oneTwoThree = 123;\n"
8403                "int oneTwo      = 12;\n"
8404                "method();\n",
8405                Alignment);
8406   verifyFormat("int oneTwoThree = 123; // comment\n"
8407                "int oneTwo      = 12;  // comment",
8408                Alignment);
8409   EXPECT_EQ("int a = 5;\n"
8410             "\n"
8411             "int oneTwoThree = 123;",
8412             format("int a       = 5;\n"
8413                    "\n"
8414                    "int oneTwoThree= 123;",
8415                    Alignment));
8416   EXPECT_EQ("int a   = 5;\n"
8417             "int one = 1;\n"
8418             "\n"
8419             "int oneTwoThree = 123;",
8420             format("int a = 5;\n"
8421                    "int one = 1;\n"
8422                    "\n"
8423                    "int oneTwoThree = 123;",
8424                    Alignment));
8425   EXPECT_EQ("int a   = 5;\n"
8426             "int one = 1;\n"
8427             "\n"
8428             "int oneTwoThree = 123;\n"
8429             "int oneTwo      = 12;",
8430             format("int a = 5;\n"
8431                    "int one = 1;\n"
8432                    "\n"
8433                    "int oneTwoThree = 123;\n"
8434                    "int oneTwo = 12;",
8435                    Alignment));
8436   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8437   verifyFormat("#define A \\\n"
8438                "  int aaaa       = 12; \\\n"
8439                "  int b          = 23; \\\n"
8440                "  int ccc        = 234; \\\n"
8441                "  int dddddddddd = 2345;",
8442                Alignment);
8443   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8444   verifyFormat("#define A               \\\n"
8445                "  int aaaa       = 12;  \\\n"
8446                "  int b          = 23;  \\\n"
8447                "  int ccc        = 234; \\\n"
8448                "  int dddddddddd = 2345;",
8449                Alignment);
8450   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8451   verifyFormat("#define A                                                      "
8452                "                \\\n"
8453                "  int aaaa       = 12;                                         "
8454                "                \\\n"
8455                "  int b          = 23;                                         "
8456                "                \\\n"
8457                "  int ccc        = 234;                                        "
8458                "                \\\n"
8459                "  int dddddddddd = 2345;",
8460                Alignment);
8461   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8462                "k = 4, int l = 5,\n"
8463                "                  int m = 6) {\n"
8464                "  int j      = 10;\n"
8465                "  otherThing = 1;\n"
8466                "}",
8467                Alignment);
8468   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8469                "  int i   = 1;\n"
8470                "  int j   = 2;\n"
8471                "  int big = 10000;\n"
8472                "}",
8473                Alignment);
8474   verifyFormat("class C {\n"
8475                "public:\n"
8476                "  int i            = 1;\n"
8477                "  virtual void f() = 0;\n"
8478                "};",
8479                Alignment);
8480   verifyFormat("int i = 1;\n"
8481                "if (SomeType t = getSomething()) {\n"
8482                "}\n"
8483                "int j   = 2;\n"
8484                "int big = 10000;",
8485                Alignment);
8486   verifyFormat("int j = 7;\n"
8487                "for (int k = 0; k < N; ++k) {\n"
8488                "}\n"
8489                "int j   = 2;\n"
8490                "int big = 10000;\n"
8491                "}",
8492                Alignment);
8493   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8494   verifyFormat("int i = 1;\n"
8495                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8496                "    = someLooooooooooooooooongFunction();\n"
8497                "int j = 2;",
8498                Alignment);
8499   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8500   verifyFormat("int i = 1;\n"
8501                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8502                "    someLooooooooooooooooongFunction();\n"
8503                "int j = 2;",
8504                Alignment);
8505 
8506   verifyFormat("auto lambda = []() {\n"
8507                "  auto i = 0;\n"
8508                "  return 0;\n"
8509                "};\n"
8510                "int i  = 0;\n"
8511                "auto v = type{\n"
8512                "    i = 1,   //\n"
8513                "    (i = 2), //\n"
8514                "    i = 3    //\n"
8515                "};",
8516                Alignment);
8517 
8518   verifyFormat(
8519       "int i      = 1;\n"
8520       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8521       "                          loooooooooooooooooooooongParameterB);\n"
8522       "int j      = 2;",
8523       Alignment);
8524 
8525   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8526                "          typename B   = very_long_type_name_1,\n"
8527                "          typename T_2 = very_long_type_name_2>\n"
8528                "auto foo() {}\n",
8529                Alignment);
8530   verifyFormat("int a, b = 1;\n"
8531                "int c  = 2;\n"
8532                "int dd = 3;\n",
8533                Alignment);
8534   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8535                "float b[1][] = {{3.f}};\n",
8536                Alignment);
8537   verifyFormat("for (int i = 0; i < 1; i++)\n"
8538                "  int x = 1;\n",
8539                Alignment);
8540   verifyFormat("for (i = 0; i < 1; i++)\n"
8541                "  x = 1;\n"
8542                "y = 1;\n",
8543                Alignment);
8544 }
8545 
8546 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8547   FormatStyle Alignment = getLLVMStyle();
8548   Alignment.AlignConsecutiveDeclarations = false;
8549   verifyFormat("float const a = 5;\n"
8550                "int oneTwoThree = 123;",
8551                Alignment);
8552   verifyFormat("int a = 5;\n"
8553                "float const oneTwoThree = 123;",
8554                Alignment);
8555 
8556   Alignment.AlignConsecutiveDeclarations = true;
8557   verifyFormat("float const a = 5;\n"
8558                "int         oneTwoThree = 123;",
8559                Alignment);
8560   verifyFormat("int         a = method();\n"
8561                "float const oneTwoThree = 133;",
8562                Alignment);
8563   verifyFormat("int i = 1, j = 10;\n"
8564                "something = 2000;",
8565                Alignment);
8566   verifyFormat("something = 2000;\n"
8567                "int i = 1, j = 10;\n",
8568                Alignment);
8569   verifyFormat("float      something = 2000;\n"
8570                "double     another = 911;\n"
8571                "int        i = 1, j = 10;\n"
8572                "const int *oneMore = 1;\n"
8573                "unsigned   i = 2;",
8574                Alignment);
8575   verifyFormat("float a = 5;\n"
8576                "int   one = 1;\n"
8577                "method();\n"
8578                "const double       oneTwoThree = 123;\n"
8579                "const unsigned int oneTwo = 12;",
8580                Alignment);
8581   verifyFormat("int      oneTwoThree{0}; // comment\n"
8582                "unsigned oneTwo;         // comment",
8583                Alignment);
8584   EXPECT_EQ("float const a = 5;\n"
8585             "\n"
8586             "int oneTwoThree = 123;",
8587             format("float const   a = 5;\n"
8588                    "\n"
8589                    "int           oneTwoThree= 123;",
8590                    Alignment));
8591   EXPECT_EQ("float a = 5;\n"
8592             "int   one = 1;\n"
8593             "\n"
8594             "unsigned oneTwoThree = 123;",
8595             format("float    a = 5;\n"
8596                    "int      one = 1;\n"
8597                    "\n"
8598                    "unsigned oneTwoThree = 123;",
8599                    Alignment));
8600   EXPECT_EQ("float a = 5;\n"
8601             "int   one = 1;\n"
8602             "\n"
8603             "unsigned oneTwoThree = 123;\n"
8604             "int      oneTwo = 12;",
8605             format("float    a = 5;\n"
8606                    "int one = 1;\n"
8607                    "\n"
8608                    "unsigned oneTwoThree = 123;\n"
8609                    "int oneTwo = 12;",
8610                    Alignment));
8611   // Function prototype alignment
8612   verifyFormat("int    a();\n"
8613                "double b();",
8614                Alignment);
8615   verifyFormat("int    a(int x);\n"
8616                "double b();",
8617                Alignment);
8618   unsigned OldColumnLimit = Alignment.ColumnLimit;
8619   // We need to set ColumnLimit to zero, in order to stress nested alignments,
8620   // otherwise the function parameters will be re-flowed onto a single line.
8621   Alignment.ColumnLimit = 0;
8622   EXPECT_EQ("int    a(int   x,\n"
8623             "         float y);\n"
8624             "double b(int    x,\n"
8625             "         double y);",
8626             format("int a(int x,\n"
8627                    " float y);\n"
8628                    "double b(int x,\n"
8629                    " double y);",
8630                    Alignment));
8631   // This ensures that function parameters of function declarations are
8632   // correctly indented when their owning functions are indented.
8633   // The failure case here is for 'double y' to not be indented enough.
8634   EXPECT_EQ("double a(int x);\n"
8635             "int    b(int    y,\n"
8636             "         double z);",
8637             format("double a(int x);\n"
8638                    "int b(int y,\n"
8639                    " double z);",
8640                    Alignment));
8641   // Set ColumnLimit low so that we induce wrapping immediately after
8642   // the function name and opening paren.
8643   Alignment.ColumnLimit = 13;
8644   verifyFormat("int function(\n"
8645                "    int  x,\n"
8646                "    bool y);",
8647                Alignment);
8648   Alignment.ColumnLimit = OldColumnLimit;
8649   // Ensure function pointers don't screw up recursive alignment
8650   verifyFormat("int    a(int x, void (*fp)(int y));\n"
8651                "double b();",
8652                Alignment);
8653   Alignment.AlignConsecutiveAssignments = true;
8654   // Ensure recursive alignment is broken by function braces, so that the
8655   // "a = 1" does not align with subsequent assignments inside the function
8656   // body.
8657   verifyFormat("int func(int a = 1) {\n"
8658                "  int b  = 2;\n"
8659                "  int cc = 3;\n"
8660                "}",
8661                Alignment);
8662   verifyFormat("float      something = 2000;\n"
8663                "double     another   = 911;\n"
8664                "int        i = 1, j = 10;\n"
8665                "const int *oneMore = 1;\n"
8666                "unsigned   i       = 2;",
8667                Alignment);
8668   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8669                "unsigned oneTwo      = 0;   // comment",
8670                Alignment);
8671   // Make sure that scope is correctly tracked, in the absence of braces
8672   verifyFormat("for (int i = 0; i < n; i++)\n"
8673                "  j = i;\n"
8674                "double x = 1;\n",
8675                Alignment);
8676   verifyFormat("if (int i = 0)\n"
8677                "  j = i;\n"
8678                "double x = 1;\n",
8679                Alignment);
8680   // Ensure operator[] and operator() are comprehended
8681   verifyFormat("struct test {\n"
8682                "  long long int foo();\n"
8683                "  int           operator[](int a);\n"
8684                "  double        bar();\n"
8685                "};\n",
8686                Alignment);
8687   verifyFormat("struct test {\n"
8688                "  long long int foo();\n"
8689                "  int           operator()(int a);\n"
8690                "  double        bar();\n"
8691                "};\n",
8692                Alignment);
8693   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8694             "  int const i   = 1;\n"
8695             "  int *     j   = 2;\n"
8696             "  int       big = 10000;\n"
8697             "\n"
8698             "  unsigned oneTwoThree = 123;\n"
8699             "  int      oneTwo      = 12;\n"
8700             "  method();\n"
8701             "  float k  = 2;\n"
8702             "  int   ll = 10000;\n"
8703             "}",
8704             format("void SomeFunction(int parameter= 0) {\n"
8705                    " int const  i= 1;\n"
8706                    "  int *j=2;\n"
8707                    " int big  =  10000;\n"
8708                    "\n"
8709                    "unsigned oneTwoThree  =123;\n"
8710                    "int oneTwo = 12;\n"
8711                    "  method();\n"
8712                    "float k= 2;\n"
8713                    "int ll=10000;\n"
8714                    "}",
8715                    Alignment));
8716   Alignment.AlignConsecutiveAssignments = false;
8717   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8718   verifyFormat("#define A \\\n"
8719                "  int       aaaa = 12; \\\n"
8720                "  float     b = 23; \\\n"
8721                "  const int ccc = 234; \\\n"
8722                "  unsigned  dddddddddd = 2345;",
8723                Alignment);
8724   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8725   verifyFormat("#define A              \\\n"
8726                "  int       aaaa = 12; \\\n"
8727                "  float     b = 23;    \\\n"
8728                "  const int ccc = 234; \\\n"
8729                "  unsigned  dddddddddd = 2345;",
8730                Alignment);
8731   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8732   Alignment.ColumnLimit = 30;
8733   verifyFormat("#define A                    \\\n"
8734                "  int       aaaa = 12;       \\\n"
8735                "  float     b = 23;          \\\n"
8736                "  const int ccc = 234;       \\\n"
8737                "  int       dddddddddd = 2345;",
8738                Alignment);
8739   Alignment.ColumnLimit = 80;
8740   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8741                "k = 4, int l = 5,\n"
8742                "                  int m = 6) {\n"
8743                "  const int j = 10;\n"
8744                "  otherThing = 1;\n"
8745                "}",
8746                Alignment);
8747   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8748                "  int const i = 1;\n"
8749                "  int *     j = 2;\n"
8750                "  int       big = 10000;\n"
8751                "}",
8752                Alignment);
8753   verifyFormat("class C {\n"
8754                "public:\n"
8755                "  int          i = 1;\n"
8756                "  virtual void f() = 0;\n"
8757                "};",
8758                Alignment);
8759   verifyFormat("float i = 1;\n"
8760                "if (SomeType t = getSomething()) {\n"
8761                "}\n"
8762                "const unsigned j = 2;\n"
8763                "int            big = 10000;",
8764                Alignment);
8765   verifyFormat("float j = 7;\n"
8766                "for (int k = 0; k < N; ++k) {\n"
8767                "}\n"
8768                "unsigned j = 2;\n"
8769                "int      big = 10000;\n"
8770                "}",
8771                Alignment);
8772   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8773   verifyFormat("float              i = 1;\n"
8774                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8775                "    = someLooooooooooooooooongFunction();\n"
8776                "int j = 2;",
8777                Alignment);
8778   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8779   verifyFormat("int                i = 1;\n"
8780                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8781                "    someLooooooooooooooooongFunction();\n"
8782                "int j = 2;",
8783                Alignment);
8784 
8785   Alignment.AlignConsecutiveAssignments = true;
8786   verifyFormat("auto lambda = []() {\n"
8787                "  auto  ii = 0;\n"
8788                "  float j  = 0;\n"
8789                "  return 0;\n"
8790                "};\n"
8791                "int   i  = 0;\n"
8792                "float i2 = 0;\n"
8793                "auto  v  = type{\n"
8794                "    i = 1,   //\n"
8795                "    (i = 2), //\n"
8796                "    i = 3    //\n"
8797                "};",
8798                Alignment);
8799   Alignment.AlignConsecutiveAssignments = false;
8800 
8801   verifyFormat(
8802       "int      i = 1;\n"
8803       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8804       "                          loooooooooooooooooooooongParameterB);\n"
8805       "int      j = 2;",
8806       Alignment);
8807 
8808   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8809   // We expect declarations and assignments to align, as long as it doesn't
8810   // exceed the column limit, starting a new alignment sequence whenever it
8811   // happens.
8812   Alignment.AlignConsecutiveAssignments = true;
8813   Alignment.ColumnLimit = 30;
8814   verifyFormat("float    ii              = 1;\n"
8815                "unsigned j               = 2;\n"
8816                "int someVerylongVariable = 1;\n"
8817                "AnotherLongType  ll = 123456;\n"
8818                "VeryVeryLongType k  = 2;\n"
8819                "int              myvar = 1;",
8820                Alignment);
8821   Alignment.ColumnLimit = 80;
8822   Alignment.AlignConsecutiveAssignments = false;
8823 
8824   verifyFormat(
8825       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8826       "          typename LongType, typename B>\n"
8827       "auto foo() {}\n",
8828       Alignment);
8829   verifyFormat("float a, b = 1;\n"
8830                "int   c = 2;\n"
8831                "int   dd = 3;\n",
8832                Alignment);
8833   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8834                "float b[1][] = {{3.f}};\n",
8835                Alignment);
8836   Alignment.AlignConsecutiveAssignments = true;
8837   verifyFormat("float a, b = 1;\n"
8838                "int   c  = 2;\n"
8839                "int   dd = 3;\n",
8840                Alignment);
8841   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8842                "float b[1][] = {{3.f}};\n",
8843                Alignment);
8844   Alignment.AlignConsecutiveAssignments = false;
8845 
8846   Alignment.ColumnLimit = 30;
8847   Alignment.BinPackParameters = false;
8848   verifyFormat("void foo(float     a,\n"
8849                "         float     b,\n"
8850                "         int       c,\n"
8851                "         uint32_t *d) {\n"
8852                "  int *  e = 0;\n"
8853                "  float  f = 0;\n"
8854                "  double g = 0;\n"
8855                "}\n"
8856                "void bar(ino_t     a,\n"
8857                "         int       b,\n"
8858                "         uint32_t *c,\n"
8859                "         bool      d) {}\n",
8860                Alignment);
8861   Alignment.BinPackParameters = true;
8862   Alignment.ColumnLimit = 80;
8863 }
8864 
8865 TEST_F(FormatTest, LinuxBraceBreaking) {
8866   FormatStyle LinuxBraceStyle = getLLVMStyle();
8867   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8868   verifyFormat("namespace a\n"
8869                "{\n"
8870                "class A\n"
8871                "{\n"
8872                "  void f()\n"
8873                "  {\n"
8874                "    if (true) {\n"
8875                "      a();\n"
8876                "      b();\n"
8877                "    } else {\n"
8878                "      a();\n"
8879                "    }\n"
8880                "  }\n"
8881                "  void g() { return; }\n"
8882                "};\n"
8883                "struct B {\n"
8884                "  int x;\n"
8885                "};\n"
8886                "}\n",
8887                LinuxBraceStyle);
8888   verifyFormat("enum X {\n"
8889                "  Y = 0,\n"
8890                "}\n",
8891                LinuxBraceStyle);
8892   verifyFormat("struct S {\n"
8893                "  int Type;\n"
8894                "  union {\n"
8895                "    int x;\n"
8896                "    double y;\n"
8897                "  } Value;\n"
8898                "  class C\n"
8899                "  {\n"
8900                "    MyFavoriteType Value;\n"
8901                "  } Class;\n"
8902                "}\n",
8903                LinuxBraceStyle);
8904 }
8905 
8906 TEST_F(FormatTest, MozillaBraceBreaking) {
8907   FormatStyle MozillaBraceStyle = getLLVMStyle();
8908   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8909   MozillaBraceStyle.FixNamespaceComments = false;
8910   verifyFormat("namespace a {\n"
8911                "class A\n"
8912                "{\n"
8913                "  void f()\n"
8914                "  {\n"
8915                "    if (true) {\n"
8916                "      a();\n"
8917                "      b();\n"
8918                "    }\n"
8919                "  }\n"
8920                "  void g() { return; }\n"
8921                "};\n"
8922                "enum E\n"
8923                "{\n"
8924                "  A,\n"
8925                "  // foo\n"
8926                "  B,\n"
8927                "  C\n"
8928                "};\n"
8929                "struct B\n"
8930                "{\n"
8931                "  int x;\n"
8932                "};\n"
8933                "}\n",
8934                MozillaBraceStyle);
8935   verifyFormat("struct S\n"
8936                "{\n"
8937                "  int Type;\n"
8938                "  union\n"
8939                "  {\n"
8940                "    int x;\n"
8941                "    double y;\n"
8942                "  } Value;\n"
8943                "  class C\n"
8944                "  {\n"
8945                "    MyFavoriteType Value;\n"
8946                "  } Class;\n"
8947                "}\n",
8948                MozillaBraceStyle);
8949 }
8950 
8951 TEST_F(FormatTest, StroustrupBraceBreaking) {
8952   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8953   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8954   verifyFormat("namespace a {\n"
8955                "class A {\n"
8956                "  void f()\n"
8957                "  {\n"
8958                "    if (true) {\n"
8959                "      a();\n"
8960                "      b();\n"
8961                "    }\n"
8962                "  }\n"
8963                "  void g() { return; }\n"
8964                "};\n"
8965                "struct B {\n"
8966                "  int x;\n"
8967                "};\n"
8968                "} // namespace a\n",
8969                StroustrupBraceStyle);
8970 
8971   verifyFormat("void foo()\n"
8972                "{\n"
8973                "  if (a) {\n"
8974                "    a();\n"
8975                "  }\n"
8976                "  else {\n"
8977                "    b();\n"
8978                "  }\n"
8979                "}\n",
8980                StroustrupBraceStyle);
8981 
8982   verifyFormat("#ifdef _DEBUG\n"
8983                "int foo(int i = 0)\n"
8984                "#else\n"
8985                "int foo(int i = 5)\n"
8986                "#endif\n"
8987                "{\n"
8988                "  return i;\n"
8989                "}",
8990                StroustrupBraceStyle);
8991 
8992   verifyFormat("void foo() {}\n"
8993                "void bar()\n"
8994                "#ifdef _DEBUG\n"
8995                "{\n"
8996                "  foo();\n"
8997                "}\n"
8998                "#else\n"
8999                "{\n"
9000                "}\n"
9001                "#endif",
9002                StroustrupBraceStyle);
9003 
9004   verifyFormat("void foobar() { int i = 5; }\n"
9005                "#ifdef _DEBUG\n"
9006                "void bar() {}\n"
9007                "#else\n"
9008                "void bar() { foobar(); }\n"
9009                "#endif",
9010                StroustrupBraceStyle);
9011 }
9012 
9013 TEST_F(FormatTest, AllmanBraceBreaking) {
9014   FormatStyle AllmanBraceStyle = getLLVMStyle();
9015   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9016   verifyFormat("namespace a\n"
9017                "{\n"
9018                "class A\n"
9019                "{\n"
9020                "  void f()\n"
9021                "  {\n"
9022                "    if (true)\n"
9023                "    {\n"
9024                "      a();\n"
9025                "      b();\n"
9026                "    }\n"
9027                "  }\n"
9028                "  void g() { return; }\n"
9029                "};\n"
9030                "struct B\n"
9031                "{\n"
9032                "  int x;\n"
9033                "};\n"
9034                "}",
9035                AllmanBraceStyle);
9036 
9037   verifyFormat("void f()\n"
9038                "{\n"
9039                "  if (true)\n"
9040                "  {\n"
9041                "    a();\n"
9042                "  }\n"
9043                "  else if (false)\n"
9044                "  {\n"
9045                "    b();\n"
9046                "  }\n"
9047                "  else\n"
9048                "  {\n"
9049                "    c();\n"
9050                "  }\n"
9051                "}\n",
9052                AllmanBraceStyle);
9053 
9054   verifyFormat("void f()\n"
9055                "{\n"
9056                "  for (int i = 0; i < 10; ++i)\n"
9057                "  {\n"
9058                "    a();\n"
9059                "  }\n"
9060                "  while (false)\n"
9061                "  {\n"
9062                "    b();\n"
9063                "  }\n"
9064                "  do\n"
9065                "  {\n"
9066                "    c();\n"
9067                "  } while (false)\n"
9068                "}\n",
9069                AllmanBraceStyle);
9070 
9071   verifyFormat("void f(int a)\n"
9072                "{\n"
9073                "  switch (a)\n"
9074                "  {\n"
9075                "  case 0:\n"
9076                "    break;\n"
9077                "  case 1:\n"
9078                "  {\n"
9079                "    break;\n"
9080                "  }\n"
9081                "  case 2:\n"
9082                "  {\n"
9083                "  }\n"
9084                "  break;\n"
9085                "  default:\n"
9086                "    break;\n"
9087                "  }\n"
9088                "}\n",
9089                AllmanBraceStyle);
9090 
9091   verifyFormat("enum X\n"
9092                "{\n"
9093                "  Y = 0,\n"
9094                "}\n",
9095                AllmanBraceStyle);
9096   verifyFormat("enum X\n"
9097                "{\n"
9098                "  Y = 0\n"
9099                "}\n",
9100                AllmanBraceStyle);
9101 
9102   verifyFormat("@interface BSApplicationController ()\n"
9103                "{\n"
9104                "@private\n"
9105                "  id _extraIvar;\n"
9106                "}\n"
9107                "@end\n",
9108                AllmanBraceStyle);
9109 
9110   verifyFormat("#ifdef _DEBUG\n"
9111                "int foo(int i = 0)\n"
9112                "#else\n"
9113                "int foo(int i = 5)\n"
9114                "#endif\n"
9115                "{\n"
9116                "  return i;\n"
9117                "}",
9118                AllmanBraceStyle);
9119 
9120   verifyFormat("void foo() {}\n"
9121                "void bar()\n"
9122                "#ifdef _DEBUG\n"
9123                "{\n"
9124                "  foo();\n"
9125                "}\n"
9126                "#else\n"
9127                "{\n"
9128                "}\n"
9129                "#endif",
9130                AllmanBraceStyle);
9131 
9132   verifyFormat("void foobar() { int i = 5; }\n"
9133                "#ifdef _DEBUG\n"
9134                "void bar() {}\n"
9135                "#else\n"
9136                "void bar() { foobar(); }\n"
9137                "#endif",
9138                AllmanBraceStyle);
9139 
9140   // This shouldn't affect ObjC blocks..
9141   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9142                "  // ...\n"
9143                "  int i;\n"
9144                "}];",
9145                AllmanBraceStyle);
9146   verifyFormat("void (^block)(void) = ^{\n"
9147                "  // ...\n"
9148                "  int i;\n"
9149                "};",
9150                AllmanBraceStyle);
9151   // .. or dict literals.
9152   verifyFormat("void f()\n"
9153                "{\n"
9154                "  // ...\n"
9155                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9156                "}",
9157                AllmanBraceStyle);
9158   verifyFormat("void f()\n"
9159                "{\n"
9160                "  // ...\n"
9161                "  [object someMethod:@{a : @\"b\"}];\n"
9162                "}",
9163                AllmanBraceStyle);
9164   verifyFormat("int f()\n"
9165                "{ // comment\n"
9166                "  return 42;\n"
9167                "}",
9168                AllmanBraceStyle);
9169 
9170   AllmanBraceStyle.ColumnLimit = 19;
9171   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9172   AllmanBraceStyle.ColumnLimit = 18;
9173   verifyFormat("void f()\n"
9174                "{\n"
9175                "  int i;\n"
9176                "}",
9177                AllmanBraceStyle);
9178   AllmanBraceStyle.ColumnLimit = 80;
9179 
9180   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9181   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9182   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9183   verifyFormat("void f(bool b)\n"
9184                "{\n"
9185                "  if (b)\n"
9186                "  {\n"
9187                "    return;\n"
9188                "  }\n"
9189                "}\n",
9190                BreakBeforeBraceShortIfs);
9191   verifyFormat("void f(bool b)\n"
9192                "{\n"
9193                "  if constexpr (b)\n"
9194                "  {\n"
9195                "    return;\n"
9196                "  }\n"
9197                "}\n",
9198                BreakBeforeBraceShortIfs);
9199   verifyFormat("void f(bool b)\n"
9200                "{\n"
9201                "  if (b) return;\n"
9202                "}\n",
9203                BreakBeforeBraceShortIfs);
9204   verifyFormat("void f(bool b)\n"
9205                "{\n"
9206                "  if constexpr (b) return;\n"
9207                "}\n",
9208                BreakBeforeBraceShortIfs);
9209   verifyFormat("void f(bool b)\n"
9210                "{\n"
9211                "  while (b)\n"
9212                "  {\n"
9213                "    return;\n"
9214                "  }\n"
9215                "}\n",
9216                BreakBeforeBraceShortIfs);
9217 }
9218 
9219 TEST_F(FormatTest, GNUBraceBreaking) {
9220   FormatStyle GNUBraceStyle = getLLVMStyle();
9221   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9222   verifyFormat("namespace a\n"
9223                "{\n"
9224                "class A\n"
9225                "{\n"
9226                "  void f()\n"
9227                "  {\n"
9228                "    int a;\n"
9229                "    {\n"
9230                "      int b;\n"
9231                "    }\n"
9232                "    if (true)\n"
9233                "      {\n"
9234                "        a();\n"
9235                "        b();\n"
9236                "      }\n"
9237                "  }\n"
9238                "  void g() { return; }\n"
9239                "}\n"
9240                "}",
9241                GNUBraceStyle);
9242 
9243   verifyFormat("void f()\n"
9244                "{\n"
9245                "  if (true)\n"
9246                "    {\n"
9247                "      a();\n"
9248                "    }\n"
9249                "  else if (false)\n"
9250                "    {\n"
9251                "      b();\n"
9252                "    }\n"
9253                "  else\n"
9254                "    {\n"
9255                "      c();\n"
9256                "    }\n"
9257                "}\n",
9258                GNUBraceStyle);
9259 
9260   verifyFormat("void f()\n"
9261                "{\n"
9262                "  for (int i = 0; i < 10; ++i)\n"
9263                "    {\n"
9264                "      a();\n"
9265                "    }\n"
9266                "  while (false)\n"
9267                "    {\n"
9268                "      b();\n"
9269                "    }\n"
9270                "  do\n"
9271                "    {\n"
9272                "      c();\n"
9273                "    }\n"
9274                "  while (false);\n"
9275                "}\n",
9276                GNUBraceStyle);
9277 
9278   verifyFormat("void f(int a)\n"
9279                "{\n"
9280                "  switch (a)\n"
9281                "    {\n"
9282                "    case 0:\n"
9283                "      break;\n"
9284                "    case 1:\n"
9285                "      {\n"
9286                "        break;\n"
9287                "      }\n"
9288                "    case 2:\n"
9289                "      {\n"
9290                "      }\n"
9291                "      break;\n"
9292                "    default:\n"
9293                "      break;\n"
9294                "    }\n"
9295                "}\n",
9296                GNUBraceStyle);
9297 
9298   verifyFormat("enum X\n"
9299                "{\n"
9300                "  Y = 0,\n"
9301                "}\n",
9302                GNUBraceStyle);
9303 
9304   verifyFormat("@interface BSApplicationController ()\n"
9305                "{\n"
9306                "@private\n"
9307                "  id _extraIvar;\n"
9308                "}\n"
9309                "@end\n",
9310                GNUBraceStyle);
9311 
9312   verifyFormat("#ifdef _DEBUG\n"
9313                "int foo(int i = 0)\n"
9314                "#else\n"
9315                "int foo(int i = 5)\n"
9316                "#endif\n"
9317                "{\n"
9318                "  return i;\n"
9319                "}",
9320                GNUBraceStyle);
9321 
9322   verifyFormat("void foo() {}\n"
9323                "void bar()\n"
9324                "#ifdef _DEBUG\n"
9325                "{\n"
9326                "  foo();\n"
9327                "}\n"
9328                "#else\n"
9329                "{\n"
9330                "}\n"
9331                "#endif",
9332                GNUBraceStyle);
9333 
9334   verifyFormat("void foobar() { int i = 5; }\n"
9335                "#ifdef _DEBUG\n"
9336                "void bar() {}\n"
9337                "#else\n"
9338                "void bar() { foobar(); }\n"
9339                "#endif",
9340                GNUBraceStyle);
9341 }
9342 
9343 TEST_F(FormatTest, WebKitBraceBreaking) {
9344   FormatStyle WebKitBraceStyle = getLLVMStyle();
9345   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9346   WebKitBraceStyle.FixNamespaceComments = false;
9347   verifyFormat("namespace a {\n"
9348                "class A {\n"
9349                "  void f()\n"
9350                "  {\n"
9351                "    if (true) {\n"
9352                "      a();\n"
9353                "      b();\n"
9354                "    }\n"
9355                "  }\n"
9356                "  void g() { return; }\n"
9357                "};\n"
9358                "enum E {\n"
9359                "  A,\n"
9360                "  // foo\n"
9361                "  B,\n"
9362                "  C\n"
9363                "};\n"
9364                "struct B {\n"
9365                "  int x;\n"
9366                "};\n"
9367                "}\n",
9368                WebKitBraceStyle);
9369   verifyFormat("struct S {\n"
9370                "  int Type;\n"
9371                "  union {\n"
9372                "    int x;\n"
9373                "    double y;\n"
9374                "  } Value;\n"
9375                "  class C {\n"
9376                "    MyFavoriteType Value;\n"
9377                "  } Class;\n"
9378                "};\n",
9379                WebKitBraceStyle);
9380 }
9381 
9382 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9383   verifyFormat("void f() {\n"
9384                "  try {\n"
9385                "  } catch (const Exception &e) {\n"
9386                "  }\n"
9387                "}\n",
9388                getLLVMStyle());
9389 }
9390 
9391 TEST_F(FormatTest, UnderstandsPragmas) {
9392   verifyFormat("#pragma omp reduction(| : var)");
9393   verifyFormat("#pragma omp reduction(+ : var)");
9394 
9395   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9396             "(including parentheses).",
9397             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9398                    "(including parentheses)."));
9399 }
9400 
9401 TEST_F(FormatTest, UnderstandPragmaOption) {
9402   verifyFormat("#pragma option -C -A");
9403 
9404   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9405 }
9406 
9407 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9408   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9409   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9410                                   << " differs from Style #0"
9411 
9412 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9413   SmallVector<FormatStyle, 3> Styles;
9414   Styles.resize(3);
9415 
9416   Styles[0] = getLLVMStyle();
9417   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9418   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9419   EXPECT_ALL_STYLES_EQUAL(Styles);
9420 
9421   Styles[0] = getGoogleStyle();
9422   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9423   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9424   EXPECT_ALL_STYLES_EQUAL(Styles);
9425 
9426   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9427   EXPECT_TRUE(
9428       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9429   EXPECT_TRUE(
9430       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9431   EXPECT_ALL_STYLES_EQUAL(Styles);
9432 
9433   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9434   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9435   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9436   EXPECT_ALL_STYLES_EQUAL(Styles);
9437 
9438   Styles[0] = getMozillaStyle();
9439   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9440   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9441   EXPECT_ALL_STYLES_EQUAL(Styles);
9442 
9443   Styles[0] = getWebKitStyle();
9444   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9445   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9446   EXPECT_ALL_STYLES_EQUAL(Styles);
9447 
9448   Styles[0] = getGNUStyle();
9449   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9450   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9451   EXPECT_ALL_STYLES_EQUAL(Styles);
9452 
9453   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9454 }
9455 
9456 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9457   SmallVector<FormatStyle, 8> Styles;
9458   Styles.resize(2);
9459 
9460   Styles[0] = getGoogleStyle();
9461   Styles[1] = getLLVMStyle();
9462   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9463   EXPECT_ALL_STYLES_EQUAL(Styles);
9464 
9465   Styles.resize(5);
9466   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9467   Styles[1] = getLLVMStyle();
9468   Styles[1].Language = FormatStyle::LK_JavaScript;
9469   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9470 
9471   Styles[2] = getLLVMStyle();
9472   Styles[2].Language = FormatStyle::LK_JavaScript;
9473   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9474                                   "BasedOnStyle: Google",
9475                                   &Styles[2])
9476                    .value());
9477 
9478   Styles[3] = getLLVMStyle();
9479   Styles[3].Language = FormatStyle::LK_JavaScript;
9480   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9481                                   "Language: JavaScript",
9482                                   &Styles[3])
9483                    .value());
9484 
9485   Styles[4] = getLLVMStyle();
9486   Styles[4].Language = FormatStyle::LK_JavaScript;
9487   EXPECT_EQ(0, parseConfiguration("---\n"
9488                                   "BasedOnStyle: LLVM\n"
9489                                   "IndentWidth: 123\n"
9490                                   "---\n"
9491                                   "BasedOnStyle: Google\n"
9492                                   "Language: JavaScript",
9493                                   &Styles[4])
9494                    .value());
9495   EXPECT_ALL_STYLES_EQUAL(Styles);
9496 }
9497 
9498 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9499   Style.FIELD = false;                                                         \
9500   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9501   EXPECT_TRUE(Style.FIELD);                                                    \
9502   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9503   EXPECT_FALSE(Style.FIELD);
9504 
9505 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9506 
9507 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9508   Style.STRUCT.FIELD = false;                                                  \
9509   EXPECT_EQ(0,                                                                 \
9510             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9511                 .value());                                                     \
9512   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9513   EXPECT_EQ(0,                                                                 \
9514             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9515                 .value());                                                     \
9516   EXPECT_FALSE(Style.STRUCT.FIELD);
9517 
9518 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9519   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9520 
9521 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9522   EXPECT_NE(VALUE, Style.FIELD);                                               \
9523   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9524   EXPECT_EQ(VALUE, Style.FIELD)
9525 
9526 TEST_F(FormatTest, ParsesConfigurationBools) {
9527   FormatStyle Style = {};
9528   Style.Language = FormatStyle::LK_Cpp;
9529   CHECK_PARSE_BOOL(AlignOperands);
9530   CHECK_PARSE_BOOL(AlignTrailingComments);
9531   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9532   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9533   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9534   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9535   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9536   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9537   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9538   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9539   CHECK_PARSE_BOOL(BinPackArguments);
9540   CHECK_PARSE_BOOL(BinPackParameters);
9541   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
9542   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9543   CHECK_PARSE_BOOL(BreakStringLiterals);
9544   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
9545   CHECK_PARSE_BOOL(CompactNamespaces);
9546   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9547   CHECK_PARSE_BOOL(DerivePointerAlignment);
9548   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9549   CHECK_PARSE_BOOL(DisableFormat);
9550   CHECK_PARSE_BOOL(IndentCaseLabels);
9551   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9552   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9553   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9554   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9555   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9556   CHECK_PARSE_BOOL(ReflowComments);
9557   CHECK_PARSE_BOOL(SortIncludes);
9558   CHECK_PARSE_BOOL(SortUsingDeclarations);
9559   CHECK_PARSE_BOOL(SpacesInParentheses);
9560   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9561   CHECK_PARSE_BOOL(SpacesInAngles);
9562   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9563   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9564   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9565   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9566   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
9567   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9568 
9569   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9570   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9571   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9572   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9573   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9574   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9575   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9576   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9577   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9578   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9579   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9580   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
9581   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
9582   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
9583 }
9584 
9585 #undef CHECK_PARSE_BOOL
9586 
9587 TEST_F(FormatTest, ParsesConfiguration) {
9588   FormatStyle Style = {};
9589   Style.Language = FormatStyle::LK_Cpp;
9590   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9591   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9592               ConstructorInitializerIndentWidth, 1234u);
9593   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9594   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9595   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9596   CHECK_PARSE("PenaltyBreakAssignment: 1234",
9597               PenaltyBreakAssignment, 1234u);
9598   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9599               PenaltyBreakBeforeFirstCallParameter, 1234u);
9600   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9601   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9602               PenaltyReturnTypeOnItsOwnLine, 1234u);
9603   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9604               SpacesBeforeTrailingComments, 1234u);
9605   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9606   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9607   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
9608 
9609   Style.PointerAlignment = FormatStyle::PAS_Middle;
9610   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9611               FormatStyle::PAS_Left);
9612   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9613               FormatStyle::PAS_Right);
9614   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9615               FormatStyle::PAS_Middle);
9616   // For backward compatibility:
9617   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9618               FormatStyle::PAS_Left);
9619   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9620               FormatStyle::PAS_Right);
9621   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9622               FormatStyle::PAS_Middle);
9623 
9624   Style.Standard = FormatStyle::LS_Auto;
9625   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9626   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9627   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9628   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9629   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9630 
9631   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9632   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9633               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9634   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9635               FormatStyle::BOS_None);
9636   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9637               FormatStyle::BOS_All);
9638   // For backward compatibility:
9639   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9640               FormatStyle::BOS_None);
9641   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9642               FormatStyle::BOS_All);
9643 
9644   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
9645   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
9646               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9647   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
9648               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
9649   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
9650               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
9651   // For backward compatibility:
9652   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
9653               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9654 
9655   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9656   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9657               FormatStyle::BAS_Align);
9658   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9659               FormatStyle::BAS_DontAlign);
9660   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9661               FormatStyle::BAS_AlwaysBreak);
9662   // For backward compatibility:
9663   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9664               FormatStyle::BAS_DontAlign);
9665   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9666               FormatStyle::BAS_Align);
9667 
9668   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9669   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
9670               FormatStyle::ENAS_DontAlign);
9671   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
9672               FormatStyle::ENAS_Left);
9673   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
9674               FormatStyle::ENAS_Right);
9675   // For backward compatibility:
9676   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
9677               FormatStyle::ENAS_Left);
9678   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
9679               FormatStyle::ENAS_Right);
9680 
9681   Style.UseTab = FormatStyle::UT_ForIndentation;
9682   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9683   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9684   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9685   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9686               FormatStyle::UT_ForContinuationAndIndentation);
9687   // For backward compatibility:
9688   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9689   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9690 
9691   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9692   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9693               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9694   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9695               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9696   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9697               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9698   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9699               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9700   // For backward compatibility:
9701   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9702               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9703   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9704               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9705 
9706   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9707   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9708               FormatStyle::SBPO_Never);
9709   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9710               FormatStyle::SBPO_Always);
9711   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9712               FormatStyle::SBPO_ControlStatements);
9713   // For backward compatibility:
9714   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9715               FormatStyle::SBPO_Never);
9716   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9717               FormatStyle::SBPO_ControlStatements);
9718 
9719   Style.ColumnLimit = 123;
9720   FormatStyle BaseStyle = getLLVMStyle();
9721   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9722   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9723 
9724   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9725   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9726               FormatStyle::BS_Attach);
9727   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9728               FormatStyle::BS_Linux);
9729   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9730               FormatStyle::BS_Mozilla);
9731   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9732               FormatStyle::BS_Stroustrup);
9733   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9734               FormatStyle::BS_Allman);
9735   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9736   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9737               FormatStyle::BS_WebKit);
9738   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9739               FormatStyle::BS_Custom);
9740 
9741   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9742   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9743               FormatStyle::RTBS_None);
9744   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9745               FormatStyle::RTBS_All);
9746   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9747               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9748   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9749               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9750   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9751               AlwaysBreakAfterReturnType,
9752               FormatStyle::RTBS_TopLevelDefinitions);
9753 
9754   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9755   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9756               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9757   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9758               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9759   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9760               AlwaysBreakAfterDefinitionReturnType,
9761               FormatStyle::DRTBS_TopLevel);
9762 
9763   Style.NamespaceIndentation = FormatStyle::NI_All;
9764   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9765               FormatStyle::NI_None);
9766   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9767               FormatStyle::NI_Inner);
9768   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9769               FormatStyle::NI_All);
9770 
9771   // FIXME: This is required because parsing a configuration simply overwrites
9772   // the first N elements of the list instead of resetting it.
9773   Style.ForEachMacros.clear();
9774   std::vector<std::string> BoostForeach;
9775   BoostForeach.push_back("BOOST_FOREACH");
9776   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9777   std::vector<std::string> BoostAndQForeach;
9778   BoostAndQForeach.push_back("BOOST_FOREACH");
9779   BoostAndQForeach.push_back("Q_FOREACH");
9780   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9781               BoostAndQForeach);
9782 
9783   Style.IncludeCategories.clear();
9784   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9785                                                                   {".*", 1}};
9786   CHECK_PARSE("IncludeCategories:\n"
9787               "  - Regex: abc/.*\n"
9788               "    Priority: 2\n"
9789               "  - Regex: .*\n"
9790               "    Priority: 1",
9791               IncludeCategories, ExpectedCategories);
9792   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
9793 }
9794 
9795 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9796   FormatStyle Style = {};
9797   Style.Language = FormatStyle::LK_Cpp;
9798   CHECK_PARSE("Language: Cpp\n"
9799               "IndentWidth: 12",
9800               IndentWidth, 12u);
9801   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9802                                "IndentWidth: 34",
9803                                &Style),
9804             ParseError::Unsuitable);
9805   EXPECT_EQ(12u, Style.IndentWidth);
9806   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9807   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9808 
9809   Style.Language = FormatStyle::LK_JavaScript;
9810   CHECK_PARSE("Language: JavaScript\n"
9811               "IndentWidth: 12",
9812               IndentWidth, 12u);
9813   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9814   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9815                                "IndentWidth: 34",
9816                                &Style),
9817             ParseError::Unsuitable);
9818   EXPECT_EQ(23u, Style.IndentWidth);
9819   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9820   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9821 
9822   CHECK_PARSE("BasedOnStyle: LLVM\n"
9823               "IndentWidth: 67",
9824               IndentWidth, 67u);
9825 
9826   CHECK_PARSE("---\n"
9827               "Language: JavaScript\n"
9828               "IndentWidth: 12\n"
9829               "---\n"
9830               "Language: Cpp\n"
9831               "IndentWidth: 34\n"
9832               "...\n",
9833               IndentWidth, 12u);
9834 
9835   Style.Language = FormatStyle::LK_Cpp;
9836   CHECK_PARSE("---\n"
9837               "Language: JavaScript\n"
9838               "IndentWidth: 12\n"
9839               "---\n"
9840               "Language: Cpp\n"
9841               "IndentWidth: 34\n"
9842               "...\n",
9843               IndentWidth, 34u);
9844   CHECK_PARSE("---\n"
9845               "IndentWidth: 78\n"
9846               "---\n"
9847               "Language: JavaScript\n"
9848               "IndentWidth: 56\n"
9849               "...\n",
9850               IndentWidth, 78u);
9851 
9852   Style.ColumnLimit = 123;
9853   Style.IndentWidth = 234;
9854   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9855   Style.TabWidth = 345;
9856   EXPECT_FALSE(parseConfiguration("---\n"
9857                                   "IndentWidth: 456\n"
9858                                   "BreakBeforeBraces: Allman\n"
9859                                   "---\n"
9860                                   "Language: JavaScript\n"
9861                                   "IndentWidth: 111\n"
9862                                   "TabWidth: 111\n"
9863                                   "---\n"
9864                                   "Language: Cpp\n"
9865                                   "BreakBeforeBraces: Stroustrup\n"
9866                                   "TabWidth: 789\n"
9867                                   "...\n",
9868                                   &Style));
9869   EXPECT_EQ(123u, Style.ColumnLimit);
9870   EXPECT_EQ(456u, Style.IndentWidth);
9871   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9872   EXPECT_EQ(789u, Style.TabWidth);
9873 
9874   EXPECT_EQ(parseConfiguration("---\n"
9875                                "Language: JavaScript\n"
9876                                "IndentWidth: 56\n"
9877                                "---\n"
9878                                "IndentWidth: 78\n"
9879                                "...\n",
9880                                &Style),
9881             ParseError::Error);
9882   EXPECT_EQ(parseConfiguration("---\n"
9883                                "Language: JavaScript\n"
9884                                "IndentWidth: 56\n"
9885                                "---\n"
9886                                "Language: JavaScript\n"
9887                                "IndentWidth: 78\n"
9888                                "...\n",
9889                                &Style),
9890             ParseError::Error);
9891 
9892   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9893 }
9894 
9895 #undef CHECK_PARSE
9896 
9897 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9898   FormatStyle Style = {};
9899   Style.Language = FormatStyle::LK_JavaScript;
9900   Style.BreakBeforeTernaryOperators = true;
9901   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9902   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9903 
9904   Style.BreakBeforeTernaryOperators = true;
9905   EXPECT_EQ(0, parseConfiguration("---\n"
9906                                   "BasedOnStyle: Google\n"
9907                                   "---\n"
9908                                   "Language: JavaScript\n"
9909                                   "IndentWidth: 76\n"
9910                                   "...\n",
9911                                   &Style)
9912                    .value());
9913   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9914   EXPECT_EQ(76u, Style.IndentWidth);
9915   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9916 }
9917 
9918 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9919   FormatStyle Style = getLLVMStyle();
9920   std::string YAML = configurationAsText(Style);
9921   FormatStyle ParsedStyle = {};
9922   ParsedStyle.Language = FormatStyle::LK_Cpp;
9923   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9924   EXPECT_EQ(Style, ParsedStyle);
9925 }
9926 
9927 TEST_F(FormatTest, WorksFor8bitEncodings) {
9928   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9929             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9930             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9931             "\"\xef\xee\xf0\xf3...\"",
9932             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9933                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9934                    "\xef\xee\xf0\xf3...\"",
9935                    getLLVMStyleWithColumns(12)));
9936 }
9937 
9938 TEST_F(FormatTest, HandlesUTF8BOM) {
9939   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9940   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9941             format("\xef\xbb\xbf#include <iostream>"));
9942   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9943             format("\xef\xbb\xbf\n#include <iostream>"));
9944 }
9945 
9946 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9947 #if !defined(_MSC_VER)
9948 
9949 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9950   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9951                getLLVMStyleWithColumns(35));
9952   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9953                getLLVMStyleWithColumns(31));
9954   verifyFormat("// Однажды в студёную зимнюю пору...",
9955                getLLVMStyleWithColumns(36));
9956   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9957   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9958                getLLVMStyleWithColumns(39));
9959   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9960                getLLVMStyleWithColumns(35));
9961 }
9962 
9963 TEST_F(FormatTest, SplitsUTF8Strings) {
9964   // Non-printable characters' width is currently considered to be the length in
9965   // bytes in UTF8. The characters can be displayed in very different manner
9966   // (zero-width, single width with a substitution glyph, expanded to their code
9967   // (e.g. "<8d>"), so there's no single correct way to handle them.
9968   EXPECT_EQ("\"aaaaÄ\"\n"
9969             "\"\xc2\x8d\";",
9970             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9971   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9972             "\"\xc2\x8d\";",
9973             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9974   EXPECT_EQ("\"Однажды, в \"\n"
9975             "\"студёную \"\n"
9976             "\"зимнюю \"\n"
9977             "\"пору,\"",
9978             format("\"Однажды, в студёную зимнюю пору,\"",
9979                    getLLVMStyleWithColumns(13)));
9980   EXPECT_EQ(
9981       "\"一 二 三 \"\n"
9982       "\"四 五六 \"\n"
9983       "\"七 八 九 \"\n"
9984       "\"十\"",
9985       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9986   EXPECT_EQ("\"一\t二 \"\n"
9987             "\"\t三 \"\n"
9988             "\"四 五\t六 \"\n"
9989             "\"\t七 \"\n"
9990             "\"八九十\tqq\"",
9991             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9992                    getLLVMStyleWithColumns(11)));
9993 
9994   // UTF8 character in an escape sequence.
9995   EXPECT_EQ("\"aaaaaa\"\n"
9996             "\"\\\xC2\x8D\"",
9997             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9998 }
9999 
10000 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10001   EXPECT_EQ("const char *sssss =\n"
10002             "    \"一二三四五六七八\\\n"
10003             " 九 十\";",
10004             format("const char *sssss = \"一二三四五六七八\\\n"
10005                    " 九 十\";",
10006                    getLLVMStyleWithColumns(30)));
10007 }
10008 
10009 TEST_F(FormatTest, SplitsUTF8LineComments) {
10010   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10011             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10012   EXPECT_EQ("// Я из лесу\n"
10013             "// вышел; был\n"
10014             "// сильный\n"
10015             "// мороз.",
10016             format("// Я из лесу вышел; был сильный мороз.",
10017                    getLLVMStyleWithColumns(13)));
10018   EXPECT_EQ("// 一二三\n"
10019             "// 四五六七\n"
10020             "// 八  九\n"
10021             "// 十",
10022             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10023 }
10024 
10025 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10026   EXPECT_EQ("/* Гляжу,\n"
10027             " * поднимается\n"
10028             " * медленно в\n"
10029             " * гору\n"
10030             " * Лошадка,\n"
10031             " * везущая\n"
10032             " * хворосту\n"
10033             " * воз. */",
10034             format("/* Гляжу, поднимается медленно в гору\n"
10035                    " * Лошадка, везущая хворосту воз. */",
10036                    getLLVMStyleWithColumns(13)));
10037   EXPECT_EQ(
10038       "/* 一二三\n"
10039       " * 四五六七\n"
10040       " * 八  九\n"
10041       " * 十  */",
10042       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10043   EXPECT_EQ("/* �������� ��������\n"
10044             " * ��������\n"
10045             " * ������-�� */",
10046             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10047 }
10048 
10049 #endif // _MSC_VER
10050 
10051 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10052   FormatStyle Style = getLLVMStyle();
10053 
10054   Style.ConstructorInitializerIndentWidth = 4;
10055   verifyFormat(
10056       "SomeClass::Constructor()\n"
10057       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10058       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10059       Style);
10060 
10061   Style.ConstructorInitializerIndentWidth = 2;
10062   verifyFormat(
10063       "SomeClass::Constructor()\n"
10064       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10065       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10066       Style);
10067 
10068   Style.ConstructorInitializerIndentWidth = 0;
10069   verifyFormat(
10070       "SomeClass::Constructor()\n"
10071       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10072       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10073       Style);
10074   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10075   verifyFormat(
10076       "SomeLongTemplateVariableName<\n"
10077       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10078       Style);
10079   verifyFormat(
10080       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10081       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10082       Style);
10083 }
10084 
10085 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10086   FormatStyle Style = getLLVMStyle();
10087   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10088   Style.ConstructorInitializerIndentWidth = 4;
10089   verifyFormat("SomeClass::Constructor()\n"
10090                "    : a(a)\n"
10091                "    , b(b)\n"
10092                "    , c(c) {}",
10093                Style);
10094   verifyFormat("SomeClass::Constructor()\n"
10095                "    : a(a) {}",
10096                Style);
10097 
10098   Style.ColumnLimit = 0;
10099   verifyFormat("SomeClass::Constructor()\n"
10100                "    : a(a) {}",
10101                Style);
10102   verifyFormat("SomeClass::Constructor() noexcept\n"
10103                "    : a(a) {}",
10104                Style);
10105   verifyFormat("SomeClass::Constructor()\n"
10106                "    : a(a)\n"
10107                "    , b(b)\n"
10108                "    , c(c) {}",
10109                Style);
10110   verifyFormat("SomeClass::Constructor()\n"
10111                "    : a(a) {\n"
10112                "  foo();\n"
10113                "  bar();\n"
10114                "}",
10115                Style);
10116 
10117   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10118   verifyFormat("SomeClass::Constructor()\n"
10119                "    : a(a)\n"
10120                "    , b(b)\n"
10121                "    , c(c) {\n}",
10122                Style);
10123   verifyFormat("SomeClass::Constructor()\n"
10124                "    : a(a) {\n}",
10125                Style);
10126 
10127   Style.ColumnLimit = 80;
10128   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10129   Style.ConstructorInitializerIndentWidth = 2;
10130   verifyFormat("SomeClass::Constructor()\n"
10131                "  : a(a)\n"
10132                "  , b(b)\n"
10133                "  , c(c) {}",
10134                Style);
10135 
10136   Style.ConstructorInitializerIndentWidth = 0;
10137   verifyFormat("SomeClass::Constructor()\n"
10138                ": a(a)\n"
10139                ", b(b)\n"
10140                ", c(c) {}",
10141                Style);
10142 
10143   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10144   Style.ConstructorInitializerIndentWidth = 4;
10145   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10146   verifyFormat(
10147       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10148       Style);
10149   verifyFormat(
10150       "SomeClass::Constructor()\n"
10151       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10152       Style);
10153   Style.ConstructorInitializerIndentWidth = 4;
10154   Style.ColumnLimit = 60;
10155   verifyFormat("SomeClass::Constructor()\n"
10156                "    : aaaaaaaa(aaaaaaaa)\n"
10157                "    , aaaaaaaa(aaaaaaaa)\n"
10158                "    , aaaaaaaa(aaaaaaaa) {}",
10159                Style);
10160 }
10161 
10162 TEST_F(FormatTest, Destructors) {
10163   verifyFormat("void F(int &i) { i.~int(); }");
10164   verifyFormat("void F(int &i) { i->~int(); }");
10165 }
10166 
10167 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10168   FormatStyle Style = getWebKitStyle();
10169 
10170   // Don't indent in outer namespaces.
10171   verifyFormat("namespace outer {\n"
10172                "int i;\n"
10173                "namespace inner {\n"
10174                "    int i;\n"
10175                "} // namespace inner\n"
10176                "} // namespace outer\n"
10177                "namespace other_outer {\n"
10178                "int i;\n"
10179                "}",
10180                Style);
10181 
10182   // Don't indent case labels.
10183   verifyFormat("switch (variable) {\n"
10184                "case 1:\n"
10185                "case 2:\n"
10186                "    doSomething();\n"
10187                "    break;\n"
10188                "default:\n"
10189                "    ++variable;\n"
10190                "}",
10191                Style);
10192 
10193   // Wrap before binary operators.
10194   EXPECT_EQ("void f()\n"
10195             "{\n"
10196             "    if (aaaaaaaaaaaaaaaa\n"
10197             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10198             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10199             "        return;\n"
10200             "}",
10201             format("void f() {\n"
10202                    "if (aaaaaaaaaaaaaaaa\n"
10203                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10204                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10205                    "return;\n"
10206                    "}",
10207                    Style));
10208 
10209   // Allow functions on a single line.
10210   verifyFormat("void f() { return; }", Style);
10211 
10212   // Constructor initializers are formatted one per line with the "," on the
10213   // new line.
10214   verifyFormat("Constructor()\n"
10215                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10216                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10217                "          aaaaaaaaaaaaaa)\n"
10218                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10219                "{\n"
10220                "}",
10221                Style);
10222   verifyFormat("SomeClass::Constructor()\n"
10223                "    : a(a)\n"
10224                "{\n"
10225                "}",
10226                Style);
10227   EXPECT_EQ("SomeClass::Constructor()\n"
10228             "    : a(a)\n"
10229             "{\n"
10230             "}",
10231             format("SomeClass::Constructor():a(a){}", Style));
10232   verifyFormat("SomeClass::Constructor()\n"
10233                "    : a(a)\n"
10234                "    , b(b)\n"
10235                "    , c(c)\n"
10236                "{\n"
10237                "}",
10238                Style);
10239   verifyFormat("SomeClass::Constructor()\n"
10240                "    : a(a)\n"
10241                "{\n"
10242                "    foo();\n"
10243                "    bar();\n"
10244                "}",
10245                Style);
10246 
10247   // Access specifiers should be aligned left.
10248   verifyFormat("class C {\n"
10249                "public:\n"
10250                "    int i;\n"
10251                "};",
10252                Style);
10253 
10254   // Do not align comments.
10255   verifyFormat("int a; // Do not\n"
10256                "double b; // align comments.",
10257                Style);
10258 
10259   // Do not align operands.
10260   EXPECT_EQ("ASSERT(aaaa\n"
10261             "    || bbbb);",
10262             format("ASSERT ( aaaa\n||bbbb);", Style));
10263 
10264   // Accept input's line breaks.
10265   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10266             "    || bbbbbbbbbbbbbbb) {\n"
10267             "    i++;\n"
10268             "}",
10269             format("if (aaaaaaaaaaaaaaa\n"
10270                    "|| bbbbbbbbbbbbbbb) { i++; }",
10271                    Style));
10272   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10273             "    i++;\n"
10274             "}",
10275             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10276 
10277   // Don't automatically break all macro definitions (llvm.org/PR17842).
10278   verifyFormat("#define aNumber 10", Style);
10279   // However, generally keep the line breaks that the user authored.
10280   EXPECT_EQ("#define aNumber \\\n"
10281             "    10",
10282             format("#define aNumber \\\n"
10283                    " 10",
10284                    Style));
10285 
10286   // Keep empty and one-element array literals on a single line.
10287   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10288             "                                  copyItems:YES];",
10289             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10290                    "copyItems:YES];",
10291                    Style));
10292   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10293             "                                  copyItems:YES];",
10294             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10295                    "             copyItems:YES];",
10296                    Style));
10297   // FIXME: This does not seem right, there should be more indentation before
10298   // the array literal's entries. Nested blocks have the same problem.
10299   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10300             "    @\"a\",\n"
10301             "    @\"a\"\n"
10302             "]\n"
10303             "                                  copyItems:YES];",
10304             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10305                    "     @\"a\",\n"
10306                    "     @\"a\"\n"
10307                    "     ]\n"
10308                    "       copyItems:YES];",
10309                    Style));
10310   EXPECT_EQ(
10311       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10312       "                                  copyItems:YES];",
10313       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10314              "   copyItems:YES];",
10315              Style));
10316 
10317   verifyFormat("[self.a b:c c:d];", Style);
10318   EXPECT_EQ("[self.a b:c\n"
10319             "        c:d];",
10320             format("[self.a b:c\n"
10321                    "c:d];",
10322                    Style));
10323 }
10324 
10325 TEST_F(FormatTest, FormatsLambdas) {
10326   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10327   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10328   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10329   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10330   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10331   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10332   verifyFormat("int x = f(*+[] {});");
10333   verifyFormat("void f() {\n"
10334                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10335                "}\n");
10336   verifyFormat("void f() {\n"
10337                "  other(x.begin(), //\n"
10338                "        x.end(),   //\n"
10339                "        [&](int, int) { return 1; });\n"
10340                "}\n");
10341   verifyFormat("SomeFunction([]() { // A cool function...\n"
10342                "  return 43;\n"
10343                "});");
10344   EXPECT_EQ("SomeFunction([]() {\n"
10345             "#define A a\n"
10346             "  return 43;\n"
10347             "});",
10348             format("SomeFunction([](){\n"
10349                    "#define A a\n"
10350                    "return 43;\n"
10351                    "});"));
10352   verifyFormat("void f() {\n"
10353                "  SomeFunction([](decltype(x), A *a) {});\n"
10354                "}");
10355   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10356                "    [](const aaaaaaaaaa &a) { return a; });");
10357   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10358                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10359                "});");
10360   verifyFormat("Constructor()\n"
10361                "    : Field([] { // comment\n"
10362                "        int i;\n"
10363                "      }) {}");
10364   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10365                "  return some_parameter.size();\n"
10366                "};");
10367   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10368                "    [](const string &s) { return s; };");
10369   verifyFormat("int i = aaaaaa ? 1 //\n"
10370                "               : [] {\n"
10371                "                   return 2; //\n"
10372                "                 }();");
10373   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10374                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10375                "                  return x == 2; // force break\n"
10376                "                });");
10377   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10378                "    [=](int iiiiiiiiiiii) {\n"
10379                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
10380                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
10381                "    });",
10382                getLLVMStyleWithColumns(60));
10383   verifyFormat("SomeFunction({[&] {\n"
10384                "                // comment\n"
10385                "              },\n"
10386                "              [&] {\n"
10387                "                // comment\n"
10388                "              }});");
10389   verifyFormat("SomeFunction({[&] {\n"
10390                "  // comment\n"
10391                "}});");
10392   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10393                "                             [&]() { return true; },\n"
10394                "                         aaaaa aaaaaaaaa);");
10395 
10396   // Lambdas with return types.
10397   verifyFormat("int c = []() -> int { return 2; }();\n");
10398   verifyFormat("int c = []() -> int * { return 2; }();\n");
10399   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10400   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10401   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10402   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10403   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10404   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10405   verifyFormat("[a, a]() -> a<1> {};");
10406   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10407                "                   int j) -> int {\n"
10408                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10409                "};");
10410   verifyFormat(
10411       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10412       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10413       "      return aaaaaaaaaaaaaaaaa;\n"
10414       "    });",
10415       getLLVMStyleWithColumns(70));
10416   verifyFormat("[]() //\n"
10417                "    -> int {\n"
10418                "  return 1; //\n"
10419                "};");
10420 
10421   // Multiple lambdas in the same parentheses change indentation rules.
10422   verifyFormat("SomeFunction(\n"
10423                "    []() {\n"
10424                "      int i = 42;\n"
10425                "      return i;\n"
10426                "    },\n"
10427                "    []() {\n"
10428                "      int j = 43;\n"
10429                "      return j;\n"
10430                "    });");
10431 
10432   // More complex introducers.
10433   verifyFormat("return [i, args...] {};");
10434 
10435   // Not lambdas.
10436   verifyFormat("constexpr char hello[]{\"hello\"};");
10437   verifyFormat("double &operator[](int i) { return 0; }\n"
10438                "int i;");
10439   verifyFormat("std::unique_ptr<int[]> foo() {}");
10440   verifyFormat("int i = a[a][a]->f();");
10441   verifyFormat("int i = (*b)[a]->f();");
10442 
10443   // Other corner cases.
10444   verifyFormat("void f() {\n"
10445                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10446                "  );\n"
10447                "}");
10448 
10449   // Lambdas created through weird macros.
10450   verifyFormat("void f() {\n"
10451                "  MACRO((const AA &a) { return 1; });\n"
10452                "  MACRO((AA &a) { return 1; });\n"
10453                "}");
10454 
10455   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10456                "      doo_dah();\n"
10457                "      doo_dah();\n"
10458                "    })) {\n"
10459                "}");
10460   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
10461                "                doo_dah();\n"
10462                "                doo_dah();\n"
10463                "              })) {\n"
10464                "}");
10465   verifyFormat("auto lambda = []() {\n"
10466                "  int a = 2\n"
10467                "#if A\n"
10468                "          + 2\n"
10469                "#endif\n"
10470                "      ;\n"
10471                "};");
10472 
10473   // Lambdas with complex multiline introducers.
10474   verifyFormat(
10475       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10476       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
10477       "        -> ::std::unordered_set<\n"
10478       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
10479       "      //\n"
10480       "    });");
10481 }
10482 
10483 TEST_F(FormatTest, FormatsBlocks) {
10484   FormatStyle ShortBlocks = getLLVMStyle();
10485   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10486   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10487   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10488   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10489   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10490   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10491   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10492 
10493   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10494   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10495   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10496 
10497   verifyFormat("[operation setCompletionBlock:^{\n"
10498                "  [self onOperationDone];\n"
10499                "}];");
10500   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10501                "  [self onOperationDone];\n"
10502                "}]};");
10503   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10504                "  f();\n"
10505                "}];");
10506   verifyFormat("int a = [operation block:^int(int *i) {\n"
10507                "  return 1;\n"
10508                "}];");
10509   verifyFormat("[myObject doSomethingWith:arg1\n"
10510                "                      aaa:^int(int *a) {\n"
10511                "                        return 1;\n"
10512                "                      }\n"
10513                "                      bbb:f(a * bbbbbbbb)];");
10514 
10515   verifyFormat("[operation setCompletionBlock:^{\n"
10516                "  [self.delegate newDataAvailable];\n"
10517                "}];",
10518                getLLVMStyleWithColumns(60));
10519   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10520                "  NSString *path = [self sessionFilePath];\n"
10521                "  if (path) {\n"
10522                "    // ...\n"
10523                "  }\n"
10524                "});");
10525   verifyFormat("[[SessionService sharedService]\n"
10526                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10527                "      if (window) {\n"
10528                "        [self windowDidLoad:window];\n"
10529                "      } else {\n"
10530                "        [self errorLoadingWindow];\n"
10531                "      }\n"
10532                "    }];");
10533   verifyFormat("void (^largeBlock)(void) = ^{\n"
10534                "  // ...\n"
10535                "};\n",
10536                getLLVMStyleWithColumns(40));
10537   verifyFormat("[[SessionService sharedService]\n"
10538                "    loadWindowWithCompletionBlock: //\n"
10539                "        ^(SessionWindow *window) {\n"
10540                "          if (window) {\n"
10541                "            [self windowDidLoad:window];\n"
10542                "          } else {\n"
10543                "            [self errorLoadingWindow];\n"
10544                "          }\n"
10545                "        }];",
10546                getLLVMStyleWithColumns(60));
10547   verifyFormat("[myObject doSomethingWith:arg1\n"
10548                "    firstBlock:^(Foo *a) {\n"
10549                "      // ...\n"
10550                "      int i;\n"
10551                "    }\n"
10552                "    secondBlock:^(Bar *b) {\n"
10553                "      // ...\n"
10554                "      int i;\n"
10555                "    }\n"
10556                "    thirdBlock:^Foo(Bar *b) {\n"
10557                "      // ...\n"
10558                "      int i;\n"
10559                "    }];");
10560   verifyFormat("[myObject doSomethingWith:arg1\n"
10561                "               firstBlock:-1\n"
10562                "              secondBlock:^(Bar *b) {\n"
10563                "                // ...\n"
10564                "                int i;\n"
10565                "              }];");
10566 
10567   verifyFormat("f(^{\n"
10568                "  @autoreleasepool {\n"
10569                "    if (a) {\n"
10570                "      g();\n"
10571                "    }\n"
10572                "  }\n"
10573                "});");
10574   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10575   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
10576                "};");
10577 
10578   FormatStyle FourIndent = getLLVMStyle();
10579   FourIndent.ObjCBlockIndentWidth = 4;
10580   verifyFormat("[operation setCompletionBlock:^{\n"
10581                "    [self onOperationDone];\n"
10582                "}];",
10583                FourIndent);
10584 }
10585 
10586 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10587   FormatStyle ZeroColumn = getLLVMStyle();
10588   ZeroColumn.ColumnLimit = 0;
10589 
10590   verifyFormat("[[SessionService sharedService] "
10591                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10592                "  if (window) {\n"
10593                "    [self windowDidLoad:window];\n"
10594                "  } else {\n"
10595                "    [self errorLoadingWindow];\n"
10596                "  }\n"
10597                "}];",
10598                ZeroColumn);
10599   EXPECT_EQ("[[SessionService sharedService]\n"
10600             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10601             "      if (window) {\n"
10602             "        [self windowDidLoad:window];\n"
10603             "      } else {\n"
10604             "        [self errorLoadingWindow];\n"
10605             "      }\n"
10606             "    }];",
10607             format("[[SessionService sharedService]\n"
10608                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10609                    "                if (window) {\n"
10610                    "    [self windowDidLoad:window];\n"
10611                    "  } else {\n"
10612                    "    [self errorLoadingWindow];\n"
10613                    "  }\n"
10614                    "}];",
10615                    ZeroColumn));
10616   verifyFormat("[myObject doSomethingWith:arg1\n"
10617                "    firstBlock:^(Foo *a) {\n"
10618                "      // ...\n"
10619                "      int i;\n"
10620                "    }\n"
10621                "    secondBlock:^(Bar *b) {\n"
10622                "      // ...\n"
10623                "      int i;\n"
10624                "    }\n"
10625                "    thirdBlock:^Foo(Bar *b) {\n"
10626                "      // ...\n"
10627                "      int i;\n"
10628                "    }];",
10629                ZeroColumn);
10630   verifyFormat("f(^{\n"
10631                "  @autoreleasepool {\n"
10632                "    if (a) {\n"
10633                "      g();\n"
10634                "    }\n"
10635                "  }\n"
10636                "});",
10637                ZeroColumn);
10638   verifyFormat("void (^largeBlock)(void) = ^{\n"
10639                "  // ...\n"
10640                "};",
10641                ZeroColumn);
10642 
10643   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10644   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10645             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10646   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10647   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10648             "  int i;\n"
10649             "};",
10650             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10651 }
10652 
10653 TEST_F(FormatTest, SupportsCRLF) {
10654   EXPECT_EQ("int a;\r\n"
10655             "int b;\r\n"
10656             "int c;\r\n",
10657             format("int a;\r\n"
10658                    "  int b;\r\n"
10659                    "    int c;\r\n",
10660                    getLLVMStyle()));
10661   EXPECT_EQ("int a;\r\n"
10662             "int b;\r\n"
10663             "int c;\r\n",
10664             format("int a;\r\n"
10665                    "  int b;\n"
10666                    "    int c;\r\n",
10667                    getLLVMStyle()));
10668   EXPECT_EQ("int a;\n"
10669             "int b;\n"
10670             "int c;\n",
10671             format("int a;\r\n"
10672                    "  int b;\n"
10673                    "    int c;\n",
10674                    getLLVMStyle()));
10675   EXPECT_EQ("\"aaaaaaa \"\r\n"
10676             "\"bbbbbbb\";\r\n",
10677             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10678   EXPECT_EQ("#define A \\\r\n"
10679             "  b;      \\\r\n"
10680             "  c;      \\\r\n"
10681             "  d;\r\n",
10682             format("#define A \\\r\n"
10683                    "  b; \\\r\n"
10684                    "  c; d; \r\n",
10685                    getGoogleStyle()));
10686 
10687   EXPECT_EQ("/*\r\n"
10688             "multi line block comments\r\n"
10689             "should not introduce\r\n"
10690             "an extra carriage return\r\n"
10691             "*/\r\n",
10692             format("/*\r\n"
10693                    "multi line block comments\r\n"
10694                    "should not introduce\r\n"
10695                    "an extra carriage return\r\n"
10696                    "*/\r\n"));
10697 }
10698 
10699 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10700   verifyFormat("MY_CLASS(C) {\n"
10701                "  int i;\n"
10702                "  int j;\n"
10703                "};");
10704 }
10705 
10706 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10707   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10708   TwoIndent.ContinuationIndentWidth = 2;
10709 
10710   EXPECT_EQ("int i =\n"
10711             "  longFunction(\n"
10712             "    arg);",
10713             format("int i = longFunction(arg);", TwoIndent));
10714 
10715   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10716   SixIndent.ContinuationIndentWidth = 6;
10717 
10718   EXPECT_EQ("int i =\n"
10719             "      longFunction(\n"
10720             "            arg);",
10721             format("int i = longFunction(arg);", SixIndent));
10722 }
10723 
10724 TEST_F(FormatTest, SpacesInAngles) {
10725   FormatStyle Spaces = getLLVMStyle();
10726   Spaces.SpacesInAngles = true;
10727 
10728   verifyFormat("static_cast< int >(arg);", Spaces);
10729   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10730   verifyFormat("f< int, float >();", Spaces);
10731   verifyFormat("template <> g() {}", Spaces);
10732   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10733   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10734   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10735                Spaces);
10736 
10737   Spaces.Standard = FormatStyle::LS_Cpp03;
10738   Spaces.SpacesInAngles = true;
10739   verifyFormat("A< A< int > >();", Spaces);
10740 
10741   Spaces.SpacesInAngles = false;
10742   verifyFormat("A<A<int> >();", Spaces);
10743 
10744   Spaces.Standard = FormatStyle::LS_Cpp11;
10745   Spaces.SpacesInAngles = true;
10746   verifyFormat("A< A< int > >();", Spaces);
10747 
10748   Spaces.SpacesInAngles = false;
10749   verifyFormat("A<A<int>>();", Spaces);
10750 }
10751 
10752 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
10753   FormatStyle Style = getLLVMStyle();
10754   Style.SpaceAfterTemplateKeyword = false;
10755   verifyFormat("template<int> void foo();", Style);
10756 }
10757 
10758 TEST_F(FormatTest, TripleAngleBrackets) {
10759   verifyFormat("f<<<1, 1>>>();");
10760   verifyFormat("f<<<1, 1, 1, s>>>();");
10761   verifyFormat("f<<<a, b, c, d>>>();");
10762   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10763   verifyFormat("f<param><<<1, 1>>>();");
10764   verifyFormat("f<1><<<1, 1>>>();");
10765   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10766   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10767                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10768   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
10769                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
10770 }
10771 
10772 TEST_F(FormatTest, MergeLessLessAtEnd) {
10773   verifyFormat("<<");
10774   EXPECT_EQ("< < <", format("\\\n<<<"));
10775   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10776                "aaallvm::outs() <<");
10777   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10778                "aaaallvm::outs()\n    <<");
10779 }
10780 
10781 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10782   std::string code = "#if A\n"
10783                      "#if B\n"
10784                      "a.\n"
10785                      "#endif\n"
10786                      "    a = 1;\n"
10787                      "#else\n"
10788                      "#endif\n"
10789                      "#if C\n"
10790                      "#else\n"
10791                      "#endif\n";
10792   EXPECT_EQ(code, format(code));
10793 }
10794 
10795 TEST_F(FormatTest, HandleConflictMarkers) {
10796   // Git/SVN conflict markers.
10797   EXPECT_EQ("int a;\n"
10798             "void f() {\n"
10799             "  callme(some(parameter1,\n"
10800             "<<<<<<< text by the vcs\n"
10801             "              parameter2),\n"
10802             "||||||| text by the vcs\n"
10803             "              parameter2),\n"
10804             "         parameter3,\n"
10805             "======= text by the vcs\n"
10806             "              parameter2, parameter3),\n"
10807             ">>>>>>> text by the vcs\n"
10808             "         otherparameter);\n",
10809             format("int a;\n"
10810                    "void f() {\n"
10811                    "  callme(some(parameter1,\n"
10812                    "<<<<<<< text by the vcs\n"
10813                    "  parameter2),\n"
10814                    "||||||| text by the vcs\n"
10815                    "  parameter2),\n"
10816                    "  parameter3,\n"
10817                    "======= text by the vcs\n"
10818                    "  parameter2,\n"
10819                    "  parameter3),\n"
10820                    ">>>>>>> text by the vcs\n"
10821                    "  otherparameter);\n"));
10822 
10823   // Perforce markers.
10824   EXPECT_EQ("void f() {\n"
10825             "  function(\n"
10826             ">>>> text by the vcs\n"
10827             "      parameter,\n"
10828             "==== text by the vcs\n"
10829             "      parameter,\n"
10830             "==== text by the vcs\n"
10831             "      parameter,\n"
10832             "<<<< text by the vcs\n"
10833             "      parameter);\n",
10834             format("void f() {\n"
10835                    "  function(\n"
10836                    ">>>> text by the vcs\n"
10837                    "  parameter,\n"
10838                    "==== text by the vcs\n"
10839                    "  parameter,\n"
10840                    "==== text by the vcs\n"
10841                    "  parameter,\n"
10842                    "<<<< text by the vcs\n"
10843                    "  parameter);\n"));
10844 
10845   EXPECT_EQ("<<<<<<<\n"
10846             "|||||||\n"
10847             "=======\n"
10848             ">>>>>>>",
10849             format("<<<<<<<\n"
10850                    "|||||||\n"
10851                    "=======\n"
10852                    ">>>>>>>"));
10853 
10854   EXPECT_EQ("<<<<<<<\n"
10855             "|||||||\n"
10856             "int i;\n"
10857             "=======\n"
10858             ">>>>>>>",
10859             format("<<<<<<<\n"
10860                    "|||||||\n"
10861                    "int i;\n"
10862                    "=======\n"
10863                    ">>>>>>>"));
10864 
10865   // FIXME: Handle parsing of macros around conflict markers correctly:
10866   EXPECT_EQ("#define Macro \\\n"
10867             "<<<<<<<\n"
10868             "Something \\\n"
10869             "|||||||\n"
10870             "Else \\\n"
10871             "=======\n"
10872             "Other \\\n"
10873             ">>>>>>>\n"
10874             "    End int i;\n",
10875             format("#define Macro \\\n"
10876                    "<<<<<<<\n"
10877                    "  Something \\\n"
10878                    "|||||||\n"
10879                    "  Else \\\n"
10880                    "=======\n"
10881                    "  Other \\\n"
10882                    ">>>>>>>\n"
10883                    "  End\n"
10884                    "int i;\n"));
10885 }
10886 
10887 TEST_F(FormatTest, DisableRegions) {
10888   EXPECT_EQ("int i;\n"
10889             "// clang-format off\n"
10890             "  int j;\n"
10891             "// clang-format on\n"
10892             "int k;",
10893             format(" int  i;\n"
10894                    "   // clang-format off\n"
10895                    "  int j;\n"
10896                    " // clang-format on\n"
10897                    "   int   k;"));
10898   EXPECT_EQ("int i;\n"
10899             "/* clang-format off */\n"
10900             "  int j;\n"
10901             "/* clang-format on */\n"
10902             "int k;",
10903             format(" int  i;\n"
10904                    "   /* clang-format off */\n"
10905                    "  int j;\n"
10906                    " /* clang-format on */\n"
10907                    "   int   k;"));
10908 
10909   // Don't reflow comments within disabled regions.
10910   EXPECT_EQ(
10911       "// clang-format off\n"
10912       "// long long long long long long line\n"
10913       "/* clang-format on */\n"
10914       "/* long long long\n"
10915       " * long long long\n"
10916       " * line */\n"
10917       "int i;\n"
10918       "/* clang-format off */\n"
10919       "/* long long long long long long line */\n",
10920       format("// clang-format off\n"
10921              "// long long long long long long line\n"
10922              "/* clang-format on */\n"
10923              "/* long long long long long long line */\n"
10924              "int i;\n"
10925              "/* clang-format off */\n"
10926              "/* long long long long long long line */\n",
10927              getLLVMStyleWithColumns(20)));
10928 }
10929 
10930 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10931   format("? ) =");
10932   verifyNoCrash("#define a\\\n /**/}");
10933 }
10934 
10935 TEST_F(FormatTest, FormatsTableGenCode) {
10936   FormatStyle Style = getLLVMStyle();
10937   Style.Language = FormatStyle::LK_TableGen;
10938   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10939 }
10940 
10941 TEST_F(FormatTest, ArrayOfTemplates) {
10942   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10943             format("auto a = new unique_ptr<int > [ 10];"));
10944 
10945   FormatStyle Spaces = getLLVMStyle();
10946   Spaces.SpacesInSquareBrackets = true;
10947   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10948             format("auto a = new unique_ptr<int > [10];", Spaces));
10949 }
10950 
10951 TEST_F(FormatTest, ArrayAsTemplateType) {
10952   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10953             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10954 
10955   FormatStyle Spaces = getLLVMStyle();
10956   Spaces.SpacesInSquareBrackets = true;
10957   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10958             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10959 }
10960 
10961 TEST_F(FormatTest, NoSpaceAfterSuper) {
10962     verifyFormat("__super::FooBar();");
10963 }
10964 
10965 TEST(FormatStyle, GetStyleOfFile) {
10966   vfs::InMemoryFileSystem FS;
10967   // Test 1: format file in the same directory.
10968   ASSERT_TRUE(
10969       FS.addFile("/a/.clang-format", 0,
10970                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10971   ASSERT_TRUE(
10972       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10973   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10974   ASSERT_TRUE((bool)Style1);
10975   ASSERT_EQ(*Style1, getLLVMStyle());
10976 
10977   // Test 2.1: fallback to default.
10978   ASSERT_TRUE(
10979       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10980   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10981   ASSERT_TRUE((bool)Style2);
10982   ASSERT_EQ(*Style2, getMozillaStyle());
10983 
10984   // Test 2.2: no format on 'none' fallback style.
10985   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10986   ASSERT_TRUE((bool)Style2);
10987   ASSERT_EQ(*Style2, getNoStyle());
10988 
10989   // Test 2.3: format if config is found with no based style while fallback is
10990   // 'none'.
10991   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
10992                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
10993   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10994   ASSERT_TRUE((bool)Style2);
10995   ASSERT_EQ(*Style2, getLLVMStyle());
10996 
10997   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
10998   Style2 = getStyle("{}", "a.h", "none", "", &FS);
10999   ASSERT_TRUE((bool)Style2);
11000   ASSERT_EQ(*Style2, getLLVMStyle());
11001 
11002   // Test 3: format file in parent directory.
11003   ASSERT_TRUE(
11004       FS.addFile("/c/.clang-format", 0,
11005                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11006   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11007                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11008   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11009   ASSERT_TRUE((bool)Style3);
11010   ASSERT_EQ(*Style3, getGoogleStyle());
11011 
11012   // Test 4: error on invalid fallback style
11013   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11014   ASSERT_FALSE((bool)Style4);
11015   llvm::consumeError(Style4.takeError());
11016 
11017   // Test 5: error on invalid yaml on command line
11018   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11019   ASSERT_FALSE((bool)Style5);
11020   llvm::consumeError(Style5.takeError());
11021 
11022   // Test 6: error on invalid style
11023   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11024   ASSERT_FALSE((bool)Style6);
11025   llvm::consumeError(Style6.takeError());
11026 
11027   // Test 7: found config file, error on parsing it
11028   ASSERT_TRUE(
11029       FS.addFile("/d/.clang-format", 0,
11030                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11031                                                   "InvalidKey: InvalidValue")));
11032   ASSERT_TRUE(
11033       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11034   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11035   ASSERT_FALSE((bool)Style7);
11036   llvm::consumeError(Style7.takeError());
11037 }
11038 
11039 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11040   // Column limit is 20.
11041   std::string Code = "Type *a =\n"
11042                      "    new Type();\n"
11043                      "g(iiiii, 0, jjjjj,\n"
11044                      "  0, kkkkk, 0, mm);\n"
11045                      "int  bad     = format   ;";
11046   std::string Expected = "auto a = new Type();\n"
11047                          "g(iiiii, nullptr,\n"
11048                          "  jjjjj, nullptr,\n"
11049                          "  kkkkk, nullptr,\n"
11050                          "  mm);\n"
11051                          "int  bad     = format   ;";
11052   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11053   tooling::Replacements Replaces = toReplacements(
11054       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11055                             "auto "),
11056        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11057                             "nullptr"),
11058        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11059                             "nullptr"),
11060        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11061                             "nullptr")});
11062 
11063   format::FormatStyle Style = format::getLLVMStyle();
11064   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11065   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11066   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11067       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11068   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11069   EXPECT_TRUE(static_cast<bool>(Result));
11070   EXPECT_EQ(Expected, *Result);
11071 }
11072 
11073 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11074   std::string Code = "#include \"a.h\"\n"
11075                      "#include \"c.h\"\n"
11076                      "\n"
11077                      "int main() {\n"
11078                      "  return 0;\n"
11079                      "}";
11080   std::string Expected = "#include \"a.h\"\n"
11081                          "#include \"b.h\"\n"
11082                          "#include \"c.h\"\n"
11083                          "\n"
11084                          "int main() {\n"
11085                          "  return 0;\n"
11086                          "}";
11087   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11088   tooling::Replacements Replaces = toReplacements(
11089       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11090                             "#include \"b.h\"\n")});
11091 
11092   format::FormatStyle Style = format::getLLVMStyle();
11093   Style.SortIncludes = true;
11094   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11095   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11096       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11097   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11098   EXPECT_TRUE(static_cast<bool>(Result));
11099   EXPECT_EQ(Expected, *Result);
11100 }
11101 
11102 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
11103   EXPECT_EQ("using std::cin;\n"
11104             "using std::cout;",
11105             format("using std::cout;\n"
11106                    "using std::cin;", getGoogleStyle()));
11107 }
11108 
11109 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
11110   format::FormatStyle Style = format::getLLVMStyle();
11111   Style.Standard = FormatStyle::LS_Cpp03;
11112   // cpp03 recognize this string as identifier u8 and literal character 'a'
11113   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
11114 }
11115 
11116 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
11117   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
11118   // all modes, including C++11, C++14 and C++17
11119   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
11120 }
11121 
11122 } // end namespace
11123 } // end namespace format
11124 } // end namespace clang
11125