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.ColumnLimit = 40;
443   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
444 
445   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
446   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
447 
448   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
449   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
450   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
451 
452   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
453   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
454   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
455   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
456   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
457   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
458   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
459   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
460   verifyFormat("if (true) {\n"
461                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
462                "}",
463                AllowSimpleBracedStatements);
464   verifyFormat("if (true) { //\n"
465                "  f();\n"
466                "}",
467                AllowSimpleBracedStatements);
468   verifyFormat("if (true) {\n"
469                "  f();\n"
470                "  f();\n"
471                "}",
472                AllowSimpleBracedStatements);
473   verifyFormat("if (true) {\n"
474                "  f();\n"
475                "} else {\n"
476                "  f();\n"
477                "}",
478                AllowSimpleBracedStatements);
479 
480   verifyFormat("struct A2 {\n"
481                "  int X;\n"
482                "};",
483                AllowSimpleBracedStatements);
484   verifyFormat("typedef struct A2 {\n"
485                "  int X;\n"
486                "} A2_t;",
487                AllowSimpleBracedStatements);
488   verifyFormat("template <int> struct A2 {\n"
489                "  struct B {};\n"
490                "};",
491                AllowSimpleBracedStatements);
492 
493   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
494   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
495   verifyFormat("if (true) {\n"
496                "  f();\n"
497                "}",
498                AllowSimpleBracedStatements);
499   verifyFormat("if (true) {\n"
500                "  f();\n"
501                "} else {\n"
502                "  f();\n"
503                "}",
504                AllowSimpleBracedStatements);
505 
506   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
507   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
508   verifyFormat("while (true) {\n"
509                "  f();\n"
510                "}",
511                AllowSimpleBracedStatements);
512   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
513   verifyFormat("for (;;) {\n"
514                "  f();\n"
515                "}",
516                AllowSimpleBracedStatements);
517 
518   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
519   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
520   AllowSimpleBracedStatements.BraceWrapping.AfterControlStatement = true;
521 
522   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
523   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
524   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
525   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
526   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
527   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
528   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
529   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
530   verifyFormat("if (true)\n"
531                "{\n"
532                "  ffffffffffffffffffffffffffffffffffffffffffffffffffffff();\n"
533                "}",
534                AllowSimpleBracedStatements);
535   verifyFormat("if (true)\n"
536                "{ //\n"
537                "  f();\n"
538                "}",
539                AllowSimpleBracedStatements);
540   verifyFormat("if (true)\n"
541                "{\n"
542                "  f();\n"
543                "  f();\n"
544                "}",
545                AllowSimpleBracedStatements);
546   verifyFormat("if (true)\n"
547                "{\n"
548                "  f();\n"
549                "} else\n"
550                "{\n"
551                "  f();\n"
552                "}",
553                AllowSimpleBracedStatements);
554 
555   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
556   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
557   verifyFormat("if (true)\n"
558                "{\n"
559                "  f();\n"
560                "}",
561                AllowSimpleBracedStatements);
562   verifyFormat("if (true)\n"
563                "{\n"
564                "  f();\n"
565                "} else\n"
566                "{\n"
567                "  f();\n"
568                "}",
569                AllowSimpleBracedStatements);
570 
571   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
572   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
573   verifyFormat("while (true)\n"
574                "{\n"
575                "  f();\n"
576                "}",
577                AllowSimpleBracedStatements);
578   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
579   verifyFormat("for (;;)\n"
580                "{\n"
581                "  f();\n"
582                "}",
583                AllowSimpleBracedStatements);
584 }
585 
586 TEST_F(FormatTest, ParseIfElse) {
587   verifyFormat("if (true)\n"
588                "  if (true)\n"
589                "    if (true)\n"
590                "      f();\n"
591                "    else\n"
592                "      g();\n"
593                "  else\n"
594                "    h();\n"
595                "else\n"
596                "  i();");
597   verifyFormat("if (true)\n"
598                "  if (true)\n"
599                "    if (true) {\n"
600                "      if (true)\n"
601                "        f();\n"
602                "    } else {\n"
603                "      g();\n"
604                "    }\n"
605                "  else\n"
606                "    h();\n"
607                "else {\n"
608                "  i();\n"
609                "}");
610   verifyFormat("if (true)\n"
611                "  if constexpr (true)\n"
612                "    if (true) {\n"
613                "      if constexpr (true)\n"
614                "        f();\n"
615                "    } else {\n"
616                "      g();\n"
617                "    }\n"
618                "  else\n"
619                "    h();\n"
620                "else {\n"
621                "  i();\n"
622                "}");
623   verifyFormat("void f() {\n"
624                "  if (a) {\n"
625                "  } else {\n"
626                "  }\n"
627                "}");
628 }
629 
630 TEST_F(FormatTest, ElseIf) {
631   verifyFormat("if (a) {\n} else if (b) {\n}");
632   verifyFormat("if (a)\n"
633                "  f();\n"
634                "else if (b)\n"
635                "  g();\n"
636                "else\n"
637                "  h();");
638   verifyFormat("if constexpr (a)\n"
639                "  f();\n"
640                "else if constexpr (b)\n"
641                "  g();\n"
642                "else\n"
643                "  h();");
644   verifyFormat("if (a) {\n"
645                "  f();\n"
646                "}\n"
647                "// or else ..\n"
648                "else {\n"
649                "  g()\n"
650                "}");
651 
652   verifyFormat("if (a) {\n"
653                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
654                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
655                "}");
656   verifyFormat("if (a) {\n"
657                "} else if (\n"
658                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
659                "}",
660                getLLVMStyleWithColumns(62));
661   verifyFormat("if (a) {\n"
662                "} else if constexpr (\n"
663                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
664                "}",
665                getLLVMStyleWithColumns(62));
666 }
667 
668 TEST_F(FormatTest, FormatsForLoop) {
669   verifyFormat(
670       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
671       "     ++VeryVeryLongLoopVariable)\n"
672       "  ;");
673   verifyFormat("for (;;)\n"
674                "  f();");
675   verifyFormat("for (;;) {\n}");
676   verifyFormat("for (;;) {\n"
677                "  f();\n"
678                "}");
679   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
680 
681   verifyFormat(
682       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
683       "                                          E = UnwrappedLines.end();\n"
684       "     I != E; ++I) {\n}");
685 
686   verifyFormat(
687       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
688       "     ++IIIII) {\n}");
689   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
690                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
691                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
692   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
693                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
694                "         E = FD->getDeclsInPrototypeScope().end();\n"
695                "     I != E; ++I) {\n}");
696   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
697                "         I = Container.begin(),\n"
698                "         E = Container.end();\n"
699                "     I != E; ++I) {\n}",
700                getLLVMStyleWithColumns(76));
701 
702   verifyFormat(
703       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
704       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
705       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
706       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
707       "     ++aaaaaaaaaaa) {\n}");
708   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
709                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
710                "     ++i) {\n}");
711   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
712                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
713                "}");
714   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
715                "         aaaaaaaaaa);\n"
716                "     iter; ++iter) {\n"
717                "}");
718   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
719                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
720                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
721                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
722 
723   FormatStyle NoBinPacking = getLLVMStyle();
724   NoBinPacking.BinPackParameters = false;
725   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
726                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
727                "                                           aaaaaaaaaaaaaaaa,\n"
728                "                                           aaaaaaaaaaaaaaaa,\n"
729                "                                           aaaaaaaaaaaaaaaa);\n"
730                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
731                "}",
732                NoBinPacking);
733   verifyFormat(
734       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
735       "                                          E = UnwrappedLines.end();\n"
736       "     I != E;\n"
737       "     ++I) {\n}",
738       NoBinPacking);
739 
740   FormatStyle AlignLeft = getLLVMStyle();
741   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
742   verifyFormat("for (A* a = start; a < end; ++a, ++value) {\n}", AlignLeft);
743 }
744 
745 TEST_F(FormatTest, RangeBasedForLoops) {
746   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
747                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
748   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
749                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
750   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
751                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
752   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
753                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
754 }
755 
756 TEST_F(FormatTest, ForEachLoops) {
757   verifyFormat("void f() {\n"
758                "  foreach (Item *item, itemlist) {}\n"
759                "  Q_FOREACH (Item *item, itemlist) {}\n"
760                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
761                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
762                "}");
763 
764   // As function-like macros.
765   verifyFormat("#define foreach(x, y)\n"
766                "#define Q_FOREACH(x, y)\n"
767                "#define BOOST_FOREACH(x, y)\n"
768                "#define UNKNOWN_FOREACH(x, y)\n");
769 
770   // Not as function-like macros.
771   verifyFormat("#define foreach (x, y)\n"
772                "#define Q_FOREACH (x, y)\n"
773                "#define BOOST_FOREACH (x, y)\n"
774                "#define UNKNOWN_FOREACH (x, y)\n");
775 }
776 
777 TEST_F(FormatTest, FormatsWhileLoop) {
778   verifyFormat("while (true) {\n}");
779   verifyFormat("while (true)\n"
780                "  f();");
781   verifyFormat("while () {\n}");
782   verifyFormat("while () {\n"
783                "  f();\n"
784                "}");
785 }
786 
787 TEST_F(FormatTest, FormatsDoWhile) {
788   verifyFormat("do {\n"
789                "  do_something();\n"
790                "} while (something());");
791   verifyFormat("do\n"
792                "  do_something();\n"
793                "while (something());");
794 }
795 
796 TEST_F(FormatTest, FormatsSwitchStatement) {
797   verifyFormat("switch (x) {\n"
798                "case 1:\n"
799                "  f();\n"
800                "  break;\n"
801                "case kFoo:\n"
802                "case ns::kBar:\n"
803                "case kBaz:\n"
804                "  break;\n"
805                "default:\n"
806                "  g();\n"
807                "  break;\n"
808                "}");
809   verifyFormat("switch (x) {\n"
810                "case 1: {\n"
811                "  f();\n"
812                "  break;\n"
813                "}\n"
814                "case 2: {\n"
815                "  break;\n"
816                "}\n"
817                "}");
818   verifyFormat("switch (x) {\n"
819                "case 1: {\n"
820                "  f();\n"
821                "  {\n"
822                "    g();\n"
823                "    h();\n"
824                "  }\n"
825                "  break;\n"
826                "}\n"
827                "}");
828   verifyFormat("switch (x) {\n"
829                "case 1: {\n"
830                "  f();\n"
831                "  if (foo) {\n"
832                "    g();\n"
833                "    h();\n"
834                "  }\n"
835                "  break;\n"
836                "}\n"
837                "}");
838   verifyFormat("switch (x) {\n"
839                "case 1: {\n"
840                "  f();\n"
841                "  g();\n"
842                "} break;\n"
843                "}");
844   verifyFormat("switch (test)\n"
845                "  ;");
846   verifyFormat("switch (x) {\n"
847                "default: {\n"
848                "  // Do nothing.\n"
849                "}\n"
850                "}");
851   verifyFormat("switch (x) {\n"
852                "// comment\n"
853                "// if 1, do f()\n"
854                "case 1:\n"
855                "  f();\n"
856                "}");
857   verifyFormat("switch (x) {\n"
858                "case 1:\n"
859                "  // Do amazing stuff\n"
860                "  {\n"
861                "    f();\n"
862                "    g();\n"
863                "  }\n"
864                "  break;\n"
865                "}");
866   verifyFormat("#define A          \\\n"
867                "  switch (x) {     \\\n"
868                "  case a:          \\\n"
869                "    foo = b;       \\\n"
870                "  }",
871                getLLVMStyleWithColumns(20));
872   verifyFormat("#define OPERATION_CASE(name)           \\\n"
873                "  case OP_name:                        \\\n"
874                "    return operations::Operation##name\n",
875                getLLVMStyleWithColumns(40));
876   verifyFormat("switch (x) {\n"
877                "case 1:;\n"
878                "default:;\n"
879                "  int i;\n"
880                "}");
881 
882   verifyGoogleFormat("switch (x) {\n"
883                      "  case 1:\n"
884                      "    f();\n"
885                      "    break;\n"
886                      "  case kFoo:\n"
887                      "  case ns::kBar:\n"
888                      "  case kBaz:\n"
889                      "    break;\n"
890                      "  default:\n"
891                      "    g();\n"
892                      "    break;\n"
893                      "}");
894   verifyGoogleFormat("switch (x) {\n"
895                      "  case 1: {\n"
896                      "    f();\n"
897                      "    break;\n"
898                      "  }\n"
899                      "}");
900   verifyGoogleFormat("switch (test)\n"
901                      "  ;");
902 
903   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
904                      "  case OP_name:              \\\n"
905                      "    return operations::Operation##name\n");
906   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
907                      "  // Get the correction operation class.\n"
908                      "  switch (OpCode) {\n"
909                      "    CASE(Add);\n"
910                      "    CASE(Subtract);\n"
911                      "    default:\n"
912                      "      return operations::Unknown;\n"
913                      "  }\n"
914                      "#undef OPERATION_CASE\n"
915                      "}");
916   verifyFormat("DEBUG({\n"
917                "  switch (x) {\n"
918                "  case A:\n"
919                "    f();\n"
920                "    break;\n"
921                "    // fallthrough\n"
922                "  case B:\n"
923                "    g();\n"
924                "    break;\n"
925                "  }\n"
926                "});");
927   EXPECT_EQ("DEBUG({\n"
928             "  switch (x) {\n"
929             "  case A:\n"
930             "    f();\n"
931             "    break;\n"
932             "  // On B:\n"
933             "  case B:\n"
934             "    g();\n"
935             "    break;\n"
936             "  }\n"
937             "});",
938             format("DEBUG({\n"
939                    "  switch (x) {\n"
940                    "  case A:\n"
941                    "    f();\n"
942                    "    break;\n"
943                    "  // On B:\n"
944                    "  case B:\n"
945                    "    g();\n"
946                    "    break;\n"
947                    "  }\n"
948                    "});",
949                    getLLVMStyle()));
950   verifyFormat("switch (a) {\n"
951                "case (b):\n"
952                "  return;\n"
953                "}");
954 
955   verifyFormat("switch (a) {\n"
956                "case some_namespace::\n"
957                "    some_constant:\n"
958                "  return;\n"
959                "}",
960                getLLVMStyleWithColumns(34));
961 }
962 
963 TEST_F(FormatTest, CaseRanges) {
964   verifyFormat("switch (x) {\n"
965                "case 'A' ... 'Z':\n"
966                "case 1 ... 5:\n"
967                "case a ... b:\n"
968                "  break;\n"
969                "}");
970 }
971 
972 TEST_F(FormatTest, ShortCaseLabels) {
973   FormatStyle Style = getLLVMStyle();
974   Style.AllowShortCaseLabelsOnASingleLine = true;
975   verifyFormat("switch (a) {\n"
976                "case 1: x = 1; break;\n"
977                "case 2: return;\n"
978                "case 3:\n"
979                "case 4:\n"
980                "case 5: return;\n"
981                "case 6: // comment\n"
982                "  return;\n"
983                "case 7:\n"
984                "  // comment\n"
985                "  return;\n"
986                "case 8:\n"
987                "  x = 8; // comment\n"
988                "  break;\n"
989                "default: y = 1; break;\n"
990                "}",
991                Style);
992   verifyFormat("switch (a) {\n"
993                "case 0: return; // comment\n"
994                "case 1: break;  // comment\n"
995                "case 2: return;\n"
996                "// comment\n"
997                "case 3: return;\n"
998                "// comment 1\n"
999                "// comment 2\n"
1000                "// comment 3\n"
1001                "case 4: break; /* comment */\n"
1002                "case 5:\n"
1003                "  // comment\n"
1004                "  break;\n"
1005                "case 6: /* comment */ x = 1; break;\n"
1006                "case 7: x = /* comment */ 1; break;\n"
1007                "case 8:\n"
1008                "  x = 1; /* comment */\n"
1009                "  break;\n"
1010                "case 9:\n"
1011                "  break; // comment line 1\n"
1012                "         // comment line 2\n"
1013                "}",
1014                Style);
1015   EXPECT_EQ("switch (a) {\n"
1016             "case 1:\n"
1017             "  x = 8;\n"
1018             "  // fall through\n"
1019             "case 2: x = 8;\n"
1020             "// comment\n"
1021             "case 3:\n"
1022             "  return; /* comment line 1\n"
1023             "           * comment line 2 */\n"
1024             "case 4: i = 8;\n"
1025             "// something else\n"
1026             "#if FOO\n"
1027             "case 5: break;\n"
1028             "#endif\n"
1029             "}",
1030             format("switch (a) {\n"
1031                    "case 1: x = 8;\n"
1032                    "  // fall through\n"
1033                    "case 2:\n"
1034                    "  x = 8;\n"
1035                    "// comment\n"
1036                    "case 3:\n"
1037                    "  return; /* comment line 1\n"
1038                    "           * comment line 2 */\n"
1039                    "case 4:\n"
1040                    "  i = 8;\n"
1041                    "// something else\n"
1042                    "#if FOO\n"
1043                    "case 5: break;\n"
1044                    "#endif\n"
1045                    "}",
1046                    Style));
1047   EXPECT_EQ("switch (a) {\n" "case 0:\n"
1048             "  return; // long long long long long long long long long long long long comment\n"
1049             "          // line\n" "}",
1050             format("switch (a) {\n"
1051                    "case 0: return; // long long long long long long long long long long long long comment line\n"
1052                    "}",
1053                    Style));
1054   EXPECT_EQ("switch (a) {\n"
1055             "case 0:\n"
1056             "  return; /* long long long long long long long long long long long long comment\n"
1057             "             line */\n"
1058             "}",
1059             format("switch (a) {\n"
1060                    "case 0: return; /* long long long long long long long long long long long long comment line */\n"
1061                    "}",
1062                    Style));
1063   verifyFormat("switch (a) {\n"
1064                "#if FOO\n"
1065                "case 0: return 0;\n"
1066                "#endif\n"
1067                "}",
1068                Style);
1069   verifyFormat("switch (a) {\n"
1070                "case 1: {\n"
1071                "}\n"
1072                "case 2: {\n"
1073                "  return;\n"
1074                "}\n"
1075                "case 3: {\n"
1076                "  x = 1;\n"
1077                "  return;\n"
1078                "}\n"
1079                "case 4:\n"
1080                "  if (x)\n"
1081                "    return;\n"
1082                "}",
1083                Style);
1084   Style.ColumnLimit = 21;
1085   verifyFormat("switch (a) {\n"
1086                "case 1: x = 1; break;\n"
1087                "case 2: return;\n"
1088                "case 3:\n"
1089                "case 4:\n"
1090                "case 5: return;\n"
1091                "default:\n"
1092                "  y = 1;\n"
1093                "  break;\n"
1094                "}",
1095                Style);
1096 }
1097 
1098 TEST_F(FormatTest, FormatsLabels) {
1099   verifyFormat("void f() {\n"
1100                "  some_code();\n"
1101                "test_label:\n"
1102                "  some_other_code();\n"
1103                "  {\n"
1104                "    some_more_code();\n"
1105                "  another_label:\n"
1106                "    some_more_code();\n"
1107                "  }\n"
1108                "}");
1109   verifyFormat("{\n"
1110                "  some_code();\n"
1111                "test_label:\n"
1112                "  some_other_code();\n"
1113                "}");
1114   verifyFormat("{\n"
1115                "  some_code();\n"
1116                "test_label:;\n"
1117                "  int i = 0;\n"
1118                "}");
1119 }
1120 
1121 //===----------------------------------------------------------------------===//
1122 // Tests for classes, namespaces, etc.
1123 //===----------------------------------------------------------------------===//
1124 
1125 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1126   verifyFormat("class A {};");
1127 }
1128 
1129 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1130   verifyFormat("class A {\n"
1131                "public:\n"
1132                "public: // comment\n"
1133                "protected:\n"
1134                "private:\n"
1135                "  void f() {}\n"
1136                "};");
1137   verifyGoogleFormat("class A {\n"
1138                      " public:\n"
1139                      " protected:\n"
1140                      " private:\n"
1141                      "  void f() {}\n"
1142                      "};");
1143   verifyFormat("class A {\n"
1144                "public slots:\n"
1145                "  void f1() {}\n"
1146                "public Q_SLOTS:\n"
1147                "  void f2() {}\n"
1148                "protected slots:\n"
1149                "  void f3() {}\n"
1150                "protected Q_SLOTS:\n"
1151                "  void f4() {}\n"
1152                "private slots:\n"
1153                "  void f5() {}\n"
1154                "private Q_SLOTS:\n"
1155                "  void f6() {}\n"
1156                "signals:\n"
1157                "  void g1();\n"
1158                "Q_SIGNALS:\n"
1159                "  void g2();\n"
1160                "};");
1161 
1162   // Don't interpret 'signals' the wrong way.
1163   verifyFormat("signals.set();");
1164   verifyFormat("for (Signals signals : f()) {\n}");
1165   verifyFormat("{\n"
1166                "  signals.set(); // This needs indentation.\n"
1167                "}");
1168   verifyFormat("void f() {\n"
1169                "label:\n"
1170                "  signals.baz();\n"
1171                "}");
1172 }
1173 
1174 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1175   EXPECT_EQ("class A {\n"
1176             "public:\n"
1177             "  void f();\n"
1178             "\n"
1179             "private:\n"
1180             "  void g() {}\n"
1181             "  // test\n"
1182             "protected:\n"
1183             "  int h;\n"
1184             "};",
1185             format("class A {\n"
1186                    "public:\n"
1187                    "void f();\n"
1188                    "private:\n"
1189                    "void g() {}\n"
1190                    "// test\n"
1191                    "protected:\n"
1192                    "int h;\n"
1193                    "};"));
1194   EXPECT_EQ("class A {\n"
1195             "protected:\n"
1196             "public:\n"
1197             "  void f();\n"
1198             "};",
1199             format("class A {\n"
1200                    "protected:\n"
1201                    "\n"
1202                    "public:\n"
1203                    "\n"
1204                    "  void f();\n"
1205                    "};"));
1206 
1207   // Even ensure proper spacing inside macros.
1208   EXPECT_EQ("#define B     \\\n"
1209             "  class A {   \\\n"
1210             "   protected: \\\n"
1211             "   public:    \\\n"
1212             "    void f(); \\\n"
1213             "  };",
1214             format("#define B     \\\n"
1215                    "  class A {   \\\n"
1216                    "   protected: \\\n"
1217                    "              \\\n"
1218                    "   public:    \\\n"
1219                    "              \\\n"
1220                    "    void f(); \\\n"
1221                    "  };",
1222                    getGoogleStyle()));
1223   // But don't remove empty lines after macros ending in access specifiers.
1224   EXPECT_EQ("#define A private:\n"
1225             "\n"
1226             "int i;",
1227             format("#define A         private:\n"
1228                    "\n"
1229                    "int              i;"));
1230 }
1231 
1232 TEST_F(FormatTest, FormatsClasses) {
1233   verifyFormat("class A : public B {};");
1234   verifyFormat("class A : public ::B {};");
1235 
1236   verifyFormat(
1237       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1238       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1239   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1240                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1241                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1242   verifyFormat(
1243       "class A : public B, public C, public D, public E, public F {};");
1244   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1245                "                     public C,\n"
1246                "                     public D,\n"
1247                "                     public E,\n"
1248                "                     public F,\n"
1249                "                     public G {};");
1250 
1251   verifyFormat("class\n"
1252                "    ReallyReallyLongClassName {\n"
1253                "  int i;\n"
1254                "};",
1255                getLLVMStyleWithColumns(32));
1256   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1257                "                           aaaaaaaaaaaaaaaa> {};");
1258   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1259                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1260                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1261   verifyFormat("template <class R, class C>\n"
1262                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1263                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1264   verifyFormat("class ::A::B {};");
1265 }
1266 
1267 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1268   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1269   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1270 
1271   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1272   verifyFormat("class MyClass\n"
1273                "    : public X\n"
1274                "    , public Y {};",
1275                StyleWithInheritanceBreak);
1276 }
1277 
1278 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1279   verifyFormat("class A {\n} a, b;");
1280   verifyFormat("struct A {\n} a, b;");
1281   verifyFormat("union A {\n} a;");
1282 }
1283 
1284 TEST_F(FormatTest, FormatsEnum) {
1285   verifyFormat("enum {\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   verifyGoogleFormat("enum {\n"
1294                      "  Zero,\n"
1295                      "  One = 1,\n"
1296                      "  Two = One + 1,\n"
1297                      "  Three = (One + Two),\n"
1298                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1299                      "  Five = (One, Two, Three, Four, 5)\n"
1300                      "};");
1301   verifyFormat("enum Enum {};");
1302   verifyFormat("enum {};");
1303   verifyFormat("enum X E {} d;");
1304   verifyFormat("enum __attribute__((...)) E {} d;");
1305   verifyFormat("enum __declspec__((...)) E {} d;");
1306   verifyFormat("enum {\n"
1307                "  Bar = Foo<int, int>::value\n"
1308                "};",
1309                getLLVMStyleWithColumns(30));
1310 
1311   verifyFormat("enum ShortEnum { A, B, C };");
1312   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1313 
1314   EXPECT_EQ("enum KeepEmptyLines {\n"
1315             "  ONE,\n"
1316             "\n"
1317             "  TWO,\n"
1318             "\n"
1319             "  THREE\n"
1320             "}",
1321             format("enum KeepEmptyLines {\n"
1322                    "  ONE,\n"
1323                    "\n"
1324                    "  TWO,\n"
1325                    "\n"
1326                    "\n"
1327                    "  THREE\n"
1328                    "}"));
1329   verifyFormat("enum E { // comment\n"
1330                "  ONE,\n"
1331                "  TWO\n"
1332                "};\n"
1333                "int i;");
1334   // Not enums.
1335   verifyFormat("enum X f() {\n"
1336                "  a();\n"
1337                "  return 42;\n"
1338                "}");
1339   verifyFormat("enum X Type::f() {\n"
1340                "  a();\n"
1341                "  return 42;\n"
1342                "}");
1343   verifyFormat("enum ::X f() {\n"
1344                "  a();\n"
1345                "  return 42;\n"
1346                "}");
1347   verifyFormat("enum ns::X f() {\n"
1348                "  a();\n"
1349                "  return 42;\n"
1350                "}");
1351 }
1352 
1353 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1354   verifyFormat("enum Type {\n"
1355                "  One = 0; // These semicolons should be commas.\n"
1356                "  Two = 1;\n"
1357                "};");
1358   verifyFormat("namespace n {\n"
1359                "enum Type {\n"
1360                "  One,\n"
1361                "  Two, // missing };\n"
1362                "  int i;\n"
1363                "}\n"
1364                "void g() {}");
1365 }
1366 
1367 TEST_F(FormatTest, FormatsEnumStruct) {
1368   verifyFormat("enum struct {\n"
1369                "  Zero,\n"
1370                "  One = 1,\n"
1371                "  Two = One + 1,\n"
1372                "  Three = (One + Two),\n"
1373                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1374                "  Five = (One, Two, Three, Four, 5)\n"
1375                "};");
1376   verifyFormat("enum struct Enum {};");
1377   verifyFormat("enum struct {};");
1378   verifyFormat("enum struct X E {} d;");
1379   verifyFormat("enum struct __attribute__((...)) E {} d;");
1380   verifyFormat("enum struct __declspec__((...)) E {} d;");
1381   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1382 }
1383 
1384 TEST_F(FormatTest, FormatsEnumClass) {
1385   verifyFormat("enum class {\n"
1386                "  Zero,\n"
1387                "  One = 1,\n"
1388                "  Two = One + 1,\n"
1389                "  Three = (One + Two),\n"
1390                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1391                "  Five = (One, Two, Three, Four, 5)\n"
1392                "};");
1393   verifyFormat("enum class Enum {};");
1394   verifyFormat("enum class {};");
1395   verifyFormat("enum class X E {} d;");
1396   verifyFormat("enum class __attribute__((...)) E {} d;");
1397   verifyFormat("enum class __declspec__((...)) E {} d;");
1398   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1399 }
1400 
1401 TEST_F(FormatTest, FormatsEnumTypes) {
1402   verifyFormat("enum X : int {\n"
1403                "  A, // Force multiple lines.\n"
1404                "  B\n"
1405                "};");
1406   verifyFormat("enum X : int { A, B };");
1407   verifyFormat("enum X : std::uint32_t { A, B };");
1408 }
1409 
1410 TEST_F(FormatTest, FormatsTypedefEnum) {
1411   FormatStyle Style = getLLVMStyle();
1412   Style.ColumnLimit = 40;
1413   verifyFormat("typedef enum {} EmptyEnum;");
1414   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1415   verifyFormat("typedef enum {\n"
1416                "  ZERO = 0,\n"
1417                "  ONE = 1,\n"
1418                "  TWO = 2,\n"
1419                "  THREE = 3\n"
1420                "} LongEnum;",
1421                Style);
1422   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1423   Style.BraceWrapping.AfterEnum = true;
1424   verifyFormat("typedef enum {} EmptyEnum;");
1425   verifyFormat("typedef enum { A, B, C } ShortEnum;");
1426   verifyFormat("typedef enum\n"
1427                "{\n"
1428                "  ZERO = 0,\n"
1429                "  ONE = 1,\n"
1430                "  TWO = 2,\n"
1431                "  THREE = 3\n"
1432                "} LongEnum;",
1433                Style);
1434 }
1435 
1436 TEST_F(FormatTest, FormatsNSEnums) {
1437   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1438   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1439                      "  // Information about someDecentlyLongValue.\n"
1440                      "  someDecentlyLongValue,\n"
1441                      "  // Information about anotherDecentlyLongValue.\n"
1442                      "  anotherDecentlyLongValue,\n"
1443                      "  // Information about aThirdDecentlyLongValue.\n"
1444                      "  aThirdDecentlyLongValue\n"
1445                      "};");
1446   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1447                      "  a = 1,\n"
1448                      "  b = 2,\n"
1449                      "  c = 3,\n"
1450                      "};");
1451   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1452                      "  a = 1,\n"
1453                      "  b = 2,\n"
1454                      "  c = 3,\n"
1455                      "};");
1456   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1457                      "  a = 1,\n"
1458                      "  b = 2,\n"
1459                      "  c = 3,\n"
1460                      "};");
1461 }
1462 
1463 TEST_F(FormatTest, FormatsBitfields) {
1464   verifyFormat("struct Bitfields {\n"
1465                "  unsigned sClass : 8;\n"
1466                "  unsigned ValueKind : 2;\n"
1467                "};");
1468   verifyFormat("struct A {\n"
1469                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1470                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1471                "};");
1472   verifyFormat("struct MyStruct {\n"
1473                "  uchar data;\n"
1474                "  uchar : 8;\n"
1475                "  uchar : 8;\n"
1476                "  uchar other;\n"
1477                "};");
1478 }
1479 
1480 TEST_F(FormatTest, FormatsNamespaces) {
1481   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1482   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1483 
1484   verifyFormat("namespace some_namespace {\n"
1485                "class A {};\n"
1486                "void f() { f(); }\n"
1487                "}",
1488                LLVMWithNoNamespaceFix);
1489   verifyFormat("namespace {\n"
1490                "class A {};\n"
1491                "void f() { f(); }\n"
1492                "}",
1493                LLVMWithNoNamespaceFix);
1494   verifyFormat("inline namespace X {\n"
1495                "class A {};\n"
1496                "void f() { f(); }\n"
1497                "}",
1498                LLVMWithNoNamespaceFix);
1499   verifyFormat("using namespace some_namespace;\n"
1500                "class A {};\n"
1501                "void f() { f(); }",
1502                LLVMWithNoNamespaceFix);
1503 
1504   // This code is more common than we thought; if we
1505   // layout this correctly the semicolon will go into
1506   // its own line, which is undesirable.
1507   verifyFormat("namespace {};",
1508                LLVMWithNoNamespaceFix);
1509   verifyFormat("namespace {\n"
1510                "class A {};\n"
1511                "};",
1512                LLVMWithNoNamespaceFix);
1513 
1514   verifyFormat("namespace {\n"
1515                "int SomeVariable = 0; // comment\n"
1516                "} // namespace",
1517                LLVMWithNoNamespaceFix);
1518   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1519             "#define HEADER_GUARD\n"
1520             "namespace my_namespace {\n"
1521             "int i;\n"
1522             "} // my_namespace\n"
1523             "#endif // HEADER_GUARD",
1524             format("#ifndef HEADER_GUARD\n"
1525                    " #define HEADER_GUARD\n"
1526                    "   namespace my_namespace {\n"
1527                    "int i;\n"
1528                    "}    // my_namespace\n"
1529                    "#endif    // HEADER_GUARD",
1530                    LLVMWithNoNamespaceFix));
1531 
1532   EXPECT_EQ("namespace A::B {\n"
1533             "class C {};\n"
1534             "}",
1535             format("namespace A::B {\n"
1536                    "class C {};\n"
1537                    "}",
1538                    LLVMWithNoNamespaceFix));
1539 
1540   FormatStyle Style = getLLVMStyle();
1541   Style.NamespaceIndentation = FormatStyle::NI_All;
1542   EXPECT_EQ("namespace out {\n"
1543             "  int i;\n"
1544             "  namespace in {\n"
1545             "    int i;\n"
1546             "  } // namespace in\n"
1547             "} // namespace out",
1548             format("namespace out {\n"
1549                    "int i;\n"
1550                    "namespace in {\n"
1551                    "int i;\n"
1552                    "} // namespace in\n"
1553                    "} // namespace out",
1554                    Style));
1555 
1556   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1557   EXPECT_EQ("namespace out {\n"
1558             "int i;\n"
1559             "namespace in {\n"
1560             "  int i;\n"
1561             "} // namespace in\n"
1562             "} // namespace out",
1563             format("namespace out {\n"
1564                    "int i;\n"
1565                    "namespace in {\n"
1566                    "int i;\n"
1567                    "} // namespace in\n"
1568                    "} // namespace out",
1569                    Style));
1570 }
1571 
1572 TEST_F(FormatTest, FormatsCompactNamespaces) {
1573   FormatStyle Style = getLLVMStyle();
1574   Style.CompactNamespaces = true;
1575 
1576   verifyFormat("namespace A { namespace B {\n"
1577 			   "}} // namespace A::B",
1578 			   Style);
1579 
1580   EXPECT_EQ("namespace out { namespace in {\n"
1581             "}} // namespace out::in",
1582             format("namespace out {\n"
1583                    "namespace in {\n"
1584                    "} // namespace in\n"
1585                    "} // namespace out",
1586                    Style));
1587 
1588   // Only namespaces which have both consecutive opening and end get compacted
1589   EXPECT_EQ("namespace out {\n"
1590             "namespace in1 {\n"
1591             "} // namespace in1\n"
1592             "namespace in2 {\n"
1593             "} // namespace in2\n"
1594             "} // namespace out",
1595             format("namespace out {\n"
1596                    "namespace in1 {\n"
1597                    "} // namespace in1\n"
1598                    "namespace in2 {\n"
1599                    "} // namespace in2\n"
1600                    "} // namespace out",
1601                    Style));
1602 
1603   EXPECT_EQ("namespace out {\n"
1604             "int i;\n"
1605             "namespace in {\n"
1606             "int j;\n"
1607             "} // namespace in\n"
1608             "int k;\n"
1609             "} // namespace out",
1610             format("namespace out { int i;\n"
1611                    "namespace in { int j; } // namespace in\n"
1612                    "int k; } // namespace out",
1613                    Style));
1614 
1615   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1616             "}}} // namespace A::B::C\n",
1617             format("namespace A { namespace B {\n"
1618                    "namespace C {\n"
1619                    "}} // namespace B::C\n"
1620                    "} // namespace A\n",
1621                    Style));
1622 
1623   Style.ColumnLimit = 40;
1624   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1625             "namespace bbbbbbbbbb {\n"
1626             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1627             format("namespace aaaaaaaaaa {\n"
1628                    "namespace bbbbbbbbbb {\n"
1629                    "} // namespace bbbbbbbbbb\n"
1630                    "} // namespace aaaaaaaaaa",
1631                    Style));
1632 
1633   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1634             "namespace cccccc {\n"
1635             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1636             format("namespace aaaaaa {\n"
1637                    "namespace bbbbbb {\n"
1638                    "namespace cccccc {\n"
1639                    "} // namespace cccccc\n"
1640                    "} // namespace bbbbbb\n"
1641                    "} // namespace aaaaaa",
1642                    Style));
1643   Style.ColumnLimit = 80;
1644 
1645   // Extra semicolon after 'inner' closing brace prevents merging
1646   EXPECT_EQ("namespace out { namespace in {\n"
1647             "}; } // namespace out::in",
1648             format("namespace out {\n"
1649                    "namespace in {\n"
1650                    "}; // namespace in\n"
1651                    "} // namespace out",
1652                    Style));
1653 
1654   // Extra semicolon after 'outer' closing brace is conserved
1655   EXPECT_EQ("namespace out { namespace in {\n"
1656             "}}; // namespace out::in",
1657             format("namespace out {\n"
1658                    "namespace in {\n"
1659                    "} // namespace in\n"
1660                    "}; // namespace out",
1661                    Style));
1662 
1663   Style.NamespaceIndentation = FormatStyle::NI_All;
1664   EXPECT_EQ("namespace out { namespace in {\n"
1665             "  int i;\n"
1666             "}} // namespace out::in",
1667             format("namespace out {\n"
1668                    "namespace in {\n"
1669                    "int i;\n"
1670                    "} // namespace in\n"
1671                    "} // namespace out",
1672                    Style));
1673   EXPECT_EQ("namespace out { namespace mid {\n"
1674             "  namespace in {\n"
1675             "    int j;\n"
1676             "  } // namespace in\n"
1677             "  int k;\n"
1678             "}} // namespace out::mid",
1679             format("namespace out { namespace mid {\n"
1680                    "namespace in { int j; } // namespace in\n"
1681                    "int k; }} // namespace out::mid",
1682                    Style));
1683 
1684   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1685   EXPECT_EQ("namespace out { namespace in {\n"
1686             "  int i;\n"
1687             "}} // namespace out::in",
1688             format("namespace out {\n"
1689                    "namespace in {\n"
1690                    "int i;\n"
1691                    "} // namespace in\n"
1692                    "} // namespace out",
1693                    Style));
1694   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1695             "  int i;\n"
1696             "}}} // namespace out::mid::in",
1697             format("namespace out {\n"
1698                    "namespace mid {\n"
1699                    "namespace in {\n"
1700                    "int i;\n"
1701                    "} // namespace in\n"
1702                    "} // namespace mid\n"
1703                    "} // namespace out",
1704                    Style));
1705 }
1706 
1707 TEST_F(FormatTest, FormatsExternC) {
1708   verifyFormat("extern \"C\" {\nint a;");
1709   verifyFormat("extern \"C\" {}");
1710   verifyFormat("extern \"C\" {\n"
1711                "int foo();\n"
1712                "}");
1713   verifyFormat("extern \"C\" int foo() {}");
1714   verifyFormat("extern \"C\" int foo();");
1715   verifyFormat("extern \"C\" int foo() {\n"
1716                "  int i = 42;\n"
1717                "  return i;\n"
1718                "}");
1719 
1720   FormatStyle Style = getLLVMStyle();
1721   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1722   Style.BraceWrapping.AfterFunction = true;
1723   verifyFormat("extern \"C\" int foo() {}", Style);
1724   verifyFormat("extern \"C\" int foo();", Style);
1725   verifyFormat("extern \"C\" int foo()\n"
1726                "{\n"
1727                "  int i = 42;\n"
1728                "  return i;\n"
1729                "}",
1730                Style);
1731 
1732   Style.BraceWrapping.AfterExternBlock = true;
1733   Style.BraceWrapping.SplitEmptyRecord = false;
1734   verifyFormat("extern \"C\"\n"
1735                "{}",
1736                Style);
1737   verifyFormat("extern \"C\"\n"
1738                "{\n"
1739                "  int foo();\n"
1740                "}",
1741                Style);
1742 }
1743 
1744 TEST_F(FormatTest, FormatsInlineASM) {
1745   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1746   verifyFormat("asm(\"nop\" ::: \"memory\");");
1747   verifyFormat(
1748       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1749       "    \"cpuid\\n\\t\"\n"
1750       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1751       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1752       "    : \"a\"(value));");
1753   EXPECT_EQ(
1754       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1755       "  __asm {\n"
1756       "        mov     edx,[that] // vtable in edx\n"
1757       "        mov     eax,methodIndex\n"
1758       "        call    [edx][eax*4] // stdcall\n"
1759       "  }\n"
1760       "}",
1761       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1762              "    __asm {\n"
1763              "        mov     edx,[that] // vtable in edx\n"
1764              "        mov     eax,methodIndex\n"
1765              "        call    [edx][eax*4] // stdcall\n"
1766              "    }\n"
1767              "}"));
1768   EXPECT_EQ("_asm {\n"
1769             "  xor eax, eax;\n"
1770             "  cpuid;\n"
1771             "}",
1772             format("_asm {\n"
1773                    "  xor eax, eax;\n"
1774                    "  cpuid;\n"
1775                    "}"));
1776   verifyFormat("void function() {\n"
1777                "  // comment\n"
1778                "  asm(\"\");\n"
1779                "}");
1780   EXPECT_EQ("__asm {\n"
1781             "}\n"
1782             "int i;",
1783             format("__asm   {\n"
1784                    "}\n"
1785                    "int   i;"));
1786 }
1787 
1788 TEST_F(FormatTest, FormatTryCatch) {
1789   verifyFormat("try {\n"
1790                "  throw a * b;\n"
1791                "} catch (int a) {\n"
1792                "  // Do nothing.\n"
1793                "} catch (...) {\n"
1794                "  exit(42);\n"
1795                "}");
1796 
1797   // Function-level try statements.
1798   verifyFormat("int f() try { return 4; } catch (...) {\n"
1799                "  return 5;\n"
1800                "}");
1801   verifyFormat("class A {\n"
1802                "  int a;\n"
1803                "  A() try : a(0) {\n"
1804                "  } catch (...) {\n"
1805                "    throw;\n"
1806                "  }\n"
1807                "};\n");
1808 
1809   // Incomplete try-catch blocks.
1810   verifyIncompleteFormat("try {} catch (");
1811 }
1812 
1813 TEST_F(FormatTest, FormatSEHTryCatch) {
1814   verifyFormat("__try {\n"
1815                "  int a = b * c;\n"
1816                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1817                "  // Do nothing.\n"
1818                "}");
1819 
1820   verifyFormat("__try {\n"
1821                "  int a = b * c;\n"
1822                "} __finally {\n"
1823                "  // Do nothing.\n"
1824                "}");
1825 
1826   verifyFormat("DEBUG({\n"
1827                "  __try {\n"
1828                "  } __finally {\n"
1829                "  }\n"
1830                "});\n");
1831 }
1832 
1833 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1834   verifyFormat("try {\n"
1835                "  f();\n"
1836                "} catch {\n"
1837                "  g();\n"
1838                "}");
1839   verifyFormat("try {\n"
1840                "  f();\n"
1841                "} catch (A a) MACRO(x) {\n"
1842                "  g();\n"
1843                "} catch (B b) MACRO(x) {\n"
1844                "  g();\n"
1845                "}");
1846 }
1847 
1848 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1849   FormatStyle Style = getLLVMStyle();
1850   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1851                           FormatStyle::BS_WebKit}) {
1852     Style.BreakBeforeBraces = BraceStyle;
1853     verifyFormat("try {\n"
1854                  "  // something\n"
1855                  "} catch (...) {\n"
1856                  "  // something\n"
1857                  "}",
1858                  Style);
1859   }
1860   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1861   verifyFormat("try {\n"
1862                "  // something\n"
1863                "}\n"
1864                "catch (...) {\n"
1865                "  // something\n"
1866                "}",
1867                Style);
1868   verifyFormat("__try {\n"
1869                "  // something\n"
1870                "}\n"
1871                "__finally {\n"
1872                "  // something\n"
1873                "}",
1874                Style);
1875   verifyFormat("@try {\n"
1876                "  // something\n"
1877                "}\n"
1878                "@finally {\n"
1879                "  // something\n"
1880                "}",
1881                Style);
1882   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1883   verifyFormat("try\n"
1884                "{\n"
1885                "  // something\n"
1886                "}\n"
1887                "catch (...)\n"
1888                "{\n"
1889                "  // something\n"
1890                "}",
1891                Style);
1892   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1893   verifyFormat("try\n"
1894                "  {\n"
1895                "    // something\n"
1896                "  }\n"
1897                "catch (...)\n"
1898                "  {\n"
1899                "    // something\n"
1900                "  }",
1901                Style);
1902   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1903   Style.BraceWrapping.BeforeCatch = true;
1904   verifyFormat("try {\n"
1905                "  // something\n"
1906                "}\n"
1907                "catch (...) {\n"
1908                "  // something\n"
1909                "}",
1910                Style);
1911 }
1912 
1913 TEST_F(FormatTest, StaticInitializers) {
1914   verifyFormat("static SomeClass SC = {1, 'a'};");
1915 
1916   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1917                "    100000000, "
1918                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1919 
1920   // Here, everything other than the "}" would fit on a line.
1921   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1922                "    10000000000000000000000000};");
1923   EXPECT_EQ("S s = {a,\n"
1924             "\n"
1925             "       b};",
1926             format("S s = {\n"
1927                    "  a,\n"
1928                    "\n"
1929                    "  b\n"
1930                    "};"));
1931 
1932   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1933   // line. However, the formatting looks a bit off and this probably doesn't
1934   // happen often in practice.
1935   verifyFormat("static int Variable[1] = {\n"
1936                "    {1000000000000000000000000000000000000}};",
1937                getLLVMStyleWithColumns(40));
1938 }
1939 
1940 TEST_F(FormatTest, DesignatedInitializers) {
1941   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1942   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1943                "                    .bbbbbbbbbb = 2,\n"
1944                "                    .cccccccccc = 3,\n"
1945                "                    .dddddddddd = 4,\n"
1946                "                    .eeeeeeeeee = 5};");
1947   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1948                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1949                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1950                "    .ccccccccccccccccccccccccccc = 3,\n"
1951                "    .ddddddddddddddddddddddddddd = 4,\n"
1952                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1953 
1954   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1955 
1956   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1957   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1958                "                    [2] = bbbbbbbbbb,\n"
1959                "                    [3] = cccccccccc,\n"
1960                "                    [4] = dddddddddd,\n"
1961                "                    [5] = eeeeeeeeee};");
1962   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1963                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1964                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1965                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1966                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1967                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1968 }
1969 
1970 TEST_F(FormatTest, NestedStaticInitializers) {
1971   verifyFormat("static A x = {{{}}};\n");
1972   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1973                "               {init1, init2, init3, init4}}};",
1974                getLLVMStyleWithColumns(50));
1975 
1976   verifyFormat("somes Status::global_reps[3] = {\n"
1977                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1978                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1979                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1980                getLLVMStyleWithColumns(60));
1981   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1982                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1983                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1984                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1985   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1986                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1987                "rect.fTop}};");
1988 
1989   verifyFormat(
1990       "SomeArrayOfSomeType a = {\n"
1991       "    {{1, 2, 3},\n"
1992       "     {1, 2, 3},\n"
1993       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1994       "      333333333333333333333333333333},\n"
1995       "     {1, 2, 3},\n"
1996       "     {1, 2, 3}}};");
1997   verifyFormat(
1998       "SomeArrayOfSomeType a = {\n"
1999       "    {{1, 2, 3}},\n"
2000       "    {{1, 2, 3}},\n"
2001       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2002       "      333333333333333333333333333333}},\n"
2003       "    {{1, 2, 3}},\n"
2004       "    {{1, 2, 3}}};");
2005 
2006   verifyFormat("struct {\n"
2007                "  unsigned bit;\n"
2008                "  const char *const name;\n"
2009                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2010                "                 {kOsWin, \"Windows\"},\n"
2011                "                 {kOsLinux, \"Linux\"},\n"
2012                "                 {kOsCrOS, \"Chrome OS\"}};");
2013   verifyFormat("struct {\n"
2014                "  unsigned bit;\n"
2015                "  const char *const name;\n"
2016                "} kBitsToOs[] = {\n"
2017                "    {kOsMac, \"Mac\"},\n"
2018                "    {kOsWin, \"Windows\"},\n"
2019                "    {kOsLinux, \"Linux\"},\n"
2020                "    {kOsCrOS, \"Chrome OS\"},\n"
2021                "};");
2022 }
2023 
2024 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2025   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2026                "                      \\\n"
2027                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2028 }
2029 
2030 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2031   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2032                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2033 
2034   // Do break defaulted and deleted functions.
2035   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2036                "    default;",
2037                getLLVMStyleWithColumns(40));
2038   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2039                "    delete;",
2040                getLLVMStyleWithColumns(40));
2041 }
2042 
2043 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2044   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2045                getLLVMStyleWithColumns(40));
2046   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2047                getLLVMStyleWithColumns(40));
2048   EXPECT_EQ("#define Q                              \\\n"
2049             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2050             "  \"aaaaaaaa.cpp\"",
2051             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2052                    getLLVMStyleWithColumns(40)));
2053 }
2054 
2055 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2056   EXPECT_EQ("# 123 \"A string literal\"",
2057             format("   #     123    \"A string literal\""));
2058 }
2059 
2060 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2061   EXPECT_EQ("#;", format("#;"));
2062   verifyFormat("#\n;\n;\n;");
2063 }
2064 
2065 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2066   EXPECT_EQ("#line 42 \"test\"\n",
2067             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2068   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2069                                     getLLVMStyleWithColumns(12)));
2070 }
2071 
2072 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2073   EXPECT_EQ("#line 42 \"test\"",
2074             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2075   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2076 }
2077 
2078 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2079   verifyFormat("#define A \\x20");
2080   verifyFormat("#define A \\ x20");
2081   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2082   verifyFormat("#define A ''");
2083   verifyFormat("#define A ''qqq");
2084   verifyFormat("#define A `qqq");
2085   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2086   EXPECT_EQ("const char *c = STRINGIFY(\n"
2087             "\\na : b);",
2088             format("const char * c = STRINGIFY(\n"
2089                    "\\na : b);"));
2090 
2091   verifyFormat("a\r\\");
2092   verifyFormat("a\v\\");
2093   verifyFormat("a\f\\");
2094 }
2095 
2096 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2097   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2098   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2099   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2100   // FIXME: We never break before the macro name.
2101   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2102 
2103   verifyFormat("#define A A\n#define A A");
2104   verifyFormat("#define A(X) A\n#define A A");
2105 
2106   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2107   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2108 }
2109 
2110 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2111   EXPECT_EQ("// somecomment\n"
2112             "#include \"a.h\"\n"
2113             "#define A(  \\\n"
2114             "    A, B)\n"
2115             "#include \"b.h\"\n"
2116             "// somecomment\n",
2117             format("  // somecomment\n"
2118                    "  #include \"a.h\"\n"
2119                    "#define A(A,\\\n"
2120                    "    B)\n"
2121                    "    #include \"b.h\"\n"
2122                    " // somecomment\n",
2123                    getLLVMStyleWithColumns(13)));
2124 }
2125 
2126 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2127 
2128 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2129   EXPECT_EQ("#define A    \\\n"
2130             "  c;         \\\n"
2131             "  e;\n"
2132             "f;",
2133             format("#define A c; e;\n"
2134                    "f;",
2135                    getLLVMStyleWithColumns(14)));
2136 }
2137 
2138 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2139 
2140 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2141   EXPECT_EQ("int x,\n"
2142             "#define A\n"
2143             "    y;",
2144             format("int x,\n#define A\ny;"));
2145 }
2146 
2147 TEST_F(FormatTest, HashInMacroDefinition) {
2148   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2149   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2150   verifyFormat("#define A  \\\n"
2151                "  {        \\\n"
2152                "    f(#c); \\\n"
2153                "  }",
2154                getLLVMStyleWithColumns(11));
2155 
2156   verifyFormat("#define A(X)         \\\n"
2157                "  void function##X()",
2158                getLLVMStyleWithColumns(22));
2159 
2160   verifyFormat("#define A(a, b, c)   \\\n"
2161                "  void a##b##c()",
2162                getLLVMStyleWithColumns(22));
2163 
2164   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2165 }
2166 
2167 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2168   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2169   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2170 }
2171 
2172 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2173   EXPECT_EQ("#define A b;", format("#define A \\\n"
2174                                    "          \\\n"
2175                                    "  b;",
2176                                    getLLVMStyleWithColumns(25)));
2177   EXPECT_EQ("#define A \\\n"
2178             "          \\\n"
2179             "  a;      \\\n"
2180             "  b;",
2181             format("#define A \\\n"
2182                    "          \\\n"
2183                    "  a;      \\\n"
2184                    "  b;",
2185                    getLLVMStyleWithColumns(11)));
2186   EXPECT_EQ("#define A \\\n"
2187             "  a;      \\\n"
2188             "          \\\n"
2189             "  b;",
2190             format("#define A \\\n"
2191                    "  a;      \\\n"
2192                    "          \\\n"
2193                    "  b;",
2194                    getLLVMStyleWithColumns(11)));
2195 }
2196 
2197 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2198   verifyIncompleteFormat("#define A :");
2199   verifyFormat("#define SOMECASES  \\\n"
2200                "  case 1:          \\\n"
2201                "  case 2\n",
2202                getLLVMStyleWithColumns(20));
2203   verifyFormat("#define MACRO(a) \\\n"
2204                "  if (a)         \\\n"
2205                "    f();         \\\n"
2206                "  else           \\\n"
2207                "    g()",
2208                getLLVMStyleWithColumns(18));
2209   verifyFormat("#define A template <typename T>");
2210   verifyIncompleteFormat("#define STR(x) #x\n"
2211                          "f(STR(this_is_a_string_literal{));");
2212   verifyFormat("#pragma omp threadprivate( \\\n"
2213                "    y)), // expected-warning",
2214                getLLVMStyleWithColumns(28));
2215   verifyFormat("#d, = };");
2216   verifyFormat("#if \"a");
2217   verifyIncompleteFormat("({\n"
2218                          "#define b     \\\n"
2219                          "  }           \\\n"
2220                          "  a\n"
2221                          "a",
2222                          getLLVMStyleWithColumns(15));
2223   verifyFormat("#define A     \\\n"
2224                "  {           \\\n"
2225                "    {\n"
2226                "#define B     \\\n"
2227                "  }           \\\n"
2228                "  }",
2229                getLLVMStyleWithColumns(15));
2230   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2231   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2232   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2233   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2234 }
2235 
2236 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2237   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2238   EXPECT_EQ("class A : public QObject {\n"
2239             "  Q_OBJECT\n"
2240             "\n"
2241             "  A() {}\n"
2242             "};",
2243             format("class A  :  public QObject {\n"
2244                    "     Q_OBJECT\n"
2245                    "\n"
2246                    "  A() {\n}\n"
2247                    "}  ;"));
2248   EXPECT_EQ("MACRO\n"
2249             "/*static*/ int i;",
2250             format("MACRO\n"
2251                    " /*static*/ int   i;"));
2252   EXPECT_EQ("SOME_MACRO\n"
2253             "namespace {\n"
2254             "void f();\n"
2255             "} // namespace",
2256             format("SOME_MACRO\n"
2257                    "  namespace    {\n"
2258                    "void   f(  );\n"
2259                    "} // namespace"));
2260   // Only if the identifier contains at least 5 characters.
2261   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2262   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2263   // Only if everything is upper case.
2264   EXPECT_EQ("class A : public QObject {\n"
2265             "  Q_Object A() {}\n"
2266             "};",
2267             format("class A  :  public QObject {\n"
2268                    "     Q_Object\n"
2269                    "  A() {\n}\n"
2270                    "}  ;"));
2271 
2272   // Only if the next line can actually start an unwrapped line.
2273   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2274             format("SOME_WEIRD_LOG_MACRO\n"
2275                    "<< SomeThing;"));
2276 
2277   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2278                "(n, buffers))\n",
2279                getChromiumStyle(FormatStyle::LK_Cpp));
2280 }
2281 
2282 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2283   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2284             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2285             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2286             "class X {};\n"
2287             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2288             "int *createScopDetectionPass() { return 0; }",
2289             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2290                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2291                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2292                    "  class X {};\n"
2293                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2294                    "  int *createScopDetectionPass() { return 0; }"));
2295   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2296   // braces, so that inner block is indented one level more.
2297   EXPECT_EQ("int q() {\n"
2298             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2299             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2300             "  IPC_END_MESSAGE_MAP()\n"
2301             "}",
2302             format("int q() {\n"
2303                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2304                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2305                    "  IPC_END_MESSAGE_MAP()\n"
2306                    "}"));
2307 
2308   // Same inside macros.
2309   EXPECT_EQ("#define LIST(L) \\\n"
2310             "  L(A)          \\\n"
2311             "  L(B)          \\\n"
2312             "  L(C)",
2313             format("#define LIST(L) \\\n"
2314                    "  L(A) \\\n"
2315                    "  L(B) \\\n"
2316                    "  L(C)",
2317                    getGoogleStyle()));
2318 
2319   // These must not be recognized as macros.
2320   EXPECT_EQ("int q() {\n"
2321             "  f(x);\n"
2322             "  f(x) {}\n"
2323             "  f(x)->g();\n"
2324             "  f(x)->*g();\n"
2325             "  f(x).g();\n"
2326             "  f(x) = x;\n"
2327             "  f(x) += x;\n"
2328             "  f(x) -= x;\n"
2329             "  f(x) *= x;\n"
2330             "  f(x) /= x;\n"
2331             "  f(x) %= x;\n"
2332             "  f(x) &= x;\n"
2333             "  f(x) |= x;\n"
2334             "  f(x) ^= x;\n"
2335             "  f(x) >>= x;\n"
2336             "  f(x) <<= x;\n"
2337             "  f(x)[y].z();\n"
2338             "  LOG(INFO) << x;\n"
2339             "  ifstream(x) >> x;\n"
2340             "}\n",
2341             format("int q() {\n"
2342                    "  f(x)\n;\n"
2343                    "  f(x)\n {}\n"
2344                    "  f(x)\n->g();\n"
2345                    "  f(x)\n->*g();\n"
2346                    "  f(x)\n.g();\n"
2347                    "  f(x)\n = x;\n"
2348                    "  f(x)\n += x;\n"
2349                    "  f(x)\n -= x;\n"
2350                    "  f(x)\n *= x;\n"
2351                    "  f(x)\n /= x;\n"
2352                    "  f(x)\n %= x;\n"
2353                    "  f(x)\n &= x;\n"
2354                    "  f(x)\n |= x;\n"
2355                    "  f(x)\n ^= x;\n"
2356                    "  f(x)\n >>= x;\n"
2357                    "  f(x)\n <<= x;\n"
2358                    "  f(x)\n[y].z();\n"
2359                    "  LOG(INFO)\n << x;\n"
2360                    "  ifstream(x)\n >> x;\n"
2361                    "}\n"));
2362   EXPECT_EQ("int q() {\n"
2363             "  F(x)\n"
2364             "  if (1) {\n"
2365             "  }\n"
2366             "  F(x)\n"
2367             "  while (1) {\n"
2368             "  }\n"
2369             "  F(x)\n"
2370             "  G(x);\n"
2371             "  F(x)\n"
2372             "  try {\n"
2373             "    Q();\n"
2374             "  } catch (...) {\n"
2375             "  }\n"
2376             "}\n",
2377             format("int q() {\n"
2378                    "F(x)\n"
2379                    "if (1) {}\n"
2380                    "F(x)\n"
2381                    "while (1) {}\n"
2382                    "F(x)\n"
2383                    "G(x);\n"
2384                    "F(x)\n"
2385                    "try { Q(); } catch (...) {}\n"
2386                    "}\n"));
2387   EXPECT_EQ("class A {\n"
2388             "  A() : t(0) {}\n"
2389             "  A(int i) noexcept() : {}\n"
2390             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2391             "  try : t(0) {\n"
2392             "  } catch (...) {\n"
2393             "  }\n"
2394             "};",
2395             format("class A {\n"
2396                    "  A()\n : t(0) {}\n"
2397                    "  A(int i)\n noexcept() : {}\n"
2398                    "  A(X x)\n"
2399                    "  try : t(0) {} catch (...) {}\n"
2400                    "};"));
2401   EXPECT_EQ("class SomeClass {\n"
2402             "public:\n"
2403             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2404             "};",
2405             format("class SomeClass {\n"
2406                    "public:\n"
2407                    "  SomeClass()\n"
2408                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2409                    "};"));
2410   EXPECT_EQ("class SomeClass {\n"
2411             "public:\n"
2412             "  SomeClass()\n"
2413             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2414             "};",
2415             format("class SomeClass {\n"
2416                    "public:\n"
2417                    "  SomeClass()\n"
2418                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2419                    "};",
2420                    getLLVMStyleWithColumns(40)));
2421 
2422   verifyFormat("MACRO(>)");
2423 }
2424 
2425 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2426   verifyFormat("#define A \\\n"
2427                "  f({     \\\n"
2428                "    g();  \\\n"
2429                "  });",
2430                getLLVMStyleWithColumns(11));
2431 }
2432 
2433 TEST_F(FormatTest, IndentPreprocessorDirectives) {
2434   FormatStyle Style = getLLVMStyle();
2435   Style.IndentPPDirectives = FormatStyle::PPDIS_None;
2436   Style.ColumnLimit = 40;
2437   verifyFormat("#ifdef _WIN32\n"
2438                "#define A 0\n"
2439                "#ifdef VAR2\n"
2440                "#define B 1\n"
2441                "#include <someheader.h>\n"
2442                "#define MACRO                          \\\n"
2443                "  some_very_long_func_aaaaaaaaaa();\n"
2444                "#endif\n"
2445                "#else\n"
2446                "#define A 1\n"
2447                "#endif",
2448                Style);
2449   Style.IndentPPDirectives = FormatStyle::PPDIS_AfterHash;
2450   verifyFormat("#ifdef _WIN32\n"
2451                "#  define A 0\n"
2452                "#  ifdef VAR2\n"
2453                "#    define B 1\n"
2454                "#    include <someheader.h>\n"
2455                "#    define MACRO                      \\\n"
2456                "      some_very_long_func_aaaaaaaaaa();\n"
2457                "#  endif\n"
2458                "#else\n"
2459                "#  define A 1\n"
2460                "#endif",
2461                Style);
2462   verifyFormat("#if A\n"
2463                "#  define MACRO                        \\\n"
2464                "    void a(int x) {                    \\\n"
2465                "      b();                             \\\n"
2466                "      c();                             \\\n"
2467                "      d();                             \\\n"
2468                "      e();                             \\\n"
2469                "      f();                             \\\n"
2470                "    }\n"
2471                "#endif",
2472                Style);
2473   // Comments before include guard.
2474   verifyFormat("// file comment\n"
2475                "// file comment\n"
2476                "#ifndef HEADER_H\n"
2477                "#define HEADER_H\n"
2478                "code();\n"
2479                "#endif",
2480                Style);
2481   // Test with include guards.
2482   // EXPECT_EQ is used because verifyFormat() calls messUp() which incorrectly
2483   // merges lines.
2484   verifyFormat("#ifndef HEADER_H\n"
2485                "#define HEADER_H\n"
2486                "code();\n"
2487                "#endif",
2488                Style);
2489   // Include guards must have a #define with the same variable immediately
2490   // after #ifndef.
2491   verifyFormat("#ifndef NOT_GUARD\n"
2492                "#  define FOO\n"
2493                "code();\n"
2494                "#endif",
2495                Style);
2496 
2497   // Include guards must cover the entire file.
2498   verifyFormat("code();\n"
2499                "code();\n"
2500                "#ifndef NOT_GUARD\n"
2501                "#  define NOT_GUARD\n"
2502                "code();\n"
2503                "#endif",
2504                Style);
2505   verifyFormat("#ifndef NOT_GUARD\n"
2506                "#  define NOT_GUARD\n"
2507                "code();\n"
2508                "#endif\n"
2509                "code();",
2510                Style);
2511   // Test with trailing blank lines.
2512   verifyFormat("#ifndef HEADER_H\n"
2513                "#define HEADER_H\n"
2514                "code();\n"
2515                "#endif\n",
2516                Style);
2517   // Include guards don't have #else.
2518   verifyFormat("#ifndef NOT_GUARD\n"
2519                "#  define NOT_GUARD\n"
2520                "code();\n"
2521                "#else\n"
2522                "#endif",
2523                Style);
2524   verifyFormat("#ifndef NOT_GUARD\n"
2525                "#  define NOT_GUARD\n"
2526                "code();\n"
2527                "#elif FOO\n"
2528                "#endif",
2529                Style);
2530   // FIXME: This doesn't handle the case where there's code between the
2531   // #ifndef and #define but all other conditions hold. This is because when
2532   // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the
2533   // previous code line yet, so we can't detect it.
2534   EXPECT_EQ("#ifndef NOT_GUARD\n"
2535             "code();\n"
2536             "#define NOT_GUARD\n"
2537             "code();\n"
2538             "#endif",
2539             format("#ifndef NOT_GUARD\n"
2540                    "code();\n"
2541                    "#  define NOT_GUARD\n"
2542                    "code();\n"
2543                    "#endif",
2544                    Style));
2545   // FIXME: This doesn't handle cases where legitimate preprocessor lines may
2546   // be outside an include guard. Examples are #pragma once and
2547   // #pragma GCC diagnostic, or anything else that does not change the meaning
2548   // of the file if it's included multiple times.
2549   EXPECT_EQ("#ifdef WIN32\n"
2550             "#  pragma once\n"
2551             "#endif\n"
2552             "#ifndef HEADER_H\n"
2553             "#  define HEADER_H\n"
2554             "code();\n"
2555             "#endif",
2556             format("#ifdef WIN32\n"
2557                    "#  pragma once\n"
2558                    "#endif\n"
2559                    "#ifndef HEADER_H\n"
2560                    "#define HEADER_H\n"
2561                    "code();\n"
2562                    "#endif",
2563                    Style));
2564   // FIXME: This does not detect when there is a single non-preprocessor line
2565   // in front of an include-guard-like structure where other conditions hold
2566   // because ScopedLineState hides the line.
2567   EXPECT_EQ("code();\n"
2568             "#ifndef HEADER_H\n"
2569             "#define HEADER_H\n"
2570             "code();\n"
2571             "#endif",
2572             format("code();\n"
2573                    "#ifndef HEADER_H\n"
2574                    "#  define HEADER_H\n"
2575                    "code();\n"
2576                    "#endif",
2577                    Style));
2578   // FIXME: The comment indent corrector in TokenAnnotator gets thrown off by
2579   // preprocessor indentation.
2580   EXPECT_EQ("#if 1\n"
2581             "  // comment\n"
2582             "#  define A 0\n"
2583             "// comment\n"
2584             "#  define B 0\n"
2585             "#endif",
2586             format("#if 1\n"
2587                    "// comment\n"
2588                    "#  define A 0\n"
2589                    "   // comment\n"
2590                    "#  define B 0\n"
2591                    "#endif",
2592                    Style));
2593   // Test with tabs.
2594   Style.UseTab = FormatStyle::UT_Always;
2595   Style.IndentWidth = 8;
2596   Style.TabWidth = 8;
2597   verifyFormat("#ifdef _WIN32\n"
2598                "#\tdefine A 0\n"
2599                "#\tifdef VAR2\n"
2600                "#\t\tdefine B 1\n"
2601                "#\t\tinclude <someheader.h>\n"
2602                "#\t\tdefine MACRO          \\\n"
2603                "\t\t\tsome_very_long_func_aaaaaaaaaa();\n"
2604                "#\tendif\n"
2605                "#else\n"
2606                "#\tdefine A 1\n"
2607                "#endif",
2608                Style);
2609 
2610   // Regression test: Multiline-macro inside include guards.
2611   verifyFormat("#ifndef HEADER_H\n"
2612                "#define HEADER_H\n"
2613                "#define A()        \\\n"
2614                "  int i;           \\\n"
2615                "  int j;\n"
2616                "#endif // HEADER_H",
2617                getLLVMStyleWithColumns(20));
2618 }
2619 
2620 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2621   verifyFormat("{\n  { a #c; }\n}");
2622 }
2623 
2624 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2625   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2626             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2627   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2628             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2629 }
2630 
2631 TEST_F(FormatTest, EscapedNewlines) {
2632   FormatStyle Narrow = getLLVMStyleWithColumns(11);
2633   EXPECT_EQ("#define A \\\n  int i;  \\\n  int j;",
2634             format("#define A \\\nint i;\\\n  int j;", Narrow));
2635   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2636   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2637   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2638   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2639 
2640   FormatStyle AlignLeft = getLLVMStyle();
2641   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
2642   EXPECT_EQ("#define MACRO(x) \\\n"
2643             "private:         \\\n"
2644             "  int x(int a);\n",
2645             format("#define MACRO(x) \\\n"
2646                    "private:         \\\n"
2647                    "  int x(int a);\n",
2648                    AlignLeft));
2649 
2650   // CRLF line endings
2651   EXPECT_EQ("#define A \\\r\n  int i;  \\\r\n  int j;",
2652             format("#define A \\\r\nint i;\\\r\n  int j;", Narrow));
2653   EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
2654   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2655   EXPECT_EQ("/* \\  \\  \\\r\n */", format("\\\r\n/* \\  \\  \\\r\n */"));
2656   EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
2657   EXPECT_EQ("#define MACRO(x) \\\r\n"
2658             "private:         \\\r\n"
2659             "  int x(int a);\r\n",
2660             format("#define MACRO(x) \\\r\n"
2661                    "private:         \\\r\n"
2662                    "  int x(int a);\r\n",
2663                    AlignLeft));
2664 
2665   FormatStyle DontAlign = getLLVMStyle();
2666   DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
2667   DontAlign.MaxEmptyLinesToKeep = 3;
2668   // FIXME: can't use verifyFormat here because the newline before
2669   // "public:" is not inserted the first time it's reformatted
2670   EXPECT_EQ("#define A \\\n"
2671             "  class Foo { \\\n"
2672             "    void bar(); \\\n"
2673             "\\\n"
2674             "\\\n"
2675             "\\\n"
2676             "  public: \\\n"
2677             "    void baz(); \\\n"
2678             "  };",
2679             format("#define A \\\n"
2680                    "  class Foo { \\\n"
2681                    "    void bar(); \\\n"
2682                    "\\\n"
2683                    "\\\n"
2684                    "\\\n"
2685                    "  public: \\\n"
2686                    "    void baz(); \\\n"
2687                    "  };",
2688                    DontAlign));
2689 }
2690 
2691 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2692   verifyFormat("#define A \\\n"
2693                "  int v(  \\\n"
2694                "      a); \\\n"
2695                "  int i;",
2696                getLLVMStyleWithColumns(11));
2697 }
2698 
2699 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2700   EXPECT_EQ(
2701       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2702       "                      \\\n"
2703       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2704       "\n"
2705       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2706       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2707       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2708              "\\\n"
2709              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2710              "  \n"
2711              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2712              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2713 }
2714 
2715 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2716   EXPECT_EQ("int\n"
2717             "#define A\n"
2718             "    a;",
2719             format("int\n#define A\na;"));
2720   verifyFormat("functionCallTo(\n"
2721                "    someOtherFunction(\n"
2722                "        withSomeParameters, whichInSequence,\n"
2723                "        areLongerThanALine(andAnotherCall,\n"
2724                "#define A B\n"
2725                "                           withMoreParamters,\n"
2726                "                           whichStronglyInfluenceTheLayout),\n"
2727                "        andMoreParameters),\n"
2728                "    trailing);",
2729                getLLVMStyleWithColumns(69));
2730   verifyFormat("Foo::Foo()\n"
2731                "#ifdef BAR\n"
2732                "    : baz(0)\n"
2733                "#endif\n"
2734                "{\n"
2735                "}");
2736   verifyFormat("void f() {\n"
2737                "  if (true)\n"
2738                "#ifdef A\n"
2739                "    f(42);\n"
2740                "  x();\n"
2741                "#else\n"
2742                "    g();\n"
2743                "  x();\n"
2744                "#endif\n"
2745                "}");
2746   verifyFormat("void f(param1, param2,\n"
2747                "       param3,\n"
2748                "#ifdef A\n"
2749                "       param4(param5,\n"
2750                "#ifdef A1\n"
2751                "              param6,\n"
2752                "#ifdef A2\n"
2753                "              param7),\n"
2754                "#else\n"
2755                "              param8),\n"
2756                "       param9,\n"
2757                "#endif\n"
2758                "       param10,\n"
2759                "#endif\n"
2760                "       param11)\n"
2761                "#else\n"
2762                "       param12)\n"
2763                "#endif\n"
2764                "{\n"
2765                "  x();\n"
2766                "}",
2767                getLLVMStyleWithColumns(28));
2768   verifyFormat("#if 1\n"
2769                "int i;");
2770   verifyFormat("#if 1\n"
2771                "#endif\n"
2772                "#if 1\n"
2773                "#else\n"
2774                "#endif\n");
2775   verifyFormat("DEBUG({\n"
2776                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2777                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2778                "});\n"
2779                "#if a\n"
2780                "#else\n"
2781                "#endif");
2782 
2783   verifyIncompleteFormat("void f(\n"
2784                          "#if A\n"
2785                          ");\n"
2786                          "#else\n"
2787                          "#endif");
2788 }
2789 
2790 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2791   verifyFormat("#endif\n"
2792                "#if B");
2793 }
2794 
2795 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2796   FormatStyle SingleLine = getLLVMStyle();
2797   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2798   verifyFormat("#if 0\n"
2799                "#elif 1\n"
2800                "#endif\n"
2801                "void foo() {\n"
2802                "  if (test) foo2();\n"
2803                "}",
2804                SingleLine);
2805 }
2806 
2807 TEST_F(FormatTest, LayoutBlockInsideParens) {
2808   verifyFormat("functionCall({ int i; });");
2809   verifyFormat("functionCall({\n"
2810                "  int i;\n"
2811                "  int j;\n"
2812                "});");
2813   verifyFormat("functionCall(\n"
2814                "    {\n"
2815                "      int i;\n"
2816                "      int j;\n"
2817                "    },\n"
2818                "    aaaa, bbbb, cccc);");
2819   verifyFormat("functionA(functionB({\n"
2820                "            int i;\n"
2821                "            int j;\n"
2822                "          }),\n"
2823                "          aaaa, bbbb, cccc);");
2824   verifyFormat("functionCall(\n"
2825                "    {\n"
2826                "      int i;\n"
2827                "      int j;\n"
2828                "    },\n"
2829                "    aaaa, bbbb, // comment\n"
2830                "    cccc);");
2831   verifyFormat("functionA(functionB({\n"
2832                "            int i;\n"
2833                "            int j;\n"
2834                "          }),\n"
2835                "          aaaa, bbbb, // comment\n"
2836                "          cccc);");
2837   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2838   verifyFormat("functionCall(aaaa, bbbb, {\n"
2839                "  int i;\n"
2840                "  int j;\n"
2841                "});");
2842   verifyFormat(
2843       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2844       "    {\n"
2845       "      int i; // break\n"
2846       "    },\n"
2847       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2848       "                                     ccccccccccccccccc));");
2849   verifyFormat("DEBUG({\n"
2850                "  if (a)\n"
2851                "    f();\n"
2852                "});");
2853 }
2854 
2855 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2856   EXPECT_EQ("SOME_MACRO { int i; }\n"
2857             "int i;",
2858             format("  SOME_MACRO  {int i;}  int i;"));
2859 }
2860 
2861 TEST_F(FormatTest, LayoutNestedBlocks) {
2862   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2863                "  struct s {\n"
2864                "    int i;\n"
2865                "  };\n"
2866                "  s kBitsToOs[] = {{10}};\n"
2867                "  for (int i = 0; i < 10; ++i)\n"
2868                "    return;\n"
2869                "}");
2870   verifyFormat("call(parameter, {\n"
2871                "  something();\n"
2872                "  // Comment using all columns.\n"
2873                "  somethingelse();\n"
2874                "});",
2875                getLLVMStyleWithColumns(40));
2876   verifyFormat("DEBUG( //\n"
2877                "    { f(); }, a);");
2878   verifyFormat("DEBUG( //\n"
2879                "    {\n"
2880                "      f(); //\n"
2881                "    },\n"
2882                "    a);");
2883 
2884   EXPECT_EQ("call(parameter, {\n"
2885             "  something();\n"
2886             "  // Comment too\n"
2887             "  // looooooooooong.\n"
2888             "  somethingElse();\n"
2889             "});",
2890             format("call(parameter, {\n"
2891                    "  something();\n"
2892                    "  // Comment too looooooooooong.\n"
2893                    "  somethingElse();\n"
2894                    "});",
2895                    getLLVMStyleWithColumns(29)));
2896   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2897   EXPECT_EQ("DEBUG({ // comment\n"
2898             "  int i;\n"
2899             "});",
2900             format("DEBUG({ // comment\n"
2901                    "int  i;\n"
2902                    "});"));
2903   EXPECT_EQ("DEBUG({\n"
2904             "  int i;\n"
2905             "\n"
2906             "  // comment\n"
2907             "  int j;\n"
2908             "});",
2909             format("DEBUG({\n"
2910                    "  int  i;\n"
2911                    "\n"
2912                    "  // comment\n"
2913                    "  int  j;\n"
2914                    "});"));
2915 
2916   verifyFormat("DEBUG({\n"
2917                "  if (a)\n"
2918                "    return;\n"
2919                "});");
2920   verifyGoogleFormat("DEBUG({\n"
2921                      "  if (a) return;\n"
2922                      "});");
2923   FormatStyle Style = getGoogleStyle();
2924   Style.ColumnLimit = 45;
2925   verifyFormat("Debug(aaaaa,\n"
2926                "      {\n"
2927                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2928                "      },\n"
2929                "      a);",
2930                Style);
2931 
2932   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2933 
2934   verifyNoCrash("^{v^{a}}");
2935 }
2936 
2937 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2938   EXPECT_EQ("#define MACRO()                     \\\n"
2939             "  Debug(aaa, /* force line break */ \\\n"
2940             "        {                           \\\n"
2941             "          int i;                    \\\n"
2942             "          int j;                    \\\n"
2943             "        })",
2944             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2945                    "          {  int   i;  int  j;   })",
2946                    getGoogleStyle()));
2947 
2948   EXPECT_EQ("#define A                                       \\\n"
2949             "  [] {                                          \\\n"
2950             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2951             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2952             "  }",
2953             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2954                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2955                    getGoogleStyle()));
2956 }
2957 
2958 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2959   EXPECT_EQ("{}", format("{}"));
2960   verifyFormat("enum E {};");
2961   verifyFormat("enum E {}");
2962 }
2963 
2964 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2965   FormatStyle Style = getLLVMStyle();
2966   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2967   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2968   verifyFormat("FOO_BEGIN\n"
2969                "  FOO_ENTRY\n"
2970                "FOO_END", Style);
2971   verifyFormat("FOO_BEGIN\n"
2972                "  NESTED_FOO_BEGIN\n"
2973                "    NESTED_FOO_ENTRY\n"
2974                "  NESTED_FOO_END\n"
2975                "FOO_END", Style);
2976   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2977                "  int x;\n"
2978                "  x = 1;\n"
2979                "FOO_END(Baz)", Style);
2980 }
2981 
2982 //===----------------------------------------------------------------------===//
2983 // Line break tests.
2984 //===----------------------------------------------------------------------===//
2985 
2986 TEST_F(FormatTest, PreventConfusingIndents) {
2987   verifyFormat(
2988       "void f() {\n"
2989       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2990       "                         parameter, parameter, parameter)),\n"
2991       "                     SecondLongCall(parameter));\n"
2992       "}");
2993   verifyFormat(
2994       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2995       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2996       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2997       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2998   verifyFormat(
2999       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3000       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3001       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3002       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3003   verifyFormat(
3004       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3005       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3006       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3007       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3008   verifyFormat("int a = bbbb && ccc &&\n"
3009                "        fffff(\n"
3010                "#define A Just forcing a new line\n"
3011                "            ddd);");
3012 }
3013 
3014 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3015   verifyFormat(
3016       "bool aaaaaaa =\n"
3017       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3018       "    bbbbbbbb();");
3019   verifyFormat(
3020       "bool aaaaaaa =\n"
3021       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3022       "    bbbbbbbb();");
3023 
3024   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3025                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3026                "    ccccccccc == ddddddddddd;");
3027   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3028                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3029                "    ccccccccc == ddddddddddd;");
3030   verifyFormat(
3031       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3032       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3033       "    ccccccccc == ddddddddddd;");
3034 
3035   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3036                "                 aaaaaa) &&\n"
3037                "         bbbbbb && cccccc;");
3038   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3039                "                 aaaaaa) >>\n"
3040                "         bbbbbb;");
3041   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
3042                "    SourceMgr.getSpellingColumnNumber(\n"
3043                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3044                "    1);");
3045 
3046   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3047                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3048                "    cccccc) {\n}");
3049   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3050                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
3051                "              cccccc) {\n}");
3052   verifyFormat("b = a &&\n"
3053                "    // Comment\n"
3054                "    b.c && d;");
3055 
3056   // If the LHS of a comparison is not a binary expression itself, the
3057   // additional linebreak confuses many people.
3058   verifyFormat(
3059       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3060       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3061       "}");
3062   verifyFormat(
3063       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3064       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3065       "}");
3066   verifyFormat(
3067       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3068       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3069       "}");
3070   // Even explicit parentheses stress the precedence enough to make the
3071   // additional break unnecessary.
3072   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3073                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3074                "}");
3075   // This cases is borderline, but with the indentation it is still readable.
3076   verifyFormat(
3077       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3078       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3079       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3080       "}",
3081       getLLVMStyleWithColumns(75));
3082 
3083   // If the LHS is a binary expression, we should still use the additional break
3084   // as otherwise the formatting hides the operator precedence.
3085   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3086                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3087                "    5) {\n"
3088                "}");
3089 
3090   FormatStyle OnePerLine = getLLVMStyle();
3091   OnePerLine.BinPackParameters = false;
3092   verifyFormat(
3093       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3094       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3095       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3096       OnePerLine);
3097 
3098   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
3099                "                .aaa(aaaaaaaaaaaaa) *\n"
3100                "            aaaaaaa +\n"
3101                "        aaaaaaa;",
3102                getLLVMStyleWithColumns(40));
3103 }
3104 
3105 TEST_F(FormatTest, ExpressionIndentation) {
3106   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3107                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3108                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3109                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3110                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3111                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3112                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3113                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3114                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3115   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3116                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3117                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3118                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3119   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3120                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3121                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3122                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3123   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3124                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3125                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3126                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3127   verifyFormat("if () {\n"
3128                "} else if (aaaaa && bbbbb > // break\n"
3129                "                        ccccc) {\n"
3130                "}");
3131   verifyFormat("if () {\n"
3132                "} else if (aaaaa &&\n"
3133                "           bbbbb > // break\n"
3134                "               ccccc &&\n"
3135                "           ddddd) {\n"
3136                "}");
3137 
3138   // Presence of a trailing comment used to change indentation of b.
3139   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3140                "       b;\n"
3141                "return aaaaaaaaaaaaaaaaaaa +\n"
3142                "       b; //",
3143                getLLVMStyleWithColumns(30));
3144 }
3145 
3146 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3147   // Not sure what the best system is here. Like this, the LHS can be found
3148   // immediately above an operator (everything with the same or a higher
3149   // indent). The RHS is aligned right of the operator and so compasses
3150   // everything until something with the same indent as the operator is found.
3151   // FIXME: Is this a good system?
3152   FormatStyle Style = getLLVMStyle();
3153   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3154   verifyFormat(
3155       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3156       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3157       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3158       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3159       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3160       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3161       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3162       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3163       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3164       Style);
3165   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3166                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3167                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3168                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3169                Style);
3170   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3171                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3172                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3173                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3174                Style);
3175   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3176                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3177                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3178                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3179                Style);
3180   verifyFormat("if () {\n"
3181                "} else if (aaaaa\n"
3182                "           && bbbbb // break\n"
3183                "                  > ccccc) {\n"
3184                "}",
3185                Style);
3186   verifyFormat("return (a)\n"
3187                "       // comment\n"
3188                "       + b;",
3189                Style);
3190   verifyFormat(
3191       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3192       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3193       "             + cc;",
3194       Style);
3195 
3196   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3197                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3198                Style);
3199 
3200   // Forced by comments.
3201   verifyFormat(
3202       "unsigned ContentSize =\n"
3203       "    sizeof(int16_t)   // DWARF ARange version number\n"
3204       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3205       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3206       "    + sizeof(int8_t); // Segment Size (in bytes)");
3207 
3208   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3209                "       == boost::fusion::at_c<1>(iiii).second;",
3210                Style);
3211 
3212   Style.ColumnLimit = 60;
3213   verifyFormat("zzzzzzzzzz\n"
3214                "    = bbbbbbbbbbbbbbbbb\n"
3215                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3216                Style);
3217 }
3218 
3219 TEST_F(FormatTest, EnforcedOperatorWraps) {
3220   // Here we'd like to wrap after the || operators, but a comment is forcing an
3221   // earlier wrap.
3222   verifyFormat("bool x = aaaaa //\n"
3223                "         || bbbbb\n"
3224                "         //\n"
3225                "         || cccc;");
3226 }
3227 
3228 TEST_F(FormatTest, NoOperandAlignment) {
3229   FormatStyle Style = getLLVMStyle();
3230   Style.AlignOperands = false;
3231   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
3232                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3233                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3234                Style);
3235   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3236   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3237                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3238                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3239                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3240                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3241                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3242                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3243                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3244                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3245                Style);
3246 
3247   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3248                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3249                "    + cc;",
3250                Style);
3251   verifyFormat("int a = aa\n"
3252                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3253                "        * cccccccccccccccccccccccccccccccccccc;\n",
3254                Style);
3255 
3256   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3257   verifyFormat("return (a > b\n"
3258                "    // comment1\n"
3259                "    // comment2\n"
3260                "    || c);",
3261                Style);
3262 }
3263 
3264 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3265   FormatStyle Style = getLLVMStyle();
3266   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3267   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3268                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3269                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3270                Style);
3271 }
3272 
3273 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
3274   FormatStyle Style = getLLVMStyle();
3275   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3276   Style.BinPackArguments = false;
3277   Style.ColumnLimit = 40;
3278   verifyFormat("void test() {\n"
3279                "  someFunction(\n"
3280                "      this + argument + is + quite\n"
3281                "      + long + so + it + gets + wrapped\n"
3282                "      + but + remains + bin - packed);\n"
3283                "}",
3284                Style);
3285   verifyFormat("void test() {\n"
3286                "  someFunction(arg1,\n"
3287                "               this + argument + is\n"
3288                "                   + quite + long + so\n"
3289                "                   + it + gets + wrapped\n"
3290                "                   + but + remains + bin\n"
3291                "                   - packed,\n"
3292                "               arg3);\n"
3293                "}",
3294                Style);
3295   verifyFormat("void test() {\n"
3296                "  someFunction(\n"
3297                "      arg1,\n"
3298                "      this + argument + has\n"
3299                "          + anotherFunc(nested,\n"
3300                "                        calls + whose\n"
3301                "                            + arguments\n"
3302                "                            + are + also\n"
3303                "                            + wrapped,\n"
3304                "                        in + addition)\n"
3305                "          + to + being + bin - packed,\n"
3306                "      arg3);\n"
3307                "}",
3308                Style);
3309 
3310   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
3311   verifyFormat("void test() {\n"
3312                "  someFunction(\n"
3313                "      arg1,\n"
3314                "      this + argument + has +\n"
3315                "          anotherFunc(nested,\n"
3316                "                      calls + whose +\n"
3317                "                          arguments +\n"
3318                "                          are + also +\n"
3319                "                          wrapped,\n"
3320                "                      in + addition) +\n"
3321                "          to + being + bin - packed,\n"
3322                "      arg3);\n"
3323                "}",
3324                Style);
3325 }
3326 
3327 TEST_F(FormatTest, ConstructorInitializers) {
3328   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3329   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3330                getLLVMStyleWithColumns(45));
3331   verifyFormat("Constructor()\n"
3332                "    : Inttializer(FitsOnTheLine) {}",
3333                getLLVMStyleWithColumns(44));
3334   verifyFormat("Constructor()\n"
3335                "    : Inttializer(FitsOnTheLine) {}",
3336                getLLVMStyleWithColumns(43));
3337 
3338   verifyFormat("template <typename T>\n"
3339                "Constructor() : Initializer(FitsOnTheLine) {}",
3340                getLLVMStyleWithColumns(45));
3341 
3342   verifyFormat(
3343       "SomeClass::Constructor()\n"
3344       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3345 
3346   verifyFormat(
3347       "SomeClass::Constructor()\n"
3348       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3349       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3350   verifyFormat(
3351       "SomeClass::Constructor()\n"
3352       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3353       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3354   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3355                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3356                "    : aaaaaaaaaa(aaaaaa) {}");
3357 
3358   verifyFormat("Constructor()\n"
3359                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3360                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3361                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3362                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3363 
3364   verifyFormat("Constructor()\n"
3365                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3366                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3367 
3368   verifyFormat("Constructor(int Parameter = 0)\n"
3369                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3370                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3371   verifyFormat("Constructor()\n"
3372                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3373                "}",
3374                getLLVMStyleWithColumns(60));
3375   verifyFormat("Constructor()\n"
3376                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3377                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3378 
3379   // Here a line could be saved by splitting the second initializer onto two
3380   // lines, but that is not desirable.
3381   verifyFormat("Constructor()\n"
3382                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3383                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3384                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3385 
3386   FormatStyle OnePerLine = getLLVMStyle();
3387   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3388   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3389   verifyFormat("SomeClass::Constructor()\n"
3390                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3391                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3392                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3393                OnePerLine);
3394   verifyFormat("SomeClass::Constructor()\n"
3395                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3396                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3397                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3398                OnePerLine);
3399   verifyFormat("MyClass::MyClass(int var)\n"
3400                "    : some_var_(var),            // 4 space indent\n"
3401                "      some_other_var_(var + 1) { // lined up\n"
3402                "}",
3403                OnePerLine);
3404   verifyFormat("Constructor()\n"
3405                "    : aaaaa(aaaaaa),\n"
3406                "      aaaaa(aaaaaa),\n"
3407                "      aaaaa(aaaaaa),\n"
3408                "      aaaaa(aaaaaa),\n"
3409                "      aaaaa(aaaaaa) {}",
3410                OnePerLine);
3411   verifyFormat("Constructor()\n"
3412                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3413                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3414                OnePerLine);
3415   OnePerLine.BinPackParameters = false;
3416   verifyFormat(
3417       "Constructor()\n"
3418       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3419       "          aaaaaaaaaaa().aaa(),\n"
3420       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3421       OnePerLine);
3422   OnePerLine.ColumnLimit = 60;
3423   verifyFormat("Constructor()\n"
3424                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
3425                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3426                OnePerLine);
3427 
3428   EXPECT_EQ("Constructor()\n"
3429             "    : // Comment forcing unwanted break.\n"
3430             "      aaaa(aaaa) {}",
3431             format("Constructor() :\n"
3432                    "    // Comment forcing unwanted break.\n"
3433                    "    aaaa(aaaa) {}"));
3434 }
3435 
3436 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
3437   FormatStyle Style = getLLVMStyle();
3438   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
3439 
3440   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3441   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
3442                getStyleWithColumns(Style, 45));
3443   verifyFormat("Constructor() :\n"
3444                "    Initializer(FitsOnTheLine) {}",
3445                getStyleWithColumns(Style, 44));
3446   verifyFormat("Constructor() :\n"
3447                "    Initializer(FitsOnTheLine) {}",
3448                getStyleWithColumns(Style, 43));
3449 
3450   verifyFormat("template <typename T>\n"
3451                "Constructor() : Initializer(FitsOnTheLine) {}",
3452                getStyleWithColumns(Style, 50));
3453 
3454   verifyFormat(
3455       "SomeClass::Constructor() :\n"
3456       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3457 	  Style);
3458 
3459   verifyFormat(
3460       "SomeClass::Constructor() :\n"
3461       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3462       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3463 	  Style);
3464   verifyFormat(
3465       "SomeClass::Constructor() :\n"
3466       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3467       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3468 	  Style);
3469   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3470                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3471                "    aaaaaaaaaa(aaaaaa) {}",
3472 			   Style);
3473 
3474   verifyFormat("Constructor() :\n"
3475                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3476                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3477                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3478                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3479 			   Style);
3480 
3481   verifyFormat("Constructor() :\n"
3482                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3483                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3484 			   Style);
3485 
3486   verifyFormat("Constructor(int Parameter = 0) :\n"
3487                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3488                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3489 			   Style);
3490   verifyFormat("Constructor() :\n"
3491                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3492                "}",
3493                getStyleWithColumns(Style, 60));
3494   verifyFormat("Constructor() :\n"
3495                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3496                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3497 			   Style);
3498 
3499   // Here a line could be saved by splitting the second initializer onto two
3500   // lines, but that is not desirable.
3501   verifyFormat("Constructor() :\n"
3502                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3503                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3504                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3505 			   Style);
3506 
3507   FormatStyle OnePerLine = Style;
3508   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3509   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3510   verifyFormat("SomeClass::Constructor() :\n"
3511                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3512                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3513                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3514                OnePerLine);
3515   verifyFormat("SomeClass::Constructor() :\n"
3516                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3517                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3518                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3519                OnePerLine);
3520   verifyFormat("MyClass::MyClass(int var) :\n"
3521                "    some_var_(var),            // 4 space indent\n"
3522                "    some_other_var_(var + 1) { // lined up\n"
3523                "}",
3524                OnePerLine);
3525   verifyFormat("Constructor() :\n"
3526                "    aaaaa(aaaaaa),\n"
3527                "    aaaaa(aaaaaa),\n"
3528                "    aaaaa(aaaaaa),\n"
3529                "    aaaaa(aaaaaa),\n"
3530                "    aaaaa(aaaaaa) {}",
3531                OnePerLine);
3532   verifyFormat("Constructor() :\n"
3533                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3534                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3535                OnePerLine);
3536   OnePerLine.BinPackParameters = false;
3537   verifyFormat(
3538       "Constructor() :\n"
3539       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3540       "        aaaaaaaaaaa().aaa(),\n"
3541       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3542       OnePerLine);
3543   OnePerLine.ColumnLimit = 60;
3544   verifyFormat("Constructor() :\n"
3545                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3546                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3547                OnePerLine);
3548 
3549   EXPECT_EQ("Constructor() :\n"
3550             "    // Comment forcing unwanted break.\n"
3551             "    aaaa(aaaa) {}",
3552             format("Constructor() :\n"
3553                    "    // Comment forcing unwanted break.\n"
3554                    "    aaaa(aaaa) {}",
3555 				   Style));
3556 
3557   Style.ColumnLimit = 0;
3558   verifyFormat("SomeClass::Constructor() :\n"
3559                "    a(a) {}",
3560                Style);
3561   verifyFormat("SomeClass::Constructor() noexcept :\n"
3562                "    a(a) {}",
3563                Style);
3564   verifyFormat("SomeClass::Constructor() :\n"
3565 			   "    a(a), b(b), c(c) {}",
3566                Style);
3567   verifyFormat("SomeClass::Constructor() :\n"
3568                "    a(a) {\n"
3569                "  foo();\n"
3570                "  bar();\n"
3571                "}",
3572                Style);
3573 
3574   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3575   verifyFormat("SomeClass::Constructor() :\n"
3576 			   "    a(a), b(b), c(c) {\n"
3577 			   "}",
3578                Style);
3579   verifyFormat("SomeClass::Constructor() :\n"
3580                "    a(a) {\n"
3581 			   "}",
3582                Style);
3583 
3584   Style.ColumnLimit = 80;
3585   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3586   Style.ConstructorInitializerIndentWidth = 2;
3587   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3588                Style);
3589   verifyFormat("SomeClass::Constructor() :\n"
3590                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3591                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3592                Style);
3593 }
3594 
3595 #ifndef EXPENSIVE_CHECKS
3596 // Expensive checks enables libstdc++ checking which includes validating the
3597 // state of ranges used in std::priority_queue - this blows out the
3598 // runtime/scalability of the function and makes this test unacceptably slow.
3599 TEST_F(FormatTest, MemoizationTests) {
3600   // This breaks if the memoization lookup does not take \c Indent and
3601   // \c LastSpace into account.
3602   verifyFormat(
3603       "extern CFRunLoopTimerRef\n"
3604       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3605       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3606       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3607       "                     CFRunLoopTimerContext *context) {}");
3608 
3609   // Deep nesting somewhat works around our memoization.
3610   verifyFormat(
3611       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3612       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3613       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3614       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3615       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3616       getLLVMStyleWithColumns(65));
3617   verifyFormat(
3618       "aaaaa(\n"
3619       "    aaaaa,\n"
3620       "    aaaaa(\n"
3621       "        aaaaa,\n"
3622       "        aaaaa(\n"
3623       "            aaaaa,\n"
3624       "            aaaaa(\n"
3625       "                aaaaa,\n"
3626       "                aaaaa(\n"
3627       "                    aaaaa,\n"
3628       "                    aaaaa(\n"
3629       "                        aaaaa,\n"
3630       "                        aaaaa(\n"
3631       "                            aaaaa,\n"
3632       "                            aaaaa(\n"
3633       "                                aaaaa,\n"
3634       "                                aaaaa(\n"
3635       "                                    aaaaa,\n"
3636       "                                    aaaaa(\n"
3637       "                                        aaaaa,\n"
3638       "                                        aaaaa(\n"
3639       "                                            aaaaa,\n"
3640       "                                            aaaaa(\n"
3641       "                                                aaaaa,\n"
3642       "                                                aaaaa))))))))))));",
3643       getLLVMStyleWithColumns(65));
3644   verifyFormat(
3645       "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"
3646       "                                  a),\n"
3647       "                                a),\n"
3648       "                              a),\n"
3649       "                            a),\n"
3650       "                          a),\n"
3651       "                        a),\n"
3652       "                      a),\n"
3653       "                    a),\n"
3654       "                  a),\n"
3655       "                a),\n"
3656       "              a),\n"
3657       "            a),\n"
3658       "          a),\n"
3659       "        a),\n"
3660       "      a),\n"
3661       "    a),\n"
3662       "  a)",
3663       getLLVMStyleWithColumns(65));
3664 
3665   // This test takes VERY long when memoization is broken.
3666   FormatStyle OnePerLine = getLLVMStyle();
3667   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3668   OnePerLine.BinPackParameters = false;
3669   std::string input = "Constructor()\n"
3670                       "    : aaaa(a,\n";
3671   for (unsigned i = 0, e = 80; i != e; ++i) {
3672     input += "           a,\n";
3673   }
3674   input += "           a) {}";
3675   verifyFormat(input, OnePerLine);
3676 }
3677 #endif
3678 
3679 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3680   verifyFormat(
3681       "void f() {\n"
3682       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3683       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3684       "    f();\n"
3685       "}");
3686   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3687                "    Intervals[i - 1].getRange().getLast()) {\n}");
3688 }
3689 
3690 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3691   // Principially, we break function declarations in a certain order:
3692   // 1) break amongst arguments.
3693   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3694                "                              Cccccccccccccc cccccccccccccc);");
3695   verifyFormat("template <class TemplateIt>\n"
3696                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3697                "                            TemplateIt *stop) {}");
3698 
3699   // 2) break after return type.
3700   verifyFormat(
3701       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3702       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3703       getGoogleStyle());
3704 
3705   // 3) break after (.
3706   verifyFormat(
3707       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3708       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3709       getGoogleStyle());
3710 
3711   // 4) break before after nested name specifiers.
3712   verifyFormat(
3713       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3714       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3715       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3716       getGoogleStyle());
3717 
3718   // However, there are exceptions, if a sufficient amount of lines can be
3719   // saved.
3720   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3721   // more adjusting.
3722   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3723                "                                  Cccccccccccccc cccccccccc,\n"
3724                "                                  Cccccccccccccc cccccccccc,\n"
3725                "                                  Cccccccccccccc cccccccccc,\n"
3726                "                                  Cccccccccccccc cccccccccc);");
3727   verifyFormat(
3728       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3729       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3730       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3731       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3732       getGoogleStyle());
3733   verifyFormat(
3734       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3735       "                                          Cccccccccccccc cccccccccc,\n"
3736       "                                          Cccccccccccccc cccccccccc,\n"
3737       "                                          Cccccccccccccc cccccccccc,\n"
3738       "                                          Cccccccccccccc cccccccccc,\n"
3739       "                                          Cccccccccccccc cccccccccc,\n"
3740       "                                          Cccccccccccccc cccccccccc);");
3741   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3742                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3743                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3744                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3745                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3746 
3747   // Break after multi-line parameters.
3748   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3749                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3750                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3751                "    bbbb bbbb);");
3752   verifyFormat("void SomeLoooooooooooongFunction(\n"
3753                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3754                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3755                "    int bbbbbbbbbbbbb);");
3756 
3757   // Treat overloaded operators like other functions.
3758   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3759                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3760   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3761                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3762   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3763                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3764   verifyGoogleFormat(
3765       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3766       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3767   verifyGoogleFormat(
3768       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3769       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3770   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3771                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3772   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3773                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3774   verifyGoogleFormat(
3775       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3776       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3777       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3778   verifyGoogleFormat(
3779       "template <typename T>\n"
3780       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3781       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3782       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3783 
3784   FormatStyle Style = getLLVMStyle();
3785   Style.PointerAlignment = FormatStyle::PAS_Left;
3786   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3787                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3788                Style);
3789   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3790                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3791                Style);
3792 }
3793 
3794 TEST_F(FormatTest, TrailingReturnType) {
3795   verifyFormat("auto foo() -> int;\n");
3796   verifyFormat("struct S {\n"
3797                "  auto bar() const -> int;\n"
3798                "};");
3799   verifyFormat("template <size_t Order, typename T>\n"
3800                "auto load_img(const std::string &filename)\n"
3801                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3802   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3803                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3804   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3805   verifyFormat("template <typename T>\n"
3806                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3807                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3808 
3809   // Not trailing return types.
3810   verifyFormat("void f() { auto a = b->c(); }");
3811 }
3812 
3813 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3814   // Avoid breaking before trailing 'const' or other trailing annotations, if
3815   // they are not function-like.
3816   FormatStyle Style = getGoogleStyle();
3817   Style.ColumnLimit = 47;
3818   verifyFormat("void someLongFunction(\n"
3819                "    int someLoooooooooooooongParameter) const {\n}",
3820                getLLVMStyleWithColumns(47));
3821   verifyFormat("LoooooongReturnType\n"
3822                "someLoooooooongFunction() const {}",
3823                getLLVMStyleWithColumns(47));
3824   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3825                "    const {}",
3826                Style);
3827   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3828                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3829   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3830                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3831   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3832                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3833   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3834                "                   aaaaaaaaaaa aaaaa) const override;");
3835   verifyGoogleFormat(
3836       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3837       "    const override;");
3838 
3839   // Even if the first parameter has to be wrapped.
3840   verifyFormat("void someLongFunction(\n"
3841                "    int someLongParameter) const {}",
3842                getLLVMStyleWithColumns(46));
3843   verifyFormat("void someLongFunction(\n"
3844                "    int someLongParameter) const {}",
3845                Style);
3846   verifyFormat("void someLongFunction(\n"
3847                "    int someLongParameter) override {}",
3848                Style);
3849   verifyFormat("void someLongFunction(\n"
3850                "    int someLongParameter) OVERRIDE {}",
3851                Style);
3852   verifyFormat("void someLongFunction(\n"
3853                "    int someLongParameter) final {}",
3854                Style);
3855   verifyFormat("void someLongFunction(\n"
3856                "    int someLongParameter) FINAL {}",
3857                Style);
3858   verifyFormat("void someLongFunction(\n"
3859                "    int parameter) const override {}",
3860                Style);
3861 
3862   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3863   verifyFormat("void someLongFunction(\n"
3864                "    int someLongParameter) const\n"
3865                "{\n"
3866                "}",
3867                Style);
3868 
3869   // Unless these are unknown annotations.
3870   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3871                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3872                "    LONG_AND_UGLY_ANNOTATION;");
3873 
3874   // Breaking before function-like trailing annotations is fine to keep them
3875   // close to their arguments.
3876   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3877                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3878   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3879                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3880   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3881                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3882   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3883                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3884   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3885 
3886   verifyFormat(
3887       "void aaaaaaaaaaaaaaaaaa()\n"
3888       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3889       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3890   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3891                "    __attribute__((unused));");
3892   verifyGoogleFormat(
3893       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3894       "    GUARDED_BY(aaaaaaaaaaaa);");
3895   verifyGoogleFormat(
3896       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3897       "    GUARDED_BY(aaaaaaaaaaaa);");
3898   verifyGoogleFormat(
3899       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3900       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3901   verifyGoogleFormat(
3902       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3903       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3904 }
3905 
3906 TEST_F(FormatTest, FunctionAnnotations) {
3907   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3908                "int OldFunction(const string &parameter) {}");
3909   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3910                "string OldFunction(const string &parameter) {}");
3911   verifyFormat("template <typename T>\n"
3912                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3913                "string OldFunction(const string &parameter) {}");
3914 
3915   // Not function annotations.
3916   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3917                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3918   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3919                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3920   verifyFormat("MACRO(abc).function() // wrap\n"
3921                "    << abc;");
3922   verifyFormat("MACRO(abc)->function() // wrap\n"
3923                "    << abc;");
3924   verifyFormat("MACRO(abc)::function() // wrap\n"
3925                "    << abc;");
3926 }
3927 
3928 TEST_F(FormatTest, BreaksDesireably) {
3929   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3930                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3931                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3932   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3933                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3934                "}");
3935 
3936   verifyFormat(
3937       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3938       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3939 
3940   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3941                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3942                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3943 
3944   verifyFormat(
3945       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3946       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3947       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3948       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3949       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3950 
3951   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3952                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3953 
3954   verifyFormat(
3955       "void f() {\n"
3956       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3957       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3958       "}");
3959   verifyFormat(
3960       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3961       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3962   verifyFormat(
3963       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3964       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3965   verifyFormat(
3966       "aaaaaa(aaa,\n"
3967       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3968       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3969       "       aaaa);");
3970   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3971                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3972                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3973 
3974   // Indent consistently independent of call expression and unary operator.
3975   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3976                "    dddddddddddddddddddddddddddddd));");
3977   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3978                "    dddddddddddddddddddddddddddddd));");
3979   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3980                "    dddddddddddddddddddddddddddddd));");
3981 
3982   // This test case breaks on an incorrect memoization, i.e. an optimization not
3983   // taking into account the StopAt value.
3984   verifyFormat(
3985       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3986       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3987       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3988       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3989 
3990   verifyFormat("{\n  {\n    {\n"
3991                "      Annotation.SpaceRequiredBefore =\n"
3992                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3993                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3994                "    }\n  }\n}");
3995 
3996   // Break on an outer level if there was a break on an inner level.
3997   EXPECT_EQ("f(g(h(a, // comment\n"
3998             "      b, c),\n"
3999             "    d, e),\n"
4000             "  x, y);",
4001             format("f(g(h(a, // comment\n"
4002                    "    b, c), d, e), x, y);"));
4003 
4004   // Prefer breaking similar line breaks.
4005   verifyFormat(
4006       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4007       "                             NSTrackingMouseEnteredAndExited |\n"
4008       "                             NSTrackingActiveAlways;");
4009 }
4010 
4011 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4012   FormatStyle NoBinPacking = getGoogleStyle();
4013   NoBinPacking.BinPackParameters = false;
4014   NoBinPacking.BinPackArguments = true;
4015   verifyFormat("void f() {\n"
4016                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4017                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4018                "}",
4019                NoBinPacking);
4020   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4021                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4022                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4023                NoBinPacking);
4024 
4025   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4026   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4027                "                        vector<int> bbbbbbbbbbbbbbb);",
4028                NoBinPacking);
4029   // FIXME: This behavior difference is probably not wanted. However, currently
4030   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
4031   // template arguments from BreakBeforeParameter being set because of the
4032   // one-per-line formatting.
4033   verifyFormat(
4034       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4035       "                                             aaaaaaaaaa> aaaaaaaaaa);",
4036       NoBinPacking);
4037   verifyFormat(
4038       "void fffffffffff(\n"
4039       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
4040       "        aaaaaaaaaa);");
4041 }
4042 
4043 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4044   FormatStyle NoBinPacking = getGoogleStyle();
4045   NoBinPacking.BinPackParameters = false;
4046   NoBinPacking.BinPackArguments = false;
4047   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4048                "  aaaaaaaaaaaaaaaaaaaa,\n"
4049                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4050                NoBinPacking);
4051   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4052                "        aaaaaaaaaaaaa,\n"
4053                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4054                NoBinPacking);
4055   verifyFormat(
4056       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4057       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4058       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4059       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4060       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4061       NoBinPacking);
4062   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4063                "    .aaaaaaaaaaaaaaaaaa();",
4064                NoBinPacking);
4065   verifyFormat("void f() {\n"
4066                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4067                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4068                "}",
4069                NoBinPacking);
4070 
4071   verifyFormat(
4072       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4073       "             aaaaaaaaaaaa,\n"
4074       "             aaaaaaaaaaaa);",
4075       NoBinPacking);
4076   verifyFormat(
4077       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4078       "                               ddddddddddddddddddddddddddddd),\n"
4079       "             test);",
4080       NoBinPacking);
4081 
4082   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4083                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4084                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
4085                "    aaaaaaaaaaaaaaaaaa;",
4086                NoBinPacking);
4087   verifyFormat("a(\"a\"\n"
4088                "  \"a\",\n"
4089                "  a);");
4090 
4091   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4092   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4093                "                aaaaaaaaa,\n"
4094                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4095                NoBinPacking);
4096   verifyFormat(
4097       "void f() {\n"
4098       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4099       "      .aaaaaaa();\n"
4100       "}",
4101       NoBinPacking);
4102   verifyFormat(
4103       "template <class SomeType, class SomeOtherType>\n"
4104       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4105       NoBinPacking);
4106 }
4107 
4108 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4109   FormatStyle Style = getLLVMStyleWithColumns(15);
4110   Style.ExperimentalAutoDetectBinPacking = true;
4111   EXPECT_EQ("aaa(aaaa,\n"
4112             "    aaaa,\n"
4113             "    aaaa);\n"
4114             "aaa(aaaa,\n"
4115             "    aaaa,\n"
4116             "    aaaa);",
4117             format("aaa(aaaa,\n" // one-per-line
4118                    "  aaaa,\n"
4119                    "    aaaa  );\n"
4120                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4121                    Style));
4122   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4123             "    aaaa);\n"
4124             "aaa(aaaa, aaaa,\n"
4125             "    aaaa);",
4126             format("aaa(aaaa,  aaaa,\n" // bin-packed
4127                    "    aaaa  );\n"
4128                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4129                    Style));
4130 }
4131 
4132 TEST_F(FormatTest, FormatsBuilderPattern) {
4133   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4134                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4135                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4136                "    .StartsWith(\".init\", ORDER_INIT)\n"
4137                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4138                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4139                "    .Default(ORDER_TEXT);\n");
4140 
4141   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4142                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4143   verifyFormat(
4144       "aaaaaaa->aaaaaaa\n"
4145       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4146       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4147       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4148   verifyFormat(
4149       "aaaaaaa->aaaaaaa\n"
4150       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4151       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4152   verifyFormat(
4153       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4154       "    aaaaaaaaaaaaaa);");
4155   verifyFormat(
4156       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4157       "    aaaaaa->aaaaaaaaaaaa()\n"
4158       "        ->aaaaaaaaaaaaaaaa(\n"
4159       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4160       "        ->aaaaaaaaaaaaaaaaa();");
4161   verifyGoogleFormat(
4162       "void f() {\n"
4163       "  someo->Add((new util::filetools::Handler(dir))\n"
4164       "                 ->OnEvent1(NewPermanentCallback(\n"
4165       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4166       "                 ->OnEvent2(NewPermanentCallback(\n"
4167       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4168       "                 ->OnEvent3(NewPermanentCallback(\n"
4169       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4170       "                 ->OnEvent5(NewPermanentCallback(\n"
4171       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4172       "                 ->OnEvent6(NewPermanentCallback(\n"
4173       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4174       "}");
4175 
4176   verifyFormat(
4177       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4178   verifyFormat("aaaaaaaaaaaaaaa()\n"
4179                "    .aaaaaaaaaaaaaaa()\n"
4180                "    .aaaaaaaaaaaaaaa()\n"
4181                "    .aaaaaaaaaaaaaaa()\n"
4182                "    .aaaaaaaaaaaaaaa();");
4183   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4184                "    .aaaaaaaaaaaaaaa()\n"
4185                "    .aaaaaaaaaaaaaaa()\n"
4186                "    .aaaaaaaaaaaaaaa();");
4187   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4188                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4189                "    .aaaaaaaaaaaaaaa();");
4190   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4191                "    ->aaaaaaaaaaaaaae(0)\n"
4192                "    ->aaaaaaaaaaaaaaa();");
4193 
4194   // Don't linewrap after very short segments.
4195   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4196                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4197                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4198   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4199                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4200                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4201   verifyFormat("aaa()\n"
4202                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4203                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4204                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4205 
4206   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4207                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4208                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4209   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4210                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4211                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4212 
4213   // Prefer not to break after empty parentheses.
4214   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4215                "    First->LastNewlineOffset);");
4216 
4217   // Prefer not to create "hanging" indents.
4218   verifyFormat(
4219       "return !soooooooooooooome_map\n"
4220       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4221       "            .second;");
4222   verifyFormat(
4223       "return aaaaaaaaaaaaaaaa\n"
4224       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
4225       "    .aaaa(aaaaaaaaaaaaaa);");
4226   // No hanging indent here.
4227   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
4228                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4229   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
4230                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4231   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4232                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4233                getLLVMStyleWithColumns(60));
4234   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
4235                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
4236                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4237                getLLVMStyleWithColumns(59));
4238   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4239                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4240                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4241 }
4242 
4243 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4244   verifyFormat(
4245       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4246       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4247   verifyFormat(
4248       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4249       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4250 
4251   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4252                "    ccccccccccccccccccccccccc) {\n}");
4253   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4254                "    ccccccccccccccccccccccccc) {\n}");
4255 
4256   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4257                "    ccccccccccccccccccccccccc) {\n}");
4258   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4259                "    ccccccccccccccccccccccccc) {\n}");
4260 
4261   verifyFormat(
4262       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4263       "    ccccccccccccccccccccccccc) {\n}");
4264   verifyFormat(
4265       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4266       "    ccccccccccccccccccccccccc) {\n}");
4267 
4268   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4269                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4270                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4271                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4272   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4273                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4274                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4275                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4276 
4277   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4278                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4279                "    aaaaaaaaaaaaaaa != aa) {\n}");
4280   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4281                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4282                "    aaaaaaaaaaaaaaa != aa) {\n}");
4283 }
4284 
4285 TEST_F(FormatTest, BreaksAfterAssignments) {
4286   verifyFormat(
4287       "unsigned Cost =\n"
4288       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4289       "                        SI->getPointerAddressSpaceee());\n");
4290   verifyFormat(
4291       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4292       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4293 
4294   verifyFormat(
4295       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4296       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4297   verifyFormat("unsigned OriginalStartColumn =\n"
4298                "    SourceMgr.getSpellingColumnNumber(\n"
4299                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4300                "    1;");
4301 }
4302 
4303 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
4304   FormatStyle Style = getLLVMStyle();
4305   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4306                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
4307                Style);
4308 
4309   Style.PenaltyBreakAssignment = 20;
4310   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
4311                "                                 cccccccccccccccccccccccccc;",
4312                Style);
4313 }
4314 
4315 TEST_F(FormatTest, AlignsAfterAssignments) {
4316   verifyFormat(
4317       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4318       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4319   verifyFormat(
4320       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4321       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4322   verifyFormat(
4323       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4324       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4325   verifyFormat(
4326       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4327       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4328   verifyFormat(
4329       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4330       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4331       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4332 }
4333 
4334 TEST_F(FormatTest, AlignsAfterReturn) {
4335   verifyFormat(
4336       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4337       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4338   verifyFormat(
4339       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4340       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4341   verifyFormat(
4342       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4343       "       aaaaaaaaaaaaaaaaaaaaaa();");
4344   verifyFormat(
4345       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4346       "        aaaaaaaaaaaaaaaaaaaaaa());");
4347   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4348                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4349   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4350                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4351                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4352   verifyFormat("return\n"
4353                "    // true if code is one of a or b.\n"
4354                "    code == a || code == b;");
4355 }
4356 
4357 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4358   verifyFormat(
4359       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4360       "                                                aaaaaaaaa aaaaaaa) {}");
4361   verifyFormat(
4362       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4363       "                                               aaaaaaaaaaa aaaaaaaaa);");
4364   verifyFormat(
4365       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4366       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4367   FormatStyle Style = getLLVMStyle();
4368   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4369   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4370                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4371                Style);
4372   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4373                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4374                Style);
4375   verifyFormat("SomeLongVariableName->someFunction(\n"
4376                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4377                Style);
4378   verifyFormat(
4379       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4380       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4381       Style);
4382   verifyFormat(
4383       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4384       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4385       Style);
4386   verifyFormat(
4387       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4388       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4389       Style);
4390 
4391   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
4392                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
4393                "        b));",
4394                Style);
4395 
4396   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4397   Style.BinPackArguments = false;
4398   Style.BinPackParameters = false;
4399   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4400                "    aaaaaaaaaaa aaaaaaaa,\n"
4401                "    aaaaaaaaa aaaaaaa,\n"
4402                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4403                Style);
4404   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4405                "    aaaaaaaaaaa aaaaaaaaa,\n"
4406                "    aaaaaaaaaaa aaaaaaaaa,\n"
4407                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4408                Style);
4409   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
4410                "    aaaaaaaaaaaaaaa,\n"
4411                "    aaaaaaaaaaaaaaaaaaaaa,\n"
4412                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4413                Style);
4414   verifyFormat(
4415       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
4416       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4417       Style);
4418   verifyFormat(
4419       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
4420       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
4421       Style);
4422   verifyFormat(
4423       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4424       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4425       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
4426       "    aaaaaaaaaaaaaaaa);",
4427       Style);
4428   verifyFormat(
4429       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
4430       "    aaaaaaaaaaaaaaaaaaaaa(\n"
4431       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
4432       "    aaaaaaaaaaaaaaaa);",
4433       Style);
4434 }
4435 
4436 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4437   FormatStyle Style = getLLVMStyleWithColumns(40);
4438   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4439                "          bbbbbbbbbbbbbbbbbbbbbb);",
4440                Style);
4441   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
4442   Style.AlignOperands = false;
4443   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4444                "          bbbbbbbbbbbbbbbbbbbbbb);",
4445                Style);
4446   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4447   Style.AlignOperands = true;
4448   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4449                "          bbbbbbbbbbbbbbbbbbbbbb);",
4450                Style);
4451   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4452   Style.AlignOperands = false;
4453   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4454                "    bbbbbbbbbbbbbbbbbbbbbb);",
4455                Style);
4456 }
4457 
4458 TEST_F(FormatTest, BreaksConditionalExpressions) {
4459   verifyFormat(
4460       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4461       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4462       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4463   verifyFormat(
4464       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4465       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4466       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4467   verifyFormat(
4468       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4469       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4470   verifyFormat(
4471       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4472       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4473       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4474   verifyFormat(
4475       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4476       "                                                    : aaaaaaaaaaaaa);");
4477   verifyFormat(
4478       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4479       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4480       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4481       "                   aaaaaaaaaaaaa);");
4482   verifyFormat(
4483       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4484       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4485       "                   aaaaaaaaaaaaa);");
4486   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4487                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4488                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4489                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4490                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4491   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4492                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4493                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4494                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4495                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4496                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4497                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4498   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4499                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4500                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4501                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4502                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4503   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4504                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4505                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4506   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4507                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4508                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4509                "        : aaaaaaaaaaaaaaaa;");
4510   verifyFormat(
4511       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4512       "    ? aaaaaaaaaaaaaaa\n"
4513       "    : aaaaaaaaaaaaaaa;");
4514   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4515                "          aaaaaaaaa\n"
4516                "      ? b\n"
4517                "      : c);");
4518   verifyFormat("return aaaa == bbbb\n"
4519                "           // comment\n"
4520                "           ? aaaa\n"
4521                "           : bbbb;");
4522   verifyFormat("unsigned Indent =\n"
4523                "    format(TheLine.First,\n"
4524                "           IndentForLevel[TheLine.Level] >= 0\n"
4525                "               ? IndentForLevel[TheLine.Level]\n"
4526                "               : TheLine * 2,\n"
4527                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4528                getLLVMStyleWithColumns(60));
4529   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4530                "                  ? aaaaaaaaaaaaaaa\n"
4531                "                  : bbbbbbbbbbbbbbb //\n"
4532                "                        ? ccccccccccccccc\n"
4533                "                        : ddddddddddddddd;");
4534   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4535                "                  ? aaaaaaaaaaaaaaa\n"
4536                "                  : (bbbbbbbbbbbbbbb //\n"
4537                "                         ? ccccccccccccccc\n"
4538                "                         : ddddddddddddddd);");
4539   verifyFormat(
4540       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4541       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4542       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4543       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4544       "                                      : aaaaaaaaaa;");
4545   verifyFormat(
4546       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4547       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4548       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4549 
4550   FormatStyle NoBinPacking = getLLVMStyle();
4551   NoBinPacking.BinPackArguments = false;
4552   verifyFormat(
4553       "void f() {\n"
4554       "  g(aaa,\n"
4555       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4556       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4557       "        ? aaaaaaaaaaaaaaa\n"
4558       "        : aaaaaaaaaaaaaaa);\n"
4559       "}",
4560       NoBinPacking);
4561   verifyFormat(
4562       "void f() {\n"
4563       "  g(aaa,\n"
4564       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4565       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4566       "        ?: aaaaaaaaaaaaaaa);\n"
4567       "}",
4568       NoBinPacking);
4569 
4570   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4571                "             // comment.\n"
4572                "             ccccccccccccccccccccccccccccccccccccccc\n"
4573                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4574                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4575 
4576   // Assignments in conditional expressions. Apparently not uncommon :-(.
4577   verifyFormat("return a != b\n"
4578                "           // comment\n"
4579                "           ? a = b\n"
4580                "           : a = b;");
4581   verifyFormat("return a != b\n"
4582                "           // comment\n"
4583                "           ? a = a != b\n"
4584                "                     // comment\n"
4585                "                     ? a = b\n"
4586                "                     : a\n"
4587                "           : a;\n");
4588   verifyFormat("return a != b\n"
4589                "           // comment\n"
4590                "           ? a\n"
4591                "           : a = a != b\n"
4592                "                     // comment\n"
4593                "                     ? a = b\n"
4594                "                     : a;");
4595 }
4596 
4597 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4598   FormatStyle Style = getLLVMStyle();
4599   Style.BreakBeforeTernaryOperators = false;
4600   Style.ColumnLimit = 70;
4601   verifyFormat(
4602       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4603       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4604       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4605       Style);
4606   verifyFormat(
4607       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4608       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4609       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4610       Style);
4611   verifyFormat(
4612       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4613       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4614       Style);
4615   verifyFormat(
4616       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4617       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4618       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4619       Style);
4620   verifyFormat(
4621       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4622       "                                                      aaaaaaaaaaaaa);",
4623       Style);
4624   verifyFormat(
4625       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4626       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4627       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4628       "                   aaaaaaaaaaaaa);",
4629       Style);
4630   verifyFormat(
4631       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4632       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4633       "                   aaaaaaaaaaaaa);",
4634       Style);
4635   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4636                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4637                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4638                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4639                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4640                Style);
4641   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4642                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4643                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4644                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4645                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4646                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4647                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4648                Style);
4649   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4650                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4651                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4652                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4653                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4654                Style);
4655   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4656                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4657                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4658                Style);
4659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4660                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4661                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4662                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4663                Style);
4664   verifyFormat(
4665       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4666       "    aaaaaaaaaaaaaaa :\n"
4667       "    aaaaaaaaaaaaaaa;",
4668       Style);
4669   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4670                "          aaaaaaaaa ?\n"
4671                "      b :\n"
4672                "      c);",
4673                Style);
4674   verifyFormat("unsigned Indent =\n"
4675                "    format(TheLine.First,\n"
4676                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4677                "               IndentForLevel[TheLine.Level] :\n"
4678                "               TheLine * 2,\n"
4679                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4680                Style);
4681   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4682                "                  aaaaaaaaaaaaaaa :\n"
4683                "                  bbbbbbbbbbbbbbb ? //\n"
4684                "                      ccccccccccccccc :\n"
4685                "                      ddddddddddddddd;",
4686                Style);
4687   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4688                "                  aaaaaaaaaaaaaaa :\n"
4689                "                  (bbbbbbbbbbbbbbb ? //\n"
4690                "                       ccccccccccccccc :\n"
4691                "                       ddddddddddddddd);",
4692                Style);
4693   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4694                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4695                "            ccccccccccccccccccccccccccc;",
4696                Style);
4697   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4698                "           aaaaa :\n"
4699                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4700                Style);
4701 }
4702 
4703 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4704   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4705                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4706   verifyFormat("bool a = true, b = false;");
4707 
4708   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4709                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4710                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4711                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4712   verifyFormat(
4713       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4714       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4715       "     d = e && f;");
4716   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4717                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4718   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4719                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4720   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4721                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4722 
4723   FormatStyle Style = getGoogleStyle();
4724   Style.PointerAlignment = FormatStyle::PAS_Left;
4725   Style.DerivePointerAlignment = false;
4726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4727                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4728                "    *b = bbbbbbbbbbbbbbbbbbb;",
4729                Style);
4730   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4731                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4732                Style);
4733   verifyFormat("vector<int*> a, b;", Style);
4734   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4735 }
4736 
4737 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4738   verifyFormat("arr[foo ? bar : baz];");
4739   verifyFormat("f()[foo ? bar : baz];");
4740   verifyFormat("(a + b)[foo ? bar : baz];");
4741   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4742 }
4743 
4744 TEST_F(FormatTest, AlignsStringLiterals) {
4745   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4746                "                                      \"short literal\");");
4747   verifyFormat(
4748       "looooooooooooooooooooooooongFunction(\n"
4749       "    \"short literal\"\n"
4750       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4751   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4752                "             \" string literals\",\n"
4753                "             and, other, parameters);");
4754   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4755             "      \"5678\";",
4756             format("fun + \"1243\" /* comment */\n"
4757                    "    \"5678\";",
4758                    getLLVMStyleWithColumns(28)));
4759   EXPECT_EQ(
4760       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4761       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4762       "         \"aaaaaaaaaaaaaaaa\";",
4763       format("aaaaaa ="
4764              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4765              "aaaaaaaaaaaaaaaaaaaaa\" "
4766              "\"aaaaaaaaaaaaaaaa\";"));
4767   verifyFormat("a = a + \"a\"\n"
4768                "        \"a\"\n"
4769                "        \"a\";");
4770   verifyFormat("f(\"a\", \"b\"\n"
4771                "       \"c\");");
4772 
4773   verifyFormat(
4774       "#define LL_FORMAT \"ll\"\n"
4775       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4776       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4777 
4778   verifyFormat("#define A(X)          \\\n"
4779                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4780                "  \"ccccc\"",
4781                getLLVMStyleWithColumns(23));
4782   verifyFormat("#define A \"def\"\n"
4783                "f(\"abc\" A \"ghi\"\n"
4784                "  \"jkl\");");
4785 
4786   verifyFormat("f(L\"a\"\n"
4787                "  L\"b\");");
4788   verifyFormat("#define A(X)            \\\n"
4789                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4790                "  L\"ccccc\"",
4791                getLLVMStyleWithColumns(25));
4792 
4793   verifyFormat("f(@\"a\"\n"
4794                "  @\"b\");");
4795   verifyFormat("NSString s = @\"a\"\n"
4796                "             @\"b\"\n"
4797                "             @\"c\";");
4798   verifyFormat("NSString s = @\"a\"\n"
4799                "              \"b\"\n"
4800                "              \"c\";");
4801 }
4802 
4803 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4804   FormatStyle Style = getLLVMStyle();
4805   // No declarations or definitions should be moved to own line.
4806   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4807   verifyFormat("class A {\n"
4808                "  int f() { return 1; }\n"
4809                "  int g();\n"
4810                "};\n"
4811                "int f() { return 1; }\n"
4812                "int g();\n",
4813                Style);
4814 
4815   // All declarations and definitions should have the return type moved to its
4816   // own
4817   // line.
4818   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4819   verifyFormat("class E {\n"
4820                "  int\n"
4821                "  f() {\n"
4822                "    return 1;\n"
4823                "  }\n"
4824                "  int\n"
4825                "  g();\n"
4826                "};\n"
4827                "int\n"
4828                "f() {\n"
4829                "  return 1;\n"
4830                "}\n"
4831                "int\n"
4832                "g();\n",
4833                Style);
4834 
4835   // Top-level definitions, and no kinds of declarations should have the
4836   // return type moved to its own line.
4837   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4838   verifyFormat("class B {\n"
4839                "  int f() { return 1; }\n"
4840                "  int g();\n"
4841                "};\n"
4842                "int\n"
4843                "f() {\n"
4844                "  return 1;\n"
4845                "}\n"
4846                "int g();\n",
4847                Style);
4848 
4849   // Top-level definitions and declarations should have the return type moved
4850   // to its own line.
4851   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4852   verifyFormat("class C {\n"
4853                "  int f() { return 1; }\n"
4854                "  int g();\n"
4855                "};\n"
4856                "int\n"
4857                "f() {\n"
4858                "  return 1;\n"
4859                "}\n"
4860                "int\n"
4861                "g();\n",
4862                Style);
4863 
4864   // All definitions should have the return type moved to its own line, but no
4865   // kinds of declarations.
4866   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4867   verifyFormat("class D {\n"
4868                "  int\n"
4869                "  f() {\n"
4870                "    return 1;\n"
4871                "  }\n"
4872                "  int g();\n"
4873                "};\n"
4874                "int\n"
4875                "f() {\n"
4876                "  return 1;\n"
4877                "}\n"
4878                "int g();\n",
4879                Style);
4880   verifyFormat("const char *\n"
4881                "f(void) {\n" // Break here.
4882                "  return \"\";\n"
4883                "}\n"
4884                "const char *bar(void);\n", // No break here.
4885                Style);
4886   verifyFormat("template <class T>\n"
4887                "T *\n"
4888                "f(T &c) {\n" // Break here.
4889                "  return NULL;\n"
4890                "}\n"
4891                "template <class T> T *f(T &c);\n", // No break here.
4892                Style);
4893   verifyFormat("class C {\n"
4894                "  int\n"
4895                "  operator+() {\n"
4896                "    return 1;\n"
4897                "  }\n"
4898                "  int\n"
4899                "  operator()() {\n"
4900                "    return 1;\n"
4901                "  }\n"
4902                "};\n",
4903                Style);
4904   verifyFormat("void\n"
4905                "A::operator()() {}\n"
4906                "void\n"
4907                "A::operator>>() {}\n"
4908                "void\n"
4909                "A::operator+() {}\n",
4910                Style);
4911   verifyFormat("void *operator new(std::size_t s);", // No break here.
4912                Style);
4913   verifyFormat("void *\n"
4914                "operator new(std::size_t s) {}",
4915                Style);
4916   verifyFormat("void *\n"
4917                "operator delete[](void *ptr) {}",
4918                Style);
4919   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4920   verifyFormat("const char *\n"
4921                "f(void)\n" // Break here.
4922                "{\n"
4923                "  return \"\";\n"
4924                "}\n"
4925                "const char *bar(void);\n", // No break here.
4926                Style);
4927   verifyFormat("template <class T>\n"
4928                "T *\n"     // Problem here: no line break
4929                "f(T &c)\n" // Break here.
4930                "{\n"
4931                "  return NULL;\n"
4932                "}\n"
4933                "template <class T> T *f(T &c);\n", // No break here.
4934                Style);
4935 }
4936 
4937 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4938   FormatStyle NoBreak = getLLVMStyle();
4939   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4940   FormatStyle Break = getLLVMStyle();
4941   Break.AlwaysBreakBeforeMultilineStrings = true;
4942   verifyFormat("aaaa = \"bbbb\"\n"
4943                "       \"cccc\";",
4944                NoBreak);
4945   verifyFormat("aaaa =\n"
4946                "    \"bbbb\"\n"
4947                "    \"cccc\";",
4948                Break);
4949   verifyFormat("aaaa(\"bbbb\"\n"
4950                "     \"cccc\");",
4951                NoBreak);
4952   verifyFormat("aaaa(\n"
4953                "    \"bbbb\"\n"
4954                "    \"cccc\");",
4955                Break);
4956   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4957                "          \"cccc\");",
4958                NoBreak);
4959   verifyFormat("aaaa(qqq,\n"
4960                "     \"bbbb\"\n"
4961                "     \"cccc\");",
4962                Break);
4963   verifyFormat("aaaa(qqq,\n"
4964                "     L\"bbbb\"\n"
4965                "     L\"cccc\");",
4966                Break);
4967   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4968                "                      \"bbbb\"));",
4969                Break);
4970   verifyFormat("string s = someFunction(\n"
4971                "    \"abc\"\n"
4972                "    \"abc\");",
4973                Break);
4974 
4975   // As we break before unary operators, breaking right after them is bad.
4976   verifyFormat("string foo = abc ? \"x\"\n"
4977                "                   \"blah blah blah blah blah blah\"\n"
4978                "                 : \"y\";",
4979                Break);
4980 
4981   // Don't break if there is no column gain.
4982   verifyFormat("f(\"aaaa\"\n"
4983                "  \"bbbb\");",
4984                Break);
4985 
4986   // Treat literals with escaped newlines like multi-line string literals.
4987   EXPECT_EQ("x = \"a\\\n"
4988             "b\\\n"
4989             "c\";",
4990             format("x = \"a\\\n"
4991                    "b\\\n"
4992                    "c\";",
4993                    NoBreak));
4994   EXPECT_EQ("xxxx =\n"
4995             "    \"a\\\n"
4996             "b\\\n"
4997             "c\";",
4998             format("xxxx = \"a\\\n"
4999                    "b\\\n"
5000                    "c\";",
5001                    Break));
5002 
5003   EXPECT_EQ("NSString *const kString =\n"
5004             "    @\"aaaa\"\n"
5005             "    @\"bbbb\";",
5006             format("NSString *const kString = @\"aaaa\"\n"
5007                    "@\"bbbb\";",
5008                    Break));
5009 
5010   Break.ColumnLimit = 0;
5011   verifyFormat("const char *hello = \"hello llvm\";", Break);
5012 }
5013 
5014 TEST_F(FormatTest, AlignsPipes) {
5015   verifyFormat(
5016       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5017       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5018       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5019   verifyFormat(
5020       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
5021       "                     << aaaaaaaaaaaaaaaaaaaa;");
5022   verifyFormat(
5023       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5024       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5025   verifyFormat(
5026       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
5027       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5028   verifyFormat(
5029       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
5030       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
5031       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
5032   verifyFormat(
5033       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5034       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5035       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5036   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5037                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5038                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5039                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5040   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
5041                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
5042   verifyFormat(
5043       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5044       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5045   verifyFormat(
5046       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
5047       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
5048 
5049   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
5050                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
5051   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5052                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5053                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
5054                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
5055   verifyFormat("LOG_IF(aaa == //\n"
5056                "       bbb)\n"
5057                "    << a << b;");
5058 
5059   // But sometimes, breaking before the first "<<" is desirable.
5060   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5061                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
5062   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
5063                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5064                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5065   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
5066                "    << BEF << IsTemplate << Description << E->getType();");
5067   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5068                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5069                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5070   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
5071                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5072                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5073                "    << aaa;");
5074 
5075   verifyFormat(
5076       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5077       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5078 
5079   // Incomplete string literal.
5080   EXPECT_EQ("llvm::errs() << \"\n"
5081             "             << a;",
5082             format("llvm::errs() << \"\n<<a;"));
5083 
5084   verifyFormat("void f() {\n"
5085                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
5086                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
5087                "}");
5088 
5089   // Handle 'endl'.
5090   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
5091                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5092   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
5093 
5094   // Handle '\n'.
5095   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
5096                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5097   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
5098                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
5099   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
5100                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
5101   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
5102 }
5103 
5104 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
5105   verifyFormat("return out << \"somepacket = {\\n\"\n"
5106                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
5107                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
5108                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
5109                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
5110                "           << \"}\";");
5111 
5112   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5113                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
5114                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
5115   verifyFormat(
5116       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
5117       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
5118       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
5119       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
5120       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
5121   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
5122                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5123   verifyFormat(
5124       "void f() {\n"
5125       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
5126       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
5127       "}");
5128 
5129   // Breaking before the first "<<" is generally not desirable.
5130   verifyFormat(
5131       "llvm::errs()\n"
5132       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5133       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5134       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5135       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5136       getLLVMStyleWithColumns(70));
5137   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5138                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5139                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5140                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5141                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
5142                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
5143                getLLVMStyleWithColumns(70));
5144 
5145   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5146                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
5147                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
5148   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5149                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
5150                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
5151   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
5152                "           (aaaa + aaaa);",
5153                getLLVMStyleWithColumns(40));
5154   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
5155                "                  (aaaaaaa + aaaaa));",
5156                getLLVMStyleWithColumns(40));
5157   verifyFormat(
5158       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
5159       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
5160       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
5161 }
5162 
5163 TEST_F(FormatTest, UnderstandsEquals) {
5164   verifyFormat(
5165       "aaaaaaaaaaaaaaaaa =\n"
5166       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5167   verifyFormat(
5168       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5169       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5170   verifyFormat(
5171       "if (a) {\n"
5172       "  f();\n"
5173       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5174       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
5175       "}");
5176 
5177   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5178                "        100000000 + 10000000) {\n}");
5179 }
5180 
5181 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
5182   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5183                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
5184 
5185   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
5186                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
5187 
5188   verifyFormat(
5189       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
5190       "                                                          Parameter2);");
5191 
5192   verifyFormat(
5193       "ShortObject->shortFunction(\n"
5194       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
5195       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
5196 
5197   verifyFormat("loooooooooooooongFunction(\n"
5198                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
5199 
5200   verifyFormat(
5201       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
5202       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
5203 
5204   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5205                "    .WillRepeatedly(Return(SomeValue));");
5206   verifyFormat("void f() {\n"
5207                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
5208                "      .Times(2)\n"
5209                "      .WillRepeatedly(Return(SomeValue));\n"
5210                "}");
5211   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
5212                "    ccccccccccccccccccccccc);");
5213   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5214                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5215                "          .aaaaa(aaaaa),\n"
5216                "      aaaaaaaaaaaaaaaaaaaaa);");
5217   verifyFormat("void f() {\n"
5218                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5219                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
5220                "}");
5221   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5222                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5223                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5224                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5225                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5226   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5227                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5228                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5229                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
5230                "}");
5231 
5232   // Here, it is not necessary to wrap at "." or "->".
5233   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
5234                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
5235   verifyFormat(
5236       "aaaaaaaaaaa->aaaaaaaaa(\n"
5237       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5238       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
5239 
5240   verifyFormat(
5241       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5242       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
5243   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
5244                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5245   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
5246                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
5247 
5248   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5249                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5250                "    .a();");
5251 
5252   FormatStyle NoBinPacking = getLLVMStyle();
5253   NoBinPacking.BinPackParameters = false;
5254   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5255                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
5256                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
5257                "                         aaaaaaaaaaaaaaaaaaa,\n"
5258                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
5259                NoBinPacking);
5260 
5261   // If there is a subsequent call, change to hanging indentation.
5262   verifyFormat(
5263       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5264       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
5265       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5266   verifyFormat(
5267       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5268       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
5269   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5270                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5271                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5272   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5273                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
5274                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5275 }
5276 
5277 TEST_F(FormatTest, WrapsTemplateDeclarations) {
5278   verifyFormat("template <typename T>\n"
5279                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5280   verifyFormat("template <typename T>\n"
5281                "// T should be one of {A, B}.\n"
5282                "virtual void loooooooooooongFunction(int Param1, int Param2);");
5283   verifyFormat(
5284       "template <typename T>\n"
5285       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
5286   verifyFormat("template <typename T>\n"
5287                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
5288                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
5289   verifyFormat(
5290       "template <typename T>\n"
5291       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
5292       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
5293   verifyFormat(
5294       "template <typename T>\n"
5295       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5296       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5297       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5298   verifyFormat("template <typename T>\n"
5299                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5300                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5301   verifyFormat(
5302       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5303       "          typename T4 = char>\n"
5304       "void f();");
5305   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5306                "          template <typename> class cccccccccccccccccccccc,\n"
5307                "          typename ddddddddddddd>\n"
5308                "class C {};");
5309   verifyFormat(
5310       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5311       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5312 
5313   verifyFormat("void f() {\n"
5314                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5315                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5316                "}");
5317 
5318   verifyFormat("template <typename T> class C {};");
5319   verifyFormat("template <typename T> void f();");
5320   verifyFormat("template <typename T> void f() {}");
5321   verifyFormat(
5322       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5323       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5324       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5325       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5326       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5327       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5328       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5329       getLLVMStyleWithColumns(72));
5330   EXPECT_EQ("static_cast<A< //\n"
5331             "    B> *>(\n"
5332             "\n"
5333             ");",
5334             format("static_cast<A<//\n"
5335                    "    B>*>(\n"
5336                    "\n"
5337                    "    );"));
5338   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5339                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5340 
5341   FormatStyle AlwaysBreak = getLLVMStyle();
5342   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5343   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5344   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5345   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5346   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5347                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5348                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5349   verifyFormat("template <template <typename> class Fooooooo,\n"
5350                "          template <typename> class Baaaaaaar>\n"
5351                "struct C {};",
5352                AlwaysBreak);
5353   verifyFormat("template <typename T> // T can be A, B or C.\n"
5354                "struct C {};",
5355                AlwaysBreak);
5356   verifyFormat("template <enum E> class A {\n"
5357                "public:\n"
5358                "  E *f();\n"
5359                "};");
5360 }
5361 
5362 TEST_F(FormatTest, WrapsTemplateParameters) {
5363   FormatStyle Style = getLLVMStyle();
5364   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5365   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5366   verifyFormat(
5367       "template <typename... a> struct q {};\n"
5368       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5369       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5370       "    y;",
5371       Style);
5372   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
5373   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5374   verifyFormat(
5375       "template <typename... a> struct r {};\n"
5376       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
5377       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
5378       "    y;",
5379       Style);
5380   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5381   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
5382   verifyFormat(
5383       "template <typename... a> struct s {};\n"
5384       "extern s<\n"
5385       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5386       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5387       "    y;",
5388       Style);
5389   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
5390   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
5391   verifyFormat(
5392       "template <typename... a> struct t {};\n"
5393       "extern t<\n"
5394       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
5395       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
5396       "    y;",
5397       Style);
5398 }
5399 
5400 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5401   verifyFormat(
5402       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5403       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5404   verifyFormat(
5405       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5406       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5407       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5408 
5409   // FIXME: Should we have the extra indent after the second break?
5410   verifyFormat(
5411       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5412       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5413       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5414 
5415   verifyFormat(
5416       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5417       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5418 
5419   // Breaking at nested name specifiers is generally not desirable.
5420   verifyFormat(
5421       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5422       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5423 
5424   verifyFormat(
5425       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
5426       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5427       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5428       "                   aaaaaaaaaaaaaaaaaaaaa);",
5429       getLLVMStyleWithColumns(74));
5430 
5431   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5432                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5433                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5434 }
5435 
5436 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5437   verifyFormat("A<int> a;");
5438   verifyFormat("A<A<A<int>>> a;");
5439   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5440   verifyFormat("bool x = a < 1 || 2 > a;");
5441   verifyFormat("bool x = 5 < f<int>();");
5442   verifyFormat("bool x = f<int>() > 5;");
5443   verifyFormat("bool x = 5 < a<int>::x;");
5444   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5445   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5446 
5447   verifyGoogleFormat("A<A<int>> a;");
5448   verifyGoogleFormat("A<A<A<int>>> a;");
5449   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5450   verifyGoogleFormat("A<A<int> > a;");
5451   verifyGoogleFormat("A<A<A<int> > > a;");
5452   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5453   verifyGoogleFormat("A<::A<int>> a;");
5454   verifyGoogleFormat("A<::A> a;");
5455   verifyGoogleFormat("A< ::A> a;");
5456   verifyGoogleFormat("A< ::A<int> > a;");
5457   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5458   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5459   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5460   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5461   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
5462             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5463 
5464   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5465 
5466   verifyFormat("test >> a >> b;");
5467   verifyFormat("test << a >> b;");
5468 
5469   verifyFormat("f<int>();");
5470   verifyFormat("template <typename T> void f() {}");
5471   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5472   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5473                "sizeof(char)>::type>;");
5474   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5475   verifyFormat("f(a.operator()<A>());");
5476   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5477                "      .template operator()<A>());",
5478                getLLVMStyleWithColumns(35));
5479 
5480   // Not template parameters.
5481   verifyFormat("return a < b && c > d;");
5482   verifyFormat("void f() {\n"
5483                "  while (a < b && c > d) {\n"
5484                "  }\n"
5485                "}");
5486   verifyFormat("template <typename... Types>\n"
5487                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5488 
5489   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5490                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5491                getLLVMStyleWithColumns(60));
5492   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5493   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5494   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5495 }
5496 
5497 TEST_F(FormatTest, BitshiftOperatorWidth) {
5498   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5499             "                   bar */",
5500             format("int    a=1<<2;  /* foo\n"
5501                    "                   bar */"));
5502 
5503   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5504             "                     bar */",
5505             format("int  b  =256>>1 ;  /* foo\n"
5506                    "                      bar */"));
5507 }
5508 
5509 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5510   verifyFormat("COMPARE(a, ==, b);");
5511   verifyFormat("auto s = sizeof...(Ts) - 1;");
5512 }
5513 
5514 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5515   verifyFormat("int A::*x;");
5516   verifyFormat("int (S::*func)(void *);");
5517   verifyFormat("void f() { int (S::*func)(void *); }");
5518   verifyFormat("typedef bool *(Class::*Member)() const;");
5519   verifyFormat("void f() {\n"
5520                "  (a->*f)();\n"
5521                "  a->*x;\n"
5522                "  (a.*f)();\n"
5523                "  ((*a).*f)();\n"
5524                "  a.*x;\n"
5525                "}");
5526   verifyFormat("void f() {\n"
5527                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5528                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5529                "}");
5530   verifyFormat(
5531       "(aaaaaaaaaa->*bbbbbbb)(\n"
5532       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5533   FormatStyle Style = getLLVMStyle();
5534   Style.PointerAlignment = FormatStyle::PAS_Left;
5535   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5536 }
5537 
5538 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5539   verifyFormat("int a = -2;");
5540   verifyFormat("f(-1, -2, -3);");
5541   verifyFormat("a[-1] = 5;");
5542   verifyFormat("int a = 5 + -2;");
5543   verifyFormat("if (i == -1) {\n}");
5544   verifyFormat("if (i != -1) {\n}");
5545   verifyFormat("if (i > -1) {\n}");
5546   verifyFormat("if (i < -1) {\n}");
5547   verifyFormat("++(a->f());");
5548   verifyFormat("--(a->f());");
5549   verifyFormat("(a->f())++;");
5550   verifyFormat("a[42]++;");
5551   verifyFormat("if (!(a->f())) {\n}");
5552 
5553   verifyFormat("a-- > b;");
5554   verifyFormat("b ? -a : c;");
5555   verifyFormat("n * sizeof char16;");
5556   verifyFormat("n * alignof char16;", getGoogleStyle());
5557   verifyFormat("sizeof(char);");
5558   verifyFormat("alignof(char);", getGoogleStyle());
5559 
5560   verifyFormat("return -1;");
5561   verifyFormat("switch (a) {\n"
5562                "case -1:\n"
5563                "  break;\n"
5564                "}");
5565   verifyFormat("#define X -1");
5566   verifyFormat("#define X -kConstant");
5567 
5568   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5569   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5570 
5571   verifyFormat("int a = /* confusing comment */ -1;");
5572   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5573   verifyFormat("int a = i /* confusing comment */++;");
5574 }
5575 
5576 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5577   verifyFormat("if (!aaaaaaaaaa( // break\n"
5578                "        aaaaa)) {\n"
5579                "}");
5580   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5581                "    aaaaa));");
5582   verifyFormat("*aaa = aaaaaaa( // break\n"
5583                "    bbbbbb);");
5584 }
5585 
5586 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5587   verifyFormat("bool operator<();");
5588   verifyFormat("bool operator>();");
5589   verifyFormat("bool operator=();");
5590   verifyFormat("bool operator==();");
5591   verifyFormat("bool operator!=();");
5592   verifyFormat("int operator+();");
5593   verifyFormat("int operator++();");
5594   verifyFormat("int operator++(int) volatile noexcept;");
5595   verifyFormat("bool operator,();");
5596   verifyFormat("bool operator();");
5597   verifyFormat("bool operator()();");
5598   verifyFormat("bool operator[]();");
5599   verifyFormat("operator bool();");
5600   verifyFormat("operator int();");
5601   verifyFormat("operator void *();");
5602   verifyFormat("operator SomeType<int>();");
5603   verifyFormat("operator SomeType<int, int>();");
5604   verifyFormat("operator SomeType<SomeType<int>>();");
5605   verifyFormat("void *operator new(std::size_t size);");
5606   verifyFormat("void *operator new[](std::size_t size);");
5607   verifyFormat("void operator delete(void *ptr);");
5608   verifyFormat("void operator delete[](void *ptr);");
5609   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5610                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5611   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5612                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5613 
5614   verifyFormat(
5615       "ostream &operator<<(ostream &OutputStream,\n"
5616       "                    SomeReallyLongType WithSomeReallyLongValue);");
5617   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5618                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5619                "  return left.group < right.group;\n"
5620                "}");
5621   verifyFormat("SomeType &operator=(const SomeType &S);");
5622   verifyFormat("f.template operator()<int>();");
5623 
5624   verifyGoogleFormat("operator void*();");
5625   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5626   verifyGoogleFormat("operator ::A();");
5627 
5628   verifyFormat("using A::operator+;");
5629   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5630                "int i;");
5631 }
5632 
5633 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5634   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5635   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5636   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5637   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5638   verifyFormat("Deleted &operator=(const Deleted &) &;");
5639   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5640   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5641   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5642   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5643   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5644   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5645   verifyFormat("void Fn(T const &) const &;");
5646   verifyFormat("void Fn(T const volatile &&) const volatile &&;");
5647   verifyFormat("template <typename T>\n"
5648                "void F(T) && = delete;",
5649                getGoogleStyle());
5650 
5651   FormatStyle AlignLeft = getLLVMStyle();
5652   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5653   verifyFormat("void A::b() && {}", AlignLeft);
5654   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5655   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5656                AlignLeft);
5657   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5658   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5659   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5660   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5661   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5662   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5663   verifyFormat("void Fn(T const&) const&;", AlignLeft);
5664   verifyFormat("void Fn(T const volatile&&) const volatile&&;", AlignLeft);
5665 
5666   FormatStyle Spaces = getLLVMStyle();
5667   Spaces.SpacesInCStyleCastParentheses = true;
5668   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5669   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5670   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5671   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5672 
5673   Spaces.SpacesInCStyleCastParentheses = false;
5674   Spaces.SpacesInParentheses = true;
5675   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5676   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5677   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5678   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5679 }
5680 
5681 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5682   verifyFormat("void f() {\n"
5683                "  A *a = new A;\n"
5684                "  A *a = new (placement) A;\n"
5685                "  delete a;\n"
5686                "  delete (A *)a;\n"
5687                "}");
5688   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5689                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5690   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5691                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5692                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5693   verifyFormat("delete[] h->p;");
5694 }
5695 
5696 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5697   verifyFormat("int *f(int *a) {}");
5698   verifyFormat("int main(int argc, char **argv) {}");
5699   verifyFormat("Test::Test(int b) : a(b * b) {}");
5700   verifyIndependentOfContext("f(a, *a);");
5701   verifyFormat("void g() { f(*a); }");
5702   verifyIndependentOfContext("int a = b * 10;");
5703   verifyIndependentOfContext("int a = 10 * b;");
5704   verifyIndependentOfContext("int a = b * c;");
5705   verifyIndependentOfContext("int a += b * c;");
5706   verifyIndependentOfContext("int a -= b * c;");
5707   verifyIndependentOfContext("int a *= b * c;");
5708   verifyIndependentOfContext("int a /= b * c;");
5709   verifyIndependentOfContext("int a = *b;");
5710   verifyIndependentOfContext("int a = *b * c;");
5711   verifyIndependentOfContext("int a = b * *c;");
5712   verifyIndependentOfContext("int a = b * (10);");
5713   verifyIndependentOfContext("S << b * (10);");
5714   verifyIndependentOfContext("return 10 * b;");
5715   verifyIndependentOfContext("return *b * *c;");
5716   verifyIndependentOfContext("return a & ~b;");
5717   verifyIndependentOfContext("f(b ? *c : *d);");
5718   verifyIndependentOfContext("int a = b ? *c : *d;");
5719   verifyIndependentOfContext("*b = a;");
5720   verifyIndependentOfContext("a * ~b;");
5721   verifyIndependentOfContext("a * !b;");
5722   verifyIndependentOfContext("a * +b;");
5723   verifyIndependentOfContext("a * -b;");
5724   verifyIndependentOfContext("a * ++b;");
5725   verifyIndependentOfContext("a * --b;");
5726   verifyIndependentOfContext("a[4] * b;");
5727   verifyIndependentOfContext("a[a * a] = 1;");
5728   verifyIndependentOfContext("f() * b;");
5729   verifyIndependentOfContext("a * [self dostuff];");
5730   verifyIndependentOfContext("int x = a * (a + b);");
5731   verifyIndependentOfContext("(a *)(a + b);");
5732   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5733   verifyIndependentOfContext("int *pa = (int *)&a;");
5734   verifyIndependentOfContext("return sizeof(int **);");
5735   verifyIndependentOfContext("return sizeof(int ******);");
5736   verifyIndependentOfContext("return (int **&)a;");
5737   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5738   verifyFormat("void f(Type (*parameter)[10]) {}");
5739   verifyFormat("void f(Type (&parameter)[10]) {}");
5740   verifyGoogleFormat("return sizeof(int**);");
5741   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5742   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5743   verifyFormat("auto a = [](int **&, int ***) {};");
5744   verifyFormat("auto PointerBinding = [](const char *S) {};");
5745   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5746   verifyFormat("[](const decltype(*a) &value) {}");
5747   verifyFormat("decltype(a * b) F();");
5748   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5749   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5750   verifyIndependentOfContext("typedef void (*f)(int *a);");
5751   verifyIndependentOfContext("int i{a * b};");
5752   verifyIndependentOfContext("aaa && aaa->f();");
5753   verifyIndependentOfContext("int x = ~*p;");
5754   verifyFormat("Constructor() : a(a), area(width * height) {}");
5755   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5756   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5757   verifyFormat("void f() { f(a, c * d); }");
5758   verifyFormat("void f() { f(new a(), c * d); }");
5759   verifyFormat("void f(const MyOverride &override);");
5760   verifyFormat("void f(const MyFinal &final);");
5761   verifyIndependentOfContext("bool a = f() && override.f();");
5762   verifyIndependentOfContext("bool a = f() && final.f();");
5763 
5764   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5765 
5766   verifyIndependentOfContext("A<int *> a;");
5767   verifyIndependentOfContext("A<int **> a;");
5768   verifyIndependentOfContext("A<int *, int *> a;");
5769   verifyIndependentOfContext("A<int *[]> a;");
5770   verifyIndependentOfContext(
5771       "const char *const p = reinterpret_cast<const char *const>(q);");
5772   verifyIndependentOfContext("A<int **, int **> a;");
5773   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5774   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5775   verifyFormat("for (; a && b;) {\n}");
5776   verifyFormat("bool foo = true && [] { return false; }();");
5777 
5778   verifyFormat(
5779       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5780       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5781 
5782   verifyGoogleFormat("int const* a = &b;");
5783   verifyGoogleFormat("**outparam = 1;");
5784   verifyGoogleFormat("*outparam = a * b;");
5785   verifyGoogleFormat("int main(int argc, char** argv) {}");
5786   verifyGoogleFormat("A<int*> a;");
5787   verifyGoogleFormat("A<int**> a;");
5788   verifyGoogleFormat("A<int*, int*> a;");
5789   verifyGoogleFormat("A<int**, int**> a;");
5790   verifyGoogleFormat("f(b ? *c : *d);");
5791   verifyGoogleFormat("int a = b ? *c : *d;");
5792   verifyGoogleFormat("Type* t = **x;");
5793   verifyGoogleFormat("Type* t = *++*x;");
5794   verifyGoogleFormat("*++*x;");
5795   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5796   verifyGoogleFormat("Type* t = x++ * y;");
5797   verifyGoogleFormat(
5798       "const char* const p = reinterpret_cast<const char* const>(q);");
5799   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5800   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5801   verifyGoogleFormat("template <typename T>\n"
5802                      "void f(int i = 0, SomeType** temps = NULL);");
5803 
5804   FormatStyle Left = getLLVMStyle();
5805   Left.PointerAlignment = FormatStyle::PAS_Left;
5806   verifyFormat("x = *a(x) = *a(y);", Left);
5807   verifyFormat("for (;; *a = b) {\n}", Left);
5808   verifyFormat("return *this += 1;", Left);
5809   verifyFormat("throw *x;", Left);
5810   verifyFormat("delete *x;", Left);
5811   verifyFormat("typedef typeof(int(int, int))* MyFuncPtr;", Left);
5812   verifyFormat("[](const decltype(*a)* ptr) {}", Left);
5813   verifyFormat("typedef typeof /*comment*/ (int(int, int))* MyFuncPtr;", Left);
5814 
5815   verifyIndependentOfContext("a = *(x + y);");
5816   verifyIndependentOfContext("a = &(x + y);");
5817   verifyIndependentOfContext("*(x + y).call();");
5818   verifyIndependentOfContext("&(x + y)->call();");
5819   verifyFormat("void f() { &(*I).first; }");
5820 
5821   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5822   verifyFormat(
5823       "int *MyValues = {\n"
5824       "    *A, // Operator detection might be confused by the '{'\n"
5825       "    *BB // Operator detection might be confused by previous comment\n"
5826       "};");
5827 
5828   verifyIndependentOfContext("if (int *a = &b)");
5829   verifyIndependentOfContext("if (int &a = *b)");
5830   verifyIndependentOfContext("if (a & b[i])");
5831   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5832   verifyIndependentOfContext("if (*b[i])");
5833   verifyIndependentOfContext("if (int *a = (&b))");
5834   verifyIndependentOfContext("while (int *a = &b)");
5835   verifyIndependentOfContext("size = sizeof *a;");
5836   verifyIndependentOfContext("if (a && (b = c))");
5837   verifyFormat("void f() {\n"
5838                "  for (const int &v : Values) {\n"
5839                "  }\n"
5840                "}");
5841   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5842   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5843   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5844 
5845   verifyFormat("#define A (!a * b)");
5846   verifyFormat("#define MACRO     \\\n"
5847                "  int *i = a * b; \\\n"
5848                "  void f(a *b);",
5849                getLLVMStyleWithColumns(19));
5850 
5851   verifyIndependentOfContext("A = new SomeType *[Length];");
5852   verifyIndependentOfContext("A = new SomeType *[Length]();");
5853   verifyIndependentOfContext("T **t = new T *;");
5854   verifyIndependentOfContext("T **t = new T *();");
5855   verifyGoogleFormat("A = new SomeType*[Length]();");
5856   verifyGoogleFormat("A = new SomeType*[Length];");
5857   verifyGoogleFormat("T** t = new T*;");
5858   verifyGoogleFormat("T** t = new T*();");
5859 
5860   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5861   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5862   verifyFormat("template <bool a, bool b> "
5863                "typename t::if<x && y>::type f() {}");
5864   verifyFormat("template <int *y> f() {}");
5865   verifyFormat("vector<int *> v;");
5866   verifyFormat("vector<int *const> v;");
5867   verifyFormat("vector<int *const **const *> v;");
5868   verifyFormat("vector<int *volatile> v;");
5869   verifyFormat("vector<a * b> v;");
5870   verifyFormat("foo<b && false>();");
5871   verifyFormat("foo<b & 1>();");
5872   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5873   verifyFormat(
5874       "template <class T, class = typename std::enable_if<\n"
5875       "                       std::is_integral<T>::value &&\n"
5876       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5877       "void F();",
5878       getLLVMStyleWithColumns(70));
5879   verifyFormat(
5880       "template <class T,\n"
5881       "          class = typename std::enable_if<\n"
5882       "              std::is_integral<T>::value &&\n"
5883       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5884       "          class U>\n"
5885       "void F();",
5886       getLLVMStyleWithColumns(70));
5887   verifyFormat(
5888       "template <class T,\n"
5889       "          class = typename ::std::enable_if<\n"
5890       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5891       "void F();",
5892       getGoogleStyleWithColumns(68));
5893 
5894   verifyIndependentOfContext("MACRO(int *i);");
5895   verifyIndependentOfContext("MACRO(auto *a);");
5896   verifyIndependentOfContext("MACRO(const A *a);");
5897   verifyIndependentOfContext("MACRO(A *const a);");
5898   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5899   verifyFormat("void f() { f(float{1}, a * a); }");
5900   // FIXME: Is there a way to make this work?
5901   // verifyIndependentOfContext("MACRO(A *a);");
5902 
5903   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5904   verifyFormat("return options != nullptr && operator==(*options);");
5905 
5906   EXPECT_EQ("#define OP(x)                                    \\\n"
5907             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5908             "    return s << a.DebugString();                 \\\n"
5909             "  }",
5910             format("#define OP(x) \\\n"
5911                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5912                    "    return s << a.DebugString(); \\\n"
5913                    "  }",
5914                    getLLVMStyleWithColumns(50)));
5915 
5916   // FIXME: We cannot handle this case yet; we might be able to figure out that
5917   // foo<x> d > v; doesn't make sense.
5918   verifyFormat("foo<a<b && c> d> v;");
5919 
5920   FormatStyle PointerMiddle = getLLVMStyle();
5921   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5922   verifyFormat("delete *x;", PointerMiddle);
5923   verifyFormat("int * x;", PointerMiddle);
5924   verifyFormat("template <int * y> f() {}", PointerMiddle);
5925   verifyFormat("int * f(int * a) {}", PointerMiddle);
5926   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5927   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5928   verifyFormat("A<int *> a;", PointerMiddle);
5929   verifyFormat("A<int **> a;", PointerMiddle);
5930   verifyFormat("A<int *, int *> a;", PointerMiddle);
5931   verifyFormat("A<int * []> a;", PointerMiddle);
5932   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5933   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5934   verifyFormat("T ** t = new T *;", PointerMiddle);
5935 
5936   // Member function reference qualifiers aren't binary operators.
5937   verifyFormat("string // break\n"
5938                "operator()() & {}");
5939   verifyFormat("string // break\n"
5940                "operator()() && {}");
5941   verifyGoogleFormat("template <typename T>\n"
5942                      "auto x() & -> int {}");
5943 }
5944 
5945 TEST_F(FormatTest, UnderstandsAttributes) {
5946   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5947   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5948                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5949   FormatStyle AfterType = getLLVMStyle();
5950   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5951   verifyFormat("__attribute__((nodebug)) void\n"
5952                "foo() {}\n",
5953                AfterType);
5954 }
5955 
5956 TEST_F(FormatTest, UnderstandsEllipsis) {
5957   verifyFormat("int printf(const char *fmt, ...);");
5958   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5959   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5960 
5961   FormatStyle PointersLeft = getLLVMStyle();
5962   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5963   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5964 }
5965 
5966 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5967   EXPECT_EQ("int *a;\n"
5968             "int *a;\n"
5969             "int *a;",
5970             format("int *a;\n"
5971                    "int* a;\n"
5972                    "int *a;",
5973                    getGoogleStyle()));
5974   EXPECT_EQ("int* a;\n"
5975             "int* a;\n"
5976             "int* a;",
5977             format("int* a;\n"
5978                    "int* a;\n"
5979                    "int *a;",
5980                    getGoogleStyle()));
5981   EXPECT_EQ("int *a;\n"
5982             "int *a;\n"
5983             "int *a;",
5984             format("int *a;\n"
5985                    "int * a;\n"
5986                    "int *  a;",
5987                    getGoogleStyle()));
5988   EXPECT_EQ("auto x = [] {\n"
5989             "  int *a;\n"
5990             "  int *a;\n"
5991             "  int *a;\n"
5992             "};",
5993             format("auto x=[]{int *a;\n"
5994                    "int * a;\n"
5995                    "int *  a;};",
5996                    getGoogleStyle()));
5997 }
5998 
5999 TEST_F(FormatTest, UnderstandsRvalueReferences) {
6000   verifyFormat("int f(int &&a) {}");
6001   verifyFormat("int f(int a, char &&b) {}");
6002   verifyFormat("void f() { int &&a = b; }");
6003   verifyGoogleFormat("int f(int a, char&& b) {}");
6004   verifyGoogleFormat("void f() { int&& a = b; }");
6005 
6006   verifyIndependentOfContext("A<int &&> a;");
6007   verifyIndependentOfContext("A<int &&, int &&> a;");
6008   verifyGoogleFormat("A<int&&> a;");
6009   verifyGoogleFormat("A<int&&, int&&> a;");
6010 
6011   // Not rvalue references:
6012   verifyFormat("template <bool B, bool C> class A {\n"
6013                "  static_assert(B && C, \"Something is wrong\");\n"
6014                "};");
6015   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
6016   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
6017   verifyFormat("#define A(a, b) (a && b)");
6018 }
6019 
6020 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
6021   verifyFormat("void f() {\n"
6022                "  x[aaaaaaaaa -\n"
6023                "    b] = 23;\n"
6024                "}",
6025                getLLVMStyleWithColumns(15));
6026 }
6027 
6028 TEST_F(FormatTest, FormatsCasts) {
6029   verifyFormat("Type *A = static_cast<Type *>(P);");
6030   verifyFormat("Type *A = (Type *)P;");
6031   verifyFormat("Type *A = (vector<Type *, int *>)P;");
6032   verifyFormat("int a = (int)(2.0f);");
6033   verifyFormat("int a = (int)2.0f;");
6034   verifyFormat("x[(int32)y];");
6035   verifyFormat("x = (int32)y;");
6036   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
6037   verifyFormat("int a = (int)*b;");
6038   verifyFormat("int a = (int)2.0f;");
6039   verifyFormat("int a = (int)~0;");
6040   verifyFormat("int a = (int)++a;");
6041   verifyFormat("int a = (int)sizeof(int);");
6042   verifyFormat("int a = (int)+2;");
6043   verifyFormat("my_int a = (my_int)2.0f;");
6044   verifyFormat("my_int a = (my_int)sizeof(int);");
6045   verifyFormat("return (my_int)aaa;");
6046   verifyFormat("#define x ((int)-1)");
6047   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
6048   verifyFormat("#define p(q) ((int *)&q)");
6049   verifyFormat("fn(a)(b) + 1;");
6050 
6051   verifyFormat("void f() { my_int a = (my_int)*b; }");
6052   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
6053   verifyFormat("my_int a = (my_int)~0;");
6054   verifyFormat("my_int a = (my_int)++a;");
6055   verifyFormat("my_int a = (my_int)-2;");
6056   verifyFormat("my_int a = (my_int)1;");
6057   verifyFormat("my_int a = (my_int *)1;");
6058   verifyFormat("my_int a = (const my_int)-1;");
6059   verifyFormat("my_int a = (const my_int *)-1;");
6060   verifyFormat("my_int a = (my_int)(my_int)-1;");
6061   verifyFormat("my_int a = (ns::my_int)-2;");
6062   verifyFormat("case (my_int)ONE:");
6063   verifyFormat("auto x = (X)this;");
6064 
6065   // FIXME: single value wrapped with paren will be treated as cast.
6066   verifyFormat("void f(int i = (kValue)*kMask) {}");
6067 
6068   verifyFormat("{ (void)F; }");
6069 
6070   // Don't break after a cast's
6071   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
6072                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
6073                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
6074 
6075   // These are not casts.
6076   verifyFormat("void f(int *) {}");
6077   verifyFormat("f(foo)->b;");
6078   verifyFormat("f(foo).b;");
6079   verifyFormat("f(foo)(b);");
6080   verifyFormat("f(foo)[b];");
6081   verifyFormat("[](foo) { return 4; }(bar);");
6082   verifyFormat("(*funptr)(foo)[4];");
6083   verifyFormat("funptrs[4](foo)[4];");
6084   verifyFormat("void f(int *);");
6085   verifyFormat("void f(int *) = 0;");
6086   verifyFormat("void f(SmallVector<int>) {}");
6087   verifyFormat("void f(SmallVector<int>);");
6088   verifyFormat("void f(SmallVector<int>) = 0;");
6089   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
6090   verifyFormat("int a = sizeof(int) * b;");
6091   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
6092   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
6093   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
6094   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
6095 
6096   // These are not casts, but at some point were confused with casts.
6097   verifyFormat("virtual void foo(int *) override;");
6098   verifyFormat("virtual void foo(char &) const;");
6099   verifyFormat("virtual void foo(int *a, char *) const;");
6100   verifyFormat("int a = sizeof(int *) + b;");
6101   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
6102   verifyFormat("bool b = f(g<int>) && c;");
6103   verifyFormat("typedef void (*f)(int i) func;");
6104 
6105   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
6106                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
6107   // FIXME: The indentation here is not ideal.
6108   verifyFormat(
6109       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6110       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
6111       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
6112 }
6113 
6114 TEST_F(FormatTest, FormatsFunctionTypes) {
6115   verifyFormat("A<bool()> a;");
6116   verifyFormat("A<SomeType()> a;");
6117   verifyFormat("A<void (*)(int, std::string)> a;");
6118   verifyFormat("A<void *(int)>;");
6119   verifyFormat("void *(*a)(int *, SomeType *);");
6120   verifyFormat("int (*func)(void *);");
6121   verifyFormat("void f() { int (*func)(void *); }");
6122   verifyFormat("template <class CallbackClass>\n"
6123                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
6124 
6125   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
6126   verifyGoogleFormat("void* (*a)(int);");
6127   verifyGoogleFormat(
6128       "template <class CallbackClass>\n"
6129       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
6130 
6131   // Other constructs can look somewhat like function types:
6132   verifyFormat("A<sizeof(*x)> a;");
6133   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
6134   verifyFormat("some_var = function(*some_pointer_var)[0];");
6135   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
6136   verifyFormat("int x = f(&h)();");
6137   verifyFormat("returnsFunction(&param1, &param2)(param);");
6138   verifyFormat("std::function<\n"
6139                "    LooooooooooongTemplatedType<\n"
6140                "        SomeType>*(\n"
6141                "        LooooooooooooooooongType type)>\n"
6142                "    function;",
6143                getGoogleStyleWithColumns(40));
6144 }
6145 
6146 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
6147   verifyFormat("A (*foo_)[6];");
6148   verifyFormat("vector<int> (*foo_)[6];");
6149 }
6150 
6151 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
6152   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6153                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6154   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
6155                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
6156   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6157                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
6158 
6159   // Different ways of ()-initializiation.
6160   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6161                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
6162   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6163                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
6164   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6165                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
6166   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
6167                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
6168 
6169   // Lambdas should not confuse the variable declaration heuristic.
6170   verifyFormat("LooooooooooooooooongType\n"
6171                "    variable(nullptr, [](A *a) {});",
6172                getLLVMStyleWithColumns(40));
6173 }
6174 
6175 TEST_F(FormatTest, BreaksLongDeclarations) {
6176   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
6177                "    AnotherNameForTheLongType;");
6178   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
6179                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6180   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6181                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6182   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
6183                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
6184   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6185                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6186   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
6187                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6188   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6189                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6190   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6191                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
6192   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6193                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
6194   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6195                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
6196   FormatStyle Indented = getLLVMStyle();
6197   Indented.IndentWrappedFunctionNames = true;
6198   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6199                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
6200                Indented);
6201   verifyFormat(
6202       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
6203       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6204       Indented);
6205   verifyFormat(
6206       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
6207       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6208       Indented);
6209   verifyFormat(
6210       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
6211       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
6212       Indented);
6213 
6214   // FIXME: Without the comment, this breaks after "(".
6215   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
6216                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
6217                getGoogleStyle());
6218 
6219   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
6220                "                  int LoooooooooooooooooooongParam2) {}");
6221   verifyFormat(
6222       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
6223       "                                   SourceLocation L, IdentifierIn *II,\n"
6224       "                                   Type *T) {}");
6225   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
6226                "ReallyReaaallyLongFunctionName(\n"
6227                "    const std::string &SomeParameter,\n"
6228                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6229                "        &ReallyReallyLongParameterName,\n"
6230                "    const SomeType<string, SomeOtherTemplateParameter>\n"
6231                "        &AnotherLongParameterName) {}");
6232   verifyFormat("template <typename A>\n"
6233                "SomeLoooooooooooooooooooooongType<\n"
6234                "    typename some_namespace::SomeOtherType<A>::Type>\n"
6235                "Function() {}");
6236 
6237   verifyGoogleFormat(
6238       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
6239       "    aaaaaaaaaaaaaaaaaaaaaaa;");
6240   verifyGoogleFormat(
6241       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
6242       "                                   SourceLocation L) {}");
6243   verifyGoogleFormat(
6244       "some_namespace::LongReturnType\n"
6245       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
6246       "    int first_long_parameter, int second_parameter) {}");
6247 
6248   verifyGoogleFormat("template <typename T>\n"
6249                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6250                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
6251   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6252                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
6253 
6254   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
6255                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6256                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6257   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6258                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
6259                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
6260   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
6261                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
6262                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
6263                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6264 
6265   verifyFormat("template <typename T> // Templates on own line.\n"
6266                "static int            // Some comment.\n"
6267                "MyFunction(int a);",
6268                getLLVMStyle());
6269 }
6270 
6271 TEST_F(FormatTest, FormatsArrays) {
6272   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6273                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
6274   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
6275                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
6276   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
6277                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
6278   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6279                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6280   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6281                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
6282   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6283                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6284                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
6285   verifyFormat(
6286       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
6287       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
6288       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
6289   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
6290                "    .aaaaaaaaaaaaaaaaaaaaaa();");
6291 
6292   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
6293                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
6294   verifyFormat(
6295       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
6296       "                                  .aaaaaaa[0]\n"
6297       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
6298   verifyFormat("a[::b::c];");
6299 
6300   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
6301 
6302   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
6303   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
6304 }
6305 
6306 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
6307   verifyFormat("(a)->b();");
6308   verifyFormat("--a;");
6309 }
6310 
6311 TEST_F(FormatTest, HandlesIncludeDirectives) {
6312   verifyFormat("#include <string>\n"
6313                "#include <a/b/c.h>\n"
6314                "#include \"a/b/string\"\n"
6315                "#include \"string.h\"\n"
6316                "#include \"string.h\"\n"
6317                "#include <a-a>\n"
6318                "#include < path with space >\n"
6319                "#include_next <test.h>"
6320                "#include \"abc.h\" // this is included for ABC\n"
6321                "#include \"some long include\" // with a comment\n"
6322                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
6323                getLLVMStyleWithColumns(35));
6324   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
6325   EXPECT_EQ("#include <a>", format("#include<a>"));
6326 
6327   verifyFormat("#import <string>");
6328   verifyFormat("#import <a/b/c.h>");
6329   verifyFormat("#import \"a/b/string\"");
6330   verifyFormat("#import \"string.h\"");
6331   verifyFormat("#import \"string.h\"");
6332   verifyFormat("#if __has_include(<strstream>)\n"
6333                "#include <strstream>\n"
6334                "#endif");
6335 
6336   verifyFormat("#define MY_IMPORT <a/b>");
6337 
6338   verifyFormat("#if __has_include(<a/b>)");
6339   verifyFormat("#if __has_include_next(<a/b>)");
6340   verifyFormat("#define F __has_include(<a/b>)");
6341   verifyFormat("#define F __has_include_next(<a/b>)");
6342 
6343   // Protocol buffer definition or missing "#".
6344   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
6345                getLLVMStyleWithColumns(30));
6346 
6347   FormatStyle Style = getLLVMStyle();
6348   Style.AlwaysBreakBeforeMultilineStrings = true;
6349   Style.ColumnLimit = 0;
6350   verifyFormat("#import \"abc.h\"", Style);
6351 
6352   // But 'import' might also be a regular C++ namespace.
6353   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6354                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
6355 }
6356 
6357 //===----------------------------------------------------------------------===//
6358 // Error recovery tests.
6359 //===----------------------------------------------------------------------===//
6360 
6361 TEST_F(FormatTest, IncompleteParameterLists) {
6362   FormatStyle NoBinPacking = getLLVMStyle();
6363   NoBinPacking.BinPackParameters = false;
6364   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
6365                "                        double *min_x,\n"
6366                "                        double *max_x,\n"
6367                "                        double *min_y,\n"
6368                "                        double *max_y,\n"
6369                "                        double *min_z,\n"
6370                "                        double *max_z, ) {}",
6371                NoBinPacking);
6372 }
6373 
6374 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
6375   verifyFormat("void f() { return; }\n42");
6376   verifyFormat("void f() {\n"
6377                "  if (0)\n"
6378                "    return;\n"
6379                "}\n"
6380                "42");
6381   verifyFormat("void f() { return }\n42");
6382   verifyFormat("void f() {\n"
6383                "  if (0)\n"
6384                "    return\n"
6385                "}\n"
6386                "42");
6387 }
6388 
6389 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
6390   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
6391   EXPECT_EQ("void f() {\n"
6392             "  if (a)\n"
6393             "    return\n"
6394             "}",
6395             format("void  f  (  )  {  if  ( a )  return  }"));
6396   EXPECT_EQ("namespace N {\n"
6397             "void f()\n"
6398             "}",
6399             format("namespace  N  {  void f()  }"));
6400   EXPECT_EQ("namespace N {\n"
6401             "void f() {}\n"
6402             "void g()\n"
6403             "} // namespace N",
6404             format("namespace N  { void f( ) { } void g( ) }"));
6405 }
6406 
6407 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
6408   verifyFormat("int aaaaaaaa =\n"
6409                "    // Overlylongcomment\n"
6410                "    b;",
6411                getLLVMStyleWithColumns(20));
6412   verifyFormat("function(\n"
6413                "    ShortArgument,\n"
6414                "    LoooooooooooongArgument);\n",
6415                getLLVMStyleWithColumns(20));
6416 }
6417 
6418 TEST_F(FormatTest, IncorrectAccessSpecifier) {
6419   verifyFormat("public:");
6420   verifyFormat("class A {\n"
6421                "public\n"
6422                "  void f() {}\n"
6423                "};");
6424   verifyFormat("public\n"
6425                "int qwerty;");
6426   verifyFormat("public\n"
6427                "B {}");
6428   verifyFormat("public\n"
6429                "{}");
6430   verifyFormat("public\n"
6431                "B { int x; }");
6432 }
6433 
6434 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
6435   verifyFormat("{");
6436   verifyFormat("#})");
6437   verifyNoCrash("(/**/[:!] ?[).");
6438 }
6439 
6440 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
6441   verifyFormat("do {\n}");
6442   verifyFormat("do {\n}\n"
6443                "f();");
6444   verifyFormat("do {\n}\n"
6445                "wheeee(fun);");
6446   verifyFormat("do {\n"
6447                "  f();\n"
6448                "}");
6449 }
6450 
6451 TEST_F(FormatTest, IncorrectCodeMissingParens) {
6452   verifyFormat("if {\n  foo;\n  foo();\n}");
6453   verifyFormat("switch {\n  foo;\n  foo();\n}");
6454   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
6455   verifyFormat("while {\n  foo;\n  foo();\n}");
6456   verifyFormat("do {\n  foo;\n  foo();\n} while;");
6457 }
6458 
6459 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
6460   verifyIncompleteFormat("namespace {\n"
6461                          "class Foo { Foo (\n"
6462                          "};\n"
6463                          "} // namespace");
6464 }
6465 
6466 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
6467   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6468   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6469   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6470   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6471 
6472   EXPECT_EQ("{\n"
6473             "  {\n"
6474             "    breakme(\n"
6475             "        qwe);\n"
6476             "  }\n",
6477             format("{\n"
6478                    "    {\n"
6479                    " breakme(qwe);\n"
6480                    "}\n",
6481                    getLLVMStyleWithColumns(10)));
6482 }
6483 
6484 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6485   verifyFormat("int x = {\n"
6486                "    avariable,\n"
6487                "    b(alongervariable)};",
6488                getLLVMStyleWithColumns(25));
6489 }
6490 
6491 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6492   verifyFormat("return (a)(b){1, 2, 3};");
6493 }
6494 
6495 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6496   verifyFormat("vector<int> x{1, 2, 3, 4};");
6497   verifyFormat("vector<int> x{\n"
6498                "    1,\n"
6499                "    2,\n"
6500                "    3,\n"
6501                "    4,\n"
6502                "};");
6503   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6504   verifyFormat("f({1, 2});");
6505   verifyFormat("auto v = Foo{-1};");
6506   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6507   verifyFormat("Class::Class : member{1, 2, 3} {}");
6508   verifyFormat("new vector<int>{1, 2, 3};");
6509   verifyFormat("new int[3]{1, 2, 3};");
6510   verifyFormat("new int{1};");
6511   verifyFormat("return {arg1, arg2};");
6512   verifyFormat("return {arg1, SomeType{parameter}};");
6513   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6514   verifyFormat("new T{arg1, arg2};");
6515   verifyFormat("f(MyMap[{composite, key}]);");
6516   verifyFormat("class Class {\n"
6517                "  T member = {arg1, arg2};\n"
6518                "};");
6519   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6520   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6521   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6522   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6523   verifyFormat("int a = std::is_integral<int>{} + 0;");
6524 
6525   verifyFormat("int foo(int i) { return fo1{}(i); }");
6526   verifyFormat("int foo(int i) { return fo1{}(i); }");
6527   verifyFormat("auto i = decltype(x){};");
6528   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6529   verifyFormat("Node n{1, Node{1000}, //\n"
6530                "       2};");
6531   verifyFormat("Aaaa aaaaaaa{\n"
6532                "    {\n"
6533                "        aaaa,\n"
6534                "    },\n"
6535                "};");
6536   verifyFormat("class C : public D {\n"
6537                "  SomeClass SC{2};\n"
6538                "};");
6539   verifyFormat("class C : public A {\n"
6540                "  class D : public B {\n"
6541                "    void f() { int i{2}; }\n"
6542                "  };\n"
6543                "};");
6544   verifyFormat("#define A {a, a},");
6545 
6546   // Binpacking only if there is no trailing comma
6547   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6548                "                      cccccccccc, dddddddddd};",
6549 			   getLLVMStyleWithColumns(50));
6550   verifyFormat("const Aaaaaa aaaaa = {\n"
6551                "    aaaaaaaaaaa,\n"
6552                "    bbbbbbbbbbb,\n"
6553                "    ccccccccccc,\n"
6554                "    ddddddddddd,\n"
6555                "};", getLLVMStyleWithColumns(50));
6556 
6557   // Cases where distinguising braced lists and blocks is hard.
6558   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6559   verifyFormat("void f() {\n"
6560                "  return; // comment\n"
6561                "}\n"
6562                "SomeType t;");
6563   verifyFormat("void f() {\n"
6564                "  if (a) {\n"
6565                "    f();\n"
6566                "  }\n"
6567                "}\n"
6568                "SomeType t;");
6569 
6570   // In combination with BinPackArguments = false.
6571   FormatStyle NoBinPacking = getLLVMStyle();
6572   NoBinPacking.BinPackArguments = false;
6573   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6574                "                      bbbbb,\n"
6575                "                      ccccc,\n"
6576                "                      ddddd,\n"
6577                "                      eeeee,\n"
6578                "                      ffffff,\n"
6579                "                      ggggg,\n"
6580                "                      hhhhhh,\n"
6581                "                      iiiiii,\n"
6582                "                      jjjjjj,\n"
6583                "                      kkkkkk};",
6584                NoBinPacking);
6585   verifyFormat("const Aaaaaa aaaaa = {\n"
6586                "    aaaaa,\n"
6587                "    bbbbb,\n"
6588                "    ccccc,\n"
6589                "    ddddd,\n"
6590                "    eeeee,\n"
6591                "    ffffff,\n"
6592                "    ggggg,\n"
6593                "    hhhhhh,\n"
6594                "    iiiiii,\n"
6595                "    jjjjjj,\n"
6596                "    kkkkkk,\n"
6597                "};",
6598                NoBinPacking);
6599   verifyFormat(
6600       "const Aaaaaa aaaaa = {\n"
6601       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6602       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6603       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6604       "};",
6605       NoBinPacking);
6606 
6607   // FIXME: The alignment of these trailing comments might be bad. Then again,
6608   // this might be utterly useless in real code.
6609   verifyFormat("Constructor::Constructor()\n"
6610                "    : some_value{         //\n"
6611                "                 aaaaaaa, //\n"
6612                "                 bbbbbbb} {}");
6613 
6614   // In braced lists, the first comment is always assumed to belong to the
6615   // first element. Thus, it can be moved to the next or previous line as
6616   // appropriate.
6617   EXPECT_EQ("function({// First element:\n"
6618             "          1,\n"
6619             "          // Second element:\n"
6620             "          2});",
6621             format("function({\n"
6622                    "    // First element:\n"
6623                    "    1,\n"
6624                    "    // Second element:\n"
6625                    "    2});"));
6626   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6627             "    // First element:\n"
6628             "    1,\n"
6629             "    // Second element:\n"
6630             "    2};",
6631             format("std::vector<int> MyNumbers{// First element:\n"
6632                    "                           1,\n"
6633                    "                           // Second element:\n"
6634                    "                           2};",
6635                    getLLVMStyleWithColumns(30)));
6636   // A trailing comma should still lead to an enforced line break and no
6637   // binpacking.
6638   EXPECT_EQ("vector<int> SomeVector = {\n"
6639             "    // aaa\n"
6640             "    1,\n"
6641             "    2,\n"
6642             "};",
6643             format("vector<int> SomeVector = { // aaa\n"
6644                    "    1, 2, };"));
6645 
6646   FormatStyle ExtraSpaces = getLLVMStyle();
6647   ExtraSpaces.Cpp11BracedListStyle = false;
6648   ExtraSpaces.ColumnLimit = 75;
6649   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6650   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6651   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6652   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6653   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6654   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6655   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6656   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6657   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6658   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6659   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6660   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6661   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6662   verifyFormat("class Class {\n"
6663                "  T member = { arg1, arg2 };\n"
6664                "};",
6665                ExtraSpaces);
6666   verifyFormat(
6667       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6668       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6669       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6670       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6671       ExtraSpaces);
6672   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6673   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6674                ExtraSpaces);
6675   verifyFormat(
6676       "someFunction(OtherParam,\n"
6677       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6678       "                         param1, param2,\n"
6679       "                         // comment 2\n"
6680       "                         param3, param4 });",
6681       ExtraSpaces);
6682   verifyFormat(
6683       "std::this_thread::sleep_for(\n"
6684       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6685       ExtraSpaces);
6686   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6687                "    aaaaaaa,\n"
6688                "    aaaaaaaaaa,\n"
6689                "    aaaaa,\n"
6690                "    aaaaaaaaaaaaaaa,\n"
6691                "    aaa,\n"
6692                "    aaaaaaaaaa,\n"
6693                "    a,\n"
6694                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6695                "    aaaaaaaaaaaa,\n"
6696                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6697                "    aaaaaaa,\n"
6698                "    a};");
6699   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6700   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6701   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6702 }
6703 
6704 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6705   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6706                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6707                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6708                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6709                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6710                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6711   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6712                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6713                "                 1, 22, 333, 4444, 55555, //\n"
6714                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6715                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6716   verifyFormat(
6717       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6718       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6719       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6720       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6721       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6722       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6723       "                 7777777};");
6724   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6725                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6726                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6727   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6728                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6729                "    // Separating comment.\n"
6730                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6731   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6732                "    // Leading comment\n"
6733                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6734                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6735   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6736                "                 1, 1, 1, 1};",
6737                getLLVMStyleWithColumns(39));
6738   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6739                "                 1, 1, 1, 1};",
6740                getLLVMStyleWithColumns(38));
6741   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6742                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6743                getLLVMStyleWithColumns(43));
6744   verifyFormat(
6745       "static unsigned SomeValues[10][3] = {\n"
6746       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6747       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6748   verifyFormat("static auto fields = new vector<string>{\n"
6749                "    \"aaaaaaaaaaaaa\",\n"
6750                "    \"aaaaaaaaaaaaa\",\n"
6751                "    \"aaaaaaaaaaaa\",\n"
6752                "    \"aaaaaaaaaaaaaa\",\n"
6753                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6754                "    \"aaaaaaaaaaaa\",\n"
6755                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6756                "};");
6757   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6758   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6759                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6760                "                 3, cccccccccccccccccccccc};",
6761                getLLVMStyleWithColumns(60));
6762 
6763   // Trailing commas.
6764   verifyFormat("vector<int> x = {\n"
6765                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6766                "};",
6767                getLLVMStyleWithColumns(39));
6768   verifyFormat("vector<int> x = {\n"
6769                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6770                "};",
6771                getLLVMStyleWithColumns(39));
6772   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6773                "                 1, 1, 1, 1,\n"
6774                "                 /**/ /**/};",
6775                getLLVMStyleWithColumns(39));
6776 
6777   // Trailing comment in the first line.
6778   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6779                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6780                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6781                "    11111111,   22222222,   333333333,   44444444};");
6782   // Trailing comment in the last line.
6783   verifyFormat("int aaaaa[] = {\n"
6784                "    1, 2, 3, // comment\n"
6785                "    4, 5, 6  // comment\n"
6786                "};");
6787 
6788   // With nested lists, we should either format one item per line or all nested
6789   // lists one on line.
6790   // FIXME: For some nested lists, we can do better.
6791   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6792                "        {aaaaaaaaaaaaaaaaaaa},\n"
6793                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6794                "        {aaaaaaaaaaaaaaaaa}};",
6795                getLLVMStyleWithColumns(60));
6796   verifyFormat(
6797       "SomeStruct my_struct_array = {\n"
6798       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6799       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6800       "    {aaa, aaa},\n"
6801       "    {aaa, aaa},\n"
6802       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6803       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6804       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6805 
6806   // No column layout should be used here.
6807   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6808                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6809 
6810   verifyNoCrash("a<,");
6811 
6812   // No braced initializer here.
6813   verifyFormat("void f() {\n"
6814                "  struct Dummy {};\n"
6815                "  f(v);\n"
6816                "}");
6817 
6818   // Long lists should be formatted in columns even if they are nested.
6819   verifyFormat(
6820       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6821       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6822       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6823       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6824       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6825       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6826 
6827   // Allow "single-column" layout even if that violates the column limit. There
6828   // isn't going to be a better way.
6829   verifyFormat("std::vector<int> a = {\n"
6830                "    aaaaaaaa,\n"
6831                "    aaaaaaaa,\n"
6832                "    aaaaaaaa,\n"
6833                "    aaaaaaaa,\n"
6834                "    aaaaaaaaaa,\n"
6835                "    aaaaaaaa,\n"
6836                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6837                getLLVMStyleWithColumns(30));
6838   verifyFormat("vector<int> aaaa = {\n"
6839                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6840                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6841                "    aaaaaa.aaaaaaa,\n"
6842                "    aaaaaa.aaaaaaa,\n"
6843                "    aaaaaa.aaaaaaa,\n"
6844                "    aaaaaa.aaaaaaa,\n"
6845                "};");
6846 
6847   // Don't create hanging lists.
6848   verifyFormat("someFunction(Param, {List1, List2,\n"
6849                "                     List3});",
6850                getLLVMStyleWithColumns(35));
6851   verifyFormat("someFunction(Param, Param,\n"
6852                "             {List1, List2,\n"
6853                "              List3});",
6854                getLLVMStyleWithColumns(35));
6855   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6856                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6857 }
6858 
6859 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6860   FormatStyle DoNotMerge = getLLVMStyle();
6861   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6862 
6863   verifyFormat("void f() { return 42; }");
6864   verifyFormat("void f() {\n"
6865                "  return 42;\n"
6866                "}",
6867                DoNotMerge);
6868   verifyFormat("void f() {\n"
6869                "  // Comment\n"
6870                "}");
6871   verifyFormat("{\n"
6872                "#error {\n"
6873                "  int a;\n"
6874                "}");
6875   verifyFormat("{\n"
6876                "  int a;\n"
6877                "#error {\n"
6878                "}");
6879   verifyFormat("void f() {} // comment");
6880   verifyFormat("void f() { int a; } // comment");
6881   verifyFormat("void f() {\n"
6882                "} // comment",
6883                DoNotMerge);
6884   verifyFormat("void f() {\n"
6885                "  int a;\n"
6886                "} // comment",
6887                DoNotMerge);
6888   verifyFormat("void f() {\n"
6889                "} // comment",
6890                getLLVMStyleWithColumns(15));
6891 
6892   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6893   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6894 
6895   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6896   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6897   verifyFormat("class C {\n"
6898                "  C()\n"
6899                "      : iiiiiiii(nullptr),\n"
6900                "        kkkkkkk(nullptr),\n"
6901                "        mmmmmmm(nullptr),\n"
6902                "        nnnnnnn(nullptr) {}\n"
6903                "};",
6904                getGoogleStyle());
6905 
6906   FormatStyle NoColumnLimit = getLLVMStyle();
6907   NoColumnLimit.ColumnLimit = 0;
6908   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6909   EXPECT_EQ("class C {\n"
6910             "  A() : b(0) {}\n"
6911             "};",
6912             format("class C{A():b(0){}};", NoColumnLimit));
6913   EXPECT_EQ("A()\n"
6914             "    : b(0) {\n"
6915             "}",
6916             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6917 
6918   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6919   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6920       FormatStyle::SFS_None;
6921   EXPECT_EQ("A()\n"
6922             "    : b(0) {\n"
6923             "}",
6924             format("A():b(0){}", DoNotMergeNoColumnLimit));
6925   EXPECT_EQ("A()\n"
6926             "    : b(0) {\n"
6927             "}",
6928             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6929 
6930   verifyFormat("#define A          \\\n"
6931                "  void f() {       \\\n"
6932                "    int i;         \\\n"
6933                "  }",
6934                getLLVMStyleWithColumns(20));
6935   verifyFormat("#define A           \\\n"
6936                "  void f() { int i; }",
6937                getLLVMStyleWithColumns(21));
6938   verifyFormat("#define A            \\\n"
6939                "  void f() {         \\\n"
6940                "    int i;           \\\n"
6941                "  }                  \\\n"
6942                "  int j;",
6943                getLLVMStyleWithColumns(22));
6944   verifyFormat("#define A             \\\n"
6945                "  void f() { int i; } \\\n"
6946                "  int j;",
6947                getLLVMStyleWithColumns(23));
6948 }
6949 
6950 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
6951   FormatStyle MergeEmptyOnly = getLLVMStyle();
6952   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6953   verifyFormat("class C {\n"
6954                "  int f() {}\n"
6955                "};",
6956                MergeEmptyOnly);
6957   verifyFormat("class C {\n"
6958                "  int f() {\n"
6959                "    return 42;\n"
6960                "  }\n"
6961                "};",
6962                MergeEmptyOnly);
6963   verifyFormat("int f() {}", MergeEmptyOnly);
6964   verifyFormat("int f() {\n"
6965                "  return 42;\n"
6966                "}",
6967                MergeEmptyOnly);
6968 
6969   // Also verify behavior when BraceWrapping.AfterFunction = true
6970   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6971   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
6972   verifyFormat("int f() {}", MergeEmptyOnly);
6973   verifyFormat("class C {\n"
6974                "  int f() {}\n"
6975                "};",
6976                MergeEmptyOnly);
6977 }
6978 
6979 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6980   FormatStyle MergeInlineOnly = getLLVMStyle();
6981   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6982   verifyFormat("class C {\n"
6983                "  int f() { return 42; }\n"
6984                "};",
6985                MergeInlineOnly);
6986   verifyFormat("int f() {\n"
6987                "  return 42;\n"
6988                "}",
6989                MergeInlineOnly);
6990 
6991   // SFS_Inline implies SFS_Empty
6992   verifyFormat("class C {\n"
6993                "  int f() {}\n"
6994                "};",
6995                MergeInlineOnly);
6996   verifyFormat("int f() {}", MergeInlineOnly);
6997 
6998   // Also verify behavior when BraceWrapping.AfterFunction = true
6999   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7000   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7001   verifyFormat("class C {\n"
7002                "  int f() { return 42; }\n"
7003                "};",
7004                MergeInlineOnly);
7005   verifyFormat("int f()\n"
7006                "{\n"
7007                "  return 42;\n"
7008                "}",
7009                MergeInlineOnly);
7010 
7011   // SFS_Inline implies SFS_Empty
7012   verifyFormat("int f() {}", MergeInlineOnly);
7013   verifyFormat("class C {\n"
7014                "  int f() {}\n"
7015                "};",
7016                MergeInlineOnly);
7017 }
7018 
7019 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
7020   FormatStyle MergeInlineOnly = getLLVMStyle();
7021   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
7022       FormatStyle::SFS_InlineOnly;
7023   verifyFormat("class C {\n"
7024                "  int f() { return 42; }\n"
7025                "};",
7026                MergeInlineOnly);
7027   verifyFormat("int f() {\n"
7028                "  return 42;\n"
7029                "}",
7030                MergeInlineOnly);
7031 
7032   // SFS_InlineOnly does not imply SFS_Empty
7033   verifyFormat("class C {\n"
7034                "  int f() {}\n"
7035                "};",
7036                MergeInlineOnly);
7037   verifyFormat("int f() {\n"
7038                "}",
7039                MergeInlineOnly);
7040 
7041   // Also verify behavior when BraceWrapping.AfterFunction = true
7042   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
7043   MergeInlineOnly.BraceWrapping.AfterFunction = true;
7044   verifyFormat("class C {\n"
7045                "  int f() { return 42; }\n"
7046                "};",
7047                MergeInlineOnly);
7048   verifyFormat("int f()\n"
7049                "{\n"
7050                "  return 42;\n"
7051                "}",
7052                MergeInlineOnly);
7053 
7054   // SFS_InlineOnly does not imply SFS_Empty
7055   verifyFormat("int f()\n"
7056                "{\n"
7057                "}",
7058                MergeInlineOnly);
7059   verifyFormat("class C {\n"
7060                "  int f() {}\n"
7061                "};",
7062                MergeInlineOnly);
7063 }
7064 
7065 TEST_F(FormatTest, SplitEmptyFunction) {
7066   FormatStyle Style = getLLVMStyle();
7067   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7068   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7069   Style.BraceWrapping.AfterFunction = true;
7070   Style.BraceWrapping.SplitEmptyFunction = false;
7071   Style.ColumnLimit = 40;
7072 
7073   verifyFormat("int f()\n"
7074                "{}",
7075                Style);
7076   verifyFormat("int f()\n"
7077                "{\n"
7078                "  return 42;\n"
7079                "}",
7080                Style);
7081   verifyFormat("int f()\n"
7082                "{\n"
7083                "  // some comment\n"
7084                "}",
7085                Style);
7086 
7087   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
7088   verifyFormat("int f() {}", Style);
7089   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7090                "{}",
7091                Style);
7092   verifyFormat("int f()\n"
7093                "{\n"
7094                "  return 0;\n"
7095                "}",
7096                Style);
7097 
7098   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7099   verifyFormat("class Foo {\n"
7100                "  int f() {}\n"
7101                "};\n",
7102                Style);
7103   verifyFormat("class Foo {\n"
7104                "  int f() { return 0; }\n"
7105                "};\n",
7106                Style);
7107   verifyFormat("class Foo {\n"
7108                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7109                "  {}\n"
7110                "};\n",
7111                Style);
7112   verifyFormat("class Foo {\n"
7113                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7114                "  {\n"
7115                "    return 0;\n"
7116                "  }\n"
7117                "};\n",
7118                Style);
7119 
7120   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7121   verifyFormat("int f() {}", Style);
7122   verifyFormat("int f() { return 0; }", Style);
7123   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7124                "{}",
7125                Style);
7126   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
7127                "{\n"
7128                "  return 0;\n"
7129                "}",
7130                Style);
7131 }
7132 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
7133   FormatStyle Style = getLLVMStyle();
7134   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7135   verifyFormat("#ifdef A\n"
7136                "int f() {}\n"
7137                "#else\n"
7138                "int g() {}\n"
7139                "#endif",
7140                Style);
7141 }
7142 
7143 TEST_F(FormatTest, SplitEmptyClass) {
7144   FormatStyle Style = getLLVMStyle();
7145   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7146   Style.BraceWrapping.AfterClass = true;
7147   Style.BraceWrapping.SplitEmptyRecord = false;
7148 
7149   verifyFormat("class Foo\n"
7150                "{};",
7151                Style);
7152   verifyFormat("/* something */ class Foo\n"
7153                "{};",
7154                Style);
7155   verifyFormat("template <typename X> class Foo\n"
7156                "{};",
7157                Style);
7158   verifyFormat("class Foo\n"
7159                "{\n"
7160                "  Foo();\n"
7161                "};",
7162                Style);
7163   verifyFormat("typedef class Foo\n"
7164                "{\n"
7165                "} Foo_t;",
7166                Style);
7167 }
7168 
7169 TEST_F(FormatTest, SplitEmptyStruct) {
7170   FormatStyle Style = getLLVMStyle();
7171   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7172   Style.BraceWrapping.AfterStruct = true;
7173   Style.BraceWrapping.SplitEmptyRecord = false;
7174 
7175   verifyFormat("struct Foo\n"
7176                "{};",
7177                Style);
7178   verifyFormat("/* something */ struct Foo\n"
7179                "{};",
7180                Style);
7181   verifyFormat("template <typename X> struct Foo\n"
7182                "{};",
7183                Style);
7184   verifyFormat("struct Foo\n"
7185                "{\n"
7186                "  Foo();\n"
7187                "};",
7188                Style);
7189   verifyFormat("typedef struct Foo\n"
7190                "{\n"
7191                "} Foo_t;",
7192                Style);
7193   //typedef struct Bar {} Bar_t;
7194 }
7195 
7196 TEST_F(FormatTest, SplitEmptyUnion) {
7197   FormatStyle Style = getLLVMStyle();
7198   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7199   Style.BraceWrapping.AfterUnion = true;
7200   Style.BraceWrapping.SplitEmptyRecord = false;
7201 
7202   verifyFormat("union Foo\n"
7203                "{};",
7204                Style);
7205   verifyFormat("/* something */ union Foo\n"
7206                "{};",
7207                Style);
7208   verifyFormat("union Foo\n"
7209                "{\n"
7210                "  A,\n"
7211                "};",
7212                Style);
7213   verifyFormat("typedef union Foo\n"
7214                "{\n"
7215                "} Foo_t;",
7216                Style);
7217 }
7218 
7219 TEST_F(FormatTest, SplitEmptyNamespace) {
7220   FormatStyle Style = getLLVMStyle();
7221   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7222   Style.BraceWrapping.AfterNamespace = true;
7223   Style.BraceWrapping.SplitEmptyNamespace = false;
7224 
7225   verifyFormat("namespace Foo\n"
7226                "{};",
7227                Style);
7228   verifyFormat("/* something */ namespace Foo\n"
7229                "{};",
7230                Style);
7231   verifyFormat("inline namespace Foo\n"
7232                "{};",
7233                Style);
7234   verifyFormat("namespace Foo\n"
7235                "{\n"
7236                "void Bar();\n"
7237                "};",
7238                Style);
7239 }
7240 
7241 TEST_F(FormatTest, NeverMergeShortRecords) {
7242   FormatStyle Style = getLLVMStyle();
7243 
7244   verifyFormat("class Foo {\n"
7245                "  Foo();\n"
7246                "};",
7247                Style);
7248   verifyFormat("typedef class Foo {\n"
7249                "  Foo();\n"
7250                "} Foo_t;",
7251                Style);
7252   verifyFormat("struct Foo {\n"
7253                "  Foo();\n"
7254                "};",
7255                Style);
7256   verifyFormat("typedef struct Foo {\n"
7257                "  Foo();\n"
7258                "} Foo_t;",
7259                Style);
7260   verifyFormat("union Foo {\n"
7261                "  A,\n"
7262                "};",
7263                Style);
7264   verifyFormat("typedef union Foo {\n"
7265                "  A,\n"
7266                "} Foo_t;",
7267                Style);
7268   verifyFormat("namespace Foo {\n"
7269                "void Bar();\n"
7270                "};",
7271                Style);
7272 
7273   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
7274   Style.BraceWrapping.AfterClass = true;
7275   Style.BraceWrapping.AfterStruct = true;
7276   Style.BraceWrapping.AfterUnion = true;
7277   Style.BraceWrapping.AfterNamespace = true;
7278   verifyFormat("class Foo\n"
7279                "{\n"
7280                "  Foo();\n"
7281                "};",
7282                Style);
7283   verifyFormat("typedef class Foo\n"
7284                "{\n"
7285                "  Foo();\n"
7286                "} Foo_t;",
7287                Style);
7288   verifyFormat("struct Foo\n"
7289                "{\n"
7290                "  Foo();\n"
7291                "};",
7292                Style);
7293   verifyFormat("typedef struct Foo\n"
7294                "{\n"
7295                "  Foo();\n"
7296                "} Foo_t;",
7297                Style);
7298   verifyFormat("union Foo\n"
7299                "{\n"
7300                "  A,\n"
7301                "};",
7302                Style);
7303   verifyFormat("typedef union Foo\n"
7304                "{\n"
7305                "  A,\n"
7306                "} Foo_t;",
7307                Style);
7308   verifyFormat("namespace Foo\n"
7309                "{\n"
7310                "void Bar();\n"
7311                "};",
7312                Style);
7313 }
7314 
7315 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
7316   // Elaborate type variable declarations.
7317   verifyFormat("struct foo a = {bar};\nint n;");
7318   verifyFormat("class foo a = {bar};\nint n;");
7319   verifyFormat("union foo a = {bar};\nint n;");
7320 
7321   // Elaborate types inside function definitions.
7322   verifyFormat("struct foo f() {}\nint n;");
7323   verifyFormat("class foo f() {}\nint n;");
7324   verifyFormat("union foo f() {}\nint n;");
7325 
7326   // Templates.
7327   verifyFormat("template <class X> void f() {}\nint n;");
7328   verifyFormat("template <struct X> void f() {}\nint n;");
7329   verifyFormat("template <union X> void f() {}\nint n;");
7330 
7331   // Actual definitions...
7332   verifyFormat("struct {\n} n;");
7333   verifyFormat(
7334       "template <template <class T, class Y>, class Z> class X {\n} n;");
7335   verifyFormat("union Z {\n  int n;\n} x;");
7336   verifyFormat("class MACRO Z {\n} n;");
7337   verifyFormat("class MACRO(X) Z {\n} n;");
7338   verifyFormat("class __attribute__(X) Z {\n} n;");
7339   verifyFormat("class __declspec(X) Z {\n} n;");
7340   verifyFormat("class A##B##C {\n} n;");
7341   verifyFormat("class alignas(16) Z {\n} n;");
7342   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
7343   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
7344 
7345   // Redefinition from nested context:
7346   verifyFormat("class A::B::C {\n} n;");
7347 
7348   // Template definitions.
7349   verifyFormat(
7350       "template <typename F>\n"
7351       "Matcher(const Matcher<F> &Other,\n"
7352       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
7353       "                             !is_same<F, T>::value>::type * = 0)\n"
7354       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
7355 
7356   // FIXME: This is still incorrectly handled at the formatter side.
7357   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
7358   verifyFormat("int i = SomeFunction(a<b, a> b);");
7359 
7360   // FIXME:
7361   // This now gets parsed incorrectly as class definition.
7362   // verifyFormat("class A<int> f() {\n}\nint n;");
7363 
7364   // Elaborate types where incorrectly parsing the structural element would
7365   // break the indent.
7366   verifyFormat("if (true)\n"
7367                "  class X x;\n"
7368                "else\n"
7369                "  f();\n");
7370 
7371   // This is simply incomplete. Formatting is not important, but must not crash.
7372   verifyFormat("class A:");
7373 }
7374 
7375 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
7376   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
7377             format("#error Leave     all         white!!!!! space* alone!\n"));
7378   EXPECT_EQ(
7379       "#warning Leave     all         white!!!!! space* alone!\n",
7380       format("#warning Leave     all         white!!!!! space* alone!\n"));
7381   EXPECT_EQ("#error 1", format("  #  error   1"));
7382   EXPECT_EQ("#warning 1", format("  #  warning 1"));
7383 }
7384 
7385 TEST_F(FormatTest, FormatHashIfExpressions) {
7386   verifyFormat("#if AAAA && BBBB");
7387   verifyFormat("#if (AAAA && BBBB)");
7388   verifyFormat("#elif (AAAA && BBBB)");
7389   // FIXME: Come up with a better indentation for #elif.
7390   verifyFormat(
7391       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
7392       "    defined(BBBBBBBB)\n"
7393       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
7394       "    defined(BBBBBBBB)\n"
7395       "#endif",
7396       getLLVMStyleWithColumns(65));
7397 }
7398 
7399 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
7400   FormatStyle AllowsMergedIf = getGoogleStyle();
7401   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
7402   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
7403   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
7404   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
7405   EXPECT_EQ("if (true) return 42;",
7406             format("if (true)\nreturn 42;", AllowsMergedIf));
7407   FormatStyle ShortMergedIf = AllowsMergedIf;
7408   ShortMergedIf.ColumnLimit = 25;
7409   verifyFormat("#define A \\\n"
7410                "  if (true) return 42;",
7411                ShortMergedIf);
7412   verifyFormat("#define A \\\n"
7413                "  f();    \\\n"
7414                "  if (true)\n"
7415                "#define B",
7416                ShortMergedIf);
7417   verifyFormat("#define A \\\n"
7418                "  f();    \\\n"
7419                "  if (true)\n"
7420                "g();",
7421                ShortMergedIf);
7422   verifyFormat("{\n"
7423                "#ifdef A\n"
7424                "  // Comment\n"
7425                "  if (true) continue;\n"
7426                "#endif\n"
7427                "  // Comment\n"
7428                "  if (true) continue;\n"
7429                "}",
7430                ShortMergedIf);
7431   ShortMergedIf.ColumnLimit = 33;
7432   verifyFormat("#define A \\\n"
7433                "  if constexpr (true) return 42;",
7434                ShortMergedIf);
7435   ShortMergedIf.ColumnLimit = 29;
7436   verifyFormat("#define A                   \\\n"
7437                "  if (aaaaaaaaaa) return 1; \\\n"
7438                "  return 2;",
7439                ShortMergedIf);
7440   ShortMergedIf.ColumnLimit = 28;
7441   verifyFormat("#define A         \\\n"
7442                "  if (aaaaaaaaaa) \\\n"
7443                "    return 1;     \\\n"
7444                "  return 2;",
7445                ShortMergedIf);
7446   verifyFormat("#define A                \\\n"
7447                "  if constexpr (aaaaaaa) \\\n"
7448                "    return 1;            \\\n"
7449                "  return 2;",
7450                ShortMergedIf);
7451 }
7452 
7453 TEST_F(FormatTest, FormatStarDependingOnContext) {
7454   verifyFormat("void f(int *a);");
7455   verifyFormat("void f() { f(fint * b); }");
7456   verifyFormat("class A {\n  void f(int *a);\n};");
7457   verifyFormat("class A {\n  int *a;\n};");
7458   verifyFormat("namespace a {\n"
7459                "namespace b {\n"
7460                "class A {\n"
7461                "  void f() {}\n"
7462                "  int *a;\n"
7463                "};\n"
7464                "} // namespace b\n"
7465                "} // namespace a");
7466 }
7467 
7468 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
7469   verifyFormat("while");
7470   verifyFormat("operator");
7471 }
7472 
7473 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
7474   // This code would be painfully slow to format if we didn't skip it.
7475   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
7476                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7477                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7478                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7479                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7480                    "A(1, 1)\n"
7481                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7482                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7483                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7484                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7485                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7486                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7487                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7488                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7489                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7490                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7491   // Deeply nested part is untouched, rest is formatted.
7492   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7493             format(std::string("int    i;\n") + Code + "int    j;\n",
7494                    getLLVMStyle(), SC_ExpectIncomplete));
7495 }
7496 
7497 //===----------------------------------------------------------------------===//
7498 // Objective-C tests.
7499 //===----------------------------------------------------------------------===//
7500 
7501 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7502   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7503   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7504             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7505   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7506   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7507   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7508             format("-(NSInteger)Method3:(id)anObject;"));
7509   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7510             format("-(NSInteger)Method4:(id)anObject;"));
7511   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7512             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7513   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7514             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7515   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7516             "forAllCells:(BOOL)flag;",
7517             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7518                    "forAllCells:(BOOL)flag;"));
7519 
7520   // Very long objectiveC method declaration.
7521   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7522                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7523   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7524                "                    inRange:(NSRange)range\n"
7525                "                   outRange:(NSRange)out_range\n"
7526                "                  outRange1:(NSRange)out_range1\n"
7527                "                  outRange2:(NSRange)out_range2\n"
7528                "                  outRange3:(NSRange)out_range3\n"
7529                "                  outRange4:(NSRange)out_range4\n"
7530                "                  outRange5:(NSRange)out_range5\n"
7531                "                  outRange6:(NSRange)out_range6\n"
7532                "                  outRange7:(NSRange)out_range7\n"
7533                "                  outRange8:(NSRange)out_range8\n"
7534                "                  outRange9:(NSRange)out_range9;");
7535 
7536   // When the function name has to be wrapped.
7537   FormatStyle Style = getLLVMStyle();
7538   Style.IndentWrappedFunctionNames = false;
7539   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7540                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7541                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7542                "}",
7543                Style);
7544   Style.IndentWrappedFunctionNames = true;
7545   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7546                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7547                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7548                "}",
7549                Style);
7550 
7551   verifyFormat("- (int)sum:(vector<int>)numbers;");
7552   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7553   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7554   // protocol lists (but not for template classes):
7555   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7556 
7557   verifyFormat("- (int (*)())foo:(int (*)())f;");
7558   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7559 
7560   // If there's no return type (very rare in practice!), LLVM and Google style
7561   // agree.
7562   verifyFormat("- foo;");
7563   verifyFormat("- foo:(int)f;");
7564   verifyGoogleFormat("- foo:(int)foo;");
7565 }
7566 
7567 
7568 TEST_F(FormatTest, BreaksStringLiterals) {
7569   EXPECT_EQ("\"some text \"\n"
7570             "\"other\";",
7571             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7572   EXPECT_EQ("\"some text \"\n"
7573             "\"other\";",
7574             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7575   EXPECT_EQ(
7576       "#define A  \\\n"
7577       "  \"some \"  \\\n"
7578       "  \"text \"  \\\n"
7579       "  \"other\";",
7580       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7581   EXPECT_EQ(
7582       "#define A  \\\n"
7583       "  \"so \"    \\\n"
7584       "  \"text \"  \\\n"
7585       "  \"other\";",
7586       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7587 
7588   EXPECT_EQ("\"some text\"",
7589             format("\"some text\"", getLLVMStyleWithColumns(1)));
7590   EXPECT_EQ("\"some text\"",
7591             format("\"some text\"", getLLVMStyleWithColumns(11)));
7592   EXPECT_EQ("\"some \"\n"
7593             "\"text\"",
7594             format("\"some text\"", getLLVMStyleWithColumns(10)));
7595   EXPECT_EQ("\"some \"\n"
7596             "\"text\"",
7597             format("\"some text\"", getLLVMStyleWithColumns(7)));
7598   EXPECT_EQ("\"some\"\n"
7599             "\" tex\"\n"
7600             "\"t\"",
7601             format("\"some text\"", getLLVMStyleWithColumns(6)));
7602   EXPECT_EQ("\"some\"\n"
7603             "\" tex\"\n"
7604             "\" and\"",
7605             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7606   EXPECT_EQ("\"some\"\n"
7607             "\"/tex\"\n"
7608             "\"/and\"",
7609             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7610 
7611   EXPECT_EQ("variable =\n"
7612             "    \"long string \"\n"
7613             "    \"literal\";",
7614             format("variable = \"long string literal\";",
7615                    getLLVMStyleWithColumns(20)));
7616 
7617   EXPECT_EQ("variable = f(\n"
7618             "    \"long string \"\n"
7619             "    \"literal\",\n"
7620             "    short,\n"
7621             "    loooooooooooooooooooong);",
7622             format("variable = f(\"long string literal\", short, "
7623                    "loooooooooooooooooooong);",
7624                    getLLVMStyleWithColumns(20)));
7625 
7626   EXPECT_EQ(
7627       "f(g(\"long string \"\n"
7628       "    \"literal\"),\n"
7629       "  b);",
7630       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7631   EXPECT_EQ("f(g(\"long string \"\n"
7632             "    \"literal\",\n"
7633             "    a),\n"
7634             "  b);",
7635             format("f(g(\"long string literal\", a), b);",
7636                    getLLVMStyleWithColumns(20)));
7637   EXPECT_EQ(
7638       "f(\"one two\".split(\n"
7639       "    variable));",
7640       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7641   EXPECT_EQ("f(\"one two three four five six \"\n"
7642             "  \"seven\".split(\n"
7643             "      really_looooong_variable));",
7644             format("f(\"one two three four five six seven\"."
7645                    "split(really_looooong_variable));",
7646                    getLLVMStyleWithColumns(33)));
7647 
7648   EXPECT_EQ("f(\"some \"\n"
7649             "  \"text\",\n"
7650             "  other);",
7651             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7652 
7653   // Only break as a last resort.
7654   verifyFormat(
7655       "aaaaaaaaaaaaaaaaaaaa(\n"
7656       "    aaaaaaaaaaaaaaaaaaaa,\n"
7657       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7658 
7659   EXPECT_EQ("\"splitmea\"\n"
7660             "\"trandomp\"\n"
7661             "\"oint\"",
7662             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7663 
7664   EXPECT_EQ("\"split/\"\n"
7665             "\"pathat/\"\n"
7666             "\"slashes\"",
7667             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7668 
7669   EXPECT_EQ("\"split/\"\n"
7670             "\"pathat/\"\n"
7671             "\"slashes\"",
7672             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7673   EXPECT_EQ("\"split at \"\n"
7674             "\"spaces/at/\"\n"
7675             "\"slashes.at.any$\"\n"
7676             "\"non-alphanumeric%\"\n"
7677             "\"1111111111characte\"\n"
7678             "\"rs\"",
7679             format("\"split at "
7680                    "spaces/at/"
7681                    "slashes.at."
7682                    "any$non-"
7683                    "alphanumeric%"
7684                    "1111111111characte"
7685                    "rs\"",
7686                    getLLVMStyleWithColumns(20)));
7687 
7688   // Verify that splitting the strings understands
7689   // Style::AlwaysBreakBeforeMultilineStrings.
7690   EXPECT_EQ(
7691       "aaaaaaaaaaaa(\n"
7692       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7693       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7694       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7695              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7696              "aaaaaaaaaaaaaaaaaaaaaa\");",
7697              getGoogleStyle()));
7698   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7699             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7700             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7701                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7702                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7703                    getGoogleStyle()));
7704   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7705             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7706             format("llvm::outs() << "
7707                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7708                    "aaaaaaaaaaaaaaaaaaa\";"));
7709   EXPECT_EQ("ffff(\n"
7710             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7711             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7712             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7713                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7714                    getGoogleStyle()));
7715 
7716   FormatStyle Style = getLLVMStyleWithColumns(12);
7717   Style.BreakStringLiterals = false;
7718   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7719 
7720   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7721   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7722   EXPECT_EQ("#define A \\\n"
7723             "  \"some \" \\\n"
7724             "  \"text \" \\\n"
7725             "  \"other\";",
7726             format("#define A \"some text other\";", AlignLeft));
7727 }
7728 
7729 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7730   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7731   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7732   EXPECT_EQ("int i = a(b());",
7733             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7734 }
7735 
7736 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7737   EXPECT_EQ(
7738       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7739       "(\n"
7740       "    \"x\t\");",
7741       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7742              "aaaaaaa("
7743              "\"x\t\");"));
7744 }
7745 
7746 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7747   EXPECT_EQ(
7748       "u8\"utf8 string \"\n"
7749       "u8\"literal\";",
7750       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7751   EXPECT_EQ(
7752       "u\"utf16 string \"\n"
7753       "u\"literal\";",
7754       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7755   EXPECT_EQ(
7756       "U\"utf32 string \"\n"
7757       "U\"literal\";",
7758       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7759   EXPECT_EQ("L\"wide string \"\n"
7760             "L\"literal\";",
7761             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7762   EXPECT_EQ("@\"NSString \"\n"
7763             "@\"literal\";",
7764             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7765   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7766 
7767   // This input makes clang-format try to split the incomplete unicode escape
7768   // sequence, which used to lead to a crasher.
7769   verifyNoCrash(
7770       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7771       getLLVMStyleWithColumns(60));
7772 }
7773 
7774 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7775   FormatStyle Style = getGoogleStyleWithColumns(15);
7776   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7777   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7778   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7779   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7780   EXPECT_EQ("u8R\"x(raw literal)x\";",
7781             format("u8R\"x(raw literal)x\";", Style));
7782 }
7783 
7784 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7785   FormatStyle Style = getLLVMStyleWithColumns(20);
7786   EXPECT_EQ(
7787       "_T(\"aaaaaaaaaaaaaa\")\n"
7788       "_T(\"aaaaaaaaaaaaaa\")\n"
7789       "_T(\"aaaaaaaaaaaa\")",
7790       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7791   EXPECT_EQ("f(x,\n"
7792             "  _T(\"aaaaaaaaaaaa\")\n"
7793             "  _T(\"aaa\"),\n"
7794             "  z);",
7795             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7796 
7797   // FIXME: Handle embedded spaces in one iteration.
7798   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7799   //            "_T(\"aaaaaaaaaaaaa\")\n"
7800   //            "_T(\"aaaaaaaaaaaaa\")\n"
7801   //            "_T(\"a\")",
7802   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7803   //                   getLLVMStyleWithColumns(20)));
7804   EXPECT_EQ(
7805       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7806       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7807   EXPECT_EQ("f(\n"
7808             "#if !TEST\n"
7809             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7810             "#endif\n"
7811             ");",
7812             format("f(\n"
7813                    "#if !TEST\n"
7814                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7815                    "#endif\n"
7816                    ");"));
7817   EXPECT_EQ("f(\n"
7818             "\n"
7819             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7820             format("f(\n"
7821                    "\n"
7822                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7823 }
7824 
7825 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7826   // In a function call with two operands, the second can be broken with no line
7827   // break before it.
7828   EXPECT_EQ("func(a, \"long long \"\n"
7829             "        \"long long\");",
7830             format("func(a, \"long long long long\");",
7831                    getLLVMStyleWithColumns(24)));
7832   // In a function call with three operands, the second must be broken with a
7833   // line break before it.
7834   EXPECT_EQ("func(a,\n"
7835             "     \"long long long \"\n"
7836             "     \"long\",\n"
7837             "     c);",
7838             format("func(a, \"long long long long\", c);",
7839                    getLLVMStyleWithColumns(24)));
7840   // In a function call with three operands, the third must be broken with a
7841   // line break before it.
7842   EXPECT_EQ("func(a, b,\n"
7843             "     \"long long long \"\n"
7844             "     \"long\");",
7845             format("func(a, b, \"long long long long\");",
7846                    getLLVMStyleWithColumns(24)));
7847   // In a function call with three operands, both the second and the third must
7848   // be broken with a line break before them.
7849   EXPECT_EQ("func(a,\n"
7850             "     \"long long long \"\n"
7851             "     \"long\",\n"
7852             "     \"long long long \"\n"
7853             "     \"long\");",
7854             format("func(a, \"long long long long\", \"long long long long\");",
7855                    getLLVMStyleWithColumns(24)));
7856   // In a chain of << with two operands, the second can be broken with no line
7857   // break before it.
7858   EXPECT_EQ("a << \"line line \"\n"
7859             "     \"line\";",
7860             format("a << \"line line line\";",
7861                    getLLVMStyleWithColumns(20)));
7862   // In a chain of << with three operands, the second can be broken with no line
7863   // break before it.
7864   EXPECT_EQ("abcde << \"line \"\n"
7865             "         \"line line\"\n"
7866             "      << c;",
7867             format("abcde << \"line line line\" << c;",
7868                    getLLVMStyleWithColumns(20)));
7869   // In a chain of << with three operands, the third must be broken with a line
7870   // break before it.
7871   EXPECT_EQ("a << b\n"
7872             "  << \"line line \"\n"
7873             "     \"line\";",
7874             format("a << b << \"line line line\";",
7875                    getLLVMStyleWithColumns(20)));
7876   // In a chain of << with three operands, the second can be broken with no line
7877   // break before it and the third must be broken with a line break before it.
7878   EXPECT_EQ("abcd << \"line line \"\n"
7879             "        \"line\"\n"
7880             "     << \"line line \"\n"
7881             "        \"line\";",
7882             format("abcd << \"line line line\" << \"line line line\";",
7883                    getLLVMStyleWithColumns(20)));
7884   // In a chain of binary operators with two operands, the second can be broken
7885   // with no line break before it.
7886   EXPECT_EQ("abcd + \"line line \"\n"
7887             "       \"line line\";",
7888             format("abcd + \"line line line line\";",
7889                    getLLVMStyleWithColumns(20)));
7890   // In a chain of binary operators with three operands, the second must be
7891   // broken with a line break before it.
7892   EXPECT_EQ("abcd +\n"
7893             "    \"line line \"\n"
7894             "    \"line line\" +\n"
7895             "    e;",
7896             format("abcd + \"line line line line\" + e;",
7897                    getLLVMStyleWithColumns(20)));
7898   // In a function call with two operands, with AlignAfterOpenBracket enabled,
7899   // the first must be broken with a line break before it.
7900   FormatStyle Style = getLLVMStyleWithColumns(25);
7901   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7902   EXPECT_EQ("someFunction(\n"
7903             "    \"long long long \"\n"
7904             "    \"long\",\n"
7905             "    a);",
7906             format("someFunction(\"long long long long\", a);", Style));
7907 }
7908 
7909 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7910   EXPECT_EQ(
7911       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7912       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7913       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7914       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7915              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7916              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7917 }
7918 
7919 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7920   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7921             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7922   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7923             "multiline raw string literal xxxxxxxxxxxxxx\n"
7924             ")x\",\n"
7925             "              a),\n"
7926             "            b);",
7927             format("fffffffffff(g(R\"x(\n"
7928                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7929                    ")x\", a), b);",
7930                    getGoogleStyleWithColumns(20)));
7931   EXPECT_EQ("fffffffffff(\n"
7932             "    g(R\"x(qqq\n"
7933             "multiline raw string literal xxxxxxxxxxxxxx\n"
7934             ")x\",\n"
7935             "      a),\n"
7936             "    b);",
7937             format("fffffffffff(g(R\"x(qqq\n"
7938                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7939                    ")x\", a), b);",
7940                    getGoogleStyleWithColumns(20)));
7941 
7942   EXPECT_EQ("fffffffffff(R\"x(\n"
7943             "multiline raw string literal xxxxxxxxxxxxxx\n"
7944             ")x\");",
7945             format("fffffffffff(R\"x(\n"
7946                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7947                    ")x\");",
7948                    getGoogleStyleWithColumns(20)));
7949   EXPECT_EQ("fffffffffff(R\"x(\n"
7950             "multiline raw string literal xxxxxxxxxxxxxx\n"
7951             ")x\" + bbbbbb);",
7952             format("fffffffffff(R\"x(\n"
7953                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7954                    ")x\" +   bbbbbb);",
7955                    getGoogleStyleWithColumns(20)));
7956   EXPECT_EQ("fffffffffff(\n"
7957             "    R\"x(\n"
7958             "multiline raw string literal xxxxxxxxxxxxxx\n"
7959             ")x\" +\n"
7960             "    bbbbbb);",
7961             format("fffffffffff(\n"
7962                    " R\"x(\n"
7963                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7964                    ")x\" + bbbbbb);",
7965                    getGoogleStyleWithColumns(20)));
7966 }
7967 
7968 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7969   verifyFormat("string a = \"unterminated;");
7970   EXPECT_EQ("function(\"unterminated,\n"
7971             "         OtherParameter);",
7972             format("function(  \"unterminated,\n"
7973                    "    OtherParameter);"));
7974 }
7975 
7976 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7977   FormatStyle Style = getLLVMStyle();
7978   Style.Standard = FormatStyle::LS_Cpp03;
7979   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7980             format("#define x(_a) printf(\"foo\"_a);", Style));
7981 }
7982 
7983 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7984 
7985 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7986   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7987             "             \"ddeeefff\");",
7988             format("someFunction(\"aaabbbcccdddeeefff\");",
7989                    getLLVMStyleWithColumns(25)));
7990   EXPECT_EQ("someFunction1234567890(\n"
7991             "    \"aaabbbcccdddeeefff\");",
7992             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7993                    getLLVMStyleWithColumns(26)));
7994   EXPECT_EQ("someFunction1234567890(\n"
7995             "    \"aaabbbcccdddeeeff\"\n"
7996             "    \"f\");",
7997             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7998                    getLLVMStyleWithColumns(25)));
7999   EXPECT_EQ("someFunction1234567890(\n"
8000             "    \"aaabbbcccdddeeeff\"\n"
8001             "    \"f\");",
8002             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
8003                    getLLVMStyleWithColumns(24)));
8004   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8005             "             \"ddde \"\n"
8006             "             \"efff\");",
8007             format("someFunction(\"aaabbbcc ddde efff\");",
8008                    getLLVMStyleWithColumns(25)));
8009   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
8010             "             \"ddeeefff\");",
8011             format("someFunction(\"aaabbbccc ddeeefff\");",
8012                    getLLVMStyleWithColumns(25)));
8013   EXPECT_EQ("someFunction1234567890(\n"
8014             "    \"aaabb \"\n"
8015             "    \"cccdddeeefff\");",
8016             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
8017                    getLLVMStyleWithColumns(25)));
8018   EXPECT_EQ("#define A          \\\n"
8019             "  string s =       \\\n"
8020             "      \"123456789\"  \\\n"
8021             "      \"0\";         \\\n"
8022             "  int i;",
8023             format("#define A string s = \"1234567890\"; int i;",
8024                    getLLVMStyleWithColumns(20)));
8025   // FIXME: Put additional penalties on breaking at non-whitespace locations.
8026   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
8027             "             \"dddeeeff\"\n"
8028             "             \"f\");",
8029             format("someFunction(\"aaabbbcc dddeeefff\");",
8030                    getLLVMStyleWithColumns(25)));
8031 }
8032 
8033 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
8034   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
8035   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
8036   EXPECT_EQ("\"test\"\n"
8037             "\"\\n\"",
8038             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
8039   EXPECT_EQ("\"tes\\\\\"\n"
8040             "\"n\"",
8041             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
8042   EXPECT_EQ("\"\\\\\\\\\"\n"
8043             "\"\\n\"",
8044             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
8045   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
8046   EXPECT_EQ("\"\\uff01\"\n"
8047             "\"test\"",
8048             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
8049   EXPECT_EQ("\"\\Uff01ff02\"",
8050             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
8051   EXPECT_EQ("\"\\x000000000001\"\n"
8052             "\"next\"",
8053             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
8054   EXPECT_EQ("\"\\x000000000001next\"",
8055             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
8056   EXPECT_EQ("\"\\x000000000001\"",
8057             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
8058   EXPECT_EQ("\"test\"\n"
8059             "\"\\000000\"\n"
8060             "\"000001\"",
8061             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
8062   EXPECT_EQ("\"test\\000\"\n"
8063             "\"00000000\"\n"
8064             "\"1\"",
8065             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
8066 }
8067 
8068 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
8069   verifyFormat("void f() {\n"
8070                "  return g() {}\n"
8071                "  void h() {}");
8072   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
8073                "g();\n"
8074                "}");
8075 }
8076 
8077 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
8078   verifyFormat(
8079       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
8080 }
8081 
8082 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
8083   verifyFormat("class X {\n"
8084                "  void f() {\n"
8085                "  }\n"
8086                "};",
8087                getLLVMStyleWithColumns(12));
8088 }
8089 
8090 TEST_F(FormatTest, ConfigurableIndentWidth) {
8091   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
8092   EightIndent.IndentWidth = 8;
8093   EightIndent.ContinuationIndentWidth = 8;
8094   verifyFormat("void f() {\n"
8095                "        someFunction();\n"
8096                "        if (true) {\n"
8097                "                f();\n"
8098                "        }\n"
8099                "}",
8100                EightIndent);
8101   verifyFormat("class X {\n"
8102                "        void f() {\n"
8103                "        }\n"
8104                "};",
8105                EightIndent);
8106   verifyFormat("int x[] = {\n"
8107                "        call(),\n"
8108                "        call()};",
8109                EightIndent);
8110 }
8111 
8112 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
8113   verifyFormat("double\n"
8114                "f();",
8115                getLLVMStyleWithColumns(8));
8116 }
8117 
8118 TEST_F(FormatTest, ConfigurableUseOfTab) {
8119   FormatStyle Tab = getLLVMStyleWithColumns(42);
8120   Tab.IndentWidth = 8;
8121   Tab.UseTab = FormatStyle::UT_Always;
8122   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8123 
8124   EXPECT_EQ("if (aaaaaaaa && // q\n"
8125             "    bb)\t\t// w\n"
8126             "\t;",
8127             format("if (aaaaaaaa &&// q\n"
8128                    "bb)// w\n"
8129                    ";",
8130                    Tab));
8131   EXPECT_EQ("if (aaa && bbb) // w\n"
8132             "\t;",
8133             format("if(aaa&&bbb)// w\n"
8134                    ";",
8135                    Tab));
8136 
8137   verifyFormat("class X {\n"
8138                "\tvoid f() {\n"
8139                "\t\tsomeFunction(parameter1,\n"
8140                "\t\t\t     parameter2);\n"
8141                "\t}\n"
8142                "};",
8143                Tab);
8144   verifyFormat("#define A                        \\\n"
8145                "\tvoid f() {               \\\n"
8146                "\t\tsomeFunction(    \\\n"
8147                "\t\t    parameter1,  \\\n"
8148                "\t\t    parameter2); \\\n"
8149                "\t}",
8150                Tab);
8151 
8152   Tab.TabWidth = 4;
8153   Tab.IndentWidth = 8;
8154   verifyFormat("class TabWidth4Indent8 {\n"
8155                "\t\tvoid f() {\n"
8156                "\t\t\t\tsomeFunction(parameter1,\n"
8157                "\t\t\t\t\t\t\t parameter2);\n"
8158                "\t\t}\n"
8159                "};",
8160                Tab);
8161 
8162   Tab.TabWidth = 4;
8163   Tab.IndentWidth = 4;
8164   verifyFormat("class TabWidth4Indent4 {\n"
8165                "\tvoid f() {\n"
8166                "\t\tsomeFunction(parameter1,\n"
8167                "\t\t\t\t\t parameter2);\n"
8168                "\t}\n"
8169                "};",
8170                Tab);
8171 
8172   Tab.TabWidth = 8;
8173   Tab.IndentWidth = 4;
8174   verifyFormat("class TabWidth8Indent4 {\n"
8175                "    void f() {\n"
8176                "\tsomeFunction(parameter1,\n"
8177                "\t\t     parameter2);\n"
8178                "    }\n"
8179                "};",
8180                Tab);
8181 
8182   Tab.TabWidth = 8;
8183   Tab.IndentWidth = 8;
8184   EXPECT_EQ("/*\n"
8185             "\t      a\t\tcomment\n"
8186             "\t      in multiple lines\n"
8187             "       */",
8188             format("   /*\t \t \n"
8189                    " \t \t a\t\tcomment\t \t\n"
8190                    " \t \t in multiple lines\t\n"
8191                    " \t  */",
8192                    Tab));
8193 
8194   Tab.UseTab = FormatStyle::UT_ForIndentation;
8195   verifyFormat("{\n"
8196                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8197                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8198                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8199                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8200                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8201                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8202                "};",
8203                Tab);
8204   verifyFormat("enum AA {\n"
8205                "\ta1, // Force multiple lines\n"
8206                "\ta2,\n"
8207                "\ta3\n"
8208                "};",
8209                Tab);
8210   EXPECT_EQ("if (aaaaaaaa && // q\n"
8211             "    bb)         // w\n"
8212             "\t;",
8213             format("if (aaaaaaaa &&// q\n"
8214                    "bb)// w\n"
8215                    ";",
8216                    Tab));
8217   verifyFormat("class X {\n"
8218                "\tvoid f() {\n"
8219                "\t\tsomeFunction(parameter1,\n"
8220                "\t\t             parameter2);\n"
8221                "\t}\n"
8222                "};",
8223                Tab);
8224   verifyFormat("{\n"
8225                "\tQ(\n"
8226                "\t    {\n"
8227                "\t\t    int a;\n"
8228                "\t\t    someFunction(aaaaaaaa,\n"
8229                "\t\t                 bbbbbbb);\n"
8230                "\t    },\n"
8231                "\t    p);\n"
8232                "}",
8233                Tab);
8234   EXPECT_EQ("{\n"
8235             "\t/* aaaa\n"
8236             "\t   bbbb */\n"
8237             "}",
8238             format("{\n"
8239                    "/* aaaa\n"
8240                    "   bbbb */\n"
8241                    "}",
8242                    Tab));
8243   EXPECT_EQ("{\n"
8244             "\t/*\n"
8245             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8246             "\t  bbbbbbbbbbbbb\n"
8247             "\t*/\n"
8248             "}",
8249             format("{\n"
8250                    "/*\n"
8251                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8252                    "*/\n"
8253                    "}",
8254                    Tab));
8255   EXPECT_EQ("{\n"
8256             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8257             "\t// bbbbbbbbbbbbb\n"
8258             "}",
8259             format("{\n"
8260                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8261                    "}",
8262                    Tab));
8263   EXPECT_EQ("{\n"
8264             "\t/*\n"
8265             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8266             "\t  bbbbbbbbbbbbb\n"
8267             "\t*/\n"
8268             "}",
8269             format("{\n"
8270                    "\t/*\n"
8271                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8272                    "\t*/\n"
8273                    "}",
8274                    Tab));
8275   EXPECT_EQ("{\n"
8276             "\t/*\n"
8277             "\n"
8278             "\t*/\n"
8279             "}",
8280             format("{\n"
8281                    "\t/*\n"
8282                    "\n"
8283                    "\t*/\n"
8284                    "}",
8285                    Tab));
8286   EXPECT_EQ("{\n"
8287             "\t/*\n"
8288             " asdf\n"
8289             "\t*/\n"
8290             "}",
8291             format("{\n"
8292                    "\t/*\n"
8293                    " asdf\n"
8294                    "\t*/\n"
8295                    "}",
8296                    Tab));
8297 
8298   Tab.UseTab = FormatStyle::UT_Never;
8299   EXPECT_EQ("/*\n"
8300             "              a\t\tcomment\n"
8301             "              in multiple lines\n"
8302             "       */",
8303             format("   /*\t \t \n"
8304                    " \t \t a\t\tcomment\t \t\n"
8305                    " \t \t in multiple lines\t\n"
8306                    " \t  */",
8307                    Tab));
8308   EXPECT_EQ("/* some\n"
8309             "   comment */",
8310             format(" \t \t /* some\n"
8311                    " \t \t    comment */",
8312                    Tab));
8313   EXPECT_EQ("int a; /* some\n"
8314             "   comment */",
8315             format(" \t \t int a; /* some\n"
8316                    " \t \t    comment */",
8317                    Tab));
8318 
8319   EXPECT_EQ("int a; /* some\n"
8320             "comment */",
8321             format(" \t \t int\ta; /* some\n"
8322                    " \t \t    comment */",
8323                    Tab));
8324   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8325             "    comment */",
8326             format(" \t \t f(\"\t\t\"); /* some\n"
8327                    " \t \t    comment */",
8328                    Tab));
8329   EXPECT_EQ("{\n"
8330             "  /*\n"
8331             "   * Comment\n"
8332             "   */\n"
8333             "  int i;\n"
8334             "}",
8335             format("{\n"
8336                    "\t/*\n"
8337                    "\t * Comment\n"
8338                    "\t */\n"
8339                    "\t int i;\n"
8340                    "}"));
8341 
8342   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8343   Tab.TabWidth = 8;
8344   Tab.IndentWidth = 8;
8345   EXPECT_EQ("if (aaaaaaaa && // q\n"
8346             "    bb)         // w\n"
8347             "\t;",
8348             format("if (aaaaaaaa &&// q\n"
8349                    "bb)// w\n"
8350                    ";",
8351                    Tab));
8352   EXPECT_EQ("if (aaa && bbb) // w\n"
8353             "\t;",
8354             format("if(aaa&&bbb)// w\n"
8355                    ";",
8356                    Tab));
8357   verifyFormat("class X {\n"
8358                "\tvoid f() {\n"
8359                "\t\tsomeFunction(parameter1,\n"
8360                "\t\t\t     parameter2);\n"
8361                "\t}\n"
8362                "};",
8363                Tab);
8364   verifyFormat("#define A                        \\\n"
8365                "\tvoid f() {               \\\n"
8366                "\t\tsomeFunction(    \\\n"
8367                "\t\t    parameter1,  \\\n"
8368                "\t\t    parameter2); \\\n"
8369                "\t}",
8370                Tab);
8371   Tab.TabWidth = 4;
8372   Tab.IndentWidth = 8;
8373   verifyFormat("class TabWidth4Indent8 {\n"
8374                "\t\tvoid f() {\n"
8375                "\t\t\t\tsomeFunction(parameter1,\n"
8376                "\t\t\t\t\t\t\t parameter2);\n"
8377                "\t\t}\n"
8378                "};",
8379                Tab);
8380   Tab.TabWidth = 4;
8381   Tab.IndentWidth = 4;
8382   verifyFormat("class TabWidth4Indent4 {\n"
8383                "\tvoid f() {\n"
8384                "\t\tsomeFunction(parameter1,\n"
8385                "\t\t\t\t\t parameter2);\n"
8386                "\t}\n"
8387                "};",
8388                Tab);
8389   Tab.TabWidth = 8;
8390   Tab.IndentWidth = 4;
8391   verifyFormat("class TabWidth8Indent4 {\n"
8392                "    void f() {\n"
8393                "\tsomeFunction(parameter1,\n"
8394                "\t\t     parameter2);\n"
8395                "    }\n"
8396                "};",
8397                Tab);
8398   Tab.TabWidth = 8;
8399   Tab.IndentWidth = 8;
8400   EXPECT_EQ("/*\n"
8401             "\t      a\t\tcomment\n"
8402             "\t      in multiple lines\n"
8403             "       */",
8404             format("   /*\t \t \n"
8405                    " \t \t a\t\tcomment\t \t\n"
8406                    " \t \t in multiple lines\t\n"
8407                    " \t  */",
8408                    Tab));
8409   verifyFormat("{\n"
8410                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8411                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8412                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8413                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8414                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8415                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8416                "};",
8417                Tab);
8418   verifyFormat("enum AA {\n"
8419                "\ta1, // Force multiple lines\n"
8420                "\ta2,\n"
8421                "\ta3\n"
8422                "};",
8423                Tab);
8424   EXPECT_EQ("if (aaaaaaaa && // q\n"
8425             "    bb)         // w\n"
8426             "\t;",
8427             format("if (aaaaaaaa &&// q\n"
8428                    "bb)// w\n"
8429                    ";",
8430                    Tab));
8431   verifyFormat("class X {\n"
8432                "\tvoid f() {\n"
8433                "\t\tsomeFunction(parameter1,\n"
8434                "\t\t\t     parameter2);\n"
8435                "\t}\n"
8436                "};",
8437                Tab);
8438   verifyFormat("{\n"
8439                "\tQ(\n"
8440                "\t    {\n"
8441                "\t\t    int a;\n"
8442                "\t\t    someFunction(aaaaaaaa,\n"
8443                "\t\t\t\t bbbbbbb);\n"
8444                "\t    },\n"
8445                "\t    p);\n"
8446                "}",
8447                Tab);
8448   EXPECT_EQ("{\n"
8449             "\t/* aaaa\n"
8450             "\t   bbbb */\n"
8451             "}",
8452             format("{\n"
8453                    "/* aaaa\n"
8454                    "   bbbb */\n"
8455                    "}",
8456                    Tab));
8457   EXPECT_EQ("{\n"
8458             "\t/*\n"
8459             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8460             "\t  bbbbbbbbbbbbb\n"
8461             "\t*/\n"
8462             "}",
8463             format("{\n"
8464                    "/*\n"
8465                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8466                    "*/\n"
8467                    "}",
8468                    Tab));
8469   EXPECT_EQ("{\n"
8470             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8471             "\t// bbbbbbbbbbbbb\n"
8472             "}",
8473             format("{\n"
8474                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8475                    "}",
8476                    Tab));
8477   EXPECT_EQ("{\n"
8478             "\t/*\n"
8479             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8480             "\t  bbbbbbbbbbbbb\n"
8481             "\t*/\n"
8482             "}",
8483             format("{\n"
8484                    "\t/*\n"
8485                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8486                    "\t*/\n"
8487                    "}",
8488                    Tab));
8489   EXPECT_EQ("{\n"
8490             "\t/*\n"
8491             "\n"
8492             "\t*/\n"
8493             "}",
8494             format("{\n"
8495                    "\t/*\n"
8496                    "\n"
8497                    "\t*/\n"
8498                    "}",
8499                    Tab));
8500   EXPECT_EQ("{\n"
8501             "\t/*\n"
8502             " asdf\n"
8503             "\t*/\n"
8504             "}",
8505             format("{\n"
8506                    "\t/*\n"
8507                    " asdf\n"
8508                    "\t*/\n"
8509                    "}",
8510                    Tab));
8511   EXPECT_EQ("/*\n"
8512             "\t      a\t\tcomment\n"
8513             "\t      in multiple lines\n"
8514             "       */",
8515             format("   /*\t \t \n"
8516                    " \t \t a\t\tcomment\t \t\n"
8517                    " \t \t in multiple lines\t\n"
8518                    " \t  */",
8519                    Tab));
8520   EXPECT_EQ("/* some\n"
8521             "   comment */",
8522             format(" \t \t /* some\n"
8523                    " \t \t    comment */",
8524                    Tab));
8525   EXPECT_EQ("int a; /* some\n"
8526             "   comment */",
8527             format(" \t \t int a; /* some\n"
8528                    " \t \t    comment */",
8529                    Tab));
8530   EXPECT_EQ("int a; /* some\n"
8531             "comment */",
8532             format(" \t \t int\ta; /* some\n"
8533                    " \t \t    comment */",
8534                    Tab));
8535   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8536             "    comment */",
8537             format(" \t \t f(\"\t\t\"); /* some\n"
8538                    " \t \t    comment */",
8539                    Tab));
8540   EXPECT_EQ("{\n"
8541             "  /*\n"
8542             "   * Comment\n"
8543             "   */\n"
8544             "  int i;\n"
8545             "}",
8546             format("{\n"
8547                    "\t/*\n"
8548                    "\t * Comment\n"
8549                    "\t */\n"
8550                    "\t int i;\n"
8551                    "}"));
8552   Tab.AlignConsecutiveAssignments = true;
8553   Tab.AlignConsecutiveDeclarations = true;
8554   Tab.TabWidth = 4;
8555   Tab.IndentWidth = 4;
8556   verifyFormat("class Assign {\n"
8557                "\tvoid f() {\n"
8558                "\t\tint         x      = 123;\n"
8559                "\t\tint         random = 4;\n"
8560                "\t\tstd::string alphabet =\n"
8561                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8562                "\t}\n"
8563                "};",
8564                Tab);
8565 }
8566 
8567 TEST_F(FormatTest, CalculatesOriginalColumn) {
8568   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8569             "q\"; /* some\n"
8570             "       comment */",
8571             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8572                    "q\"; /* some\n"
8573                    "       comment */",
8574                    getLLVMStyle()));
8575   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8576             "/* some\n"
8577             "   comment */",
8578             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8579                    " /* some\n"
8580                    "    comment */",
8581                    getLLVMStyle()));
8582   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8583             "qqq\n"
8584             "/* some\n"
8585             "   comment */",
8586             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8587                    "qqq\n"
8588                    " /* some\n"
8589                    "    comment */",
8590                    getLLVMStyle()));
8591   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8592             "wwww; /* some\n"
8593             "         comment */",
8594             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8595                    "wwww; /* some\n"
8596                    "         comment */",
8597                    getLLVMStyle()));
8598 }
8599 
8600 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8601   FormatStyle NoSpace = getLLVMStyle();
8602   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8603 
8604   verifyFormat("while(true)\n"
8605                "  continue;",
8606                NoSpace);
8607   verifyFormat("for(;;)\n"
8608                "  continue;",
8609                NoSpace);
8610   verifyFormat("if(true)\n"
8611                "  f();\n"
8612                "else if(true)\n"
8613                "  f();",
8614                NoSpace);
8615   verifyFormat("do {\n"
8616                "  do_something();\n"
8617                "} while(something());",
8618                NoSpace);
8619   verifyFormat("switch(x) {\n"
8620                "default:\n"
8621                "  break;\n"
8622                "}",
8623                NoSpace);
8624   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8625   verifyFormat("size_t x = sizeof(x);", NoSpace);
8626   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8627   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8628   verifyFormat("alignas(128) char a[128];", NoSpace);
8629   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8630   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8631   verifyFormat("int f() throw(Deprecated);", NoSpace);
8632   verifyFormat("typedef void (*cb)(int);", NoSpace);
8633   verifyFormat("T A::operator()();", NoSpace);
8634   verifyFormat("X A::operator++(T);", NoSpace);
8635 
8636   FormatStyle Space = getLLVMStyle();
8637   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8638 
8639   verifyFormat("int f ();", Space);
8640   verifyFormat("void f (int a, T b) {\n"
8641                "  while (true)\n"
8642                "    continue;\n"
8643                "}",
8644                Space);
8645   verifyFormat("if (true)\n"
8646                "  f ();\n"
8647                "else if (true)\n"
8648                "  f ();",
8649                Space);
8650   verifyFormat("do {\n"
8651                "  do_something ();\n"
8652                "} while (something ());",
8653                Space);
8654   verifyFormat("switch (x) {\n"
8655                "default:\n"
8656                "  break;\n"
8657                "}",
8658                Space);
8659   verifyFormat("A::A () : a (1) {}", Space);
8660   verifyFormat("void f () __attribute__ ((asdf));", Space);
8661   verifyFormat("*(&a + 1);\n"
8662                "&((&a)[1]);\n"
8663                "a[(b + c) * d];\n"
8664                "(((a + 1) * 2) + 3) * 4;",
8665                Space);
8666   verifyFormat("#define A(x) x", Space);
8667   verifyFormat("#define A (x) x", Space);
8668   verifyFormat("#if defined(x)\n"
8669                "#endif",
8670                Space);
8671   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8672   verifyFormat("size_t x = sizeof (x);", Space);
8673   verifyFormat("auto f (int x) -> decltype (x);", Space);
8674   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8675   verifyFormat("alignas (128) char a[128];", Space);
8676   verifyFormat("size_t x = alignof (MyType);", Space);
8677   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8678   verifyFormat("int f () throw (Deprecated);", Space);
8679   verifyFormat("typedef void (*cb) (int);", Space);
8680   verifyFormat("T A::operator() ();", Space);
8681   verifyFormat("X A::operator++ (T);", Space);
8682 }
8683 
8684 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8685   FormatStyle Spaces = getLLVMStyle();
8686 
8687   Spaces.SpacesInParentheses = true;
8688   verifyFormat("call( x, y, z );", Spaces);
8689   verifyFormat("call();", Spaces);
8690   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8691   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8692                Spaces);
8693   verifyFormat("while ( (bool)1 )\n"
8694                "  continue;",
8695                Spaces);
8696   verifyFormat("for ( ;; )\n"
8697                "  continue;",
8698                Spaces);
8699   verifyFormat("if ( true )\n"
8700                "  f();\n"
8701                "else if ( true )\n"
8702                "  f();",
8703                Spaces);
8704   verifyFormat("do {\n"
8705                "  do_something( (int)i );\n"
8706                "} while ( something() );",
8707                Spaces);
8708   verifyFormat("switch ( x ) {\n"
8709                "default:\n"
8710                "  break;\n"
8711                "}",
8712                Spaces);
8713 
8714   Spaces.SpacesInParentheses = false;
8715   Spaces.SpacesInCStyleCastParentheses = true;
8716   verifyFormat("Type *A = ( Type * )P;", Spaces);
8717   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8718   verifyFormat("x = ( int32 )y;", Spaces);
8719   verifyFormat("int a = ( int )(2.0f);", Spaces);
8720   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8721   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8722   verifyFormat("#define x (( int )-1)", Spaces);
8723 
8724   // Run the first set of tests again with:
8725   Spaces.SpacesInParentheses = false;
8726   Spaces.SpaceInEmptyParentheses = true;
8727   Spaces.SpacesInCStyleCastParentheses = true;
8728   verifyFormat("call(x, y, z);", Spaces);
8729   verifyFormat("call( );", Spaces);
8730   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8731   verifyFormat("while (( bool )1)\n"
8732                "  continue;",
8733                Spaces);
8734   verifyFormat("for (;;)\n"
8735                "  continue;",
8736                Spaces);
8737   verifyFormat("if (true)\n"
8738                "  f( );\n"
8739                "else if (true)\n"
8740                "  f( );",
8741                Spaces);
8742   verifyFormat("do {\n"
8743                "  do_something(( int )i);\n"
8744                "} while (something( ));",
8745                Spaces);
8746   verifyFormat("switch (x) {\n"
8747                "default:\n"
8748                "  break;\n"
8749                "}",
8750                Spaces);
8751 
8752   // Run the first set of tests again with:
8753   Spaces.SpaceAfterCStyleCast = true;
8754   verifyFormat("call(x, y, z);", Spaces);
8755   verifyFormat("call( );", Spaces);
8756   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8757   verifyFormat("while (( bool ) 1)\n"
8758                "  continue;",
8759                Spaces);
8760   verifyFormat("for (;;)\n"
8761                "  continue;",
8762                Spaces);
8763   verifyFormat("if (true)\n"
8764                "  f( );\n"
8765                "else if (true)\n"
8766                "  f( );",
8767                Spaces);
8768   verifyFormat("do {\n"
8769                "  do_something(( int ) i);\n"
8770                "} while (something( ));",
8771                Spaces);
8772   verifyFormat("switch (x) {\n"
8773                "default:\n"
8774                "  break;\n"
8775                "}",
8776                Spaces);
8777 
8778   // Run subset of tests again with:
8779   Spaces.SpacesInCStyleCastParentheses = false;
8780   Spaces.SpaceAfterCStyleCast = true;
8781   verifyFormat("while ((bool) 1)\n"
8782                "  continue;",
8783                Spaces);
8784   verifyFormat("do {\n"
8785                "  do_something((int) i);\n"
8786                "} while (something( ));",
8787                Spaces);
8788 }
8789 
8790 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8791   verifyFormat("int a[5];");
8792   verifyFormat("a[3] += 42;");
8793 
8794   FormatStyle Spaces = getLLVMStyle();
8795   Spaces.SpacesInSquareBrackets = true;
8796   // Lambdas unchanged.
8797   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8798   verifyFormat("return [i, args...] {};", Spaces);
8799 
8800   // Not lambdas.
8801   verifyFormat("int a[ 5 ];", Spaces);
8802   verifyFormat("a[ 3 ] += 42;", Spaces);
8803   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8804   verifyFormat("double &operator[](int i) { return 0; }\n"
8805                "int i;",
8806                Spaces);
8807   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8808   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8809   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8810 }
8811 
8812 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8813   verifyFormat("int a = 5;");
8814   verifyFormat("a += 42;");
8815   verifyFormat("a or_eq 8;");
8816 
8817   FormatStyle Spaces = getLLVMStyle();
8818   Spaces.SpaceBeforeAssignmentOperators = false;
8819   verifyFormat("int a= 5;", Spaces);
8820   verifyFormat("a+= 42;", Spaces);
8821   verifyFormat("a or_eq 8;", Spaces);
8822 }
8823 
8824 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8825   FormatStyle Alignment = getLLVMStyle();
8826   Alignment.AlignConsecutiveAssignments = false;
8827   verifyFormat("int a = 5;\n"
8828                "int oneTwoThree = 123;",
8829                Alignment);
8830   verifyFormat("int a = 5;\n"
8831                "int oneTwoThree = 123;",
8832                Alignment);
8833 
8834   Alignment.AlignConsecutiveAssignments = true;
8835   verifyFormat("int a           = 5;\n"
8836                "int oneTwoThree = 123;",
8837                Alignment);
8838   verifyFormat("int a           = method();\n"
8839                "int oneTwoThree = 133;",
8840                Alignment);
8841   verifyFormat("a &= 5;\n"
8842                "bcd *= 5;\n"
8843                "ghtyf += 5;\n"
8844                "dvfvdb -= 5;\n"
8845                "a /= 5;\n"
8846                "vdsvsv %= 5;\n"
8847                "sfdbddfbdfbb ^= 5;\n"
8848                "dvsdsv |= 5;\n"
8849                "int dsvvdvsdvvv = 123;",
8850                Alignment);
8851   verifyFormat("int i = 1, j = 10;\n"
8852                "something = 2000;",
8853                Alignment);
8854   verifyFormat("something = 2000;\n"
8855                "int i = 1, j = 10;\n",
8856                Alignment);
8857   verifyFormat("something = 2000;\n"
8858                "another   = 911;\n"
8859                "int i = 1, j = 10;\n"
8860                "oneMore = 1;\n"
8861                "i       = 2;",
8862                Alignment);
8863   verifyFormat("int a   = 5;\n"
8864                "int one = 1;\n"
8865                "method();\n"
8866                "int oneTwoThree = 123;\n"
8867                "int oneTwo      = 12;",
8868                Alignment);
8869   verifyFormat("int oneTwoThree = 123;\n"
8870                "int oneTwo      = 12;\n"
8871                "method();\n",
8872                Alignment);
8873   verifyFormat("int oneTwoThree = 123; // comment\n"
8874                "int oneTwo      = 12;  // comment",
8875                Alignment);
8876   EXPECT_EQ("int a = 5;\n"
8877             "\n"
8878             "int oneTwoThree = 123;",
8879             format("int a       = 5;\n"
8880                    "\n"
8881                    "int oneTwoThree= 123;",
8882                    Alignment));
8883   EXPECT_EQ("int a   = 5;\n"
8884             "int one = 1;\n"
8885             "\n"
8886             "int oneTwoThree = 123;",
8887             format("int a = 5;\n"
8888                    "int one = 1;\n"
8889                    "\n"
8890                    "int oneTwoThree = 123;",
8891                    Alignment));
8892   EXPECT_EQ("int a   = 5;\n"
8893             "int one = 1;\n"
8894             "\n"
8895             "int oneTwoThree = 123;\n"
8896             "int oneTwo      = 12;",
8897             format("int a = 5;\n"
8898                    "int one = 1;\n"
8899                    "\n"
8900                    "int oneTwoThree = 123;\n"
8901                    "int oneTwo = 12;",
8902                    Alignment));
8903   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8904   verifyFormat("#define A \\\n"
8905                "  int aaaa       = 12; \\\n"
8906                "  int b          = 23; \\\n"
8907                "  int ccc        = 234; \\\n"
8908                "  int dddddddddd = 2345;",
8909                Alignment);
8910   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8911   verifyFormat("#define A               \\\n"
8912                "  int aaaa       = 12;  \\\n"
8913                "  int b          = 23;  \\\n"
8914                "  int ccc        = 234; \\\n"
8915                "  int dddddddddd = 2345;",
8916                Alignment);
8917   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8918   verifyFormat("#define A                                                      "
8919                "                \\\n"
8920                "  int aaaa       = 12;                                         "
8921                "                \\\n"
8922                "  int b          = 23;                                         "
8923                "                \\\n"
8924                "  int ccc        = 234;                                        "
8925                "                \\\n"
8926                "  int dddddddddd = 2345;",
8927                Alignment);
8928   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8929                "k = 4, int l = 5,\n"
8930                "                  int m = 6) {\n"
8931                "  int j      = 10;\n"
8932                "  otherThing = 1;\n"
8933                "}",
8934                Alignment);
8935   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8936                "  int i   = 1;\n"
8937                "  int j   = 2;\n"
8938                "  int big = 10000;\n"
8939                "}",
8940                Alignment);
8941   verifyFormat("class C {\n"
8942                "public:\n"
8943                "  int i            = 1;\n"
8944                "  virtual void f() = 0;\n"
8945                "};",
8946                Alignment);
8947   verifyFormat("int i = 1;\n"
8948                "if (SomeType t = getSomething()) {\n"
8949                "}\n"
8950                "int j   = 2;\n"
8951                "int big = 10000;",
8952                Alignment);
8953   verifyFormat("int j = 7;\n"
8954                "for (int k = 0; k < N; ++k) {\n"
8955                "}\n"
8956                "int j   = 2;\n"
8957                "int big = 10000;\n"
8958                "}",
8959                Alignment);
8960   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8961   verifyFormat("int i = 1;\n"
8962                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8963                "    = someLooooooooooooooooongFunction();\n"
8964                "int j = 2;",
8965                Alignment);
8966   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8967   verifyFormat("int i = 1;\n"
8968                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8969                "    someLooooooooooooooooongFunction();\n"
8970                "int j = 2;",
8971                Alignment);
8972 
8973   verifyFormat("auto lambda = []() {\n"
8974                "  auto i = 0;\n"
8975                "  return 0;\n"
8976                "};\n"
8977                "int i  = 0;\n"
8978                "auto v = type{\n"
8979                "    i = 1,   //\n"
8980                "    (i = 2), //\n"
8981                "    i = 3    //\n"
8982                "};",
8983                Alignment);
8984 
8985   verifyFormat(
8986       "int i      = 1;\n"
8987       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8988       "                          loooooooooooooooooooooongParameterB);\n"
8989       "int j      = 2;",
8990       Alignment);
8991 
8992   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8993                "          typename B   = very_long_type_name_1,\n"
8994                "          typename T_2 = very_long_type_name_2>\n"
8995                "auto foo() {}\n",
8996                Alignment);
8997   verifyFormat("int a, b = 1;\n"
8998                "int c  = 2;\n"
8999                "int dd = 3;\n",
9000                Alignment);
9001   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
9002                "float b[1][] = {{3.f}};\n",
9003                Alignment);
9004   verifyFormat("for (int i = 0; i < 1; i++)\n"
9005                "  int x = 1;\n",
9006                Alignment);
9007   verifyFormat("for (i = 0; i < 1; i++)\n"
9008                "  x = 1;\n"
9009                "y = 1;\n",
9010                Alignment);
9011 }
9012 
9013 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
9014   FormatStyle Alignment = getLLVMStyle();
9015   Alignment.AlignConsecutiveDeclarations = false;
9016   verifyFormat("float const a = 5;\n"
9017                "int oneTwoThree = 123;",
9018                Alignment);
9019   verifyFormat("int a = 5;\n"
9020                "float const oneTwoThree = 123;",
9021                Alignment);
9022 
9023   Alignment.AlignConsecutiveDeclarations = true;
9024   verifyFormat("float const a = 5;\n"
9025                "int         oneTwoThree = 123;",
9026                Alignment);
9027   verifyFormat("int         a = method();\n"
9028                "float const oneTwoThree = 133;",
9029                Alignment);
9030   verifyFormat("int i = 1, j = 10;\n"
9031                "something = 2000;",
9032                Alignment);
9033   verifyFormat("something = 2000;\n"
9034                "int i = 1, j = 10;\n",
9035                Alignment);
9036   verifyFormat("float      something = 2000;\n"
9037                "double     another = 911;\n"
9038                "int        i = 1, j = 10;\n"
9039                "const int *oneMore = 1;\n"
9040                "unsigned   i = 2;",
9041                Alignment);
9042   verifyFormat("float a = 5;\n"
9043                "int   one = 1;\n"
9044                "method();\n"
9045                "const double       oneTwoThree = 123;\n"
9046                "const unsigned int oneTwo = 12;",
9047                Alignment);
9048   verifyFormat("int      oneTwoThree{0}; // comment\n"
9049                "unsigned oneTwo;         // comment",
9050                Alignment);
9051   EXPECT_EQ("float const a = 5;\n"
9052             "\n"
9053             "int oneTwoThree = 123;",
9054             format("float const   a = 5;\n"
9055                    "\n"
9056                    "int           oneTwoThree= 123;",
9057                    Alignment));
9058   EXPECT_EQ("float a = 5;\n"
9059             "int   one = 1;\n"
9060             "\n"
9061             "unsigned oneTwoThree = 123;",
9062             format("float    a = 5;\n"
9063                    "int      one = 1;\n"
9064                    "\n"
9065                    "unsigned oneTwoThree = 123;",
9066                    Alignment));
9067   EXPECT_EQ("float a = 5;\n"
9068             "int   one = 1;\n"
9069             "\n"
9070             "unsigned oneTwoThree = 123;\n"
9071             "int      oneTwo = 12;",
9072             format("float    a = 5;\n"
9073                    "int one = 1;\n"
9074                    "\n"
9075                    "unsigned oneTwoThree = 123;\n"
9076                    "int oneTwo = 12;",
9077                    Alignment));
9078   // Function prototype alignment
9079   verifyFormat("int    a();\n"
9080                "double b();",
9081                Alignment);
9082   verifyFormat("int    a(int x);\n"
9083                "double b();",
9084                Alignment);
9085   unsigned OldColumnLimit = Alignment.ColumnLimit;
9086   // We need to set ColumnLimit to zero, in order to stress nested alignments,
9087   // otherwise the function parameters will be re-flowed onto a single line.
9088   Alignment.ColumnLimit = 0;
9089   EXPECT_EQ("int    a(int   x,\n"
9090             "         float y);\n"
9091             "double b(int    x,\n"
9092             "         double y);",
9093             format("int a(int x,\n"
9094                    " float y);\n"
9095                    "double b(int x,\n"
9096                    " double y);",
9097                    Alignment));
9098   // This ensures that function parameters of function declarations are
9099   // correctly indented when their owning functions are indented.
9100   // The failure case here is for 'double y' to not be indented enough.
9101   EXPECT_EQ("double a(int x);\n"
9102             "int    b(int    y,\n"
9103             "         double z);",
9104             format("double a(int x);\n"
9105                    "int b(int y,\n"
9106                    " double z);",
9107                    Alignment));
9108   // Set ColumnLimit low so that we induce wrapping immediately after
9109   // the function name and opening paren.
9110   Alignment.ColumnLimit = 13;
9111   verifyFormat("int function(\n"
9112                "    int  x,\n"
9113                "    bool y);",
9114                Alignment);
9115   Alignment.ColumnLimit = OldColumnLimit;
9116   // Ensure function pointers don't screw up recursive alignment
9117   verifyFormat("int    a(int x, void (*fp)(int y));\n"
9118                "double b();",
9119                Alignment);
9120   Alignment.AlignConsecutiveAssignments = true;
9121   // Ensure recursive alignment is broken by function braces, so that the
9122   // "a = 1" does not align with subsequent assignments inside the function
9123   // body.
9124   verifyFormat("int func(int a = 1) {\n"
9125                "  int b  = 2;\n"
9126                "  int cc = 3;\n"
9127                "}",
9128                Alignment);
9129   verifyFormat("float      something = 2000;\n"
9130                "double     another   = 911;\n"
9131                "int        i = 1, j = 10;\n"
9132                "const int *oneMore = 1;\n"
9133                "unsigned   i       = 2;",
9134                Alignment);
9135   verifyFormat("int      oneTwoThree = {0}; // comment\n"
9136                "unsigned oneTwo      = 0;   // comment",
9137                Alignment);
9138   // Make sure that scope is correctly tracked, in the absence of braces
9139   verifyFormat("for (int i = 0; i < n; i++)\n"
9140                "  j = i;\n"
9141                "double x = 1;\n",
9142                Alignment);
9143   verifyFormat("if (int i = 0)\n"
9144                "  j = i;\n"
9145                "double x = 1;\n",
9146                Alignment);
9147   // Ensure operator[] and operator() are comprehended
9148   verifyFormat("struct test {\n"
9149                "  long long int foo();\n"
9150                "  int           operator[](int a);\n"
9151                "  double        bar();\n"
9152                "};\n",
9153                Alignment);
9154   verifyFormat("struct test {\n"
9155                "  long long int foo();\n"
9156                "  int           operator()(int a);\n"
9157                "  double        bar();\n"
9158                "};\n",
9159                Alignment);
9160   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
9161             "  int const i   = 1;\n"
9162             "  int *     j   = 2;\n"
9163             "  int       big = 10000;\n"
9164             "\n"
9165             "  unsigned oneTwoThree = 123;\n"
9166             "  int      oneTwo      = 12;\n"
9167             "  method();\n"
9168             "  float k  = 2;\n"
9169             "  int   ll = 10000;\n"
9170             "}",
9171             format("void SomeFunction(int parameter= 0) {\n"
9172                    " int const  i= 1;\n"
9173                    "  int *j=2;\n"
9174                    " int big  =  10000;\n"
9175                    "\n"
9176                    "unsigned oneTwoThree  =123;\n"
9177                    "int oneTwo = 12;\n"
9178                    "  method();\n"
9179                    "float k= 2;\n"
9180                    "int ll=10000;\n"
9181                    "}",
9182                    Alignment));
9183   Alignment.AlignConsecutiveAssignments = false;
9184   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
9185   verifyFormat("#define A \\\n"
9186                "  int       aaaa = 12; \\\n"
9187                "  float     b = 23; \\\n"
9188                "  const int ccc = 234; \\\n"
9189                "  unsigned  dddddddddd = 2345;",
9190                Alignment);
9191   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9192   verifyFormat("#define A              \\\n"
9193                "  int       aaaa = 12; \\\n"
9194                "  float     b = 23;    \\\n"
9195                "  const int ccc = 234; \\\n"
9196                "  unsigned  dddddddddd = 2345;",
9197                Alignment);
9198   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
9199   Alignment.ColumnLimit = 30;
9200   verifyFormat("#define A                    \\\n"
9201                "  int       aaaa = 12;       \\\n"
9202                "  float     b = 23;          \\\n"
9203                "  const int ccc = 234;       \\\n"
9204                "  int       dddddddddd = 2345;",
9205                Alignment);
9206   Alignment.ColumnLimit = 80;
9207   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
9208                "k = 4, int l = 5,\n"
9209                "                  int m = 6) {\n"
9210                "  const int j = 10;\n"
9211                "  otherThing = 1;\n"
9212                "}",
9213                Alignment);
9214   verifyFormat("void SomeFunction(int parameter = 0) {\n"
9215                "  int const i = 1;\n"
9216                "  int *     j = 2;\n"
9217                "  int       big = 10000;\n"
9218                "}",
9219                Alignment);
9220   verifyFormat("class C {\n"
9221                "public:\n"
9222                "  int          i = 1;\n"
9223                "  virtual void f() = 0;\n"
9224                "};",
9225                Alignment);
9226   verifyFormat("float i = 1;\n"
9227                "if (SomeType t = getSomething()) {\n"
9228                "}\n"
9229                "const unsigned j = 2;\n"
9230                "int            big = 10000;",
9231                Alignment);
9232   verifyFormat("float j = 7;\n"
9233                "for (int k = 0; k < N; ++k) {\n"
9234                "}\n"
9235                "unsigned j = 2;\n"
9236                "int      big = 10000;\n"
9237                "}",
9238                Alignment);
9239   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9240   verifyFormat("float              i = 1;\n"
9241                "LooooooooooongType loooooooooooooooooooooongVariable\n"
9242                "    = someLooooooooooooooooongFunction();\n"
9243                "int j = 2;",
9244                Alignment);
9245   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
9246   verifyFormat("int                i = 1;\n"
9247                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
9248                "    someLooooooooooooooooongFunction();\n"
9249                "int j = 2;",
9250                Alignment);
9251 
9252   Alignment.AlignConsecutiveAssignments = true;
9253   verifyFormat("auto lambda = []() {\n"
9254                "  auto  ii = 0;\n"
9255                "  float j  = 0;\n"
9256                "  return 0;\n"
9257                "};\n"
9258                "int   i  = 0;\n"
9259                "float i2 = 0;\n"
9260                "auto  v  = type{\n"
9261                "    i = 1,   //\n"
9262                "    (i = 2), //\n"
9263                "    i = 3    //\n"
9264                "};",
9265                Alignment);
9266   Alignment.AlignConsecutiveAssignments = false;
9267 
9268   verifyFormat(
9269       "int      i = 1;\n"
9270       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
9271       "                          loooooooooooooooooooooongParameterB);\n"
9272       "int      j = 2;",
9273       Alignment);
9274 
9275   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
9276   // We expect declarations and assignments to align, as long as it doesn't
9277   // exceed the column limit, starting a new alignment sequence whenever it
9278   // happens.
9279   Alignment.AlignConsecutiveAssignments = true;
9280   Alignment.ColumnLimit = 30;
9281   verifyFormat("float    ii              = 1;\n"
9282                "unsigned j               = 2;\n"
9283                "int someVerylongVariable = 1;\n"
9284                "AnotherLongType  ll = 123456;\n"
9285                "VeryVeryLongType k  = 2;\n"
9286                "int              myvar = 1;",
9287                Alignment);
9288   Alignment.ColumnLimit = 80;
9289   Alignment.AlignConsecutiveAssignments = false;
9290 
9291   verifyFormat(
9292       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
9293       "          typename LongType, typename B>\n"
9294       "auto foo() {}\n",
9295       Alignment);
9296   verifyFormat("float a, b = 1;\n"
9297                "int   c = 2;\n"
9298                "int   dd = 3;\n",
9299                Alignment);
9300   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
9301                "float b[1][] = {{3.f}};\n",
9302                Alignment);
9303   Alignment.AlignConsecutiveAssignments = true;
9304   verifyFormat("float a, b = 1;\n"
9305                "int   c  = 2;\n"
9306                "int   dd = 3;\n",
9307                Alignment);
9308   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
9309                "float b[1][] = {{3.f}};\n",
9310                Alignment);
9311   Alignment.AlignConsecutiveAssignments = false;
9312 
9313   Alignment.ColumnLimit = 30;
9314   Alignment.BinPackParameters = false;
9315   verifyFormat("void foo(float     a,\n"
9316                "         float     b,\n"
9317                "         int       c,\n"
9318                "         uint32_t *d) {\n"
9319                "  int *  e = 0;\n"
9320                "  float  f = 0;\n"
9321                "  double g = 0;\n"
9322                "}\n"
9323                "void bar(ino_t     a,\n"
9324                "         int       b,\n"
9325                "         uint32_t *c,\n"
9326                "         bool      d) {}\n",
9327                Alignment);
9328   Alignment.BinPackParameters = true;
9329   Alignment.ColumnLimit = 80;
9330 
9331   // Bug 33507
9332   Alignment.PointerAlignment = FormatStyle::PAS_Middle;
9333   verifyFormat(
9334       "auto found = range::find_if(vsProducts, [&](auto * aProduct) {\n"
9335       "  static const Version verVs2017;\n"
9336       "  return true;\n"
9337       "});\n",
9338       Alignment);
9339   Alignment.PointerAlignment = FormatStyle::PAS_Right;
9340 }
9341 
9342 TEST_F(FormatTest, LinuxBraceBreaking) {
9343   FormatStyle LinuxBraceStyle = getLLVMStyle();
9344   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
9345   verifyFormat("namespace a\n"
9346                "{\n"
9347                "class A\n"
9348                "{\n"
9349                "  void f()\n"
9350                "  {\n"
9351                "    if (true) {\n"
9352                "      a();\n"
9353                "      b();\n"
9354                "    } else {\n"
9355                "      a();\n"
9356                "    }\n"
9357                "  }\n"
9358                "  void g() { return; }\n"
9359                "};\n"
9360                "struct B {\n"
9361                "  int x;\n"
9362                "};\n"
9363                "} // namespace a\n",
9364                LinuxBraceStyle);
9365   verifyFormat("enum X {\n"
9366                "  Y = 0,\n"
9367                "}\n",
9368                LinuxBraceStyle);
9369   verifyFormat("struct S {\n"
9370                "  int Type;\n"
9371                "  union {\n"
9372                "    int x;\n"
9373                "    double y;\n"
9374                "  } Value;\n"
9375                "  class C\n"
9376                "  {\n"
9377                "    MyFavoriteType Value;\n"
9378                "  } Class;\n"
9379                "}\n",
9380                LinuxBraceStyle);
9381 }
9382 
9383 TEST_F(FormatTest, MozillaBraceBreaking) {
9384   FormatStyle MozillaBraceStyle = getLLVMStyle();
9385   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
9386   MozillaBraceStyle.FixNamespaceComments = false;
9387   verifyFormat("namespace a {\n"
9388                "class A\n"
9389                "{\n"
9390                "  void f()\n"
9391                "  {\n"
9392                "    if (true) {\n"
9393                "      a();\n"
9394                "      b();\n"
9395                "    }\n"
9396                "  }\n"
9397                "  void g() { return; }\n"
9398                "};\n"
9399                "enum E\n"
9400                "{\n"
9401                "  A,\n"
9402                "  // foo\n"
9403                "  B,\n"
9404                "  C\n"
9405                "};\n"
9406                "struct B\n"
9407                "{\n"
9408                "  int x;\n"
9409                "};\n"
9410                "}\n",
9411                MozillaBraceStyle);
9412   verifyFormat("struct S\n"
9413                "{\n"
9414                "  int Type;\n"
9415                "  union\n"
9416                "  {\n"
9417                "    int x;\n"
9418                "    double y;\n"
9419                "  } Value;\n"
9420                "  class C\n"
9421                "  {\n"
9422                "    MyFavoriteType Value;\n"
9423                "  } Class;\n"
9424                "}\n",
9425                MozillaBraceStyle);
9426 }
9427 
9428 TEST_F(FormatTest, StroustrupBraceBreaking) {
9429   FormatStyle StroustrupBraceStyle = getLLVMStyle();
9430   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9431   verifyFormat("namespace a {\n"
9432                "class A {\n"
9433                "  void f()\n"
9434                "  {\n"
9435                "    if (true) {\n"
9436                "      a();\n"
9437                "      b();\n"
9438                "    }\n"
9439                "  }\n"
9440                "  void g() { return; }\n"
9441                "};\n"
9442                "struct B {\n"
9443                "  int x;\n"
9444                "};\n"
9445                "} // namespace a\n",
9446                StroustrupBraceStyle);
9447 
9448   verifyFormat("void foo()\n"
9449                "{\n"
9450                "  if (a) {\n"
9451                "    a();\n"
9452                "  }\n"
9453                "  else {\n"
9454                "    b();\n"
9455                "  }\n"
9456                "}\n",
9457                StroustrupBraceStyle);
9458 
9459   verifyFormat("#ifdef _DEBUG\n"
9460                "int foo(int i = 0)\n"
9461                "#else\n"
9462                "int foo(int i = 5)\n"
9463                "#endif\n"
9464                "{\n"
9465                "  return i;\n"
9466                "}",
9467                StroustrupBraceStyle);
9468 
9469   verifyFormat("void foo() {}\n"
9470                "void bar()\n"
9471                "#ifdef _DEBUG\n"
9472                "{\n"
9473                "  foo();\n"
9474                "}\n"
9475                "#else\n"
9476                "{\n"
9477                "}\n"
9478                "#endif",
9479                StroustrupBraceStyle);
9480 
9481   verifyFormat("void foobar() { int i = 5; }\n"
9482                "#ifdef _DEBUG\n"
9483                "void bar() {}\n"
9484                "#else\n"
9485                "void bar() { foobar(); }\n"
9486                "#endif",
9487                StroustrupBraceStyle);
9488 }
9489 
9490 TEST_F(FormatTest, AllmanBraceBreaking) {
9491   FormatStyle AllmanBraceStyle = getLLVMStyle();
9492   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9493 
9494   EXPECT_EQ("namespace a\n"
9495             "{\n"
9496             "void f();\n"
9497             "void g();\n"
9498             "} // namespace a\n",
9499             format("namespace a\n"
9500                    "{\n"
9501                    "void f();\n"
9502                    "void g();\n"
9503                    "}\n",
9504                    AllmanBraceStyle));
9505 
9506   verifyFormat("namespace a\n"
9507                "{\n"
9508                "class A\n"
9509                "{\n"
9510                "  void f()\n"
9511                "  {\n"
9512                "    if (true)\n"
9513                "    {\n"
9514                "      a();\n"
9515                "      b();\n"
9516                "    }\n"
9517                "  }\n"
9518                "  void g() { return; }\n"
9519                "};\n"
9520                "struct B\n"
9521                "{\n"
9522                "  int x;\n"
9523                "};\n"
9524                "} // namespace a",
9525                AllmanBraceStyle);
9526 
9527   verifyFormat("void f()\n"
9528                "{\n"
9529                "  if (true)\n"
9530                "  {\n"
9531                "    a();\n"
9532                "  }\n"
9533                "  else if (false)\n"
9534                "  {\n"
9535                "    b();\n"
9536                "  }\n"
9537                "  else\n"
9538                "  {\n"
9539                "    c();\n"
9540                "  }\n"
9541                "}\n",
9542                AllmanBraceStyle);
9543 
9544   verifyFormat("void f()\n"
9545                "{\n"
9546                "  for (int i = 0; i < 10; ++i)\n"
9547                "  {\n"
9548                "    a();\n"
9549                "  }\n"
9550                "  while (false)\n"
9551                "  {\n"
9552                "    b();\n"
9553                "  }\n"
9554                "  do\n"
9555                "  {\n"
9556                "    c();\n"
9557                "  } while (false)\n"
9558                "}\n",
9559                AllmanBraceStyle);
9560 
9561   verifyFormat("void f(int a)\n"
9562                "{\n"
9563                "  switch (a)\n"
9564                "  {\n"
9565                "  case 0:\n"
9566                "    break;\n"
9567                "  case 1:\n"
9568                "  {\n"
9569                "    break;\n"
9570                "  }\n"
9571                "  case 2:\n"
9572                "  {\n"
9573                "  }\n"
9574                "  break;\n"
9575                "  default:\n"
9576                "    break;\n"
9577                "  }\n"
9578                "}\n",
9579                AllmanBraceStyle);
9580 
9581   verifyFormat("enum X\n"
9582                "{\n"
9583                "  Y = 0,\n"
9584                "}\n",
9585                AllmanBraceStyle);
9586   verifyFormat("enum X\n"
9587                "{\n"
9588                "  Y = 0\n"
9589                "}\n",
9590                AllmanBraceStyle);
9591 
9592   verifyFormat("@interface BSApplicationController ()\n"
9593                "{\n"
9594                "@private\n"
9595                "  id _extraIvar;\n"
9596                "}\n"
9597                "@end\n",
9598                AllmanBraceStyle);
9599 
9600   verifyFormat("#ifdef _DEBUG\n"
9601                "int foo(int i = 0)\n"
9602                "#else\n"
9603                "int foo(int i = 5)\n"
9604                "#endif\n"
9605                "{\n"
9606                "  return i;\n"
9607                "}",
9608                AllmanBraceStyle);
9609 
9610   verifyFormat("void foo() {}\n"
9611                "void bar()\n"
9612                "#ifdef _DEBUG\n"
9613                "{\n"
9614                "  foo();\n"
9615                "}\n"
9616                "#else\n"
9617                "{\n"
9618                "}\n"
9619                "#endif",
9620                AllmanBraceStyle);
9621 
9622   verifyFormat("void foobar() { int i = 5; }\n"
9623                "#ifdef _DEBUG\n"
9624                "void bar() {}\n"
9625                "#else\n"
9626                "void bar() { foobar(); }\n"
9627                "#endif",
9628                AllmanBraceStyle);
9629 
9630   // This shouldn't affect ObjC blocks..
9631   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9632                "  // ...\n"
9633                "  int i;\n"
9634                "}];",
9635                AllmanBraceStyle);
9636   verifyFormat("void (^block)(void) = ^{\n"
9637                "  // ...\n"
9638                "  int i;\n"
9639                "};",
9640                AllmanBraceStyle);
9641   // .. or dict literals.
9642   verifyFormat("void f()\n"
9643                "{\n"
9644                "  // ...\n"
9645                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9646                "}",
9647                AllmanBraceStyle);
9648   verifyFormat("void f()\n"
9649                "{\n"
9650                "  // ...\n"
9651                "  [object someMethod:@{a : @\"b\"}];\n"
9652                "}",
9653                AllmanBraceStyle);
9654   verifyFormat("int f()\n"
9655                "{ // comment\n"
9656                "  return 42;\n"
9657                "}",
9658                AllmanBraceStyle);
9659 
9660   AllmanBraceStyle.ColumnLimit = 19;
9661   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9662   AllmanBraceStyle.ColumnLimit = 18;
9663   verifyFormat("void f()\n"
9664                "{\n"
9665                "  int i;\n"
9666                "}",
9667                AllmanBraceStyle);
9668   AllmanBraceStyle.ColumnLimit = 80;
9669 
9670   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9671   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9672   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9673   verifyFormat("void f(bool b)\n"
9674                "{\n"
9675                "  if (b)\n"
9676                "  {\n"
9677                "    return;\n"
9678                "  }\n"
9679                "}\n",
9680                BreakBeforeBraceShortIfs);
9681   verifyFormat("void f(bool b)\n"
9682                "{\n"
9683                "  if constexpr (b)\n"
9684                "  {\n"
9685                "    return;\n"
9686                "  }\n"
9687                "}\n",
9688                BreakBeforeBraceShortIfs);
9689   verifyFormat("void f(bool b)\n"
9690                "{\n"
9691                "  if (b) return;\n"
9692                "}\n",
9693                BreakBeforeBraceShortIfs);
9694   verifyFormat("void f(bool b)\n"
9695                "{\n"
9696                "  if constexpr (b) return;\n"
9697                "}\n",
9698                BreakBeforeBraceShortIfs);
9699   verifyFormat("void f(bool b)\n"
9700                "{\n"
9701                "  while (b)\n"
9702                "  {\n"
9703                "    return;\n"
9704                "  }\n"
9705                "}\n",
9706                BreakBeforeBraceShortIfs);
9707 }
9708 
9709 TEST_F(FormatTest, GNUBraceBreaking) {
9710   FormatStyle GNUBraceStyle = getLLVMStyle();
9711   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9712   verifyFormat("namespace a\n"
9713                "{\n"
9714                "class A\n"
9715                "{\n"
9716                "  void f()\n"
9717                "  {\n"
9718                "    int a;\n"
9719                "    {\n"
9720                "      int b;\n"
9721                "    }\n"
9722                "    if (true)\n"
9723                "      {\n"
9724                "        a();\n"
9725                "        b();\n"
9726                "      }\n"
9727                "  }\n"
9728                "  void g() { return; }\n"
9729                "}\n"
9730                "} // namespace a",
9731                GNUBraceStyle);
9732 
9733   verifyFormat("void f()\n"
9734                "{\n"
9735                "  if (true)\n"
9736                "    {\n"
9737                "      a();\n"
9738                "    }\n"
9739                "  else if (false)\n"
9740                "    {\n"
9741                "      b();\n"
9742                "    }\n"
9743                "  else\n"
9744                "    {\n"
9745                "      c();\n"
9746                "    }\n"
9747                "}\n",
9748                GNUBraceStyle);
9749 
9750   verifyFormat("void f()\n"
9751                "{\n"
9752                "  for (int i = 0; i < 10; ++i)\n"
9753                "    {\n"
9754                "      a();\n"
9755                "    }\n"
9756                "  while (false)\n"
9757                "    {\n"
9758                "      b();\n"
9759                "    }\n"
9760                "  do\n"
9761                "    {\n"
9762                "      c();\n"
9763                "    }\n"
9764                "  while (false);\n"
9765                "}\n",
9766                GNUBraceStyle);
9767 
9768   verifyFormat("void f(int a)\n"
9769                "{\n"
9770                "  switch (a)\n"
9771                "    {\n"
9772                "    case 0:\n"
9773                "      break;\n"
9774                "    case 1:\n"
9775                "      {\n"
9776                "        break;\n"
9777                "      }\n"
9778                "    case 2:\n"
9779                "      {\n"
9780                "      }\n"
9781                "      break;\n"
9782                "    default:\n"
9783                "      break;\n"
9784                "    }\n"
9785                "}\n",
9786                GNUBraceStyle);
9787 
9788   verifyFormat("enum X\n"
9789                "{\n"
9790                "  Y = 0,\n"
9791                "}\n",
9792                GNUBraceStyle);
9793 
9794   verifyFormat("@interface BSApplicationController ()\n"
9795                "{\n"
9796                "@private\n"
9797                "  id _extraIvar;\n"
9798                "}\n"
9799                "@end\n",
9800                GNUBraceStyle);
9801 
9802   verifyFormat("#ifdef _DEBUG\n"
9803                "int foo(int i = 0)\n"
9804                "#else\n"
9805                "int foo(int i = 5)\n"
9806                "#endif\n"
9807                "{\n"
9808                "  return i;\n"
9809                "}",
9810                GNUBraceStyle);
9811 
9812   verifyFormat("void foo() {}\n"
9813                "void bar()\n"
9814                "#ifdef _DEBUG\n"
9815                "{\n"
9816                "  foo();\n"
9817                "}\n"
9818                "#else\n"
9819                "{\n"
9820                "}\n"
9821                "#endif",
9822                GNUBraceStyle);
9823 
9824   verifyFormat("void foobar() { int i = 5; }\n"
9825                "#ifdef _DEBUG\n"
9826                "void bar() {}\n"
9827                "#else\n"
9828                "void bar() { foobar(); }\n"
9829                "#endif",
9830                GNUBraceStyle);
9831 }
9832 
9833 TEST_F(FormatTest, WebKitBraceBreaking) {
9834   FormatStyle WebKitBraceStyle = getLLVMStyle();
9835   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9836   WebKitBraceStyle.FixNamespaceComments = false;
9837   verifyFormat("namespace a {\n"
9838                "class A {\n"
9839                "  void f()\n"
9840                "  {\n"
9841                "    if (true) {\n"
9842                "      a();\n"
9843                "      b();\n"
9844                "    }\n"
9845                "  }\n"
9846                "  void g() { return; }\n"
9847                "};\n"
9848                "enum E {\n"
9849                "  A,\n"
9850                "  // foo\n"
9851                "  B,\n"
9852                "  C\n"
9853                "};\n"
9854                "struct B {\n"
9855                "  int x;\n"
9856                "};\n"
9857                "}\n",
9858                WebKitBraceStyle);
9859   verifyFormat("struct S {\n"
9860                "  int Type;\n"
9861                "  union {\n"
9862                "    int x;\n"
9863                "    double y;\n"
9864                "  } Value;\n"
9865                "  class C {\n"
9866                "    MyFavoriteType Value;\n"
9867                "  } Class;\n"
9868                "};\n",
9869                WebKitBraceStyle);
9870 }
9871 
9872 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9873   verifyFormat("void f() {\n"
9874                "  try {\n"
9875                "  } catch (const Exception &e) {\n"
9876                "  }\n"
9877                "}\n",
9878                getLLVMStyle());
9879 }
9880 
9881 TEST_F(FormatTest, UnderstandsPragmas) {
9882   verifyFormat("#pragma omp reduction(| : var)");
9883   verifyFormat("#pragma omp reduction(+ : var)");
9884 
9885   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9886             "(including parentheses).",
9887             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9888                    "(including parentheses)."));
9889 }
9890 
9891 TEST_F(FormatTest, UnderstandPragmaOption) {
9892   verifyFormat("#pragma option -C -A");
9893 
9894   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9895 }
9896 
9897 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9898   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9899   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9900                                   << " differs from Style #0"
9901 
9902 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9903   SmallVector<FormatStyle, 3> Styles;
9904   Styles.resize(3);
9905 
9906   Styles[0] = getLLVMStyle();
9907   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9908   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9909   EXPECT_ALL_STYLES_EQUAL(Styles);
9910 
9911   Styles[0] = getGoogleStyle();
9912   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9913   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9914   EXPECT_ALL_STYLES_EQUAL(Styles);
9915 
9916   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9917   EXPECT_TRUE(
9918       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9919   EXPECT_TRUE(
9920       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9921   EXPECT_ALL_STYLES_EQUAL(Styles);
9922 
9923   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9924   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9925   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9926   EXPECT_ALL_STYLES_EQUAL(Styles);
9927 
9928   Styles[0] = getMozillaStyle();
9929   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9930   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9931   EXPECT_ALL_STYLES_EQUAL(Styles);
9932 
9933   Styles[0] = getWebKitStyle();
9934   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9935   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9936   EXPECT_ALL_STYLES_EQUAL(Styles);
9937 
9938   Styles[0] = getGNUStyle();
9939   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9940   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9941   EXPECT_ALL_STYLES_EQUAL(Styles);
9942 
9943   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9944 }
9945 
9946 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9947   SmallVector<FormatStyle, 8> Styles;
9948   Styles.resize(2);
9949 
9950   Styles[0] = getGoogleStyle();
9951   Styles[1] = getLLVMStyle();
9952   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9953   EXPECT_ALL_STYLES_EQUAL(Styles);
9954 
9955   Styles.resize(5);
9956   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9957   Styles[1] = getLLVMStyle();
9958   Styles[1].Language = FormatStyle::LK_JavaScript;
9959   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9960 
9961   Styles[2] = getLLVMStyle();
9962   Styles[2].Language = FormatStyle::LK_JavaScript;
9963   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9964                                   "BasedOnStyle: Google",
9965                                   &Styles[2])
9966                    .value());
9967 
9968   Styles[3] = getLLVMStyle();
9969   Styles[3].Language = FormatStyle::LK_JavaScript;
9970   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9971                                   "Language: JavaScript",
9972                                   &Styles[3])
9973                    .value());
9974 
9975   Styles[4] = getLLVMStyle();
9976   Styles[4].Language = FormatStyle::LK_JavaScript;
9977   EXPECT_EQ(0, parseConfiguration("---\n"
9978                                   "BasedOnStyle: LLVM\n"
9979                                   "IndentWidth: 123\n"
9980                                   "---\n"
9981                                   "BasedOnStyle: Google\n"
9982                                   "Language: JavaScript",
9983                                   &Styles[4])
9984                    .value());
9985   EXPECT_ALL_STYLES_EQUAL(Styles);
9986 }
9987 
9988 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9989   Style.FIELD = false;                                                         \
9990   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9991   EXPECT_TRUE(Style.FIELD);                                                    \
9992   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9993   EXPECT_FALSE(Style.FIELD);
9994 
9995 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9996 
9997 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9998   Style.STRUCT.FIELD = false;                                                  \
9999   EXPECT_EQ(0,                                                                 \
10000             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
10001                 .value());                                                     \
10002   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
10003   EXPECT_EQ(0,                                                                 \
10004             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
10005                 .value());                                                     \
10006   EXPECT_FALSE(Style.STRUCT.FIELD);
10007 
10008 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
10009   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
10010 
10011 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
10012   EXPECT_NE(VALUE, Style.FIELD);                                               \
10013   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
10014   EXPECT_EQ(VALUE, Style.FIELD)
10015 
10016 TEST_F(FormatTest, ParsesConfigurationBools) {
10017   FormatStyle Style = {};
10018   Style.Language = FormatStyle::LK_Cpp;
10019   CHECK_PARSE_BOOL(AlignOperands);
10020   CHECK_PARSE_BOOL(AlignTrailingComments);
10021   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
10022   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
10023   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
10024   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
10025   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
10026   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
10027   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
10028   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
10029   CHECK_PARSE_BOOL(BinPackArguments);
10030   CHECK_PARSE_BOOL(BinPackParameters);
10031   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
10032   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
10033   CHECK_PARSE_BOOL(BreakStringLiterals);
10034   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
10035   CHECK_PARSE_BOOL(CompactNamespaces);
10036   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
10037   CHECK_PARSE_BOOL(DerivePointerAlignment);
10038   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
10039   CHECK_PARSE_BOOL(DisableFormat);
10040   CHECK_PARSE_BOOL(IndentCaseLabels);
10041   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
10042   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
10043   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
10044   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
10045   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
10046   CHECK_PARSE_BOOL(ReflowComments);
10047   CHECK_PARSE_BOOL(SortIncludes);
10048   CHECK_PARSE_BOOL(SortUsingDeclarations);
10049   CHECK_PARSE_BOOL(SpacesInParentheses);
10050   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
10051   CHECK_PARSE_BOOL(SpacesInAngles);
10052   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
10053   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
10054   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
10055   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
10056   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
10057   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
10058 
10059   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
10060   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
10061   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
10062   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
10063   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
10064   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
10065   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
10066   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
10067   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExternBlock);
10068   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
10069   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
10070   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
10071   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
10072   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
10073   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
10074 }
10075 
10076 #undef CHECK_PARSE_BOOL
10077 
10078 TEST_F(FormatTest, ParsesConfiguration) {
10079   FormatStyle Style = {};
10080   Style.Language = FormatStyle::LK_Cpp;
10081   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
10082   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
10083               ConstructorInitializerIndentWidth, 1234u);
10084   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
10085   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
10086   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
10087   CHECK_PARSE("PenaltyBreakAssignment: 1234",
10088               PenaltyBreakAssignment, 1234u);
10089   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
10090               PenaltyBreakBeforeFirstCallParameter, 1234u);
10091   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
10092   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
10093               PenaltyReturnTypeOnItsOwnLine, 1234u);
10094   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
10095               SpacesBeforeTrailingComments, 1234u);
10096   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
10097   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
10098   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
10099 
10100   Style.PointerAlignment = FormatStyle::PAS_Middle;
10101   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
10102               FormatStyle::PAS_Left);
10103   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
10104               FormatStyle::PAS_Right);
10105   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
10106               FormatStyle::PAS_Middle);
10107   // For backward compatibility:
10108   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
10109               FormatStyle::PAS_Left);
10110   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
10111               FormatStyle::PAS_Right);
10112   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
10113               FormatStyle::PAS_Middle);
10114 
10115   Style.Standard = FormatStyle::LS_Auto;
10116   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
10117   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
10118   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
10119   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
10120   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
10121 
10122   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
10123   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
10124               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
10125   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
10126               FormatStyle::BOS_None);
10127   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
10128               FormatStyle::BOS_All);
10129   // For backward compatibility:
10130   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
10131               FormatStyle::BOS_None);
10132   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
10133               FormatStyle::BOS_All);
10134 
10135   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
10136   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
10137               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10138   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
10139               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
10140   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
10141               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
10142   // For backward compatibility:
10143   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
10144               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
10145 
10146   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10147   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
10148               FormatStyle::BAS_Align);
10149   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
10150               FormatStyle::BAS_DontAlign);
10151   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
10152               FormatStyle::BAS_AlwaysBreak);
10153   // For backward compatibility:
10154   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
10155               FormatStyle::BAS_DontAlign);
10156   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
10157               FormatStyle::BAS_Align);
10158 
10159   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
10160   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
10161               FormatStyle::ENAS_DontAlign);
10162   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
10163               FormatStyle::ENAS_Left);
10164   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
10165               FormatStyle::ENAS_Right);
10166   // For backward compatibility:
10167   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
10168               FormatStyle::ENAS_Left);
10169   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
10170               FormatStyle::ENAS_Right);
10171 
10172   Style.UseTab = FormatStyle::UT_ForIndentation;
10173   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
10174   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
10175   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10176   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10177               FormatStyle::UT_ForContinuationAndIndentation);
10178   // For backward compatibility:
10179   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
10180   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
10181 
10182   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
10183   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
10184               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10185   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
10186               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
10187   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
10188               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
10189   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
10190               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10191   // For backward compatibility:
10192   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
10193               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
10194   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
10195               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
10196 
10197   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
10198   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
10199               FormatStyle::SBPO_Never);
10200   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
10201               FormatStyle::SBPO_Always);
10202   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
10203               FormatStyle::SBPO_ControlStatements);
10204   // For backward compatibility:
10205   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
10206               FormatStyle::SBPO_Never);
10207   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
10208               FormatStyle::SBPO_ControlStatements);
10209 
10210   Style.ColumnLimit = 123;
10211   FormatStyle BaseStyle = getLLVMStyle();
10212   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
10213   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
10214 
10215   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
10216   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
10217               FormatStyle::BS_Attach);
10218   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
10219               FormatStyle::BS_Linux);
10220   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
10221               FormatStyle::BS_Mozilla);
10222   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
10223               FormatStyle::BS_Stroustrup);
10224   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
10225               FormatStyle::BS_Allman);
10226   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
10227   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
10228               FormatStyle::BS_WebKit);
10229   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
10230               FormatStyle::BS_Custom);
10231 
10232   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
10233   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
10234               FormatStyle::RTBS_None);
10235   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
10236               FormatStyle::RTBS_All);
10237   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
10238               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
10239   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
10240               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
10241   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
10242               AlwaysBreakAfterReturnType,
10243               FormatStyle::RTBS_TopLevelDefinitions);
10244 
10245   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
10246   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
10247               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
10248   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
10249               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
10250   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
10251               AlwaysBreakAfterDefinitionReturnType,
10252               FormatStyle::DRTBS_TopLevel);
10253 
10254   Style.NamespaceIndentation = FormatStyle::NI_All;
10255   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
10256               FormatStyle::NI_None);
10257   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
10258               FormatStyle::NI_Inner);
10259   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
10260               FormatStyle::NI_All);
10261 
10262   // FIXME: This is required because parsing a configuration simply overwrites
10263   // the first N elements of the list instead of resetting it.
10264   Style.ForEachMacros.clear();
10265   std::vector<std::string> BoostForeach;
10266   BoostForeach.push_back("BOOST_FOREACH");
10267   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
10268   std::vector<std::string> BoostAndQForeach;
10269   BoostAndQForeach.push_back("BOOST_FOREACH");
10270   BoostAndQForeach.push_back("Q_FOREACH");
10271   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
10272               BoostAndQForeach);
10273 
10274   Style.IncludeCategories.clear();
10275   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
10276                                                                   {".*", 1}};
10277   CHECK_PARSE("IncludeCategories:\n"
10278               "  - Regex: abc/.*\n"
10279               "    Priority: 2\n"
10280               "  - Regex: .*\n"
10281               "    Priority: 1",
10282               IncludeCategories, ExpectedCategories);
10283   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
10284 
10285   Style.RawStringFormats.clear();
10286   std::vector<FormatStyle::RawStringFormat> ExpectedRawStringFormats = {
10287       {"pb", FormatStyle::LK_TextProto, "llvm"},
10288       {"cpp", FormatStyle::LK_Cpp, "google"}};
10289 
10290   CHECK_PARSE("RawStringFormats:\n"
10291               "  - Delimiter: 'pb'\n"
10292               "    Language: TextProto\n"
10293               "    BasedOnStyle: llvm\n"
10294               "  - Delimiter: 'cpp'\n"
10295               "    Language: Cpp\n"
10296               "    BasedOnStyle: google",
10297               RawStringFormats, ExpectedRawStringFormats);
10298 }
10299 
10300 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
10301   FormatStyle Style = {};
10302   Style.Language = FormatStyle::LK_Cpp;
10303   CHECK_PARSE("Language: Cpp\n"
10304               "IndentWidth: 12",
10305               IndentWidth, 12u);
10306   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
10307                                "IndentWidth: 34",
10308                                &Style),
10309             ParseError::Unsuitable);
10310   EXPECT_EQ(12u, Style.IndentWidth);
10311   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10312   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10313 
10314   Style.Language = FormatStyle::LK_JavaScript;
10315   CHECK_PARSE("Language: JavaScript\n"
10316               "IndentWidth: 12",
10317               IndentWidth, 12u);
10318   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
10319   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
10320                                "IndentWidth: 34",
10321                                &Style),
10322             ParseError::Unsuitable);
10323   EXPECT_EQ(23u, Style.IndentWidth);
10324   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
10325   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10326 
10327   CHECK_PARSE("BasedOnStyle: LLVM\n"
10328               "IndentWidth: 67",
10329               IndentWidth, 67u);
10330 
10331   CHECK_PARSE("---\n"
10332               "Language: JavaScript\n"
10333               "IndentWidth: 12\n"
10334               "---\n"
10335               "Language: Cpp\n"
10336               "IndentWidth: 34\n"
10337               "...\n",
10338               IndentWidth, 12u);
10339 
10340   Style.Language = FormatStyle::LK_Cpp;
10341   CHECK_PARSE("---\n"
10342               "Language: JavaScript\n"
10343               "IndentWidth: 12\n"
10344               "---\n"
10345               "Language: Cpp\n"
10346               "IndentWidth: 34\n"
10347               "...\n",
10348               IndentWidth, 34u);
10349   CHECK_PARSE("---\n"
10350               "IndentWidth: 78\n"
10351               "---\n"
10352               "Language: JavaScript\n"
10353               "IndentWidth: 56\n"
10354               "...\n",
10355               IndentWidth, 78u);
10356 
10357   Style.ColumnLimit = 123;
10358   Style.IndentWidth = 234;
10359   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
10360   Style.TabWidth = 345;
10361   EXPECT_FALSE(parseConfiguration("---\n"
10362                                   "IndentWidth: 456\n"
10363                                   "BreakBeforeBraces: Allman\n"
10364                                   "---\n"
10365                                   "Language: JavaScript\n"
10366                                   "IndentWidth: 111\n"
10367                                   "TabWidth: 111\n"
10368                                   "---\n"
10369                                   "Language: Cpp\n"
10370                                   "BreakBeforeBraces: Stroustrup\n"
10371                                   "TabWidth: 789\n"
10372                                   "...\n",
10373                                   &Style));
10374   EXPECT_EQ(123u, Style.ColumnLimit);
10375   EXPECT_EQ(456u, Style.IndentWidth);
10376   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
10377   EXPECT_EQ(789u, Style.TabWidth);
10378 
10379   EXPECT_EQ(parseConfiguration("---\n"
10380                                "Language: JavaScript\n"
10381                                "IndentWidth: 56\n"
10382                                "---\n"
10383                                "IndentWidth: 78\n"
10384                                "...\n",
10385                                &Style),
10386             ParseError::Error);
10387   EXPECT_EQ(parseConfiguration("---\n"
10388                                "Language: JavaScript\n"
10389                                "IndentWidth: 56\n"
10390                                "---\n"
10391                                "Language: JavaScript\n"
10392                                "IndentWidth: 78\n"
10393                                "...\n",
10394                                &Style),
10395             ParseError::Error);
10396 
10397   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
10398 }
10399 
10400 #undef CHECK_PARSE
10401 
10402 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
10403   FormatStyle Style = {};
10404   Style.Language = FormatStyle::LK_JavaScript;
10405   Style.BreakBeforeTernaryOperators = true;
10406   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
10407   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10408 
10409   Style.BreakBeforeTernaryOperators = true;
10410   EXPECT_EQ(0, parseConfiguration("---\n"
10411                                   "BasedOnStyle: Google\n"
10412                                   "---\n"
10413                                   "Language: JavaScript\n"
10414                                   "IndentWidth: 76\n"
10415                                   "...\n",
10416                                   &Style)
10417                    .value());
10418   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
10419   EXPECT_EQ(76u, Style.IndentWidth);
10420   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
10421 }
10422 
10423 TEST_F(FormatTest, ConfigurationRoundTripTest) {
10424   FormatStyle Style = getLLVMStyle();
10425   std::string YAML = configurationAsText(Style);
10426   FormatStyle ParsedStyle = {};
10427   ParsedStyle.Language = FormatStyle::LK_Cpp;
10428   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
10429   EXPECT_EQ(Style, ParsedStyle);
10430 }
10431 
10432 TEST_F(FormatTest, WorksFor8bitEncodings) {
10433   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
10434             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
10435             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
10436             "\"\xef\xee\xf0\xf3...\"",
10437             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
10438                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
10439                    "\xef\xee\xf0\xf3...\"",
10440                    getLLVMStyleWithColumns(12)));
10441 }
10442 
10443 TEST_F(FormatTest, HandlesUTF8BOM) {
10444   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
10445   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
10446             format("\xef\xbb\xbf#include <iostream>"));
10447   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
10448             format("\xef\xbb\xbf\n#include <iostream>"));
10449 }
10450 
10451 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
10452 #if !defined(_MSC_VER)
10453 
10454 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
10455   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
10456                getLLVMStyleWithColumns(35));
10457   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
10458                getLLVMStyleWithColumns(31));
10459   verifyFormat("// Однажды в студёную зимнюю пору...",
10460                getLLVMStyleWithColumns(36));
10461   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
10462   verifyFormat("/* Однажды в студёную зимнюю пору... */",
10463                getLLVMStyleWithColumns(39));
10464   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
10465                getLLVMStyleWithColumns(35));
10466 }
10467 
10468 TEST_F(FormatTest, SplitsUTF8Strings) {
10469   // Non-printable characters' width is currently considered to be the length in
10470   // bytes in UTF8. The characters can be displayed in very different manner
10471   // (zero-width, single width with a substitution glyph, expanded to their code
10472   // (e.g. "<8d>"), so there's no single correct way to handle them.
10473   EXPECT_EQ("\"aaaaÄ\"\n"
10474             "\"\xc2\x8d\";",
10475             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10476   EXPECT_EQ("\"aaaaaaaÄ\"\n"
10477             "\"\xc2\x8d\";",
10478             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
10479   EXPECT_EQ("\"Однажды, в \"\n"
10480             "\"студёную \"\n"
10481             "\"зимнюю \"\n"
10482             "\"пору,\"",
10483             format("\"Однажды, в студёную зимнюю пору,\"",
10484                    getLLVMStyleWithColumns(13)));
10485   EXPECT_EQ(
10486       "\"一 二 三 \"\n"
10487       "\"四 五六 \"\n"
10488       "\"七 八 九 \"\n"
10489       "\"十\"",
10490       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
10491   EXPECT_EQ("\"一\t二 \"\n"
10492             "\"\t三 \"\n"
10493             "\"四 五\t六 \"\n"
10494             "\"\t七 \"\n"
10495             "\"八九十\tqq\"",
10496             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
10497                    getLLVMStyleWithColumns(11)));
10498 
10499   // UTF8 character in an escape sequence.
10500   EXPECT_EQ("\"aaaaaa\"\n"
10501             "\"\\\xC2\x8D\"",
10502             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
10503 }
10504 
10505 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
10506   EXPECT_EQ("const char *sssss =\n"
10507             "    \"一二三四五六七八\\\n"
10508             " 九 十\";",
10509             format("const char *sssss = \"一二三四五六七八\\\n"
10510                    " 九 十\";",
10511                    getLLVMStyleWithColumns(30)));
10512 }
10513 
10514 TEST_F(FormatTest, SplitsUTF8LineComments) {
10515   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10516             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10517   EXPECT_EQ("// Я из лесу\n"
10518             "// вышел; был\n"
10519             "// сильный\n"
10520             "// мороз.",
10521             format("// Я из лесу вышел; был сильный мороз.",
10522                    getLLVMStyleWithColumns(13)));
10523   EXPECT_EQ("// 一二三\n"
10524             "// 四五六七\n"
10525             "// 八  九\n"
10526             "// 十",
10527             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10528 }
10529 
10530 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10531   EXPECT_EQ("/* Гляжу,\n"
10532             " * поднимается\n"
10533             " * медленно в\n"
10534             " * гору\n"
10535             " * Лошадка,\n"
10536             " * везущая\n"
10537             " * хворосту\n"
10538             " * воз. */",
10539             format("/* Гляжу, поднимается медленно в гору\n"
10540                    " * Лошадка, везущая хворосту воз. */",
10541                    getLLVMStyleWithColumns(13)));
10542   EXPECT_EQ(
10543       "/* 一二三\n"
10544       " * 四五六七\n"
10545       " * 八  九\n"
10546       " * 十  */",
10547       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10548   EXPECT_EQ("/* �������� ��������\n"
10549             " * ��������\n"
10550             " * ������-�� */",
10551             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10552 }
10553 
10554 #endif // _MSC_VER
10555 
10556 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10557   FormatStyle Style = getLLVMStyle();
10558 
10559   Style.ConstructorInitializerIndentWidth = 4;
10560   verifyFormat(
10561       "SomeClass::Constructor()\n"
10562       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10563       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10564       Style);
10565 
10566   Style.ConstructorInitializerIndentWidth = 2;
10567   verifyFormat(
10568       "SomeClass::Constructor()\n"
10569       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10570       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10571       Style);
10572 
10573   Style.ConstructorInitializerIndentWidth = 0;
10574   verifyFormat(
10575       "SomeClass::Constructor()\n"
10576       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10577       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10578       Style);
10579   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10580   verifyFormat(
10581       "SomeLongTemplateVariableName<\n"
10582       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10583       Style);
10584   verifyFormat(
10585       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10586       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10587       Style);
10588 }
10589 
10590 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10591   FormatStyle Style = getLLVMStyle();
10592   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10593   Style.ConstructorInitializerIndentWidth = 4;
10594   verifyFormat("SomeClass::Constructor()\n"
10595                "    : a(a)\n"
10596                "    , b(b)\n"
10597                "    , c(c) {}",
10598                Style);
10599   verifyFormat("SomeClass::Constructor()\n"
10600                "    : a(a) {}",
10601                Style);
10602 
10603   Style.ColumnLimit = 0;
10604   verifyFormat("SomeClass::Constructor()\n"
10605                "    : a(a) {}",
10606                Style);
10607   verifyFormat("SomeClass::Constructor() noexcept\n"
10608                "    : a(a) {}",
10609                Style);
10610   verifyFormat("SomeClass::Constructor()\n"
10611                "    : a(a)\n"
10612                "    , b(b)\n"
10613                "    , c(c) {}",
10614                Style);
10615   verifyFormat("SomeClass::Constructor()\n"
10616                "    : a(a) {\n"
10617                "  foo();\n"
10618                "  bar();\n"
10619                "}",
10620                Style);
10621 
10622   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10623   verifyFormat("SomeClass::Constructor()\n"
10624                "    : a(a)\n"
10625                "    , b(b)\n"
10626                "    , c(c) {\n}",
10627                Style);
10628   verifyFormat("SomeClass::Constructor()\n"
10629                "    : a(a) {\n}",
10630                Style);
10631 
10632   Style.ColumnLimit = 80;
10633   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10634   Style.ConstructorInitializerIndentWidth = 2;
10635   verifyFormat("SomeClass::Constructor()\n"
10636                "  : a(a)\n"
10637                "  , b(b)\n"
10638                "  , c(c) {}",
10639                Style);
10640 
10641   Style.ConstructorInitializerIndentWidth = 0;
10642   verifyFormat("SomeClass::Constructor()\n"
10643                ": a(a)\n"
10644                ", b(b)\n"
10645                ", c(c) {}",
10646                Style);
10647 
10648   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10649   Style.ConstructorInitializerIndentWidth = 4;
10650   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10651   verifyFormat(
10652       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10653       Style);
10654   verifyFormat(
10655       "SomeClass::Constructor()\n"
10656       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10657       Style);
10658   Style.ConstructorInitializerIndentWidth = 4;
10659   Style.ColumnLimit = 60;
10660   verifyFormat("SomeClass::Constructor()\n"
10661                "    : aaaaaaaa(aaaaaaaa)\n"
10662                "    , aaaaaaaa(aaaaaaaa)\n"
10663                "    , aaaaaaaa(aaaaaaaa) {}",
10664                Style);
10665 }
10666 
10667 TEST_F(FormatTest, Destructors) {
10668   verifyFormat("void F(int &i) { i.~int(); }");
10669   verifyFormat("void F(int &i) { i->~int(); }");
10670 }
10671 
10672 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10673   FormatStyle Style = getWebKitStyle();
10674 
10675   // Don't indent in outer namespaces.
10676   verifyFormat("namespace outer {\n"
10677                "int i;\n"
10678                "namespace inner {\n"
10679                "    int i;\n"
10680                "} // namespace inner\n"
10681                "} // namespace outer\n"
10682                "namespace other_outer {\n"
10683                "int i;\n"
10684                "}",
10685                Style);
10686 
10687   // Don't indent case labels.
10688   verifyFormat("switch (variable) {\n"
10689                "case 1:\n"
10690                "case 2:\n"
10691                "    doSomething();\n"
10692                "    break;\n"
10693                "default:\n"
10694                "    ++variable;\n"
10695                "}",
10696                Style);
10697 
10698   // Wrap before binary operators.
10699   EXPECT_EQ("void f()\n"
10700             "{\n"
10701             "    if (aaaaaaaaaaaaaaaa\n"
10702             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10703             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10704             "        return;\n"
10705             "}",
10706             format("void f() {\n"
10707                    "if (aaaaaaaaaaaaaaaa\n"
10708                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10709                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10710                    "return;\n"
10711                    "}",
10712                    Style));
10713 
10714   // Allow functions on a single line.
10715   verifyFormat("void f() { return; }", Style);
10716 
10717   // Constructor initializers are formatted one per line with the "," on the
10718   // new line.
10719   verifyFormat("Constructor()\n"
10720                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10721                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10722                "          aaaaaaaaaaaaaa)\n"
10723                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10724                "{\n"
10725                "}",
10726                Style);
10727   verifyFormat("SomeClass::Constructor()\n"
10728                "    : a(a)\n"
10729                "{\n"
10730                "}",
10731                Style);
10732   EXPECT_EQ("SomeClass::Constructor()\n"
10733             "    : a(a)\n"
10734             "{\n"
10735             "}",
10736             format("SomeClass::Constructor():a(a){}", Style));
10737   verifyFormat("SomeClass::Constructor()\n"
10738                "    : a(a)\n"
10739                "    , b(b)\n"
10740                "    , c(c)\n"
10741                "{\n"
10742                "}",
10743                Style);
10744   verifyFormat("SomeClass::Constructor()\n"
10745                "    : a(a)\n"
10746                "{\n"
10747                "    foo();\n"
10748                "    bar();\n"
10749                "}",
10750                Style);
10751 
10752   // Access specifiers should be aligned left.
10753   verifyFormat("class C {\n"
10754                "public:\n"
10755                "    int i;\n"
10756                "};",
10757                Style);
10758 
10759   // Do not align comments.
10760   verifyFormat("int a; // Do not\n"
10761                "double b; // align comments.",
10762                Style);
10763 
10764   // Do not align operands.
10765   EXPECT_EQ("ASSERT(aaaa\n"
10766             "    || bbbb);",
10767             format("ASSERT ( aaaa\n||bbbb);", Style));
10768 
10769   // Accept input's line breaks.
10770   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10771             "    || bbbbbbbbbbbbbbb) {\n"
10772             "    i++;\n"
10773             "}",
10774             format("if (aaaaaaaaaaaaaaa\n"
10775                    "|| bbbbbbbbbbbbbbb) { i++; }",
10776                    Style));
10777   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10778             "    i++;\n"
10779             "}",
10780             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10781 
10782   // Don't automatically break all macro definitions (llvm.org/PR17842).
10783   verifyFormat("#define aNumber 10", Style);
10784   // However, generally keep the line breaks that the user authored.
10785   EXPECT_EQ("#define aNumber \\\n"
10786             "    10",
10787             format("#define aNumber \\\n"
10788                    " 10",
10789                    Style));
10790 
10791   // Keep empty and one-element array literals on a single line.
10792   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10793             "                                  copyItems:YES];",
10794             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10795                    "copyItems:YES];",
10796                    Style));
10797   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10798             "                                  copyItems:YES];",
10799             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10800                    "             copyItems:YES];",
10801                    Style));
10802   // FIXME: This does not seem right, there should be more indentation before
10803   // the array literal's entries. Nested blocks have the same problem.
10804   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10805             "    @\"a\",\n"
10806             "    @\"a\"\n"
10807             "]\n"
10808             "                                  copyItems:YES];",
10809             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10810                    "     @\"a\",\n"
10811                    "     @\"a\"\n"
10812                    "     ]\n"
10813                    "       copyItems:YES];",
10814                    Style));
10815   EXPECT_EQ(
10816       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10817       "                                  copyItems:YES];",
10818       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10819              "   copyItems:YES];",
10820              Style));
10821 
10822   verifyFormat("[self.a b:c c:d];", Style);
10823   EXPECT_EQ("[self.a b:c\n"
10824             "        c:d];",
10825             format("[self.a b:c\n"
10826                    "c:d];",
10827                    Style));
10828 }
10829 
10830 TEST_F(FormatTest, FormatsLambdas) {
10831   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10832   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10833   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10834   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10835   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10836   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10837   verifyFormat("auto c = [a = [b = 42] {}] {};\n");
10838   verifyFormat("auto c = [a = &i + 10, b = [] {}] {};\n");
10839   verifyFormat("int x = f(*+[] {});");
10840   verifyFormat("void f() {\n"
10841                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10842                "}\n");
10843   verifyFormat("void f() {\n"
10844                "  other(x.begin(), //\n"
10845                "        x.end(),   //\n"
10846                "        [&](int, int) { return 1; });\n"
10847                "}\n");
10848   verifyFormat("SomeFunction([]() { // A cool function...\n"
10849                "  return 43;\n"
10850                "});");
10851   EXPECT_EQ("SomeFunction([]() {\n"
10852             "#define A a\n"
10853             "  return 43;\n"
10854             "});",
10855             format("SomeFunction([](){\n"
10856                    "#define A a\n"
10857                    "return 43;\n"
10858                    "});"));
10859   verifyFormat("void f() {\n"
10860                "  SomeFunction([](decltype(x), A *a) {});\n"
10861                "}");
10862   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10863                "    [](const aaaaaaaaaa &a) { return a; });");
10864   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10865                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10866                "});");
10867   verifyFormat("Constructor()\n"
10868                "    : Field([] { // comment\n"
10869                "        int i;\n"
10870                "      }) {}");
10871   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10872                "  return some_parameter.size();\n"
10873                "};");
10874   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10875                "    [](const string &s) { return s; };");
10876   verifyFormat("int i = aaaaaa ? 1 //\n"
10877                "               : [] {\n"
10878                "                   return 2; //\n"
10879                "                 }();");
10880   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10881                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10882                "                  return x == 2; // force break\n"
10883                "                });");
10884   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10885                "    [=](int iiiiiiiiiiii) {\n"
10886                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
10887                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
10888                "    });",
10889                getLLVMStyleWithColumns(60));
10890   verifyFormat("SomeFunction({[&] {\n"
10891                "                // comment\n"
10892                "              },\n"
10893                "              [&] {\n"
10894                "                // comment\n"
10895                "              }});");
10896   verifyFormat("SomeFunction({[&] {\n"
10897                "  // comment\n"
10898                "}});");
10899   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10900                "                             [&]() { return true; },\n"
10901                "                         aaaaa aaaaaaaaa);");
10902 
10903   // Lambdas with return types.
10904   verifyFormat("int c = []() -> int { return 2; }();\n");
10905   verifyFormat("int c = []() -> int * { return 2; }();\n");
10906   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10907   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10908   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10909   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10910   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10911   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10912   verifyFormat("[a, a]() -> a<1> {};");
10913   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10914                "                   int j) -> int {\n"
10915                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10916                "};");
10917   verifyFormat(
10918       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10919       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10920       "      return aaaaaaaaaaaaaaaaa;\n"
10921       "    });",
10922       getLLVMStyleWithColumns(70));
10923   verifyFormat("[]() //\n"
10924                "    -> int {\n"
10925                "  return 1; //\n"
10926                "};");
10927 
10928   // Multiple lambdas in the same parentheses change indentation rules.
10929   verifyFormat("SomeFunction(\n"
10930                "    []() {\n"
10931                "      int i = 42;\n"
10932                "      return i;\n"
10933                "    },\n"
10934                "    []() {\n"
10935                "      int j = 43;\n"
10936                "      return j;\n"
10937                "    });");
10938 
10939   // More complex introducers.
10940   verifyFormat("return [i, args...] {};");
10941 
10942   // Not lambdas.
10943   verifyFormat("constexpr char hello[]{\"hello\"};");
10944   verifyFormat("double &operator[](int i) { return 0; }\n"
10945                "int i;");
10946   verifyFormat("std::unique_ptr<int[]> foo() {}");
10947   verifyFormat("int i = a[a][a]->f();");
10948   verifyFormat("int i = (*b)[a]->f();");
10949 
10950   // Other corner cases.
10951   verifyFormat("void f() {\n"
10952                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10953                "  );\n"
10954                "}");
10955 
10956   // Lambdas created through weird macros.
10957   verifyFormat("void f() {\n"
10958                "  MACRO((const AA &a) { return 1; });\n"
10959                "  MACRO((AA &a) { return 1; });\n"
10960                "}");
10961 
10962   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10963                "      doo_dah();\n"
10964                "      doo_dah();\n"
10965                "    })) {\n"
10966                "}");
10967   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
10968                "                doo_dah();\n"
10969                "                doo_dah();\n"
10970                "              })) {\n"
10971                "}");
10972   verifyFormat("auto lambda = []() {\n"
10973                "  int a = 2\n"
10974                "#if A\n"
10975                "          + 2\n"
10976                "#endif\n"
10977                "      ;\n"
10978                "};");
10979 
10980   // Lambdas with complex multiline introducers.
10981   verifyFormat(
10982       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10983       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
10984       "        -> ::std::unordered_set<\n"
10985       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
10986       "      //\n"
10987       "    });");
10988 }
10989 
10990 TEST_F(FormatTest, FormatsBlocks) {
10991   FormatStyle ShortBlocks = getLLVMStyle();
10992   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10993   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10994   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10995   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10996   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10997   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10998   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10999 
11000   verifyFormat("foo(^{ bar(); });", ShortBlocks);
11001   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
11002   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
11003 
11004   verifyFormat("[operation setCompletionBlock:^{\n"
11005                "  [self onOperationDone];\n"
11006                "}];");
11007   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
11008                "  [self onOperationDone];\n"
11009                "}]};");
11010   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
11011                "  f();\n"
11012                "}];");
11013   verifyFormat("int a = [operation block:^int(int *i) {\n"
11014                "  return 1;\n"
11015                "}];");
11016   verifyFormat("[myObject doSomethingWith:arg1\n"
11017                "                      aaa:^int(int *a) {\n"
11018                "                        return 1;\n"
11019                "                      }\n"
11020                "                      bbb:f(a * bbbbbbbb)];");
11021 
11022   verifyFormat("[operation setCompletionBlock:^{\n"
11023                "  [self.delegate newDataAvailable];\n"
11024                "}];",
11025                getLLVMStyleWithColumns(60));
11026   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
11027                "  NSString *path = [self sessionFilePath];\n"
11028                "  if (path) {\n"
11029                "    // ...\n"
11030                "  }\n"
11031                "});");
11032   verifyFormat("[[SessionService sharedService]\n"
11033                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11034                "      if (window) {\n"
11035                "        [self windowDidLoad:window];\n"
11036                "      } else {\n"
11037                "        [self errorLoadingWindow];\n"
11038                "      }\n"
11039                "    }];");
11040   verifyFormat("void (^largeBlock)(void) = ^{\n"
11041                "  // ...\n"
11042                "};\n",
11043                getLLVMStyleWithColumns(40));
11044   verifyFormat("[[SessionService sharedService]\n"
11045                "    loadWindowWithCompletionBlock: //\n"
11046                "        ^(SessionWindow *window) {\n"
11047                "          if (window) {\n"
11048                "            [self windowDidLoad:window];\n"
11049                "          } else {\n"
11050                "            [self errorLoadingWindow];\n"
11051                "          }\n"
11052                "        }];",
11053                getLLVMStyleWithColumns(60));
11054   verifyFormat("[myObject doSomethingWith:arg1\n"
11055                "    firstBlock:^(Foo *a) {\n"
11056                "      // ...\n"
11057                "      int i;\n"
11058                "    }\n"
11059                "    secondBlock:^(Bar *b) {\n"
11060                "      // ...\n"
11061                "      int i;\n"
11062                "    }\n"
11063                "    thirdBlock:^Foo(Bar *b) {\n"
11064                "      // ...\n"
11065                "      int i;\n"
11066                "    }];");
11067   verifyFormat("[myObject doSomethingWith:arg1\n"
11068                "               firstBlock:-1\n"
11069                "              secondBlock:^(Bar *b) {\n"
11070                "                // ...\n"
11071                "                int i;\n"
11072                "              }];");
11073 
11074   verifyFormat("f(^{\n"
11075                "  @autoreleasepool {\n"
11076                "    if (a) {\n"
11077                "      g();\n"
11078                "    }\n"
11079                "  }\n"
11080                "});");
11081   verifyFormat("Block b = ^int *(A *a, B *b) {}");
11082   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
11083                "};");
11084 
11085   FormatStyle FourIndent = getLLVMStyle();
11086   FourIndent.ObjCBlockIndentWidth = 4;
11087   verifyFormat("[operation setCompletionBlock:^{\n"
11088                "    [self onOperationDone];\n"
11089                "}];",
11090                FourIndent);
11091 }
11092 
11093 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
11094   FormatStyle ZeroColumn = getLLVMStyle();
11095   ZeroColumn.ColumnLimit = 0;
11096 
11097   verifyFormat("[[SessionService sharedService] "
11098                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11099                "  if (window) {\n"
11100                "    [self windowDidLoad:window];\n"
11101                "  } else {\n"
11102                "    [self errorLoadingWindow];\n"
11103                "  }\n"
11104                "}];",
11105                ZeroColumn);
11106   EXPECT_EQ("[[SessionService sharedService]\n"
11107             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11108             "      if (window) {\n"
11109             "        [self windowDidLoad:window];\n"
11110             "      } else {\n"
11111             "        [self errorLoadingWindow];\n"
11112             "      }\n"
11113             "    }];",
11114             format("[[SessionService sharedService]\n"
11115                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
11116                    "                if (window) {\n"
11117                    "    [self windowDidLoad:window];\n"
11118                    "  } else {\n"
11119                    "    [self errorLoadingWindow];\n"
11120                    "  }\n"
11121                    "}];",
11122                    ZeroColumn));
11123   verifyFormat("[myObject doSomethingWith:arg1\n"
11124                "    firstBlock:^(Foo *a) {\n"
11125                "      // ...\n"
11126                "      int i;\n"
11127                "    }\n"
11128                "    secondBlock:^(Bar *b) {\n"
11129                "      // ...\n"
11130                "      int i;\n"
11131                "    }\n"
11132                "    thirdBlock:^Foo(Bar *b) {\n"
11133                "      // ...\n"
11134                "      int i;\n"
11135                "    }];",
11136                ZeroColumn);
11137   verifyFormat("f(^{\n"
11138                "  @autoreleasepool {\n"
11139                "    if (a) {\n"
11140                "      g();\n"
11141                "    }\n"
11142                "  }\n"
11143                "});",
11144                ZeroColumn);
11145   verifyFormat("void (^largeBlock)(void) = ^{\n"
11146                "  // ...\n"
11147                "};",
11148                ZeroColumn);
11149 
11150   ZeroColumn.AllowShortBlocksOnASingleLine = true;
11151   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
11152             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11153   ZeroColumn.AllowShortBlocksOnASingleLine = false;
11154   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
11155             "  int i;\n"
11156             "};",
11157             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
11158 }
11159 
11160 TEST_F(FormatTest, SupportsCRLF) {
11161   EXPECT_EQ("int a;\r\n"
11162             "int b;\r\n"
11163             "int c;\r\n",
11164             format("int a;\r\n"
11165                    "  int b;\r\n"
11166                    "    int c;\r\n",
11167                    getLLVMStyle()));
11168   EXPECT_EQ("int a;\r\n"
11169             "int b;\r\n"
11170             "int c;\r\n",
11171             format("int a;\r\n"
11172                    "  int b;\n"
11173                    "    int c;\r\n",
11174                    getLLVMStyle()));
11175   EXPECT_EQ("int a;\n"
11176             "int b;\n"
11177             "int c;\n",
11178             format("int a;\r\n"
11179                    "  int b;\n"
11180                    "    int c;\n",
11181                    getLLVMStyle()));
11182   EXPECT_EQ("\"aaaaaaa \"\r\n"
11183             "\"bbbbbbb\";\r\n",
11184             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
11185   EXPECT_EQ("#define A \\\r\n"
11186             "  b;      \\\r\n"
11187             "  c;      \\\r\n"
11188             "  d;\r\n",
11189             format("#define A \\\r\n"
11190                    "  b; \\\r\n"
11191                    "  c; d; \r\n",
11192                    getGoogleStyle()));
11193 
11194   EXPECT_EQ("/*\r\n"
11195             "multi line block comments\r\n"
11196             "should not introduce\r\n"
11197             "an extra carriage return\r\n"
11198             "*/\r\n",
11199             format("/*\r\n"
11200                    "multi line block comments\r\n"
11201                    "should not introduce\r\n"
11202                    "an extra carriage return\r\n"
11203                    "*/\r\n"));
11204 }
11205 
11206 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
11207   verifyFormat("MY_CLASS(C) {\n"
11208                "  int i;\n"
11209                "  int j;\n"
11210                "};");
11211 }
11212 
11213 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
11214   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
11215   TwoIndent.ContinuationIndentWidth = 2;
11216 
11217   EXPECT_EQ("int i =\n"
11218             "  longFunction(\n"
11219             "    arg);",
11220             format("int i = longFunction(arg);", TwoIndent));
11221 
11222   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
11223   SixIndent.ContinuationIndentWidth = 6;
11224 
11225   EXPECT_EQ("int i =\n"
11226             "      longFunction(\n"
11227             "            arg);",
11228             format("int i = longFunction(arg);", SixIndent));
11229 }
11230 
11231 TEST_F(FormatTest, SpacesInAngles) {
11232   FormatStyle Spaces = getLLVMStyle();
11233   Spaces.SpacesInAngles = true;
11234 
11235   verifyFormat("static_cast< int >(arg);", Spaces);
11236   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
11237   verifyFormat("f< int, float >();", Spaces);
11238   verifyFormat("template <> g() {}", Spaces);
11239   verifyFormat("template < std::vector< int > > f() {}", Spaces);
11240   verifyFormat("std::function< void(int, int) > fct;", Spaces);
11241   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
11242                Spaces);
11243 
11244   Spaces.Standard = FormatStyle::LS_Cpp03;
11245   Spaces.SpacesInAngles = true;
11246   verifyFormat("A< A< int > >();", Spaces);
11247 
11248   Spaces.SpacesInAngles = false;
11249   verifyFormat("A<A<int> >();", Spaces);
11250 
11251   Spaces.Standard = FormatStyle::LS_Cpp11;
11252   Spaces.SpacesInAngles = true;
11253   verifyFormat("A< A< int > >();", Spaces);
11254 
11255   Spaces.SpacesInAngles = false;
11256   verifyFormat("A<A<int>>();", Spaces);
11257 }
11258 
11259 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
11260   FormatStyle Style = getLLVMStyle();
11261   Style.SpaceAfterTemplateKeyword = false;
11262   verifyFormat("template<int> void foo();", Style);
11263 }
11264 
11265 TEST_F(FormatTest, TripleAngleBrackets) {
11266   verifyFormat("f<<<1, 1>>>();");
11267   verifyFormat("f<<<1, 1, 1, s>>>();");
11268   verifyFormat("f<<<a, b, c, d>>>();");
11269   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
11270   verifyFormat("f<param><<<1, 1>>>();");
11271   verifyFormat("f<1><<<1, 1>>>();");
11272   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
11273   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11274                "aaaaaaaaaaa<<<\n    1, 1>>>();");
11275   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
11276                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
11277 }
11278 
11279 TEST_F(FormatTest, MergeLessLessAtEnd) {
11280   verifyFormat("<<");
11281   EXPECT_EQ("< < <", format("\\\n<<<"));
11282   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11283                "aaallvm::outs() <<");
11284   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
11285                "aaaallvm::outs()\n    <<");
11286 }
11287 
11288 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
11289   std::string code = "#if A\n"
11290                      "#if B\n"
11291                      "a.\n"
11292                      "#endif\n"
11293                      "    a = 1;\n"
11294                      "#else\n"
11295                      "#endif\n"
11296                      "#if C\n"
11297                      "#else\n"
11298                      "#endif\n";
11299   EXPECT_EQ(code, format(code));
11300 }
11301 
11302 TEST_F(FormatTest, HandleConflictMarkers) {
11303   // Git/SVN conflict markers.
11304   EXPECT_EQ("int a;\n"
11305             "void f() {\n"
11306             "  callme(some(parameter1,\n"
11307             "<<<<<<< text by the vcs\n"
11308             "              parameter2),\n"
11309             "||||||| text by the vcs\n"
11310             "              parameter2),\n"
11311             "         parameter3,\n"
11312             "======= text by the vcs\n"
11313             "              parameter2, parameter3),\n"
11314             ">>>>>>> text by the vcs\n"
11315             "         otherparameter);\n",
11316             format("int a;\n"
11317                    "void f() {\n"
11318                    "  callme(some(parameter1,\n"
11319                    "<<<<<<< text by the vcs\n"
11320                    "  parameter2),\n"
11321                    "||||||| text by the vcs\n"
11322                    "  parameter2),\n"
11323                    "  parameter3,\n"
11324                    "======= text by the vcs\n"
11325                    "  parameter2,\n"
11326                    "  parameter3),\n"
11327                    ">>>>>>> text by the vcs\n"
11328                    "  otherparameter);\n"));
11329 
11330   // Perforce markers.
11331   EXPECT_EQ("void f() {\n"
11332             "  function(\n"
11333             ">>>> text by the vcs\n"
11334             "      parameter,\n"
11335             "==== text by the vcs\n"
11336             "      parameter,\n"
11337             "==== text by the vcs\n"
11338             "      parameter,\n"
11339             "<<<< text by the vcs\n"
11340             "      parameter);\n",
11341             format("void f() {\n"
11342                    "  function(\n"
11343                    ">>>> text by the vcs\n"
11344                    "  parameter,\n"
11345                    "==== text by the vcs\n"
11346                    "  parameter,\n"
11347                    "==== text by the vcs\n"
11348                    "  parameter,\n"
11349                    "<<<< text by the vcs\n"
11350                    "  parameter);\n"));
11351 
11352   EXPECT_EQ("<<<<<<<\n"
11353             "|||||||\n"
11354             "=======\n"
11355             ">>>>>>>",
11356             format("<<<<<<<\n"
11357                    "|||||||\n"
11358                    "=======\n"
11359                    ">>>>>>>"));
11360 
11361   EXPECT_EQ("<<<<<<<\n"
11362             "|||||||\n"
11363             "int i;\n"
11364             "=======\n"
11365             ">>>>>>>",
11366             format("<<<<<<<\n"
11367                    "|||||||\n"
11368                    "int i;\n"
11369                    "=======\n"
11370                    ">>>>>>>"));
11371 
11372   // FIXME: Handle parsing of macros around conflict markers correctly:
11373   EXPECT_EQ("#define Macro \\\n"
11374             "<<<<<<<\n"
11375             "Something \\\n"
11376             "|||||||\n"
11377             "Else \\\n"
11378             "=======\n"
11379             "Other \\\n"
11380             ">>>>>>>\n"
11381             "    End int i;\n",
11382             format("#define Macro \\\n"
11383                    "<<<<<<<\n"
11384                    "  Something \\\n"
11385                    "|||||||\n"
11386                    "  Else \\\n"
11387                    "=======\n"
11388                    "  Other \\\n"
11389                    ">>>>>>>\n"
11390                    "  End\n"
11391                    "int i;\n"));
11392 }
11393 
11394 TEST_F(FormatTest, DisableRegions) {
11395   EXPECT_EQ("int i;\n"
11396             "// clang-format off\n"
11397             "  int j;\n"
11398             "// clang-format on\n"
11399             "int k;",
11400             format(" int  i;\n"
11401                    "   // clang-format off\n"
11402                    "  int j;\n"
11403                    " // clang-format on\n"
11404                    "   int   k;"));
11405   EXPECT_EQ("int i;\n"
11406             "/* clang-format off */\n"
11407             "  int j;\n"
11408             "/* clang-format on */\n"
11409             "int k;",
11410             format(" int  i;\n"
11411                    "   /* clang-format off */\n"
11412                    "  int j;\n"
11413                    " /* clang-format on */\n"
11414                    "   int   k;"));
11415 
11416   // Don't reflow comments within disabled regions.
11417   EXPECT_EQ(
11418       "// clang-format off\n"
11419       "// long long long long long long line\n"
11420       "/* clang-format on */\n"
11421       "/* long long long\n"
11422       " * long long long\n"
11423       " * line */\n"
11424       "int i;\n"
11425       "/* clang-format off */\n"
11426       "/* long long long long long long line */\n",
11427       format("// clang-format off\n"
11428              "// long long long long long long line\n"
11429              "/* clang-format on */\n"
11430              "/* long long long long long long line */\n"
11431              "int i;\n"
11432              "/* clang-format off */\n"
11433              "/* long long long long long long line */\n",
11434              getLLVMStyleWithColumns(20)));
11435 }
11436 
11437 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
11438   format("? ) =");
11439   verifyNoCrash("#define a\\\n /**/}");
11440 }
11441 
11442 TEST_F(FormatTest, FormatsTableGenCode) {
11443   FormatStyle Style = getLLVMStyle();
11444   Style.Language = FormatStyle::LK_TableGen;
11445   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
11446 }
11447 
11448 TEST_F(FormatTest, ArrayOfTemplates) {
11449   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
11450             format("auto a = new unique_ptr<int > [ 10];"));
11451 
11452   FormatStyle Spaces = getLLVMStyle();
11453   Spaces.SpacesInSquareBrackets = true;
11454   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
11455             format("auto a = new unique_ptr<int > [10];", Spaces));
11456 }
11457 
11458 TEST_F(FormatTest, ArrayAsTemplateType) {
11459   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
11460             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
11461 
11462   FormatStyle Spaces = getLLVMStyle();
11463   Spaces.SpacesInSquareBrackets = true;
11464   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
11465             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
11466 }
11467 
11468 TEST_F(FormatTest, NoSpaceAfterSuper) {
11469     verifyFormat("__super::FooBar();");
11470 }
11471 
11472 TEST(FormatStyle, GetStyleOfFile) {
11473   vfs::InMemoryFileSystem FS;
11474   // Test 1: format file in the same directory.
11475   ASSERT_TRUE(
11476       FS.addFile("/a/.clang-format", 0,
11477                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
11478   ASSERT_TRUE(
11479       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11480   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
11481   ASSERT_TRUE((bool)Style1);
11482   ASSERT_EQ(*Style1, getLLVMStyle());
11483 
11484   // Test 2.1: fallback to default.
11485   ASSERT_TRUE(
11486       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11487   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
11488   ASSERT_TRUE((bool)Style2);
11489   ASSERT_EQ(*Style2, getMozillaStyle());
11490 
11491   // Test 2.2: no format on 'none' fallback style.
11492   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11493   ASSERT_TRUE((bool)Style2);
11494   ASSERT_EQ(*Style2, getNoStyle());
11495 
11496   // Test 2.3: format if config is found with no based style while fallback is
11497   // 'none'.
11498   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
11499                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
11500   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
11501   ASSERT_TRUE((bool)Style2);
11502   ASSERT_EQ(*Style2, getLLVMStyle());
11503 
11504   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
11505   Style2 = getStyle("{}", "a.h", "none", "", &FS);
11506   ASSERT_TRUE((bool)Style2);
11507   ASSERT_EQ(*Style2, getLLVMStyle());
11508 
11509   // Test 3: format file in parent directory.
11510   ASSERT_TRUE(
11511       FS.addFile("/c/.clang-format", 0,
11512                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
11513   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
11514                          llvm::MemoryBuffer::getMemBuffer("int i;")));
11515   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
11516   ASSERT_TRUE((bool)Style3);
11517   ASSERT_EQ(*Style3, getGoogleStyle());
11518 
11519   // Test 4: error on invalid fallback style
11520   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11521   ASSERT_FALSE((bool)Style4);
11522   llvm::consumeError(Style4.takeError());
11523 
11524   // Test 5: error on invalid yaml on command line
11525   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11526   ASSERT_FALSE((bool)Style5);
11527   llvm::consumeError(Style5.takeError());
11528 
11529   // Test 6: error on invalid style
11530   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11531   ASSERT_FALSE((bool)Style6);
11532   llvm::consumeError(Style6.takeError());
11533 
11534   // Test 7: found config file, error on parsing it
11535   ASSERT_TRUE(
11536       FS.addFile("/d/.clang-format", 0,
11537                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11538                                                   "InvalidKey: InvalidValue")));
11539   ASSERT_TRUE(
11540       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11541   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11542   ASSERT_FALSE((bool)Style7);
11543   llvm::consumeError(Style7.takeError());
11544 }
11545 
11546 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11547   // Column limit is 20.
11548   std::string Code = "Type *a =\n"
11549                      "    new Type();\n"
11550                      "g(iiiii, 0, jjjjj,\n"
11551                      "  0, kkkkk, 0, mm);\n"
11552                      "int  bad     = format   ;";
11553   std::string Expected = "auto a = new Type();\n"
11554                          "g(iiiii, nullptr,\n"
11555                          "  jjjjj, nullptr,\n"
11556                          "  kkkkk, nullptr,\n"
11557                          "  mm);\n"
11558                          "int  bad     = format   ;";
11559   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11560   tooling::Replacements Replaces = toReplacements(
11561       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11562                             "auto "),
11563        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11564                             "nullptr"),
11565        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11566                             "nullptr"),
11567        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11568                             "nullptr")});
11569 
11570   format::FormatStyle Style = format::getLLVMStyle();
11571   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11572   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11573   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11574       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11575   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11576   EXPECT_TRUE(static_cast<bool>(Result));
11577   EXPECT_EQ(Expected, *Result);
11578 }
11579 
11580 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11581   std::string Code = "#include \"a.h\"\n"
11582                      "#include \"c.h\"\n"
11583                      "\n"
11584                      "int main() {\n"
11585                      "  return 0;\n"
11586                      "}";
11587   std::string Expected = "#include \"a.h\"\n"
11588                          "#include \"b.h\"\n"
11589                          "#include \"c.h\"\n"
11590                          "\n"
11591                          "int main() {\n"
11592                          "  return 0;\n"
11593                          "}";
11594   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11595   tooling::Replacements Replaces = toReplacements(
11596       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11597                             "#include \"b.h\"\n")});
11598 
11599   format::FormatStyle Style = format::getLLVMStyle();
11600   Style.SortIncludes = true;
11601   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11602   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11603       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11604   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11605   EXPECT_TRUE(static_cast<bool>(Result));
11606   EXPECT_EQ(Expected, *Result);
11607 }
11608 
11609 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
11610   EXPECT_EQ("using std::cin;\n"
11611             "using std::cout;",
11612             format("using std::cout;\n"
11613                    "using std::cin;", getGoogleStyle()));
11614 }
11615 
11616 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
11617   format::FormatStyle Style = format::getLLVMStyle();
11618   Style.Standard = FormatStyle::LS_Cpp03;
11619   // cpp03 recognize this string as identifier u8 and literal character 'a'
11620   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
11621 }
11622 
11623 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
11624   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
11625   // all modes, including C++11, C++14 and C++17
11626   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
11627 }
11628 
11629 TEST_F(FormatTest, DoNotFormatLikelyXml) {
11630   EXPECT_EQ("<!-- ;> -->",
11631             format("<!-- ;> -->", getGoogleStyle()));
11632   EXPECT_EQ(" <!-- >; -->",
11633             format(" <!-- >; -->", getGoogleStyle()));
11634 }
11635 
11636 TEST_F(FormatTest, StructuredBindings) {
11637   // Structured bindings is a C++17 feature.
11638   // all modes, including C++11, C++14 and C++17
11639   verifyFormat("auto [a, b] = f();");
11640   EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();"));
11641   EXPECT_EQ("const auto [a, b] = f();", format("const   auto[a, b] = f();"));
11642   EXPECT_EQ("auto const [a, b] = f();", format("auto  const[a, b] = f();"));
11643   EXPECT_EQ("auto const volatile [a, b] = f();",
11644             format("auto  const   volatile[a, b] = f();"));
11645   EXPECT_EQ("auto [a, b, c] = f();", format("auto   [  a  ,  b,c   ] = f();"));
11646   EXPECT_EQ("auto &[a, b, c] = f();",
11647             format("auto   &[  a  ,  b,c   ] = f();"));
11648   EXPECT_EQ("auto &&[a, b, c] = f();",
11649             format("auto   &&[  a  ,  b,c   ] = f();"));
11650   EXPECT_EQ("auto const &[a, b] = f();", format("auto  const&[a, b] = f();"));
11651   EXPECT_EQ("auto const volatile &&[a, b] = f();",
11652             format("auto  const  volatile  &&[a, b] = f();"));
11653   EXPECT_EQ("auto const &&[a, b] = f();", format("auto  const   &&  [a, b] = f();"));
11654   EXPECT_EQ("const auto &[a, b] = f();", format("const  auto  &  [a, b] = f();"));
11655   EXPECT_EQ("const auto volatile &&[a, b] = f();",
11656             format("const  auto   volatile  &&[a, b] = f();"));
11657   EXPECT_EQ("volatile const auto &&[a, b] = f();",
11658             format("volatile  const  auto   &&[a, b] = f();"));
11659   EXPECT_EQ("const auto &&[a, b] = f();", format("const  auto  &&  [a, b] = f();"));
11660 
11661   // Make sure we don't mistake structured bindings for lambdas.
11662   FormatStyle PointerMiddle = getLLVMStyle();
11663   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
11664   verifyFormat("auto [a1, b]{A * i};", getGoogleStyle());
11665   verifyFormat("auto [a2, b]{A * i};", getLLVMStyle());
11666   verifyFormat("auto [a3, b]{A * i};", PointerMiddle);
11667   verifyFormat("auto const [a1, b]{A * i};", getGoogleStyle());
11668   verifyFormat("auto const [a2, b]{A * i};", getLLVMStyle());
11669   verifyFormat("auto const [a3, b]{A * i};", PointerMiddle);
11670   verifyFormat("auto const& [a1, b]{A * i};", getGoogleStyle());
11671   verifyFormat("auto const &[a2, b]{A * i};", getLLVMStyle());
11672   verifyFormat("auto const & [a3, b]{A * i};", PointerMiddle);
11673   verifyFormat("auto const&& [a1, b]{A * i};", getGoogleStyle());
11674   verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle());
11675   verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle);
11676 
11677   EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}",
11678             format("for (const auto   &&   [a, b] : some_range) {\n}"));
11679   EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}",
11680             format("for (const auto   &   [a, b] : some_range) {\n}"));
11681   EXPECT_EQ("for (const auto [a, b] : some_range) {\n}",
11682             format("for (const auto[a, b] : some_range) {\n}"));
11683   EXPECT_EQ("auto [x, y](expr);", format("auto[x,y]  (expr);"));
11684   EXPECT_EQ("auto &[x, y](expr);", format("auto  &  [x,y]  (expr);"));
11685   EXPECT_EQ("auto &&[x, y](expr);", format("auto  &&  [x,y]  (expr);"));
11686   EXPECT_EQ("auto const &[x, y](expr);", format("auto  const  &  [x,y]  (expr);"));
11687   EXPECT_EQ("auto const &&[x, y](expr);", format("auto  const  &&  [x,y]  (expr);"));
11688   EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y]     {expr};"));
11689   EXPECT_EQ("auto const &[x, y]{expr};", format("auto  const  &  [x,y]  {expr};"));
11690   EXPECT_EQ("auto const &&[x, y]{expr};", format("auto  const  &&  [x,y]  {expr};"));
11691 
11692   format::FormatStyle Spaces = format::getLLVMStyle();
11693   Spaces.SpacesInSquareBrackets = true;
11694   verifyFormat("auto [ a, b ] = f();", Spaces);
11695   verifyFormat("auto &&[ a, b ] = f();", Spaces);
11696   verifyFormat("auto &[ a, b ] = f();", Spaces);
11697   verifyFormat("auto const &&[ a, b ] = f();", Spaces);
11698   verifyFormat("auto const &[ a, b ] = f();", Spaces);
11699 }
11700 
11701 } // end namespace
11702 } // end namespace format
11703 } // end namespace clang
11704