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                "case 0: return; // comment\n"
911                "case 1: break;  // comment\n"
912                "case 2: return;\n"
913                "// comment\n"
914                "case 3: return;\n"
915                "// comment 1\n"
916                "// comment 2\n"
917                "// comment 3\n"
918                "case 4: break; /* comment */\n"
919                "case 5:\n"
920                "  // comment\n"
921                "  break;\n"
922                "case 6: /* comment */ x = 1; break;\n"
923                "case 7: x = /* comment */ 1; break;\n"
924                "case 8:\n"
925                "  x = 1; /* comment */\n"
926                "  break;\n"
927                "case 9:\n"
928                "  break; // comment line 1\n"
929                "         // comment line 2\n"
930                "}",
931                Style);
932   EXPECT_EQ("switch (a) {\n"
933             "case 1:\n"
934             "  x = 8;\n"
935             "  // fall through\n"
936             "case 2: x = 8;\n"
937             "// comment\n"
938             "case 3:\n"
939             "  return; /* comment line 1\n"
940             "           * comment line 2 */\n"
941             "case 4: i = 8;\n"
942             "// something else\n"
943             "#if FOO\n"
944             "case 5: break;\n"
945             "#endif\n"
946             "}",
947             format("switch (a) {\n"
948                    "case 1: x = 8;\n"
949                    "  // fall through\n"
950                    "case 2:\n"
951                    "  x = 8;\n"
952                    "// comment\n"
953                    "case 3:\n"
954                    "  return; /* comment line 1\n"
955                    "           * comment line 2 */\n"
956                    "case 4:\n"
957                    "  i = 8;\n"
958                    "// something else\n"
959                    "#if FOO\n"
960                    "case 5: break;\n"
961                    "#endif\n"
962                    "}",
963                    Style));
964   EXPECT_EQ("switch (a) {\n" "case 0:\n"
965             "  return; // long long long long long long long long long long long long comment\n"
966             "          // line\n" "}",
967             format("switch (a) {\n"
968                    "case 0: return; // long long long long long long long long long long long long comment line\n"
969                    "}",
970                    Style));
971   EXPECT_EQ("switch (a) {\n"
972             "case 0:\n"
973             "  return; /* long long long long long long long long long long long long comment\n"
974             "             line */\n"
975             "}",
976             format("switch (a) {\n"
977                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
978                    "}",
979                    Style));
980   verifyFormat("switch (a) {\n"
981                "#if FOO\n"
982                "case 0: return 0;\n"
983                "#endif\n"
984                "}",
985                Style);
986   verifyFormat("switch (a) {\n"
987                "case 1: {\n"
988                "}\n"
989                "case 2: {\n"
990                "  return;\n"
991                "}\n"
992                "case 3: {\n"
993                "  x = 1;\n"
994                "  return;\n"
995                "}\n"
996                "case 4:\n"
997                "  if (x)\n"
998                "    return;\n"
999                "}",
1000                Style);
1001   Style.ColumnLimit = 21;
1002   verifyFormat("switch (a) {\n"
1003                "case 1: x = 1; break;\n"
1004                "case 2: return;\n"
1005                "case 3:\n"
1006                "case 4:\n"
1007                "case 5: return;\n"
1008                "default:\n"
1009                "  y = 1;\n"
1010                "  break;\n"
1011                "}",
1012                Style);
1013 }
1014 
1015 TEST_F(FormatTest, FormatsLabels) {
1016   verifyFormat("void f() {\n"
1017                "  some_code();\n"
1018                "test_label:\n"
1019                "  some_other_code();\n"
1020                "  {\n"
1021                "    some_more_code();\n"
1022                "  another_label:\n"
1023                "    some_more_code();\n"
1024                "  }\n"
1025                "}");
1026   verifyFormat("{\n"
1027                "  some_code();\n"
1028                "test_label:\n"
1029                "  some_other_code();\n"
1030                "}");
1031   verifyFormat("{\n"
1032                "  some_code();\n"
1033                "test_label:;\n"
1034                "  int i = 0;\n"
1035                "}");
1036 }
1037 
1038 //===----------------------------------------------------------------------===//
1039 // Tests for classes, namespaces, etc.
1040 //===----------------------------------------------------------------------===//
1041 
1042 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1043   verifyFormat("class A {};");
1044 }
1045 
1046 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1047   verifyFormat("class A {\n"
1048                "public:\n"
1049                "public: // comment\n"
1050                "protected:\n"
1051                "private:\n"
1052                "  void f() {}\n"
1053                "};");
1054   verifyGoogleFormat("class A {\n"
1055                      " public:\n"
1056                      " protected:\n"
1057                      " private:\n"
1058                      "  void f() {}\n"
1059                      "};");
1060   verifyFormat("class A {\n"
1061                "public slots:\n"
1062                "  void f1() {}\n"
1063                "public Q_SLOTS:\n"
1064                "  void f2() {}\n"
1065                "protected slots:\n"
1066                "  void f3() {}\n"
1067                "protected Q_SLOTS:\n"
1068                "  void f4() {}\n"
1069                "private slots:\n"
1070                "  void f5() {}\n"
1071                "private Q_SLOTS:\n"
1072                "  void f6() {}\n"
1073                "signals:\n"
1074                "  void g1();\n"
1075                "Q_SIGNALS:\n"
1076                "  void g2();\n"
1077                "};");
1078 
1079   // Don't interpret 'signals' the wrong way.
1080   verifyFormat("signals.set();");
1081   verifyFormat("for (Signals signals : f()) {\n}");
1082   verifyFormat("{\n"
1083                "  signals.set(); // This needs indentation.\n"
1084                "}");
1085   verifyFormat("void f() {\n"
1086                "label:\n"
1087                "  signals.baz();\n"
1088                "}");
1089 }
1090 
1091 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1092   EXPECT_EQ("class A {\n"
1093             "public:\n"
1094             "  void f();\n"
1095             "\n"
1096             "private:\n"
1097             "  void g() {}\n"
1098             "  // test\n"
1099             "protected:\n"
1100             "  int h;\n"
1101             "};",
1102             format("class A {\n"
1103                    "public:\n"
1104                    "void f();\n"
1105                    "private:\n"
1106                    "void g() {}\n"
1107                    "// test\n"
1108                    "protected:\n"
1109                    "int h;\n"
1110                    "};"));
1111   EXPECT_EQ("class A {\n"
1112             "protected:\n"
1113             "public:\n"
1114             "  void f();\n"
1115             "};",
1116             format("class A {\n"
1117                    "protected:\n"
1118                    "\n"
1119                    "public:\n"
1120                    "\n"
1121                    "  void f();\n"
1122                    "};"));
1123 
1124   // Even ensure proper spacing inside macros.
1125   EXPECT_EQ("#define B     \\\n"
1126             "  class A {   \\\n"
1127             "   protected: \\\n"
1128             "   public:    \\\n"
1129             "    void f(); \\\n"
1130             "  };",
1131             format("#define B     \\\n"
1132                    "  class A {   \\\n"
1133                    "   protected: \\\n"
1134                    "              \\\n"
1135                    "   public:    \\\n"
1136                    "              \\\n"
1137                    "    void f(); \\\n"
1138                    "  };",
1139                    getGoogleStyle()));
1140   // But don't remove empty lines after macros ending in access specifiers.
1141   EXPECT_EQ("#define A private:\n"
1142             "\n"
1143             "int i;",
1144             format("#define A         private:\n"
1145                    "\n"
1146                    "int              i;"));
1147 }
1148 
1149 TEST_F(FormatTest, FormatsClasses) {
1150   verifyFormat("class A : public B {};");
1151   verifyFormat("class A : public ::B {};");
1152 
1153   verifyFormat(
1154       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1155       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1156   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1157                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1158                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1159   verifyFormat(
1160       "class A : public B, public C, public D, public E, public F {};");
1161   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1162                "                     public C,\n"
1163                "                     public D,\n"
1164                "                     public E,\n"
1165                "                     public F,\n"
1166                "                     public G {};");
1167 
1168   verifyFormat("class\n"
1169                "    ReallyReallyLongClassName {\n"
1170                "  int i;\n"
1171                "};",
1172                getLLVMStyleWithColumns(32));
1173   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1174                "                           aaaaaaaaaaaaaaaa> {};");
1175   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1176                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1177                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1178   verifyFormat("template <class R, class C>\n"
1179                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1180                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1181   verifyFormat("class ::A::B {};");
1182 }
1183 
1184 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1185   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1186   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1187 
1188   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1189   verifyFormat("class MyClass\n"
1190                "    : public X\n"
1191                "    , public Y {};",
1192                StyleWithInheritanceBreak);
1193 }
1194 
1195 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1196   verifyFormat("class A {\n} a, b;");
1197   verifyFormat("struct A {\n} a, b;");
1198   verifyFormat("union A {\n} a;");
1199 }
1200 
1201 TEST_F(FormatTest, FormatsEnum) {
1202   verifyFormat("enum {\n"
1203                "  Zero,\n"
1204                "  One = 1,\n"
1205                "  Two = One + 1,\n"
1206                "  Three = (One + Two),\n"
1207                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1208                "  Five = (One, Two, Three, Four, 5)\n"
1209                "};");
1210   verifyGoogleFormat("enum {\n"
1211                      "  Zero,\n"
1212                      "  One = 1,\n"
1213                      "  Two = One + 1,\n"
1214                      "  Three = (One + Two),\n"
1215                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1216                      "  Five = (One, Two, Three, Four, 5)\n"
1217                      "};");
1218   verifyFormat("enum Enum {};");
1219   verifyFormat("enum {};");
1220   verifyFormat("enum X E {} d;");
1221   verifyFormat("enum __attribute__((...)) E {} d;");
1222   verifyFormat("enum __declspec__((...)) E {} d;");
1223   verifyFormat("enum {\n"
1224                "  Bar = Foo<int, int>::value\n"
1225                "};",
1226                getLLVMStyleWithColumns(30));
1227 
1228   verifyFormat("enum ShortEnum { A, B, C };");
1229   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1230 
1231   EXPECT_EQ("enum KeepEmptyLines {\n"
1232             "  ONE,\n"
1233             "\n"
1234             "  TWO,\n"
1235             "\n"
1236             "  THREE\n"
1237             "}",
1238             format("enum KeepEmptyLines {\n"
1239                    "  ONE,\n"
1240                    "\n"
1241                    "  TWO,\n"
1242                    "\n"
1243                    "\n"
1244                    "  THREE\n"
1245                    "}"));
1246   verifyFormat("enum E { // comment\n"
1247                "  ONE,\n"
1248                "  TWO\n"
1249                "};\n"
1250                "int i;");
1251   // Not enums.
1252   verifyFormat("enum X f() {\n"
1253                "  a();\n"
1254                "  return 42;\n"
1255                "}");
1256   verifyFormat("enum X Type::f() {\n"
1257                "  a();\n"
1258                "  return 42;\n"
1259                "}");
1260   verifyFormat("enum ::X f() {\n"
1261                "  a();\n"
1262                "  return 42;\n"
1263                "}");
1264   verifyFormat("enum ns::X f() {\n"
1265                "  a();\n"
1266                "  return 42;\n"
1267                "}");
1268 }
1269 
1270 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1271   verifyFormat("enum Type {\n"
1272                "  One = 0; // These semicolons should be commas.\n"
1273                "  Two = 1;\n"
1274                "};");
1275   verifyFormat("namespace n {\n"
1276                "enum Type {\n"
1277                "  One,\n"
1278                "  Two, // missing };\n"
1279                "  int i;\n"
1280                "}\n"
1281                "void g() {}");
1282 }
1283 
1284 TEST_F(FormatTest, FormatsEnumStruct) {
1285   verifyFormat("enum struct {\n"
1286                "  Zero,\n"
1287                "  One = 1,\n"
1288                "  Two = One + 1,\n"
1289                "  Three = (One + Two),\n"
1290                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1291                "  Five = (One, Two, Three, Four, 5)\n"
1292                "};");
1293   verifyFormat("enum struct Enum {};");
1294   verifyFormat("enum struct {};");
1295   verifyFormat("enum struct X E {} d;");
1296   verifyFormat("enum struct __attribute__((...)) E {} d;");
1297   verifyFormat("enum struct __declspec__((...)) E {} d;");
1298   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1299 }
1300 
1301 TEST_F(FormatTest, FormatsEnumClass) {
1302   verifyFormat("enum class {\n"
1303                "  Zero,\n"
1304                "  One = 1,\n"
1305                "  Two = One + 1,\n"
1306                "  Three = (One + Two),\n"
1307                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1308                "  Five = (One, Two, Three, Four, 5)\n"
1309                "};");
1310   verifyFormat("enum class Enum {};");
1311   verifyFormat("enum class {};");
1312   verifyFormat("enum class X E {} d;");
1313   verifyFormat("enum class __attribute__((...)) E {} d;");
1314   verifyFormat("enum class __declspec__((...)) E {} d;");
1315   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1316 }
1317 
1318 TEST_F(FormatTest, FormatsEnumTypes) {
1319   verifyFormat("enum X : int {\n"
1320                "  A, // Force multiple lines.\n"
1321                "  B\n"
1322                "};");
1323   verifyFormat("enum X : int { A, B };");
1324   verifyFormat("enum X : std::uint32_t { A, B };");
1325 }
1326 
1327 TEST_F(FormatTest, FormatsTypedefEnum) {
1328   FormatStyle Style = getLLVMStyle();
1329   Style.ColumnLimit = 40;
1330   verifyFormat("typedef enum {} EmptyEnum;");
1331   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1332   verifyFormat("typedef enum {\n"
1333                "  ZERO = 0,\n"
1334                "  ONE = 1,\n"
1335                "  TWO = 2,\n"
1336                "  THREE = 3\n"
1337                "} LongEnum;",
1338                Style);
1339   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1340   Style.BraceWrapping.AfterEnum = true;
1341   verifyFormat("typedef enum {} EmptyEnum;");
1342   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1343   verifyFormat("typedef enum\n"
1344                "{\n"
1345                "  ZERO = 0,\n"
1346                "  ONE = 1,\n"
1347                "  TWO = 2,\n"
1348                "  THREE = 3\n"
1349                "} LongEnum;",
1350                Style);
1351 }
1352 
1353 TEST_F(FormatTest, FormatsNSEnums) {
1354   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1355   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1356                      "  // Information about someDecentlyLongValue.\n"
1357                      "  someDecentlyLongValue,\n"
1358                      "  // Information about anotherDecentlyLongValue.\n"
1359                      "  anotherDecentlyLongValue,\n"
1360                      "  // Information about aThirdDecentlyLongValue.\n"
1361                      "  aThirdDecentlyLongValue\n"
1362                      "};");
1363   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1364                      "  a = 1,\n"
1365                      "  b = 2,\n"
1366                      "  c = 3,\n"
1367                      "};");
1368   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1369                      "  a = 1,\n"
1370                      "  b = 2,\n"
1371                      "  c = 3,\n"
1372                      "};");
1373   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1374                      "  a = 1,\n"
1375                      "  b = 2,\n"
1376                      "  c = 3,\n"
1377                      "};");
1378 }
1379 
1380 TEST_F(FormatTest, FormatsBitfields) {
1381   verifyFormat("struct Bitfields {\n"
1382                "  unsigned sClass : 8;\n"
1383                "  unsigned ValueKind : 2;\n"
1384                "};");
1385   verifyFormat("struct A {\n"
1386                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1387                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1388                "};");
1389   verifyFormat("struct MyStruct {\n"
1390                "  uchar data;\n"
1391                "  uchar : 8;\n"
1392                "  uchar : 8;\n"
1393                "  uchar other;\n"
1394                "};");
1395 }
1396 
1397 TEST_F(FormatTest, FormatsNamespaces) {
1398   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1399   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1400 
1401   verifyFormat("namespace some_namespace {\n"
1402                "class A {};\n"
1403                "void f() { f(); }\n"
1404                "}",
1405                LLVMWithNoNamespaceFix);
1406   verifyFormat("namespace {\n"
1407                "class A {};\n"
1408                "void f() { f(); }\n"
1409                "}",
1410                LLVMWithNoNamespaceFix);
1411   verifyFormat("inline namespace X {\n"
1412                "class A {};\n"
1413                "void f() { f(); }\n"
1414                "}",
1415                LLVMWithNoNamespaceFix);
1416   verifyFormat("using namespace some_namespace;\n"
1417                "class A {};\n"
1418                "void f() { f(); }",
1419                LLVMWithNoNamespaceFix);
1420 
1421   // This code is more common than we thought; if we
1422   // layout this correctly the semicolon will go into
1423   // its own line, which is undesirable.
1424   verifyFormat("namespace {};",
1425                LLVMWithNoNamespaceFix);
1426   verifyFormat("namespace {\n"
1427                "class A {};\n"
1428                "};",
1429                LLVMWithNoNamespaceFix);
1430 
1431   verifyFormat("namespace {\n"
1432                "int SomeVariable = 0; // comment\n"
1433                "} // namespace",
1434                LLVMWithNoNamespaceFix);
1435   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1436             "#define HEADER_GUARD\n"
1437             "namespace my_namespace {\n"
1438             "int i;\n"
1439             "} // my_namespace\n"
1440             "#endif // HEADER_GUARD",
1441             format("#ifndef HEADER_GUARD\n"
1442                    " #define HEADER_GUARD\n"
1443                    "   namespace my_namespace {\n"
1444                    "int i;\n"
1445                    "}    // my_namespace\n"
1446                    "#endif    // HEADER_GUARD",
1447                    LLVMWithNoNamespaceFix));
1448 
1449   EXPECT_EQ("namespace A::B {\n"
1450             "class C {};\n"
1451             "}",
1452             format("namespace A::B {\n"
1453                    "class C {};\n"
1454                    "}",
1455                    LLVMWithNoNamespaceFix));
1456 
1457   FormatStyle Style = getLLVMStyle();
1458   Style.NamespaceIndentation = FormatStyle::NI_All;
1459   EXPECT_EQ("namespace out {\n"
1460             "  int i;\n"
1461             "  namespace in {\n"
1462             "    int i;\n"
1463             "  } // namespace in\n"
1464             "} // namespace out",
1465             format("namespace out {\n"
1466                    "int i;\n"
1467                    "namespace in {\n"
1468                    "int i;\n"
1469                    "} // namespace in\n"
1470                    "} // namespace out",
1471                    Style));
1472 
1473   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1474   EXPECT_EQ("namespace out {\n"
1475             "int i;\n"
1476             "namespace in {\n"
1477             "  int i;\n"
1478             "} // namespace in\n"
1479             "} // namespace out",
1480             format("namespace out {\n"
1481                    "int i;\n"
1482                    "namespace in {\n"
1483                    "int i;\n"
1484                    "} // namespace in\n"
1485                    "} // namespace out",
1486                    Style));
1487 }
1488 
1489 TEST_F(FormatTest, FormatsCompactNamespaces) {
1490   FormatStyle Style = getLLVMStyle();
1491   Style.CompactNamespaces = true;
1492 
1493   verifyFormat("namespace A { namespace B {\n"
1494 			   "}} // namespace A::B",
1495 			   Style);
1496 
1497   EXPECT_EQ("namespace out { namespace in {\n"
1498             "}} // namespace out::in",
1499             format("namespace out {\n"
1500                    "namespace in {\n"
1501                    "} // namespace in\n"
1502                    "} // namespace out",
1503                    Style));
1504 
1505   // Only namespaces which have both consecutive opening and end get compacted
1506   EXPECT_EQ("namespace out {\n"
1507             "namespace in1 {\n"
1508             "} // namespace in1\n"
1509             "namespace in2 {\n"
1510             "} // namespace in2\n"
1511             "} // namespace out",
1512             format("namespace out {\n"
1513                    "namespace in1 {\n"
1514                    "} // namespace in1\n"
1515                    "namespace in2 {\n"
1516                    "} // namespace in2\n"
1517                    "} // namespace out",
1518                    Style));
1519 
1520   EXPECT_EQ("namespace out {\n"
1521             "int i;\n"
1522             "namespace in {\n"
1523             "int j;\n"
1524             "} // namespace in\n"
1525             "int k;\n"
1526             "} // namespace out",
1527             format("namespace out { int i;\n"
1528                    "namespace in { int j; } // namespace in\n"
1529                    "int k; } // namespace out",
1530                    Style));
1531 
1532   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1533             "}}} // namespace A::B::C\n",
1534             format("namespace A { namespace B {\n"
1535                    "namespace C {\n"
1536                    "}} // namespace B::C\n"
1537                    "} // namespace A\n",
1538                    Style));
1539 
1540   Style.ColumnLimit = 40;
1541   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1542             "namespace bbbbbbbbbb {\n"
1543             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1544             format("namespace aaaaaaaaaa {\n"
1545                    "namespace bbbbbbbbbb {\n"
1546                    "} // namespace bbbbbbbbbb\n"
1547                    "} // namespace aaaaaaaaaa",
1548                    Style));
1549 
1550   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1551             "namespace cccccc {\n"
1552             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1553             format("namespace aaaaaa {\n"
1554                    "namespace bbbbbb {\n"
1555                    "namespace cccccc {\n"
1556                    "} // namespace cccccc\n"
1557                    "} // namespace bbbbbb\n"
1558                    "} // namespace aaaaaa",
1559                    Style));
1560   Style.ColumnLimit = 80;
1561 
1562   // Extra semicolon after 'inner' closing brace prevents merging
1563   EXPECT_EQ("namespace out { namespace in {\n"
1564             "}; } // namespace out::in",
1565             format("namespace out {\n"
1566                    "namespace in {\n"
1567                    "}; // namespace in\n"
1568                    "} // namespace out",
1569                    Style));
1570 
1571   // Extra semicolon after 'outer' closing brace is conserved
1572   EXPECT_EQ("namespace out { namespace in {\n"
1573             "}}; // namespace out::in",
1574             format("namespace out {\n"
1575                    "namespace in {\n"
1576                    "} // namespace in\n"
1577                    "}; // namespace out",
1578                    Style));
1579 
1580   Style.NamespaceIndentation = FormatStyle::NI_All;
1581   EXPECT_EQ("namespace out { namespace in {\n"
1582             "  int i;\n"
1583             "}} // namespace out::in",
1584             format("namespace out {\n"
1585                    "namespace in {\n"
1586                    "int i;\n"
1587                    "} // namespace in\n"
1588                    "} // namespace out",
1589                    Style));
1590   EXPECT_EQ("namespace out { namespace mid {\n"
1591             "  namespace in {\n"
1592             "    int j;\n"
1593             "  } // namespace in\n"
1594             "  int k;\n"
1595             "}} // namespace out::mid",
1596             format("namespace out { namespace mid {\n"
1597                    "namespace in { int j; } // namespace in\n"
1598                    "int k; }} // namespace out::mid",
1599                    Style));
1600 
1601   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1602   EXPECT_EQ("namespace out { namespace in {\n"
1603             "  int i;\n"
1604             "}} // namespace out::in",
1605             format("namespace out {\n"
1606                    "namespace in {\n"
1607                    "int i;\n"
1608                    "} // namespace in\n"
1609                    "} // namespace out",
1610                    Style));
1611   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1612             "  int i;\n"
1613             "}}} // namespace out::mid::in",
1614             format("namespace out {\n"
1615                    "namespace mid {\n"
1616                    "namespace in {\n"
1617                    "int i;\n"
1618                    "} // namespace in\n"
1619                    "} // namespace mid\n"
1620                    "} // namespace out",
1621                    Style));
1622 }
1623 
1624 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1625 
1626 TEST_F(FormatTest, FormatsInlineASM) {
1627   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1628   verifyFormat("asm(\"nop\" ::: \"memory\");");
1629   verifyFormat(
1630       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1631       "    \"cpuid\\n\\t\"\n"
1632       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1633       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1634       "    : \"a\"(value));");
1635   EXPECT_EQ(
1636       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1637       "  __asm {\n"
1638       "        mov     edx,[that] // vtable in edx\n"
1639       "        mov     eax,methodIndex\n"
1640       "        call    [edx][eax*4] // stdcall\n"
1641       "  }\n"
1642       "}",
1643       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1644              "    __asm {\n"
1645              "        mov     edx,[that] // vtable in edx\n"
1646              "        mov     eax,methodIndex\n"
1647              "        call    [edx][eax*4] // stdcall\n"
1648              "    }\n"
1649              "}"));
1650   EXPECT_EQ("_asm {\n"
1651             "  xor eax, eax;\n"
1652             "  cpuid;\n"
1653             "}",
1654             format("_asm {\n"
1655                    "  xor eax, eax;\n"
1656                    "  cpuid;\n"
1657                    "}"));
1658   verifyFormat("void function() {\n"
1659                "  // comment\n"
1660                "  asm(\"\");\n"
1661                "}");
1662   EXPECT_EQ("__asm {\n"
1663             "}\n"
1664             "int i;",
1665             format("__asm   {\n"
1666                    "}\n"
1667                    "int   i;"));
1668 }
1669 
1670 TEST_F(FormatTest, FormatTryCatch) {
1671   verifyFormat("try {\n"
1672                "  throw a * b;\n"
1673                "} catch (int a) {\n"
1674                "  // Do nothing.\n"
1675                "} catch (...) {\n"
1676                "  exit(42);\n"
1677                "}");
1678 
1679   // Function-level try statements.
1680   verifyFormat("int f() try { return 4; } catch (...) {\n"
1681                "  return 5;\n"
1682                "}");
1683   verifyFormat("class A {\n"
1684                "  int a;\n"
1685                "  A() try : a(0) {\n"
1686                "  } catch (...) {\n"
1687                "    throw;\n"
1688                "  }\n"
1689                "};\n");
1690 
1691   // Incomplete try-catch blocks.
1692   verifyIncompleteFormat("try {} catch (");
1693 }
1694 
1695 TEST_F(FormatTest, FormatSEHTryCatch) {
1696   verifyFormat("__try {\n"
1697                "  int a = b * c;\n"
1698                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1699                "  // Do nothing.\n"
1700                "}");
1701 
1702   verifyFormat("__try {\n"
1703                "  int a = b * c;\n"
1704                "} __finally {\n"
1705                "  // Do nothing.\n"
1706                "}");
1707 
1708   verifyFormat("DEBUG({\n"
1709                "  __try {\n"
1710                "  } __finally {\n"
1711                "  }\n"
1712                "});\n");
1713 }
1714 
1715 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1716   verifyFormat("try {\n"
1717                "  f();\n"
1718                "} catch {\n"
1719                "  g();\n"
1720                "}");
1721   verifyFormat("try {\n"
1722                "  f();\n"
1723                "} catch (A a) MACRO(x) {\n"
1724                "  g();\n"
1725                "} catch (B b) MACRO(x) {\n"
1726                "  g();\n"
1727                "}");
1728 }
1729 
1730 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1731   FormatStyle Style = getLLVMStyle();
1732   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1733                           FormatStyle::BS_WebKit}) {
1734     Style.BreakBeforeBraces = BraceStyle;
1735     verifyFormat("try {\n"
1736                  "  // something\n"
1737                  "} catch (...) {\n"
1738                  "  // something\n"
1739                  "}",
1740                  Style);
1741   }
1742   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1743   verifyFormat("try {\n"
1744                "  // something\n"
1745                "}\n"
1746                "catch (...) {\n"
1747                "  // something\n"
1748                "}",
1749                Style);
1750   verifyFormat("__try {\n"
1751                "  // something\n"
1752                "}\n"
1753                "__finally {\n"
1754                "  // something\n"
1755                "}",
1756                Style);
1757   verifyFormat("@try {\n"
1758                "  // something\n"
1759                "}\n"
1760                "@finally {\n"
1761                "  // something\n"
1762                "}",
1763                Style);
1764   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1765   verifyFormat("try\n"
1766                "{\n"
1767                "  // something\n"
1768                "}\n"
1769                "catch (...)\n"
1770                "{\n"
1771                "  // something\n"
1772                "}",
1773                Style);
1774   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1775   verifyFormat("try\n"
1776                "  {\n"
1777                "    // something\n"
1778                "  }\n"
1779                "catch (...)\n"
1780                "  {\n"
1781                "    // something\n"
1782                "  }",
1783                Style);
1784   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1785   Style.BraceWrapping.BeforeCatch = true;
1786   verifyFormat("try {\n"
1787                "  // something\n"
1788                "}\n"
1789                "catch (...) {\n"
1790                "  // something\n"
1791                "}",
1792                Style);
1793 }
1794 
1795 TEST_F(FormatTest, StaticInitializers) {
1796   verifyFormat("static SomeClass SC = {1, 'a'};");
1797 
1798   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1799                "    100000000, "
1800                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1801 
1802   // Here, everything other than the "}" would fit on a line.
1803   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1804                "    10000000000000000000000000};");
1805   EXPECT_EQ("S s = {a,\n"
1806             "\n"
1807             "       b};",
1808             format("S s = {\n"
1809                    "  a,\n"
1810                    "\n"
1811                    "  b\n"
1812                    "};"));
1813 
1814   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1815   // line. However, the formatting looks a bit off and this probably doesn't
1816   // happen often in practice.
1817   verifyFormat("static int Variable[1] = {\n"
1818                "    {1000000000000000000000000000000000000}};",
1819                getLLVMStyleWithColumns(40));
1820 }
1821 
1822 TEST_F(FormatTest, DesignatedInitializers) {
1823   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1824   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1825                "                    .bbbbbbbbbb = 2,\n"
1826                "                    .cccccccccc = 3,\n"
1827                "                    .dddddddddd = 4,\n"
1828                "                    .eeeeeeeeee = 5};");
1829   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1830                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1831                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1832                "    .ccccccccccccccccccccccccccc = 3,\n"
1833                "    .ddddddddddddddddddddddddddd = 4,\n"
1834                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1835 
1836   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1837 
1838   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1839   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1840                "                    [2] = bbbbbbbbbb,\n"
1841                "                    [3] = cccccccccc,\n"
1842                "                    [4] = dddddddddd,\n"
1843                "                    [5] = eeeeeeeeee};");
1844   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1845                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1846                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1847                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1848                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1849                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1850 }
1851 
1852 TEST_F(FormatTest, NestedStaticInitializers) {
1853   verifyFormat("static A x = {{{}}};\n");
1854   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1855                "               {init1, init2, init3, init4}}};",
1856                getLLVMStyleWithColumns(50));
1857 
1858   verifyFormat("somes Status::global_reps[3] = {\n"
1859                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1860                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1861                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1862                getLLVMStyleWithColumns(60));
1863   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1864                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1865                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1866                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1867   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1868                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1869                "rect.fTop}};");
1870 
1871   verifyFormat(
1872       "SomeArrayOfSomeType a = {\n"
1873       "    {{1, 2, 3},\n"
1874       "     {1, 2, 3},\n"
1875       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1876       "      333333333333333333333333333333},\n"
1877       "     {1, 2, 3},\n"
1878       "     {1, 2, 3}}};");
1879   verifyFormat(
1880       "SomeArrayOfSomeType a = {\n"
1881       "    {{1, 2, 3}},\n"
1882       "    {{1, 2, 3}},\n"
1883       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1884       "      333333333333333333333333333333}},\n"
1885       "    {{1, 2, 3}},\n"
1886       "    {{1, 2, 3}}};");
1887 
1888   verifyFormat("struct {\n"
1889                "  unsigned bit;\n"
1890                "  const char *const name;\n"
1891                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1892                "                 {kOsWin, \"Windows\"},\n"
1893                "                 {kOsLinux, \"Linux\"},\n"
1894                "                 {kOsCrOS, \"Chrome OS\"}};");
1895   verifyFormat("struct {\n"
1896                "  unsigned bit;\n"
1897                "  const char *const name;\n"
1898                "} kBitsToOs[] = {\n"
1899                "    {kOsMac, \"Mac\"},\n"
1900                "    {kOsWin, \"Windows\"},\n"
1901                "    {kOsLinux, \"Linux\"},\n"
1902                "    {kOsCrOS, \"Chrome OS\"},\n"
1903                "};");
1904 }
1905 
1906 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1907   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1908                "                      \\\n"
1909                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1910 }
1911 
1912 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1913   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1914                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1915 
1916   // Do break defaulted and deleted functions.
1917   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1918                "    default;",
1919                getLLVMStyleWithColumns(40));
1920   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1921                "    delete;",
1922                getLLVMStyleWithColumns(40));
1923 }
1924 
1925 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1926   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1927                getLLVMStyleWithColumns(40));
1928   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1929                getLLVMStyleWithColumns(40));
1930   EXPECT_EQ("#define Q                              \\\n"
1931             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1932             "  \"aaaaaaaa.cpp\"",
1933             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1934                    getLLVMStyleWithColumns(40)));
1935 }
1936 
1937 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1938   EXPECT_EQ("# 123 \"A string literal\"",
1939             format("   #     123    \"A string literal\""));
1940 }
1941 
1942 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1943   EXPECT_EQ("#;", format("#;"));
1944   verifyFormat("#\n;\n;\n;");
1945 }
1946 
1947 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1948   EXPECT_EQ("#line 42 \"test\"\n",
1949             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1950   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1951                                     getLLVMStyleWithColumns(12)));
1952 }
1953 
1954 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1955   EXPECT_EQ("#line 42 \"test\"",
1956             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1957   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1958 }
1959 
1960 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1961   verifyFormat("#define A \\x20");
1962   verifyFormat("#define A \\ x20");
1963   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1964   verifyFormat("#define A ''");
1965   verifyFormat("#define A ''qqq");
1966   verifyFormat("#define A `qqq");
1967   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1968   EXPECT_EQ("const char *c = STRINGIFY(\n"
1969             "\\na : b);",
1970             format("const char * c = STRINGIFY(\n"
1971                    "\\na : b);"));
1972 
1973   verifyFormat("a\r\\");
1974   verifyFormat("a\v\\");
1975   verifyFormat("a\f\\");
1976 }
1977 
1978 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1979   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1980   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1981   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1982   // FIXME: We never break before the macro name.
1983   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1984 
1985   verifyFormat("#define A A\n#define A A");
1986   verifyFormat("#define A(X) A\n#define A A");
1987 
1988   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1989   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1990 }
1991 
1992 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1993   EXPECT_EQ("// somecomment\n"
1994             "#include \"a.h\"\n"
1995             "#define A(  \\\n"
1996             "    A, B)\n"
1997             "#include \"b.h\"\n"
1998             "// somecomment\n",
1999             format("  // somecomment\n"
2000                    "  #include \"a.h\"\n"
2001                    "#define A(A,\\\n"
2002                    "    B)\n"
2003                    "    #include \"b.h\"\n"
2004                    " // somecomment\n",
2005                    getLLVMStyleWithColumns(13)));
2006 }
2007 
2008 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2009 
2010 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2011   EXPECT_EQ("#define A    \\\n"
2012             "  c;         \\\n"
2013             "  e;\n"
2014             "f;",
2015             format("#define A c; e;\n"
2016                    "f;",
2017                    getLLVMStyleWithColumns(14)));
2018 }
2019 
2020 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2021 
2022 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2023   EXPECT_EQ("int x,\n"
2024             "#define A\n"
2025             "    y;",
2026             format("int x,\n#define A\ny;"));
2027 }
2028 
2029 TEST_F(FormatTest, HashInMacroDefinition) {
2030   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2031   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2032   verifyFormat("#define A  \\\n"
2033                "  {        \\\n"
2034                "    f(#c); \\\n"
2035                "  }",
2036                getLLVMStyleWithColumns(11));
2037 
2038   verifyFormat("#define A(X)         \\\n"
2039                "  void function##X()",
2040                getLLVMStyleWithColumns(22));
2041 
2042   verifyFormat("#define A(a, b, c)   \\\n"
2043                "  void a##b##c()",
2044                getLLVMStyleWithColumns(22));
2045 
2046   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2047 }
2048 
2049 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2050   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2051   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2052 }
2053 
2054 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2055   EXPECT_EQ("#define A b;", format("#define A \\\n"
2056                                    "          \\\n"
2057                                    "  b;",
2058                                    getLLVMStyleWithColumns(25)));
2059   EXPECT_EQ("#define A \\\n"
2060             "          \\\n"
2061             "  a;      \\\n"
2062             "  b;",
2063             format("#define A \\\n"
2064                    "          \\\n"
2065                    "  a;      \\\n"
2066                    "  b;",
2067                    getLLVMStyleWithColumns(11)));
2068   EXPECT_EQ("#define A \\\n"
2069             "  a;      \\\n"
2070             "          \\\n"
2071             "  b;",
2072             format("#define A \\\n"
2073                    "  a;      \\\n"
2074                    "          \\\n"
2075                    "  b;",
2076                    getLLVMStyleWithColumns(11)));
2077 }
2078 
2079 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2080   verifyIncompleteFormat("#define A :");
2081   verifyFormat("#define SOMECASES  \\\n"
2082                "  case 1:          \\\n"
2083                "  case 2\n",
2084                getLLVMStyleWithColumns(20));
2085   verifyFormat("#define MACRO(a) \\\n"
2086                "  if (a)         \\\n"
2087                "    f();         \\\n"
2088                "  else           \\\n"
2089                "    g()",
2090                getLLVMStyleWithColumns(18));
2091   verifyFormat("#define A template <typename T>");
2092   verifyIncompleteFormat("#define STR(x) #x\n"
2093                          "f(STR(this_is_a_string_literal{));");
2094   verifyFormat("#pragma omp threadprivate( \\\n"
2095                "    y)), // expected-warning",
2096                getLLVMStyleWithColumns(28));
2097   verifyFormat("#d, = };");
2098   verifyFormat("#if \"a");
2099   verifyIncompleteFormat("({\n"
2100                          "#define b     \\\n"
2101                          "  }           \\\n"
2102                          "  a\n"
2103                          "a",
2104                          getLLVMStyleWithColumns(15));
2105   verifyFormat("#define A     \\\n"
2106                "  {           \\\n"
2107                "    {\n"
2108                "#define B     \\\n"
2109                "  }           \\\n"
2110                "  }",
2111                getLLVMStyleWithColumns(15));
2112   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2113   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2114   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2115   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2116 }
2117 
2118 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2119   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2120   EXPECT_EQ("class A : public QObject {\n"
2121             "  Q_OBJECT\n"
2122             "\n"
2123             "  A() {}\n"
2124             "};",
2125             format("class A  :  public QObject {\n"
2126                    "     Q_OBJECT\n"
2127                    "\n"
2128                    "  A() {\n}\n"
2129                    "}  ;"));
2130   EXPECT_EQ("MACRO\n"
2131             "/*static*/ int i;",
2132             format("MACRO\n"
2133                    " /*static*/ int   i;"));
2134   EXPECT_EQ("SOME_MACRO\n"
2135             "namespace {\n"
2136             "void f();\n"
2137             "} // namespace",
2138             format("SOME_MACRO\n"
2139                    "  namespace    {\n"
2140                    "void   f(  );\n"
2141                    "} // namespace"));
2142   // Only if the identifier contains at least 5 characters.
2143   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2144   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2145   // Only if everything is upper case.
2146   EXPECT_EQ("class A : public QObject {\n"
2147             "  Q_Object A() {}\n"
2148             "};",
2149             format("class A  :  public QObject {\n"
2150                    "     Q_Object\n"
2151                    "  A() {\n}\n"
2152                    "}  ;"));
2153 
2154   // Only if the next line can actually start an unwrapped line.
2155   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2156             format("SOME_WEIRD_LOG_MACRO\n"
2157                    "<< SomeThing;"));
2158 
2159   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2160                "(n, buffers))\n",
2161                getChromiumStyle(FormatStyle::LK_Cpp));
2162 }
2163 
2164 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2165   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2166             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2167             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2168             "class X {};\n"
2169             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2170             "int *createScopDetectionPass() { return 0; }",
2171             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2172                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2173                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2174                    "  class X {};\n"
2175                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2176                    "  int *createScopDetectionPass() { return 0; }"));
2177   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2178   // braces, so that inner block is indented one level more.
2179   EXPECT_EQ("int q() {\n"
2180             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2181             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2182             "  IPC_END_MESSAGE_MAP()\n"
2183             "}",
2184             format("int q() {\n"
2185                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2186                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2187                    "  IPC_END_MESSAGE_MAP()\n"
2188                    "}"));
2189 
2190   // Same inside macros.
2191   EXPECT_EQ("#define LIST(L) \\\n"
2192             "  L(A)          \\\n"
2193             "  L(B)          \\\n"
2194             "  L(C)",
2195             format("#define LIST(L) \\\n"
2196                    "  L(A) \\\n"
2197                    "  L(B) \\\n"
2198                    "  L(C)",
2199                    getGoogleStyle()));
2200 
2201   // These must not be recognized as macros.
2202   EXPECT_EQ("int q() {\n"
2203             "  f(x);\n"
2204             "  f(x) {}\n"
2205             "  f(x)->g();\n"
2206             "  f(x)->*g();\n"
2207             "  f(x).g();\n"
2208             "  f(x) = x;\n"
2209             "  f(x) += x;\n"
2210             "  f(x) -= x;\n"
2211             "  f(x) *= x;\n"
2212             "  f(x) /= x;\n"
2213             "  f(x) %= x;\n"
2214             "  f(x) &= x;\n"
2215             "  f(x) |= x;\n"
2216             "  f(x) ^= x;\n"
2217             "  f(x) >>= x;\n"
2218             "  f(x) <<= x;\n"
2219             "  f(x)[y].z();\n"
2220             "  LOG(INFO) << x;\n"
2221             "  ifstream(x) >> x;\n"
2222             "}\n",
2223             format("int q() {\n"
2224                    "  f(x)\n;\n"
2225                    "  f(x)\n {}\n"
2226                    "  f(x)\n->g();\n"
2227                    "  f(x)\n->*g();\n"
2228                    "  f(x)\n.g();\n"
2229                    "  f(x)\n = x;\n"
2230                    "  f(x)\n += x;\n"
2231                    "  f(x)\n -= x;\n"
2232                    "  f(x)\n *= x;\n"
2233                    "  f(x)\n /= x;\n"
2234                    "  f(x)\n %= x;\n"
2235                    "  f(x)\n &= x;\n"
2236                    "  f(x)\n |= x;\n"
2237                    "  f(x)\n ^= x;\n"
2238                    "  f(x)\n >>= x;\n"
2239                    "  f(x)\n <<= x;\n"
2240                    "  f(x)\n[y].z();\n"
2241                    "  LOG(INFO)\n << x;\n"
2242                    "  ifstream(x)\n >> x;\n"
2243                    "}\n"));
2244   EXPECT_EQ("int q() {\n"
2245             "  F(x)\n"
2246             "  if (1) {\n"
2247             "  }\n"
2248             "  F(x)\n"
2249             "  while (1) {\n"
2250             "  }\n"
2251             "  F(x)\n"
2252             "  G(x);\n"
2253             "  F(x)\n"
2254             "  try {\n"
2255             "    Q();\n"
2256             "  } catch (...) {\n"
2257             "  }\n"
2258             "}\n",
2259             format("int q() {\n"
2260                    "F(x)\n"
2261                    "if (1) {}\n"
2262                    "F(x)\n"
2263                    "while (1) {}\n"
2264                    "F(x)\n"
2265                    "G(x);\n"
2266                    "F(x)\n"
2267                    "try { Q(); } catch (...) {}\n"
2268                    "}\n"));
2269   EXPECT_EQ("class A {\n"
2270             "  A() : t(0) {}\n"
2271             "  A(int i) noexcept() : {}\n"
2272             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2273             "  try : t(0) {\n"
2274             "  } catch (...) {\n"
2275             "  }\n"
2276             "};",
2277             format("class A {\n"
2278                    "  A()\n : t(0) {}\n"
2279                    "  A(int i)\n noexcept() : {}\n"
2280                    "  A(X x)\n"
2281                    "  try : t(0) {} catch (...) {}\n"
2282                    "};"));
2283   EXPECT_EQ("class SomeClass {\n"
2284             "public:\n"
2285             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2286             "};",
2287             format("class SomeClass {\n"
2288                    "public:\n"
2289                    "  SomeClass()\n"
2290                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2291                    "};"));
2292   EXPECT_EQ("class SomeClass {\n"
2293             "public:\n"
2294             "  SomeClass()\n"
2295             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2296             "};",
2297             format("class SomeClass {\n"
2298                    "public:\n"
2299                    "  SomeClass()\n"
2300                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2301                    "};",
2302                    getLLVMStyleWithColumns(40)));
2303 
2304   verifyFormat("MACRO(>)");
2305 }
2306 
2307 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2308   verifyFormat("#define A \\\n"
2309                "  f({     \\\n"
2310                "    g();  \\\n"
2311                "  });",
2312                getLLVMStyleWithColumns(11));
2313 }
2314 
2315 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2316   FormatStyle Style = getLLVMStyle();
2317   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2318   Style.ColumnLimit = 40;
2319   verifyFormat("#ifdef _WIN32\n"
2320                "#define A 0\n"
2321                "#ifdef VAR2\n"
2322                "#define B 1\n"
2323                "#include <someheader.h>\n"
2324                "#define MACRO                          \\\n"
2325                "  some_very_long_func_aaaaaaaaaa();\n"
2326                "#endif\n"
2327                "#else\n"
2328                "#define A 1\n"
2329                "#endif",
2330                Style);
2331 
2332   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2333   verifyFormat("#ifdef _WIN32\n"
2334                "#  define A 0\n"
2335                "#  ifdef VAR2\n"
2336                "#    define B 1\n"
2337                "#    include <someheader.h>\n"
2338                "#    define MACRO                      \\\n"
2339                "      some_very_long_func_aaaaaaaaaa();\n"
2340                "#  endif\n"
2341                "#else\n"
2342                "#  define A 1\n"
2343                "#endif",
2344                Style);
2345   verifyFormat("#if A\n"
2346                "#  define MACRO                        \\\n"
2347                "    void a(int x) {                    \\\n"
2348                "      b();                             \\\n"
2349                "      c();                             \\\n"
2350                "      d();                             \\\n"
2351                "      e();                             \\\n"
2352                "      f();                             \\\n"
2353                "    }\n"
2354                "#endif",
2355                Style);
2356   // Comments before include guard.
2357   verifyFormat("// file comment\n"
2358                "// file comment\n"
2359                "#ifndef HEADER_H\n"
2360                "#define HEADER_H\n"
2361                "code();\n"
2362                "#endif",
2363                Style);
2364   // Test with include guards.
2365   // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly
2366   // merges lines.
2367   verifyFormat("#ifndef HEADER_H\n"
2368                "#define HEADER_H\n"
2369                "code();\n"
2370                "#endif",
2371                Style);
2372   // Include guards must have a #define with the same variable immediately
2373   // after #ifndef.
2374   verifyFormat("#ifndef NOT_GUARD\n"
2375                "#  define FOO\n"
2376                "code();\n"
2377                "#endif",
2378                Style);
2379 
2380   // Include guards must cover the entire file.
2381   verifyFormat("code();\n"
2382                "code();\n"
2383                "#ifndef NOT_GUARD\n"
2384                "#  define NOT_GUARD\n"
2385                "code();\n"
2386                "#endif",
2387                Style);
2388   verifyFormat("#ifndef NOT_GUARD\n"
2389                "#  define NOT_GUARD\n"
2390                "code();\n"
2391                "#endif\n"
2392                "code();",
2393                Style);
2394   // Test with trailing blank lines.
2395   verifyFormat("#ifndef HEADER_H\n"
2396                "#define HEADER_H\n"
2397                "code();\n"
2398                "#endif\n",
2399                Style);
2400   // Include guards don't have #else.
2401   verifyFormat("#ifndef NOT_GUARD\n"
2402                "#  define NOT_GUARD\n"
2403                "code();\n"
2404                "#else\n"
2405                "#endif",
2406                Style);
2407   verifyFormat("#ifndef NOT_GUARD\n"
2408                "#  define NOT_GUARD\n"
2409                "code();\n"
2410                "#elif FOO\n"
2411                "#endif",
2412                Style);
2413   // FIXME: This doesn't handle the case where there's code between the
2414   // #ifndef and #define but all other conditions hold. This is because when
2415   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2416   // previous code line yet, so we can't detect it.
2417   EXPECT_EQ("#ifndef NOT_GUARD\n"
2418             "code();\n"
2419             "#define NOT_GUARD\n"
2420             "code();\n"
2421             "#endif",
2422             format("#ifndef NOT_GUARD\n"
2423                    "code();\n"
2424                    "#  define NOT_GUARD\n"
2425                    "code();\n"
2426                    "#endif",
2427                    Style));
2428   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2429   // be outside an include guard. Examples are #pragma once and
2430   // #pragma GCC diagnostic, or anything else that does not change the meaning
2431   // of the file if it's included multiple times.
2432   EXPECT_EQ("#ifdef WIN32\n"
2433             "#  pragma once\n"
2434             "#endif\n"
2435             "#ifndef HEADER_H\n"
2436             "#  define HEADER_H\n"
2437             "code();\n"
2438             "#endif",
2439             format("#ifdef WIN32\n"
2440                    "#  pragma once\n"
2441                    "#endif\n"
2442                    "#ifndef HEADER_H\n"
2443                    "#define HEADER_H\n"
2444                    "code();\n"
2445                    "#endif",
2446                    Style));
2447   // FIXME: This does not detect when there is a single non-preprocessor line
2448   // in front of an include-guard-like structure where other conditions hold
2449   // because ScopedLineState hides the line.
2450   EXPECT_EQ("code();\n"
2451             "#ifndef HEADER_H\n"
2452             "#define HEADER_H\n"
2453             "code();\n"
2454             "#endif",
2455             format("code();\n"
2456                    "#ifndef HEADER_H\n"
2457                    "#  define HEADER_H\n"
2458                    "code();\n"
2459                    "#endif",
2460                    Style));
2461   // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by
2462   // preprocessor indentation.
2463   EXPECT_EQ("#if 1\n"
2464             "  // comment\n"
2465             "#  define A 0\n"
2466             "// comment\n"
2467             "#  define B 0\n"
2468             "#endif",
2469             format("#if 1\n"
2470                    "// comment\n"
2471                    "#  define A 0\n"
2472                    "   // comment\n"
2473                    "#  define B 0\n"
2474                    "#endif",
2475                    Style));
2476   // Test with tabs.
2477   Style.UseTab = FormatStyle::UT_Always;
2478   Style.IndentWidth = 8;
2479   Style.TabWidth = 8;
2480   verifyFormat("#ifdef _WIN32\n"
2481                "#\tdefine A 0\n"
2482                "#\tifdef VAR2\n"
2483                "#\t\tdefine B 1\n"
2484                "#\t\tinclude <someheader.h>\n"
2485                "#\t\tdefine MACRO          \\\n"
2486                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2487                "#\tendif\n"
2488                "#else\n"
2489                "#\tdefine A 1\n"
2490                "#endif",
2491                Style);
2492 }
2493 
2494 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2495   verifyFormat("{\n  { a #c; }\n}");
2496 }
2497 
2498 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2499   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2500             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2501   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2502             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2503 }
2504 
2505 TEST_F(FormatTest, EscapedNewlines) {
2506   EXPECT_EQ(
2507       "#define A \\\n  int i;  \\\n  int j;",
2508       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2509   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2510   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2511   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2512   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2513 
2514   FormatStyle DontAlign = getLLVMStyle();
2515   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2516   DontAlign.MaxEmptyLinesToKeep = 3;
2517   // FIXME: can't use verifyFormat here because the newline before
2518   // "public:" is not inserted the first time it's reformatted
2519   EXPECT_EQ("#define A \\\n"
2520             "  class Foo { \\\n"
2521             "    void bar(); \\\n"
2522             "\\\n"
2523             "\\\n"
2524             "\\\n"
2525             "  public: \\\n"
2526             "    void baz(); \\\n"
2527             "  };",
2528             format("#define A \\\n"
2529                    "  class Foo { \\\n"
2530                    "    void bar(); \\\n"
2531                    "\\\n"
2532                    "\\\n"
2533                    "\\\n"
2534                    "  public: \\\n"
2535                    "    void baz(); \\\n"
2536                    "  };", DontAlign));
2537 }
2538 
2539 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2540   verifyFormat("#define A \\\n"
2541                "  int v(  \\\n"
2542                "      a); \\\n"
2543                "  int i;",
2544                getLLVMStyleWithColumns(11));
2545 }
2546 
2547 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2548   EXPECT_EQ(
2549       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2550       "                      \\\n"
2551       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2552       "\n"
2553       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2554       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2555       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2556              "\\\n"
2557              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2558              "  \n"
2559              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2560              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2561 }
2562 
2563 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2564   EXPECT_EQ("int\n"
2565             "#define A\n"
2566             "    a;",
2567             format("int\n#define A\na;"));
2568   verifyFormat("functionCallTo(\n"
2569                "    someOtherFunction(\n"
2570                "        withSomeParameters, whichInSequence,\n"
2571                "        areLongerThanALine(andAnotherCall,\n"
2572                "#define A B\n"
2573                "                           withMoreParamters,\n"
2574                "                           whichStronglyInfluenceTheLayout),\n"
2575                "        andMoreParameters),\n"
2576                "    trailing);",
2577                getLLVMStyleWithColumns(69));
2578   verifyFormat("Foo::Foo()\n"
2579                "#ifdef BAR\n"
2580                "    : baz(0)\n"
2581                "#endif\n"
2582                "{\n"
2583                "}");
2584   verifyFormat("void f() {\n"
2585                "  if (true)\n"
2586                "#ifdef A\n"
2587                "    f(42);\n"
2588                "  x();\n"
2589                "#else\n"
2590                "    g();\n"
2591                "  x();\n"
2592                "#endif\n"
2593                "}");
2594   verifyFormat("void f(param1, param2,\n"
2595                "       param3,\n"
2596                "#ifdef A\n"
2597                "       param4(param5,\n"
2598                "#ifdef A1\n"
2599                "              param6,\n"
2600                "#ifdef A2\n"
2601                "              param7),\n"
2602                "#else\n"
2603                "              param8),\n"
2604                "       param9,\n"
2605                "#endif\n"
2606                "       param10,\n"
2607                "#endif\n"
2608                "       param11)\n"
2609                "#else\n"
2610                "       param12)\n"
2611                "#endif\n"
2612                "{\n"
2613                "  x();\n"
2614                "}",
2615                getLLVMStyleWithColumns(28));
2616   verifyFormat("#if 1\n"
2617                "int i;");
2618   verifyFormat("#if 1\n"
2619                "#endif\n"
2620                "#if 1\n"
2621                "#else\n"
2622                "#endif\n");
2623   verifyFormat("DEBUG({\n"
2624                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2625                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2626                "});\n"
2627                "#if a\n"
2628                "#else\n"
2629                "#endif");
2630 
2631   verifyIncompleteFormat("void f(\n"
2632                          "#if A\n"
2633                          ");\n"
2634                          "#else\n"
2635                          "#endif");
2636 }
2637 
2638 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2639   verifyFormat("#endif\n"
2640                "#if B");
2641 }
2642 
2643 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2644   FormatStyle SingleLine = getLLVMStyle();
2645   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2646   verifyFormat("#if 0\n"
2647                "#elif 1\n"
2648                "#endif\n"
2649                "void foo() {\n"
2650                "  if (test) foo2();\n"
2651                "}",
2652                SingleLine);
2653 }
2654 
2655 TEST_F(FormatTest, LayoutBlockInsideParens) {
2656   verifyFormat("functionCall({ int i; });");
2657   verifyFormat("functionCall({\n"
2658                "  int i;\n"
2659                "  int j;\n"
2660                "});");
2661   verifyFormat("functionCall(\n"
2662                "    {\n"
2663                "      int i;\n"
2664                "      int j;\n"
2665                "    },\n"
2666                "    aaaa, bbbb, cccc);");
2667   verifyFormat("functionA(functionB({\n"
2668                "            int i;\n"
2669                "            int j;\n"
2670                "          }),\n"
2671                "          aaaa, bbbb, cccc);");
2672   verifyFormat("functionCall(\n"
2673                "    {\n"
2674                "      int i;\n"
2675                "      int j;\n"
2676                "    },\n"
2677                "    aaaa, bbbb, // comment\n"
2678                "    cccc);");
2679   verifyFormat("functionA(functionB({\n"
2680                "            int i;\n"
2681                "            int j;\n"
2682                "          }),\n"
2683                "          aaaa, bbbb, // comment\n"
2684                "          cccc);");
2685   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2686   verifyFormat("functionCall(aaaa, bbbb, {\n"
2687                "  int i;\n"
2688                "  int j;\n"
2689                "});");
2690   verifyFormat(
2691       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2692       "    {\n"
2693       "      int i; // break\n"
2694       "    },\n"
2695       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2696       "                                     ccccccccccccccccc));");
2697   verifyFormat("DEBUG({\n"
2698                "  if (a)\n"
2699                "    f();\n"
2700                "});");
2701 }
2702 
2703 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2704   EXPECT_EQ("SOME_MACRO { int i; }\n"
2705             "int i;",
2706             format("  SOME_MACRO  {int i;}  int i;"));
2707 }
2708 
2709 TEST_F(FormatTest, LayoutNestedBlocks) {
2710   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2711                "  struct s {\n"
2712                "    int i;\n"
2713                "  };\n"
2714                "  s kBitsToOs[] = {{10}};\n"
2715                "  for (int i = 0; i < 10; ++i)\n"
2716                "    return;\n"
2717                "}");
2718   verifyFormat("call(parameter, {\n"
2719                "  something();\n"
2720                "  // Comment using all columns.\n"
2721                "  somethingelse();\n"
2722                "});",
2723                getLLVMStyleWithColumns(40));
2724   verifyFormat("DEBUG( //\n"
2725                "    { f(); }, a);");
2726   verifyFormat("DEBUG( //\n"
2727                "    {\n"
2728                "      f(); //\n"
2729                "    },\n"
2730                "    a);");
2731 
2732   EXPECT_EQ("call(parameter, {\n"
2733             "  something();\n"
2734             "  // Comment too\n"
2735             "  // looooooooooong.\n"
2736             "  somethingElse();\n"
2737             "});",
2738             format("call(parameter, {\n"
2739                    "  something();\n"
2740                    "  // Comment too looooooooooong.\n"
2741                    "  somethingElse();\n"
2742                    "});",
2743                    getLLVMStyleWithColumns(29)));
2744   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2745   EXPECT_EQ("DEBUG({ // comment\n"
2746             "  int i;\n"
2747             "});",
2748             format("DEBUG({ // comment\n"
2749                    "int  i;\n"
2750                    "});"));
2751   EXPECT_EQ("DEBUG({\n"
2752             "  int i;\n"
2753             "\n"
2754             "  // comment\n"
2755             "  int j;\n"
2756             "});",
2757             format("DEBUG({\n"
2758                    "  int  i;\n"
2759                    "\n"
2760                    "  // comment\n"
2761                    "  int  j;\n"
2762                    "});"));
2763 
2764   verifyFormat("DEBUG({\n"
2765                "  if (a)\n"
2766                "    return;\n"
2767                "});");
2768   verifyGoogleFormat("DEBUG({\n"
2769                      "  if (a) return;\n"
2770                      "});");
2771   FormatStyle Style = getGoogleStyle();
2772   Style.ColumnLimit = 45;
2773   verifyFormat("Debug(aaaaa,\n"
2774                "      {\n"
2775                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2776                "      },\n"
2777                "      a);",
2778                Style);
2779 
2780   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2781 
2782   verifyNoCrash("^{v^{a}}");
2783 }
2784 
2785 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2786   EXPECT_EQ("#define MACRO()                     \\\n"
2787             "  Debug(aaa, /* force line break */ \\\n"
2788             "        {                           \\\n"
2789             "          int i;                    \\\n"
2790             "          int j;                    \\\n"
2791             "        })",
2792             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2793                    "          {  int   i;  int  j;   })",
2794                    getGoogleStyle()));
2795 
2796   EXPECT_EQ("#define A                                       \\\n"
2797             "  [] {                                          \\\n"
2798             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2799             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2800             "  }",
2801             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2802                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2803                    getGoogleStyle()));
2804 }
2805 
2806 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2807   EXPECT_EQ("{}", format("{}"));
2808   verifyFormat("enum E {};");
2809   verifyFormat("enum E {}");
2810 }
2811 
2812 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2813   FormatStyle Style = getLLVMStyle();
2814   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2815   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2816   verifyFormat("FOO_BEGIN\n"
2817                "  FOO_ENTRY\n"
2818                "FOO_END", Style);
2819   verifyFormat("FOO_BEGIN\n"
2820                "  NESTED_FOO_BEGIN\n"
2821                "    NESTED_FOO_ENTRY\n"
2822                "  NESTED_FOO_END\n"
2823                "FOO_END", Style);
2824   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2825                "  int x;\n"
2826                "  x = 1;\n"
2827                "FOO_END(Baz)", Style);
2828 }
2829 
2830 //===----------------------------------------------------------------------===//
2831 // Line break tests.
2832 //===----------------------------------------------------------------------===//
2833 
2834 TEST_F(FormatTest, PreventConfusingIndents) {
2835   verifyFormat(
2836       "void f() {\n"
2837       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2838       "                         parameter, parameter, parameter)),\n"
2839       "                     SecondLongCall(parameter));\n"
2840       "}");
2841   verifyFormat(
2842       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2843       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2844       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2845       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2846   verifyFormat(
2847       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2848       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2849       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2850       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2851   verifyFormat(
2852       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2853       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2854       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2855       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2856   verifyFormat("int a = bbbb && ccc &&\n"
2857                "        fffff(\n"
2858                "#define A Just forcing a new line\n"
2859                "            ddd);");
2860 }
2861 
2862 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2863   verifyFormat(
2864       "bool aaaaaaa =\n"
2865       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2866       "    bbbbbbbb();");
2867   verifyFormat(
2868       "bool aaaaaaa =\n"
2869       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2870       "    bbbbbbbb();");
2871 
2872   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2873                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2874                "    ccccccccc == ddddddddddd;");
2875   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2876                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2877                "    ccccccccc == ddddddddddd;");
2878   verifyFormat(
2879       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2880       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2881       "    ccccccccc == ddddddddddd;");
2882 
2883   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2884                "                 aaaaaa) &&\n"
2885                "         bbbbbb && cccccc;");
2886   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2887                "                 aaaaaa) >>\n"
2888                "         bbbbbb;");
2889   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2890                "    SourceMgr.getSpellingColumnNumber(\n"
2891                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2892                "    1);");
2893 
2894   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2895                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2896                "    cccccc) {\n}");
2897   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2898                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
2899                "              cccccc) {\n}");
2900   verifyFormat("b = a &&\n"
2901                "    // Comment\n"
2902                "    b.c && d;");
2903 
2904   // If the LHS of a comparison is not a binary expression itself, the
2905   // additional linebreak confuses many people.
2906   verifyFormat(
2907       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2908       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2909       "}");
2910   verifyFormat(
2911       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2912       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2913       "}");
2914   verifyFormat(
2915       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2916       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2917       "}");
2918   // Even explicit parentheses stress the precedence enough to make the
2919   // additional break unnecessary.
2920   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2921                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2922                "}");
2923   // This cases is borderline, but with the indentation it is still readable.
2924   verifyFormat(
2925       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2926       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2927       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2928       "}",
2929       getLLVMStyleWithColumns(75));
2930 
2931   // If the LHS is a binary expression, we should still use the additional break
2932   // as otherwise the formatting hides the operator precedence.
2933   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2934                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2935                "    5) {\n"
2936                "}");
2937 
2938   FormatStyle OnePerLine = getLLVMStyle();
2939   OnePerLine.BinPackParameters = false;
2940   verifyFormat(
2941       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2942       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2943       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2944       OnePerLine);
2945 
2946   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2947                "                .aaa(aaaaaaaaaaaaa) *\n"
2948                "            aaaaaaa +\n"
2949                "        aaaaaaa;",
2950                getLLVMStyleWithColumns(40));
2951 }
2952 
2953 TEST_F(FormatTest, ExpressionIndentation) {
2954   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2955                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2956                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2957                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2958                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2959                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2960                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2961                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2962                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2963   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2964                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2965                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2966                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2967   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2968                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2969                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2970                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2971   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2972                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2973                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2974                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2975   verifyFormat("if () {\n"
2976                "} else if (aaaaa && bbbbb > // break\n"
2977                "                        ccccc) {\n"
2978                "}");
2979   verifyFormat("if () {\n"
2980                "} else if (aaaaa &&\n"
2981                "           bbbbb > // break\n"
2982                "               ccccc &&\n"
2983                "           ddddd) {\n"
2984                "}");
2985 
2986   // Presence of a trailing comment used to change indentation of b.
2987   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2988                "       b;\n"
2989                "return aaaaaaaaaaaaaaaaaaa +\n"
2990                "       b; //",
2991                getLLVMStyleWithColumns(30));
2992 }
2993 
2994 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2995   // Not sure what the best system is here. Like this, the LHS can be found
2996   // immediately above an operator (everything with the same or a higher
2997   // indent). The RHS is aligned right of the operator and so compasses
2998   // everything until something with the same indent as the operator is found.
2999   // FIXME: Is this a good system?
3000   FormatStyle Style = getLLVMStyle();
3001   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3002   verifyFormat(
3003       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3004       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3005       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3006       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3007       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3008       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3009       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3010       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3011       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3012       Style);
3013   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3014                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3015                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3016                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3017                Style);
3018   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3019                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3020                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3021                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3022                Style);
3023   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3024                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3025                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3026                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3027                Style);
3028   verifyFormat("if () {\n"
3029                "} else if (aaaaa\n"
3030                "           && bbbbb // break\n"
3031                "                  > ccccc) {\n"
3032                "}",
3033                Style);
3034   verifyFormat("return (a)\n"
3035                "       // comment\n"
3036                "       + b;",
3037                Style);
3038   verifyFormat(
3039       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3040       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3041       "             + cc;",
3042       Style);
3043 
3044   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3045                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3046                Style);
3047 
3048   // Forced by comments.
3049   verifyFormat(
3050       "unsigned ContentSize =\n"
3051       "    sizeof(int16_t)   // DWARF ARange version number\n"
3052       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3053       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3054       "    + sizeof(int8_t); // Segment Size (in bytes)");
3055 
3056   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3057                "       == boost::fusion::at_c<1>(iiii).second;",
3058                Style);
3059 
3060   Style.ColumnLimit = 60;
3061   verifyFormat("zzzzzzzzzz\n"
3062                "    = bbbbbbbbbbbbbbbbb\n"
3063                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3064                Style);
3065 }
3066 
3067 TEST_F(FormatTest, EnforcedOperatorWraps) {
3068   // Here we'd like to wrap after the || operators, but a comment is forcing an
3069   // earlier wrap.
3070   verifyFormat("bool x = aaaaa //\n"
3071                "         || bbbbb\n"
3072                "         //\n"
3073                "         || cccc;");
3074 }
3075 
3076 TEST_F(FormatTest, NoOperandAlignment) {
3077   FormatStyle Style = getLLVMStyle();
3078   Style.AlignOperands = false;
3079   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3080                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3081                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3082                Style);
3083   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3084   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3085                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3086                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3087                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3088                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3089                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3090                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3091                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3092                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3093                Style);
3094 
3095   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3096                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3097                "    + cc;",
3098                Style);
3099   verifyFormat("int a = aa\n"
3100                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3101                "        * cccccccccccccccccccccccccccccccccccc;\n",
3102                Style);
3103 
3104   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3105   verifyFormat("return (a > b\n"
3106                "    // comment1\n"
3107                "    // comment2\n"
3108                "    || c);",
3109                Style);
3110 }
3111 
3112 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3113   FormatStyle Style = getLLVMStyle();
3114   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3115   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3116                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3117                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3118                Style);
3119 }
3120 
3121 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3122   FormatStyle Style = getLLVMStyle();
3123   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3124   Style.BinPackArguments = false;
3125   Style.ColumnLimit = 40;
3126   verifyFormat("void test() {\n"
3127                "  someFunction(\n"
3128                "      this + argument + is + quite\n"
3129                "      + long + so + it + gets + wrapped\n"
3130                "      + but + remains + bin - packed);\n"
3131                "}",
3132                Style);
3133   verifyFormat("void test() {\n"
3134                "  someFunction(arg1,\n"
3135                "               this + argument + is\n"
3136                "                   + quite + long + so\n"
3137                "                   + it + gets + wrapped\n"
3138                "                   + but + remains + bin\n"
3139                "                   - packed,\n"
3140                "               arg3);\n"
3141                "}",
3142                Style);
3143   verifyFormat("void test() {\n"
3144                "  someFunction(\n"
3145                "      arg1,\n"
3146                "      this + argument + has\n"
3147                "          + anotherFunc(nested,\n"
3148                "                        calls + whose\n"
3149                "                            + arguments\n"
3150                "                            + are + also\n"
3151                "                            + wrapped,\n"
3152                "                        in + addition)\n"
3153                "          + to + being + bin - packed,\n"
3154                "      arg3);\n"
3155                "}",
3156                Style);
3157 
3158   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3159   verifyFormat("void test() {\n"
3160                "  someFunction(\n"
3161                "      arg1,\n"
3162                "      this + argument + has +\n"
3163                "          anotherFunc(nested,\n"
3164                "                      calls + whose +\n"
3165                "                          arguments +\n"
3166                "                          are + also +\n"
3167                "                          wrapped,\n"
3168                "                      in + addition) +\n"
3169                "          to + being + bin - packed,\n"
3170                "      arg3);\n"
3171                "}",
3172                Style);
3173 }
3174 
3175 TEST_F(FormatTest, ConstructorInitializers) {
3176   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3177   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3178                getLLVMStyleWithColumns(45));
3179   verifyFormat("Constructor()\n"
3180                "    : Inttializer(FitsOnTheLine) {}",
3181                getLLVMStyleWithColumns(44));
3182   verifyFormat("Constructor()\n"
3183                "    : Inttializer(FitsOnTheLine) {}",
3184                getLLVMStyleWithColumns(43));
3185 
3186   verifyFormat("template <typename T>\n"
3187                "Constructor() : Initializer(FitsOnTheLine) {}",
3188                getLLVMStyleWithColumns(45));
3189 
3190   verifyFormat(
3191       "SomeClass::Constructor()\n"
3192       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3193 
3194   verifyFormat(
3195       "SomeClass::Constructor()\n"
3196       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3197       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3198   verifyFormat(
3199       "SomeClass::Constructor()\n"
3200       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3201       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3202   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3203                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3204                "    : aaaaaaaaaa(aaaaaa) {}");
3205 
3206   verifyFormat("Constructor()\n"
3207                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3208                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3209                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3210                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3211 
3212   verifyFormat("Constructor()\n"
3213                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3214                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3215 
3216   verifyFormat("Constructor(int Parameter = 0)\n"
3217                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3218                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3219   verifyFormat("Constructor()\n"
3220                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3221                "}",
3222                getLLVMStyleWithColumns(60));
3223   verifyFormat("Constructor()\n"
3224                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3225                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3226 
3227   // Here a line could be saved by splitting the second initializer onto two
3228   // lines, but that is not desirable.
3229   verifyFormat("Constructor()\n"
3230                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3231                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3232                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3233 
3234   FormatStyle OnePerLine = getLLVMStyle();
3235   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3236   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3237   verifyFormat("SomeClass::Constructor()\n"
3238                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3239                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3240                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3241                OnePerLine);
3242   verifyFormat("SomeClass::Constructor()\n"
3243                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3244                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3245                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3246                OnePerLine);
3247   verifyFormat("MyClass::MyClass(int var)\n"
3248                "    : some_var_(var),            // 4 space indent\n"
3249                "      some_other_var_(var + 1) { // lined up\n"
3250                "}",
3251                OnePerLine);
3252   verifyFormat("Constructor()\n"
3253                "    : aaaaa(aaaaaa),\n"
3254                "      aaaaa(aaaaaa),\n"
3255                "      aaaaa(aaaaaa),\n"
3256                "      aaaaa(aaaaaa),\n"
3257                "      aaaaa(aaaaaa) {}",
3258                OnePerLine);
3259   verifyFormat("Constructor()\n"
3260                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3261                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3262                OnePerLine);
3263   OnePerLine.BinPackParameters = false;
3264   verifyFormat(
3265       "Constructor()\n"
3266       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3267       "          aaaaaaaaaaa().aaa(),\n"
3268       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3269       OnePerLine);
3270   OnePerLine.ColumnLimit = 60;
3271   verifyFormat("Constructor()\n"
3272                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3273                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3274                OnePerLine);
3275 
3276   EXPECT_EQ("Constructor()\n"
3277             "    : // Comment forcing unwanted break.\n"
3278             "      aaaa(aaaa) {}",
3279             format("Constructor() :\n"
3280                    "    // Comment forcing unwanted break.\n"
3281                    "    aaaa(aaaa) {}"));
3282 }
3283 
3284 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3285   FormatStyle Style = getLLVMStyle();
3286   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3287 
3288   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3289   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3290                getStyleWithColumns(Style, 45));
3291   verifyFormat("Constructor() :\n"
3292                "    Initializer(FitsOnTheLine) {}",
3293                getStyleWithColumns(Style, 44));
3294   verifyFormat("Constructor() :\n"
3295                "    Initializer(FitsOnTheLine) {}",
3296                getStyleWithColumns(Style, 43));
3297 
3298   verifyFormat("template <typename T>\n"
3299                "Constructor() : Initializer(FitsOnTheLine) {}",
3300                getStyleWithColumns(Style, 50));
3301 
3302   verifyFormat(
3303       "SomeClass::Constructor() :\n"
3304       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3305 	  Style);
3306 
3307   verifyFormat(
3308       "SomeClass::Constructor() :\n"
3309       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3310       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3311 	  Style);
3312   verifyFormat(
3313       "SomeClass::Constructor() :\n"
3314       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3315       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3316 	  Style);
3317   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3318                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3319                "    aaaaaaaaaa(aaaaaa) {}",
3320 			   Style);
3321 
3322   verifyFormat("Constructor() :\n"
3323                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3324                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3325                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3326                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3327 			   Style);
3328 
3329   verifyFormat("Constructor() :\n"
3330                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3331                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3332 			   Style);
3333 
3334   verifyFormat("Constructor(int Parameter = 0) :\n"
3335                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3336                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3337 			   Style);
3338   verifyFormat("Constructor() :\n"
3339                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3340                "}",
3341                getStyleWithColumns(Style, 60));
3342   verifyFormat("Constructor() :\n"
3343                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3344                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3345 			   Style);
3346 
3347   // Here a line could be saved by splitting the second initializer onto two
3348   // lines, but that is not desirable.
3349   verifyFormat("Constructor() :\n"
3350                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3351                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3352                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3353 			   Style);
3354 
3355   FormatStyle OnePerLine = Style;
3356   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3357   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3358   verifyFormat("SomeClass::Constructor() :\n"
3359                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3360                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3361                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3362                OnePerLine);
3363   verifyFormat("SomeClass::Constructor() :\n"
3364                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3365                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3366                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3367                OnePerLine);
3368   verifyFormat("MyClass::MyClass(int var) :\n"
3369                "    some_var_(var),            // 4 space indent\n"
3370                "    some_other_var_(var + 1) { // lined up\n"
3371                "}",
3372                OnePerLine);
3373   verifyFormat("Constructor() :\n"
3374                "    aaaaa(aaaaaa),\n"
3375                "    aaaaa(aaaaaa),\n"
3376                "    aaaaa(aaaaaa),\n"
3377                "    aaaaa(aaaaaa),\n"
3378                "    aaaaa(aaaaaa) {}",
3379                OnePerLine);
3380   verifyFormat("Constructor() :\n"
3381                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3382                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3383                OnePerLine);
3384   OnePerLine.BinPackParameters = false;
3385   verifyFormat(
3386       "Constructor() :\n"
3387       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3388       "        aaaaaaaaaaa().aaa(),\n"
3389       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3390       OnePerLine);
3391   OnePerLine.ColumnLimit = 60;
3392   verifyFormat("Constructor() :\n"
3393                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3394                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3395                OnePerLine);
3396 
3397   EXPECT_EQ("Constructor() :\n"
3398             "    // Comment forcing unwanted break.\n"
3399             "    aaaa(aaaa) {}",
3400             format("Constructor() :\n"
3401                    "    // Comment forcing unwanted break.\n"
3402                    "    aaaa(aaaa) {}",
3403 				   Style));
3404 
3405   Style.ColumnLimit = 0;
3406   verifyFormat("SomeClass::Constructor() :\n"
3407                "    a(a) {}",
3408                Style);
3409   verifyFormat("SomeClass::Constructor() noexcept :\n"
3410                "    a(a) {}",
3411                Style);
3412   verifyFormat("SomeClass::Constructor() :\n"
3413 			   "    a(a), b(b), c(c) {}",
3414                Style);
3415   verifyFormat("SomeClass::Constructor() :\n"
3416                "    a(a) {\n"
3417                "  foo();\n"
3418                "  bar();\n"
3419                "}",
3420                Style);
3421 
3422   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3423   verifyFormat("SomeClass::Constructor() :\n"
3424 			   "    a(a), b(b), c(c) {\n"
3425 			   "}",
3426                Style);
3427   verifyFormat("SomeClass::Constructor() :\n"
3428                "    a(a) {\n"
3429 			   "}",
3430                Style);
3431 
3432   Style.ColumnLimit = 80;
3433   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3434   Style.ConstructorInitializerIndentWidth = 2;
3435   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3436                Style);
3437   verifyFormat("SomeClass::Constructor() :\n"
3438                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3439                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3440                Style);
3441 }
3442 
3443 TEST_F(FormatTest, MemoizationTests) {
3444   // This breaks if the memoization lookup does not take \c Indent and
3445   // \c LastSpace into account.
3446   verifyFormat(
3447       "extern CFRunLoopTimerRef\n"
3448       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3449       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3450       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3451       "                     CFRunLoopTimerContext *context) {}");
3452 
3453   // Deep nesting somewhat works around our memoization.
3454   verifyFormat(
3455       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3456       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3457       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3458       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3459       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3460       getLLVMStyleWithColumns(65));
3461   verifyFormat(
3462       "aaaaa(\n"
3463       "    aaaaa,\n"
3464       "    aaaaa(\n"
3465       "        aaaaa,\n"
3466       "        aaaaa(\n"
3467       "            aaaaa,\n"
3468       "            aaaaa(\n"
3469       "                aaaaa,\n"
3470       "                aaaaa(\n"
3471       "                    aaaaa,\n"
3472       "                    aaaaa(\n"
3473       "                        aaaaa,\n"
3474       "                        aaaaa(\n"
3475       "                            aaaaa,\n"
3476       "                            aaaaa(\n"
3477       "                                aaaaa,\n"
3478       "                                aaaaa(\n"
3479       "                                    aaaaa,\n"
3480       "                                    aaaaa(\n"
3481       "                                        aaaaa,\n"
3482       "                                        aaaaa(\n"
3483       "                                            aaaaa,\n"
3484       "                                            aaaaa(\n"
3485       "                                                aaaaa,\n"
3486       "                                                aaaaa))))))))))));",
3487       getLLVMStyleWithColumns(65));
3488   verifyFormat(
3489       "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"
3490       "                                  a),\n"
3491       "                                a),\n"
3492       "                              a),\n"
3493       "                            a),\n"
3494       "                          a),\n"
3495       "                        a),\n"
3496       "                      a),\n"
3497       "                    a),\n"
3498       "                  a),\n"
3499       "                a),\n"
3500       "              a),\n"
3501       "            a),\n"
3502       "          a),\n"
3503       "        a),\n"
3504       "      a),\n"
3505       "    a),\n"
3506       "  a)",
3507       getLLVMStyleWithColumns(65));
3508 
3509   // This test takes VERY long when memoization is broken.
3510   FormatStyle OnePerLine = getLLVMStyle();
3511   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3512   OnePerLine.BinPackParameters = false;
3513   std::string input = "Constructor()\n"
3514                       "    : aaaa(a,\n";
3515   for (unsigned i = 0, e = 80; i != e; ++i) {
3516     input += "           a,\n";
3517   }
3518   input += "           a) {}";
3519   verifyFormat(input, OnePerLine);
3520 }
3521 
3522 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3523   verifyFormat(
3524       "void f() {\n"
3525       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3526       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3527       "    f();\n"
3528       "}");
3529   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3530                "    Intervals[i - 1].getRange().getLast()) {\n}");
3531 }
3532 
3533 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3534   // Principially, we break function declarations in a certain order:
3535   // 1) break amongst arguments.
3536   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3537                "                              Cccccccccccccc cccccccccccccc);");
3538   verifyFormat("template <class TemplateIt>\n"
3539                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3540                "                            TemplateIt *stop) {}");
3541 
3542   // 2) break after return type.
3543   verifyFormat(
3544       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3545       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3546       getGoogleStyle());
3547 
3548   // 3) break after (.
3549   verifyFormat(
3550       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3551       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3552       getGoogleStyle());
3553 
3554   // 4) break before after nested name specifiers.
3555   verifyFormat(
3556       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3557       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3558       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3559       getGoogleStyle());
3560 
3561   // However, there are exceptions, if a sufficient amount of lines can be
3562   // saved.
3563   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3564   // more adjusting.
3565   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3566                "                                  Cccccccccccccc cccccccccc,\n"
3567                "                                  Cccccccccccccc cccccccccc,\n"
3568                "                                  Cccccccccccccc cccccccccc,\n"
3569                "                                  Cccccccccccccc cccccccccc);");
3570   verifyFormat(
3571       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3572       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3573       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3574       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3575       getGoogleStyle());
3576   verifyFormat(
3577       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3578       "                                          Cccccccccccccc cccccccccc,\n"
3579       "                                          Cccccccccccccc cccccccccc,\n"
3580       "                                          Cccccccccccccc cccccccccc,\n"
3581       "                                          Cccccccccccccc cccccccccc,\n"
3582       "                                          Cccccccccccccc cccccccccc,\n"
3583       "                                          Cccccccccccccc cccccccccc);");
3584   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3585                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3586                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3587                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3588                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3589 
3590   // Break after multi-line parameters.
3591   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3592                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3593                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3594                "    bbbb bbbb);");
3595   verifyFormat("void SomeLoooooooooooongFunction(\n"
3596                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3597                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3598                "    int bbbbbbbbbbbbb);");
3599 
3600   // Treat overloaded operators like other functions.
3601   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3602                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3603   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3604                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3605   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3606                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3607   verifyGoogleFormat(
3608       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3609       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3610   verifyGoogleFormat(
3611       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3612       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3613   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3614                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3615   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3616                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3617   verifyGoogleFormat(
3618       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3619       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3620       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3621   verifyGoogleFormat(
3622       "template <typename T>\n"
3623       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3624       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3625       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3626 
3627   FormatStyle Style = getLLVMStyle();
3628   Style.PointerAlignment = FormatStyle::PAS_Left;
3629   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3630                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3631                Style);
3632   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3633                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3634                Style);
3635 }
3636 
3637 TEST_F(FormatTest, TrailingReturnType) {
3638   verifyFormat("auto foo() -> int;\n");
3639   verifyFormat("struct S {\n"
3640                "  auto bar() const -> int;\n"
3641                "};");
3642   verifyFormat("template <size_t Order, typename T>\n"
3643                "auto load_img(const std::string &filename)\n"
3644                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3645   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3646                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3647   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3648   verifyFormat("template <typename T>\n"
3649                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3650                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3651 
3652   // Not trailing return types.
3653   verifyFormat("void f() { auto a = b->c(); }");
3654 }
3655 
3656 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3657   // Avoid breaking before trailing 'const' or other trailing annotations, if
3658   // they are not function-like.
3659   FormatStyle Style = getGoogleStyle();
3660   Style.ColumnLimit = 47;
3661   verifyFormat("void someLongFunction(\n"
3662                "    int someLoooooooooooooongParameter) const {\n}",
3663                getLLVMStyleWithColumns(47));
3664   verifyFormat("LoooooongReturnType\n"
3665                "someLoooooooongFunction() const {}",
3666                getLLVMStyleWithColumns(47));
3667   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3668                "    const {}",
3669                Style);
3670   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3671                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3672   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3673                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3674   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3675                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3676   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3677                "                   aaaaaaaaaaa aaaaa) const override;");
3678   verifyGoogleFormat(
3679       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3680       "    const override;");
3681 
3682   // Even if the first parameter has to be wrapped.
3683   verifyFormat("void someLongFunction(\n"
3684                "    int someLongParameter) const {}",
3685                getLLVMStyleWithColumns(46));
3686   verifyFormat("void someLongFunction(\n"
3687                "    int someLongParameter) const {}",
3688                Style);
3689   verifyFormat("void someLongFunction(\n"
3690                "    int someLongParameter) override {}",
3691                Style);
3692   verifyFormat("void someLongFunction(\n"
3693                "    int someLongParameter) OVERRIDE {}",
3694                Style);
3695   verifyFormat("void someLongFunction(\n"
3696                "    int someLongParameter) final {}",
3697                Style);
3698   verifyFormat("void someLongFunction(\n"
3699                "    int someLongParameter) FINAL {}",
3700                Style);
3701   verifyFormat("void someLongFunction(\n"
3702                "    int parameter) const override {}",
3703                Style);
3704 
3705   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3706   verifyFormat("void someLongFunction(\n"
3707                "    int someLongParameter) const\n"
3708                "{\n"
3709                "}",
3710                Style);
3711 
3712   // Unless these are unknown annotations.
3713   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3714                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3715                "    LONG_AND_UGLY_ANNOTATION;");
3716 
3717   // Breaking before function-like trailing annotations is fine to keep them
3718   // close to their arguments.
3719   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3720                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3721   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3722                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3723   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3724                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3725   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3726                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3727   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3728 
3729   verifyFormat(
3730       "void aaaaaaaaaaaaaaaaaa()\n"
3731       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3732       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3733   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3734                "    __attribute__((unused));");
3735   verifyGoogleFormat(
3736       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3737       "    GUARDED_BY(aaaaaaaaaaaa);");
3738   verifyGoogleFormat(
3739       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3740       "    GUARDED_BY(aaaaaaaaaaaa);");
3741   verifyGoogleFormat(
3742       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3743       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3744   verifyGoogleFormat(
3745       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3746       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3747 }
3748 
3749 TEST_F(FormatTest, FunctionAnnotations) {
3750   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3751                "int OldFunction(const string &parameter) {}");
3752   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3753                "string OldFunction(const string &parameter) {}");
3754   verifyFormat("template <typename T>\n"
3755                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3756                "string OldFunction(const string &parameter) {}");
3757 
3758   // Not function annotations.
3759   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3760                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3761   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3762                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3763   verifyFormat("MACRO(abc).function() // wrap\n"
3764                "    << abc;");
3765   verifyFormat("MACRO(abc)->function() // wrap\n"
3766                "    << abc;");
3767   verifyFormat("MACRO(abc)::function() // wrap\n"
3768                "    << abc;");
3769 }
3770 
3771 TEST_F(FormatTest, BreaksDesireably) {
3772   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3773                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3774                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3775   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3776                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3777                "}");
3778 
3779   verifyFormat(
3780       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3781       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3782 
3783   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3784                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3785                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3786 
3787   verifyFormat(
3788       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3789       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3790       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3791       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3792       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3793 
3794   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3795                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3796 
3797   verifyFormat(
3798       "void f() {\n"
3799       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3800       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3801       "}");
3802   verifyFormat(
3803       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3804       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3805   verifyFormat(
3806       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3807       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3808   verifyFormat(
3809       "aaaaaa(aaa,\n"
3810       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3811       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3812       "       aaaa);");
3813   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3814                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3815                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3816 
3817   // Indent consistently independent of call expression and unary operator.
3818   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3819                "    dddddddddddddddddddddddddddddd));");
3820   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3821                "    dddddddddddddddddddddddddddddd));");
3822   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3823                "    dddddddddddddddddddddddddddddd));");
3824 
3825   // This test case breaks on an incorrect memoization, i.e. an optimization not
3826   // taking into account the StopAt value.
3827   verifyFormat(
3828       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3829       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3830       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3831       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3832 
3833   verifyFormat("{\n  {\n    {\n"
3834                "      Annotation.SpaceRequiredBefore =\n"
3835                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3836                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3837                "    }\n  }\n}");
3838 
3839   // Break on an outer level if there was a break on an inner level.
3840   EXPECT_EQ("f(g(h(a, // comment\n"
3841             "      b, c),\n"
3842             "    d, e),\n"
3843             "  x, y);",
3844             format("f(g(h(a, // comment\n"
3845                    "    b, c), d, e), x, y);"));
3846 
3847   // Prefer breaking similar line breaks.
3848   verifyFormat(
3849       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3850       "                             NSTrackingMouseEnteredAndExited |\n"
3851       "                             NSTrackingActiveAlways;");
3852 }
3853 
3854 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3855   FormatStyle NoBinPacking = getGoogleStyle();
3856   NoBinPacking.BinPackParameters = false;
3857   NoBinPacking.BinPackArguments = true;
3858   verifyFormat("void f() {\n"
3859                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3860                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3861                "}",
3862                NoBinPacking);
3863   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3864                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3865                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3866                NoBinPacking);
3867 
3868   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3869   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3870                "                        vector<int> bbbbbbbbbbbbbbb);",
3871                NoBinPacking);
3872   // FIXME: This behavior difference is probably not wanted. However, currently
3873   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3874   // template arguments from BreakBeforeParameter being set because of the
3875   // one-per-line formatting.
3876   verifyFormat(
3877       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3878       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3879       NoBinPacking);
3880   verifyFormat(
3881       "void fffffffffff(\n"
3882       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3883       "        aaaaaaaaaa);");
3884 }
3885 
3886 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3887   FormatStyle NoBinPacking = getGoogleStyle();
3888   NoBinPacking.BinPackParameters = false;
3889   NoBinPacking.BinPackArguments = false;
3890   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3891                "  aaaaaaaaaaaaaaaaaaaa,\n"
3892                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3893                NoBinPacking);
3894   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3895                "        aaaaaaaaaaaaa,\n"
3896                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3897                NoBinPacking);
3898   verifyFormat(
3899       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3900       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3901       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3902       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3903       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3904       NoBinPacking);
3905   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3906                "    .aaaaaaaaaaaaaaaaaa();",
3907                NoBinPacking);
3908   verifyFormat("void f() {\n"
3909                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3910                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3911                "}",
3912                NoBinPacking);
3913 
3914   verifyFormat(
3915       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3916       "             aaaaaaaaaaaa,\n"
3917       "             aaaaaaaaaaaa);",
3918       NoBinPacking);
3919   verifyFormat(
3920       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3921       "                               ddddddddddddddddddddddddddddd),\n"
3922       "             test);",
3923       NoBinPacking);
3924 
3925   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3926                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3927                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3928                "    aaaaaaaaaaaaaaaaaa;",
3929                NoBinPacking);
3930   verifyFormat("a(\"a\"\n"
3931                "  \"a\",\n"
3932                "  a);");
3933 
3934   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3935   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3936                "                aaaaaaaaa,\n"
3937                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3938                NoBinPacking);
3939   verifyFormat(
3940       "void f() {\n"
3941       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3942       "      .aaaaaaa();\n"
3943       "}",
3944       NoBinPacking);
3945   verifyFormat(
3946       "template <class SomeType, class SomeOtherType>\n"
3947       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3948       NoBinPacking);
3949 }
3950 
3951 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3952   FormatStyle Style = getLLVMStyleWithColumns(15);
3953   Style.ExperimentalAutoDetectBinPacking = true;
3954   EXPECT_EQ("aaa(aaaa,\n"
3955             "    aaaa,\n"
3956             "    aaaa);\n"
3957             "aaa(aaaa,\n"
3958             "    aaaa,\n"
3959             "    aaaa);",
3960             format("aaa(aaaa,\n" // one-per-line
3961                    "  aaaa,\n"
3962                    "    aaaa  );\n"
3963                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3964                    Style));
3965   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3966             "    aaaa);\n"
3967             "aaa(aaaa, aaaa,\n"
3968             "    aaaa);",
3969             format("aaa(aaaa,  aaaa,\n" // bin-packed
3970                    "    aaaa  );\n"
3971                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3972                    Style));
3973 }
3974 
3975 TEST_F(FormatTest, FormatsBuilderPattern) {
3976   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3977                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3978                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3979                "    .StartsWith(\".init\", ORDER_INIT)\n"
3980                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3981                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3982                "    .Default(ORDER_TEXT);\n");
3983 
3984   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3985                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3986   verifyFormat(
3987       "aaaaaaa->aaaaaaa\n"
3988       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3989       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3990       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3991   verifyFormat(
3992       "aaaaaaa->aaaaaaa\n"
3993       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3994       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3995   verifyFormat(
3996       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3997       "    aaaaaaaaaaaaaa);");
3998   verifyFormat(
3999       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4000       "    aaaaaa->aaaaaaaaaaaa()\n"
4001       "        ->aaaaaaaaaaaaaaaa(\n"
4002       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4003       "        ->aaaaaaaaaaaaaaaaa();");
4004   verifyGoogleFormat(
4005       "void f() {\n"
4006       "  someo->Add((new util::filetools::Handler(dir))\n"
4007       "                 ->OnEvent1(NewPermanentCallback(\n"
4008       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4009       "                 ->OnEvent2(NewPermanentCallback(\n"
4010       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4011       "                 ->OnEvent3(NewPermanentCallback(\n"
4012       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4013       "                 ->OnEvent5(NewPermanentCallback(\n"
4014       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4015       "                 ->OnEvent6(NewPermanentCallback(\n"
4016       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4017       "}");
4018 
4019   verifyFormat(
4020       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4021   verifyFormat("aaaaaaaaaaaaaaa()\n"
4022                "    .aaaaaaaaaaaaaaa()\n"
4023                "    .aaaaaaaaaaaaaaa()\n"
4024                "    .aaaaaaaaaaaaaaa()\n"
4025                "    .aaaaaaaaaaaaaaa();");
4026   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4027                "    .aaaaaaaaaaaaaaa()\n"
4028                "    .aaaaaaaaaaaaaaa()\n"
4029                "    .aaaaaaaaaaaaaaa();");
4030   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4031                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4032                "    .aaaaaaaaaaaaaaa();");
4033   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4034                "    ->aaaaaaaaaaaaaae(0)\n"
4035                "    ->aaaaaaaaaaaaaaa();");
4036 
4037   // Don't linewrap after very short segments.
4038   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4039                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4040                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4041   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4042                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4043                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4044   verifyFormat("aaa()\n"
4045                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4046                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4047                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4048 
4049   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4050                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4051                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4052   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4053                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4054                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4055 
4056   // Prefer not to break after empty parentheses.
4057   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4058                "    First->LastNewlineOffset);");
4059 
4060   // Prefer not to create "hanging" indents.
4061   verifyFormat(
4062       "return !soooooooooooooome_map\n"
4063       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4064       "            .second;");
4065   verifyFormat(
4066       "return aaaaaaaaaaaaaaaa\n"
4067       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4068       "    .aaaa(aaaaaaaaaaaaaa);");
4069   // No hanging indent here.
4070   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4071                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4072   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4073                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4074   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4075                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4076                getLLVMStyleWithColumns(60));
4077   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4078                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4079                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4080                getLLVMStyleWithColumns(59));
4081   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4082                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4083                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4084 }
4085 
4086 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4087   verifyFormat(
4088       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4089       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4090   verifyFormat(
4091       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4092       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4093 
4094   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4095                "    ccccccccccccccccccccccccc) {\n}");
4096   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4097                "    ccccccccccccccccccccccccc) {\n}");
4098 
4099   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4100                "    ccccccccccccccccccccccccc) {\n}");
4101   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4102                "    ccccccccccccccccccccccccc) {\n}");
4103 
4104   verifyFormat(
4105       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4106       "    ccccccccccccccccccccccccc) {\n}");
4107   verifyFormat(
4108       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4109       "    ccccccccccccccccccccccccc) {\n}");
4110 
4111   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4112                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4113                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4114                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4115   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4116                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4117                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4118                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4119 
4120   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4121                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4122                "    aaaaaaaaaaaaaaa != aa) {\n}");
4123   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4124                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4125                "    aaaaaaaaaaaaaaa != aa) {\n}");
4126 }
4127 
4128 TEST_F(FormatTest, BreaksAfterAssignments) {
4129   verifyFormat(
4130       "unsigned Cost =\n"
4131       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4132       "                        SI->getPointerAddressSpaceee());\n");
4133   verifyFormat(
4134       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4135       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4136 
4137   verifyFormat(
4138       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4139       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4140   verifyFormat("unsigned OriginalStartColumn =\n"
4141                "    SourceMgr.getSpellingColumnNumber(\n"
4142                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4143                "    1;");
4144 }
4145 
4146 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4147   FormatStyle Style = getLLVMStyle();
4148   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4149                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4150                Style);
4151 
4152   Style.PenaltyBreakAssignment = 20;
4153   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4154                "                                 cccccccccccccccccccccccccc;",
4155                Style);
4156 }
4157 
4158 TEST_F(FormatTest, AlignsAfterAssignments) {
4159   verifyFormat(
4160       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4161       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4162   verifyFormat(
4163       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4164       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4165   verifyFormat(
4166       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4167       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4168   verifyFormat(
4169       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4170       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4171   verifyFormat(
4172       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4173       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4174       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4175 }
4176 
4177 TEST_F(FormatTest, AlignsAfterReturn) {
4178   verifyFormat(
4179       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4180       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4181   verifyFormat(
4182       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4183       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4184   verifyFormat(
4185       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4186       "       aaaaaaaaaaaaaaaaaaaaaa();");
4187   verifyFormat(
4188       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4189       "        aaaaaaaaaaaaaaaaaaaaaa());");
4190   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4191                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4192   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4193                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4194                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4195   verifyFormat("return\n"
4196                "    // true if code is one of a or b.\n"
4197                "    code == a || code == b;");
4198 }
4199 
4200 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4201   verifyFormat(
4202       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4203       "                                                aaaaaaaaa aaaaaaa) {}");
4204   verifyFormat(
4205       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4206       "                                               aaaaaaaaaaa aaaaaaaaa);");
4207   verifyFormat(
4208       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4209       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4210   FormatStyle Style = getLLVMStyle();
4211   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4212   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4213                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4214                Style);
4215   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4216                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4217                Style);
4218   verifyFormat("SomeLongVariableName->someFunction(\n"
4219                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4220                Style);
4221   verifyFormat(
4222       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4223       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4224       Style);
4225   verifyFormat(
4226       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4227       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4228       Style);
4229   verifyFormat(
4230       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4231       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4232       Style);
4233 
4234   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4235                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4236                "        b));",
4237                Style);
4238 
4239   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4240   Style.BinPackArguments = false;
4241   Style.BinPackParameters = false;
4242   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4243                "    aaaaaaaaaaa aaaaaaaa,\n"
4244                "    aaaaaaaaa aaaaaaa,\n"
4245                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4246                Style);
4247   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4248                "    aaaaaaaaaaa aaaaaaaaa,\n"
4249                "    aaaaaaaaaaa aaaaaaaaa,\n"
4250                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4251                Style);
4252   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4253                "    aaaaaaaaaaaaaaa,\n"
4254                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4255                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4256                Style);
4257   verifyFormat(
4258       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4259       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4260       Style);
4261   verifyFormat(
4262       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4263       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4264       Style);
4265   verifyFormat(
4266       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4267       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4268       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4269       "    aaaaaaaaaaaaaaaa);",
4270       Style);
4271   verifyFormat(
4272       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4273       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4274       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4275       "    aaaaaaaaaaaaaaaa);",
4276       Style);
4277 }
4278 
4279 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4280   FormatStyle Style = getLLVMStyleWithColumns(40);
4281   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4282                "          bbbbbbbbbbbbbbbbbbbbbb);",
4283                Style);
4284   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4285   Style.AlignOperands = false;
4286   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4287                "          bbbbbbbbbbbbbbbbbbbbbb);",
4288                Style);
4289   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4290   Style.AlignOperands = true;
4291   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4292                "          bbbbbbbbbbbbbbbbbbbbbb);",
4293                Style);
4294   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4295   Style.AlignOperands = false;
4296   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4297                "    bbbbbbbbbbbbbbbbbbbbbb);",
4298                Style);
4299 }
4300 
4301 TEST_F(FormatTest, BreaksConditionalExpressions) {
4302   verifyFormat(
4303       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4304       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4305       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4306   verifyFormat(
4307       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4308       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4309       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4310   verifyFormat(
4311       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4312       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4313   verifyFormat(
4314       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4315       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4316       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4317   verifyFormat(
4318       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4319       "                                                    : aaaaaaaaaaaaa);");
4320   verifyFormat(
4321       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4322       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4323       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4324       "                   aaaaaaaaaaaaa);");
4325   verifyFormat(
4326       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4327       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4328       "                   aaaaaaaaaaaaa);");
4329   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4330                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4331                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4332                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4333                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4334   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4335                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4336                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4337                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4338                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4339                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4340                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4341   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4342                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4343                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4344                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4345                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4346   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4347                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4348                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4349   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4350                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4351                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4352                "        : aaaaaaaaaaaaaaaa;");
4353   verifyFormat(
4354       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4355       "    ? aaaaaaaaaaaaaaa\n"
4356       "    : aaaaaaaaaaaaaaa;");
4357   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4358                "          aaaaaaaaa\n"
4359                "      ? b\n"
4360                "      : c);");
4361   verifyFormat("return aaaa == bbbb\n"
4362                "           // comment\n"
4363                "           ? aaaa\n"
4364                "           : bbbb;");
4365   verifyFormat("unsigned Indent =\n"
4366                "    format(TheLine.First,\n"
4367                "           IndentForLevel[TheLine.Level] >= 0\n"
4368                "               ? IndentForLevel[TheLine.Level]\n"
4369                "               : TheLine * 2,\n"
4370                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4371                getLLVMStyleWithColumns(60));
4372   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4373                "                  ? aaaaaaaaaaaaaaa\n"
4374                "                  : bbbbbbbbbbbbbbb //\n"
4375                "                        ? ccccccccccccccc\n"
4376                "                        : ddddddddddddddd;");
4377   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4378                "                  ? aaaaaaaaaaaaaaa\n"
4379                "                  : (bbbbbbbbbbbbbbb //\n"
4380                "                         ? ccccccccccccccc\n"
4381                "                         : ddddddddddddddd);");
4382   verifyFormat(
4383       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4384       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4385       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4386       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4387       "                                      : aaaaaaaaaa;");
4388   verifyFormat(
4389       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4390       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4391       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4392 
4393   FormatStyle NoBinPacking = getLLVMStyle();
4394   NoBinPacking.BinPackArguments = false;
4395   verifyFormat(
4396       "void f() {\n"
4397       "  g(aaa,\n"
4398       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4399       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4400       "        ? aaaaaaaaaaaaaaa\n"
4401       "        : aaaaaaaaaaaaaaa);\n"
4402       "}",
4403       NoBinPacking);
4404   verifyFormat(
4405       "void f() {\n"
4406       "  g(aaa,\n"
4407       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4408       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4409       "        ?: aaaaaaaaaaaaaaa);\n"
4410       "}",
4411       NoBinPacking);
4412 
4413   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4414                "             // comment.\n"
4415                "             ccccccccccccccccccccccccccccccccccccccc\n"
4416                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4417                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4418 
4419   // Assignments in conditional expressions. Apparently not uncommon :-(.
4420   verifyFormat("return a != b\n"
4421                "           // comment\n"
4422                "           ? a = b\n"
4423                "           : a = b;");
4424   verifyFormat("return a != b\n"
4425                "           // comment\n"
4426                "           ? a = a != b\n"
4427                "                     // comment\n"
4428                "                     ? a = b\n"
4429                "                     : a\n"
4430                "           : a;\n");
4431   verifyFormat("return a != b\n"
4432                "           // comment\n"
4433                "           ? a\n"
4434                "           : a = a != b\n"
4435                "                     // comment\n"
4436                "                     ? a = b\n"
4437                "                     : a;");
4438 }
4439 
4440 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4441   FormatStyle Style = getLLVMStyle();
4442   Style.BreakBeforeTernaryOperators = false;
4443   Style.ColumnLimit = 70;
4444   verifyFormat(
4445       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4446       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4447       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4448       Style);
4449   verifyFormat(
4450       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4451       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4452       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4453       Style);
4454   verifyFormat(
4455       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4456       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4457       Style);
4458   verifyFormat(
4459       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4460       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4461       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4462       Style);
4463   verifyFormat(
4464       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4465       "                                                      aaaaaaaaaaaaa);",
4466       Style);
4467   verifyFormat(
4468       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4469       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4470       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4471       "                   aaaaaaaaaaaaa);",
4472       Style);
4473   verifyFormat(
4474       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4475       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4476       "                   aaaaaaaaaaaaa);",
4477       Style);
4478   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4479                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4480                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4481                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4482                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4483                Style);
4484   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4485                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4486                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4487                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4488                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4489                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4490                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4491                Style);
4492   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4493                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4494                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4495                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4496                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4497                Style);
4498   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4499                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4500                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4501                Style);
4502   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4503                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4504                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4505                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4506                Style);
4507   verifyFormat(
4508       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4509       "    aaaaaaaaaaaaaaa :\n"
4510       "    aaaaaaaaaaaaaaa;",
4511       Style);
4512   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4513                "          aaaaaaaaa ?\n"
4514                "      b :\n"
4515                "      c);",
4516                Style);
4517   verifyFormat("unsigned Indent =\n"
4518                "    format(TheLine.First,\n"
4519                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4520                "               IndentForLevel[TheLine.Level] :\n"
4521                "               TheLine * 2,\n"
4522                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4523                Style);
4524   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4525                "                  aaaaaaaaaaaaaaa :\n"
4526                "                  bbbbbbbbbbbbbbb ? //\n"
4527                "                      ccccccccccccccc :\n"
4528                "                      ddddddddddddddd;",
4529                Style);
4530   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4531                "                  aaaaaaaaaaaaaaa :\n"
4532                "                  (bbbbbbbbbbbbbbb ? //\n"
4533                "                       ccccccccccccccc :\n"
4534                "                       ddddddddddddddd);",
4535                Style);
4536   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4537                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4538                "            ccccccccccccccccccccccccccc;",
4539                Style);
4540   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4541                "           aaaaa :\n"
4542                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4543                Style);
4544 }
4545 
4546 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4547   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4548                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4549   verifyFormat("bool a = true, b = false;");
4550 
4551   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4552                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4553                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4554                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4555   verifyFormat(
4556       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4557       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4558       "     d = e && f;");
4559   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4560                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4561   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4562                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4563   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4564                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4565 
4566   FormatStyle Style = getGoogleStyle();
4567   Style.PointerAlignment = FormatStyle::PAS_Left;
4568   Style.DerivePointerAlignment = false;
4569   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4570                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4571                "    *b = bbbbbbbbbbbbbbbbbbb;",
4572                Style);
4573   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4574                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4575                Style);
4576   verifyFormat("vector<int*> a, b;", Style);
4577   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4578 }
4579 
4580 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4581   verifyFormat("arr[foo ? bar : baz];");
4582   verifyFormat("f()[foo ? bar : baz];");
4583   verifyFormat("(a + b)[foo ? bar : baz];");
4584   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4585 }
4586 
4587 TEST_F(FormatTest, AlignsStringLiterals) {
4588   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4589                "                                      \"short literal\");");
4590   verifyFormat(
4591       "looooooooooooooooooooooooongFunction(\n"
4592       "    \"short literal\"\n"
4593       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4594   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4595                "             \" string literals\",\n"
4596                "             and, other, parameters);");
4597   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4598             "      \"5678\";",
4599             format("fun + \"1243\" /* comment */\n"
4600                    "    \"5678\";",
4601                    getLLVMStyleWithColumns(28)));
4602   EXPECT_EQ(
4603       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4604       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4605       "         \"aaaaaaaaaaaaaaaa\";",
4606       format("aaaaaa ="
4607              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4608              "aaaaaaaaaaaaaaaaaaaaa\" "
4609              "\"aaaaaaaaaaaaaaaa\";"));
4610   verifyFormat("a = a + \"a\"\n"
4611                "        \"a\"\n"
4612                "        \"a\";");
4613   verifyFormat("f(\"a\", \"b\"\n"
4614                "       \"c\");");
4615 
4616   verifyFormat(
4617       "#define LL_FORMAT \"ll\"\n"
4618       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4619       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4620 
4621   verifyFormat("#define A(X)          \\\n"
4622                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4623                "  \"ccccc\"",
4624                getLLVMStyleWithColumns(23));
4625   verifyFormat("#define A \"def\"\n"
4626                "f(\"abc\" A \"ghi\"\n"
4627                "  \"jkl\");");
4628 
4629   verifyFormat("f(L\"a\"\n"
4630                "  L\"b\");");
4631   verifyFormat("#define A(X)            \\\n"
4632                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4633                "  L\"ccccc\"",
4634                getLLVMStyleWithColumns(25));
4635 
4636   verifyFormat("f(@\"a\"\n"
4637                "  @\"b\");");
4638   verifyFormat("NSString s = @\"a\"\n"
4639                "             @\"b\"\n"
4640                "             @\"c\";");
4641   verifyFormat("NSString s = @\"a\"\n"
4642                "              \"b\"\n"
4643                "              \"c\";");
4644 }
4645 
4646 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4647   FormatStyle Style = getLLVMStyle();
4648   // No declarations or definitions should be moved to own line.
4649   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4650   verifyFormat("class A {\n"
4651                "  int f() { return 1; }\n"
4652                "  int g();\n"
4653                "};\n"
4654                "int f() { return 1; }\n"
4655                "int g();\n",
4656                Style);
4657 
4658   // All declarations and definitions should have the return type moved to its
4659   // own
4660   // line.
4661   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4662   verifyFormat("class E {\n"
4663                "  int\n"
4664                "  f() {\n"
4665                "    return 1;\n"
4666                "  }\n"
4667                "  int\n"
4668                "  g();\n"
4669                "};\n"
4670                "int\n"
4671                "f() {\n"
4672                "  return 1;\n"
4673                "}\n"
4674                "int\n"
4675                "g();\n",
4676                Style);
4677 
4678   // Top-level definitions, and no kinds of declarations should have the
4679   // return type moved to its own line.
4680   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4681   verifyFormat("class B {\n"
4682                "  int f() { return 1; }\n"
4683                "  int g();\n"
4684                "};\n"
4685                "int\n"
4686                "f() {\n"
4687                "  return 1;\n"
4688                "}\n"
4689                "int g();\n",
4690                Style);
4691 
4692   // Top-level definitions and declarations should have the return type moved
4693   // to its own line.
4694   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4695   verifyFormat("class C {\n"
4696                "  int f() { return 1; }\n"
4697                "  int g();\n"
4698                "};\n"
4699                "int\n"
4700                "f() {\n"
4701                "  return 1;\n"
4702                "}\n"
4703                "int\n"
4704                "g();\n",
4705                Style);
4706 
4707   // All definitions should have the return type moved to its own line, but no
4708   // kinds of declarations.
4709   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4710   verifyFormat("class D {\n"
4711                "  int\n"
4712                "  f() {\n"
4713                "    return 1;\n"
4714                "  }\n"
4715                "  int g();\n"
4716                "};\n"
4717                "int\n"
4718                "f() {\n"
4719                "  return 1;\n"
4720                "}\n"
4721                "int g();\n",
4722                Style);
4723   verifyFormat("const char *\n"
4724                "f(void) {\n" // Break here.
4725                "  return \"\";\n"
4726                "}\n"
4727                "const char *bar(void);\n", // No break here.
4728                Style);
4729   verifyFormat("template <class T>\n"
4730                "T *\n"
4731                "f(T &c) {\n" // Break here.
4732                "  return NULL;\n"
4733                "}\n"
4734                "template <class T> T *f(T &c);\n", // No break here.
4735                Style);
4736   verifyFormat("class C {\n"
4737                "  int\n"
4738                "  operator+() {\n"
4739                "    return 1;\n"
4740                "  }\n"
4741                "  int\n"
4742                "  operator()() {\n"
4743                "    return 1;\n"
4744                "  }\n"
4745                "};\n",
4746                Style);
4747   verifyFormat("void\n"
4748                "A::operator()() {}\n"
4749                "void\n"
4750                "A::operator>>() {}\n"
4751                "void\n"
4752                "A::operator+() {}\n",
4753                Style);
4754   verifyFormat("void *operator new(std::size_t s);", // No break here.
4755                Style);
4756   verifyFormat("void *\n"
4757                "operator new(std::size_t s) {}",
4758                Style);
4759   verifyFormat("void *\n"
4760                "operator delete[](void *ptr) {}",
4761                Style);
4762   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4763   verifyFormat("const char *\n"
4764                "f(void)\n" // Break here.
4765                "{\n"
4766                "  return \"\";\n"
4767                "}\n"
4768                "const char *bar(void);\n", // No break here.
4769                Style);
4770   verifyFormat("template <class T>\n"
4771                "T *\n"     // Problem here: no line break
4772                "f(T &c)\n" // Break here.
4773                "{\n"
4774                "  return NULL;\n"
4775                "}\n"
4776                "template <class T> T *f(T &c);\n", // No break here.
4777                Style);
4778 }
4779 
4780 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4781   FormatStyle NoBreak = getLLVMStyle();
4782   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4783   FormatStyle Break = getLLVMStyle();
4784   Break.AlwaysBreakBeforeMultilineStrings = true;
4785   verifyFormat("aaaa = \"bbbb\"\n"
4786                "       \"cccc\";",
4787                NoBreak);
4788   verifyFormat("aaaa =\n"
4789                "    \"bbbb\"\n"
4790                "    \"cccc\";",
4791                Break);
4792   verifyFormat("aaaa(\"bbbb\"\n"
4793                "     \"cccc\");",
4794                NoBreak);
4795   verifyFormat("aaaa(\n"
4796                "    \"bbbb\"\n"
4797                "    \"cccc\");",
4798                Break);
4799   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4800                "          \"cccc\");",
4801                NoBreak);
4802   verifyFormat("aaaa(qqq,\n"
4803                "     \"bbbb\"\n"
4804                "     \"cccc\");",
4805                Break);
4806   verifyFormat("aaaa(qqq,\n"
4807                "     L\"bbbb\"\n"
4808                "     L\"cccc\");",
4809                Break);
4810   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4811                "                      \"bbbb\"));",
4812                Break);
4813   verifyFormat("string s = someFunction(\n"
4814                "    \"abc\"\n"
4815                "    \"abc\");",
4816                Break);
4817 
4818   // As we break before unary operators, breaking right after them is bad.
4819   verifyFormat("string foo = abc ? \"x\"\n"
4820                "                   \"blah blah blah blah blah blah\"\n"
4821                "                 : \"y\";",
4822                Break);
4823 
4824   // Don't break if there is no column gain.
4825   verifyFormat("f(\"aaaa\"\n"
4826                "  \"bbbb\");",
4827                Break);
4828 
4829   // Treat literals with escaped newlines like multi-line string literals.
4830   EXPECT_EQ("x = \"a\\\n"
4831             "b\\\n"
4832             "c\";",
4833             format("x = \"a\\\n"
4834                    "b\\\n"
4835                    "c\";",
4836                    NoBreak));
4837   EXPECT_EQ("xxxx =\n"
4838             "    \"a\\\n"
4839             "b\\\n"
4840             "c\";",
4841             format("xxxx = \"a\\\n"
4842                    "b\\\n"
4843                    "c\";",
4844                    Break));
4845 
4846   EXPECT_EQ("NSString *const kString =\n"
4847             "    @\"aaaa\"\n"
4848             "    @\"bbbb\";",
4849             format("NSString *const kString = @\"aaaa\"\n"
4850                    "@\"bbbb\";",
4851                    Break));
4852 
4853   Break.ColumnLimit = 0;
4854   verifyFormat("const char *hello = \"hello llvm\";", Break);
4855 }
4856 
4857 TEST_F(FormatTest, AlignsPipes) {
4858   verifyFormat(
4859       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4860       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4861       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4862   verifyFormat(
4863       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4864       "                     << aaaaaaaaaaaaaaaaaaaa;");
4865   verifyFormat(
4866       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4867       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4868   verifyFormat(
4869       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4870       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4871   verifyFormat(
4872       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4873       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4874       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4875   verifyFormat(
4876       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4877       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4878       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4879   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4880                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4881                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4882                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4883   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4884                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4885   verifyFormat(
4886       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4887       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4888   verifyFormat(
4889       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
4890       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
4891 
4892   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4893                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4894   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4895                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4896                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4897                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4898   verifyFormat("LOG_IF(aaa == //\n"
4899                "       bbb)\n"
4900                "    << a << b;");
4901 
4902   // But sometimes, breaking before the first "<<" is desirable.
4903   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4904                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4905   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4906                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4907                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4908   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4909                "    << BEF << IsTemplate << Description << E->getType();");
4910   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4911                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4912                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4913   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4914                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4915                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4916                "    << aaa;");
4917 
4918   verifyFormat(
4919       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4920       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4921 
4922   // Incomplete string literal.
4923   EXPECT_EQ("llvm::errs() << \"\n"
4924             "             << a;",
4925             format("llvm::errs() << \"\n<<a;"));
4926 
4927   verifyFormat("void f() {\n"
4928                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4929                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4930                "}");
4931 
4932   // Handle 'endl'.
4933   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4934                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4935   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4936 
4937   // Handle '\n'.
4938   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4939                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4940   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4941                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4942   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4943                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4944   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4945 }
4946 
4947 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4948   verifyFormat("return out << \"somepacket = {\\n\"\n"
4949                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4950                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4951                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4952                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4953                "           << \"}\";");
4954 
4955   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4956                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4957                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4958   verifyFormat(
4959       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4960       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4961       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4962       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4963       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4964   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4965                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4966   verifyFormat(
4967       "void f() {\n"
4968       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4969       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4970       "}");
4971 
4972   // Breaking before the first "<<" is generally not desirable.
4973   verifyFormat(
4974       "llvm::errs()\n"
4975       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4976       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4977       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4978       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4979       getLLVMStyleWithColumns(70));
4980   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4981                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4982                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4983                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4984                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4985                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4986                getLLVMStyleWithColumns(70));
4987 
4988   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4989                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4990                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4991   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4992                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4993                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4994   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4995                "           (aaaa + aaaa);",
4996                getLLVMStyleWithColumns(40));
4997   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4998                "                  (aaaaaaa + aaaaa));",
4999                getLLVMStyleWithColumns(40));
5000   verifyFormat(
5001       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5002       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5003       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5004 }
5005 
5006 TEST_F(FormatTest, UnderstandsEquals) {
5007   verifyFormat(
5008       "aaaaaaaaaaaaaaaaa =\n"
5009       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5010   verifyFormat(
5011       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5012       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5013   verifyFormat(
5014       "if (a) {\n"
5015       "  f();\n"
5016       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5017       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5018       "}");
5019 
5020   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5021                "        100000000 + 10000000) {\n}");
5022 }
5023 
5024 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5025   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5026                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5027 
5028   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5029                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5030 
5031   verifyFormat(
5032       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5033       "                                                          Parameter2);");
5034 
5035   verifyFormat(
5036       "ShortObject->shortFunction(\n"
5037       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5038       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5039 
5040   verifyFormat("loooooooooooooongFunction(\n"
5041                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5042 
5043   verifyFormat(
5044       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5045       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5046 
5047   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5048                "    .WillRepeatedly(Return(SomeValue));");
5049   verifyFormat("void f() {\n"
5050                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5051                "      .Times(2)\n"
5052                "      .WillRepeatedly(Return(SomeValue));\n"
5053                "}");
5054   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5055                "    ccccccccccccccccccccccc);");
5056   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5057                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5058                "          .aaaaa(aaaaa),\n"
5059                "      aaaaaaaaaaaaaaaaaaaaa);");
5060   verifyFormat("void f() {\n"
5061                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5062                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5063                "}");
5064   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5065                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5066                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5067                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5068                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5069   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5070                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5071                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5072                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5073                "}");
5074 
5075   // Here, it is not necessary to wrap at "." or "->".
5076   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5077                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5078   verifyFormat(
5079       "aaaaaaaaaaa->aaaaaaaaa(\n"
5080       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5081       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5082 
5083   verifyFormat(
5084       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5085       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5086   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5087                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5088   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5089                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5090 
5091   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5092                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5093                "    .a();");
5094 
5095   FormatStyle NoBinPacking = getLLVMStyle();
5096   NoBinPacking.BinPackParameters = false;
5097   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5098                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5099                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5100                "                         aaaaaaaaaaaaaaaaaaa,\n"
5101                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5102                NoBinPacking);
5103 
5104   // If there is a subsequent call, change to hanging indentation.
5105   verifyFormat(
5106       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5107       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5108       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5109   verifyFormat(
5110       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5111       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5112   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5113                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5114                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5115   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5116                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5117                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5118 }
5119 
5120 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5121   verifyFormat("template <typename T>\n"
5122                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5123   verifyFormat("template <typename T>\n"
5124                "// T should be one of {A, B}.\n"
5125                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5126   verifyFormat(
5127       "template <typename T>\n"
5128       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5129   verifyFormat("template <typename T>\n"
5130                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5131                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5132   verifyFormat(
5133       "template <typename T>\n"
5134       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5135       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5136   verifyFormat(
5137       "template <typename T>\n"
5138       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5139       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5140       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5141   verifyFormat("template <typename T>\n"
5142                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5143                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5144   verifyFormat(
5145       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5146       "          typename T4 = char>\n"
5147       "void f();");
5148   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5149                "          template <typename> class cccccccccccccccccccccc,\n"
5150                "          typename ddddddddddddd>\n"
5151                "class C {};");
5152   verifyFormat(
5153       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5154       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5155 
5156   verifyFormat("void f() {\n"
5157                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5158                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5159                "}");
5160 
5161   verifyFormat("template <typename T> class C {};");
5162   verifyFormat("template <typename T> void f();");
5163   verifyFormat("template <typename T> void f() {}");
5164   verifyFormat(
5165       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5166       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5167       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5168       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5169       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5170       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5171       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5172       getLLVMStyleWithColumns(72));
5173   EXPECT_EQ("static_cast<A< //\n"
5174             "    B> *>(\n"
5175             "\n"
5176             ");",
5177             format("static_cast<A<//\n"
5178                    "    B>*>(\n"
5179                    "\n"
5180                    "    );"));
5181   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5182                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5183 
5184   FormatStyle AlwaysBreak = getLLVMStyle();
5185   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5186   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5187   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5188   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5189   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5190                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5191                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5192   verifyFormat("template <template <typename> class Fooooooo,\n"
5193                "          template <typename> class Baaaaaaar>\n"
5194                "struct C {};",
5195                AlwaysBreak);
5196   verifyFormat("template <typename T> // T can be A, B or C.\n"
5197                "struct C {};",
5198                AlwaysBreak);
5199   verifyFormat("template <enum E> class A {\n"
5200                "public:\n"
5201                "  E *f();\n"
5202                "};");
5203 }
5204 
5205 TEST_F(FormatTest, WrapsTemplateParameters) {
5206   FormatStyle Style = getLLVMStyle();
5207   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5208   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5209   verifyFormat(
5210       "template <typename... a> struct q {};\n"
5211       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5212       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5213       "    y;",
5214       Style);
5215   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5216   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5217   verifyFormat(
5218       "template <typename... a> struct r {};\n"
5219       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5220       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5221       "    y;",
5222       Style);
5223   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5224   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5225   verifyFormat(
5226       "template <typename... a> struct s {};\n"
5227       "extern s<\n"
5228       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5229       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5230       "    y;",
5231       Style);
5232   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5233   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5234   verifyFormat(
5235       "template <typename... a> struct t {};\n"
5236       "extern t<\n"
5237       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5238       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5239       "    y;",
5240       Style);
5241 }
5242 
5243 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5244   verifyFormat(
5245       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5246       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5247   verifyFormat(
5248       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5249       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5250       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5251 
5252   // FIXME: Should we have the extra indent after the second break?
5253   verifyFormat(
5254       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5255       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5256       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5257 
5258   verifyFormat(
5259       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5260       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5261 
5262   // Breaking at nested name specifiers is generally not desirable.
5263   verifyFormat(
5264       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5265       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5266 
5267   verifyFormat(
5268       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5269       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5270       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5271       "                   aaaaaaaaaaaaaaaaaaaaa);",
5272       getLLVMStyleWithColumns(74));
5273 
5274   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5275                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5276                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5277 }
5278 
5279 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5280   verifyFormat("A<int> a;");
5281   verifyFormat("A<A<A<int>>> a;");
5282   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5283   verifyFormat("bool x = a < 1 || 2 > a;");
5284   verifyFormat("bool x = 5 < f<int>();");
5285   verifyFormat("bool x = f<int>() > 5;");
5286   verifyFormat("bool x = 5 < a<int>::x;");
5287   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5288   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5289 
5290   verifyGoogleFormat("A<A<int>> a;");
5291   verifyGoogleFormat("A<A<A<int>>> a;");
5292   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5293   verifyGoogleFormat("A<A<int> > a;");
5294   verifyGoogleFormat("A<A<A<int> > > a;");
5295   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5296   verifyGoogleFormat("A<::A<int>> a;");
5297   verifyGoogleFormat("A<::A> a;");
5298   verifyGoogleFormat("A< ::A> a;");
5299   verifyGoogleFormat("A< ::A<int> > a;");
5300   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5301   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5302   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5303   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5304   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5305             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5306 
5307   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5308 
5309   verifyFormat("test >> a >> b;");
5310   verifyFormat("test << a >> b;");
5311 
5312   verifyFormat("f<int>();");
5313   verifyFormat("template <typename T> void f() {}");
5314   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5315   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5316                "sizeof(char)>::type>;");
5317   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5318   verifyFormat("f(a.operator()<A>());");
5319   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5320                "      .template operator()<A>());",
5321                getLLVMStyleWithColumns(35));
5322 
5323   // Not template parameters.
5324   verifyFormat("return a < b && c > d;");
5325   verifyFormat("void f() {\n"
5326                "  while (a < b && c > d) {\n"
5327                "  }\n"
5328                "}");
5329   verifyFormat("template <typename... Types>\n"
5330                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5331 
5332   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5333                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5334                getLLVMStyleWithColumns(60));
5335   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5336   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5337   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5338 }
5339 
5340 TEST_F(FormatTest, BitshiftOperatorWidth) {
5341   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5342             "                   bar */",
5343             format("int    a=1<<2;  /* foo\n"
5344                    "                   bar */"));
5345 
5346   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5347             "                     bar */",
5348             format("int  b  =256>>1 ;  /* foo\n"
5349                    "                      bar */"));
5350 }
5351 
5352 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5353   verifyFormat("COMPARE(a, ==, b);");
5354   verifyFormat("auto s = sizeof...(Ts) - 1;");
5355 }
5356 
5357 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5358   verifyFormat("int A::*x;");
5359   verifyFormat("int (S::*func)(void *);");
5360   verifyFormat("void f() { int (S::*func)(void *); }");
5361   verifyFormat("typedef bool *(Class::*Member)() const;");
5362   verifyFormat("void f() {\n"
5363                "  (a->*f)();\n"
5364                "  a->*x;\n"
5365                "  (a.*f)();\n"
5366                "  ((*a).*f)();\n"
5367                "  a.*x;\n"
5368                "}");
5369   verifyFormat("void f() {\n"
5370                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5371                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5372                "}");
5373   verifyFormat(
5374       "(aaaaaaaaaa->*bbbbbbb)(\n"
5375       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5376   FormatStyle Style = getLLVMStyle();
5377   Style.PointerAlignment = FormatStyle::PAS_Left;
5378   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5379 }
5380 
5381 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5382   verifyFormat("int a = -2;");
5383   verifyFormat("f(-1, -2, -3);");
5384   verifyFormat("a[-1] = 5;");
5385   verifyFormat("int a = 5 + -2;");
5386   verifyFormat("if (i == -1) {\n}");
5387   verifyFormat("if (i != -1) {\n}");
5388   verifyFormat("if (i > -1) {\n}");
5389   verifyFormat("if (i < -1) {\n}");
5390   verifyFormat("++(a->f());");
5391   verifyFormat("--(a->f());");
5392   verifyFormat("(a->f())++;");
5393   verifyFormat("a[42]++;");
5394   verifyFormat("if (!(a->f())) {\n}");
5395 
5396   verifyFormat("a-- > b;");
5397   verifyFormat("b ? -a : c;");
5398   verifyFormat("n * sizeof char16;");
5399   verifyFormat("n * alignof char16;", getGoogleStyle());
5400   verifyFormat("sizeof(char);");
5401   verifyFormat("alignof(char);", getGoogleStyle());
5402 
5403   verifyFormat("return -1;");
5404   verifyFormat("switch (a) {\n"
5405                "case -1:\n"
5406                "  break;\n"
5407                "}");
5408   verifyFormat("#define X -1");
5409   verifyFormat("#define X -kConstant");
5410 
5411   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5412   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5413 
5414   verifyFormat("int a = /* confusing comment */ -1;");
5415   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5416   verifyFormat("int a = i /* confusing comment */++;");
5417 }
5418 
5419 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5420   verifyFormat("if (!aaaaaaaaaa( // break\n"
5421                "        aaaaa)) {\n"
5422                "}");
5423   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5424                "    aaaaa));");
5425   verifyFormat("*aaa = aaaaaaa( // break\n"
5426                "    bbbbbb);");
5427 }
5428 
5429 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5430   verifyFormat("bool operator<();");
5431   verifyFormat("bool operator>();");
5432   verifyFormat("bool operator=();");
5433   verifyFormat("bool operator==();");
5434   verifyFormat("bool operator!=();");
5435   verifyFormat("int operator+();");
5436   verifyFormat("int operator++();");
5437   verifyFormat("bool operator,();");
5438   verifyFormat("bool operator();");
5439   verifyFormat("bool operator()();");
5440   verifyFormat("bool operator[]();");
5441   verifyFormat("operator bool();");
5442   verifyFormat("operator int();");
5443   verifyFormat("operator void *();");
5444   verifyFormat("operator SomeType<int>();");
5445   verifyFormat("operator SomeType<int, int>();");
5446   verifyFormat("operator SomeType<SomeType<int>>();");
5447   verifyFormat("void *operator new(std::size_t size);");
5448   verifyFormat("void *operator new[](std::size_t size);");
5449   verifyFormat("void operator delete(void *ptr);");
5450   verifyFormat("void operator delete[](void *ptr);");
5451   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5452                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5453   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5454                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5455 
5456   verifyFormat(
5457       "ostream &operator<<(ostream &OutputStream,\n"
5458       "                    SomeReallyLongType WithSomeReallyLongValue);");
5459   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5460                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5461                "  return left.group < right.group;\n"
5462                "}");
5463   verifyFormat("SomeType &operator=(const SomeType &S);");
5464   verifyFormat("f.template operator()<int>();");
5465 
5466   verifyGoogleFormat("operator void*();");
5467   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5468   verifyGoogleFormat("operator ::A();");
5469 
5470   verifyFormat("using A::operator+;");
5471   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5472                "int i;");
5473 }
5474 
5475 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5476   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5477   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5478   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5479   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5480   verifyFormat("Deleted &operator=(const Deleted &) &;");
5481   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5482   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5483   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5484   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5485   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5486   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5487   verifyFormat("void Fn(T const &) const &;");
5488   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5489   verifyFormat("template <typename T>\n"
5490                "void F(T) && = delete;",
5491                getGoogleStyle());
5492 
5493   FormatStyle AlignLeft = getLLVMStyle();
5494   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5495   verifyFormat("void A::b() && {}", AlignLeft);
5496   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5497   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5498                AlignLeft);
5499   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5500   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5501   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5502   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5503   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5504   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5505   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5506   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5507 
5508   FormatStyle Spaces = getLLVMStyle();
5509   Spaces.SpacesInCStyleCastParentheses = true;
5510   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5511   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5512   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5513   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5514 
5515   Spaces.SpacesInCStyleCastParentheses = false;
5516   Spaces.SpacesInParentheses = true;
5517   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5518   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5519   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5520   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5521 }
5522 
5523 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5524   verifyFormat("void f() {\n"
5525                "  A *a = new A;\n"
5526                "  A *a = new (placement) A;\n"
5527                "  delete a;\n"
5528                "  delete (A *)a;\n"
5529                "}");
5530   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5531                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5532   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5533                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5534                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5535   verifyFormat("delete[] h->p;");
5536 }
5537 
5538 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5539   verifyFormat("int *f(int *a) {}");
5540   verifyFormat("int main(int argc, char **argv) {}");
5541   verifyFormat("Test::Test(int b) : a(b * b) {}");
5542   verifyIndependentOfContext("f(a, *a);");
5543   verifyFormat("void g() { f(*a); }");
5544   verifyIndependentOfContext("int a = b * 10;");
5545   verifyIndependentOfContext("int a = 10 * b;");
5546   verifyIndependentOfContext("int a = b * c;");
5547   verifyIndependentOfContext("int a += b * c;");
5548   verifyIndependentOfContext("int a -= b * c;");
5549   verifyIndependentOfContext("int a *= b * c;");
5550   verifyIndependentOfContext("int a /= b * c;");
5551   verifyIndependentOfContext("int a = *b;");
5552   verifyIndependentOfContext("int a = *b * c;");
5553   verifyIndependentOfContext("int a = b * *c;");
5554   verifyIndependentOfContext("int a = b * (10);");
5555   verifyIndependentOfContext("S << b * (10);");
5556   verifyIndependentOfContext("return 10 * b;");
5557   verifyIndependentOfContext("return *b * *c;");
5558   verifyIndependentOfContext("return a & ~b;");
5559   verifyIndependentOfContext("f(b ? *c : *d);");
5560   verifyIndependentOfContext("int a = b ? *c : *d;");
5561   verifyIndependentOfContext("*b = a;");
5562   verifyIndependentOfContext("a * ~b;");
5563   verifyIndependentOfContext("a * !b;");
5564   verifyIndependentOfContext("a * +b;");
5565   verifyIndependentOfContext("a * -b;");
5566   verifyIndependentOfContext("a * ++b;");
5567   verifyIndependentOfContext("a * --b;");
5568   verifyIndependentOfContext("a[4] * b;");
5569   verifyIndependentOfContext("a[a * a] = 1;");
5570   verifyIndependentOfContext("f() * b;");
5571   verifyIndependentOfContext("a * [self dostuff];");
5572   verifyIndependentOfContext("int x = a * (a + b);");
5573   verifyIndependentOfContext("(a *)(a + b);");
5574   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5575   verifyIndependentOfContext("int *pa = (int *)&a;");
5576   verifyIndependentOfContext("return sizeof(int **);");
5577   verifyIndependentOfContext("return sizeof(int ******);");
5578   verifyIndependentOfContext("return (int **&)a;");
5579   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5580   verifyFormat("void f(Type (*parameter)[10]) {}");
5581   verifyFormat("void f(Type (&parameter)[10]) {}");
5582   verifyGoogleFormat("return sizeof(int**);");
5583   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5584   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5585   verifyFormat("auto a = [](int **&, int ***) {};");
5586   verifyFormat("auto PointerBinding = [](const char *S) {};");
5587   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5588   verifyFormat("[](const decltype(*a) &value) {}");
5589   verifyFormat("decltype(a * b) F();");
5590   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5591   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5592   verifyIndependentOfContext("typedef void (*f)(int *a);");
5593   verifyIndependentOfContext("int i{a * b};");
5594   verifyIndependentOfContext("aaa && aaa->f();");
5595   verifyIndependentOfContext("int x = ~*p;");
5596   verifyFormat("Constructor() : a(a), area(width * height) {}");
5597   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5598   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5599   verifyFormat("void f() { f(a, c * d); }");
5600   verifyFormat("void f() { f(new a(), c * d); }");
5601   verifyFormat("void f(const MyOverride &override);");
5602   verifyFormat("void f(const MyFinal &final);");
5603   verifyIndependentOfContext("bool a = f() && override.f();");
5604   verifyIndependentOfContext("bool a = f() && final.f();");
5605 
5606   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5607 
5608   verifyIndependentOfContext("A<int *> a;");
5609   verifyIndependentOfContext("A<int **> a;");
5610   verifyIndependentOfContext("A<int *, int *> a;");
5611   verifyIndependentOfContext("A<int *[]> a;");
5612   verifyIndependentOfContext(
5613       "const char *const p = reinterpret_cast<const char *const>(q);");
5614   verifyIndependentOfContext("A<int **, int **> a;");
5615   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5616   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5617   verifyFormat("for (; a && b;) {\n}");
5618   verifyFormat("bool foo = true && [] { return false; }();");
5619 
5620   verifyFormat(
5621       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5622       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5623 
5624   verifyGoogleFormat("int const* a = &b;");
5625   verifyGoogleFormat("**outparam = 1;");
5626   verifyGoogleFormat("*outparam = a * b;");
5627   verifyGoogleFormat("int main(int argc, char** argv) {}");
5628   verifyGoogleFormat("A<int*> a;");
5629   verifyGoogleFormat("A<int**> a;");
5630   verifyGoogleFormat("A<int*, int*> a;");
5631   verifyGoogleFormat("A<int**, int**> a;");
5632   verifyGoogleFormat("f(b ? *c : *d);");
5633   verifyGoogleFormat("int a = b ? *c : *d;");
5634   verifyGoogleFormat("Type* t = **x;");
5635   verifyGoogleFormat("Type* t = *++*x;");
5636   verifyGoogleFormat("*++*x;");
5637   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5638   verifyGoogleFormat("Type* t = x++ * y;");
5639   verifyGoogleFormat(
5640       "const char* const p = reinterpret_cast<const char* const>(q);");
5641   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5642   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5643   verifyGoogleFormat("template <typename T>\n"
5644                      "void f(int i = 0, SomeType** temps = NULL);");
5645 
5646   FormatStyle Left = getLLVMStyle();
5647   Left.PointerAlignment = FormatStyle::PAS_Left;
5648   verifyFormat("x = *a(x) = *a(y);", Left);
5649   verifyFormat("for (;; *a = b) {\n}", Left);
5650   verifyFormat("return *this += 1;", Left);
5651   verifyFormat("throw *x;", Left);
5652   verifyFormat("delete *x;", Left);
5653   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
5654   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
5655   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
5656 
5657   verifyIndependentOfContext("a = *(x + y);");
5658   verifyIndependentOfContext("a = &(x + y);");
5659   verifyIndependentOfContext("*(x + y).call();");
5660   verifyIndependentOfContext("&(x + y)->call();");
5661   verifyFormat("void f() { &(*I).first; }");
5662 
5663   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5664   verifyFormat(
5665       "int *MyValues = {\n"
5666       "    *A, // Operator detection might be confused by the '{'\n"
5667       "    *BB // Operator detection might be confused by previous comment\n"
5668       "};");
5669 
5670   verifyIndependentOfContext("if (int *a = &b)");
5671   verifyIndependentOfContext("if (int &a = *b)");
5672   verifyIndependentOfContext("if (a & b[i])");
5673   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5674   verifyIndependentOfContext("if (*b[i])");
5675   verifyIndependentOfContext("if (int *a = (&b))");
5676   verifyIndependentOfContext("while (int *a = &b)");
5677   verifyIndependentOfContext("size = sizeof *a;");
5678   verifyIndependentOfContext("if (a && (b = c))");
5679   verifyFormat("void f() {\n"
5680                "  for (const int &v : Values) {\n"
5681                "  }\n"
5682                "}");
5683   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5684   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5685   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5686 
5687   verifyFormat("#define A (!a * b)");
5688   verifyFormat("#define MACRO     \\\n"
5689                "  int *i = a * b; \\\n"
5690                "  void f(a *b);",
5691                getLLVMStyleWithColumns(19));
5692 
5693   verifyIndependentOfContext("A = new SomeType *[Length];");
5694   verifyIndependentOfContext("A = new SomeType *[Length]();");
5695   verifyIndependentOfContext("T **t = new T *;");
5696   verifyIndependentOfContext("T **t = new T *();");
5697   verifyGoogleFormat("A = new SomeType*[Length]();");
5698   verifyGoogleFormat("A = new SomeType*[Length];");
5699   verifyGoogleFormat("T** t = new T*;");
5700   verifyGoogleFormat("T** t = new T*();");
5701 
5702   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5703   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5704   verifyFormat("template <bool a, bool b> "
5705                "typename t::if<x && y>::type f() {}");
5706   verifyFormat("template <int *y> f() {}");
5707   verifyFormat("vector<int *> v;");
5708   verifyFormat("vector<int *const> v;");
5709   verifyFormat("vector<int *const **const *> v;");
5710   verifyFormat("vector<int *volatile> v;");
5711   verifyFormat("vector<a * b> v;");
5712   verifyFormat("foo<b && false>();");
5713   verifyFormat("foo<b & 1>();");
5714   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5715   verifyFormat(
5716       "template <class T, class = typename std::enable_if<\n"
5717       "                       std::is_integral<T>::value &&\n"
5718       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5719       "void F();",
5720       getLLVMStyleWithColumns(70));
5721   verifyFormat(
5722       "template <class T,\n"
5723       "          class = typename std::enable_if<\n"
5724       "              std::is_integral<T>::value &&\n"
5725       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5726       "          class U>\n"
5727       "void F();",
5728       getLLVMStyleWithColumns(70));
5729   verifyFormat(
5730       "template <class T,\n"
5731       "          class = typename ::std::enable_if<\n"
5732       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5733       "void F();",
5734       getGoogleStyleWithColumns(68));
5735 
5736   verifyIndependentOfContext("MACRO(int *i);");
5737   verifyIndependentOfContext("MACRO(auto *a);");
5738   verifyIndependentOfContext("MACRO(const A *a);");
5739   verifyIndependentOfContext("MACRO(A *const a);");
5740   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5741   verifyFormat("void f() { f(float{1}, a * a); }");
5742   // FIXME: Is there a way to make this work?
5743   // verifyIndependentOfContext("MACRO(A *a);");
5744 
5745   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5746   verifyFormat("return options != nullptr && operator==(*options);");
5747 
5748   EXPECT_EQ("#define OP(x)                                    \\\n"
5749             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5750             "    return s << a.DebugString();                 \\\n"
5751             "  }",
5752             format("#define OP(x) \\\n"
5753                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5754                    "    return s << a.DebugString(); \\\n"
5755                    "  }",
5756                    getLLVMStyleWithColumns(50)));
5757 
5758   // FIXME: We cannot handle this case yet; we might be able to figure out that
5759   // foo<x> d > v; doesn't make sense.
5760   verifyFormat("foo<a<b && c> d> v;");
5761 
5762   FormatStyle PointerMiddle = getLLVMStyle();
5763   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5764   verifyFormat("delete *x;", PointerMiddle);
5765   verifyFormat("int * x;", PointerMiddle);
5766   verifyFormat("template <int * y> f() {}", PointerMiddle);
5767   verifyFormat("int * f(int * a) {}", PointerMiddle);
5768   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5769   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5770   verifyFormat("A<int *> a;", PointerMiddle);
5771   verifyFormat("A<int **> a;", PointerMiddle);
5772   verifyFormat("A<int *, int *> a;", PointerMiddle);
5773   verifyFormat("A<int * []> a;", PointerMiddle);
5774   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5775   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5776   verifyFormat("T ** t = new T *;", PointerMiddle);
5777 
5778   // Member function reference qualifiers aren't binary operators.
5779   verifyFormat("string // break\n"
5780                "operator()() & {}");
5781   verifyFormat("string // break\n"
5782                "operator()() && {}");
5783   verifyGoogleFormat("template <typename T>\n"
5784                      "auto x() & -> int {}");
5785 }
5786 
5787 TEST_F(FormatTest, UnderstandsAttributes) {
5788   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5789   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5790                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5791   FormatStyle AfterType = getLLVMStyle();
5792   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5793   verifyFormat("__attribute__((nodebug)) void\n"
5794                "foo() {}\n",
5795                AfterType);
5796 }
5797 
5798 TEST_F(FormatTest, UnderstandsEllipsis) {
5799   verifyFormat("int printf(const char *fmt, ...);");
5800   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5801   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5802 
5803   FormatStyle PointersLeft = getLLVMStyle();
5804   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5805   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5806 }
5807 
5808 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5809   EXPECT_EQ("int *a;\n"
5810             "int *a;\n"
5811             "int *a;",
5812             format("int *a;\n"
5813                    "int* a;\n"
5814                    "int *a;",
5815                    getGoogleStyle()));
5816   EXPECT_EQ("int* a;\n"
5817             "int* a;\n"
5818             "int* a;",
5819             format("int* a;\n"
5820                    "int* a;\n"
5821                    "int *a;",
5822                    getGoogleStyle()));
5823   EXPECT_EQ("int *a;\n"
5824             "int *a;\n"
5825             "int *a;",
5826             format("int *a;\n"
5827                    "int * a;\n"
5828                    "int *  a;",
5829                    getGoogleStyle()));
5830   EXPECT_EQ("auto x = [] {\n"
5831             "  int *a;\n"
5832             "  int *a;\n"
5833             "  int *a;\n"
5834             "};",
5835             format("auto x=[]{int *a;\n"
5836                    "int * a;\n"
5837                    "int *  a;};",
5838                    getGoogleStyle()));
5839 }
5840 
5841 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5842   verifyFormat("int f(int &&a) {}");
5843   verifyFormat("int f(int a, char &&b) {}");
5844   verifyFormat("void f() { int &&a = b; }");
5845   verifyGoogleFormat("int f(int a, char&& b) {}");
5846   verifyGoogleFormat("void f() { int&& a = b; }");
5847 
5848   verifyIndependentOfContext("A<int &&> a;");
5849   verifyIndependentOfContext("A<int &&, int &&> a;");
5850   verifyGoogleFormat("A<int&&> a;");
5851   verifyGoogleFormat("A<int&&, int&&> a;");
5852 
5853   // Not rvalue references:
5854   verifyFormat("template <bool B, bool C> class A {\n"
5855                "  static_assert(B && C, \"Something is wrong\");\n"
5856                "};");
5857   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5858   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5859   verifyFormat("#define A(a, b) (a && b)");
5860 }
5861 
5862 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5863   verifyFormat("void f() {\n"
5864                "  x[aaaaaaaaa -\n"
5865                "    b] = 23;\n"
5866                "}",
5867                getLLVMStyleWithColumns(15));
5868 }
5869 
5870 TEST_F(FormatTest, FormatsCasts) {
5871   verifyFormat("Type *A = static_cast<Type *>(P);");
5872   verifyFormat("Type *A = (Type *)P;");
5873   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5874   verifyFormat("int a = (int)(2.0f);");
5875   verifyFormat("int a = (int)2.0f;");
5876   verifyFormat("x[(int32)y];");
5877   verifyFormat("x = (int32)y;");
5878   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5879   verifyFormat("int a = (int)*b;");
5880   verifyFormat("int a = (int)2.0f;");
5881   verifyFormat("int a = (int)~0;");
5882   verifyFormat("int a = (int)++a;");
5883   verifyFormat("int a = (int)sizeof(int);");
5884   verifyFormat("int a = (int)+2;");
5885   verifyFormat("my_int a = (my_int)2.0f;");
5886   verifyFormat("my_int a = (my_int)sizeof(int);");
5887   verifyFormat("return (my_int)aaa;");
5888   verifyFormat("#define x ((int)-1)");
5889   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5890   verifyFormat("#define p(q) ((int *)&q)");
5891   verifyFormat("fn(a)(b) + 1;");
5892 
5893   verifyFormat("void f() { my_int a = (my_int)*b; }");
5894   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5895   verifyFormat("my_int a = (my_int)~0;");
5896   verifyFormat("my_int a = (my_int)++a;");
5897   verifyFormat("my_int a = (my_int)-2;");
5898   verifyFormat("my_int a = (my_int)1;");
5899   verifyFormat("my_int a = (my_int *)1;");
5900   verifyFormat("my_int a = (const my_int)-1;");
5901   verifyFormat("my_int a = (const my_int *)-1;");
5902   verifyFormat("my_int a = (my_int)(my_int)-1;");
5903   verifyFormat("my_int a = (ns::my_int)-2;");
5904   verifyFormat("case (my_int)ONE:");
5905   verifyFormat("auto x = (X)this;");
5906 
5907   // FIXME: single value wrapped with paren will be treated as cast.
5908   verifyFormat("void f(int i = (kValue)*kMask) {}");
5909 
5910   verifyFormat("{ (void)F; }");
5911 
5912   // Don't break after a cast's
5913   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5914                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5915                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5916 
5917   // These are not casts.
5918   verifyFormat("void f(int *) {}");
5919   verifyFormat("f(foo)->b;");
5920   verifyFormat("f(foo).b;");
5921   verifyFormat("f(foo)(b);");
5922   verifyFormat("f(foo)[b];");
5923   verifyFormat("[](foo) { return 4; }(bar);");
5924   verifyFormat("(*funptr)(foo)[4];");
5925   verifyFormat("funptrs[4](foo)[4];");
5926   verifyFormat("void f(int *);");
5927   verifyFormat("void f(int *) = 0;");
5928   verifyFormat("void f(SmallVector<int>) {}");
5929   verifyFormat("void f(SmallVector<int>);");
5930   verifyFormat("void f(SmallVector<int>) = 0;");
5931   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5932   verifyFormat("int a = sizeof(int) * b;");
5933   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5934   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5935   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5936   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5937 
5938   // These are not casts, but at some point were confused with casts.
5939   verifyFormat("virtual void foo(int *) override;");
5940   verifyFormat("virtual void foo(char &) const;");
5941   verifyFormat("virtual void foo(int *a, char *) const;");
5942   verifyFormat("int a = sizeof(int *) + b;");
5943   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5944   verifyFormat("bool b = f(g<int>) && c;");
5945   verifyFormat("typedef void (*f)(int i) func;");
5946 
5947   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5948                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5949   // FIXME: The indentation here is not ideal.
5950   verifyFormat(
5951       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5952       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5953       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5954 }
5955 
5956 TEST_F(FormatTest, FormatsFunctionTypes) {
5957   verifyFormat("A<bool()> a;");
5958   verifyFormat("A<SomeType()> a;");
5959   verifyFormat("A<void (*)(int, std::string)> a;");
5960   verifyFormat("A<void *(int)>;");
5961   verifyFormat("void *(*a)(int *, SomeType *);");
5962   verifyFormat("int (*func)(void *);");
5963   verifyFormat("void f() { int (*func)(void *); }");
5964   verifyFormat("template <class CallbackClass>\n"
5965                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5966 
5967   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5968   verifyGoogleFormat("void* (*a)(int);");
5969   verifyGoogleFormat(
5970       "template <class CallbackClass>\n"
5971       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5972 
5973   // Other constructs can look somewhat like function types:
5974   verifyFormat("A<sizeof(*x)> a;");
5975   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5976   verifyFormat("some_var = function(*some_pointer_var)[0];");
5977   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5978   verifyFormat("int x = f(&h)();");
5979   verifyFormat("returnsFunction(&param1, &param2)(param);");
5980   verifyFormat("std::function<\n"
5981                "    LooooooooooongTemplatedType<\n"
5982                "        SomeType>*(\n"
5983                "        LooooooooooooooooongType type)>\n"
5984                "    function;",
5985                getGoogleStyleWithColumns(40));
5986 }
5987 
5988 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5989   verifyFormat("A (*foo_)[6];");
5990   verifyFormat("vector<int> (*foo_)[6];");
5991 }
5992 
5993 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5994   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5995                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5996   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5997                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5998   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5999                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6000 
6001   // Different ways of ()-initializiation.
6002   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6003                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6004   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6005                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6006   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6007                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6008   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6009                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6010 
6011   // Lambdas should not confuse the variable declaration heuristic.
6012   verifyFormat("LooooooooooooooooongType\n"
6013                "    variable(nullptr, [](A *a) {});",
6014                getLLVMStyleWithColumns(40));
6015 }
6016 
6017 TEST_F(FormatTest, BreaksLongDeclarations) {
6018   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6019                "    AnotherNameForTheLongType;");
6020   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6021                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6022   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6023                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6024   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6025                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6026   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6027                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6028   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6029                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6030   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6031                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6032   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6033                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6034   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6035                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6036   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6037                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6038   FormatStyle Indented = getLLVMStyle();
6039   Indented.IndentWrappedFunctionNames = true;
6040   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6041                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6042                Indented);
6043   verifyFormat(
6044       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6045       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6046       Indented);
6047   verifyFormat(
6048       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6049       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6050       Indented);
6051   verifyFormat(
6052       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6053       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6054       Indented);
6055 
6056   // FIXME: Without the comment, this breaks after "(".
6057   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6058                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6059                getGoogleStyle());
6060 
6061   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6062                "                  int LoooooooooooooooooooongParam2) {}");
6063   verifyFormat(
6064       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6065       "                                   SourceLocation L, IdentifierIn *II,\n"
6066       "                                   Type *T) {}");
6067   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6068                "ReallyReaaallyLongFunctionName(\n"
6069                "    const std::string &SomeParameter,\n"
6070                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6071                "        &ReallyReallyLongParameterName,\n"
6072                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6073                "        &AnotherLongParameterName) {}");
6074   verifyFormat("template <typename A>\n"
6075                "SomeLoooooooooooooooooooooongType<\n"
6076                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6077                "Function() {}");
6078 
6079   verifyGoogleFormat(
6080       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6081       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6082   verifyGoogleFormat(
6083       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6084       "                                   SourceLocation L) {}");
6085   verifyGoogleFormat(
6086       "some_namespace::LongReturnType\n"
6087       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6088       "    int first_long_parameter, int second_parameter) {}");
6089 
6090   verifyGoogleFormat("template <typename T>\n"
6091                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6092                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6093   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6094                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6095 
6096   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6097                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6098                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6099   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6100                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6101                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6102   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6103                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6104                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6105                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6106 
6107   verifyFormat("template <typename T> // Templates on own line.\n"
6108                "static int            // Some comment.\n"
6109                "MyFunction(int a);",
6110                getLLVMStyle());
6111 }
6112 
6113 TEST_F(FormatTest, FormatsArrays) {
6114   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6115                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6116   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6117                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6118   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6119                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6120   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6121                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6122   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6123                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6124   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6125                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6126                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6127   verifyFormat(
6128       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6129       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6130       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6131   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6132                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6133 
6134   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6135                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6136   verifyFormat(
6137       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6138       "                                  .aaaaaaa[0]\n"
6139       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6140   verifyFormat("a[::b::c];");
6141 
6142   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6143 
6144   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6145   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6146 }
6147 
6148 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6149   verifyFormat("(a)->b();");
6150   verifyFormat("--a;");
6151 }
6152 
6153 TEST_F(FormatTest, HandlesIncludeDirectives) {
6154   verifyFormat("#include <string>\n"
6155                "#include <a/b/c.h>\n"
6156                "#include \"a/b/string\"\n"
6157                "#include \"string.h\"\n"
6158                "#include \"string.h\"\n"
6159                "#include <a-a>\n"
6160                "#include < path with space >\n"
6161                "#include_next <test.h>"
6162                "#include \"abc.h\" // this is included for ABC\n"
6163                "#include \"some long include\" // with a comment\n"
6164                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6165                getLLVMStyleWithColumns(35));
6166   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6167   EXPECT_EQ("#include <a>", format("#include<a>"));
6168 
6169   verifyFormat("#import <string>");
6170   verifyFormat("#import <a/b/c.h>");
6171   verifyFormat("#import \"a/b/string\"");
6172   verifyFormat("#import \"string.h\"");
6173   verifyFormat("#import \"string.h\"");
6174   verifyFormat("#if __has_include(<strstream>)\n"
6175                "#include <strstream>\n"
6176                "#endif");
6177 
6178   verifyFormat("#define MY_IMPORT <a/b>");
6179 
6180   verifyFormat("#if __has_include(<a/b>)");
6181   verifyFormat("#if __has_include_next(<a/b>)");
6182   verifyFormat("#define F __has_include(<a/b>)");
6183   verifyFormat("#define F __has_include_next(<a/b>)");
6184 
6185   // Protocol buffer definition or missing "#".
6186   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6187                getLLVMStyleWithColumns(30));
6188 
6189   FormatStyle Style = getLLVMStyle();
6190   Style.AlwaysBreakBeforeMultilineStrings = true;
6191   Style.ColumnLimit = 0;
6192   verifyFormat("#import \"abc.h\"", Style);
6193 
6194   // But 'import' might also be a regular C++ namespace.
6195   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6196                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6197 }
6198 
6199 //===----------------------------------------------------------------------===//
6200 // Error recovery tests.
6201 //===----------------------------------------------------------------------===//
6202 
6203 TEST_F(FormatTest, IncompleteParameterLists) {
6204   FormatStyle NoBinPacking = getLLVMStyle();
6205   NoBinPacking.BinPackParameters = false;
6206   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6207                "                        double *min_x,\n"
6208                "                        double *max_x,\n"
6209                "                        double *min_y,\n"
6210                "                        double *max_y,\n"
6211                "                        double *min_z,\n"
6212                "                        double *max_z, ) {}",
6213                NoBinPacking);
6214 }
6215 
6216 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6217   verifyFormat("void f() { return; }\n42");
6218   verifyFormat("void f() {\n"
6219                "  if (0)\n"
6220                "    return;\n"
6221                "}\n"
6222                "42");
6223   verifyFormat("void f() { return }\n42");
6224   verifyFormat("void f() {\n"
6225                "  if (0)\n"
6226                "    return\n"
6227                "}\n"
6228                "42");
6229 }
6230 
6231 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6232   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6233   EXPECT_EQ("void f() {\n"
6234             "  if (a)\n"
6235             "    return\n"
6236             "}",
6237             format("void  f  (  )  {  if  ( a )  return  }"));
6238   EXPECT_EQ("namespace N {\n"
6239             "void f()\n"
6240             "}",
6241             format("namespace  N  {  void f()  }"));
6242   EXPECT_EQ("namespace N {\n"
6243             "void f() {}\n"
6244             "void g()\n"
6245             "} // namespace N",
6246             format("namespace N  { void f( ) { } void g( ) }"));
6247 }
6248 
6249 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6250   verifyFormat("int aaaaaaaa =\n"
6251                "    // Overlylongcomment\n"
6252                "    b;",
6253                getLLVMStyleWithColumns(20));
6254   verifyFormat("function(\n"
6255                "    ShortArgument,\n"
6256                "    LoooooooooooongArgument);\n",
6257                getLLVMStyleWithColumns(20));
6258 }
6259 
6260 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6261   verifyFormat("public:");
6262   verifyFormat("class A {\n"
6263                "public\n"
6264                "  void f() {}\n"
6265                "};");
6266   verifyFormat("public\n"
6267                "int qwerty;");
6268   verifyFormat("public\n"
6269                "B {}");
6270   verifyFormat("public\n"
6271                "{}");
6272   verifyFormat("public\n"
6273                "B { int x; }");
6274 }
6275 
6276 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6277   verifyFormat("{");
6278   verifyFormat("#})");
6279   verifyNoCrash("(/**/[:!] ?[).");
6280 }
6281 
6282 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6283   verifyFormat("do {\n}");
6284   verifyFormat("do {\n}\n"
6285                "f();");
6286   verifyFormat("do {\n}\n"
6287                "wheeee(fun);");
6288   verifyFormat("do {\n"
6289                "  f();\n"
6290                "}");
6291 }
6292 
6293 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6294   verifyFormat("if {\n  foo;\n  foo();\n}");
6295   verifyFormat("switch {\n  foo;\n  foo();\n}");
6296   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6297   verifyFormat("while {\n  foo;\n  foo();\n}");
6298   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6299 }
6300 
6301 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6302   verifyIncompleteFormat("namespace {\n"
6303                          "class Foo { Foo (\n"
6304                          "};\n"
6305                          "} // namespace");
6306 }
6307 
6308 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6309   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6310   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6311   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6312   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6313 
6314   EXPECT_EQ("{\n"
6315             "  {\n"
6316             "    breakme(\n"
6317             "        qwe);\n"
6318             "  }\n",
6319             format("{\n"
6320                    "    {\n"
6321                    " breakme(qwe);\n"
6322                    "}\n",
6323                    getLLVMStyleWithColumns(10)));
6324 }
6325 
6326 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6327   verifyFormat("int x = {\n"
6328                "    avariable,\n"
6329                "    b(alongervariable)};",
6330                getLLVMStyleWithColumns(25));
6331 }
6332 
6333 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6334   verifyFormat("return (a)(b){1, 2, 3};");
6335 }
6336 
6337 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6338   verifyFormat("vector<int> x{1, 2, 3, 4};");
6339   verifyFormat("vector<int> x{\n"
6340                "    1,\n"
6341                "    2,\n"
6342                "    3,\n"
6343                "    4,\n"
6344                "};");
6345   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6346   verifyFormat("f({1, 2});");
6347   verifyFormat("auto v = Foo{-1};");
6348   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6349   verifyFormat("Class::Class : member{1, 2, 3} {}");
6350   verifyFormat("new vector<int>{1, 2, 3};");
6351   verifyFormat("new int[3]{1, 2, 3};");
6352   verifyFormat("new int{1};");
6353   verifyFormat("return {arg1, arg2};");
6354   verifyFormat("return {arg1, SomeType{parameter}};");
6355   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6356   verifyFormat("new T{arg1, arg2};");
6357   verifyFormat("f(MyMap[{composite, key}]);");
6358   verifyFormat("class Class {\n"
6359                "  T member = {arg1, arg2};\n"
6360                "};");
6361   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6362   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6363   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6364   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6365   verifyFormat("int a = std::is_integral<int>{} + 0;");
6366 
6367   verifyFormat("int foo(int i) { return fo1{}(i); }");
6368   verifyFormat("int foo(int i) { return fo1{}(i); }");
6369   verifyFormat("auto i = decltype(x){};");
6370   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6371   verifyFormat("Node n{1, Node{1000}, //\n"
6372                "       2};");
6373   verifyFormat("Aaaa aaaaaaa{\n"
6374                "    {\n"
6375                "        aaaa,\n"
6376                "    },\n"
6377                "};");
6378   verifyFormat("class C : public D {\n"
6379                "  SomeClass SC{2};\n"
6380                "};");
6381   verifyFormat("class C : public A {\n"
6382                "  class D : public B {\n"
6383                "    void f() { int i{2}; }\n"
6384                "  };\n"
6385                "};");
6386   verifyFormat("#define A {a, a},");
6387 
6388   // Binpacking only if there is no trailing comma
6389   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6390                "                      cccccccccc, dddddddddd};",
6391 			   getLLVMStyleWithColumns(50));
6392   verifyFormat("const Aaaaaa aaaaa = {\n"
6393                "    aaaaaaaaaaa,\n"
6394                "    bbbbbbbbbbb,\n"
6395                "    ccccccccccc,\n"
6396                "    ddddddddddd,\n"
6397                "};", getLLVMStyleWithColumns(50));
6398 
6399   // Cases where distinguising braced lists and blocks is hard.
6400   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6401   verifyFormat("void f() {\n"
6402                "  return; // comment\n"
6403                "}\n"
6404                "SomeType t;");
6405   verifyFormat("void f() {\n"
6406                "  if (a) {\n"
6407                "    f();\n"
6408                "  }\n"
6409                "}\n"
6410                "SomeType t;");
6411 
6412   // In combination with BinPackArguments = false.
6413   FormatStyle NoBinPacking = getLLVMStyle();
6414   NoBinPacking.BinPackArguments = false;
6415   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6416                "                      bbbbb,\n"
6417                "                      ccccc,\n"
6418                "                      ddddd,\n"
6419                "                      eeeee,\n"
6420                "                      ffffff,\n"
6421                "                      ggggg,\n"
6422                "                      hhhhhh,\n"
6423                "                      iiiiii,\n"
6424                "                      jjjjjj,\n"
6425                "                      kkkkkk};",
6426                NoBinPacking);
6427   verifyFormat("const Aaaaaa aaaaa = {\n"
6428                "    aaaaa,\n"
6429                "    bbbbb,\n"
6430                "    ccccc,\n"
6431                "    ddddd,\n"
6432                "    eeeee,\n"
6433                "    ffffff,\n"
6434                "    ggggg,\n"
6435                "    hhhhhh,\n"
6436                "    iiiiii,\n"
6437                "    jjjjjj,\n"
6438                "    kkkkkk,\n"
6439                "};",
6440                NoBinPacking);
6441   verifyFormat(
6442       "const Aaaaaa aaaaa = {\n"
6443       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6444       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6445       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6446       "};",
6447       NoBinPacking);
6448 
6449   // FIXME: The alignment of these trailing comments might be bad. Then again,
6450   // this might be utterly useless in real code.
6451   verifyFormat("Constructor::Constructor()\n"
6452                "    : some_value{         //\n"
6453                "                 aaaaaaa, //\n"
6454                "                 bbbbbbb} {}");
6455 
6456   // In braced lists, the first comment is always assumed to belong to the
6457   // first element. Thus, it can be moved to the next or previous line as
6458   // appropriate.
6459   EXPECT_EQ("function({// First element:\n"
6460             "          1,\n"
6461             "          // Second element:\n"
6462             "          2});",
6463             format("function({\n"
6464                    "    // First element:\n"
6465                    "    1,\n"
6466                    "    // Second element:\n"
6467                    "    2});"));
6468   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6469             "    // First element:\n"
6470             "    1,\n"
6471             "    // Second element:\n"
6472             "    2};",
6473             format("std::vector<int> MyNumbers{// First element:\n"
6474                    "                           1,\n"
6475                    "                           // Second element:\n"
6476                    "                           2};",
6477                    getLLVMStyleWithColumns(30)));
6478   // A trailing comma should still lead to an enforced line break and no
6479   // binpacking.
6480   EXPECT_EQ("vector<int> SomeVector = {\n"
6481             "    // aaa\n"
6482             "    1,\n"
6483             "    2,\n"
6484             "};",
6485             format("vector<int> SomeVector = { // aaa\n"
6486                    "    1, 2, };"));
6487 
6488   FormatStyle ExtraSpaces = getLLVMStyle();
6489   ExtraSpaces.Cpp11BracedListStyle = false;
6490   ExtraSpaces.ColumnLimit = 75;
6491   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6492   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6493   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6494   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6495   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6496   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6497   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6498   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6499   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6500   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6501   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6502   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6503   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6504   verifyFormat("class Class {\n"
6505                "  T member = { arg1, arg2 };\n"
6506                "};",
6507                ExtraSpaces);
6508   verifyFormat(
6509       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6510       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6511       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6512       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6513       ExtraSpaces);
6514   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6515   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6516                ExtraSpaces);
6517   verifyFormat(
6518       "someFunction(OtherParam,\n"
6519       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6520       "                         param1, param2,\n"
6521       "                         // comment 2\n"
6522       "                         param3, param4 });",
6523       ExtraSpaces);
6524   verifyFormat(
6525       "std::this_thread::sleep_for(\n"
6526       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6527       ExtraSpaces);
6528   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6529                "    aaaaaaa,\n"
6530                "    aaaaaaaaaa,\n"
6531                "    aaaaa,\n"
6532                "    aaaaaaaaaaaaaaa,\n"
6533                "    aaa,\n"
6534                "    aaaaaaaaaa,\n"
6535                "    a,\n"
6536                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6537                "    aaaaaaaaaaaa,\n"
6538                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6539                "    aaaaaaa,\n"
6540                "    a};");
6541   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6542   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6543   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6544 }
6545 
6546 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6547   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6548                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6549                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6550                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6551                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6552                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6553   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6554                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6555                "                 1, 22, 333, 4444, 55555, //\n"
6556                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6557                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6558   verifyFormat(
6559       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6560       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6561       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6562       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6563       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6564       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6565       "                 7777777};");
6566   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6567                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6568                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6569   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6570                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6571                "    // Separating comment.\n"
6572                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6573   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6574                "    // Leading comment\n"
6575                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6576                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6577   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6578                "                 1, 1, 1, 1};",
6579                getLLVMStyleWithColumns(39));
6580   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6581                "                 1, 1, 1, 1};",
6582                getLLVMStyleWithColumns(38));
6583   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6584                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6585                getLLVMStyleWithColumns(43));
6586   verifyFormat(
6587       "static unsigned SomeValues[10][3] = {\n"
6588       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6589       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6590   verifyFormat("static auto fields = new vector<string>{\n"
6591                "    \"aaaaaaaaaaaaa\",\n"
6592                "    \"aaaaaaaaaaaaa\",\n"
6593                "    \"aaaaaaaaaaaa\",\n"
6594                "    \"aaaaaaaaaaaaaa\",\n"
6595                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6596                "    \"aaaaaaaaaaaa\",\n"
6597                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6598                "};");
6599   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6600   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6601                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6602                "                 3, cccccccccccccccccccccc};",
6603                getLLVMStyleWithColumns(60));
6604 
6605   // Trailing commas.
6606   verifyFormat("vector<int> x = {\n"
6607                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6608                "};",
6609                getLLVMStyleWithColumns(39));
6610   verifyFormat("vector<int> x = {\n"
6611                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6612                "};",
6613                getLLVMStyleWithColumns(39));
6614   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6615                "                 1, 1, 1, 1,\n"
6616                "                 /**/ /**/};",
6617                getLLVMStyleWithColumns(39));
6618 
6619   // Trailing comment in the first line.
6620   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6621                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6622                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6623                "    11111111,   22222222,   333333333,   44444444};");
6624   // Trailing comment in the last line.
6625   verifyFormat("int aaaaa[] = {\n"
6626                "    1, 2, 3, // comment\n"
6627                "    4, 5, 6  // comment\n"
6628                "};");
6629 
6630   // With nested lists, we should either format one item per line or all nested
6631   // lists one on line.
6632   // FIXME: For some nested lists, we can do better.
6633   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6634                "        {aaaaaaaaaaaaaaaaaaa},\n"
6635                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6636                "        {aaaaaaaaaaaaaaaaa}};",
6637                getLLVMStyleWithColumns(60));
6638   verifyFormat(
6639       "SomeStruct my_struct_array = {\n"
6640       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6641       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6642       "    {aaa, aaa},\n"
6643       "    {aaa, aaa},\n"
6644       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6645       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6646       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6647 
6648   // No column layout should be used here.
6649   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6650                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6651 
6652   verifyNoCrash("a<,");
6653 
6654   // No braced initializer here.
6655   verifyFormat("void f() {\n"
6656                "  struct Dummy {};\n"
6657                "  f(v);\n"
6658                "}");
6659 
6660   // Long lists should be formatted in columns even if they are nested.
6661   verifyFormat(
6662       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6663       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6664       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6665       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6666       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6667       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6668 
6669   // Allow "single-column" layout even if that violates the column limit. There
6670   // isn't going to be a better way.
6671   verifyFormat("std::vector<int> a = {\n"
6672                "    aaaaaaaa,\n"
6673                "    aaaaaaaa,\n"
6674                "    aaaaaaaa,\n"
6675                "    aaaaaaaa,\n"
6676                "    aaaaaaaaaa,\n"
6677                "    aaaaaaaa,\n"
6678                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6679                getLLVMStyleWithColumns(30));
6680   verifyFormat("vector<int> aaaa = {\n"
6681                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6682                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6683                "    aaaaaa.aaaaaaa,\n"
6684                "    aaaaaa.aaaaaaa,\n"
6685                "    aaaaaa.aaaaaaa,\n"
6686                "    aaaaaa.aaaaaaa,\n"
6687                "};");
6688 
6689   // Don't create hanging lists.
6690   verifyFormat("someFunction(Param, {List1, List2,\n"
6691                "                     List3});",
6692                getLLVMStyleWithColumns(35));
6693   verifyFormat("someFunction(Param, Param,\n"
6694                "             {List1, List2,\n"
6695                "              List3});",
6696                getLLVMStyleWithColumns(35));
6697   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6698                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6699 }
6700 
6701 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6702   FormatStyle DoNotMerge = getLLVMStyle();
6703   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6704 
6705   verifyFormat("void f() { return 42; }");
6706   verifyFormat("void f() {\n"
6707                "  return 42;\n"
6708                "}",
6709                DoNotMerge);
6710   verifyFormat("void f() {\n"
6711                "  // Comment\n"
6712                "}");
6713   verifyFormat("{\n"
6714                "#error {\n"
6715                "  int a;\n"
6716                "}");
6717   verifyFormat("{\n"
6718                "  int a;\n"
6719                "#error {\n"
6720                "}");
6721   verifyFormat("void f() {} // comment");
6722   verifyFormat("void f() { int a; } // comment");
6723   verifyFormat("void f() {\n"
6724                "} // comment",
6725                DoNotMerge);
6726   verifyFormat("void f() {\n"
6727                "  int a;\n"
6728                "} // comment",
6729                DoNotMerge);
6730   verifyFormat("void f() {\n"
6731                "} // comment",
6732                getLLVMStyleWithColumns(15));
6733 
6734   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6735   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6736 
6737   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6738   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6739   verifyFormat("class C {\n"
6740                "  C()\n"
6741                "      : iiiiiiii(nullptr),\n"
6742                "        kkkkkkk(nullptr),\n"
6743                "        mmmmmmm(nullptr),\n"
6744                "        nnnnnnn(nullptr) {}\n"
6745                "};",
6746                getGoogleStyle());
6747 
6748   FormatStyle NoColumnLimit = getLLVMStyle();
6749   NoColumnLimit.ColumnLimit = 0;
6750   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6751   EXPECT_EQ("class C {\n"
6752             "  A() : b(0) {}\n"
6753             "};",
6754             format("class C{A():b(0){}};", NoColumnLimit));
6755   EXPECT_EQ("A()\n"
6756             "    : b(0) {\n"
6757             "}",
6758             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6759 
6760   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6761   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6762       FormatStyle::SFS_None;
6763   EXPECT_EQ("A()\n"
6764             "    : b(0) {\n"
6765             "}",
6766             format("A():b(0){}", DoNotMergeNoColumnLimit));
6767   EXPECT_EQ("A()\n"
6768             "    : b(0) {\n"
6769             "}",
6770             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6771 
6772   verifyFormat("#define A          \\\n"
6773                "  void f() {       \\\n"
6774                "    int i;         \\\n"
6775                "  }",
6776                getLLVMStyleWithColumns(20));
6777   verifyFormat("#define A           \\\n"
6778                "  void f() { int i; }",
6779                getLLVMStyleWithColumns(21));
6780   verifyFormat("#define A            \\\n"
6781                "  void f() {         \\\n"
6782                "    int i;           \\\n"
6783                "  }                  \\\n"
6784                "  int j;",
6785                getLLVMStyleWithColumns(22));
6786   verifyFormat("#define A             \\\n"
6787                "  void f() { int i; } \\\n"
6788                "  int j;",
6789                getLLVMStyleWithColumns(23));
6790 }
6791 
6792 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
6793   FormatStyle MergeEmptyOnly = getLLVMStyle();
6794   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6795   verifyFormat("class C {\n"
6796                "  int f() {}\n"
6797                "};",
6798                MergeEmptyOnly);
6799   verifyFormat("class C {\n"
6800                "  int f() {\n"
6801                "    return 42;\n"
6802                "  }\n"
6803                "};",
6804                MergeEmptyOnly);
6805   verifyFormat("int f() {}", MergeEmptyOnly);
6806   verifyFormat("int f() {\n"
6807                "  return 42;\n"
6808                "}",
6809                MergeEmptyOnly);
6810 
6811   // Also verify behavior when BraceWrapping.AfterFunction = true
6812   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6813   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
6814   verifyFormat("int f() {}", MergeEmptyOnly);
6815   verifyFormat("class C {\n"
6816                "  int f() {}\n"
6817                "};",
6818                MergeEmptyOnly);
6819 }
6820 
6821 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6822   FormatStyle MergeInlineOnly = getLLVMStyle();
6823   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6824   verifyFormat("class C {\n"
6825                "  int f() { return 42; }\n"
6826                "};",
6827                MergeInlineOnly);
6828   verifyFormat("int f() {\n"
6829                "  return 42;\n"
6830                "}",
6831                MergeInlineOnly);
6832 
6833   // SFS_Inline implies SFS_Empty
6834   verifyFormat("class C {\n"
6835                "  int f() {}\n"
6836                "};",
6837                MergeInlineOnly);
6838   verifyFormat("int f() {}", MergeInlineOnly);
6839 
6840   // Also verify behavior when BraceWrapping.AfterFunction = true
6841   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6842   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6843   verifyFormat("class C {\n"
6844                "  int f() { return 42; }\n"
6845                "};",
6846                MergeInlineOnly);
6847   verifyFormat("int f()\n"
6848                "{\n"
6849                "  return 42;\n"
6850                "}",
6851                MergeInlineOnly);
6852 
6853   // SFS_Inline implies SFS_Empty
6854   verifyFormat("int f() {}", MergeInlineOnly);
6855   verifyFormat("class C {\n"
6856                "  int f() {}\n"
6857                "};",
6858                MergeInlineOnly);
6859 }
6860 
6861 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
6862   FormatStyle MergeInlineOnly = getLLVMStyle();
6863   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
6864       FormatStyle::SFS_InlineOnly;
6865   verifyFormat("class C {\n"
6866                "  int f() { return 42; }\n"
6867                "};",
6868                MergeInlineOnly);
6869   verifyFormat("int f() {\n"
6870                "  return 42;\n"
6871                "}",
6872                MergeInlineOnly);
6873 
6874   // SFS_InlineOnly does not imply SFS_Empty
6875   verifyFormat("class C {\n"
6876                "  int f() {}\n"
6877                "};",
6878                MergeInlineOnly);
6879   verifyFormat("int f() {\n"
6880                "}",
6881                MergeInlineOnly);
6882 
6883   // Also verify behavior when BraceWrapping.AfterFunction = true
6884   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6885   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6886   verifyFormat("class C {\n"
6887                "  int f() { return 42; }\n"
6888                "};",
6889                MergeInlineOnly);
6890   verifyFormat("int f()\n"
6891                "{\n"
6892                "  return 42;\n"
6893                "}",
6894                MergeInlineOnly);
6895 
6896   // SFS_InlineOnly does not imply SFS_Empty
6897   verifyFormat("int f()\n"
6898                "{\n"
6899                "}",
6900                MergeInlineOnly);
6901   verifyFormat("class C {\n"
6902                "  int f() {}\n"
6903                "};",
6904                MergeInlineOnly);
6905 }
6906 
6907 TEST_F(FormatTest, SplitEmptyFunction) {
6908   FormatStyle Style = getLLVMStyle();
6909   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6910   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6911   Style.BraceWrapping.AfterFunction = true;
6912   Style.BraceWrapping.SplitEmptyFunction = false;
6913   Style.ColumnLimit = 40;
6914 
6915   verifyFormat("int f()\n"
6916                "{}",
6917                Style);
6918   verifyFormat("int f()\n"
6919                "{\n"
6920                "  return 42;\n"
6921                "}",
6922                Style);
6923   verifyFormat("int f()\n"
6924                "{\n"
6925                "  // some comment\n"
6926                "}",
6927                Style);
6928 
6929   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6930   verifyFormat("int f() {}", Style);
6931   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6932                "{}",
6933                Style);
6934   verifyFormat("int f()\n"
6935                "{\n"
6936                "  return 0;\n"
6937                "}",
6938                Style);
6939 
6940   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6941   verifyFormat("class Foo {\n"
6942                "  int f() {}\n"
6943                "};\n",
6944                Style);
6945   verifyFormat("class Foo {\n"
6946                "  int f() { return 0; }\n"
6947                "};\n",
6948                Style);
6949   verifyFormat("class Foo {\n"
6950                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6951                "  {}\n"
6952                "};\n",
6953                Style);
6954   verifyFormat("class Foo {\n"
6955                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6956                "  {\n"
6957                "    return 0;\n"
6958                "  }\n"
6959                "};\n",
6960                Style);
6961 
6962   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6963   verifyFormat("int f() {}", Style);
6964   verifyFormat("int f() { return 0; }", Style);
6965   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6966                "{}",
6967                Style);
6968   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6969                "{\n"
6970                "  return 0;\n"
6971                "}",
6972                Style);
6973 }
6974 
6975 TEST_F(FormatTest, SplitEmptyClass) {
6976   FormatStyle Style = getLLVMStyle();
6977   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6978   Style.BraceWrapping.AfterClass = true;
6979   Style.BraceWrapping.SplitEmptyRecord = false;
6980 
6981   verifyFormat("class Foo\n"
6982                "{};",
6983                Style);
6984   verifyFormat("/* something */ class Foo\n"
6985                "{};",
6986                Style);
6987   verifyFormat("template <typename X> class Foo\n"
6988                "{};",
6989                Style);
6990   verifyFormat("class Foo\n"
6991                "{\n"
6992                "  Foo();\n"
6993                "};",
6994                Style);
6995   verifyFormat("typedef class Foo\n"
6996                "{\n"
6997                "} Foo_t;",
6998                Style);
6999 }
7000 
7001 TEST_F(FormatTest, SplitEmptyStruct) {
7002   FormatStyle Style = getLLVMStyle();
7003   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7004   Style.BraceWrapping.AfterStruct = true;
7005   Style.BraceWrapping.SplitEmptyRecord = false;
7006 
7007   verifyFormat("struct Foo\n"
7008                "{};",
7009                Style);
7010   verifyFormat("/* something */ struct Foo\n"
7011                "{};",
7012                Style);
7013   verifyFormat("template <typename X> struct Foo\n"
7014                "{};",
7015                Style);
7016   verifyFormat("struct Foo\n"
7017                "{\n"
7018                "  Foo();\n"
7019                "};",
7020                Style);
7021   verifyFormat("typedef struct Foo\n"
7022                "{\n"
7023                "} Foo_t;",
7024                Style);
7025   //typedef struct Bar {} Bar_t;
7026 }
7027 
7028 TEST_F(FormatTest, SplitEmptyUnion) {
7029   FormatStyle Style = getLLVMStyle();
7030   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7031   Style.BraceWrapping.AfterUnion = true;
7032   Style.BraceWrapping.SplitEmptyRecord = false;
7033 
7034   verifyFormat("union Foo\n"
7035                "{};",
7036                Style);
7037   verifyFormat("/* something */ union Foo\n"
7038                "{};",
7039                Style);
7040   verifyFormat("union Foo\n"
7041                "{\n"
7042                "  A,\n"
7043                "};",
7044                Style);
7045   verifyFormat("typedef union Foo\n"
7046                "{\n"
7047                "} Foo_t;",
7048                Style);
7049 }
7050 
7051 TEST_F(FormatTest, SplitEmptyNamespace) {
7052   FormatStyle Style = getLLVMStyle();
7053   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7054   Style.BraceWrapping.AfterNamespace = true;
7055   Style.BraceWrapping.SplitEmptyNamespace = false;
7056 
7057   verifyFormat("namespace Foo\n"
7058                "{};",
7059                Style);
7060   verifyFormat("/* something */ namespace Foo\n"
7061                "{};",
7062                Style);
7063   verifyFormat("inline namespace Foo\n"
7064                "{};",
7065                Style);
7066   verifyFormat("namespace Foo\n"
7067                "{\n"
7068                "void Bar();\n"
7069                "};",
7070                Style);
7071 }
7072 
7073 TEST_F(FormatTest, NeverMergeShortRecords) {
7074   FormatStyle Style = getLLVMStyle();
7075 
7076   verifyFormat("class Foo {\n"
7077                "  Foo();\n"
7078                "};",
7079                Style);
7080   verifyFormat("typedef class Foo {\n"
7081                "  Foo();\n"
7082                "} Foo_t;",
7083                Style);
7084   verifyFormat("struct Foo {\n"
7085                "  Foo();\n"
7086                "};",
7087                Style);
7088   verifyFormat("typedef struct Foo {\n"
7089                "  Foo();\n"
7090                "} Foo_t;",
7091                Style);
7092   verifyFormat("union Foo {\n"
7093                "  A,\n"
7094                "};",
7095                Style);
7096   verifyFormat("typedef union Foo {\n"
7097                "  A,\n"
7098                "} Foo_t;",
7099                Style);
7100   verifyFormat("namespace Foo {\n"
7101                "void Bar();\n"
7102                "};",
7103                Style);
7104 
7105   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7106   Style.BraceWrapping.AfterClass = true;
7107   Style.BraceWrapping.AfterStruct = true;
7108   Style.BraceWrapping.AfterUnion = true;
7109   Style.BraceWrapping.AfterNamespace = true;
7110   verifyFormat("class Foo\n"
7111                "{\n"
7112                "  Foo();\n"
7113                "};",
7114                Style);
7115   verifyFormat("typedef class Foo\n"
7116                "{\n"
7117                "  Foo();\n"
7118                "} Foo_t;",
7119                Style);
7120   verifyFormat("struct Foo\n"
7121                "{\n"
7122                "  Foo();\n"
7123                "};",
7124                Style);
7125   verifyFormat("typedef struct Foo\n"
7126                "{\n"
7127                "  Foo();\n"
7128                "} Foo_t;",
7129                Style);
7130   verifyFormat("union Foo\n"
7131                "{\n"
7132                "  A,\n"
7133                "};",
7134                Style);
7135   verifyFormat("typedef union Foo\n"
7136                "{\n"
7137                "  A,\n"
7138                "} Foo_t;",
7139                Style);
7140   verifyFormat("namespace Foo\n"
7141                "{\n"
7142                "void Bar();\n"
7143                "};",
7144                Style);
7145 }
7146 
7147 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7148   // Elaborate type variable declarations.
7149   verifyFormat("struct foo a = {bar};\nint n;");
7150   verifyFormat("class foo a = {bar};\nint n;");
7151   verifyFormat("union foo a = {bar};\nint n;");
7152 
7153   // Elaborate types inside function definitions.
7154   verifyFormat("struct foo f() {}\nint n;");
7155   verifyFormat("class foo f() {}\nint n;");
7156   verifyFormat("union foo f() {}\nint n;");
7157 
7158   // Templates.
7159   verifyFormat("template <class X> void f() {}\nint n;");
7160   verifyFormat("template <struct X> void f() {}\nint n;");
7161   verifyFormat("template <union X> void f() {}\nint n;");
7162 
7163   // Actual definitions...
7164   verifyFormat("struct {\n} n;");
7165   verifyFormat(
7166       "template <template <class T, class Y>, class Z> class X {\n} n;");
7167   verifyFormat("union Z {\n  int n;\n} x;");
7168   verifyFormat("class MACRO Z {\n} n;");
7169   verifyFormat("class MACRO(X) Z {\n} n;");
7170   verifyFormat("class __attribute__(X) Z {\n} n;");
7171   verifyFormat("class __declspec(X) Z {\n} n;");
7172   verifyFormat("class A##B##C {\n} n;");
7173   verifyFormat("class alignas(16) Z {\n} n;");
7174   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7175   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7176 
7177   // Redefinition from nested context:
7178   verifyFormat("class A::B::C {\n} n;");
7179 
7180   // Template definitions.
7181   verifyFormat(
7182       "template <typename F>\n"
7183       "Matcher(const Matcher<F> &Other,\n"
7184       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7185       "                             !is_same<F, T>::value>::type * = 0)\n"
7186       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7187 
7188   // FIXME: This is still incorrectly handled at the formatter side.
7189   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7190   verifyFormat("int i = SomeFunction(a<b, a> b);");
7191 
7192   // FIXME:
7193   // This now gets parsed incorrectly as class definition.
7194   // verifyFormat("class A<int> f() {\n}\nint n;");
7195 
7196   // Elaborate types where incorrectly parsing the structural element would
7197   // break the indent.
7198   verifyFormat("if (true)\n"
7199                "  class X x;\n"
7200                "else\n"
7201                "  f();\n");
7202 
7203   // This is simply incomplete. Formatting is not important, but must not crash.
7204   verifyFormat("class A:");
7205 }
7206 
7207 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7208   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7209             format("#error Leave     all         white!!!!! space* alone!\n"));
7210   EXPECT_EQ(
7211       "#warning Leave     all         white!!!!! space* alone!\n",
7212       format("#warning Leave     all         white!!!!! space* alone!\n"));
7213   EXPECT_EQ("#error 1", format("  #  error   1"));
7214   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7215 }
7216 
7217 TEST_F(FormatTest, FormatHashIfExpressions) {
7218   verifyFormat("#if AAAA && BBBB");
7219   verifyFormat("#if (AAAA && BBBB)");
7220   verifyFormat("#elif (AAAA && BBBB)");
7221   // FIXME: Come up with a better indentation for #elif.
7222   verifyFormat(
7223       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7224       "    defined(BBBBBBBB)\n"
7225       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7226       "    defined(BBBBBBBB)\n"
7227       "#endif",
7228       getLLVMStyleWithColumns(65));
7229 }
7230 
7231 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7232   FormatStyle AllowsMergedIf = getGoogleStyle();
7233   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7234   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7235   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7236   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7237   EXPECT_EQ("if (true) return 42;",
7238             format("if (true)\nreturn 42;", AllowsMergedIf));
7239   FormatStyle ShortMergedIf = AllowsMergedIf;
7240   ShortMergedIf.ColumnLimit = 25;
7241   verifyFormat("#define A \\\n"
7242                "  if (true) return 42;",
7243                ShortMergedIf);
7244   verifyFormat("#define A \\\n"
7245                "  f();    \\\n"
7246                "  if (true)\n"
7247                "#define B",
7248                ShortMergedIf);
7249   verifyFormat("#define A \\\n"
7250                "  f();    \\\n"
7251                "  if (true)\n"
7252                "g();",
7253                ShortMergedIf);
7254   verifyFormat("{\n"
7255                "#ifdef A\n"
7256                "  // Comment\n"
7257                "  if (true) continue;\n"
7258                "#endif\n"
7259                "  // Comment\n"
7260                "  if (true) continue;\n"
7261                "}",
7262                ShortMergedIf);
7263   ShortMergedIf.ColumnLimit = 33;
7264   verifyFormat("#define A \\\n"
7265                "  if constexpr (true) return 42;",
7266                ShortMergedIf);
7267   ShortMergedIf.ColumnLimit = 29;
7268   verifyFormat("#define A                   \\\n"
7269                "  if (aaaaaaaaaa) return 1; \\\n"
7270                "  return 2;",
7271                ShortMergedIf);
7272   ShortMergedIf.ColumnLimit = 28;
7273   verifyFormat("#define A         \\\n"
7274                "  if (aaaaaaaaaa) \\\n"
7275                "    return 1;     \\\n"
7276                "  return 2;",
7277                ShortMergedIf);
7278   verifyFormat("#define A                \\\n"
7279                "  if constexpr (aaaaaaa) \\\n"
7280                "    return 1;            \\\n"
7281                "  return 2;",
7282                ShortMergedIf);
7283 }
7284 
7285 TEST_F(FormatTest, FormatStarDependingOnContext) {
7286   verifyFormat("void f(int *a);");
7287   verifyFormat("void f() { f(fint * b); }");
7288   verifyFormat("class A {\n  void f(int *a);\n};");
7289   verifyFormat("class A {\n  int *a;\n};");
7290   verifyFormat("namespace a {\n"
7291                "namespace b {\n"
7292                "class A {\n"
7293                "  void f() {}\n"
7294                "  int *a;\n"
7295                "};\n"
7296                "} // namespace b\n"
7297                "} // namespace a");
7298 }
7299 
7300 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7301   verifyFormat("while");
7302   verifyFormat("operator");
7303 }
7304 
7305 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7306   // This code would be painfully slow to format if we didn't skip it.
7307   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
7308                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7309                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7310                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7311                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7312                    "A(1, 1)\n"
7313                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7314                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7315                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7316                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7317                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7318                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7319                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7320                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7321                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7322                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7323   // Deeply nested part is untouched, rest is formatted.
7324   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7325             format(std::string("int    i;\n") + Code + "int    j;\n",
7326                    getLLVMStyle(), SC_ExpectIncomplete));
7327 }
7328 
7329 //===----------------------------------------------------------------------===//
7330 // Objective-C tests.
7331 //===----------------------------------------------------------------------===//
7332 
7333 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7334   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7335   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7336             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7337   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7338   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7339   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7340             format("-(NSInteger)Method3:(id)anObject;"));
7341   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7342             format("-(NSInteger)Method4:(id)anObject;"));
7343   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7344             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7345   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7346             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7347   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7348             "forAllCells:(BOOL)flag;",
7349             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7350                    "forAllCells:(BOOL)flag;"));
7351 
7352   // Very long objectiveC method declaration.
7353   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7354                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7355   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7356                "                    inRange:(NSRange)range\n"
7357                "                   outRange:(NSRange)out_range\n"
7358                "                  outRange1:(NSRange)out_range1\n"
7359                "                  outRange2:(NSRange)out_range2\n"
7360                "                  outRange3:(NSRange)out_range3\n"
7361                "                  outRange4:(NSRange)out_range4\n"
7362                "                  outRange5:(NSRange)out_range5\n"
7363                "                  outRange6:(NSRange)out_range6\n"
7364                "                  outRange7:(NSRange)out_range7\n"
7365                "                  outRange8:(NSRange)out_range8\n"
7366                "                  outRange9:(NSRange)out_range9;");
7367 
7368   // When the function name has to be wrapped.
7369   FormatStyle Style = getLLVMStyle();
7370   Style.IndentWrappedFunctionNames = false;
7371   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7372                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7373                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7374                "}",
7375                Style);
7376   Style.IndentWrappedFunctionNames = true;
7377   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7378                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7379                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7380                "}",
7381                Style);
7382 
7383   verifyFormat("- (int)sum:(vector<int>)numbers;");
7384   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7385   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7386   // protocol lists (but not for template classes):
7387   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7388 
7389   verifyFormat("- (int (*)())foo:(int (*)())f;");
7390   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7391 
7392   // If there's no return type (very rare in practice!), LLVM and Google style
7393   // agree.
7394   verifyFormat("- foo;");
7395   verifyFormat("- foo:(int)f;");
7396   verifyGoogleFormat("- foo:(int)foo;");
7397 }
7398 
7399 
7400 TEST_F(FormatTest, BreaksStringLiterals) {
7401   EXPECT_EQ("\"some text \"\n"
7402             "\"other\";",
7403             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7404   EXPECT_EQ("\"some text \"\n"
7405             "\"other\";",
7406             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7407   EXPECT_EQ(
7408       "#define A  \\\n"
7409       "  \"some \"  \\\n"
7410       "  \"text \"  \\\n"
7411       "  \"other\";",
7412       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7413   EXPECT_EQ(
7414       "#define A  \\\n"
7415       "  \"so \"    \\\n"
7416       "  \"text \"  \\\n"
7417       "  \"other\";",
7418       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7419 
7420   EXPECT_EQ("\"some text\"",
7421             format("\"some text\"", getLLVMStyleWithColumns(1)));
7422   EXPECT_EQ("\"some text\"",
7423             format("\"some text\"", getLLVMStyleWithColumns(11)));
7424   EXPECT_EQ("\"some \"\n"
7425             "\"text\"",
7426             format("\"some text\"", getLLVMStyleWithColumns(10)));
7427   EXPECT_EQ("\"some \"\n"
7428             "\"text\"",
7429             format("\"some text\"", getLLVMStyleWithColumns(7)));
7430   EXPECT_EQ("\"some\"\n"
7431             "\" tex\"\n"
7432             "\"t\"",
7433             format("\"some text\"", getLLVMStyleWithColumns(6)));
7434   EXPECT_EQ("\"some\"\n"
7435             "\" tex\"\n"
7436             "\" and\"",
7437             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7438   EXPECT_EQ("\"some\"\n"
7439             "\"/tex\"\n"
7440             "\"/and\"",
7441             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7442 
7443   EXPECT_EQ("variable =\n"
7444             "    \"long string \"\n"
7445             "    \"literal\";",
7446             format("variable = \"long string literal\";",
7447                    getLLVMStyleWithColumns(20)));
7448 
7449   EXPECT_EQ("variable = f(\n"
7450             "    \"long string \"\n"
7451             "    \"literal\",\n"
7452             "    short,\n"
7453             "    loooooooooooooooooooong);",
7454             format("variable = f(\"long string literal\", short, "
7455                    "loooooooooooooooooooong);",
7456                    getLLVMStyleWithColumns(20)));
7457 
7458   EXPECT_EQ(
7459       "f(g(\"long string \"\n"
7460       "    \"literal\"),\n"
7461       "  b);",
7462       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7463   EXPECT_EQ("f(g(\"long string \"\n"
7464             "    \"literal\",\n"
7465             "    a),\n"
7466             "  b);",
7467             format("f(g(\"long string literal\", a), b);",
7468                    getLLVMStyleWithColumns(20)));
7469   EXPECT_EQ(
7470       "f(\"one two\".split(\n"
7471       "    variable));",
7472       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7473   EXPECT_EQ("f(\"one two three four five six \"\n"
7474             "  \"seven\".split(\n"
7475             "      really_looooong_variable));",
7476             format("f(\"one two three four five six seven\"."
7477                    "split(really_looooong_variable));",
7478                    getLLVMStyleWithColumns(33)));
7479 
7480   EXPECT_EQ("f(\"some \"\n"
7481             "  \"text\",\n"
7482             "  other);",
7483             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7484 
7485   // Only break as a last resort.
7486   verifyFormat(
7487       "aaaaaaaaaaaaaaaaaaaa(\n"
7488       "    aaaaaaaaaaaaaaaaaaaa,\n"
7489       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7490 
7491   EXPECT_EQ("\"splitmea\"\n"
7492             "\"trandomp\"\n"
7493             "\"oint\"",
7494             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7495 
7496   EXPECT_EQ("\"split/\"\n"
7497             "\"pathat/\"\n"
7498             "\"slashes\"",
7499             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7500 
7501   EXPECT_EQ("\"split/\"\n"
7502             "\"pathat/\"\n"
7503             "\"slashes\"",
7504             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7505   EXPECT_EQ("\"split at \"\n"
7506             "\"spaces/at/\"\n"
7507             "\"slashes.at.any$\"\n"
7508             "\"non-alphanumeric%\"\n"
7509             "\"1111111111characte\"\n"
7510             "\"rs\"",
7511             format("\"split at "
7512                    "spaces/at/"
7513                    "slashes.at."
7514                    "any$non-"
7515                    "alphanumeric%"
7516                    "1111111111characte"
7517                    "rs\"",
7518                    getLLVMStyleWithColumns(20)));
7519 
7520   // Verify that splitting the strings understands
7521   // Style::AlwaysBreakBeforeMultilineStrings.
7522   EXPECT_EQ(
7523       "aaaaaaaaaaaa(\n"
7524       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7525       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7526       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7527              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7528              "aaaaaaaaaaaaaaaaaaaaaa\");",
7529              getGoogleStyle()));
7530   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7531             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7532             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7533                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7534                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7535                    getGoogleStyle()));
7536   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7537             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7538             format("llvm::outs() << "
7539                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7540                    "aaaaaaaaaaaaaaaaaaa\";"));
7541   EXPECT_EQ("ffff(\n"
7542             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7543             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7544             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7545                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7546                    getGoogleStyle()));
7547 
7548   FormatStyle Style = getLLVMStyleWithColumns(12);
7549   Style.BreakStringLiterals = false;
7550   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7551 
7552   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7553   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7554   EXPECT_EQ("#define A \\\n"
7555             "  \"some \" \\\n"
7556             "  \"text \" \\\n"
7557             "  \"other\";",
7558             format("#define A \"some text other\";", AlignLeft));
7559 }
7560 
7561 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7562   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7563   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7564   EXPECT_EQ("int i = a(b());",
7565             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7566 }
7567 
7568 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7569   EXPECT_EQ(
7570       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7571       "(\n"
7572       "    \"x\t\");",
7573       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7574              "aaaaaaa("
7575              "\"x\t\");"));
7576 }
7577 
7578 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7579   EXPECT_EQ(
7580       "u8\"utf8 string \"\n"
7581       "u8\"literal\";",
7582       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7583   EXPECT_EQ(
7584       "u\"utf16 string \"\n"
7585       "u\"literal\";",
7586       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7587   EXPECT_EQ(
7588       "U\"utf32 string \"\n"
7589       "U\"literal\";",
7590       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7591   EXPECT_EQ("L\"wide string \"\n"
7592             "L\"literal\";",
7593             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7594   EXPECT_EQ("@\"NSString \"\n"
7595             "@\"literal\";",
7596             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7597   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7598 
7599   // This input makes clang-format try to split the incomplete unicode escape
7600   // sequence, which used to lead to a crasher.
7601   verifyNoCrash(
7602       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7603       getLLVMStyleWithColumns(60));
7604 }
7605 
7606 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7607   FormatStyle Style = getGoogleStyleWithColumns(15);
7608   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7609   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7610   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7611   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7612   EXPECT_EQ("u8R\"x(raw literal)x\";",
7613             format("u8R\"x(raw literal)x\";", Style));
7614 }
7615 
7616 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7617   FormatStyle Style = getLLVMStyleWithColumns(20);
7618   EXPECT_EQ(
7619       "_T(\"aaaaaaaaaaaaaa\")\n"
7620       "_T(\"aaaaaaaaaaaaaa\")\n"
7621       "_T(\"aaaaaaaaaaaa\")",
7622       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7623   EXPECT_EQ("f(x,\n"
7624             "  _T(\"aaaaaaaaaaaa\")\n"
7625             "  _T(\"aaa\"),\n"
7626             "  z);",
7627             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7628 
7629   // FIXME: Handle embedded spaces in one iteration.
7630   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7631   //            "_T(\"aaaaaaaaaaaaa\")\n"
7632   //            "_T(\"aaaaaaaaaaaaa\")\n"
7633   //            "_T(\"a\")",
7634   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7635   //                   getLLVMStyleWithColumns(20)));
7636   EXPECT_EQ(
7637       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7638       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7639   EXPECT_EQ("f(\n"
7640             "#if !TEST\n"
7641             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7642             "#endif\n"
7643             ");",
7644             format("f(\n"
7645                    "#if !TEST\n"
7646                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7647                    "#endif\n"
7648                    ");"));
7649   EXPECT_EQ("f(\n"
7650             "\n"
7651             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7652             format("f(\n"
7653                    "\n"
7654                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7655 }
7656 
7657 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7658   // In a function call with two operands, the second can be broken with no line
7659   // break before it.
7660   EXPECT_EQ("func(a, \"long long \"\n"
7661             "        \"long long\");",
7662             format("func(a, \"long long long long\");",
7663                    getLLVMStyleWithColumns(24)));
7664   // In a function call with three operands, the second must be broken with a
7665   // line break before it.
7666   EXPECT_EQ("func(a,\n"
7667             "     \"long long long \"\n"
7668             "     \"long\",\n"
7669             "     c);",
7670             format("func(a, \"long long long long\", c);",
7671                    getLLVMStyleWithColumns(24)));
7672   // In a function call with three operands, the third must be broken with a
7673   // line break before it.
7674   EXPECT_EQ("func(a, b,\n"
7675             "     \"long long long \"\n"
7676             "     \"long\");",
7677             format("func(a, b, \"long long long long\");",
7678                    getLLVMStyleWithColumns(24)));
7679   // In a function call with three operands, both the second and the third must
7680   // be broken with a line break before them.
7681   EXPECT_EQ("func(a,\n"
7682             "     \"long long long \"\n"
7683             "     \"long\",\n"
7684             "     \"long long long \"\n"
7685             "     \"long\");",
7686             format("func(a, \"long long long long\", \"long long long long\");",
7687                    getLLVMStyleWithColumns(24)));
7688   // In a chain of << with two operands, the second can be broken with no line
7689   // break before it.
7690   EXPECT_EQ("a << \"line line \"\n"
7691             "     \"line\";",
7692             format("a << \"line line line\";",
7693                    getLLVMStyleWithColumns(20)));
7694   // In a chain of << with three operands, the second can be broken with no line
7695   // break before it.
7696   EXPECT_EQ("abcde << \"line \"\n"
7697             "         \"line line\"\n"
7698             "      << c;",
7699             format("abcde << \"line line line\" << c;",
7700                    getLLVMStyleWithColumns(20)));
7701   // In a chain of << with three operands, the third must be broken with a line
7702   // break before it.
7703   EXPECT_EQ("a << b\n"
7704             "  << \"line line \"\n"
7705             "     \"line\";",
7706             format("a << b << \"line line line\";",
7707                    getLLVMStyleWithColumns(20)));
7708   // In a chain of << with three operands, the second can be broken with no line
7709   // break before it and the third must be broken with a line break before it.
7710   EXPECT_EQ("abcd << \"line line \"\n"
7711             "        \"line\"\n"
7712             "     << \"line line \"\n"
7713             "        \"line\";",
7714             format("abcd << \"line line line\" << \"line line line\";",
7715                    getLLVMStyleWithColumns(20)));
7716   // In a chain of binary operators with two operands, the second can be broken
7717   // with no line break before it.
7718   EXPECT_EQ("abcd + \"line line \"\n"
7719             "       \"line line\";",
7720             format("abcd + \"line line line line\";",
7721                    getLLVMStyleWithColumns(20)));
7722   // In a chain of binary operators with three operands, the second must be
7723   // broken with a line break before it.
7724   EXPECT_EQ("abcd +\n"
7725             "    \"line line \"\n"
7726             "    \"line line\" +\n"
7727             "    e;",
7728             format("abcd + \"line line line line\" + e;",
7729                    getLLVMStyleWithColumns(20)));
7730   // In a function call with two operands, with AlignAfterOpenBracket enabled,
7731   // the first must be broken with a line break before it.
7732   FormatStyle Style = getLLVMStyleWithColumns(25);
7733   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7734   EXPECT_EQ("someFunction(\n"
7735             "    \"long long long \"\n"
7736             "    \"long\",\n"
7737             "    a);",
7738             format("someFunction(\"long long long long\", a);", Style));
7739 }
7740 
7741 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7742   EXPECT_EQ(
7743       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7744       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7745       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7746       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7747              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7748              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7749 }
7750 
7751 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7752   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7753             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7754   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7755             "multiline raw string literal xxxxxxxxxxxxxx\n"
7756             ")x\",\n"
7757             "              a),\n"
7758             "            b);",
7759             format("fffffffffff(g(R\"x(\n"
7760                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7761                    ")x\", a), b);",
7762                    getGoogleStyleWithColumns(20)));
7763   EXPECT_EQ("fffffffffff(\n"
7764             "    g(R\"x(qqq\n"
7765             "multiline raw string literal xxxxxxxxxxxxxx\n"
7766             ")x\",\n"
7767             "      a),\n"
7768             "    b);",
7769             format("fffffffffff(g(R\"x(qqq\n"
7770                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7771                    ")x\", a), b);",
7772                    getGoogleStyleWithColumns(20)));
7773 
7774   EXPECT_EQ("fffffffffff(R\"x(\n"
7775             "multiline raw string literal xxxxxxxxxxxxxx\n"
7776             ")x\");",
7777             format("fffffffffff(R\"x(\n"
7778                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7779                    ")x\");",
7780                    getGoogleStyleWithColumns(20)));
7781   EXPECT_EQ("fffffffffff(R\"x(\n"
7782             "multiline raw string literal xxxxxxxxxxxxxx\n"
7783             ")x\" + bbbbbb);",
7784             format("fffffffffff(R\"x(\n"
7785                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7786                    ")x\" +   bbbbbb);",
7787                    getGoogleStyleWithColumns(20)));
7788   EXPECT_EQ("fffffffffff(\n"
7789             "    R\"x(\n"
7790             "multiline raw string literal xxxxxxxxxxxxxx\n"
7791             ")x\" +\n"
7792             "    bbbbbb);",
7793             format("fffffffffff(\n"
7794                    " R\"x(\n"
7795                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7796                    ")x\" + bbbbbb);",
7797                    getGoogleStyleWithColumns(20)));
7798 }
7799 
7800 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7801   verifyFormat("string a = \"unterminated;");
7802   EXPECT_EQ("function(\"unterminated,\n"
7803             "         OtherParameter);",
7804             format("function(  \"unterminated,\n"
7805                    "    OtherParameter);"));
7806 }
7807 
7808 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7809   FormatStyle Style = getLLVMStyle();
7810   Style.Standard = FormatStyle::LS_Cpp03;
7811   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7812             format("#define x(_a) printf(\"foo\"_a);", Style));
7813 }
7814 
7815 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7816 
7817 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7818   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7819             "             \"ddeeefff\");",
7820             format("someFunction(\"aaabbbcccdddeeefff\");",
7821                    getLLVMStyleWithColumns(25)));
7822   EXPECT_EQ("someFunction1234567890(\n"
7823             "    \"aaabbbcccdddeeefff\");",
7824             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7825                    getLLVMStyleWithColumns(26)));
7826   EXPECT_EQ("someFunction1234567890(\n"
7827             "    \"aaabbbcccdddeeeff\"\n"
7828             "    \"f\");",
7829             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7830                    getLLVMStyleWithColumns(25)));
7831   EXPECT_EQ("someFunction1234567890(\n"
7832             "    \"aaabbbcccdddeeeff\"\n"
7833             "    \"f\");",
7834             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7835                    getLLVMStyleWithColumns(24)));
7836   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7837             "             \"ddde \"\n"
7838             "             \"efff\");",
7839             format("someFunction(\"aaabbbcc ddde efff\");",
7840                    getLLVMStyleWithColumns(25)));
7841   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7842             "             \"ddeeefff\");",
7843             format("someFunction(\"aaabbbccc ddeeefff\");",
7844                    getLLVMStyleWithColumns(25)));
7845   EXPECT_EQ("someFunction1234567890(\n"
7846             "    \"aaabb \"\n"
7847             "    \"cccdddeeefff\");",
7848             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7849                    getLLVMStyleWithColumns(25)));
7850   EXPECT_EQ("#define A          \\\n"
7851             "  string s =       \\\n"
7852             "      \"123456789\"  \\\n"
7853             "      \"0\";         \\\n"
7854             "  int i;",
7855             format("#define A string s = \"1234567890\"; int i;",
7856                    getLLVMStyleWithColumns(20)));
7857   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7858   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7859             "             \"dddeeeff\"\n"
7860             "             \"f\");",
7861             format("someFunction(\"aaabbbcc dddeeefff\");",
7862                    getLLVMStyleWithColumns(25)));
7863 }
7864 
7865 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7866   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7867   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7868   EXPECT_EQ("\"test\"\n"
7869             "\"\\n\"",
7870             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7871   EXPECT_EQ("\"tes\\\\\"\n"
7872             "\"n\"",
7873             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7874   EXPECT_EQ("\"\\\\\\\\\"\n"
7875             "\"\\n\"",
7876             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7877   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7878   EXPECT_EQ("\"\\uff01\"\n"
7879             "\"test\"",
7880             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7881   EXPECT_EQ("\"\\Uff01ff02\"",
7882             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7883   EXPECT_EQ("\"\\x000000000001\"\n"
7884             "\"next\"",
7885             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7886   EXPECT_EQ("\"\\x000000000001next\"",
7887             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7888   EXPECT_EQ("\"\\x000000000001\"",
7889             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7890   EXPECT_EQ("\"test\"\n"
7891             "\"\\000000\"\n"
7892             "\"000001\"",
7893             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7894   EXPECT_EQ("\"test\\000\"\n"
7895             "\"00000000\"\n"
7896             "\"1\"",
7897             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7898 }
7899 
7900 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7901   verifyFormat("void f() {\n"
7902                "  return g() {}\n"
7903                "  void h() {}");
7904   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7905                "g();\n"
7906                "}");
7907 }
7908 
7909 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7910   verifyFormat(
7911       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7912 }
7913 
7914 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7915   verifyFormat("class X {\n"
7916                "  void f() {\n"
7917                "  }\n"
7918                "};",
7919                getLLVMStyleWithColumns(12));
7920 }
7921 
7922 TEST_F(FormatTest, ConfigurableIndentWidth) {
7923   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7924   EightIndent.IndentWidth = 8;
7925   EightIndent.ContinuationIndentWidth = 8;
7926   verifyFormat("void f() {\n"
7927                "        someFunction();\n"
7928                "        if (true) {\n"
7929                "                f();\n"
7930                "        }\n"
7931                "}",
7932                EightIndent);
7933   verifyFormat("class X {\n"
7934                "        void f() {\n"
7935                "        }\n"
7936                "};",
7937                EightIndent);
7938   verifyFormat("int x[] = {\n"
7939                "        call(),\n"
7940                "        call()};",
7941                EightIndent);
7942 }
7943 
7944 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7945   verifyFormat("double\n"
7946                "f();",
7947                getLLVMStyleWithColumns(8));
7948 }
7949 
7950 TEST_F(FormatTest, ConfigurableUseOfTab) {
7951   FormatStyle Tab = getLLVMStyleWithColumns(42);
7952   Tab.IndentWidth = 8;
7953   Tab.UseTab = FormatStyle::UT_Always;
7954   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7955 
7956   EXPECT_EQ("if (aaaaaaaa && // q\n"
7957             "    bb)\t\t// w\n"
7958             "\t;",
7959             format("if (aaaaaaaa &&// q\n"
7960                    "bb)// w\n"
7961                    ";",
7962                    Tab));
7963   EXPECT_EQ("if (aaa && bbb) // w\n"
7964             "\t;",
7965             format("if(aaa&&bbb)// w\n"
7966                    ";",
7967                    Tab));
7968 
7969   verifyFormat("class X {\n"
7970                "\tvoid f() {\n"
7971                "\t\tsomeFunction(parameter1,\n"
7972                "\t\t\t     parameter2);\n"
7973                "\t}\n"
7974                "};",
7975                Tab);
7976   verifyFormat("#define A                        \\\n"
7977                "\tvoid f() {               \\\n"
7978                "\t\tsomeFunction(    \\\n"
7979                "\t\t    parameter1,  \\\n"
7980                "\t\t    parameter2); \\\n"
7981                "\t}",
7982                Tab);
7983 
7984   Tab.TabWidth = 4;
7985   Tab.IndentWidth = 8;
7986   verifyFormat("class TabWidth4Indent8 {\n"
7987                "\t\tvoid f() {\n"
7988                "\t\t\t\tsomeFunction(parameter1,\n"
7989                "\t\t\t\t\t\t\t parameter2);\n"
7990                "\t\t}\n"
7991                "};",
7992                Tab);
7993 
7994   Tab.TabWidth = 4;
7995   Tab.IndentWidth = 4;
7996   verifyFormat("class TabWidth4Indent4 {\n"
7997                "\tvoid f() {\n"
7998                "\t\tsomeFunction(parameter1,\n"
7999                "\t\t\t\t\t parameter2);\n"
8000                "\t}\n"
8001                "};",
8002                Tab);
8003 
8004   Tab.TabWidth = 8;
8005   Tab.IndentWidth = 4;
8006   verifyFormat("class TabWidth8Indent4 {\n"
8007                "    void f() {\n"
8008                "\tsomeFunction(parameter1,\n"
8009                "\t\t     parameter2);\n"
8010                "    }\n"
8011                "};",
8012                Tab);
8013 
8014   Tab.TabWidth = 8;
8015   Tab.IndentWidth = 8;
8016   EXPECT_EQ("/*\n"
8017             "\t      a\t\tcomment\n"
8018             "\t      in multiple lines\n"
8019             "       */",
8020             format("   /*\t \t \n"
8021                    " \t \t a\t\tcomment\t \t\n"
8022                    " \t \t in multiple lines\t\n"
8023                    " \t  */",
8024                    Tab));
8025 
8026   Tab.UseTab = FormatStyle::UT_ForIndentation;
8027   verifyFormat("{\n"
8028                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8029                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8030                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8031                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8032                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8033                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8034                "};",
8035                Tab);
8036   verifyFormat("enum AA {\n"
8037                "\ta1, // Force multiple lines\n"
8038                "\ta2,\n"
8039                "\ta3\n"
8040                "};",
8041                Tab);
8042   EXPECT_EQ("if (aaaaaaaa && // q\n"
8043             "    bb)         // w\n"
8044             "\t;",
8045             format("if (aaaaaaaa &&// q\n"
8046                    "bb)// w\n"
8047                    ";",
8048                    Tab));
8049   verifyFormat("class X {\n"
8050                "\tvoid f() {\n"
8051                "\t\tsomeFunction(parameter1,\n"
8052                "\t\t             parameter2);\n"
8053                "\t}\n"
8054                "};",
8055                Tab);
8056   verifyFormat("{\n"
8057                "\tQ(\n"
8058                "\t    {\n"
8059                "\t\t    int a;\n"
8060                "\t\t    someFunction(aaaaaaaa,\n"
8061                "\t\t                 bbbbbbb);\n"
8062                "\t    },\n"
8063                "\t    p);\n"
8064                "}",
8065                Tab);
8066   EXPECT_EQ("{\n"
8067             "\t/* aaaa\n"
8068             "\t   bbbb */\n"
8069             "}",
8070             format("{\n"
8071                    "/* aaaa\n"
8072                    "   bbbb */\n"
8073                    "}",
8074                    Tab));
8075   EXPECT_EQ("{\n"
8076             "\t/*\n"
8077             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8078             "\t  bbbbbbbbbbbbb\n"
8079             "\t*/\n"
8080             "}",
8081             format("{\n"
8082                    "/*\n"
8083                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8084                    "*/\n"
8085                    "}",
8086                    Tab));
8087   EXPECT_EQ("{\n"
8088             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8089             "\t// bbbbbbbbbbbbb\n"
8090             "}",
8091             format("{\n"
8092                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8093                    "}",
8094                    Tab));
8095   EXPECT_EQ("{\n"
8096             "\t/*\n"
8097             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8098             "\t  bbbbbbbbbbbbb\n"
8099             "\t*/\n"
8100             "}",
8101             format("{\n"
8102                    "\t/*\n"
8103                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8104                    "\t*/\n"
8105                    "}",
8106                    Tab));
8107   EXPECT_EQ("{\n"
8108             "\t/*\n"
8109             "\n"
8110             "\t*/\n"
8111             "}",
8112             format("{\n"
8113                    "\t/*\n"
8114                    "\n"
8115                    "\t*/\n"
8116                    "}",
8117                    Tab));
8118   EXPECT_EQ("{\n"
8119             "\t/*\n"
8120             " asdf\n"
8121             "\t*/\n"
8122             "}",
8123             format("{\n"
8124                    "\t/*\n"
8125                    " asdf\n"
8126                    "\t*/\n"
8127                    "}",
8128                    Tab));
8129 
8130   Tab.UseTab = FormatStyle::UT_Never;
8131   EXPECT_EQ("/*\n"
8132             "              a\t\tcomment\n"
8133             "              in multiple lines\n"
8134             "       */",
8135             format("   /*\t \t \n"
8136                    " \t \t a\t\tcomment\t \t\n"
8137                    " \t \t in multiple lines\t\n"
8138                    " \t  */",
8139                    Tab));
8140   EXPECT_EQ("/* some\n"
8141             "   comment */",
8142             format(" \t \t /* some\n"
8143                    " \t \t    comment */",
8144                    Tab));
8145   EXPECT_EQ("int a; /* some\n"
8146             "   comment */",
8147             format(" \t \t int a; /* some\n"
8148                    " \t \t    comment */",
8149                    Tab));
8150 
8151   EXPECT_EQ("int a; /* some\n"
8152             "comment */",
8153             format(" \t \t int\ta; /* some\n"
8154                    " \t \t    comment */",
8155                    Tab));
8156   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8157             "    comment */",
8158             format(" \t \t f(\"\t\t\"); /* some\n"
8159                    " \t \t    comment */",
8160                    Tab));
8161   EXPECT_EQ("{\n"
8162             "  /*\n"
8163             "   * Comment\n"
8164             "   */\n"
8165             "  int i;\n"
8166             "}",
8167             format("{\n"
8168                    "\t/*\n"
8169                    "\t * Comment\n"
8170                    "\t */\n"
8171                    "\t int i;\n"
8172                    "}"));
8173 
8174   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8175   Tab.TabWidth = 8;
8176   Tab.IndentWidth = 8;
8177   EXPECT_EQ("if (aaaaaaaa && // q\n"
8178             "    bb)         // w\n"
8179             "\t;",
8180             format("if (aaaaaaaa &&// q\n"
8181                    "bb)// w\n"
8182                    ";",
8183                    Tab));
8184   EXPECT_EQ("if (aaa && bbb) // w\n"
8185             "\t;",
8186             format("if(aaa&&bbb)// w\n"
8187                    ";",
8188                    Tab));
8189   verifyFormat("class X {\n"
8190                "\tvoid f() {\n"
8191                "\t\tsomeFunction(parameter1,\n"
8192                "\t\t\t     parameter2);\n"
8193                "\t}\n"
8194                "};",
8195                Tab);
8196   verifyFormat("#define A                        \\\n"
8197                "\tvoid f() {               \\\n"
8198                "\t\tsomeFunction(    \\\n"
8199                "\t\t    parameter1,  \\\n"
8200                "\t\t    parameter2); \\\n"
8201                "\t}",
8202                Tab);
8203   Tab.TabWidth = 4;
8204   Tab.IndentWidth = 8;
8205   verifyFormat("class TabWidth4Indent8 {\n"
8206                "\t\tvoid f() {\n"
8207                "\t\t\t\tsomeFunction(parameter1,\n"
8208                "\t\t\t\t\t\t\t parameter2);\n"
8209                "\t\t}\n"
8210                "};",
8211                Tab);
8212   Tab.TabWidth = 4;
8213   Tab.IndentWidth = 4;
8214   verifyFormat("class TabWidth4Indent4 {\n"
8215                "\tvoid f() {\n"
8216                "\t\tsomeFunction(parameter1,\n"
8217                "\t\t\t\t\t parameter2);\n"
8218                "\t}\n"
8219                "};",
8220                Tab);
8221   Tab.TabWidth = 8;
8222   Tab.IndentWidth = 4;
8223   verifyFormat("class TabWidth8Indent4 {\n"
8224                "    void f() {\n"
8225                "\tsomeFunction(parameter1,\n"
8226                "\t\t     parameter2);\n"
8227                "    }\n"
8228                "};",
8229                Tab);
8230   Tab.TabWidth = 8;
8231   Tab.IndentWidth = 8;
8232   EXPECT_EQ("/*\n"
8233             "\t      a\t\tcomment\n"
8234             "\t      in multiple lines\n"
8235             "       */",
8236             format("   /*\t \t \n"
8237                    " \t \t a\t\tcomment\t \t\n"
8238                    " \t \t in multiple lines\t\n"
8239                    " \t  */",
8240                    Tab));
8241   verifyFormat("{\n"
8242                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8243                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8244                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8245                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8246                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8247                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8248                "};",
8249                Tab);
8250   verifyFormat("enum AA {\n"
8251                "\ta1, // Force multiple lines\n"
8252                "\ta2,\n"
8253                "\ta3\n"
8254                "};",
8255                Tab);
8256   EXPECT_EQ("if (aaaaaaaa && // q\n"
8257             "    bb)         // w\n"
8258             "\t;",
8259             format("if (aaaaaaaa &&// q\n"
8260                    "bb)// w\n"
8261                    ";",
8262                    Tab));
8263   verifyFormat("class X {\n"
8264                "\tvoid f() {\n"
8265                "\t\tsomeFunction(parameter1,\n"
8266                "\t\t\t     parameter2);\n"
8267                "\t}\n"
8268                "};",
8269                Tab);
8270   verifyFormat("{\n"
8271                "\tQ(\n"
8272                "\t    {\n"
8273                "\t\t    int a;\n"
8274                "\t\t    someFunction(aaaaaaaa,\n"
8275                "\t\t\t\t bbbbbbb);\n"
8276                "\t    },\n"
8277                "\t    p);\n"
8278                "}",
8279                Tab);
8280   EXPECT_EQ("{\n"
8281             "\t/* aaaa\n"
8282             "\t   bbbb */\n"
8283             "}",
8284             format("{\n"
8285                    "/* aaaa\n"
8286                    "   bbbb */\n"
8287                    "}",
8288                    Tab));
8289   EXPECT_EQ("{\n"
8290             "\t/*\n"
8291             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8292             "\t  bbbbbbbbbbbbb\n"
8293             "\t*/\n"
8294             "}",
8295             format("{\n"
8296                    "/*\n"
8297                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8298                    "*/\n"
8299                    "}",
8300                    Tab));
8301   EXPECT_EQ("{\n"
8302             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8303             "\t// bbbbbbbbbbbbb\n"
8304             "}",
8305             format("{\n"
8306                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8307                    "}",
8308                    Tab));
8309   EXPECT_EQ("{\n"
8310             "\t/*\n"
8311             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8312             "\t  bbbbbbbbbbbbb\n"
8313             "\t*/\n"
8314             "}",
8315             format("{\n"
8316                    "\t/*\n"
8317                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8318                    "\t*/\n"
8319                    "}",
8320                    Tab));
8321   EXPECT_EQ("{\n"
8322             "\t/*\n"
8323             "\n"
8324             "\t*/\n"
8325             "}",
8326             format("{\n"
8327                    "\t/*\n"
8328                    "\n"
8329                    "\t*/\n"
8330                    "}",
8331                    Tab));
8332   EXPECT_EQ("{\n"
8333             "\t/*\n"
8334             " asdf\n"
8335             "\t*/\n"
8336             "}",
8337             format("{\n"
8338                    "\t/*\n"
8339                    " asdf\n"
8340                    "\t*/\n"
8341                    "}",
8342                    Tab));
8343   EXPECT_EQ("/*\n"
8344             "\t      a\t\tcomment\n"
8345             "\t      in multiple lines\n"
8346             "       */",
8347             format("   /*\t \t \n"
8348                    " \t \t a\t\tcomment\t \t\n"
8349                    " \t \t in multiple lines\t\n"
8350                    " \t  */",
8351                    Tab));
8352   EXPECT_EQ("/* some\n"
8353             "   comment */",
8354             format(" \t \t /* some\n"
8355                    " \t \t    comment */",
8356                    Tab));
8357   EXPECT_EQ("int a; /* some\n"
8358             "   comment */",
8359             format(" \t \t int a; /* some\n"
8360                    " \t \t    comment */",
8361                    Tab));
8362   EXPECT_EQ("int a; /* some\n"
8363             "comment */",
8364             format(" \t \t int\ta; /* some\n"
8365                    " \t \t    comment */",
8366                    Tab));
8367   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8368             "    comment */",
8369             format(" \t \t f(\"\t\t\"); /* some\n"
8370                    " \t \t    comment */",
8371                    Tab));
8372   EXPECT_EQ("{\n"
8373             "  /*\n"
8374             "   * Comment\n"
8375             "   */\n"
8376             "  int i;\n"
8377             "}",
8378             format("{\n"
8379                    "\t/*\n"
8380                    "\t * Comment\n"
8381                    "\t */\n"
8382                    "\t int i;\n"
8383                    "}"));
8384   Tab.AlignConsecutiveAssignments = true;
8385   Tab.AlignConsecutiveDeclarations = true;
8386   Tab.TabWidth = 4;
8387   Tab.IndentWidth = 4;
8388   verifyFormat("class Assign {\n"
8389                "\tvoid f() {\n"
8390                "\t\tint         x      = 123;\n"
8391                "\t\tint         random = 4;\n"
8392                "\t\tstd::string alphabet =\n"
8393                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8394                "\t}\n"
8395                "};",
8396                Tab);
8397 }
8398 
8399 TEST_F(FormatTest, CalculatesOriginalColumn) {
8400   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8401             "q\"; /* some\n"
8402             "       comment */",
8403             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8404                    "q\"; /* some\n"
8405                    "       comment */",
8406                    getLLVMStyle()));
8407   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8408             "/* some\n"
8409             "   comment */",
8410             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8411                    " /* some\n"
8412                    "    comment */",
8413                    getLLVMStyle()));
8414   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8415             "qqq\n"
8416             "/* some\n"
8417             "   comment */",
8418             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8419                    "qqq\n"
8420                    " /* some\n"
8421                    "    comment */",
8422                    getLLVMStyle()));
8423   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8424             "wwww; /* some\n"
8425             "         comment */",
8426             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8427                    "wwww; /* some\n"
8428                    "         comment */",
8429                    getLLVMStyle()));
8430 }
8431 
8432 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8433   FormatStyle NoSpace = getLLVMStyle();
8434   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8435 
8436   verifyFormat("while(true)\n"
8437                "  continue;",
8438                NoSpace);
8439   verifyFormat("for(;;)\n"
8440                "  continue;",
8441                NoSpace);
8442   verifyFormat("if(true)\n"
8443                "  f();\n"
8444                "else if(true)\n"
8445                "  f();",
8446                NoSpace);
8447   verifyFormat("do {\n"
8448                "  do_something();\n"
8449                "} while(something());",
8450                NoSpace);
8451   verifyFormat("switch(x) {\n"
8452                "default:\n"
8453                "  break;\n"
8454                "}",
8455                NoSpace);
8456   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8457   verifyFormat("size_t x = sizeof(x);", NoSpace);
8458   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8459   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8460   verifyFormat("alignas(128) char a[128];", NoSpace);
8461   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8462   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8463   verifyFormat("int f() throw(Deprecated);", NoSpace);
8464   verifyFormat("typedef void (*cb)(int);", NoSpace);
8465   verifyFormat("T A::operator()();", NoSpace);
8466   verifyFormat("X A::operator++(T);", NoSpace);
8467 
8468   FormatStyle Space = getLLVMStyle();
8469   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8470 
8471   verifyFormat("int f ();", Space);
8472   verifyFormat("void f (int a, T b) {\n"
8473                "  while (true)\n"
8474                "    continue;\n"
8475                "}",
8476                Space);
8477   verifyFormat("if (true)\n"
8478                "  f ();\n"
8479                "else if (true)\n"
8480                "  f ();",
8481                Space);
8482   verifyFormat("do {\n"
8483                "  do_something ();\n"
8484                "} while (something ());",
8485                Space);
8486   verifyFormat("switch (x) {\n"
8487                "default:\n"
8488                "  break;\n"
8489                "}",
8490                Space);
8491   verifyFormat("A::A () : a (1) {}", Space);
8492   verifyFormat("void f () __attribute__ ((asdf));", Space);
8493   verifyFormat("*(&a + 1);\n"
8494                "&((&a)[1]);\n"
8495                "a[(b + c) * d];\n"
8496                "(((a + 1) * 2) + 3) * 4;",
8497                Space);
8498   verifyFormat("#define A(x) x", Space);
8499   verifyFormat("#define A (x) x", Space);
8500   verifyFormat("#if defined(x)\n"
8501                "#endif",
8502                Space);
8503   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8504   verifyFormat("size_t x = sizeof (x);", Space);
8505   verifyFormat("auto f (int x) -> decltype (x);", Space);
8506   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8507   verifyFormat("alignas (128) char a[128];", Space);
8508   verifyFormat("size_t x = alignof (MyType);", Space);
8509   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8510   verifyFormat("int f () throw (Deprecated);", Space);
8511   verifyFormat("typedef void (*cb) (int);", Space);
8512   verifyFormat("T A::operator() ();", Space);
8513   verifyFormat("X A::operator++ (T);", Space);
8514 }
8515 
8516 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8517   FormatStyle Spaces = getLLVMStyle();
8518 
8519   Spaces.SpacesInParentheses = true;
8520   verifyFormat("call( x, y, z );", Spaces);
8521   verifyFormat("call();", Spaces);
8522   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8523   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8524                Spaces);
8525   verifyFormat("while ( (bool)1 )\n"
8526                "  continue;",
8527                Spaces);
8528   verifyFormat("for ( ;; )\n"
8529                "  continue;",
8530                Spaces);
8531   verifyFormat("if ( true )\n"
8532                "  f();\n"
8533                "else if ( true )\n"
8534                "  f();",
8535                Spaces);
8536   verifyFormat("do {\n"
8537                "  do_something( (int)i );\n"
8538                "} while ( something() );",
8539                Spaces);
8540   verifyFormat("switch ( x ) {\n"
8541                "default:\n"
8542                "  break;\n"
8543                "}",
8544                Spaces);
8545 
8546   Spaces.SpacesInParentheses = false;
8547   Spaces.SpacesInCStyleCastParentheses = true;
8548   verifyFormat("Type *A = ( Type * )P;", Spaces);
8549   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8550   verifyFormat("x = ( int32 )y;", Spaces);
8551   verifyFormat("int a = ( int )(2.0f);", Spaces);
8552   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8553   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8554   verifyFormat("#define x (( int )-1)", Spaces);
8555 
8556   // Run the first set of tests again with:
8557   Spaces.SpacesInParentheses = false;
8558   Spaces.SpaceInEmptyParentheses = true;
8559   Spaces.SpacesInCStyleCastParentheses = true;
8560   verifyFormat("call(x, y, z);", Spaces);
8561   verifyFormat("call( );", Spaces);
8562   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8563   verifyFormat("while (( bool )1)\n"
8564                "  continue;",
8565                Spaces);
8566   verifyFormat("for (;;)\n"
8567                "  continue;",
8568                Spaces);
8569   verifyFormat("if (true)\n"
8570                "  f( );\n"
8571                "else if (true)\n"
8572                "  f( );",
8573                Spaces);
8574   verifyFormat("do {\n"
8575                "  do_something(( int )i);\n"
8576                "} while (something( ));",
8577                Spaces);
8578   verifyFormat("switch (x) {\n"
8579                "default:\n"
8580                "  break;\n"
8581                "}",
8582                Spaces);
8583 
8584   // Run the first set of tests again with:
8585   Spaces.SpaceAfterCStyleCast = true;
8586   verifyFormat("call(x, y, z);", Spaces);
8587   verifyFormat("call( );", Spaces);
8588   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8589   verifyFormat("while (( bool ) 1)\n"
8590                "  continue;",
8591                Spaces);
8592   verifyFormat("for (;;)\n"
8593                "  continue;",
8594                Spaces);
8595   verifyFormat("if (true)\n"
8596                "  f( );\n"
8597                "else if (true)\n"
8598                "  f( );",
8599                Spaces);
8600   verifyFormat("do {\n"
8601                "  do_something(( int ) i);\n"
8602                "} while (something( ));",
8603                Spaces);
8604   verifyFormat("switch (x) {\n"
8605                "default:\n"
8606                "  break;\n"
8607                "}",
8608                Spaces);
8609 
8610   // Run subset of tests again with:
8611   Spaces.SpacesInCStyleCastParentheses = false;
8612   Spaces.SpaceAfterCStyleCast = true;
8613   verifyFormat("while ((bool) 1)\n"
8614                "  continue;",
8615                Spaces);
8616   verifyFormat("do {\n"
8617                "  do_something((int) i);\n"
8618                "} while (something( ));",
8619                Spaces);
8620 }
8621 
8622 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8623   verifyFormat("int a[5];");
8624   verifyFormat("a[3] += 42;");
8625 
8626   FormatStyle Spaces = getLLVMStyle();
8627   Spaces.SpacesInSquareBrackets = true;
8628   // Lambdas unchanged.
8629   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8630   verifyFormat("return [i, args...] {};", Spaces);
8631 
8632   // Not lambdas.
8633   verifyFormat("int a[ 5 ];", Spaces);
8634   verifyFormat("a[ 3 ] += 42;", Spaces);
8635   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8636   verifyFormat("double &operator[](int i) { return 0; }\n"
8637                "int i;",
8638                Spaces);
8639   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8640   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8641   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8642 }
8643 
8644 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8645   verifyFormat("int a = 5;");
8646   verifyFormat("a += 42;");
8647   verifyFormat("a or_eq 8;");
8648 
8649   FormatStyle Spaces = getLLVMStyle();
8650   Spaces.SpaceBeforeAssignmentOperators = false;
8651   verifyFormat("int a= 5;", Spaces);
8652   verifyFormat("a+= 42;", Spaces);
8653   verifyFormat("a or_eq 8;", Spaces);
8654 }
8655 
8656 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8657   FormatStyle Alignment = getLLVMStyle();
8658   Alignment.AlignConsecutiveAssignments = false;
8659   verifyFormat("int a = 5;\n"
8660                "int oneTwoThree = 123;",
8661                Alignment);
8662   verifyFormat("int a = 5;\n"
8663                "int oneTwoThree = 123;",
8664                Alignment);
8665 
8666   Alignment.AlignConsecutiveAssignments = true;
8667   verifyFormat("int a           = 5;\n"
8668                "int oneTwoThree = 123;",
8669                Alignment);
8670   verifyFormat("int a           = method();\n"
8671                "int oneTwoThree = 133;",
8672                Alignment);
8673   verifyFormat("a &= 5;\n"
8674                "bcd *= 5;\n"
8675                "ghtyf += 5;\n"
8676                "dvfvdb -= 5;\n"
8677                "a /= 5;\n"
8678                "vdsvsv %= 5;\n"
8679                "sfdbddfbdfbb ^= 5;\n"
8680                "dvsdsv |= 5;\n"
8681                "int dsvvdvsdvvv = 123;",
8682                Alignment);
8683   verifyFormat("int i = 1, j = 10;\n"
8684                "something = 2000;",
8685                Alignment);
8686   verifyFormat("something = 2000;\n"
8687                "int i = 1, j = 10;\n",
8688                Alignment);
8689   verifyFormat("something = 2000;\n"
8690                "another   = 911;\n"
8691                "int i = 1, j = 10;\n"
8692                "oneMore = 1;\n"
8693                "i       = 2;",
8694                Alignment);
8695   verifyFormat("int a   = 5;\n"
8696                "int one = 1;\n"
8697                "method();\n"
8698                "int oneTwoThree = 123;\n"
8699                "int oneTwo      = 12;",
8700                Alignment);
8701   verifyFormat("int oneTwoThree = 123;\n"
8702                "int oneTwo      = 12;\n"
8703                "method();\n",
8704                Alignment);
8705   verifyFormat("int oneTwoThree = 123; // comment\n"
8706                "int oneTwo      = 12;  // comment",
8707                Alignment);
8708   EXPECT_EQ("int a = 5;\n"
8709             "\n"
8710             "int oneTwoThree = 123;",
8711             format("int a       = 5;\n"
8712                    "\n"
8713                    "int oneTwoThree= 123;",
8714                    Alignment));
8715   EXPECT_EQ("int a   = 5;\n"
8716             "int one = 1;\n"
8717             "\n"
8718             "int oneTwoThree = 123;",
8719             format("int a = 5;\n"
8720                    "int one = 1;\n"
8721                    "\n"
8722                    "int oneTwoThree = 123;",
8723                    Alignment));
8724   EXPECT_EQ("int a   = 5;\n"
8725             "int one = 1;\n"
8726             "\n"
8727             "int oneTwoThree = 123;\n"
8728             "int oneTwo      = 12;",
8729             format("int a = 5;\n"
8730                    "int one = 1;\n"
8731                    "\n"
8732                    "int oneTwoThree = 123;\n"
8733                    "int oneTwo = 12;",
8734                    Alignment));
8735   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8736   verifyFormat("#define A \\\n"
8737                "  int aaaa       = 12; \\\n"
8738                "  int b          = 23; \\\n"
8739                "  int ccc        = 234; \\\n"
8740                "  int dddddddddd = 2345;",
8741                Alignment);
8742   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8743   verifyFormat("#define A               \\\n"
8744                "  int aaaa       = 12;  \\\n"
8745                "  int b          = 23;  \\\n"
8746                "  int ccc        = 234; \\\n"
8747                "  int dddddddddd = 2345;",
8748                Alignment);
8749   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8750   verifyFormat("#define A                                                      "
8751                "                \\\n"
8752                "  int aaaa       = 12;                                         "
8753                "                \\\n"
8754                "  int b          = 23;                                         "
8755                "                \\\n"
8756                "  int ccc        = 234;                                        "
8757                "                \\\n"
8758                "  int dddddddddd = 2345;",
8759                Alignment);
8760   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8761                "k = 4, int l = 5,\n"
8762                "                  int m = 6) {\n"
8763                "  int j      = 10;\n"
8764                "  otherThing = 1;\n"
8765                "}",
8766                Alignment);
8767   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8768                "  int i   = 1;\n"
8769                "  int j   = 2;\n"
8770                "  int big = 10000;\n"
8771                "}",
8772                Alignment);
8773   verifyFormat("class C {\n"
8774                "public:\n"
8775                "  int i            = 1;\n"
8776                "  virtual void f() = 0;\n"
8777                "};",
8778                Alignment);
8779   verifyFormat("int i = 1;\n"
8780                "if (SomeType t = getSomething()) {\n"
8781                "}\n"
8782                "int j   = 2;\n"
8783                "int big = 10000;",
8784                Alignment);
8785   verifyFormat("int j = 7;\n"
8786                "for (int k = 0; k < N; ++k) {\n"
8787                "}\n"
8788                "int j   = 2;\n"
8789                "int big = 10000;\n"
8790                "}",
8791                Alignment);
8792   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8793   verifyFormat("int i = 1;\n"
8794                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8795                "    = someLooooooooooooooooongFunction();\n"
8796                "int j = 2;",
8797                Alignment);
8798   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8799   verifyFormat("int i = 1;\n"
8800                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8801                "    someLooooooooooooooooongFunction();\n"
8802                "int j = 2;",
8803                Alignment);
8804 
8805   verifyFormat("auto lambda = []() {\n"
8806                "  auto i = 0;\n"
8807                "  return 0;\n"
8808                "};\n"
8809                "int i  = 0;\n"
8810                "auto v = type{\n"
8811                "    i = 1,   //\n"
8812                "    (i = 2), //\n"
8813                "    i = 3    //\n"
8814                "};",
8815                Alignment);
8816 
8817   verifyFormat(
8818       "int i      = 1;\n"
8819       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8820       "                          loooooooooooooooooooooongParameterB);\n"
8821       "int j      = 2;",
8822       Alignment);
8823 
8824   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8825                "          typename B   = very_long_type_name_1,\n"
8826                "          typename T_2 = very_long_type_name_2>\n"
8827                "auto foo() {}\n",
8828                Alignment);
8829   verifyFormat("int 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   verifyFormat("for (int i = 0; i < 1; i++)\n"
8837                "  int x = 1;\n",
8838                Alignment);
8839   verifyFormat("for (i = 0; i < 1; i++)\n"
8840                "  x = 1;\n"
8841                "y = 1;\n",
8842                Alignment);
8843 }
8844 
8845 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8846   FormatStyle Alignment = getLLVMStyle();
8847   Alignment.AlignConsecutiveDeclarations = false;
8848   verifyFormat("float const a = 5;\n"
8849                "int oneTwoThree = 123;",
8850                Alignment);
8851   verifyFormat("int a = 5;\n"
8852                "float const oneTwoThree = 123;",
8853                Alignment);
8854 
8855   Alignment.AlignConsecutiveDeclarations = true;
8856   verifyFormat("float const a = 5;\n"
8857                "int         oneTwoThree = 123;",
8858                Alignment);
8859   verifyFormat("int         a = method();\n"
8860                "float const oneTwoThree = 133;",
8861                Alignment);
8862   verifyFormat("int i = 1, j = 10;\n"
8863                "something = 2000;",
8864                Alignment);
8865   verifyFormat("something = 2000;\n"
8866                "int i = 1, j = 10;\n",
8867                Alignment);
8868   verifyFormat("float      something = 2000;\n"
8869                "double     another = 911;\n"
8870                "int        i = 1, j = 10;\n"
8871                "const int *oneMore = 1;\n"
8872                "unsigned   i = 2;",
8873                Alignment);
8874   verifyFormat("float a = 5;\n"
8875                "int   one = 1;\n"
8876                "method();\n"
8877                "const double       oneTwoThree = 123;\n"
8878                "const unsigned int oneTwo = 12;",
8879                Alignment);
8880   verifyFormat("int      oneTwoThree{0}; // comment\n"
8881                "unsigned oneTwo;         // comment",
8882                Alignment);
8883   EXPECT_EQ("float const a = 5;\n"
8884             "\n"
8885             "int oneTwoThree = 123;",
8886             format("float const   a = 5;\n"
8887                    "\n"
8888                    "int           oneTwoThree= 123;",
8889                    Alignment));
8890   EXPECT_EQ("float a = 5;\n"
8891             "int   one = 1;\n"
8892             "\n"
8893             "unsigned oneTwoThree = 123;",
8894             format("float    a = 5;\n"
8895                    "int      one = 1;\n"
8896                    "\n"
8897                    "unsigned oneTwoThree = 123;",
8898                    Alignment));
8899   EXPECT_EQ("float a = 5;\n"
8900             "int   one = 1;\n"
8901             "\n"
8902             "unsigned oneTwoThree = 123;\n"
8903             "int      oneTwo = 12;",
8904             format("float    a = 5;\n"
8905                    "int one = 1;\n"
8906                    "\n"
8907                    "unsigned oneTwoThree = 123;\n"
8908                    "int oneTwo = 12;",
8909                    Alignment));
8910   // Function prototype alignment
8911   verifyFormat("int    a();\n"
8912                "double b();",
8913                Alignment);
8914   verifyFormat("int    a(int x);\n"
8915                "double b();",
8916                Alignment);
8917   unsigned OldColumnLimit = Alignment.ColumnLimit;
8918   // We need to set ColumnLimit to zero, in order to stress nested alignments,
8919   // otherwise the function parameters will be re-flowed onto a single line.
8920   Alignment.ColumnLimit = 0;
8921   EXPECT_EQ("int    a(int   x,\n"
8922             "         float y);\n"
8923             "double b(int    x,\n"
8924             "         double y);",
8925             format("int a(int x,\n"
8926                    " float y);\n"
8927                    "double b(int x,\n"
8928                    " double y);",
8929                    Alignment));
8930   // This ensures that function parameters of function declarations are
8931   // correctly indented when their owning functions are indented.
8932   // The failure case here is for 'double y' to not be indented enough.
8933   EXPECT_EQ("double a(int x);\n"
8934             "int    b(int    y,\n"
8935             "         double z);",
8936             format("double a(int x);\n"
8937                    "int b(int y,\n"
8938                    " double z);",
8939                    Alignment));
8940   // Set ColumnLimit low so that we induce wrapping immediately after
8941   // the function name and opening paren.
8942   Alignment.ColumnLimit = 13;
8943   verifyFormat("int function(\n"
8944                "    int  x,\n"
8945                "    bool y);",
8946                Alignment);
8947   Alignment.ColumnLimit = OldColumnLimit;
8948   // Ensure function pointers don't screw up recursive alignment
8949   verifyFormat("int    a(int x, void (*fp)(int y));\n"
8950                "double b();",
8951                Alignment);
8952   Alignment.AlignConsecutiveAssignments = true;
8953   // Ensure recursive alignment is broken by function braces, so that the
8954   // "a = 1" does not align with subsequent assignments inside the function
8955   // body.
8956   verifyFormat("int func(int a = 1) {\n"
8957                "  int b  = 2;\n"
8958                "  int cc = 3;\n"
8959                "}",
8960                Alignment);
8961   verifyFormat("float      something = 2000;\n"
8962                "double     another   = 911;\n"
8963                "int        i = 1, j = 10;\n"
8964                "const int *oneMore = 1;\n"
8965                "unsigned   i       = 2;",
8966                Alignment);
8967   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8968                "unsigned oneTwo      = 0;   // comment",
8969                Alignment);
8970   // Make sure that scope is correctly tracked, in the absence of braces
8971   verifyFormat("for (int i = 0; i < n; i++)\n"
8972                "  j = i;\n"
8973                "double x = 1;\n",
8974                Alignment);
8975   verifyFormat("if (int i = 0)\n"
8976                "  j = i;\n"
8977                "double x = 1;\n",
8978                Alignment);
8979   // Ensure operator[] and operator() are comprehended
8980   verifyFormat("struct test {\n"
8981                "  long long int foo();\n"
8982                "  int           operator[](int a);\n"
8983                "  double        bar();\n"
8984                "};\n",
8985                Alignment);
8986   verifyFormat("struct test {\n"
8987                "  long long int foo();\n"
8988                "  int           operator()(int a);\n"
8989                "  double        bar();\n"
8990                "};\n",
8991                Alignment);
8992   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8993             "  int const i   = 1;\n"
8994             "  int *     j   = 2;\n"
8995             "  int       big = 10000;\n"
8996             "\n"
8997             "  unsigned oneTwoThree = 123;\n"
8998             "  int      oneTwo      = 12;\n"
8999             "  method();\n"
9000             "  float k  = 2;\n"
9001             "  int   ll = 10000;\n"
9002             "}",
9003             format("void SomeFunction(int parameter= 0) {\n"
9004                    " int const  i= 1;\n"
9005                    "  int *j=2;\n"
9006                    " int big  =  10000;\n"
9007                    "\n"
9008                    "unsigned oneTwoThree  =123;\n"
9009                    "int oneTwo = 12;\n"
9010                    "  method();\n"
9011                    "float k= 2;\n"
9012                    "int ll=10000;\n"
9013                    "}",
9014                    Alignment));
9015   Alignment.AlignConsecutiveAssignments = false;
9016   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9017   verifyFormat("#define A \\\n"
9018                "  int       aaaa = 12; \\\n"
9019                "  float     b = 23; \\\n"
9020                "  const int ccc = 234; \\\n"
9021                "  unsigned  dddddddddd = 2345;",
9022                Alignment);
9023   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9024   verifyFormat("#define A              \\\n"
9025                "  int       aaaa = 12; \\\n"
9026                "  float     b = 23;    \\\n"
9027                "  const int ccc = 234; \\\n"
9028                "  unsigned  dddddddddd = 2345;",
9029                Alignment);
9030   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9031   Alignment.ColumnLimit = 30;
9032   verifyFormat("#define A                    \\\n"
9033                "  int       aaaa = 12;       \\\n"
9034                "  float     b = 23;          \\\n"
9035                "  const int ccc = 234;       \\\n"
9036                "  int       dddddddddd = 2345;",
9037                Alignment);
9038   Alignment.ColumnLimit = 80;
9039   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9040                "k = 4, int l = 5,\n"
9041                "                  int m = 6) {\n"
9042                "  const int j = 10;\n"
9043                "  otherThing = 1;\n"
9044                "}",
9045                Alignment);
9046   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9047                "  int const i = 1;\n"
9048                "  int *     j = 2;\n"
9049                "  int       big = 10000;\n"
9050                "}",
9051                Alignment);
9052   verifyFormat("class C {\n"
9053                "public:\n"
9054                "  int          i = 1;\n"
9055                "  virtual void f() = 0;\n"
9056                "};",
9057                Alignment);
9058   verifyFormat("float i = 1;\n"
9059                "if (SomeType t = getSomething()) {\n"
9060                "}\n"
9061                "const unsigned j = 2;\n"
9062                "int            big = 10000;",
9063                Alignment);
9064   verifyFormat("float j = 7;\n"
9065                "for (int k = 0; k < N; ++k) {\n"
9066                "}\n"
9067                "unsigned j = 2;\n"
9068                "int      big = 10000;\n"
9069                "}",
9070                Alignment);
9071   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9072   verifyFormat("float              i = 1;\n"
9073                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9074                "    = someLooooooooooooooooongFunction();\n"
9075                "int j = 2;",
9076                Alignment);
9077   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9078   verifyFormat("int                i = 1;\n"
9079                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9080                "    someLooooooooooooooooongFunction();\n"
9081                "int j = 2;",
9082                Alignment);
9083 
9084   Alignment.AlignConsecutiveAssignments = true;
9085   verifyFormat("auto lambda = []() {\n"
9086                "  auto  ii = 0;\n"
9087                "  float j  = 0;\n"
9088                "  return 0;\n"
9089                "};\n"
9090                "int   i  = 0;\n"
9091                "float i2 = 0;\n"
9092                "auto  v  = type{\n"
9093                "    i = 1,   //\n"
9094                "    (i = 2), //\n"
9095                "    i = 3    //\n"
9096                "};",
9097                Alignment);
9098   Alignment.AlignConsecutiveAssignments = false;
9099 
9100   verifyFormat(
9101       "int      i = 1;\n"
9102       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9103       "                          loooooooooooooooooooooongParameterB);\n"
9104       "int      j = 2;",
9105       Alignment);
9106 
9107   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9108   // We expect declarations and assignments to align, as long as it doesn't
9109   // exceed the column limit, starting a new alignment sequence whenever it
9110   // happens.
9111   Alignment.AlignConsecutiveAssignments = true;
9112   Alignment.ColumnLimit = 30;
9113   verifyFormat("float    ii              = 1;\n"
9114                "unsigned j               = 2;\n"
9115                "int someVerylongVariable = 1;\n"
9116                "AnotherLongType  ll = 123456;\n"
9117                "VeryVeryLongType k  = 2;\n"
9118                "int              myvar = 1;",
9119                Alignment);
9120   Alignment.ColumnLimit = 80;
9121   Alignment.AlignConsecutiveAssignments = false;
9122 
9123   verifyFormat(
9124       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9125       "          typename LongType, typename B>\n"
9126       "auto foo() {}\n",
9127       Alignment);
9128   verifyFormat("float a, b = 1;\n"
9129                "int   c = 2;\n"
9130                "int   dd = 3;\n",
9131                Alignment);
9132   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9133                "float b[1][] = {{3.f}};\n",
9134                Alignment);
9135   Alignment.AlignConsecutiveAssignments = true;
9136   verifyFormat("float a, b = 1;\n"
9137                "int   c  = 2;\n"
9138                "int   dd = 3;\n",
9139                Alignment);
9140   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9141                "float b[1][] = {{3.f}};\n",
9142                Alignment);
9143   Alignment.AlignConsecutiveAssignments = false;
9144 
9145   Alignment.ColumnLimit = 30;
9146   Alignment.BinPackParameters = false;
9147   verifyFormat("void foo(float     a,\n"
9148                "         float     b,\n"
9149                "         int       c,\n"
9150                "         uint32_t *d) {\n"
9151                "  int *  e = 0;\n"
9152                "  float  f = 0;\n"
9153                "  double g = 0;\n"
9154                "}\n"
9155                "void bar(ino_t     a,\n"
9156                "         int       b,\n"
9157                "         uint32_t *c,\n"
9158                "         bool      d) {}\n",
9159                Alignment);
9160   Alignment.BinPackParameters = true;
9161   Alignment.ColumnLimit = 80;
9162 
9163   // Bug 33507
9164   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9165   verifyFormat(
9166       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9167       "  static const Version verVs2017;\n"
9168       "  return true;\n"
9169       "});\n",
9170       Alignment);
9171   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9172 }
9173 
9174 TEST_F(FormatTest, LinuxBraceBreaking) {
9175   FormatStyle LinuxBraceStyle = getLLVMStyle();
9176   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9177   verifyFormat("namespace a\n"
9178                "{\n"
9179                "class A\n"
9180                "{\n"
9181                "  void f()\n"
9182                "  {\n"
9183                "    if (true) {\n"
9184                "      a();\n"
9185                "      b();\n"
9186                "    } else {\n"
9187                "      a();\n"
9188                "    }\n"
9189                "  }\n"
9190                "  void g() { return; }\n"
9191                "};\n"
9192                "struct B {\n"
9193                "  int x;\n"
9194                "};\n"
9195                "}\n",
9196                LinuxBraceStyle);
9197   verifyFormat("enum X {\n"
9198                "  Y = 0,\n"
9199                "}\n",
9200                LinuxBraceStyle);
9201   verifyFormat("struct S {\n"
9202                "  int Type;\n"
9203                "  union {\n"
9204                "    int x;\n"
9205                "    double y;\n"
9206                "  } Value;\n"
9207                "  class C\n"
9208                "  {\n"
9209                "    MyFavoriteType Value;\n"
9210                "  } Class;\n"
9211                "}\n",
9212                LinuxBraceStyle);
9213 }
9214 
9215 TEST_F(FormatTest, MozillaBraceBreaking) {
9216   FormatStyle MozillaBraceStyle = getLLVMStyle();
9217   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9218   MozillaBraceStyle.FixNamespaceComments = false;
9219   verifyFormat("namespace a {\n"
9220                "class A\n"
9221                "{\n"
9222                "  void f()\n"
9223                "  {\n"
9224                "    if (true) {\n"
9225                "      a();\n"
9226                "      b();\n"
9227                "    }\n"
9228                "  }\n"
9229                "  void g() { return; }\n"
9230                "};\n"
9231                "enum E\n"
9232                "{\n"
9233                "  A,\n"
9234                "  // foo\n"
9235                "  B,\n"
9236                "  C\n"
9237                "};\n"
9238                "struct B\n"
9239                "{\n"
9240                "  int x;\n"
9241                "};\n"
9242                "}\n",
9243                MozillaBraceStyle);
9244   verifyFormat("struct S\n"
9245                "{\n"
9246                "  int Type;\n"
9247                "  union\n"
9248                "  {\n"
9249                "    int x;\n"
9250                "    double y;\n"
9251                "  } Value;\n"
9252                "  class C\n"
9253                "  {\n"
9254                "    MyFavoriteType Value;\n"
9255                "  } Class;\n"
9256                "}\n",
9257                MozillaBraceStyle);
9258 }
9259 
9260 TEST_F(FormatTest, StroustrupBraceBreaking) {
9261   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9262   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9263   verifyFormat("namespace a {\n"
9264                "class A {\n"
9265                "  void f()\n"
9266                "  {\n"
9267                "    if (true) {\n"
9268                "      a();\n"
9269                "      b();\n"
9270                "    }\n"
9271                "  }\n"
9272                "  void g() { return; }\n"
9273                "};\n"
9274                "struct B {\n"
9275                "  int x;\n"
9276                "};\n"
9277                "} // namespace a\n",
9278                StroustrupBraceStyle);
9279 
9280   verifyFormat("void foo()\n"
9281                "{\n"
9282                "  if (a) {\n"
9283                "    a();\n"
9284                "  }\n"
9285                "  else {\n"
9286                "    b();\n"
9287                "  }\n"
9288                "}\n",
9289                StroustrupBraceStyle);
9290 
9291   verifyFormat("#ifdef _DEBUG\n"
9292                "int foo(int i = 0)\n"
9293                "#else\n"
9294                "int foo(int i = 5)\n"
9295                "#endif\n"
9296                "{\n"
9297                "  return i;\n"
9298                "}",
9299                StroustrupBraceStyle);
9300 
9301   verifyFormat("void foo() {}\n"
9302                "void bar()\n"
9303                "#ifdef _DEBUG\n"
9304                "{\n"
9305                "  foo();\n"
9306                "}\n"
9307                "#else\n"
9308                "{\n"
9309                "}\n"
9310                "#endif",
9311                StroustrupBraceStyle);
9312 
9313   verifyFormat("void foobar() { int i = 5; }\n"
9314                "#ifdef _DEBUG\n"
9315                "void bar() {}\n"
9316                "#else\n"
9317                "void bar() { foobar(); }\n"
9318                "#endif",
9319                StroustrupBraceStyle);
9320 }
9321 
9322 TEST_F(FormatTest, AllmanBraceBreaking) {
9323   FormatStyle AllmanBraceStyle = getLLVMStyle();
9324   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9325   verifyFormat("namespace a\n"
9326                "{\n"
9327                "class A\n"
9328                "{\n"
9329                "  void f()\n"
9330                "  {\n"
9331                "    if (true)\n"
9332                "    {\n"
9333                "      a();\n"
9334                "      b();\n"
9335                "    }\n"
9336                "  }\n"
9337                "  void g() { return; }\n"
9338                "};\n"
9339                "struct B\n"
9340                "{\n"
9341                "  int x;\n"
9342                "};\n"
9343                "}",
9344                AllmanBraceStyle);
9345 
9346   verifyFormat("void f()\n"
9347                "{\n"
9348                "  if (true)\n"
9349                "  {\n"
9350                "    a();\n"
9351                "  }\n"
9352                "  else if (false)\n"
9353                "  {\n"
9354                "    b();\n"
9355                "  }\n"
9356                "  else\n"
9357                "  {\n"
9358                "    c();\n"
9359                "  }\n"
9360                "}\n",
9361                AllmanBraceStyle);
9362 
9363   verifyFormat("void f()\n"
9364                "{\n"
9365                "  for (int i = 0; i < 10; ++i)\n"
9366                "  {\n"
9367                "    a();\n"
9368                "  }\n"
9369                "  while (false)\n"
9370                "  {\n"
9371                "    b();\n"
9372                "  }\n"
9373                "  do\n"
9374                "  {\n"
9375                "    c();\n"
9376                "  } while (false)\n"
9377                "}\n",
9378                AllmanBraceStyle);
9379 
9380   verifyFormat("void f(int a)\n"
9381                "{\n"
9382                "  switch (a)\n"
9383                "  {\n"
9384                "  case 0:\n"
9385                "    break;\n"
9386                "  case 1:\n"
9387                "  {\n"
9388                "    break;\n"
9389                "  }\n"
9390                "  case 2:\n"
9391                "  {\n"
9392                "  }\n"
9393                "  break;\n"
9394                "  default:\n"
9395                "    break;\n"
9396                "  }\n"
9397                "}\n",
9398                AllmanBraceStyle);
9399 
9400   verifyFormat("enum X\n"
9401                "{\n"
9402                "  Y = 0,\n"
9403                "}\n",
9404                AllmanBraceStyle);
9405   verifyFormat("enum X\n"
9406                "{\n"
9407                "  Y = 0\n"
9408                "}\n",
9409                AllmanBraceStyle);
9410 
9411   verifyFormat("@interface BSApplicationController ()\n"
9412                "{\n"
9413                "@private\n"
9414                "  id _extraIvar;\n"
9415                "}\n"
9416                "@end\n",
9417                AllmanBraceStyle);
9418 
9419   verifyFormat("#ifdef _DEBUG\n"
9420                "int foo(int i = 0)\n"
9421                "#else\n"
9422                "int foo(int i = 5)\n"
9423                "#endif\n"
9424                "{\n"
9425                "  return i;\n"
9426                "}",
9427                AllmanBraceStyle);
9428 
9429   verifyFormat("void foo() {}\n"
9430                "void bar()\n"
9431                "#ifdef _DEBUG\n"
9432                "{\n"
9433                "  foo();\n"
9434                "}\n"
9435                "#else\n"
9436                "{\n"
9437                "}\n"
9438                "#endif",
9439                AllmanBraceStyle);
9440 
9441   verifyFormat("void foobar() { int i = 5; }\n"
9442                "#ifdef _DEBUG\n"
9443                "void bar() {}\n"
9444                "#else\n"
9445                "void bar() { foobar(); }\n"
9446                "#endif",
9447                AllmanBraceStyle);
9448 
9449   // This shouldn't affect ObjC blocks..
9450   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9451                "  // ...\n"
9452                "  int i;\n"
9453                "}];",
9454                AllmanBraceStyle);
9455   verifyFormat("void (^block)(void) = ^{\n"
9456                "  // ...\n"
9457                "  int i;\n"
9458                "};",
9459                AllmanBraceStyle);
9460   // .. or dict literals.
9461   verifyFormat("void f()\n"
9462                "{\n"
9463                "  // ...\n"
9464                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9465                "}",
9466                AllmanBraceStyle);
9467   verifyFormat("void f()\n"
9468                "{\n"
9469                "  // ...\n"
9470                "  [object someMethod:@{a : @\"b\"}];\n"
9471                "}",
9472                AllmanBraceStyle);
9473   verifyFormat("int f()\n"
9474                "{ // comment\n"
9475                "  return 42;\n"
9476                "}",
9477                AllmanBraceStyle);
9478 
9479   AllmanBraceStyle.ColumnLimit = 19;
9480   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9481   AllmanBraceStyle.ColumnLimit = 18;
9482   verifyFormat("void f()\n"
9483                "{\n"
9484                "  int i;\n"
9485                "}",
9486                AllmanBraceStyle);
9487   AllmanBraceStyle.ColumnLimit = 80;
9488 
9489   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9490   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9491   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9492   verifyFormat("void f(bool b)\n"
9493                "{\n"
9494                "  if (b)\n"
9495                "  {\n"
9496                "    return;\n"
9497                "  }\n"
9498                "}\n",
9499                BreakBeforeBraceShortIfs);
9500   verifyFormat("void f(bool b)\n"
9501                "{\n"
9502                "  if constexpr (b)\n"
9503                "  {\n"
9504                "    return;\n"
9505                "  }\n"
9506                "}\n",
9507                BreakBeforeBraceShortIfs);
9508   verifyFormat("void f(bool b)\n"
9509                "{\n"
9510                "  if (b) return;\n"
9511                "}\n",
9512                BreakBeforeBraceShortIfs);
9513   verifyFormat("void f(bool b)\n"
9514                "{\n"
9515                "  if constexpr (b) return;\n"
9516                "}\n",
9517                BreakBeforeBraceShortIfs);
9518   verifyFormat("void f(bool b)\n"
9519                "{\n"
9520                "  while (b)\n"
9521                "  {\n"
9522                "    return;\n"
9523                "  }\n"
9524                "}\n",
9525                BreakBeforeBraceShortIfs);
9526 }
9527 
9528 TEST_F(FormatTest, GNUBraceBreaking) {
9529   FormatStyle GNUBraceStyle = getLLVMStyle();
9530   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9531   verifyFormat("namespace a\n"
9532                "{\n"
9533                "class A\n"
9534                "{\n"
9535                "  void f()\n"
9536                "  {\n"
9537                "    int a;\n"
9538                "    {\n"
9539                "      int b;\n"
9540                "    }\n"
9541                "    if (true)\n"
9542                "      {\n"
9543                "        a();\n"
9544                "        b();\n"
9545                "      }\n"
9546                "  }\n"
9547                "  void g() { return; }\n"
9548                "}\n"
9549                "}",
9550                GNUBraceStyle);
9551 
9552   verifyFormat("void f()\n"
9553                "{\n"
9554                "  if (true)\n"
9555                "    {\n"
9556                "      a();\n"
9557                "    }\n"
9558                "  else if (false)\n"
9559                "    {\n"
9560                "      b();\n"
9561                "    }\n"
9562                "  else\n"
9563                "    {\n"
9564                "      c();\n"
9565                "    }\n"
9566                "}\n",
9567                GNUBraceStyle);
9568 
9569   verifyFormat("void f()\n"
9570                "{\n"
9571                "  for (int i = 0; i < 10; ++i)\n"
9572                "    {\n"
9573                "      a();\n"
9574                "    }\n"
9575                "  while (false)\n"
9576                "    {\n"
9577                "      b();\n"
9578                "    }\n"
9579                "  do\n"
9580                "    {\n"
9581                "      c();\n"
9582                "    }\n"
9583                "  while (false);\n"
9584                "}\n",
9585                GNUBraceStyle);
9586 
9587   verifyFormat("void f(int a)\n"
9588                "{\n"
9589                "  switch (a)\n"
9590                "    {\n"
9591                "    case 0:\n"
9592                "      break;\n"
9593                "    case 1:\n"
9594                "      {\n"
9595                "        break;\n"
9596                "      }\n"
9597                "    case 2:\n"
9598                "      {\n"
9599                "      }\n"
9600                "      break;\n"
9601                "    default:\n"
9602                "      break;\n"
9603                "    }\n"
9604                "}\n",
9605                GNUBraceStyle);
9606 
9607   verifyFormat("enum X\n"
9608                "{\n"
9609                "  Y = 0,\n"
9610                "}\n",
9611                GNUBraceStyle);
9612 
9613   verifyFormat("@interface BSApplicationController ()\n"
9614                "{\n"
9615                "@private\n"
9616                "  id _extraIvar;\n"
9617                "}\n"
9618                "@end\n",
9619                GNUBraceStyle);
9620 
9621   verifyFormat("#ifdef _DEBUG\n"
9622                "int foo(int i = 0)\n"
9623                "#else\n"
9624                "int foo(int i = 5)\n"
9625                "#endif\n"
9626                "{\n"
9627                "  return i;\n"
9628                "}",
9629                GNUBraceStyle);
9630 
9631   verifyFormat("void foo() {}\n"
9632                "void bar()\n"
9633                "#ifdef _DEBUG\n"
9634                "{\n"
9635                "  foo();\n"
9636                "}\n"
9637                "#else\n"
9638                "{\n"
9639                "}\n"
9640                "#endif",
9641                GNUBraceStyle);
9642 
9643   verifyFormat("void foobar() { int i = 5; }\n"
9644                "#ifdef _DEBUG\n"
9645                "void bar() {}\n"
9646                "#else\n"
9647                "void bar() { foobar(); }\n"
9648                "#endif",
9649                GNUBraceStyle);
9650 }
9651 
9652 TEST_F(FormatTest, WebKitBraceBreaking) {
9653   FormatStyle WebKitBraceStyle = getLLVMStyle();
9654   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9655   WebKitBraceStyle.FixNamespaceComments = false;
9656   verifyFormat("namespace a {\n"
9657                "class A {\n"
9658                "  void f()\n"
9659                "  {\n"
9660                "    if (true) {\n"
9661                "      a();\n"
9662                "      b();\n"
9663                "    }\n"
9664                "  }\n"
9665                "  void g() { return; }\n"
9666                "};\n"
9667                "enum E {\n"
9668                "  A,\n"
9669                "  // foo\n"
9670                "  B,\n"
9671                "  C\n"
9672                "};\n"
9673                "struct B {\n"
9674                "  int x;\n"
9675                "};\n"
9676                "}\n",
9677                WebKitBraceStyle);
9678   verifyFormat("struct S {\n"
9679                "  int Type;\n"
9680                "  union {\n"
9681                "    int x;\n"
9682                "    double y;\n"
9683                "  } Value;\n"
9684                "  class C {\n"
9685                "    MyFavoriteType Value;\n"
9686                "  } Class;\n"
9687                "};\n",
9688                WebKitBraceStyle);
9689 }
9690 
9691 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9692   verifyFormat("void f() {\n"
9693                "  try {\n"
9694                "  } catch (const Exception &e) {\n"
9695                "  }\n"
9696                "}\n",
9697                getLLVMStyle());
9698 }
9699 
9700 TEST_F(FormatTest, UnderstandsPragmas) {
9701   verifyFormat("#pragma omp reduction(| : var)");
9702   verifyFormat("#pragma omp reduction(+ : var)");
9703 
9704   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9705             "(including parentheses).",
9706             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9707                    "(including parentheses)."));
9708 }
9709 
9710 TEST_F(FormatTest, UnderstandPragmaOption) {
9711   verifyFormat("#pragma option -C -A");
9712 
9713   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9714 }
9715 
9716 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9717   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9718   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9719                                   << " differs from Style #0"
9720 
9721 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9722   SmallVector<FormatStyle, 3> Styles;
9723   Styles.resize(3);
9724 
9725   Styles[0] = getLLVMStyle();
9726   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9727   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9728   EXPECT_ALL_STYLES_EQUAL(Styles);
9729 
9730   Styles[0] = getGoogleStyle();
9731   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9732   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9733   EXPECT_ALL_STYLES_EQUAL(Styles);
9734 
9735   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9736   EXPECT_TRUE(
9737       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9738   EXPECT_TRUE(
9739       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9740   EXPECT_ALL_STYLES_EQUAL(Styles);
9741 
9742   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9743   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9744   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9745   EXPECT_ALL_STYLES_EQUAL(Styles);
9746 
9747   Styles[0] = getMozillaStyle();
9748   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9749   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9750   EXPECT_ALL_STYLES_EQUAL(Styles);
9751 
9752   Styles[0] = getWebKitStyle();
9753   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9754   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9755   EXPECT_ALL_STYLES_EQUAL(Styles);
9756 
9757   Styles[0] = getGNUStyle();
9758   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9759   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9760   EXPECT_ALL_STYLES_EQUAL(Styles);
9761 
9762   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9763 }
9764 
9765 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9766   SmallVector<FormatStyle, 8> Styles;
9767   Styles.resize(2);
9768 
9769   Styles[0] = getGoogleStyle();
9770   Styles[1] = getLLVMStyle();
9771   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9772   EXPECT_ALL_STYLES_EQUAL(Styles);
9773 
9774   Styles.resize(5);
9775   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9776   Styles[1] = getLLVMStyle();
9777   Styles[1].Language = FormatStyle::LK_JavaScript;
9778   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9779 
9780   Styles[2] = getLLVMStyle();
9781   Styles[2].Language = FormatStyle::LK_JavaScript;
9782   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9783                                   "BasedOnStyle: Google",
9784                                   &Styles[2])
9785                    .value());
9786 
9787   Styles[3] = getLLVMStyle();
9788   Styles[3].Language = FormatStyle::LK_JavaScript;
9789   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9790                                   "Language: JavaScript",
9791                                   &Styles[3])
9792                    .value());
9793 
9794   Styles[4] = getLLVMStyle();
9795   Styles[4].Language = FormatStyle::LK_JavaScript;
9796   EXPECT_EQ(0, parseConfiguration("---\n"
9797                                   "BasedOnStyle: LLVM\n"
9798                                   "IndentWidth: 123\n"
9799                                   "---\n"
9800                                   "BasedOnStyle: Google\n"
9801                                   "Language: JavaScript",
9802                                   &Styles[4])
9803                    .value());
9804   EXPECT_ALL_STYLES_EQUAL(Styles);
9805 }
9806 
9807 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9808   Style.FIELD = false;                                                         \
9809   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9810   EXPECT_TRUE(Style.FIELD);                                                    \
9811   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9812   EXPECT_FALSE(Style.FIELD);
9813 
9814 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9815 
9816 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9817   Style.STRUCT.FIELD = false;                                                  \
9818   EXPECT_EQ(0,                                                                 \
9819             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9820                 .value());                                                     \
9821   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9822   EXPECT_EQ(0,                                                                 \
9823             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9824                 .value());                                                     \
9825   EXPECT_FALSE(Style.STRUCT.FIELD);
9826 
9827 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9828   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9829 
9830 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9831   EXPECT_NE(VALUE, Style.FIELD);                                               \
9832   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9833   EXPECT_EQ(VALUE, Style.FIELD)
9834 
9835 TEST_F(FormatTest, ParsesConfigurationBools) {
9836   FormatStyle Style = {};
9837   Style.Language = FormatStyle::LK_Cpp;
9838   CHECK_PARSE_BOOL(AlignOperands);
9839   CHECK_PARSE_BOOL(AlignTrailingComments);
9840   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9841   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9842   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9843   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9844   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9845   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9846   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9847   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9848   CHECK_PARSE_BOOL(BinPackArguments);
9849   CHECK_PARSE_BOOL(BinPackParameters);
9850   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
9851   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9852   CHECK_PARSE_BOOL(BreakStringLiterals);
9853   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
9854   CHECK_PARSE_BOOL(CompactNamespaces);
9855   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9856   CHECK_PARSE_BOOL(DerivePointerAlignment);
9857   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9858   CHECK_PARSE_BOOL(DisableFormat);
9859   CHECK_PARSE_BOOL(IndentCaseLabels);
9860   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9861   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9862   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9863   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9864   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9865   CHECK_PARSE_BOOL(ReflowComments);
9866   CHECK_PARSE_BOOL(SortIncludes);
9867   CHECK_PARSE_BOOL(SortUsingDeclarations);
9868   CHECK_PARSE_BOOL(SpacesInParentheses);
9869   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9870   CHECK_PARSE_BOOL(SpacesInAngles);
9871   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9872   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9873   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9874   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9875   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
9876   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9877 
9878   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9879   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9880   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9881   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9882   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9883   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9884   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9885   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9886   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9887   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9888   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9889   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
9890   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
9891   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
9892 }
9893 
9894 #undef CHECK_PARSE_BOOL
9895 
9896 TEST_F(FormatTest, ParsesConfiguration) {
9897   FormatStyle Style = {};
9898   Style.Language = FormatStyle::LK_Cpp;
9899   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9900   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9901               ConstructorInitializerIndentWidth, 1234u);
9902   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9903   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9904   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9905   CHECK_PARSE("PenaltyBreakAssignment: 1234",
9906               PenaltyBreakAssignment, 1234u);
9907   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9908               PenaltyBreakBeforeFirstCallParameter, 1234u);
9909   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9910   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9911               PenaltyReturnTypeOnItsOwnLine, 1234u);
9912   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9913               SpacesBeforeTrailingComments, 1234u);
9914   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9915   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9916   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
9917 
9918   Style.PointerAlignment = FormatStyle::PAS_Middle;
9919   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9920               FormatStyle::PAS_Left);
9921   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9922               FormatStyle::PAS_Right);
9923   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9924               FormatStyle::PAS_Middle);
9925   // For backward compatibility:
9926   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9927               FormatStyle::PAS_Left);
9928   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9929               FormatStyle::PAS_Right);
9930   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9931               FormatStyle::PAS_Middle);
9932 
9933   Style.Standard = FormatStyle::LS_Auto;
9934   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9935   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9936   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9937   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9938   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9939 
9940   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9941   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9942               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9943   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9944               FormatStyle::BOS_None);
9945   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9946               FormatStyle::BOS_All);
9947   // For backward compatibility:
9948   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9949               FormatStyle::BOS_None);
9950   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9951               FormatStyle::BOS_All);
9952 
9953   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
9954   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
9955               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9956   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
9957               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
9958   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
9959               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
9960   // For backward compatibility:
9961   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
9962               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9963 
9964   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9965   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9966               FormatStyle::BAS_Align);
9967   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9968               FormatStyle::BAS_DontAlign);
9969   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9970               FormatStyle::BAS_AlwaysBreak);
9971   // For backward compatibility:
9972   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9973               FormatStyle::BAS_DontAlign);
9974   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9975               FormatStyle::BAS_Align);
9976 
9977   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9978   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
9979               FormatStyle::ENAS_DontAlign);
9980   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
9981               FormatStyle::ENAS_Left);
9982   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
9983               FormatStyle::ENAS_Right);
9984   // For backward compatibility:
9985   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
9986               FormatStyle::ENAS_Left);
9987   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
9988               FormatStyle::ENAS_Right);
9989 
9990   Style.UseTab = FormatStyle::UT_ForIndentation;
9991   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9992   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9993   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9994   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9995               FormatStyle::UT_ForContinuationAndIndentation);
9996   // For backward compatibility:
9997   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9998   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9999 
10000   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10001   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10002               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10003   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10004               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10005   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10006               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10007   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10008               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10009   // For backward compatibility:
10010   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10011               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10012   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10013               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10014 
10015   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10016   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10017               FormatStyle::SBPO_Never);
10018   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10019               FormatStyle::SBPO_Always);
10020   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10021               FormatStyle::SBPO_ControlStatements);
10022   // For backward compatibility:
10023   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10024               FormatStyle::SBPO_Never);
10025   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10026               FormatStyle::SBPO_ControlStatements);
10027 
10028   Style.ColumnLimit = 123;
10029   FormatStyle BaseStyle = getLLVMStyle();
10030   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10031   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10032 
10033   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10034   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10035               FormatStyle::BS_Attach);
10036   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10037               FormatStyle::BS_Linux);
10038   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10039               FormatStyle::BS_Mozilla);
10040   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10041               FormatStyle::BS_Stroustrup);
10042   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10043               FormatStyle::BS_Allman);
10044   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10045   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10046               FormatStyle::BS_WebKit);
10047   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10048               FormatStyle::BS_Custom);
10049 
10050   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10051   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10052               FormatStyle::RTBS_None);
10053   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10054               FormatStyle::RTBS_All);
10055   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10056               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10057   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10058               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10059   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10060               AlwaysBreakAfterReturnType,
10061               FormatStyle::RTBS_TopLevelDefinitions);
10062 
10063   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10064   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10065               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10066   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10067               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10068   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10069               AlwaysBreakAfterDefinitionReturnType,
10070               FormatStyle::DRTBS_TopLevel);
10071 
10072   Style.NamespaceIndentation = FormatStyle::NI_All;
10073   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10074               FormatStyle::NI_None);
10075   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10076               FormatStyle::NI_Inner);
10077   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10078               FormatStyle::NI_All);
10079 
10080   // FIXME: This is required because parsing a configuration simply overwrites
10081   // the first N elements of the list instead of resetting it.
10082   Style.ForEachMacros.clear();
10083   std::vector<std::string> BoostForeach;
10084   BoostForeach.push_back("BOOST_FOREACH");
10085   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10086   std::vector<std::string> BoostAndQForeach;
10087   BoostAndQForeach.push_back("BOOST_FOREACH");
10088   BoostAndQForeach.push_back("Q_FOREACH");
10089   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10090               BoostAndQForeach);
10091 
10092   Style.IncludeCategories.clear();
10093   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10094                                                                   {".*", 1}};
10095   CHECK_PARSE("IncludeCategories:\n"
10096               "  - Regex: abc/.*\n"
10097               "    Priority: 2\n"
10098               "  - Regex: .*\n"
10099               "    Priority: 1",
10100               IncludeCategories, ExpectedCategories);
10101   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10102 }
10103 
10104 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10105   FormatStyle Style = {};
10106   Style.Language = FormatStyle::LK_Cpp;
10107   CHECK_PARSE("Language: Cpp\n"
10108               "IndentWidth: 12",
10109               IndentWidth, 12u);
10110   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10111                                "IndentWidth: 34",
10112                                &Style),
10113             ParseError::Unsuitable);
10114   EXPECT_EQ(12u, Style.IndentWidth);
10115   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10116   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10117 
10118   Style.Language = FormatStyle::LK_JavaScript;
10119   CHECK_PARSE("Language: JavaScript\n"
10120               "IndentWidth: 12",
10121               IndentWidth, 12u);
10122   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10123   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10124                                "IndentWidth: 34",
10125                                &Style),
10126             ParseError::Unsuitable);
10127   EXPECT_EQ(23u, Style.IndentWidth);
10128   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10129   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10130 
10131   CHECK_PARSE("BasedOnStyle: LLVM\n"
10132               "IndentWidth: 67",
10133               IndentWidth, 67u);
10134 
10135   CHECK_PARSE("---\n"
10136               "Language: JavaScript\n"
10137               "IndentWidth: 12\n"
10138               "---\n"
10139               "Language: Cpp\n"
10140               "IndentWidth: 34\n"
10141               "...\n",
10142               IndentWidth, 12u);
10143 
10144   Style.Language = FormatStyle::LK_Cpp;
10145   CHECK_PARSE("---\n"
10146               "Language: JavaScript\n"
10147               "IndentWidth: 12\n"
10148               "---\n"
10149               "Language: Cpp\n"
10150               "IndentWidth: 34\n"
10151               "...\n",
10152               IndentWidth, 34u);
10153   CHECK_PARSE("---\n"
10154               "IndentWidth: 78\n"
10155               "---\n"
10156               "Language: JavaScript\n"
10157               "IndentWidth: 56\n"
10158               "...\n",
10159               IndentWidth, 78u);
10160 
10161   Style.ColumnLimit = 123;
10162   Style.IndentWidth = 234;
10163   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10164   Style.TabWidth = 345;
10165   EXPECT_FALSE(parseConfiguration("---\n"
10166                                   "IndentWidth: 456\n"
10167                                   "BreakBeforeBraces: Allman\n"
10168                                   "---\n"
10169                                   "Language: JavaScript\n"
10170                                   "IndentWidth: 111\n"
10171                                   "TabWidth: 111\n"
10172                                   "---\n"
10173                                   "Language: Cpp\n"
10174                                   "BreakBeforeBraces: Stroustrup\n"
10175                                   "TabWidth: 789\n"
10176                                   "...\n",
10177                                   &Style));
10178   EXPECT_EQ(123u, Style.ColumnLimit);
10179   EXPECT_EQ(456u, Style.IndentWidth);
10180   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10181   EXPECT_EQ(789u, Style.TabWidth);
10182 
10183   EXPECT_EQ(parseConfiguration("---\n"
10184                                "Language: JavaScript\n"
10185                                "IndentWidth: 56\n"
10186                                "---\n"
10187                                "IndentWidth: 78\n"
10188                                "...\n",
10189                                &Style),
10190             ParseError::Error);
10191   EXPECT_EQ(parseConfiguration("---\n"
10192                                "Language: JavaScript\n"
10193                                "IndentWidth: 56\n"
10194                                "---\n"
10195                                "Language: JavaScript\n"
10196                                "IndentWidth: 78\n"
10197                                "...\n",
10198                                &Style),
10199             ParseError::Error);
10200 
10201   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10202 }
10203 
10204 #undef CHECK_PARSE
10205 
10206 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10207   FormatStyle Style = {};
10208   Style.Language = FormatStyle::LK_JavaScript;
10209   Style.BreakBeforeTernaryOperators = true;
10210   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10211   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10212 
10213   Style.BreakBeforeTernaryOperators = true;
10214   EXPECT_EQ(0, parseConfiguration("---\n"
10215                                   "BasedOnStyle: Google\n"
10216                                   "---\n"
10217                                   "Language: JavaScript\n"
10218                                   "IndentWidth: 76\n"
10219                                   "...\n",
10220                                   &Style)
10221                    .value());
10222   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10223   EXPECT_EQ(76u, Style.IndentWidth);
10224   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10225 }
10226 
10227 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10228   FormatStyle Style = getLLVMStyle();
10229   std::string YAML = configurationAsText(Style);
10230   FormatStyle ParsedStyle = {};
10231   ParsedStyle.Language = FormatStyle::LK_Cpp;
10232   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10233   EXPECT_EQ(Style, ParsedStyle);
10234 }
10235 
10236 TEST_F(FormatTest, WorksFor8bitEncodings) {
10237   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10238             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10239             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10240             "\"\xef\xee\xf0\xf3...\"",
10241             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10242                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10243                    "\xef\xee\xf0\xf3...\"",
10244                    getLLVMStyleWithColumns(12)));
10245 }
10246 
10247 TEST_F(FormatTest, HandlesUTF8BOM) {
10248   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10249   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10250             format("\xef\xbb\xbf#include <iostream>"));
10251   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10252             format("\xef\xbb\xbf\n#include <iostream>"));
10253 }
10254 
10255 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10256 #if !defined(_MSC_VER)
10257 
10258 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10259   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10260                getLLVMStyleWithColumns(35));
10261   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10262                getLLVMStyleWithColumns(31));
10263   verifyFormat("// Однажды в студёную зимнюю пору...",
10264                getLLVMStyleWithColumns(36));
10265   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10266   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10267                getLLVMStyleWithColumns(39));
10268   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10269                getLLVMStyleWithColumns(35));
10270 }
10271 
10272 TEST_F(FormatTest, SplitsUTF8Strings) {
10273   // Non-printable characters' width is currently considered to be the length in
10274   // bytes in UTF8. The characters can be displayed in very different manner
10275   // (zero-width, single width with a substitution glyph, expanded to their code
10276   // (e.g. "<8d>"), so there's no single correct way to handle them.
10277   EXPECT_EQ("\"aaaaÄ\"\n"
10278             "\"\xc2\x8d\";",
10279             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10280   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10281             "\"\xc2\x8d\";",
10282             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10283   EXPECT_EQ("\"Однажды, в \"\n"
10284             "\"студёную \"\n"
10285             "\"зимнюю \"\n"
10286             "\"пору,\"",
10287             format("\"Однажды, в студёную зимнюю пору,\"",
10288                    getLLVMStyleWithColumns(13)));
10289   EXPECT_EQ(
10290       "\"一 二 三 \"\n"
10291       "\"四 五六 \"\n"
10292       "\"七 八 九 \"\n"
10293       "\"十\"",
10294       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10295   EXPECT_EQ("\"一\t二 \"\n"
10296             "\"\t三 \"\n"
10297             "\"四 五\t六 \"\n"
10298             "\"\t七 \"\n"
10299             "\"八九十\tqq\"",
10300             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10301                    getLLVMStyleWithColumns(11)));
10302 
10303   // UTF8 character in an escape sequence.
10304   EXPECT_EQ("\"aaaaaa\"\n"
10305             "\"\\\xC2\x8D\"",
10306             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10307 }
10308 
10309 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10310   EXPECT_EQ("const char *sssss =\n"
10311             "    \"一二三四五六七八\\\n"
10312             " 九 十\";",
10313             format("const char *sssss = \"一二三四五六七八\\\n"
10314                    " 九 十\";",
10315                    getLLVMStyleWithColumns(30)));
10316 }
10317 
10318 TEST_F(FormatTest, SplitsUTF8LineComments) {
10319   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10320             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10321   EXPECT_EQ("// Я из лесу\n"
10322             "// вышел; был\n"
10323             "// сильный\n"
10324             "// мороз.",
10325             format("// Я из лесу вышел; был сильный мороз.",
10326                    getLLVMStyleWithColumns(13)));
10327   EXPECT_EQ("// 一二三\n"
10328             "// 四五六七\n"
10329             "// 八  九\n"
10330             "// 十",
10331             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10332 }
10333 
10334 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10335   EXPECT_EQ("/* Гляжу,\n"
10336             " * поднимается\n"
10337             " * медленно в\n"
10338             " * гору\n"
10339             " * Лошадка,\n"
10340             " * везущая\n"
10341             " * хворосту\n"
10342             " * воз. */",
10343             format("/* Гляжу, поднимается медленно в гору\n"
10344                    " * Лошадка, везущая хворосту воз. */",
10345                    getLLVMStyleWithColumns(13)));
10346   EXPECT_EQ(
10347       "/* 一二三\n"
10348       " * 四五六七\n"
10349       " * 八  九\n"
10350       " * 十  */",
10351       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10352   EXPECT_EQ("/* �������� ��������\n"
10353             " * ��������\n"
10354             " * ������-�� */",
10355             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10356 }
10357 
10358 #endif // _MSC_VER
10359 
10360 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10361   FormatStyle Style = getLLVMStyle();
10362 
10363   Style.ConstructorInitializerIndentWidth = 4;
10364   verifyFormat(
10365       "SomeClass::Constructor()\n"
10366       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10367       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10368       Style);
10369 
10370   Style.ConstructorInitializerIndentWidth = 2;
10371   verifyFormat(
10372       "SomeClass::Constructor()\n"
10373       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10374       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10375       Style);
10376 
10377   Style.ConstructorInitializerIndentWidth = 0;
10378   verifyFormat(
10379       "SomeClass::Constructor()\n"
10380       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10381       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10382       Style);
10383   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10384   verifyFormat(
10385       "SomeLongTemplateVariableName<\n"
10386       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10387       Style);
10388   verifyFormat(
10389       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10390       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10391       Style);
10392 }
10393 
10394 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10395   FormatStyle Style = getLLVMStyle();
10396   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10397   Style.ConstructorInitializerIndentWidth = 4;
10398   verifyFormat("SomeClass::Constructor()\n"
10399                "    : a(a)\n"
10400                "    , b(b)\n"
10401                "    , c(c) {}",
10402                Style);
10403   verifyFormat("SomeClass::Constructor()\n"
10404                "    : a(a) {}",
10405                Style);
10406 
10407   Style.ColumnLimit = 0;
10408   verifyFormat("SomeClass::Constructor()\n"
10409                "    : a(a) {}",
10410                Style);
10411   verifyFormat("SomeClass::Constructor() noexcept\n"
10412                "    : a(a) {}",
10413                Style);
10414   verifyFormat("SomeClass::Constructor()\n"
10415                "    : a(a)\n"
10416                "    , b(b)\n"
10417                "    , c(c) {}",
10418                Style);
10419   verifyFormat("SomeClass::Constructor()\n"
10420                "    : a(a) {\n"
10421                "  foo();\n"
10422                "  bar();\n"
10423                "}",
10424                Style);
10425 
10426   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10427   verifyFormat("SomeClass::Constructor()\n"
10428                "    : a(a)\n"
10429                "    , b(b)\n"
10430                "    , c(c) {\n}",
10431                Style);
10432   verifyFormat("SomeClass::Constructor()\n"
10433                "    : a(a) {\n}",
10434                Style);
10435 
10436   Style.ColumnLimit = 80;
10437   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10438   Style.ConstructorInitializerIndentWidth = 2;
10439   verifyFormat("SomeClass::Constructor()\n"
10440                "  : a(a)\n"
10441                "  , b(b)\n"
10442                "  , c(c) {}",
10443                Style);
10444 
10445   Style.ConstructorInitializerIndentWidth = 0;
10446   verifyFormat("SomeClass::Constructor()\n"
10447                ": a(a)\n"
10448                ", b(b)\n"
10449                ", c(c) {}",
10450                Style);
10451 
10452   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10453   Style.ConstructorInitializerIndentWidth = 4;
10454   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10455   verifyFormat(
10456       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10457       Style);
10458   verifyFormat(
10459       "SomeClass::Constructor()\n"
10460       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10461       Style);
10462   Style.ConstructorInitializerIndentWidth = 4;
10463   Style.ColumnLimit = 60;
10464   verifyFormat("SomeClass::Constructor()\n"
10465                "    : aaaaaaaa(aaaaaaaa)\n"
10466                "    , aaaaaaaa(aaaaaaaa)\n"
10467                "    , aaaaaaaa(aaaaaaaa) {}",
10468                Style);
10469 }
10470 
10471 TEST_F(FormatTest, Destructors) {
10472   verifyFormat("void F(int &i) { i.~int(); }");
10473   verifyFormat("void F(int &i) { i->~int(); }");
10474 }
10475 
10476 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10477   FormatStyle Style = getWebKitStyle();
10478 
10479   // Don't indent in outer namespaces.
10480   verifyFormat("namespace outer {\n"
10481                "int i;\n"
10482                "namespace inner {\n"
10483                "    int i;\n"
10484                "} // namespace inner\n"
10485                "} // namespace outer\n"
10486                "namespace other_outer {\n"
10487                "int i;\n"
10488                "}",
10489                Style);
10490 
10491   // Don't indent case labels.
10492   verifyFormat("switch (variable) {\n"
10493                "case 1:\n"
10494                "case 2:\n"
10495                "    doSomething();\n"
10496                "    break;\n"
10497                "default:\n"
10498                "    ++variable;\n"
10499                "}",
10500                Style);
10501 
10502   // Wrap before binary operators.
10503   EXPECT_EQ("void f()\n"
10504             "{\n"
10505             "    if (aaaaaaaaaaaaaaaa\n"
10506             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10507             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10508             "        return;\n"
10509             "}",
10510             format("void f() {\n"
10511                    "if (aaaaaaaaaaaaaaaa\n"
10512                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10513                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10514                    "return;\n"
10515                    "}",
10516                    Style));
10517 
10518   // Allow functions on a single line.
10519   verifyFormat("void f() { return; }", Style);
10520 
10521   // Constructor initializers are formatted one per line with the "," on the
10522   // new line.
10523   verifyFormat("Constructor()\n"
10524                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10525                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10526                "          aaaaaaaaaaaaaa)\n"
10527                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10528                "{\n"
10529                "}",
10530                Style);
10531   verifyFormat("SomeClass::Constructor()\n"
10532                "    : a(a)\n"
10533                "{\n"
10534                "}",
10535                Style);
10536   EXPECT_EQ("SomeClass::Constructor()\n"
10537             "    : a(a)\n"
10538             "{\n"
10539             "}",
10540             format("SomeClass::Constructor():a(a){}", Style));
10541   verifyFormat("SomeClass::Constructor()\n"
10542                "    : a(a)\n"
10543                "    , b(b)\n"
10544                "    , c(c)\n"
10545                "{\n"
10546                "}",
10547                Style);
10548   verifyFormat("SomeClass::Constructor()\n"
10549                "    : a(a)\n"
10550                "{\n"
10551                "    foo();\n"
10552                "    bar();\n"
10553                "}",
10554                Style);
10555 
10556   // Access specifiers should be aligned left.
10557   verifyFormat("class C {\n"
10558                "public:\n"
10559                "    int i;\n"
10560                "};",
10561                Style);
10562 
10563   // Do not align comments.
10564   verifyFormat("int a; // Do not\n"
10565                "double b; // align comments.",
10566                Style);
10567 
10568   // Do not align operands.
10569   EXPECT_EQ("ASSERT(aaaa\n"
10570             "    || bbbb);",
10571             format("ASSERT ( aaaa\n||bbbb);", Style));
10572 
10573   // Accept input's line breaks.
10574   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10575             "    || bbbbbbbbbbbbbbb) {\n"
10576             "    i++;\n"
10577             "}",
10578             format("if (aaaaaaaaaaaaaaa\n"
10579                    "|| bbbbbbbbbbbbbbb) { i++; }",
10580                    Style));
10581   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10582             "    i++;\n"
10583             "}",
10584             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10585 
10586   // Don't automatically break all macro definitions (llvm.org/PR17842).
10587   verifyFormat("#define aNumber 10", Style);
10588   // However, generally keep the line breaks that the user authored.
10589   EXPECT_EQ("#define aNumber \\\n"
10590             "    10",
10591             format("#define aNumber \\\n"
10592                    " 10",
10593                    Style));
10594 
10595   // Keep empty and one-element array literals on a single line.
10596   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10597             "                                  copyItems:YES];",
10598             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10599                    "copyItems:YES];",
10600                    Style));
10601   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10602             "                                  copyItems:YES];",
10603             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10604                    "             copyItems:YES];",
10605                    Style));
10606   // FIXME: This does not seem right, there should be more indentation before
10607   // the array literal's entries. Nested blocks have the same problem.
10608   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10609             "    @\"a\",\n"
10610             "    @\"a\"\n"
10611             "]\n"
10612             "                                  copyItems:YES];",
10613             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10614                    "     @\"a\",\n"
10615                    "     @\"a\"\n"
10616                    "     ]\n"
10617                    "       copyItems:YES];",
10618                    Style));
10619   EXPECT_EQ(
10620       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10621       "                                  copyItems:YES];",
10622       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10623              "   copyItems:YES];",
10624              Style));
10625 
10626   verifyFormat("[self.a b:c c:d];", Style);
10627   EXPECT_EQ("[self.a b:c\n"
10628             "        c:d];",
10629             format("[self.a b:c\n"
10630                    "c:d];",
10631                    Style));
10632 }
10633 
10634 TEST_F(FormatTest, FormatsLambdas) {
10635   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10636   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10637   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10638   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10639   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10640   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10641   verifyFormat("int x = f(*+[] {});");
10642   verifyFormat("void f() {\n"
10643                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10644                "}\n");
10645   verifyFormat("void f() {\n"
10646                "  other(x.begin(), //\n"
10647                "        x.end(),   //\n"
10648                "        [&](int, int) { return 1; });\n"
10649                "}\n");
10650   verifyFormat("SomeFunction([]() { // A cool function...\n"
10651                "  return 43;\n"
10652                "});");
10653   EXPECT_EQ("SomeFunction([]() {\n"
10654             "#define A a\n"
10655             "  return 43;\n"
10656             "});",
10657             format("SomeFunction([](){\n"
10658                    "#define A a\n"
10659                    "return 43;\n"
10660                    "});"));
10661   verifyFormat("void f() {\n"
10662                "  SomeFunction([](decltype(x), A *a) {});\n"
10663                "}");
10664   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10665                "    [](const aaaaaaaaaa &a) { return a; });");
10666   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10667                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10668                "});");
10669   verifyFormat("Constructor()\n"
10670                "    : Field([] { // comment\n"
10671                "        int i;\n"
10672                "      }) {}");
10673   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10674                "  return some_parameter.size();\n"
10675                "};");
10676   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10677                "    [](const string &s) { return s; };");
10678   verifyFormat("int i = aaaaaa ? 1 //\n"
10679                "               : [] {\n"
10680                "                   return 2; //\n"
10681                "                 }();");
10682   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10683                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10684                "                  return x == 2; // force break\n"
10685                "                });");
10686   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10687                "    [=](int iiiiiiiiiiii) {\n"
10688                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
10689                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
10690                "    });",
10691                getLLVMStyleWithColumns(60));
10692   verifyFormat("SomeFunction({[&] {\n"
10693                "                // comment\n"
10694                "              },\n"
10695                "              [&] {\n"
10696                "                // comment\n"
10697                "              }});");
10698   verifyFormat("SomeFunction({[&] {\n"
10699                "  // comment\n"
10700                "}});");
10701   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10702                "                             [&]() { return true; },\n"
10703                "                         aaaaa aaaaaaaaa);");
10704 
10705   // Lambdas with return types.
10706   verifyFormat("int c = []() -> int { return 2; }();\n");
10707   verifyFormat("int c = []() -> int * { return 2; }();\n");
10708   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10709   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10710   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10711   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10712   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10713   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10714   verifyFormat("[a, a]() -> a<1> {};");
10715   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10716                "                   int j) -> int {\n"
10717                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10718                "};");
10719   verifyFormat(
10720       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10721       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10722       "      return aaaaaaaaaaaaaaaaa;\n"
10723       "    });",
10724       getLLVMStyleWithColumns(70));
10725   verifyFormat("[]() //\n"
10726                "    -> int {\n"
10727                "  return 1; //\n"
10728                "};");
10729 
10730   // Multiple lambdas in the same parentheses change indentation rules.
10731   verifyFormat("SomeFunction(\n"
10732                "    []() {\n"
10733                "      int i = 42;\n"
10734                "      return i;\n"
10735                "    },\n"
10736                "    []() {\n"
10737                "      int j = 43;\n"
10738                "      return j;\n"
10739                "    });");
10740 
10741   // More complex introducers.
10742   verifyFormat("return [i, args...] {};");
10743 
10744   // Not lambdas.
10745   verifyFormat("constexpr char hello[]{\"hello\"};");
10746   verifyFormat("double &operator[](int i) { return 0; }\n"
10747                "int i;");
10748   verifyFormat("std::unique_ptr<int[]> foo() {}");
10749   verifyFormat("int i = a[a][a]->f();");
10750   verifyFormat("int i = (*b)[a]->f();");
10751 
10752   // Other corner cases.
10753   verifyFormat("void f() {\n"
10754                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10755                "  );\n"
10756                "}");
10757 
10758   // Lambdas created through weird macros.
10759   verifyFormat("void f() {\n"
10760                "  MACRO((const AA &a) { return 1; });\n"
10761                "  MACRO((AA &a) { return 1; });\n"
10762                "}");
10763 
10764   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10765                "      doo_dah();\n"
10766                "      doo_dah();\n"
10767                "    })) {\n"
10768                "}");
10769   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
10770                "                doo_dah();\n"
10771                "                doo_dah();\n"
10772                "              })) {\n"
10773                "}");
10774   verifyFormat("auto lambda = []() {\n"
10775                "  int a = 2\n"
10776                "#if A\n"
10777                "          + 2\n"
10778                "#endif\n"
10779                "      ;\n"
10780                "};");
10781 
10782   // Lambdas with complex multiline introducers.
10783   verifyFormat(
10784       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10785       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
10786       "        -> ::std::unordered_set<\n"
10787       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
10788       "      //\n"
10789       "    });");
10790 }
10791 
10792 TEST_F(FormatTest, FormatsBlocks) {
10793   FormatStyle ShortBlocks = getLLVMStyle();
10794   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10795   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10796   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10797   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10798   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10799   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10800   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10801 
10802   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10803   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10804   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10805 
10806   verifyFormat("[operation setCompletionBlock:^{\n"
10807                "  [self onOperationDone];\n"
10808                "}];");
10809   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10810                "  [self onOperationDone];\n"
10811                "}]};");
10812   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10813                "  f();\n"
10814                "}];");
10815   verifyFormat("int a = [operation block:^int(int *i) {\n"
10816                "  return 1;\n"
10817                "}];");
10818   verifyFormat("[myObject doSomethingWith:arg1\n"
10819                "                      aaa:^int(int *a) {\n"
10820                "                        return 1;\n"
10821                "                      }\n"
10822                "                      bbb:f(a * bbbbbbbb)];");
10823 
10824   verifyFormat("[operation setCompletionBlock:^{\n"
10825                "  [self.delegate newDataAvailable];\n"
10826                "}];",
10827                getLLVMStyleWithColumns(60));
10828   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10829                "  NSString *path = [self sessionFilePath];\n"
10830                "  if (path) {\n"
10831                "    // ...\n"
10832                "  }\n"
10833                "});");
10834   verifyFormat("[[SessionService sharedService]\n"
10835                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10836                "      if (window) {\n"
10837                "        [self windowDidLoad:window];\n"
10838                "      } else {\n"
10839                "        [self errorLoadingWindow];\n"
10840                "      }\n"
10841                "    }];");
10842   verifyFormat("void (^largeBlock)(void) = ^{\n"
10843                "  // ...\n"
10844                "};\n",
10845                getLLVMStyleWithColumns(40));
10846   verifyFormat("[[SessionService sharedService]\n"
10847                "    loadWindowWithCompletionBlock: //\n"
10848                "        ^(SessionWindow *window) {\n"
10849                "          if (window) {\n"
10850                "            [self windowDidLoad:window];\n"
10851                "          } else {\n"
10852                "            [self errorLoadingWindow];\n"
10853                "          }\n"
10854                "        }];",
10855                getLLVMStyleWithColumns(60));
10856   verifyFormat("[myObject doSomethingWith:arg1\n"
10857                "    firstBlock:^(Foo *a) {\n"
10858                "      // ...\n"
10859                "      int i;\n"
10860                "    }\n"
10861                "    secondBlock:^(Bar *b) {\n"
10862                "      // ...\n"
10863                "      int i;\n"
10864                "    }\n"
10865                "    thirdBlock:^Foo(Bar *b) {\n"
10866                "      // ...\n"
10867                "      int i;\n"
10868                "    }];");
10869   verifyFormat("[myObject doSomethingWith:arg1\n"
10870                "               firstBlock:-1\n"
10871                "              secondBlock:^(Bar *b) {\n"
10872                "                // ...\n"
10873                "                int i;\n"
10874                "              }];");
10875 
10876   verifyFormat("f(^{\n"
10877                "  @autoreleasepool {\n"
10878                "    if (a) {\n"
10879                "      g();\n"
10880                "    }\n"
10881                "  }\n"
10882                "});");
10883   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10884   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
10885                "};");
10886 
10887   FormatStyle FourIndent = getLLVMStyle();
10888   FourIndent.ObjCBlockIndentWidth = 4;
10889   verifyFormat("[operation setCompletionBlock:^{\n"
10890                "    [self onOperationDone];\n"
10891                "}];",
10892                FourIndent);
10893 }
10894 
10895 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10896   FormatStyle ZeroColumn = getLLVMStyle();
10897   ZeroColumn.ColumnLimit = 0;
10898 
10899   verifyFormat("[[SessionService sharedService] "
10900                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10901                "  if (window) {\n"
10902                "    [self windowDidLoad:window];\n"
10903                "  } else {\n"
10904                "    [self errorLoadingWindow];\n"
10905                "  }\n"
10906                "}];",
10907                ZeroColumn);
10908   EXPECT_EQ("[[SessionService sharedService]\n"
10909             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10910             "      if (window) {\n"
10911             "        [self windowDidLoad:window];\n"
10912             "      } else {\n"
10913             "        [self errorLoadingWindow];\n"
10914             "      }\n"
10915             "    }];",
10916             format("[[SessionService sharedService]\n"
10917                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10918                    "                if (window) {\n"
10919                    "    [self windowDidLoad:window];\n"
10920                    "  } else {\n"
10921                    "    [self errorLoadingWindow];\n"
10922                    "  }\n"
10923                    "}];",
10924                    ZeroColumn));
10925   verifyFormat("[myObject doSomethingWith:arg1\n"
10926                "    firstBlock:^(Foo *a) {\n"
10927                "      // ...\n"
10928                "      int i;\n"
10929                "    }\n"
10930                "    secondBlock:^(Bar *b) {\n"
10931                "      // ...\n"
10932                "      int i;\n"
10933                "    }\n"
10934                "    thirdBlock:^Foo(Bar *b) {\n"
10935                "      // ...\n"
10936                "      int i;\n"
10937                "    }];",
10938                ZeroColumn);
10939   verifyFormat("f(^{\n"
10940                "  @autoreleasepool {\n"
10941                "    if (a) {\n"
10942                "      g();\n"
10943                "    }\n"
10944                "  }\n"
10945                "});",
10946                ZeroColumn);
10947   verifyFormat("void (^largeBlock)(void) = ^{\n"
10948                "  // ...\n"
10949                "};",
10950                ZeroColumn);
10951 
10952   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10953   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10954             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10955   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10956   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10957             "  int i;\n"
10958             "};",
10959             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10960 }
10961 
10962 TEST_F(FormatTest, SupportsCRLF) {
10963   EXPECT_EQ("int a;\r\n"
10964             "int b;\r\n"
10965             "int c;\r\n",
10966             format("int a;\r\n"
10967                    "  int b;\r\n"
10968                    "    int c;\r\n",
10969                    getLLVMStyle()));
10970   EXPECT_EQ("int a;\r\n"
10971             "int b;\r\n"
10972             "int c;\r\n",
10973             format("int a;\r\n"
10974                    "  int b;\n"
10975                    "    int c;\r\n",
10976                    getLLVMStyle()));
10977   EXPECT_EQ("int a;\n"
10978             "int b;\n"
10979             "int c;\n",
10980             format("int a;\r\n"
10981                    "  int b;\n"
10982                    "    int c;\n",
10983                    getLLVMStyle()));
10984   EXPECT_EQ("\"aaaaaaa \"\r\n"
10985             "\"bbbbbbb\";\r\n",
10986             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10987   EXPECT_EQ("#define A \\\r\n"
10988             "  b;      \\\r\n"
10989             "  c;      \\\r\n"
10990             "  d;\r\n",
10991             format("#define A \\\r\n"
10992                    "  b; \\\r\n"
10993                    "  c; d; \r\n",
10994                    getGoogleStyle()));
10995 
10996   EXPECT_EQ("/*\r\n"
10997             "multi line block comments\r\n"
10998             "should not introduce\r\n"
10999             "an extra carriage return\r\n"
11000             "*/\r\n",
11001             format("/*\r\n"
11002                    "multi line block comments\r\n"
11003                    "should not introduce\r\n"
11004                    "an extra carriage return\r\n"
11005                    "*/\r\n"));
11006 }
11007 
11008 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11009   verifyFormat("MY_CLASS(C) {\n"
11010                "  int i;\n"
11011                "  int j;\n"
11012                "};");
11013 }
11014 
11015 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11016   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11017   TwoIndent.ContinuationIndentWidth = 2;
11018 
11019   EXPECT_EQ("int i =\n"
11020             "  longFunction(\n"
11021             "    arg);",
11022             format("int i = longFunction(arg);", TwoIndent));
11023 
11024   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11025   SixIndent.ContinuationIndentWidth = 6;
11026 
11027   EXPECT_EQ("int i =\n"
11028             "      longFunction(\n"
11029             "            arg);",
11030             format("int i = longFunction(arg);", SixIndent));
11031 }
11032 
11033 TEST_F(FormatTest, SpacesInAngles) {
11034   FormatStyle Spaces = getLLVMStyle();
11035   Spaces.SpacesInAngles = true;
11036 
11037   verifyFormat("static_cast< int >(arg);", Spaces);
11038   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11039   verifyFormat("f< int, float >();", Spaces);
11040   verifyFormat("template <> g() {}", Spaces);
11041   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11042   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11043   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11044                Spaces);
11045 
11046   Spaces.Standard = FormatStyle::LS_Cpp03;
11047   Spaces.SpacesInAngles = true;
11048   verifyFormat("A< A< int > >();", Spaces);
11049 
11050   Spaces.SpacesInAngles = false;
11051   verifyFormat("A<A<int> >();", Spaces);
11052 
11053   Spaces.Standard = FormatStyle::LS_Cpp11;
11054   Spaces.SpacesInAngles = true;
11055   verifyFormat("A< A< int > >();", Spaces);
11056 
11057   Spaces.SpacesInAngles = false;
11058   verifyFormat("A<A<int>>();", Spaces);
11059 }
11060 
11061 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11062   FormatStyle Style = getLLVMStyle();
11063   Style.SpaceAfterTemplateKeyword = false;
11064   verifyFormat("template<int> void foo();", Style);
11065 }
11066 
11067 TEST_F(FormatTest, TripleAngleBrackets) {
11068   verifyFormat("f<<<1, 1>>>();");
11069   verifyFormat("f<<<1, 1, 1, s>>>();");
11070   verifyFormat("f<<<a, b, c, d>>>();");
11071   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11072   verifyFormat("f<param><<<1, 1>>>();");
11073   verifyFormat("f<1><<<1, 1>>>();");
11074   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11075   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11076                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11077   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11078                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11079 }
11080 
11081 TEST_F(FormatTest, MergeLessLessAtEnd) {
11082   verifyFormat("<<");
11083   EXPECT_EQ("< < <", format("\\\n<<<"));
11084   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11085                "aaallvm::outs() <<");
11086   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11087                "aaaallvm::outs()\n    <<");
11088 }
11089 
11090 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11091   std::string code = "#if A\n"
11092                      "#if B\n"
11093                      "a.\n"
11094                      "#endif\n"
11095                      "    a = 1;\n"
11096                      "#else\n"
11097                      "#endif\n"
11098                      "#if C\n"
11099                      "#else\n"
11100                      "#endif\n";
11101   EXPECT_EQ(code, format(code));
11102 }
11103 
11104 TEST_F(FormatTest, HandleConflictMarkers) {
11105   // Git/SVN conflict markers.
11106   EXPECT_EQ("int a;\n"
11107             "void f() {\n"
11108             "  callme(some(parameter1,\n"
11109             "<<<<<<< text by the vcs\n"
11110             "              parameter2),\n"
11111             "||||||| text by the vcs\n"
11112             "              parameter2),\n"
11113             "         parameter3,\n"
11114             "======= text by the vcs\n"
11115             "              parameter2, parameter3),\n"
11116             ">>>>>>> text by the vcs\n"
11117             "         otherparameter);\n",
11118             format("int a;\n"
11119                    "void f() {\n"
11120                    "  callme(some(parameter1,\n"
11121                    "<<<<<<< text by the vcs\n"
11122                    "  parameter2),\n"
11123                    "||||||| text by the vcs\n"
11124                    "  parameter2),\n"
11125                    "  parameter3,\n"
11126                    "======= text by the vcs\n"
11127                    "  parameter2,\n"
11128                    "  parameter3),\n"
11129                    ">>>>>>> text by the vcs\n"
11130                    "  otherparameter);\n"));
11131 
11132   // Perforce markers.
11133   EXPECT_EQ("void f() {\n"
11134             "  function(\n"
11135             ">>>> text by the vcs\n"
11136             "      parameter,\n"
11137             "==== text by the vcs\n"
11138             "      parameter,\n"
11139             "==== text by the vcs\n"
11140             "      parameter,\n"
11141             "<<<< text by the vcs\n"
11142             "      parameter);\n",
11143             format("void f() {\n"
11144                    "  function(\n"
11145                    ">>>> text by the vcs\n"
11146                    "  parameter,\n"
11147                    "==== text by the vcs\n"
11148                    "  parameter,\n"
11149                    "==== text by the vcs\n"
11150                    "  parameter,\n"
11151                    "<<<< text by the vcs\n"
11152                    "  parameter);\n"));
11153 
11154   EXPECT_EQ("<<<<<<<\n"
11155             "|||||||\n"
11156             "=======\n"
11157             ">>>>>>>",
11158             format("<<<<<<<\n"
11159                    "|||||||\n"
11160                    "=======\n"
11161                    ">>>>>>>"));
11162 
11163   EXPECT_EQ("<<<<<<<\n"
11164             "|||||||\n"
11165             "int i;\n"
11166             "=======\n"
11167             ">>>>>>>",
11168             format("<<<<<<<\n"
11169                    "|||||||\n"
11170                    "int i;\n"
11171                    "=======\n"
11172                    ">>>>>>>"));
11173 
11174   // FIXME: Handle parsing of macros around conflict markers correctly:
11175   EXPECT_EQ("#define Macro \\\n"
11176             "<<<<<<<\n"
11177             "Something \\\n"
11178             "|||||||\n"
11179             "Else \\\n"
11180             "=======\n"
11181             "Other \\\n"
11182             ">>>>>>>\n"
11183             "    End int i;\n",
11184             format("#define Macro \\\n"
11185                    "<<<<<<<\n"
11186                    "  Something \\\n"
11187                    "|||||||\n"
11188                    "  Else \\\n"
11189                    "=======\n"
11190                    "  Other \\\n"
11191                    ">>>>>>>\n"
11192                    "  End\n"
11193                    "int i;\n"));
11194 }
11195 
11196 TEST_F(FormatTest, DisableRegions) {
11197   EXPECT_EQ("int i;\n"
11198             "// clang-format off\n"
11199             "  int j;\n"
11200             "// clang-format on\n"
11201             "int k;",
11202             format(" int  i;\n"
11203                    "   // clang-format off\n"
11204                    "  int j;\n"
11205                    " // clang-format on\n"
11206                    "   int   k;"));
11207   EXPECT_EQ("int i;\n"
11208             "/* clang-format off */\n"
11209             "  int j;\n"
11210             "/* clang-format on */\n"
11211             "int k;",
11212             format(" int  i;\n"
11213                    "   /* clang-format off */\n"
11214                    "  int j;\n"
11215                    " /* clang-format on */\n"
11216                    "   int   k;"));
11217 
11218   // Don't reflow comments within disabled regions.
11219   EXPECT_EQ(
11220       "// clang-format off\n"
11221       "// long long long long long long line\n"
11222       "/* clang-format on */\n"
11223       "/* long long long\n"
11224       " * long long long\n"
11225       " * line */\n"
11226       "int i;\n"
11227       "/* clang-format off */\n"
11228       "/* long long long long long long line */\n",
11229       format("// clang-format off\n"
11230              "// long long long long long long line\n"
11231              "/* clang-format on */\n"
11232              "/* long long long long long long line */\n"
11233              "int i;\n"
11234              "/* clang-format off */\n"
11235              "/* long long long long long long line */\n",
11236              getLLVMStyleWithColumns(20)));
11237 }
11238 
11239 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11240   format("? ) =");
11241   verifyNoCrash("#define a\\\n /**/}");
11242 }
11243 
11244 TEST_F(FormatTest, FormatsTableGenCode) {
11245   FormatStyle Style = getLLVMStyle();
11246   Style.Language = FormatStyle::LK_TableGen;
11247   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11248 }
11249 
11250 TEST_F(FormatTest, ArrayOfTemplates) {
11251   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
11252             format("auto a = new unique_ptr<int > [ 10];"));
11253 
11254   FormatStyle Spaces = getLLVMStyle();
11255   Spaces.SpacesInSquareBrackets = true;
11256   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
11257             format("auto a = new unique_ptr<int > [10];", Spaces));
11258 }
11259 
11260 TEST_F(FormatTest, ArrayAsTemplateType) {
11261   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
11262             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
11263 
11264   FormatStyle Spaces = getLLVMStyle();
11265   Spaces.SpacesInSquareBrackets = true;
11266   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
11267             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
11268 }
11269 
11270 TEST_F(FormatTest, NoSpaceAfterSuper) {
11271     verifyFormat("__super::FooBar();");
11272 }
11273 
11274 TEST(FormatStyle, GetStyleOfFile) {
11275   vfs::InMemoryFileSystem FS;
11276   // Test 1: format file in the same directory.
11277   ASSERT_TRUE(
11278       FS.addFile("/a/.clang-format", 0,
11279                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11280   ASSERT_TRUE(
11281       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11282   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
11283   ASSERT_TRUE((bool)Style1);
11284   ASSERT_EQ(*Style1, getLLVMStyle());
11285 
11286   // Test 2.1: fallback to default.
11287   ASSERT_TRUE(
11288       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11289   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
11290   ASSERT_TRUE((bool)Style2);
11291   ASSERT_EQ(*Style2, getMozillaStyle());
11292 
11293   // Test 2.2: no format on 'none' fallback style.
11294   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11295   ASSERT_TRUE((bool)Style2);
11296   ASSERT_EQ(*Style2, getNoStyle());
11297 
11298   // Test 2.3: format if config is found with no based style while fallback is
11299   // 'none'.
11300   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
11301                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
11302   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11303   ASSERT_TRUE((bool)Style2);
11304   ASSERT_EQ(*Style2, getLLVMStyle());
11305 
11306   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
11307   Style2 = getStyle("{}", "a.h", "none", "", &FS);
11308   ASSERT_TRUE((bool)Style2);
11309   ASSERT_EQ(*Style2, getLLVMStyle());
11310 
11311   // Test 3: format file in parent directory.
11312   ASSERT_TRUE(
11313       FS.addFile("/c/.clang-format", 0,
11314                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11315   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11316                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11317   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11318   ASSERT_TRUE((bool)Style3);
11319   ASSERT_EQ(*Style3, getGoogleStyle());
11320 
11321   // Test 4: error on invalid fallback style
11322   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11323   ASSERT_FALSE((bool)Style4);
11324   llvm::consumeError(Style4.takeError());
11325 
11326   // Test 5: error on invalid yaml on command line
11327   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11328   ASSERT_FALSE((bool)Style5);
11329   llvm::consumeError(Style5.takeError());
11330 
11331   // Test 6: error on invalid style
11332   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11333   ASSERT_FALSE((bool)Style6);
11334   llvm::consumeError(Style6.takeError());
11335 
11336   // Test 7: found config file, error on parsing it
11337   ASSERT_TRUE(
11338       FS.addFile("/d/.clang-format", 0,
11339                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11340                                                   "InvalidKey: InvalidValue")));
11341   ASSERT_TRUE(
11342       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11343   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11344   ASSERT_FALSE((bool)Style7);
11345   llvm::consumeError(Style7.takeError());
11346 }
11347 
11348 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11349   // Column limit is 20.
11350   std::string Code = "Type *a =\n"
11351                      "    new Type();\n"
11352                      "g(iiiii, 0, jjjjj,\n"
11353                      "  0, kkkkk, 0, mm);\n"
11354                      "int  bad     = format   ;";
11355   std::string Expected = "auto a = new Type();\n"
11356                          "g(iiiii, nullptr,\n"
11357                          "  jjjjj, nullptr,\n"
11358                          "  kkkkk, nullptr,\n"
11359                          "  mm);\n"
11360                          "int  bad     = format   ;";
11361   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11362   tooling::Replacements Replaces = toReplacements(
11363       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11364                             "auto "),
11365        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11366                             "nullptr"),
11367        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11368                             "nullptr"),
11369        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11370                             "nullptr")});
11371 
11372   format::FormatStyle Style = format::getLLVMStyle();
11373   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11374   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11375   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11376       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11377   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11378   EXPECT_TRUE(static_cast<bool>(Result));
11379   EXPECT_EQ(Expected, *Result);
11380 }
11381 
11382 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11383   std::string Code = "#include \"a.h\"\n"
11384                      "#include \"c.h\"\n"
11385                      "\n"
11386                      "int main() {\n"
11387                      "  return 0;\n"
11388                      "}";
11389   std::string Expected = "#include \"a.h\"\n"
11390                          "#include \"b.h\"\n"
11391                          "#include \"c.h\"\n"
11392                          "\n"
11393                          "int main() {\n"
11394                          "  return 0;\n"
11395                          "}";
11396   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11397   tooling::Replacements Replaces = toReplacements(
11398       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11399                             "#include \"b.h\"\n")});
11400 
11401   format::FormatStyle Style = format::getLLVMStyle();
11402   Style.SortIncludes = true;
11403   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11404   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11405       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11406   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11407   EXPECT_TRUE(static_cast<bool>(Result));
11408   EXPECT_EQ(Expected, *Result);
11409 }
11410 
11411 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
11412   EXPECT_EQ("using std::cin;\n"
11413             "using std::cout;",
11414             format("using std::cout;\n"
11415                    "using std::cin;", getGoogleStyle()));
11416 }
11417 
11418 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
11419   format::FormatStyle Style = format::getLLVMStyle();
11420   Style.Standard = FormatStyle::LS_Cpp03;
11421   // cpp03 recognize this string as identifier u8 and literal character 'a'
11422   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
11423 }
11424 
11425 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
11426   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
11427   // all modes, including C++11, C++14 and C++17
11428   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
11429 }
11430 
11431 TEST_F(FormatTest, DoNotFormatLikelyXml) {
11432   EXPECT_EQ("<!-- ;> -->",
11433             format("<!-- ;> -->", getGoogleStyle()));
11434   EXPECT_EQ(" <!-- >; -->",
11435             format(" <!-- >; -->", getGoogleStyle()));
11436 }
11437 
11438 } // end namespace
11439 } // end namespace format
11440 } // end namespace clang
11441