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 //===----------------------------------------------------------------------===//
337 // Tests for control statements.
338 //===----------------------------------------------------------------------===//
339 
340 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
341   verifyFormat("if (true)\n  f();\ng();");
342   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
343   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
344   verifyFormat("if constexpr (true)\n"
345                "  f();\ng();");
346   verifyFormat("if constexpr (a)\n"
347                "  if constexpr (b)\n"
348                "    if constexpr (c)\n"
349                "      g();\n"
350                "h();");
351   verifyFormat("if constexpr (a)\n"
352                "  if constexpr (b) {\n"
353                "    f();\n"
354                "  }\n"
355                "g();");
356 
357   FormatStyle AllowsMergedIf = getLLVMStyle();
358   AllowsMergedIf.AlignEscapedNewlines = FormatStyle::ENAS_Left;
359   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
360   verifyFormat("if (a)\n"
361                "  // comment\n"
362                "  f();",
363                AllowsMergedIf);
364   verifyFormat("{\n"
365                "  if (a)\n"
366                "  label:\n"
367                "    f();\n"
368                "}",
369                AllowsMergedIf);
370   verifyFormat("#define A \\\n"
371                "  if (a)  \\\n"
372                "  label:  \\\n"
373                "    f()",
374                AllowsMergedIf);
375   verifyFormat("if (a)\n"
376                "  ;",
377                AllowsMergedIf);
378   verifyFormat("if (a)\n"
379                "  if (b) return;",
380                AllowsMergedIf);
381 
382   verifyFormat("if (a) // Can't merge this\n"
383                "  f();\n",
384                AllowsMergedIf);
385   verifyFormat("if (a) /* still don't merge */\n"
386                "  f();",
387                AllowsMergedIf);
388   verifyFormat("if (a) { // Never merge this\n"
389                "  f();\n"
390                "}",
391                AllowsMergedIf);
392   verifyFormat("if (a) { /* Never merge this */\n"
393                "  f();\n"
394                "}",
395                AllowsMergedIf);
396 
397   AllowsMergedIf.ColumnLimit = 14;
398   verifyFormat("if (a) return;", AllowsMergedIf);
399   verifyFormat("if (aaaaaaaaa)\n"
400                "  return;",
401                AllowsMergedIf);
402 
403   AllowsMergedIf.ColumnLimit = 13;
404   verifyFormat("if (a)\n  return;", AllowsMergedIf);
405 }
406 
407 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
408   FormatStyle AllowsMergedLoops = getLLVMStyle();
409   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
410   verifyFormat("while (true) continue;", AllowsMergedLoops);
411   verifyFormat("for (;;) continue;", AllowsMergedLoops);
412   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
413   verifyFormat("while (true)\n"
414                "  ;",
415                AllowsMergedLoops);
416   verifyFormat("for (;;)\n"
417                "  ;",
418                AllowsMergedLoops);
419   verifyFormat("for (;;)\n"
420                "  for (;;) continue;",
421                AllowsMergedLoops);
422   verifyFormat("for (;;) // Can't merge this\n"
423                "  continue;",
424                AllowsMergedLoops);
425   verifyFormat("for (;;) /* still don't merge */\n"
426                "  continue;",
427                AllowsMergedLoops);
428 }
429 
430 TEST_F(FormatTest, FormatShortBracedStatements) {
431   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
432   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
433 
434   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
435   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
436 
437   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
438   verifyFormat("if constexpr (true) {}", AllowSimpleBracedStatements);
439   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
440   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
441   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
442   verifyFormat("if constexpr (true) { f(); }", AllowSimpleBracedStatements);
443   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
444   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
445   verifyFormat("if (true) { //\n"
446                "  f();\n"
447                "}",
448                AllowSimpleBracedStatements);
449   verifyFormat("if (true) {\n"
450                "  f();\n"
451                "  f();\n"
452                "}",
453                AllowSimpleBracedStatements);
454   verifyFormat("if (true) {\n"
455                "  f();\n"
456                "} else {\n"
457                "  f();\n"
458                "}",
459                AllowSimpleBracedStatements);
460 
461   verifyFormat("struct A2 {\n"
462                "  int X;\n"
463                "};",
464                AllowSimpleBracedStatements);
465   verifyFormat("typedef struct A2 {\n"
466                "  int X;\n"
467                "} A2_t;",
468                AllowSimpleBracedStatements);
469   verifyFormat("template <int> struct A2 {\n"
470                "  struct B {};\n"
471                "};",
472                AllowSimpleBracedStatements);
473 
474   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
475   verifyFormat("if (true) {\n"
476                "  f();\n"
477                "}",
478                AllowSimpleBracedStatements);
479   verifyFormat("if (true) {\n"
480                "  f();\n"
481                "} else {\n"
482                "  f();\n"
483                "}",
484                AllowSimpleBracedStatements);
485 
486   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
487   verifyFormat("while (true) {\n"
488                "  f();\n"
489                "}",
490                AllowSimpleBracedStatements);
491   verifyFormat("for (;;) {\n"
492                "  f();\n"
493                "}",
494                AllowSimpleBracedStatements);
495 }
496 
497 TEST_F(FormatTest, ParseIfElse) {
498   verifyFormat("if (true)\n"
499                "  if (true)\n"
500                "    if (true)\n"
501                "      f();\n"
502                "    else\n"
503                "      g();\n"
504                "  else\n"
505                "    h();\n"
506                "else\n"
507                "  i();");
508   verifyFormat("if (true)\n"
509                "  if (true)\n"
510                "    if (true) {\n"
511                "      if (true)\n"
512                "        f();\n"
513                "    } else {\n"
514                "      g();\n"
515                "    }\n"
516                "  else\n"
517                "    h();\n"
518                "else {\n"
519                "  i();\n"
520                "}");
521   verifyFormat("if (true)\n"
522                "  if constexpr (true)\n"
523                "    if (true) {\n"
524                "      if constexpr (true)\n"
525                "        f();\n"
526                "    } else {\n"
527                "      g();\n"
528                "    }\n"
529                "  else\n"
530                "    h();\n"
531                "else {\n"
532                "  i();\n"
533                "}");
534   verifyFormat("void f() {\n"
535                "  if (a) {\n"
536                "  } else {\n"
537                "  }\n"
538                "}");
539 }
540 
541 TEST_F(FormatTest, ElseIf) {
542   verifyFormat("if (a) {\n} else if (b) {\n}");
543   verifyFormat("if (a)\n"
544                "  f();\n"
545                "else if (b)\n"
546                "  g();\n"
547                "else\n"
548                "  h();");
549   verifyFormat("if constexpr (a)\n"
550                "  f();\n"
551                "else if constexpr (b)\n"
552                "  g();\n"
553                "else\n"
554                "  h();");
555   verifyFormat("if (a) {\n"
556                "  f();\n"
557                "}\n"
558                "// or else ..\n"
559                "else {\n"
560                "  g()\n"
561                "}");
562 
563   verifyFormat("if (a) {\n"
564                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
565                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
566                "}");
567   verifyFormat("if (a) {\n"
568                "} else if (\n"
569                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
570                "}",
571                getLLVMStyleWithColumns(62));
572   verifyFormat("if (a) {\n"
573                "} else if constexpr (\n"
574                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
575                "}",
576                getLLVMStyleWithColumns(62));
577 }
578 
579 TEST_F(FormatTest, FormatsForLoop) {
580   verifyFormat(
581       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
582       "     ++VeryVeryLongLoopVariable)\n"
583       "  ;");
584   verifyFormat("for (;;)\n"
585                "  f();");
586   verifyFormat("for (;;) {\n}");
587   verifyFormat("for (;;) {\n"
588                "  f();\n"
589                "}");
590   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
591 
592   verifyFormat(
593       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
594       "                                          E = UnwrappedLines.end();\n"
595       "     I != E; ++I) {\n}");
596 
597   verifyFormat(
598       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
599       "     ++IIIII) {\n}");
600   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
601                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
602                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
603   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
604                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
605                "         E = FD->getDeclsInPrototypeScope().end();\n"
606                "     I != E; ++I) {\n}");
607   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
608                "         I = Container.begin(),\n"
609                "         E = Container.end();\n"
610                "     I != E; ++I) {\n}",
611                getLLVMStyleWithColumns(76));
612 
613   verifyFormat(
614       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
615       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
616       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
617       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
618       "     ++aaaaaaaaaaa) {\n}");
619   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
620                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
621                "     ++i) {\n}");
622   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
623                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
624                "}");
625   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
626                "         aaaaaaaaaa);\n"
627                "     iter; ++iter) {\n"
628                "}");
629   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
630                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
631                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
632                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
633 
634   FormatStyle NoBinPacking = getLLVMStyle();
635   NoBinPacking.BinPackParameters = false;
636   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
637                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
638                "                                           aaaaaaaaaaaaaaaa,\n"
639                "                                           aaaaaaaaaaaaaaaa,\n"
640                "                                           aaaaaaaaaaaaaaaa);\n"
641                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
642                "}",
643                NoBinPacking);
644   verifyFormat(
645       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
646       "                                          E = UnwrappedLines.end();\n"
647       "     I != E;\n"
648       "     ++I) {\n}",
649       NoBinPacking);
650 }
651 
652 TEST_F(FormatTest, RangeBasedForLoops) {
653   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
654                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
655   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
656                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
657   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
658                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
659   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
660                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
661 }
662 
663 TEST_F(FormatTest, ForEachLoops) {
664   verifyFormat("void f() {\n"
665                "  foreach (Item *item, itemlist) {}\n"
666                "  Q_FOREACH (Item *item, itemlist) {}\n"
667                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
668                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
669                "}");
670 
671   // As function-like macros.
672   verifyFormat("#define foreach(x, y)\n"
673                "#define Q_FOREACH(x, y)\n"
674                "#define BOOST_FOREACH(x, y)\n"
675                "#define UNKNOWN_FOREACH(x, y)\n");
676 
677   // Not as function-like macros.
678   verifyFormat("#define foreach (x, y)\n"
679                "#define Q_FOREACH (x, y)\n"
680                "#define BOOST_FOREACH (x, y)\n"
681                "#define UNKNOWN_FOREACH (x, y)\n");
682 }
683 
684 TEST_F(FormatTest, FormatsWhileLoop) {
685   verifyFormat("while (true) {\n}");
686   verifyFormat("while (true)\n"
687                "  f();");
688   verifyFormat("while () {\n}");
689   verifyFormat("while () {\n"
690                "  f();\n"
691                "}");
692 }
693 
694 TEST_F(FormatTest, FormatsDoWhile) {
695   verifyFormat("do {\n"
696                "  do_something();\n"
697                "} while (something());");
698   verifyFormat("do\n"
699                "  do_something();\n"
700                "while (something());");
701 }
702 
703 TEST_F(FormatTest, FormatsSwitchStatement) {
704   verifyFormat("switch (x) {\n"
705                "case 1:\n"
706                "  f();\n"
707                "  break;\n"
708                "case kFoo:\n"
709                "case ns::kBar:\n"
710                "case kBaz:\n"
711                "  break;\n"
712                "default:\n"
713                "  g();\n"
714                "  break;\n"
715                "}");
716   verifyFormat("switch (x) {\n"
717                "case 1: {\n"
718                "  f();\n"
719                "  break;\n"
720                "}\n"
721                "case 2: {\n"
722                "  break;\n"
723                "}\n"
724                "}");
725   verifyFormat("switch (x) {\n"
726                "case 1: {\n"
727                "  f();\n"
728                "  {\n"
729                "    g();\n"
730                "    h();\n"
731                "  }\n"
732                "  break;\n"
733                "}\n"
734                "}");
735   verifyFormat("switch (x) {\n"
736                "case 1: {\n"
737                "  f();\n"
738                "  if (foo) {\n"
739                "    g();\n"
740                "    h();\n"
741                "  }\n"
742                "  break;\n"
743                "}\n"
744                "}");
745   verifyFormat("switch (x) {\n"
746                "case 1: {\n"
747                "  f();\n"
748                "  g();\n"
749                "} break;\n"
750                "}");
751   verifyFormat("switch (test)\n"
752                "  ;");
753   verifyFormat("switch (x) {\n"
754                "default: {\n"
755                "  // Do nothing.\n"
756                "}\n"
757                "}");
758   verifyFormat("switch (x) {\n"
759                "// comment\n"
760                "// if 1, do f()\n"
761                "case 1:\n"
762                "  f();\n"
763                "}");
764   verifyFormat("switch (x) {\n"
765                "case 1:\n"
766                "  // Do amazing stuff\n"
767                "  {\n"
768                "    f();\n"
769                "    g();\n"
770                "  }\n"
771                "  break;\n"
772                "}");
773   verifyFormat("#define A          \\\n"
774                "  switch (x) {     \\\n"
775                "  case a:          \\\n"
776                "    foo = b;       \\\n"
777                "  }",
778                getLLVMStyleWithColumns(20));
779   verifyFormat("#define OPERATION_CASE(name)           \\\n"
780                "  case OP_name:                        \\\n"
781                "    return operations::Operation##name\n",
782                getLLVMStyleWithColumns(40));
783   verifyFormat("switch (x) {\n"
784                "case 1:;\n"
785                "default:;\n"
786                "  int i;\n"
787                "}");
788 
789   verifyGoogleFormat("switch (x) {\n"
790                      "  case 1:\n"
791                      "    f();\n"
792                      "    break;\n"
793                      "  case kFoo:\n"
794                      "  case ns::kBar:\n"
795                      "  case kBaz:\n"
796                      "    break;\n"
797                      "  default:\n"
798                      "    g();\n"
799                      "    break;\n"
800                      "}");
801   verifyGoogleFormat("switch (x) {\n"
802                      "  case 1: {\n"
803                      "    f();\n"
804                      "    break;\n"
805                      "  }\n"
806                      "}");
807   verifyGoogleFormat("switch (test)\n"
808                      "  ;");
809 
810   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
811                      "  case OP_name:              \\\n"
812                      "    return operations::Operation##name\n");
813   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
814                      "  // Get the correction operation class.\n"
815                      "  switch (OpCode) {\n"
816                      "    CASE(Add);\n"
817                      "    CASE(Subtract);\n"
818                      "    default:\n"
819                      "      return operations::Unknown;\n"
820                      "  }\n"
821                      "#undef OPERATION_CASE\n"
822                      "}");
823   verifyFormat("DEBUG({\n"
824                "  switch (x) {\n"
825                "  case A:\n"
826                "    f();\n"
827                "    break;\n"
828                "    // fallthrough\n"
829                "  case B:\n"
830                "    g();\n"
831                "    break;\n"
832                "  }\n"
833                "});");
834   EXPECT_EQ("DEBUG({\n"
835             "  switch (x) {\n"
836             "  case A:\n"
837             "    f();\n"
838             "    break;\n"
839             "  // On B:\n"
840             "  case B:\n"
841             "    g();\n"
842             "    break;\n"
843             "  }\n"
844             "});",
845             format("DEBUG({\n"
846                    "  switch (x) {\n"
847                    "  case A:\n"
848                    "    f();\n"
849                    "    break;\n"
850                    "  // On B:\n"
851                    "  case B:\n"
852                    "    g();\n"
853                    "    break;\n"
854                    "  }\n"
855                    "});",
856                    getLLVMStyle()));
857   verifyFormat("switch (a) {\n"
858                "case (b):\n"
859                "  return;\n"
860                "}");
861 
862   verifyFormat("switch (a) {\n"
863                "case some_namespace::\n"
864                "    some_constant:\n"
865                "  return;\n"
866                "}",
867                getLLVMStyleWithColumns(34));
868 }
869 
870 TEST_F(FormatTest, CaseRanges) {
871   verifyFormat("switch (x) {\n"
872                "case 'A' ... 'Z':\n"
873                "case 1 ... 5:\n"
874                "case a ... b:\n"
875                "  break;\n"
876                "}");
877 }
878 
879 TEST_F(FormatTest, ShortCaseLabels) {
880   FormatStyle Style = getLLVMStyle();
881   Style.AllowShortCaseLabelsOnASingleLine = true;
882   verifyFormat("switch (a) {\n"
883                "case 1: x = 1; break;\n"
884                "case 2: return;\n"
885                "case 3:\n"
886                "case 4:\n"
887                "case 5: return;\n"
888                "case 6: // comment\n"
889                "  return;\n"
890                "case 7:\n"
891                "  // comment\n"
892                "  return;\n"
893                "case 8:\n"
894                "  x = 8; // comment\n"
895                "  break;\n"
896                "default: y = 1; break;\n"
897                "}",
898                Style);
899   verifyFormat("switch (a) {\n"
900                "#if FOO\n"
901                "case 0: return 0;\n"
902                "#endif\n"
903                "}",
904                Style);
905   verifyFormat("switch (a) {\n"
906                "case 1: {\n"
907                "}\n"
908                "case 2: {\n"
909                "  return;\n"
910                "}\n"
911                "case 3: {\n"
912                "  x = 1;\n"
913                "  return;\n"
914                "}\n"
915                "case 4:\n"
916                "  if (x)\n"
917                "    return;\n"
918                "}",
919                Style);
920   Style.ColumnLimit = 21;
921   verifyFormat("switch (a) {\n"
922                "case 1: x = 1; break;\n"
923                "case 2: return;\n"
924                "case 3:\n"
925                "case 4:\n"
926                "case 5: return;\n"
927                "default:\n"
928                "  y = 1;\n"
929                "  break;\n"
930                "}",
931                Style);
932 }
933 
934 TEST_F(FormatTest, FormatsLabels) {
935   verifyFormat("void f() {\n"
936                "  some_code();\n"
937                "test_label:\n"
938                "  some_other_code();\n"
939                "  {\n"
940                "    some_more_code();\n"
941                "  another_label:\n"
942                "    some_more_code();\n"
943                "  }\n"
944                "}");
945   verifyFormat("{\n"
946                "  some_code();\n"
947                "test_label:\n"
948                "  some_other_code();\n"
949                "}");
950   verifyFormat("{\n"
951                "  some_code();\n"
952                "test_label:;\n"
953                "  int i = 0;\n"
954                "}");
955 }
956 
957 //===----------------------------------------------------------------------===//
958 // Tests for classes, namespaces, etc.
959 //===----------------------------------------------------------------------===//
960 
961 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
962   verifyFormat("class A {};");
963 }
964 
965 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
966   verifyFormat("class A {\n"
967                "public:\n"
968                "public: // comment\n"
969                "protected:\n"
970                "private:\n"
971                "  void f() {}\n"
972                "};");
973   verifyGoogleFormat("class A {\n"
974                      " public:\n"
975                      " protected:\n"
976                      " private:\n"
977                      "  void f() {}\n"
978                      "};");
979   verifyFormat("class A {\n"
980                "public slots:\n"
981                "  void f1() {}\n"
982                "public Q_SLOTS:\n"
983                "  void f2() {}\n"
984                "protected slots:\n"
985                "  void f3() {}\n"
986                "protected Q_SLOTS:\n"
987                "  void f4() {}\n"
988                "private slots:\n"
989                "  void f5() {}\n"
990                "private Q_SLOTS:\n"
991                "  void f6() {}\n"
992                "signals:\n"
993                "  void g1();\n"
994                "Q_SIGNALS:\n"
995                "  void g2();\n"
996                "};");
997 
998   // Don't interpret 'signals' the wrong way.
999   verifyFormat("signals.set();");
1000   verifyFormat("for (Signals signals : f()) {\n}");
1001   verifyFormat("{\n"
1002                "  signals.set(); // This needs indentation.\n"
1003                "}");
1004   verifyFormat("void f() {\n"
1005                "label:\n"
1006                "  signals.baz();\n"
1007                "}");
1008 }
1009 
1010 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1011   EXPECT_EQ("class A {\n"
1012             "public:\n"
1013             "  void f();\n"
1014             "\n"
1015             "private:\n"
1016             "  void g() {}\n"
1017             "  // test\n"
1018             "protected:\n"
1019             "  int h;\n"
1020             "};",
1021             format("class A {\n"
1022                    "public:\n"
1023                    "void f();\n"
1024                    "private:\n"
1025                    "void g() {}\n"
1026                    "// test\n"
1027                    "protected:\n"
1028                    "int h;\n"
1029                    "};"));
1030   EXPECT_EQ("class A {\n"
1031             "protected:\n"
1032             "public:\n"
1033             "  void f();\n"
1034             "};",
1035             format("class A {\n"
1036                    "protected:\n"
1037                    "\n"
1038                    "public:\n"
1039                    "\n"
1040                    "  void f();\n"
1041                    "};"));
1042 
1043   // Even ensure proper spacing inside macros.
1044   EXPECT_EQ("#define B     \\\n"
1045             "  class A {   \\\n"
1046             "   protected: \\\n"
1047             "   public:    \\\n"
1048             "    void f(); \\\n"
1049             "  };",
1050             format("#define B     \\\n"
1051                    "  class A {   \\\n"
1052                    "   protected: \\\n"
1053                    "              \\\n"
1054                    "   public:    \\\n"
1055                    "              \\\n"
1056                    "    void f(); \\\n"
1057                    "  };",
1058                    getGoogleStyle()));
1059   // But don't remove empty lines after macros ending in access specifiers.
1060   EXPECT_EQ("#define A private:\n"
1061             "\n"
1062             "int i;",
1063             format("#define A         private:\n"
1064                    "\n"
1065                    "int              i;"));
1066 }
1067 
1068 TEST_F(FormatTest, FormatsClasses) {
1069   verifyFormat("class A : public B {};");
1070   verifyFormat("class A : public ::B {};");
1071 
1072   verifyFormat(
1073       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1074       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1075   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1076                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1077                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1078   verifyFormat(
1079       "class A : public B, public C, public D, public E, public F {};");
1080   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1081                "                     public C,\n"
1082                "                     public D,\n"
1083                "                     public E,\n"
1084                "                     public F,\n"
1085                "                     public G {};");
1086 
1087   verifyFormat("class\n"
1088                "    ReallyReallyLongClassName {\n"
1089                "  int i;\n"
1090                "};",
1091                getLLVMStyleWithColumns(32));
1092   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1093                "                           aaaaaaaaaaaaaaaa> {};");
1094   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1095                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1096                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1097   verifyFormat("template <class R, class C>\n"
1098                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1099                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1100   verifyFormat("class ::A::B {};");
1101 }
1102 
1103 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1104   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1105   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1106 
1107   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1108   verifyFormat("class MyClass\n"
1109                "    : public X\n"
1110                "    , public Y {};",
1111                StyleWithInheritanceBreak);
1112 }
1113 
1114 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1115   verifyFormat("class A {\n} a, b;");
1116   verifyFormat("struct A {\n} a, b;");
1117   verifyFormat("union A {\n} a;");
1118 }
1119 
1120 TEST_F(FormatTest, FormatsEnum) {
1121   verifyFormat("enum {\n"
1122                "  Zero,\n"
1123                "  One = 1,\n"
1124                "  Two = One + 1,\n"
1125                "  Three = (One + Two),\n"
1126                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1127                "  Five = (One, Two, Three, Four, 5)\n"
1128                "};");
1129   verifyGoogleFormat("enum {\n"
1130                      "  Zero,\n"
1131                      "  One = 1,\n"
1132                      "  Two = One + 1,\n"
1133                      "  Three = (One + Two),\n"
1134                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1135                      "  Five = (One, Two, Three, Four, 5)\n"
1136                      "};");
1137   verifyFormat("enum Enum {};");
1138   verifyFormat("enum {};");
1139   verifyFormat("enum X E {} d;");
1140   verifyFormat("enum __attribute__((...)) E {} d;");
1141   verifyFormat("enum __declspec__((...)) E {} d;");
1142   verifyFormat("enum {\n"
1143                "  Bar = Foo<int, int>::value\n"
1144                "};",
1145                getLLVMStyleWithColumns(30));
1146 
1147   verifyFormat("enum ShortEnum { A, B, C };");
1148   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1149 
1150   EXPECT_EQ("enum KeepEmptyLines {\n"
1151             "  ONE,\n"
1152             "\n"
1153             "  TWO,\n"
1154             "\n"
1155             "  THREE\n"
1156             "}",
1157             format("enum KeepEmptyLines {\n"
1158                    "  ONE,\n"
1159                    "\n"
1160                    "  TWO,\n"
1161                    "\n"
1162                    "\n"
1163                    "  THREE\n"
1164                    "}"));
1165   verifyFormat("enum E { // comment\n"
1166                "  ONE,\n"
1167                "  TWO\n"
1168                "};\n"
1169                "int i;");
1170   // Not enums.
1171   verifyFormat("enum X f() {\n"
1172                "  a();\n"
1173                "  return 42;\n"
1174                "}");
1175   verifyFormat("enum X Type::f() {\n"
1176                "  a();\n"
1177                "  return 42;\n"
1178                "}");
1179   verifyFormat("enum ::X f() {\n"
1180                "  a();\n"
1181                "  return 42;\n"
1182                "}");
1183   verifyFormat("enum ns::X f() {\n"
1184                "  a();\n"
1185                "  return 42;\n"
1186                "}");
1187 }
1188 
1189 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1190   verifyFormat("enum Type {\n"
1191                "  One = 0; // These semicolons should be commas.\n"
1192                "  Two = 1;\n"
1193                "};");
1194   verifyFormat("namespace n {\n"
1195                "enum Type {\n"
1196                "  One,\n"
1197                "  Two, // missing };\n"
1198                "  int i;\n"
1199                "}\n"
1200                "void g() {}");
1201 }
1202 
1203 TEST_F(FormatTest, FormatsEnumStruct) {
1204   verifyFormat("enum struct {\n"
1205                "  Zero,\n"
1206                "  One = 1,\n"
1207                "  Two = One + 1,\n"
1208                "  Three = (One + Two),\n"
1209                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1210                "  Five = (One, Two, Three, Four, 5)\n"
1211                "};");
1212   verifyFormat("enum struct Enum {};");
1213   verifyFormat("enum struct {};");
1214   verifyFormat("enum struct X E {} d;");
1215   verifyFormat("enum struct __attribute__((...)) E {} d;");
1216   verifyFormat("enum struct __declspec__((...)) E {} d;");
1217   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1218 }
1219 
1220 TEST_F(FormatTest, FormatsEnumClass) {
1221   verifyFormat("enum class {\n"
1222                "  Zero,\n"
1223                "  One = 1,\n"
1224                "  Two = One + 1,\n"
1225                "  Three = (One + Two),\n"
1226                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1227                "  Five = (One, Two, Three, Four, 5)\n"
1228                "};");
1229   verifyFormat("enum class Enum {};");
1230   verifyFormat("enum class {};");
1231   verifyFormat("enum class X E {} d;");
1232   verifyFormat("enum class __attribute__((...)) E {} d;");
1233   verifyFormat("enum class __declspec__((...)) E {} d;");
1234   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1235 }
1236 
1237 TEST_F(FormatTest, FormatsEnumTypes) {
1238   verifyFormat("enum X : int {\n"
1239                "  A, // Force multiple lines.\n"
1240                "  B\n"
1241                "};");
1242   verifyFormat("enum X : int { A, B };");
1243   verifyFormat("enum X : std::uint32_t { A, B };");
1244 }
1245 
1246 TEST_F(FormatTest, FormatsNSEnums) {
1247   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1248   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1249                      "  // Information about someDecentlyLongValue.\n"
1250                      "  someDecentlyLongValue,\n"
1251                      "  // Information about anotherDecentlyLongValue.\n"
1252                      "  anotherDecentlyLongValue,\n"
1253                      "  // Information about aThirdDecentlyLongValue.\n"
1254                      "  aThirdDecentlyLongValue\n"
1255                      "};");
1256   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1257                      "  a = 1,\n"
1258                      "  b = 2,\n"
1259                      "  c = 3,\n"
1260                      "};");
1261   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1262                      "  a = 1,\n"
1263                      "  b = 2,\n"
1264                      "  c = 3,\n"
1265                      "};");
1266   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1267                      "  a = 1,\n"
1268                      "  b = 2,\n"
1269                      "  c = 3,\n"
1270                      "};");
1271 }
1272 
1273 TEST_F(FormatTest, FormatsBitfields) {
1274   verifyFormat("struct Bitfields {\n"
1275                "  unsigned sClass : 8;\n"
1276                "  unsigned ValueKind : 2;\n"
1277                "};");
1278   verifyFormat("struct A {\n"
1279                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1280                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1281                "};");
1282   verifyFormat("struct MyStruct {\n"
1283                "  uchar data;\n"
1284                "  uchar : 8;\n"
1285                "  uchar : 8;\n"
1286                "  uchar other;\n"
1287                "};");
1288 }
1289 
1290 TEST_F(FormatTest, FormatsNamespaces) {
1291   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1292   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1293 
1294   verifyFormat("namespace some_namespace {\n"
1295                "class A {};\n"
1296                "void f() { f(); }\n"
1297                "}",
1298                LLVMWithNoNamespaceFix);
1299   verifyFormat("namespace {\n"
1300                "class A {};\n"
1301                "void f() { f(); }\n"
1302                "}",
1303                LLVMWithNoNamespaceFix);
1304   verifyFormat("inline namespace X {\n"
1305                "class A {};\n"
1306                "void f() { f(); }\n"
1307                "}",
1308                LLVMWithNoNamespaceFix);
1309   verifyFormat("using namespace some_namespace;\n"
1310                "class A {};\n"
1311                "void f() { f(); }",
1312                LLVMWithNoNamespaceFix);
1313 
1314   // This code is more common than we thought; if we
1315   // layout this correctly the semicolon will go into
1316   // its own line, which is undesirable.
1317   verifyFormat("namespace {};",
1318                LLVMWithNoNamespaceFix);
1319   verifyFormat("namespace {\n"
1320                "class A {};\n"
1321                "};",
1322                LLVMWithNoNamespaceFix);
1323 
1324   verifyFormat("namespace {\n"
1325                "int SomeVariable = 0; // comment\n"
1326                "} // namespace",
1327                LLVMWithNoNamespaceFix);
1328   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1329             "#define HEADER_GUARD\n"
1330             "namespace my_namespace {\n"
1331             "int i;\n"
1332             "} // my_namespace\n"
1333             "#endif // HEADER_GUARD",
1334             format("#ifndef HEADER_GUARD\n"
1335                    " #define HEADER_GUARD\n"
1336                    "   namespace my_namespace {\n"
1337                    "int i;\n"
1338                    "}    // my_namespace\n"
1339                    "#endif    // HEADER_GUARD",
1340                    LLVMWithNoNamespaceFix));
1341 
1342   EXPECT_EQ("namespace A::B {\n"
1343             "class C {};\n"
1344             "}",
1345             format("namespace A::B {\n"
1346                    "class C {};\n"
1347                    "}",
1348                    LLVMWithNoNamespaceFix));
1349 
1350   FormatStyle Style = getLLVMStyle();
1351   Style.NamespaceIndentation = FormatStyle::NI_All;
1352   EXPECT_EQ("namespace out {\n"
1353             "  int i;\n"
1354             "  namespace in {\n"
1355             "    int i;\n"
1356             "  } // namespace in\n"
1357             "} // namespace out",
1358             format("namespace out {\n"
1359                    "int i;\n"
1360                    "namespace in {\n"
1361                    "int i;\n"
1362                    "} // namespace in\n"
1363                    "} // namespace out",
1364                    Style));
1365 
1366   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1367   EXPECT_EQ("namespace out {\n"
1368             "int i;\n"
1369             "namespace in {\n"
1370             "  int i;\n"
1371             "} // namespace in\n"
1372             "} // namespace out",
1373             format("namespace out {\n"
1374                    "int i;\n"
1375                    "namespace in {\n"
1376                    "int i;\n"
1377                    "} // namespace in\n"
1378                    "} // namespace out",
1379                    Style));
1380 }
1381 
1382 TEST_F(FormatTest, FormatsCompactNamespaces) {
1383   FormatStyle Style = getLLVMStyle();
1384   Style.CompactNamespaces = true;
1385 
1386   verifyFormat("namespace A { namespace B {\n"
1387 			   "}} // namespace A::B",
1388 			   Style);
1389 
1390   EXPECT_EQ("namespace out { namespace in {\n"
1391             "}} // namespace out::in",
1392             format("namespace out {\n"
1393                    "namespace in {\n"
1394                    "} // namespace in\n"
1395                    "} // namespace out",
1396                    Style));
1397 
1398   // Only namespaces which have both consecutive opening and end get compacted
1399   EXPECT_EQ("namespace out {\n"
1400             "namespace in1 {\n"
1401             "} // namespace in1\n"
1402             "namespace in2 {\n"
1403             "} // namespace in2\n"
1404             "} // namespace out",
1405             format("namespace out {\n"
1406                    "namespace in1 {\n"
1407                    "} // namespace in1\n"
1408                    "namespace in2 {\n"
1409                    "} // namespace in2\n"
1410                    "} // namespace out",
1411                    Style));
1412 
1413   EXPECT_EQ("namespace out {\n"
1414             "int i;\n"
1415             "namespace in {\n"
1416             "int j;\n"
1417             "} // namespace in\n"
1418             "int k;\n"
1419             "} // namespace out",
1420             format("namespace out { int i;\n"
1421                    "namespace in { int j; } // namespace in\n"
1422                    "int k; } // namespace out",
1423                    Style));
1424 
1425   EXPECT_EQ("namespace A { namespace B { namespace C {\n"
1426             "}}} // namespace A::B::C\n",
1427             format("namespace A { namespace B {\n"
1428                    "namespace C {\n"
1429                    "}} // namespace B::C\n"
1430                    "} // namespace A\n",
1431                    Style));
1432 
1433   Style.ColumnLimit = 40;
1434   EXPECT_EQ("namespace aaaaaaaaaa {\n"
1435             "namespace bbbbbbbbbb {\n"
1436             "}} // namespace aaaaaaaaaa::bbbbbbbbbb",
1437             format("namespace aaaaaaaaaa {\n"
1438                    "namespace bbbbbbbbbb {\n"
1439                    "} // namespace bbbbbbbbbb\n"
1440                    "} // namespace aaaaaaaaaa",
1441                    Style));
1442 
1443   EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n"
1444             "namespace cccccc {\n"
1445             "}}} // namespace aaaaaa::bbbbbb::cccccc",
1446             format("namespace aaaaaa {\n"
1447                    "namespace bbbbbb {\n"
1448                    "namespace cccccc {\n"
1449                    "} // namespace cccccc\n"
1450                    "} // namespace bbbbbb\n"
1451                    "} // namespace aaaaaa",
1452                    Style));
1453   Style.ColumnLimit = 80;
1454 
1455   // Extra semicolon after 'inner' closing brace prevents merging
1456   EXPECT_EQ("namespace out { namespace in {\n"
1457             "}; } // namespace out::in",
1458             format("namespace out {\n"
1459                    "namespace in {\n"
1460                    "}; // namespace in\n"
1461                    "} // namespace out",
1462                    Style));
1463 
1464   // Extra semicolon after 'outer' closing brace is conserved
1465   EXPECT_EQ("namespace out { namespace in {\n"
1466             "}}; // namespace out::in",
1467             format("namespace out {\n"
1468                    "namespace in {\n"
1469                    "} // namespace in\n"
1470                    "}; // namespace out",
1471                    Style));
1472 
1473   Style.NamespaceIndentation = FormatStyle::NI_All;
1474   EXPECT_EQ("namespace out { namespace in {\n"
1475             "  int i;\n"
1476             "}} // namespace out::in",
1477             format("namespace out {\n"
1478                    "namespace in {\n"
1479                    "int i;\n"
1480                    "} // namespace in\n"
1481                    "} // namespace out",
1482                    Style));
1483   EXPECT_EQ("namespace out { namespace mid {\n"
1484             "  namespace in {\n"
1485             "    int j;\n"
1486             "  } // namespace in\n"
1487             "  int k;\n"
1488             "}} // namespace out::mid",
1489             format("namespace out { namespace mid {\n"
1490                    "namespace in { int j; } // namespace in\n"
1491                    "int k; }} // namespace out::mid",
1492                    Style));
1493 
1494   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1495   EXPECT_EQ("namespace out { namespace in {\n"
1496             "  int i;\n"
1497             "}} // namespace out::in",
1498             format("namespace out {\n"
1499                    "namespace in {\n"
1500                    "int i;\n"
1501                    "} // namespace in\n"
1502                    "} // namespace out",
1503                    Style));
1504   EXPECT_EQ("namespace out { namespace mid { namespace in {\n"
1505             "  int i;\n"
1506             "}}} // namespace out::mid::in",
1507             format("namespace out {\n"
1508                    "namespace mid {\n"
1509                    "namespace in {\n"
1510                    "int i;\n"
1511                    "} // namespace in\n"
1512                    "} // namespace mid\n"
1513                    "} // namespace out",
1514                    Style));
1515 }
1516 
1517 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1518 
1519 TEST_F(FormatTest, FormatsInlineASM) {
1520   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1521   verifyFormat("asm(\"nop\" ::: \"memory\");");
1522   verifyFormat(
1523       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1524       "    \"cpuid\\n\\t\"\n"
1525       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1526       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1527       "    : \"a\"(value));");
1528   EXPECT_EQ(
1529       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1530       "  __asm {\n"
1531       "        mov     edx,[that] // vtable in edx\n"
1532       "        mov     eax,methodIndex\n"
1533       "        call    [edx][eax*4] // stdcall\n"
1534       "  }\n"
1535       "}",
1536       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1537              "    __asm {\n"
1538              "        mov     edx,[that] // vtable in edx\n"
1539              "        mov     eax,methodIndex\n"
1540              "        call    [edx][eax*4] // stdcall\n"
1541              "    }\n"
1542              "}"));
1543   EXPECT_EQ("_asm {\n"
1544             "  xor eax, eax;\n"
1545             "  cpuid;\n"
1546             "}",
1547             format("_asm {\n"
1548                    "  xor eax, eax;\n"
1549                    "  cpuid;\n"
1550                    "}"));
1551   verifyFormat("void function() {\n"
1552                "  // comment\n"
1553                "  asm(\"\");\n"
1554                "}");
1555   EXPECT_EQ("__asm {\n"
1556             "}\n"
1557             "int i;",
1558             format("__asm   {\n"
1559                    "}\n"
1560                    "int   i;"));
1561 }
1562 
1563 TEST_F(FormatTest, FormatTryCatch) {
1564   verifyFormat("try {\n"
1565                "  throw a * b;\n"
1566                "} catch (int a) {\n"
1567                "  // Do nothing.\n"
1568                "} catch (...) {\n"
1569                "  exit(42);\n"
1570                "}");
1571 
1572   // Function-level try statements.
1573   verifyFormat("int f() try { return 4; } catch (...) {\n"
1574                "  return 5;\n"
1575                "}");
1576   verifyFormat("class A {\n"
1577                "  int a;\n"
1578                "  A() try : a(0) {\n"
1579                "  } catch (...) {\n"
1580                "    throw;\n"
1581                "  }\n"
1582                "};\n");
1583 
1584   // Incomplete try-catch blocks.
1585   verifyIncompleteFormat("try {} catch (");
1586 }
1587 
1588 TEST_F(FormatTest, FormatSEHTryCatch) {
1589   verifyFormat("__try {\n"
1590                "  int a = b * c;\n"
1591                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1592                "  // Do nothing.\n"
1593                "}");
1594 
1595   verifyFormat("__try {\n"
1596                "  int a = b * c;\n"
1597                "} __finally {\n"
1598                "  // Do nothing.\n"
1599                "}");
1600 
1601   verifyFormat("DEBUG({\n"
1602                "  __try {\n"
1603                "  } __finally {\n"
1604                "  }\n"
1605                "});\n");
1606 }
1607 
1608 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1609   verifyFormat("try {\n"
1610                "  f();\n"
1611                "} catch {\n"
1612                "  g();\n"
1613                "}");
1614   verifyFormat("try {\n"
1615                "  f();\n"
1616                "} catch (A a) MACRO(x) {\n"
1617                "  g();\n"
1618                "} catch (B b) MACRO(x) {\n"
1619                "  g();\n"
1620                "}");
1621 }
1622 
1623 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1624   FormatStyle Style = getLLVMStyle();
1625   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1626                           FormatStyle::BS_WebKit}) {
1627     Style.BreakBeforeBraces = BraceStyle;
1628     verifyFormat("try {\n"
1629                  "  // something\n"
1630                  "} catch (...) {\n"
1631                  "  // something\n"
1632                  "}",
1633                  Style);
1634   }
1635   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1636   verifyFormat("try {\n"
1637                "  // something\n"
1638                "}\n"
1639                "catch (...) {\n"
1640                "  // something\n"
1641                "}",
1642                Style);
1643   verifyFormat("__try {\n"
1644                "  // something\n"
1645                "}\n"
1646                "__finally {\n"
1647                "  // something\n"
1648                "}",
1649                Style);
1650   verifyFormat("@try {\n"
1651                "  // something\n"
1652                "}\n"
1653                "@finally {\n"
1654                "  // something\n"
1655                "}",
1656                Style);
1657   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1658   verifyFormat("try\n"
1659                "{\n"
1660                "  // something\n"
1661                "}\n"
1662                "catch (...)\n"
1663                "{\n"
1664                "  // something\n"
1665                "}",
1666                Style);
1667   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1668   verifyFormat("try\n"
1669                "  {\n"
1670                "    // something\n"
1671                "  }\n"
1672                "catch (...)\n"
1673                "  {\n"
1674                "    // something\n"
1675                "  }",
1676                Style);
1677   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1678   Style.BraceWrapping.BeforeCatch = true;
1679   verifyFormat("try {\n"
1680                "  // something\n"
1681                "}\n"
1682                "catch (...) {\n"
1683                "  // something\n"
1684                "}",
1685                Style);
1686 }
1687 
1688 TEST_F(FormatTest, StaticInitializers) {
1689   verifyFormat("static SomeClass SC = {1, 'a'};");
1690 
1691   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1692                "    100000000, "
1693                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1694 
1695   // Here, everything other than the "}" would fit on a line.
1696   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1697                "    10000000000000000000000000};");
1698   EXPECT_EQ("S s = {a,\n"
1699             "\n"
1700             "       b};",
1701             format("S s = {\n"
1702                    "  a,\n"
1703                    "\n"
1704                    "  b\n"
1705                    "};"));
1706 
1707   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1708   // line. However, the formatting looks a bit off and this probably doesn't
1709   // happen often in practice.
1710   verifyFormat("static int Variable[1] = {\n"
1711                "    {1000000000000000000000000000000000000}};",
1712                getLLVMStyleWithColumns(40));
1713 }
1714 
1715 TEST_F(FormatTest, DesignatedInitializers) {
1716   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1717   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1718                "                    .bbbbbbbbbb = 2,\n"
1719                "                    .cccccccccc = 3,\n"
1720                "                    .dddddddddd = 4,\n"
1721                "                    .eeeeeeeeee = 5};");
1722   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1723                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1724                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1725                "    .ccccccccccccccccccccccccccc = 3,\n"
1726                "    .ddddddddddddddddddddddddddd = 4,\n"
1727                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1728 
1729   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1730 
1731   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
1732   verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n"
1733                "                    [2] = bbbbbbbbbb,\n"
1734                "                    [3] = cccccccccc,\n"
1735                "                    [4] = dddddddddd,\n"
1736                "                    [5] = eeeeeeeeee};");
1737   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1738                "    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1739                "    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
1740                "    [3] = cccccccccccccccccccccccccccccccccccccc,\n"
1741                "    [4] = dddddddddddddddddddddddddddddddddddddd,\n"
1742                "    [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};");
1743 }
1744 
1745 TEST_F(FormatTest, NestedStaticInitializers) {
1746   verifyFormat("static A x = {{{}}};\n");
1747   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1748                "               {init1, init2, init3, init4}}};",
1749                getLLVMStyleWithColumns(50));
1750 
1751   verifyFormat("somes Status::global_reps[3] = {\n"
1752                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1753                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1754                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1755                getLLVMStyleWithColumns(60));
1756   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1757                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1758                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1759                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1760   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1761                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1762                "rect.fTop}};");
1763 
1764   verifyFormat(
1765       "SomeArrayOfSomeType a = {\n"
1766       "    {{1, 2, 3},\n"
1767       "     {1, 2, 3},\n"
1768       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1769       "      333333333333333333333333333333},\n"
1770       "     {1, 2, 3},\n"
1771       "     {1, 2, 3}}};");
1772   verifyFormat(
1773       "SomeArrayOfSomeType a = {\n"
1774       "    {{1, 2, 3}},\n"
1775       "    {{1, 2, 3}},\n"
1776       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1777       "      333333333333333333333333333333}},\n"
1778       "    {{1, 2, 3}},\n"
1779       "    {{1, 2, 3}}};");
1780 
1781   verifyFormat("struct {\n"
1782                "  unsigned bit;\n"
1783                "  const char *const name;\n"
1784                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1785                "                 {kOsWin, \"Windows\"},\n"
1786                "                 {kOsLinux, \"Linux\"},\n"
1787                "                 {kOsCrOS, \"Chrome OS\"}};");
1788   verifyFormat("struct {\n"
1789                "  unsigned bit;\n"
1790                "  const char *const name;\n"
1791                "} kBitsToOs[] = {\n"
1792                "    {kOsMac, \"Mac\"},\n"
1793                "    {kOsWin, \"Windows\"},\n"
1794                "    {kOsLinux, \"Linux\"},\n"
1795                "    {kOsCrOS, \"Chrome OS\"},\n"
1796                "};");
1797 }
1798 
1799 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1800   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1801                "                      \\\n"
1802                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1803 }
1804 
1805 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1806   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1807                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1808 
1809   // Do break defaulted and deleted functions.
1810   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1811                "    default;",
1812                getLLVMStyleWithColumns(40));
1813   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1814                "    delete;",
1815                getLLVMStyleWithColumns(40));
1816 }
1817 
1818 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1819   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1820                getLLVMStyleWithColumns(40));
1821   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1822                getLLVMStyleWithColumns(40));
1823   EXPECT_EQ("#define Q                              \\\n"
1824             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1825             "  \"aaaaaaaa.cpp\"",
1826             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1827                    getLLVMStyleWithColumns(40)));
1828 }
1829 
1830 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1831   EXPECT_EQ("# 123 \"A string literal\"",
1832             format("   #     123    \"A string literal\""));
1833 }
1834 
1835 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1836   EXPECT_EQ("#;", format("#;"));
1837   verifyFormat("#\n;\n;\n;");
1838 }
1839 
1840 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1841   EXPECT_EQ("#line 42 \"test\"\n",
1842             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1843   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1844                                     getLLVMStyleWithColumns(12)));
1845 }
1846 
1847 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1848   EXPECT_EQ("#line 42 \"test\"",
1849             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1850   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1851 }
1852 
1853 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1854   verifyFormat("#define A \\x20");
1855   verifyFormat("#define A \\ x20");
1856   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1857   verifyFormat("#define A ''");
1858   verifyFormat("#define A ''qqq");
1859   verifyFormat("#define A `qqq");
1860   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1861   EXPECT_EQ("const char *c = STRINGIFY(\n"
1862             "\\na : b);",
1863             format("const char * c = STRINGIFY(\n"
1864                    "\\na : b);"));
1865 
1866   verifyFormat("a\r\\");
1867   verifyFormat("a\v\\");
1868   verifyFormat("a\f\\");
1869 }
1870 
1871 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1872   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1873   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1874   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1875   // FIXME: We never break before the macro name.
1876   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1877 
1878   verifyFormat("#define A A\n#define A A");
1879   verifyFormat("#define A(X) A\n#define A A");
1880 
1881   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1882   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1883 }
1884 
1885 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1886   EXPECT_EQ("// somecomment\n"
1887             "#include \"a.h\"\n"
1888             "#define A(  \\\n"
1889             "    A, B)\n"
1890             "#include \"b.h\"\n"
1891             "// somecomment\n",
1892             format("  // somecomment\n"
1893                    "  #include \"a.h\"\n"
1894                    "#define A(A,\\\n"
1895                    "    B)\n"
1896                    "    #include \"b.h\"\n"
1897                    " // somecomment\n",
1898                    getLLVMStyleWithColumns(13)));
1899 }
1900 
1901 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1902 
1903 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1904   EXPECT_EQ("#define A    \\\n"
1905             "  c;         \\\n"
1906             "  e;\n"
1907             "f;",
1908             format("#define A c; e;\n"
1909                    "f;",
1910                    getLLVMStyleWithColumns(14)));
1911 }
1912 
1913 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1914 
1915 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1916   EXPECT_EQ("int x,\n"
1917             "#define A\n"
1918             "    y;",
1919             format("int x,\n#define A\ny;"));
1920 }
1921 
1922 TEST_F(FormatTest, HashInMacroDefinition) {
1923   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
1924   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1925   verifyFormat("#define A  \\\n"
1926                "  {        \\\n"
1927                "    f(#c); \\\n"
1928                "  }",
1929                getLLVMStyleWithColumns(11));
1930 
1931   verifyFormat("#define A(X)         \\\n"
1932                "  void function##X()",
1933                getLLVMStyleWithColumns(22));
1934 
1935   verifyFormat("#define A(a, b, c)   \\\n"
1936                "  void a##b##c()",
1937                getLLVMStyleWithColumns(22));
1938 
1939   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1940 }
1941 
1942 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1943   EXPECT_EQ("#define A (x)", format("#define A (x)"));
1944   EXPECT_EQ("#define A(x)", format("#define A(x)"));
1945 }
1946 
1947 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1948   EXPECT_EQ("#define A b;", format("#define A \\\n"
1949                                    "          \\\n"
1950                                    "  b;",
1951                                    getLLVMStyleWithColumns(25)));
1952   EXPECT_EQ("#define A \\\n"
1953             "          \\\n"
1954             "  a;      \\\n"
1955             "  b;",
1956             format("#define A \\\n"
1957                    "          \\\n"
1958                    "  a;      \\\n"
1959                    "  b;",
1960                    getLLVMStyleWithColumns(11)));
1961   EXPECT_EQ("#define A \\\n"
1962             "  a;      \\\n"
1963             "          \\\n"
1964             "  b;",
1965             format("#define A \\\n"
1966                    "  a;      \\\n"
1967                    "          \\\n"
1968                    "  b;",
1969                    getLLVMStyleWithColumns(11)));
1970 }
1971 
1972 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1973   verifyIncompleteFormat("#define A :");
1974   verifyFormat("#define SOMECASES  \\\n"
1975                "  case 1:          \\\n"
1976                "  case 2\n",
1977                getLLVMStyleWithColumns(20));
1978   verifyFormat("#define MACRO(a) \\\n"
1979                "  if (a)         \\\n"
1980                "    f();         \\\n"
1981                "  else           \\\n"
1982                "    g()",
1983                getLLVMStyleWithColumns(18));
1984   verifyFormat("#define A template <typename T>");
1985   verifyIncompleteFormat("#define STR(x) #x\n"
1986                          "f(STR(this_is_a_string_literal{));");
1987   verifyFormat("#pragma omp threadprivate( \\\n"
1988                "    y)), // expected-warning",
1989                getLLVMStyleWithColumns(28));
1990   verifyFormat("#d, = };");
1991   verifyFormat("#if \"a");
1992   verifyIncompleteFormat("({\n"
1993                          "#define b     \\\n"
1994                          "  }           \\\n"
1995                          "  a\n"
1996                          "a",
1997                          getLLVMStyleWithColumns(15));
1998   verifyFormat("#define A     \\\n"
1999                "  {           \\\n"
2000                "    {\n"
2001                "#define B     \\\n"
2002                "  }           \\\n"
2003                "  }",
2004                getLLVMStyleWithColumns(15));
2005   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2006   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2007   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2008   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2009 }
2010 
2011 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2012   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2013   EXPECT_EQ("class A : public QObject {\n"
2014             "  Q_OBJECT\n"
2015             "\n"
2016             "  A() {}\n"
2017             "};",
2018             format("class A  :  public QObject {\n"
2019                    "     Q_OBJECT\n"
2020                    "\n"
2021                    "  A() {\n}\n"
2022                    "}  ;"));
2023   EXPECT_EQ("MACRO\n"
2024             "/*static*/ int i;",
2025             format("MACRO\n"
2026                    " /*static*/ int   i;"));
2027   EXPECT_EQ("SOME_MACRO\n"
2028             "namespace {\n"
2029             "void f();\n"
2030             "} // namespace",
2031             format("SOME_MACRO\n"
2032                    "  namespace    {\n"
2033                    "void   f(  );\n"
2034                    "} // namespace"));
2035   // Only if the identifier contains at least 5 characters.
2036   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2037   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2038   // Only if everything is upper case.
2039   EXPECT_EQ("class A : public QObject {\n"
2040             "  Q_Object A() {}\n"
2041             "};",
2042             format("class A  :  public QObject {\n"
2043                    "     Q_Object\n"
2044                    "  A() {\n}\n"
2045                    "}  ;"));
2046 
2047   // Only if the next line can actually start an unwrapped line.
2048   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2049             format("SOME_WEIRD_LOG_MACRO\n"
2050                    "<< SomeThing;"));
2051 
2052   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2053                "(n, buffers))\n",
2054                getChromiumStyle(FormatStyle::LK_Cpp));
2055 }
2056 
2057 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2058   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2059             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2060             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2061             "class X {};\n"
2062             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2063             "int *createScopDetectionPass() { return 0; }",
2064             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2065                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2066                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2067                    "  class X {};\n"
2068                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2069                    "  int *createScopDetectionPass() { return 0; }"));
2070   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2071   // braces, so that inner block is indented one level more.
2072   EXPECT_EQ("int q() {\n"
2073             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2074             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2075             "  IPC_END_MESSAGE_MAP()\n"
2076             "}",
2077             format("int q() {\n"
2078                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2079                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2080                    "  IPC_END_MESSAGE_MAP()\n"
2081                    "}"));
2082 
2083   // Same inside macros.
2084   EXPECT_EQ("#define LIST(L) \\\n"
2085             "  L(A)          \\\n"
2086             "  L(B)          \\\n"
2087             "  L(C)",
2088             format("#define LIST(L) \\\n"
2089                    "  L(A) \\\n"
2090                    "  L(B) \\\n"
2091                    "  L(C)",
2092                    getGoogleStyle()));
2093 
2094   // These must not be recognized as macros.
2095   EXPECT_EQ("int q() {\n"
2096             "  f(x);\n"
2097             "  f(x) {}\n"
2098             "  f(x)->g();\n"
2099             "  f(x)->*g();\n"
2100             "  f(x).g();\n"
2101             "  f(x) = x;\n"
2102             "  f(x) += x;\n"
2103             "  f(x) -= x;\n"
2104             "  f(x) *= x;\n"
2105             "  f(x) /= x;\n"
2106             "  f(x) %= x;\n"
2107             "  f(x) &= x;\n"
2108             "  f(x) |= x;\n"
2109             "  f(x) ^= x;\n"
2110             "  f(x) >>= x;\n"
2111             "  f(x) <<= x;\n"
2112             "  f(x)[y].z();\n"
2113             "  LOG(INFO) << x;\n"
2114             "  ifstream(x) >> x;\n"
2115             "}\n",
2116             format("int q() {\n"
2117                    "  f(x)\n;\n"
2118                    "  f(x)\n {}\n"
2119                    "  f(x)\n->g();\n"
2120                    "  f(x)\n->*g();\n"
2121                    "  f(x)\n.g();\n"
2122                    "  f(x)\n = x;\n"
2123                    "  f(x)\n += x;\n"
2124                    "  f(x)\n -= x;\n"
2125                    "  f(x)\n *= x;\n"
2126                    "  f(x)\n /= x;\n"
2127                    "  f(x)\n %= x;\n"
2128                    "  f(x)\n &= x;\n"
2129                    "  f(x)\n |= x;\n"
2130                    "  f(x)\n ^= x;\n"
2131                    "  f(x)\n >>= x;\n"
2132                    "  f(x)\n <<= x;\n"
2133                    "  f(x)\n[y].z();\n"
2134                    "  LOG(INFO)\n << x;\n"
2135                    "  ifstream(x)\n >> x;\n"
2136                    "}\n"));
2137   EXPECT_EQ("int q() {\n"
2138             "  F(x)\n"
2139             "  if (1) {\n"
2140             "  }\n"
2141             "  F(x)\n"
2142             "  while (1) {\n"
2143             "  }\n"
2144             "  F(x)\n"
2145             "  G(x);\n"
2146             "  F(x)\n"
2147             "  try {\n"
2148             "    Q();\n"
2149             "  } catch (...) {\n"
2150             "  }\n"
2151             "}\n",
2152             format("int q() {\n"
2153                    "F(x)\n"
2154                    "if (1) {}\n"
2155                    "F(x)\n"
2156                    "while (1) {}\n"
2157                    "F(x)\n"
2158                    "G(x);\n"
2159                    "F(x)\n"
2160                    "try { Q(); } catch (...) {}\n"
2161                    "}\n"));
2162   EXPECT_EQ("class A {\n"
2163             "  A() : t(0) {}\n"
2164             "  A(int i) noexcept() : {}\n"
2165             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2166             "  try : t(0) {\n"
2167             "  } catch (...) {\n"
2168             "  }\n"
2169             "};",
2170             format("class A {\n"
2171                    "  A()\n : t(0) {}\n"
2172                    "  A(int i)\n noexcept() : {}\n"
2173                    "  A(X x)\n"
2174                    "  try : t(0) {} catch (...) {}\n"
2175                    "};"));
2176   EXPECT_EQ("class SomeClass {\n"
2177             "public:\n"
2178             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2179             "};",
2180             format("class SomeClass {\n"
2181                    "public:\n"
2182                    "  SomeClass()\n"
2183                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2184                    "};"));
2185   EXPECT_EQ("class SomeClass {\n"
2186             "public:\n"
2187             "  SomeClass()\n"
2188             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2189             "};",
2190             format("class SomeClass {\n"
2191                    "public:\n"
2192                    "  SomeClass()\n"
2193                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2194                    "};",
2195                    getLLVMStyleWithColumns(40)));
2196 
2197   verifyFormat("MACRO(>)");
2198 }
2199 
2200 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2201   verifyFormat("#define A \\\n"
2202                "  f({     \\\n"
2203                "    g();  \\\n"
2204                "  });",
2205                getLLVMStyleWithColumns(11));
2206 }
2207 
2208 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2209   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2210 }
2211 
2212 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2213   verifyFormat("{\n  { a #c; }\n}");
2214 }
2215 
2216 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2217   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2218             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2219   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2220             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2221 }
2222 
2223 TEST_F(FormatTest, EscapedNewlines) {
2224   EXPECT_EQ(
2225       "#define A \\\n  int i;  \\\n  int j;",
2226       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2227   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2228   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2229   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2230   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2231 }
2232 
2233 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2234   verifyFormat("#define A \\\n"
2235                "  int v(  \\\n"
2236                "      a); \\\n"
2237                "  int i;",
2238                getLLVMStyleWithColumns(11));
2239 }
2240 
2241 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2242   EXPECT_EQ(
2243       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2244       "                      \\\n"
2245       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2246       "\n"
2247       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2248       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2249       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2250              "\\\n"
2251              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2252              "  \n"
2253              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2254              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2255 }
2256 
2257 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2258   EXPECT_EQ("int\n"
2259             "#define A\n"
2260             "    a;",
2261             format("int\n#define A\na;"));
2262   verifyFormat("functionCallTo(\n"
2263                "    someOtherFunction(\n"
2264                "        withSomeParameters, whichInSequence,\n"
2265                "        areLongerThanALine(andAnotherCall,\n"
2266                "#define A B\n"
2267                "                           withMoreParamters,\n"
2268                "                           whichStronglyInfluenceTheLayout),\n"
2269                "        andMoreParameters),\n"
2270                "    trailing);",
2271                getLLVMStyleWithColumns(69));
2272   verifyFormat("Foo::Foo()\n"
2273                "#ifdef BAR\n"
2274                "    : baz(0)\n"
2275                "#endif\n"
2276                "{\n"
2277                "}");
2278   verifyFormat("void f() {\n"
2279                "  if (true)\n"
2280                "#ifdef A\n"
2281                "    f(42);\n"
2282                "  x();\n"
2283                "#else\n"
2284                "    g();\n"
2285                "  x();\n"
2286                "#endif\n"
2287                "}");
2288   verifyFormat("void f(param1, param2,\n"
2289                "       param3,\n"
2290                "#ifdef A\n"
2291                "       param4(param5,\n"
2292                "#ifdef A1\n"
2293                "              param6,\n"
2294                "#ifdef A2\n"
2295                "              param7),\n"
2296                "#else\n"
2297                "              param8),\n"
2298                "       param9,\n"
2299                "#endif\n"
2300                "       param10,\n"
2301                "#endif\n"
2302                "       param11)\n"
2303                "#else\n"
2304                "       param12)\n"
2305                "#endif\n"
2306                "{\n"
2307                "  x();\n"
2308                "}",
2309                getLLVMStyleWithColumns(28));
2310   verifyFormat("#if 1\n"
2311                "int i;");
2312   verifyFormat("#if 1\n"
2313                "#endif\n"
2314                "#if 1\n"
2315                "#else\n"
2316                "#endif\n");
2317   verifyFormat("DEBUG({\n"
2318                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2319                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2320                "});\n"
2321                "#if a\n"
2322                "#else\n"
2323                "#endif");
2324 
2325   verifyIncompleteFormat("void f(\n"
2326                          "#if A\n"
2327                          ");\n"
2328                          "#else\n"
2329                          "#endif");
2330 }
2331 
2332 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2333   verifyFormat("#endif\n"
2334                "#if B");
2335 }
2336 
2337 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2338   FormatStyle SingleLine = getLLVMStyle();
2339   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2340   verifyFormat("#if 0\n"
2341                "#elif 1\n"
2342                "#endif\n"
2343                "void foo() {\n"
2344                "  if (test) foo2();\n"
2345                "}",
2346                SingleLine);
2347 }
2348 
2349 TEST_F(FormatTest, LayoutBlockInsideParens) {
2350   verifyFormat("functionCall({ int i; });");
2351   verifyFormat("functionCall({\n"
2352                "  int i;\n"
2353                "  int j;\n"
2354                "});");
2355   verifyFormat("functionCall(\n"
2356                "    {\n"
2357                "      int i;\n"
2358                "      int j;\n"
2359                "    },\n"
2360                "    aaaa, bbbb, cccc);");
2361   verifyFormat("functionA(functionB({\n"
2362                "            int i;\n"
2363                "            int j;\n"
2364                "          }),\n"
2365                "          aaaa, bbbb, cccc);");
2366   verifyFormat("functionCall(\n"
2367                "    {\n"
2368                "      int i;\n"
2369                "      int j;\n"
2370                "    },\n"
2371                "    aaaa, bbbb, // comment\n"
2372                "    cccc);");
2373   verifyFormat("functionA(functionB({\n"
2374                "            int i;\n"
2375                "            int j;\n"
2376                "          }),\n"
2377                "          aaaa, bbbb, // comment\n"
2378                "          cccc);");
2379   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2380   verifyFormat("functionCall(aaaa, bbbb, {\n"
2381                "  int i;\n"
2382                "  int j;\n"
2383                "});");
2384   verifyFormat(
2385       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2386       "    {\n"
2387       "      int i; // break\n"
2388       "    },\n"
2389       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2390       "                                     ccccccccccccccccc));");
2391   verifyFormat("DEBUG({\n"
2392                "  if (a)\n"
2393                "    f();\n"
2394                "});");
2395 }
2396 
2397 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2398   EXPECT_EQ("SOME_MACRO { int i; }\n"
2399             "int i;",
2400             format("  SOME_MACRO  {int i;}  int i;"));
2401 }
2402 
2403 TEST_F(FormatTest, LayoutNestedBlocks) {
2404   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2405                "  struct s {\n"
2406                "    int i;\n"
2407                "  };\n"
2408                "  s kBitsToOs[] = {{10}};\n"
2409                "  for (int i = 0; i < 10; ++i)\n"
2410                "    return;\n"
2411                "}");
2412   verifyFormat("call(parameter, {\n"
2413                "  something();\n"
2414                "  // Comment using all columns.\n"
2415                "  somethingelse();\n"
2416                "});",
2417                getLLVMStyleWithColumns(40));
2418   verifyFormat("DEBUG( //\n"
2419                "    { f(); }, a);");
2420   verifyFormat("DEBUG( //\n"
2421                "    {\n"
2422                "      f(); //\n"
2423                "    },\n"
2424                "    a);");
2425 
2426   EXPECT_EQ("call(parameter, {\n"
2427             "  something();\n"
2428             "  // Comment too\n"
2429             "  // looooooooooong.\n"
2430             "  somethingElse();\n"
2431             "});",
2432             format("call(parameter, {\n"
2433                    "  something();\n"
2434                    "  // Comment too looooooooooong.\n"
2435                    "  somethingElse();\n"
2436                    "});",
2437                    getLLVMStyleWithColumns(29)));
2438   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2439   EXPECT_EQ("DEBUG({ // comment\n"
2440             "  int i;\n"
2441             "});",
2442             format("DEBUG({ // comment\n"
2443                    "int  i;\n"
2444                    "});"));
2445   EXPECT_EQ("DEBUG({\n"
2446             "  int i;\n"
2447             "\n"
2448             "  // comment\n"
2449             "  int j;\n"
2450             "});",
2451             format("DEBUG({\n"
2452                    "  int  i;\n"
2453                    "\n"
2454                    "  // comment\n"
2455                    "  int  j;\n"
2456                    "});"));
2457 
2458   verifyFormat("DEBUG({\n"
2459                "  if (a)\n"
2460                "    return;\n"
2461                "});");
2462   verifyGoogleFormat("DEBUG({\n"
2463                      "  if (a) return;\n"
2464                      "});");
2465   FormatStyle Style = getGoogleStyle();
2466   Style.ColumnLimit = 45;
2467   verifyFormat("Debug(aaaaa,\n"
2468                "      {\n"
2469                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2470                "      },\n"
2471                "      a);",
2472                Style);
2473 
2474   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2475 
2476   verifyNoCrash("^{v^{a}}");
2477 }
2478 
2479 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2480   EXPECT_EQ("#define MACRO()                     \\\n"
2481             "  Debug(aaa, /* force line break */ \\\n"
2482             "        {                           \\\n"
2483             "          int i;                    \\\n"
2484             "          int j;                    \\\n"
2485             "        })",
2486             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2487                    "          {  int   i;  int  j;   })",
2488                    getGoogleStyle()));
2489 
2490   EXPECT_EQ("#define A                                       \\\n"
2491             "  [] {                                          \\\n"
2492             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2493             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2494             "  }",
2495             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2496                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2497                    getGoogleStyle()));
2498 }
2499 
2500 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2501   EXPECT_EQ("{}", format("{}"));
2502   verifyFormat("enum E {};");
2503   verifyFormat("enum E {}");
2504 }
2505 
2506 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2507   FormatStyle Style = getLLVMStyle();
2508   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2509   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2510   verifyFormat("FOO_BEGIN\n"
2511                "  FOO_ENTRY\n"
2512                "FOO_END", Style);
2513   verifyFormat("FOO_BEGIN\n"
2514                "  NESTED_FOO_BEGIN\n"
2515                "    NESTED_FOO_ENTRY\n"
2516                "  NESTED_FOO_END\n"
2517                "FOO_END", Style);
2518   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2519                "  int x;\n"
2520                "  x = 1;\n"
2521                "FOO_END(Baz)", Style);
2522 }
2523 
2524 //===----------------------------------------------------------------------===//
2525 // Line break tests.
2526 //===----------------------------------------------------------------------===//
2527 
2528 TEST_F(FormatTest, PreventConfusingIndents) {
2529   verifyFormat(
2530       "void f() {\n"
2531       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2532       "                         parameter, parameter, parameter)),\n"
2533       "                     SecondLongCall(parameter));\n"
2534       "}");
2535   verifyFormat(
2536       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2537       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2538       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2539       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2540   verifyFormat(
2541       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2542       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2543       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2544       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2545   verifyFormat(
2546       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2547       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2548       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2549       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2550   verifyFormat("int a = bbbb && ccc &&\n"
2551                "        fffff(\n"
2552                "#define A Just forcing a new line\n"
2553                "            ddd);");
2554 }
2555 
2556 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2557   verifyFormat(
2558       "bool aaaaaaa =\n"
2559       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2560       "    bbbbbbbb();");
2561   verifyFormat(
2562       "bool aaaaaaa =\n"
2563       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2564       "    bbbbbbbb();");
2565 
2566   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2567                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2568                "    ccccccccc == ddddddddddd;");
2569   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2570                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2571                "    ccccccccc == ddddddddddd;");
2572   verifyFormat(
2573       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2574       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2575       "    ccccccccc == ddddddddddd;");
2576 
2577   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2578                "                 aaaaaa) &&\n"
2579                "         bbbbbb && cccccc;");
2580   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2581                "                 aaaaaa) >>\n"
2582                "         bbbbbb;");
2583   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2584                "    SourceMgr.getSpellingColumnNumber(\n"
2585                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2586                "    1);");
2587 
2588   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2589                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2590                "    cccccc) {\n}");
2591   verifyFormat("if constexpr ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2592                "               bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaa\n"
2593                "              cccccc) {\n}");
2594   verifyFormat("b = a &&\n"
2595                "    // Comment\n"
2596                "    b.c && d;");
2597 
2598   // If the LHS of a comparison is not a binary expression itself, the
2599   // additional linebreak confuses many people.
2600   verifyFormat(
2601       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2602       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2603       "}");
2604   verifyFormat(
2605       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2606       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2607       "}");
2608   verifyFormat(
2609       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2610       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2611       "}");
2612   // Even explicit parentheses stress the precedence enough to make the
2613   // additional break unnecessary.
2614   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2615                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2616                "}");
2617   // This cases is borderline, but with the indentation it is still readable.
2618   verifyFormat(
2619       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2620       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2621       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2622       "}",
2623       getLLVMStyleWithColumns(75));
2624 
2625   // If the LHS is a binary expression, we should still use the additional break
2626   // as otherwise the formatting hides the operator precedence.
2627   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2628                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2629                "    5) {\n"
2630                "}");
2631 
2632   FormatStyle OnePerLine = getLLVMStyle();
2633   OnePerLine.BinPackParameters = false;
2634   verifyFormat(
2635       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2636       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2637       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2638       OnePerLine);
2639 
2640   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2641                "                .aaa(aaaaaaaaaaaaa) *\n"
2642                "            aaaaaaa +\n"
2643                "        aaaaaaa;",
2644                getLLVMStyleWithColumns(40));
2645 }
2646 
2647 TEST_F(FormatTest, ExpressionIndentation) {
2648   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2649                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2650                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2651                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2652                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2653                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2654                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2655                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2656                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2657   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2658                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2659                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2660                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2661   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2662                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2663                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2664                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2665   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2666                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2667                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2668                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2669   verifyFormat("if () {\n"
2670                "} else if (aaaaa && bbbbb > // break\n"
2671                "                        ccccc) {\n"
2672                "}");
2673   verifyFormat("if () {\n"
2674                "} else if (aaaaa &&\n"
2675                "           bbbbb > // break\n"
2676                "               ccccc &&\n"
2677                "           ddddd) {\n"
2678                "}");
2679 
2680   // Presence of a trailing comment used to change indentation of b.
2681   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2682                "       b;\n"
2683                "return aaaaaaaaaaaaaaaaaaa +\n"
2684                "       b; //",
2685                getLLVMStyleWithColumns(30));
2686 }
2687 
2688 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2689   // Not sure what the best system is here. Like this, the LHS can be found
2690   // immediately above an operator (everything with the same or a higher
2691   // indent). The RHS is aligned right of the operator and so compasses
2692   // everything until something with the same indent as the operator is found.
2693   // FIXME: Is this a good system?
2694   FormatStyle Style = getLLVMStyle();
2695   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
2696   verifyFormat(
2697       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2698       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2699       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2700       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2701       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2702       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2703       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2704       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2705       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
2706       Style);
2707   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2708                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2709                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2710                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2711                Style);
2712   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2713                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2714                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2715                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2716                Style);
2717   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2718                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2719                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2720                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2721                Style);
2722   verifyFormat("if () {\n"
2723                "} else if (aaaaa\n"
2724                "           && bbbbb // break\n"
2725                "                  > ccccc) {\n"
2726                "}",
2727                Style);
2728   verifyFormat("return (a)\n"
2729                "       // comment\n"
2730                "       + b;",
2731                Style);
2732   verifyFormat(
2733       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2734       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2735       "             + cc;",
2736       Style);
2737 
2738   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2739                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
2740                Style);
2741 
2742   // Forced by comments.
2743   verifyFormat(
2744       "unsigned ContentSize =\n"
2745       "    sizeof(int16_t)   // DWARF ARange version number\n"
2746       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2747       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2748       "    + sizeof(int8_t); // Segment Size (in bytes)");
2749 
2750   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2751                "       == boost::fusion::at_c<1>(iiii).second;",
2752                Style);
2753 
2754   Style.ColumnLimit = 60;
2755   verifyFormat("zzzzzzzzzz\n"
2756                "    = bbbbbbbbbbbbbbbbb\n"
2757                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
2758                Style);
2759 }
2760 
2761 TEST_F(FormatTest, EnforcedOperatorWraps) {
2762   // Here we'd like to wrap after the || operators, but a comment is forcing an
2763   // earlier wrap.
2764   verifyFormat("bool x = aaaaa //\n"
2765                "         || bbbbb\n"
2766                "         //\n"
2767                "         || cccc;");
2768 }
2769 
2770 TEST_F(FormatTest, NoOperandAlignment) {
2771   FormatStyle Style = getLLVMStyle();
2772   Style.AlignOperands = false;
2773   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
2774                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2775                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2776                Style);
2777   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2778   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2779                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2780                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2781                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2782                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2783                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2784                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2785                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2786                "        > ccccccccccccccccccccccccccccccccccccccccc;",
2787                Style);
2788 
2789   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2790                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2791                "    + cc;",
2792                Style);
2793   verifyFormat("int a = aa\n"
2794                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2795                "        * cccccccccccccccccccccccccccccccccccc;\n",
2796                Style);
2797 
2798   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
2799   verifyFormat("return (a > b\n"
2800                "    // comment1\n"
2801                "    // comment2\n"
2802                "    || c);",
2803                Style);
2804 }
2805 
2806 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
2807   FormatStyle Style = getLLVMStyle();
2808   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2809   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2810                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2811                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2812                Style);
2813 }
2814 
2815 TEST_F(FormatTest, AllowBinPackingInsideArguments) {
2816   FormatStyle Style = getLLVMStyle();
2817   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2818   Style.BinPackArguments = false;
2819   Style.ColumnLimit = 40;
2820   verifyFormat("void test() {\n"
2821                "  someFunction(\n"
2822                "      this + argument + is + quite\n"
2823                "      + long + so + it + gets + wrapped\n"
2824                "      + but + remains + bin - packed);\n"
2825                "}",
2826                Style);
2827   verifyFormat("void test() {\n"
2828                "  someFunction(arg1,\n"
2829                "               this + argument + is\n"
2830                "                   + quite + long + so\n"
2831                "                   + it + gets + wrapped\n"
2832                "                   + but + remains + bin\n"
2833                "                   - packed,\n"
2834                "               arg3);\n"
2835                "}",
2836                Style);
2837   verifyFormat("void test() {\n"
2838                "  someFunction(\n"
2839                "      arg1,\n"
2840                "      this + argument + has\n"
2841                "          + anotherFunc(nested,\n"
2842                "                        calls + whose\n"
2843                "                            + arguments\n"
2844                "                            + are + also\n"
2845                "                            + wrapped,\n"
2846                "                        in + addition)\n"
2847                "          + to + being + bin - packed,\n"
2848                "      arg3);\n"
2849                "}",
2850                Style);
2851 
2852   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
2853   verifyFormat("void test() {\n"
2854                "  someFunction(\n"
2855                "      arg1,\n"
2856                "      this + argument + has +\n"
2857                "          anotherFunc(nested,\n"
2858                "                      calls + whose +\n"
2859                "                          arguments +\n"
2860                "                          are + also +\n"
2861                "                          wrapped,\n"
2862                "                      in + addition) +\n"
2863                "          to + being + bin - packed,\n"
2864                "      arg3);\n"
2865                "}",
2866                Style);
2867 }
2868 
2869 TEST_F(FormatTest, ConstructorInitializers) {
2870   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2871   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2872                getLLVMStyleWithColumns(45));
2873   verifyFormat("Constructor()\n"
2874                "    : Inttializer(FitsOnTheLine) {}",
2875                getLLVMStyleWithColumns(44));
2876   verifyFormat("Constructor()\n"
2877                "    : Inttializer(FitsOnTheLine) {}",
2878                getLLVMStyleWithColumns(43));
2879 
2880   verifyFormat("template <typename T>\n"
2881                "Constructor() : Initializer(FitsOnTheLine) {}",
2882                getLLVMStyleWithColumns(45));
2883 
2884   verifyFormat(
2885       "SomeClass::Constructor()\n"
2886       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2887 
2888   verifyFormat(
2889       "SomeClass::Constructor()\n"
2890       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2891       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2892   verifyFormat(
2893       "SomeClass::Constructor()\n"
2894       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2895       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2896   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2897                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2898                "    : aaaaaaaaaa(aaaaaa) {}");
2899 
2900   verifyFormat("Constructor()\n"
2901                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2902                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2903                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2904                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2905 
2906   verifyFormat("Constructor()\n"
2907                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2908                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2909 
2910   verifyFormat("Constructor(int Parameter = 0)\n"
2911                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2912                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2913   verifyFormat("Constructor()\n"
2914                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2915                "}",
2916                getLLVMStyleWithColumns(60));
2917   verifyFormat("Constructor()\n"
2918                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2919                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
2920 
2921   // Here a line could be saved by splitting the second initializer onto two
2922   // lines, but that is not desirable.
2923   verifyFormat("Constructor()\n"
2924                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2925                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2926                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2927 
2928   FormatStyle OnePerLine = getLLVMStyle();
2929   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2930   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2931   verifyFormat("SomeClass::Constructor()\n"
2932                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2933                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2934                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2935                OnePerLine);
2936   verifyFormat("SomeClass::Constructor()\n"
2937                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2938                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2939                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2940                OnePerLine);
2941   verifyFormat("MyClass::MyClass(int var)\n"
2942                "    : some_var_(var),            // 4 space indent\n"
2943                "      some_other_var_(var + 1) { // lined up\n"
2944                "}",
2945                OnePerLine);
2946   verifyFormat("Constructor()\n"
2947                "    : aaaaa(aaaaaa),\n"
2948                "      aaaaa(aaaaaa),\n"
2949                "      aaaaa(aaaaaa),\n"
2950                "      aaaaa(aaaaaa),\n"
2951                "      aaaaa(aaaaaa) {}",
2952                OnePerLine);
2953   verifyFormat("Constructor()\n"
2954                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2955                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2956                OnePerLine);
2957   OnePerLine.BinPackParameters = false;
2958   verifyFormat(
2959       "Constructor()\n"
2960       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2961       "          aaaaaaaaaaa().aaa(),\n"
2962       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2963       OnePerLine);
2964   OnePerLine.ColumnLimit = 60;
2965   verifyFormat("Constructor()\n"
2966                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
2967                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2968                OnePerLine);
2969 
2970   EXPECT_EQ("Constructor()\n"
2971             "    : // Comment forcing unwanted break.\n"
2972             "      aaaa(aaaa) {}",
2973             format("Constructor() :\n"
2974                    "    // Comment forcing unwanted break.\n"
2975                    "    aaaa(aaaa) {}"));
2976 }
2977 
2978 TEST_F(FormatTest, BreakConstructorInitializersAfterColon) {
2979   FormatStyle Style = getLLVMStyle();
2980   Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
2981 
2982   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2983   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}",
2984                getStyleWithColumns(Style, 45));
2985   verifyFormat("Constructor() :\n"
2986                "    Initializer(FitsOnTheLine) {}",
2987                getStyleWithColumns(Style, 44));
2988   verifyFormat("Constructor() :\n"
2989                "    Initializer(FitsOnTheLine) {}",
2990                getStyleWithColumns(Style, 43));
2991 
2992   verifyFormat("template <typename T>\n"
2993                "Constructor() : Initializer(FitsOnTheLine) {}",
2994                getStyleWithColumns(Style, 50));
2995 
2996   verifyFormat(
2997       "SomeClass::Constructor() :\n"
2998       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
2999 	  Style);
3000 
3001   verifyFormat(
3002       "SomeClass::Constructor() :\n"
3003       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3004       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3005 	  Style);
3006   verifyFormat(
3007       "SomeClass::Constructor() :\n"
3008       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3009       "    aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}",
3010 	  Style);
3011   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3012                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3013                "    aaaaaaaaaa(aaaaaa) {}",
3014 			   Style);
3015 
3016   verifyFormat("Constructor() :\n"
3017                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3018                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3019                "                             aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3020                "    aaaaaaaaaaaaaaaaaaaaaaa() {}",
3021 			   Style);
3022 
3023   verifyFormat("Constructor() :\n"
3024                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3025                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3026 			   Style);
3027 
3028   verifyFormat("Constructor(int Parameter = 0) :\n"
3029                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3030                "    aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}",
3031 			   Style);
3032   verifyFormat("Constructor() :\n"
3033                "    aaaaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3034                "}",
3035                getStyleWithColumns(Style, 60));
3036   verifyFormat("Constructor() :\n"
3037                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3038                "        aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}",
3039 			   Style);
3040 
3041   // Here a line could be saved by splitting the second initializer onto two
3042   // lines, but that is not desirable.
3043   verifyFormat("Constructor() :\n"
3044                "    aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3045                "    aaaaaaaaaaa(aaaaaaaaaaa),\n"
3046                "    aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3047 			   Style);
3048 
3049   FormatStyle OnePerLine = Style;
3050   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3051   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
3052   verifyFormat("SomeClass::Constructor() :\n"
3053                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3054                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3055                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3056                OnePerLine);
3057   verifyFormat("SomeClass::Constructor() :\n"
3058                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3059                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3060                "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3061                OnePerLine);
3062   verifyFormat("MyClass::MyClass(int var) :\n"
3063                "    some_var_(var),            // 4 space indent\n"
3064                "    some_other_var_(var + 1) { // lined up\n"
3065                "}",
3066                OnePerLine);
3067   verifyFormat("Constructor() :\n"
3068                "    aaaaa(aaaaaa),\n"
3069                "    aaaaa(aaaaaa),\n"
3070                "    aaaaa(aaaaaa),\n"
3071                "    aaaaa(aaaaaa),\n"
3072                "    aaaaa(aaaaaa) {}",
3073                OnePerLine);
3074   verifyFormat("Constructor() :\n"
3075                "    aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3076                "          aaaaaaaaaaaaaaaaaaaaaa) {}",
3077                OnePerLine);
3078   OnePerLine.BinPackParameters = false;
3079   verifyFormat(
3080       "Constructor() :\n"
3081       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3082       "        aaaaaaaaaaa().aaa(),\n"
3083       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3084       OnePerLine);
3085   OnePerLine.ColumnLimit = 60;
3086   verifyFormat("Constructor() :\n"
3087                "    aaaaaaaaaaaaaaaaaaaa(a),\n"
3088                "    bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
3089                OnePerLine);
3090 
3091   EXPECT_EQ("Constructor() :\n"
3092             "    // Comment forcing unwanted break.\n"
3093             "    aaaa(aaaa) {}",
3094             format("Constructor() :\n"
3095                    "    // Comment forcing unwanted break.\n"
3096                    "    aaaa(aaaa) {}",
3097 				   Style));
3098 
3099   Style.ColumnLimit = 0;
3100   verifyFormat("SomeClass::Constructor() :\n"
3101                "    a(a) {}",
3102                Style);
3103   verifyFormat("SomeClass::Constructor() noexcept :\n"
3104                "    a(a) {}",
3105                Style);
3106   verifyFormat("SomeClass::Constructor() :\n"
3107 			   "    a(a), b(b), c(c) {}",
3108                Style);
3109   verifyFormat("SomeClass::Constructor() :\n"
3110                "    a(a) {\n"
3111                "  foo();\n"
3112                "  bar();\n"
3113                "}",
3114                Style);
3115 
3116   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
3117   verifyFormat("SomeClass::Constructor() :\n"
3118 			   "    a(a), b(b), c(c) {\n"
3119 			   "}",
3120                Style);
3121   verifyFormat("SomeClass::Constructor() :\n"
3122                "    a(a) {\n"
3123 			   "}",
3124                Style);
3125 
3126   Style.ColumnLimit = 80;
3127   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
3128   Style.ConstructorInitializerIndentWidth = 2;
3129   verifyFormat("SomeClass::Constructor() : a(a), b(b), c(c) {}",
3130                Style);
3131   verifyFormat("SomeClass::Constructor() :\n"
3132                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3133                "  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb {}",
3134                Style);
3135 }
3136 
3137 TEST_F(FormatTest, MemoizationTests) {
3138   // This breaks if the memoization lookup does not take \c Indent and
3139   // \c LastSpace into account.
3140   verifyFormat(
3141       "extern CFRunLoopTimerRef\n"
3142       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3143       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3144       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3145       "                     CFRunLoopTimerContext *context) {}");
3146 
3147   // Deep nesting somewhat works around our memoization.
3148   verifyFormat(
3149       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3150       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3151       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3152       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3153       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3154       getLLVMStyleWithColumns(65));
3155   verifyFormat(
3156       "aaaaa(\n"
3157       "    aaaaa,\n"
3158       "    aaaaa(\n"
3159       "        aaaaa,\n"
3160       "        aaaaa(\n"
3161       "            aaaaa,\n"
3162       "            aaaaa(\n"
3163       "                aaaaa,\n"
3164       "                aaaaa(\n"
3165       "                    aaaaa,\n"
3166       "                    aaaaa(\n"
3167       "                        aaaaa,\n"
3168       "                        aaaaa(\n"
3169       "                            aaaaa,\n"
3170       "                            aaaaa(\n"
3171       "                                aaaaa,\n"
3172       "                                aaaaa(\n"
3173       "                                    aaaaa,\n"
3174       "                                    aaaaa(\n"
3175       "                                        aaaaa,\n"
3176       "                                        aaaaa(\n"
3177       "                                            aaaaa,\n"
3178       "                                            aaaaa(\n"
3179       "                                                aaaaa,\n"
3180       "                                                aaaaa))))))))))));",
3181       getLLVMStyleWithColumns(65));
3182   verifyFormat(
3183       "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"
3184       "                                  a),\n"
3185       "                                a),\n"
3186       "                              a),\n"
3187       "                            a),\n"
3188       "                          a),\n"
3189       "                        a),\n"
3190       "                      a),\n"
3191       "                    a),\n"
3192       "                  a),\n"
3193       "                a),\n"
3194       "              a),\n"
3195       "            a),\n"
3196       "          a),\n"
3197       "        a),\n"
3198       "      a),\n"
3199       "    a),\n"
3200       "  a)",
3201       getLLVMStyleWithColumns(65));
3202 
3203   // This test takes VERY long when memoization is broken.
3204   FormatStyle OnePerLine = getLLVMStyle();
3205   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3206   OnePerLine.BinPackParameters = false;
3207   std::string input = "Constructor()\n"
3208                       "    : aaaa(a,\n";
3209   for (unsigned i = 0, e = 80; i != e; ++i) {
3210     input += "           a,\n";
3211   }
3212   input += "           a) {}";
3213   verifyFormat(input, OnePerLine);
3214 }
3215 
3216 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3217   verifyFormat(
3218       "void f() {\n"
3219       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3220       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3221       "    f();\n"
3222       "}");
3223   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3224                "    Intervals[i - 1].getRange().getLast()) {\n}");
3225 }
3226 
3227 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3228   // Principially, we break function declarations in a certain order:
3229   // 1) break amongst arguments.
3230   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3231                "                              Cccccccccccccc cccccccccccccc);");
3232   verifyFormat("template <class TemplateIt>\n"
3233                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3234                "                            TemplateIt *stop) {}");
3235 
3236   // 2) break after return type.
3237   verifyFormat(
3238       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3239       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3240       getGoogleStyle());
3241 
3242   // 3) break after (.
3243   verifyFormat(
3244       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3245       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3246       getGoogleStyle());
3247 
3248   // 4) break before after nested name specifiers.
3249   verifyFormat(
3250       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3251       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3252       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3253       getGoogleStyle());
3254 
3255   // However, there are exceptions, if a sufficient amount of lines can be
3256   // saved.
3257   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3258   // more adjusting.
3259   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3260                "                                  Cccccccccccccc cccccccccc,\n"
3261                "                                  Cccccccccccccc cccccccccc,\n"
3262                "                                  Cccccccccccccc cccccccccc,\n"
3263                "                                  Cccccccccccccc cccccccccc);");
3264   verifyFormat(
3265       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3266       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3267       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3268       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3269       getGoogleStyle());
3270   verifyFormat(
3271       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3272       "                                          Cccccccccccccc cccccccccc,\n"
3273       "                                          Cccccccccccccc cccccccccc,\n"
3274       "                                          Cccccccccccccc cccccccccc,\n"
3275       "                                          Cccccccccccccc cccccccccc,\n"
3276       "                                          Cccccccccccccc cccccccccc,\n"
3277       "                                          Cccccccccccccc cccccccccc);");
3278   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3279                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3280                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3281                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3282                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3283 
3284   // Break after multi-line parameters.
3285   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3286                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3287                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3288                "    bbbb bbbb);");
3289   verifyFormat("void SomeLoooooooooooongFunction(\n"
3290                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3291                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3292                "    int bbbbbbbbbbbbb);");
3293 
3294   // Treat overloaded operators like other functions.
3295   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3296                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3297   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3298                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3299   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3300                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3301   verifyGoogleFormat(
3302       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3303       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3304   verifyGoogleFormat(
3305       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3306       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3307   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3308                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3309   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3310                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3311   verifyGoogleFormat(
3312       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3313       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3314       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3315   verifyGoogleFormat(
3316       "template <typename T>\n"
3317       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3318       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
3319       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
3320 
3321   FormatStyle Style = getLLVMStyle();
3322   Style.PointerAlignment = FormatStyle::PAS_Left;
3323   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3324                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3325                Style);
3326   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3327                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3328                Style);
3329 }
3330 
3331 TEST_F(FormatTest, TrailingReturnType) {
3332   verifyFormat("auto foo() -> int;\n");
3333   verifyFormat("struct S {\n"
3334                "  auto bar() const -> int;\n"
3335                "};");
3336   verifyFormat("template <size_t Order, typename T>\n"
3337                "auto load_img(const std::string &filename)\n"
3338                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3339   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3340                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3341   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3342   verifyFormat("template <typename T>\n"
3343                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
3344                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
3345 
3346   // Not trailing return types.
3347   verifyFormat("void f() { auto a = b->c(); }");
3348 }
3349 
3350 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3351   // Avoid breaking before trailing 'const' or other trailing annotations, if
3352   // they are not function-like.
3353   FormatStyle Style = getGoogleStyle();
3354   Style.ColumnLimit = 47;
3355   verifyFormat("void someLongFunction(\n"
3356                "    int someLoooooooooooooongParameter) const {\n}",
3357                getLLVMStyleWithColumns(47));
3358   verifyFormat("LoooooongReturnType\n"
3359                "someLoooooooongFunction() const {}",
3360                getLLVMStyleWithColumns(47));
3361   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3362                "    const {}",
3363                Style);
3364   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3365                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3366   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3367                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3368   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3369                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3370   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3371                "                   aaaaaaaaaaa aaaaa) const override;");
3372   verifyGoogleFormat(
3373       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3374       "    const override;");
3375 
3376   // Even if the first parameter has to be wrapped.
3377   verifyFormat("void someLongFunction(\n"
3378                "    int someLongParameter) const {}",
3379                getLLVMStyleWithColumns(46));
3380   verifyFormat("void someLongFunction(\n"
3381                "    int someLongParameter) const {}",
3382                Style);
3383   verifyFormat("void someLongFunction(\n"
3384                "    int someLongParameter) override {}",
3385                Style);
3386   verifyFormat("void someLongFunction(\n"
3387                "    int someLongParameter) OVERRIDE {}",
3388                Style);
3389   verifyFormat("void someLongFunction(\n"
3390                "    int someLongParameter) final {}",
3391                Style);
3392   verifyFormat("void someLongFunction(\n"
3393                "    int someLongParameter) FINAL {}",
3394                Style);
3395   verifyFormat("void someLongFunction(\n"
3396                "    int parameter) const override {}",
3397                Style);
3398 
3399   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3400   verifyFormat("void someLongFunction(\n"
3401                "    int someLongParameter) const\n"
3402                "{\n"
3403                "}",
3404                Style);
3405 
3406   // Unless these are unknown annotations.
3407   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3408                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3409                "    LONG_AND_UGLY_ANNOTATION;");
3410 
3411   // Breaking before function-like trailing annotations is fine to keep them
3412   // close to their arguments.
3413   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3414                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3415   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3416                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3417   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3418                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3419   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3420                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3421   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3422 
3423   verifyFormat(
3424       "void aaaaaaaaaaaaaaaaaa()\n"
3425       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3426       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3427   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3428                "    __attribute__((unused));");
3429   verifyGoogleFormat(
3430       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3431       "    GUARDED_BY(aaaaaaaaaaaa);");
3432   verifyGoogleFormat(
3433       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3434       "    GUARDED_BY(aaaaaaaaaaaa);");
3435   verifyGoogleFormat(
3436       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3437       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3438   verifyGoogleFormat(
3439       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3440       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3441 }
3442 
3443 TEST_F(FormatTest, FunctionAnnotations) {
3444   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3445                "int OldFunction(const string &parameter) {}");
3446   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3447                "string OldFunction(const string &parameter) {}");
3448   verifyFormat("template <typename T>\n"
3449                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3450                "string OldFunction(const string &parameter) {}");
3451 
3452   // Not function annotations.
3453   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3454                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3455   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3456                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3457   verifyFormat("MACRO(abc).function() // wrap\n"
3458                "    << abc;");
3459   verifyFormat("MACRO(abc)->function() // wrap\n"
3460                "    << abc;");
3461   verifyFormat("MACRO(abc)::function() // wrap\n"
3462                "    << abc;");
3463 }
3464 
3465 TEST_F(FormatTest, BreaksDesireably) {
3466   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3467                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3468                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3469   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3470                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3471                "}");
3472 
3473   verifyFormat(
3474       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3475       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3476 
3477   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3478                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3479                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3480 
3481   verifyFormat(
3482       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3483       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3484       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3485       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3486       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3487 
3488   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3489                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3490 
3491   verifyFormat(
3492       "void f() {\n"
3493       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3494       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3495       "}");
3496   verifyFormat(
3497       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3498       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3499   verifyFormat(
3500       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3501       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3502   verifyFormat(
3503       "aaaaaa(aaa,\n"
3504       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3505       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3506       "       aaaa);");
3507   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3508                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3509                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3510 
3511   // Indent consistently independent of call expression and unary operator.
3512   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3513                "    dddddddddddddddddddddddddddddd));");
3514   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3515                "    dddddddddddddddddddddddddddddd));");
3516   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3517                "    dddddddddddddddddddddddddddddd));");
3518 
3519   // This test case breaks on an incorrect memoization, i.e. an optimization not
3520   // taking into account the StopAt value.
3521   verifyFormat(
3522       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3523       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3524       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3525       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3526 
3527   verifyFormat("{\n  {\n    {\n"
3528                "      Annotation.SpaceRequiredBefore =\n"
3529                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3530                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3531                "    }\n  }\n}");
3532 
3533   // Break on an outer level if there was a break on an inner level.
3534   EXPECT_EQ("f(g(h(a, // comment\n"
3535             "      b, c),\n"
3536             "    d, e),\n"
3537             "  x, y);",
3538             format("f(g(h(a, // comment\n"
3539                    "    b, c), d, e), x, y);"));
3540 
3541   // Prefer breaking similar line breaks.
3542   verifyFormat(
3543       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3544       "                             NSTrackingMouseEnteredAndExited |\n"
3545       "                             NSTrackingActiveAlways;");
3546 }
3547 
3548 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3549   FormatStyle NoBinPacking = getGoogleStyle();
3550   NoBinPacking.BinPackParameters = false;
3551   NoBinPacking.BinPackArguments = true;
3552   verifyFormat("void f() {\n"
3553                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3554                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3555                "}",
3556                NoBinPacking);
3557   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3558                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3559                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3560                NoBinPacking);
3561 
3562   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3563   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3564                "                        vector<int> bbbbbbbbbbbbbbb);",
3565                NoBinPacking);
3566   // FIXME: This behavior difference is probably not wanted. However, currently
3567   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3568   // template arguments from BreakBeforeParameter being set because of the
3569   // one-per-line formatting.
3570   verifyFormat(
3571       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3572       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3573       NoBinPacking);
3574   verifyFormat(
3575       "void fffffffffff(\n"
3576       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3577       "        aaaaaaaaaa);");
3578 }
3579 
3580 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3581   FormatStyle NoBinPacking = getGoogleStyle();
3582   NoBinPacking.BinPackParameters = false;
3583   NoBinPacking.BinPackArguments = false;
3584   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3585                "  aaaaaaaaaaaaaaaaaaaa,\n"
3586                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3587                NoBinPacking);
3588   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3589                "        aaaaaaaaaaaaa,\n"
3590                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3591                NoBinPacking);
3592   verifyFormat(
3593       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3594       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3595       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3596       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3597       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3598       NoBinPacking);
3599   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3600                "    .aaaaaaaaaaaaaaaaaa();",
3601                NoBinPacking);
3602   verifyFormat("void f() {\n"
3603                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3604                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3605                "}",
3606                NoBinPacking);
3607 
3608   verifyFormat(
3609       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3610       "             aaaaaaaaaaaa,\n"
3611       "             aaaaaaaaaaaa);",
3612       NoBinPacking);
3613   verifyFormat(
3614       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3615       "                               ddddddddddddddddddddddddddddd),\n"
3616       "             test);",
3617       NoBinPacking);
3618 
3619   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3620                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3621                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3622                "    aaaaaaaaaaaaaaaaaa;",
3623                NoBinPacking);
3624   verifyFormat("a(\"a\"\n"
3625                "  \"a\",\n"
3626                "  a);");
3627 
3628   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3629   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3630                "                aaaaaaaaa,\n"
3631                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3632                NoBinPacking);
3633   verifyFormat(
3634       "void f() {\n"
3635       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3636       "      .aaaaaaa();\n"
3637       "}",
3638       NoBinPacking);
3639   verifyFormat(
3640       "template <class SomeType, class SomeOtherType>\n"
3641       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3642       NoBinPacking);
3643 }
3644 
3645 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3646   FormatStyle Style = getLLVMStyleWithColumns(15);
3647   Style.ExperimentalAutoDetectBinPacking = true;
3648   EXPECT_EQ("aaa(aaaa,\n"
3649             "    aaaa,\n"
3650             "    aaaa);\n"
3651             "aaa(aaaa,\n"
3652             "    aaaa,\n"
3653             "    aaaa);",
3654             format("aaa(aaaa,\n" // one-per-line
3655                    "  aaaa,\n"
3656                    "    aaaa  );\n"
3657                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3658                    Style));
3659   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3660             "    aaaa);\n"
3661             "aaa(aaaa, aaaa,\n"
3662             "    aaaa);",
3663             format("aaa(aaaa,  aaaa,\n" // bin-packed
3664                    "    aaaa  );\n"
3665                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3666                    Style));
3667 }
3668 
3669 TEST_F(FormatTest, FormatsBuilderPattern) {
3670   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3671                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3672                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3673                "    .StartsWith(\".init\", ORDER_INIT)\n"
3674                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3675                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3676                "    .Default(ORDER_TEXT);\n");
3677 
3678   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3679                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3680   verifyFormat(
3681       "aaaaaaa->aaaaaaa\n"
3682       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3683       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3684       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3685   verifyFormat(
3686       "aaaaaaa->aaaaaaa\n"
3687       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3688       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3689   verifyFormat(
3690       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3691       "    aaaaaaaaaaaaaa);");
3692   verifyFormat(
3693       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3694       "    aaaaaa->aaaaaaaaaaaa()\n"
3695       "        ->aaaaaaaaaaaaaaaa(\n"
3696       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3697       "        ->aaaaaaaaaaaaaaaaa();");
3698   verifyGoogleFormat(
3699       "void f() {\n"
3700       "  someo->Add((new util::filetools::Handler(dir))\n"
3701       "                 ->OnEvent1(NewPermanentCallback(\n"
3702       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3703       "                 ->OnEvent2(NewPermanentCallback(\n"
3704       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3705       "                 ->OnEvent3(NewPermanentCallback(\n"
3706       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3707       "                 ->OnEvent5(NewPermanentCallback(\n"
3708       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3709       "                 ->OnEvent6(NewPermanentCallback(\n"
3710       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3711       "}");
3712 
3713   verifyFormat(
3714       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3715   verifyFormat("aaaaaaaaaaaaaaa()\n"
3716                "    .aaaaaaaaaaaaaaa()\n"
3717                "    .aaaaaaaaaaaaaaa()\n"
3718                "    .aaaaaaaaaaaaaaa()\n"
3719                "    .aaaaaaaaaaaaaaa();");
3720   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3721                "    .aaaaaaaaaaaaaaa()\n"
3722                "    .aaaaaaaaaaaaaaa()\n"
3723                "    .aaaaaaaaaaaaaaa();");
3724   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3725                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3726                "    .aaaaaaaaaaaaaaa();");
3727   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3728                "    ->aaaaaaaaaaaaaae(0)\n"
3729                "    ->aaaaaaaaaaaaaaa();");
3730 
3731   // Don't linewrap after very short segments.
3732   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3733                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3734                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3735   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3736                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3737                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3738   verifyFormat("aaa()\n"
3739                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3740                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3741                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3742 
3743   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3744                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3745                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3746   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3747                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3748                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3749 
3750   // Prefer not to break after empty parentheses.
3751   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3752                "    First->LastNewlineOffset);");
3753 
3754   // Prefer not to create "hanging" indents.
3755   verifyFormat(
3756       "return !soooooooooooooome_map\n"
3757       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3758       "            .second;");
3759   verifyFormat(
3760       "return aaaaaaaaaaaaaaaa\n"
3761       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
3762       "    .aaaa(aaaaaaaaaaaaaa);");
3763   // No hanging indent here.
3764   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
3765                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3766   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
3767                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3768   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3769                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3770                getLLVMStyleWithColumns(60));
3771   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
3772                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3773                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3774                getLLVMStyleWithColumns(59));
3775   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3776                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3777                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3778 }
3779 
3780 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3781   verifyFormat(
3782       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3783       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3784   verifyFormat(
3785       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3786       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3787 
3788   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3789                "    ccccccccccccccccccccccccc) {\n}");
3790   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3791                "    ccccccccccccccccccccccccc) {\n}");
3792 
3793   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3794                "    ccccccccccccccccccccccccc) {\n}");
3795   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3796                "    ccccccccccccccccccccccccc) {\n}");
3797 
3798   verifyFormat(
3799       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3800       "    ccccccccccccccccccccccccc) {\n}");
3801   verifyFormat(
3802       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3803       "    ccccccccccccccccccccccccc) {\n}");
3804 
3805   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3806                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3807                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3808                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3809   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3810                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3811                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3812                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3813 
3814   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3815                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3816                "    aaaaaaaaaaaaaaa != aa) {\n}");
3817   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3818                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3819                "    aaaaaaaaaaaaaaa != aa) {\n}");
3820 }
3821 
3822 TEST_F(FormatTest, BreaksAfterAssignments) {
3823   verifyFormat(
3824       "unsigned Cost =\n"
3825       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3826       "                        SI->getPointerAddressSpaceee());\n");
3827   verifyFormat(
3828       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3829       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3830 
3831   verifyFormat(
3832       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3833       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3834   verifyFormat("unsigned OriginalStartColumn =\n"
3835                "    SourceMgr.getSpellingColumnNumber(\n"
3836                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3837                "    1;");
3838 }
3839 
3840 TEST_F(FormatTest, ConfigurableBreakAssignmentPenalty) {
3841   FormatStyle Style = getLLVMStyle();
3842   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3843                "    bbbbbbbbbbbbbbbbbbbbbbbbbb + cccccccccccccccccccccccccc;",
3844                Style);
3845 
3846   Style.PenaltyBreakAssignment = 20;
3847   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3848                "                                 cccccccccccccccccccccccccc;",
3849                Style);
3850 }
3851 
3852 TEST_F(FormatTest, AlignsAfterAssignments) {
3853   verifyFormat(
3854       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3855       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3856   verifyFormat(
3857       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3858       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3859   verifyFormat(
3860       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3861       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3862   verifyFormat(
3863       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3864       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3865   verifyFormat(
3866       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3867       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3868       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3869 }
3870 
3871 TEST_F(FormatTest, AlignsAfterReturn) {
3872   verifyFormat(
3873       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3874       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3875   verifyFormat(
3876       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3877       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3878   verifyFormat(
3879       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3880       "       aaaaaaaaaaaaaaaaaaaaaa();");
3881   verifyFormat(
3882       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3883       "        aaaaaaaaaaaaaaaaaaaaaa());");
3884   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3885                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3886   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3887                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3888                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3889   verifyFormat("return\n"
3890                "    // true if code is one of a or b.\n"
3891                "    code == a || code == b;");
3892 }
3893 
3894 TEST_F(FormatTest, AlignsAfterOpenBracket) {
3895   verifyFormat(
3896       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3897       "                                                aaaaaaaaa aaaaaaa) {}");
3898   verifyFormat(
3899       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3900       "                                               aaaaaaaaaaa aaaaaaaaa);");
3901   verifyFormat(
3902       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3903       "                                             aaaaaaaaaaaaaaaaaaaaa));");
3904   FormatStyle Style = getLLVMStyle();
3905   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3906   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3907                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
3908                Style);
3909   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3910                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
3911                Style);
3912   verifyFormat("SomeLongVariableName->someFunction(\n"
3913                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
3914                Style);
3915   verifyFormat(
3916       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3917       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3918       Style);
3919   verifyFormat(
3920       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3921       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3922       Style);
3923   verifyFormat(
3924       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3925       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3926       Style);
3927 
3928   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
3929                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
3930                "        b));",
3931                Style);
3932 
3933   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
3934   Style.BinPackArguments = false;
3935   Style.BinPackParameters = false;
3936   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3937                "    aaaaaaaaaaa aaaaaaaa,\n"
3938                "    aaaaaaaaa aaaaaaa,\n"
3939                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3940                Style);
3941   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3942                "    aaaaaaaaaaa aaaaaaaaa,\n"
3943                "    aaaaaaaaaaa aaaaaaaaa,\n"
3944                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3945                Style);
3946   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
3947                "    aaaaaaaaaaaaaaa,\n"
3948                "    aaaaaaaaaaaaaaaaaaaaa,\n"
3949                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3950                Style);
3951   verifyFormat(
3952       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
3953       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3954       Style);
3955   verifyFormat(
3956       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
3957       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3958       Style);
3959   verifyFormat(
3960       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3961       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3962       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
3963       "    aaaaaaaaaaaaaaaa);",
3964       Style);
3965   verifyFormat(
3966       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3967       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3968       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
3969       "    aaaaaaaaaaaaaaaa);",
3970       Style);
3971 }
3972 
3973 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
3974   FormatStyle Style = getLLVMStyleWithColumns(40);
3975   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3976                "          bbbbbbbbbbbbbbbbbbbbbb);",
3977                Style);
3978   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
3979   Style.AlignOperands = false;
3980   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3981                "          bbbbbbbbbbbbbbbbbbbbbb);",
3982                Style);
3983   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3984   Style.AlignOperands = true;
3985   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3986                "          bbbbbbbbbbbbbbbbbbbbbb);",
3987                Style);
3988   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3989   Style.AlignOperands = false;
3990   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3991                "    bbbbbbbbbbbbbbbbbbbbbb);",
3992                Style);
3993 }
3994 
3995 TEST_F(FormatTest, BreaksConditionalExpressions) {
3996   verifyFormat(
3997       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3998       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3999       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4000   verifyFormat(
4001       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4002       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4003       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4004   verifyFormat(
4005       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4006       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4007   verifyFormat(
4008       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
4009       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4010       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4011   verifyFormat(
4012       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4013       "                                                    : aaaaaaaaaaaaa);");
4014   verifyFormat(
4015       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4016       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4017       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4018       "                   aaaaaaaaaaaaa);");
4019   verifyFormat(
4020       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4021       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4022       "                   aaaaaaaaaaaaa);");
4023   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4024                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4025                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4026                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4027                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4028   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4029                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4030                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4031                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4032                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4033                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4034                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4035   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4036                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4037                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4038                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4039                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4040   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4041                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4042                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4043   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4044                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4045                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4046                "        : aaaaaaaaaaaaaaaa;");
4047   verifyFormat(
4048       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4049       "    ? aaaaaaaaaaaaaaa\n"
4050       "    : aaaaaaaaaaaaaaa;");
4051   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4052                "          aaaaaaaaa\n"
4053                "      ? b\n"
4054                "      : c);");
4055   verifyFormat("return aaaa == bbbb\n"
4056                "           // comment\n"
4057                "           ? aaaa\n"
4058                "           : bbbb;");
4059   verifyFormat("unsigned Indent =\n"
4060                "    format(TheLine.First,\n"
4061                "           IndentForLevel[TheLine.Level] >= 0\n"
4062                "               ? IndentForLevel[TheLine.Level]\n"
4063                "               : TheLine * 2,\n"
4064                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4065                getLLVMStyleWithColumns(60));
4066   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4067                "                  ? aaaaaaaaaaaaaaa\n"
4068                "                  : bbbbbbbbbbbbbbb //\n"
4069                "                        ? ccccccccccccccc\n"
4070                "                        : ddddddddddddddd;");
4071   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4072                "                  ? aaaaaaaaaaaaaaa\n"
4073                "                  : (bbbbbbbbbbbbbbb //\n"
4074                "                         ? ccccccccccccccc\n"
4075                "                         : ddddddddddddddd);");
4076   verifyFormat(
4077       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4078       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4079       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4080       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4081       "                                      : aaaaaaaaaa;");
4082   verifyFormat(
4083       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4084       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
4085       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4086 
4087   FormatStyle NoBinPacking = getLLVMStyle();
4088   NoBinPacking.BinPackArguments = false;
4089   verifyFormat(
4090       "void f() {\n"
4091       "  g(aaa,\n"
4092       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4093       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4094       "        ? aaaaaaaaaaaaaaa\n"
4095       "        : aaaaaaaaaaaaaaa);\n"
4096       "}",
4097       NoBinPacking);
4098   verifyFormat(
4099       "void f() {\n"
4100       "  g(aaa,\n"
4101       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4102       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4103       "        ?: aaaaaaaaaaaaaaa);\n"
4104       "}",
4105       NoBinPacking);
4106 
4107   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4108                "             // comment.\n"
4109                "             ccccccccccccccccccccccccccccccccccccccc\n"
4110                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4111                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4112 
4113   // Assignments in conditional expressions. Apparently not uncommon :-(.
4114   verifyFormat("return a != b\n"
4115                "           // comment\n"
4116                "           ? a = b\n"
4117                "           : a = b;");
4118   verifyFormat("return a != b\n"
4119                "           // comment\n"
4120                "           ? a = a != b\n"
4121                "                     // comment\n"
4122                "                     ? a = b\n"
4123                "                     : a\n"
4124                "           : a;\n");
4125   verifyFormat("return a != b\n"
4126                "           // comment\n"
4127                "           ? a\n"
4128                "           : a = a != b\n"
4129                "                     // comment\n"
4130                "                     ? a = b\n"
4131                "                     : a;");
4132 }
4133 
4134 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4135   FormatStyle Style = getLLVMStyle();
4136   Style.BreakBeforeTernaryOperators = false;
4137   Style.ColumnLimit = 70;
4138   verifyFormat(
4139       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4140       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4141       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4142       Style);
4143   verifyFormat(
4144       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
4145       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4146       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4147       Style);
4148   verifyFormat(
4149       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4150       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4151       Style);
4152   verifyFormat(
4153       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
4154       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4155       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4156       Style);
4157   verifyFormat(
4158       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4159       "                                                      aaaaaaaaaaaaa);",
4160       Style);
4161   verifyFormat(
4162       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4163       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4164       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4165       "                   aaaaaaaaaaaaa);",
4166       Style);
4167   verifyFormat(
4168       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4169       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4170       "                   aaaaaaaaaaaaa);",
4171       Style);
4172   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4173                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4174                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4175                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4176                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4177                Style);
4178   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4179                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4180                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4181                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4182                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4183                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4184                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4185                Style);
4186   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4187                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4188                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4189                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4190                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4191                Style);
4192   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4193                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4194                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4195                Style);
4196   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4197                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4198                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4199                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4200                Style);
4201   verifyFormat(
4202       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4203       "    aaaaaaaaaaaaaaa :\n"
4204       "    aaaaaaaaaaaaaaa;",
4205       Style);
4206   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4207                "          aaaaaaaaa ?\n"
4208                "      b :\n"
4209                "      c);",
4210                Style);
4211   verifyFormat("unsigned Indent =\n"
4212                "    format(TheLine.First,\n"
4213                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
4214                "               IndentForLevel[TheLine.Level] :\n"
4215                "               TheLine * 2,\n"
4216                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4217                Style);
4218   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4219                "                  aaaaaaaaaaaaaaa :\n"
4220                "                  bbbbbbbbbbbbbbb ? //\n"
4221                "                      ccccccccccccccc :\n"
4222                "                      ddddddddddddddd;",
4223                Style);
4224   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4225                "                  aaaaaaaaaaaaaaa :\n"
4226                "                  (bbbbbbbbbbbbbbb ? //\n"
4227                "                       ccccccccccccccc :\n"
4228                "                       ddddddddddddddd);",
4229                Style);
4230   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4231                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
4232                "            ccccccccccccccccccccccccccc;",
4233                Style);
4234   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4235                "           aaaaa :\n"
4236                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
4237                Style);
4238 }
4239 
4240 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4241   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4242                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4243   verifyFormat("bool a = true, b = false;");
4244 
4245   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4246                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4247                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4248                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4249   verifyFormat(
4250       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4251       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4252       "     d = e && f;");
4253   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4254                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4255   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4256                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4257   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4258                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4259 
4260   FormatStyle Style = getGoogleStyle();
4261   Style.PointerAlignment = FormatStyle::PAS_Left;
4262   Style.DerivePointerAlignment = false;
4263   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4264                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4265                "    *b = bbbbbbbbbbbbbbbbbbb;",
4266                Style);
4267   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4268                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4269                Style);
4270   verifyFormat("vector<int*> a, b;", Style);
4271   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
4272 }
4273 
4274 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4275   verifyFormat("arr[foo ? bar : baz];");
4276   verifyFormat("f()[foo ? bar : baz];");
4277   verifyFormat("(a + b)[foo ? bar : baz];");
4278   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4279 }
4280 
4281 TEST_F(FormatTest, AlignsStringLiterals) {
4282   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4283                "                                      \"short literal\");");
4284   verifyFormat(
4285       "looooooooooooooooooooooooongFunction(\n"
4286       "    \"short literal\"\n"
4287       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4288   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4289                "             \" string literals\",\n"
4290                "             and, other, parameters);");
4291   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4292             "      \"5678\";",
4293             format("fun + \"1243\" /* comment */\n"
4294                    "    \"5678\";",
4295                    getLLVMStyleWithColumns(28)));
4296   EXPECT_EQ(
4297       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4298       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4299       "         \"aaaaaaaaaaaaaaaa\";",
4300       format("aaaaaa ="
4301              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4302              "aaaaaaaaaaaaaaaaaaaaa\" "
4303              "\"aaaaaaaaaaaaaaaa\";"));
4304   verifyFormat("a = a + \"a\"\n"
4305                "        \"a\"\n"
4306                "        \"a\";");
4307   verifyFormat("f(\"a\", \"b\"\n"
4308                "       \"c\");");
4309 
4310   verifyFormat(
4311       "#define LL_FORMAT \"ll\"\n"
4312       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4313       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4314 
4315   verifyFormat("#define A(X)          \\\n"
4316                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4317                "  \"ccccc\"",
4318                getLLVMStyleWithColumns(23));
4319   verifyFormat("#define A \"def\"\n"
4320                "f(\"abc\" A \"ghi\"\n"
4321                "  \"jkl\");");
4322 
4323   verifyFormat("f(L\"a\"\n"
4324                "  L\"b\");");
4325   verifyFormat("#define A(X)            \\\n"
4326                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4327                "  L\"ccccc\"",
4328                getLLVMStyleWithColumns(25));
4329 
4330   verifyFormat("f(@\"a\"\n"
4331                "  @\"b\");");
4332   verifyFormat("NSString s = @\"a\"\n"
4333                "             @\"b\"\n"
4334                "             @\"c\";");
4335   verifyFormat("NSString s = @\"a\"\n"
4336                "              \"b\"\n"
4337                "              \"c\";");
4338 }
4339 
4340 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
4341   FormatStyle Style = getLLVMStyle();
4342   // No declarations or definitions should be moved to own line.
4343   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
4344   verifyFormat("class A {\n"
4345                "  int f() { return 1; }\n"
4346                "  int g();\n"
4347                "};\n"
4348                "int f() { return 1; }\n"
4349                "int g();\n",
4350                Style);
4351 
4352   // All declarations and definitions should have the return type moved to its
4353   // own
4354   // line.
4355   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
4356   verifyFormat("class E {\n"
4357                "  int\n"
4358                "  f() {\n"
4359                "    return 1;\n"
4360                "  }\n"
4361                "  int\n"
4362                "  g();\n"
4363                "};\n"
4364                "int\n"
4365                "f() {\n"
4366                "  return 1;\n"
4367                "}\n"
4368                "int\n"
4369                "g();\n",
4370                Style);
4371 
4372   // Top-level definitions, and no kinds of declarations should have the
4373   // return type moved to its own line.
4374   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
4375   verifyFormat("class B {\n"
4376                "  int f() { return 1; }\n"
4377                "  int g();\n"
4378                "};\n"
4379                "int\n"
4380                "f() {\n"
4381                "  return 1;\n"
4382                "}\n"
4383                "int g();\n",
4384                Style);
4385 
4386   // Top-level definitions and declarations should have the return type moved
4387   // to its own line.
4388   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
4389   verifyFormat("class C {\n"
4390                "  int f() { return 1; }\n"
4391                "  int g();\n"
4392                "};\n"
4393                "int\n"
4394                "f() {\n"
4395                "  return 1;\n"
4396                "}\n"
4397                "int\n"
4398                "g();\n",
4399                Style);
4400 
4401   // All definitions should have the return type moved to its own line, but no
4402   // kinds of declarations.
4403   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
4404   verifyFormat("class D {\n"
4405                "  int\n"
4406                "  f() {\n"
4407                "    return 1;\n"
4408                "  }\n"
4409                "  int g();\n"
4410                "};\n"
4411                "int\n"
4412                "f() {\n"
4413                "  return 1;\n"
4414                "}\n"
4415                "int g();\n",
4416                Style);
4417   verifyFormat("const char *\n"
4418                "f(void) {\n" // Break here.
4419                "  return \"\";\n"
4420                "}\n"
4421                "const char *bar(void);\n", // No break here.
4422                Style);
4423   verifyFormat("template <class T>\n"
4424                "T *\n"
4425                "f(T &c) {\n" // Break here.
4426                "  return NULL;\n"
4427                "}\n"
4428                "template <class T> T *f(T &c);\n", // No break here.
4429                Style);
4430   verifyFormat("class C {\n"
4431                "  int\n"
4432                "  operator+() {\n"
4433                "    return 1;\n"
4434                "  }\n"
4435                "  int\n"
4436                "  operator()() {\n"
4437                "    return 1;\n"
4438                "  }\n"
4439                "};\n",
4440                Style);
4441   verifyFormat("void\n"
4442                "A::operator()() {}\n"
4443                "void\n"
4444                "A::operator>>() {}\n"
4445                "void\n"
4446                "A::operator+() {}\n",
4447                Style);
4448   verifyFormat("void *operator new(std::size_t s);", // No break here.
4449                Style);
4450   verifyFormat("void *\n"
4451                "operator new(std::size_t s) {}",
4452                Style);
4453   verifyFormat("void *\n"
4454                "operator delete[](void *ptr) {}",
4455                Style);
4456   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4457   verifyFormat("const char *\n"
4458                "f(void)\n" // Break here.
4459                "{\n"
4460                "  return \"\";\n"
4461                "}\n"
4462                "const char *bar(void);\n", // No break here.
4463                Style);
4464   verifyFormat("template <class T>\n"
4465                "T *\n"     // Problem here: no line break
4466                "f(T &c)\n" // Break here.
4467                "{\n"
4468                "  return NULL;\n"
4469                "}\n"
4470                "template <class T> T *f(T &c);\n", // No break here.
4471                Style);
4472 }
4473 
4474 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4475   FormatStyle NoBreak = getLLVMStyle();
4476   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4477   FormatStyle Break = getLLVMStyle();
4478   Break.AlwaysBreakBeforeMultilineStrings = true;
4479   verifyFormat("aaaa = \"bbbb\"\n"
4480                "       \"cccc\";",
4481                NoBreak);
4482   verifyFormat("aaaa =\n"
4483                "    \"bbbb\"\n"
4484                "    \"cccc\";",
4485                Break);
4486   verifyFormat("aaaa(\"bbbb\"\n"
4487                "     \"cccc\");",
4488                NoBreak);
4489   verifyFormat("aaaa(\n"
4490                "    \"bbbb\"\n"
4491                "    \"cccc\");",
4492                Break);
4493   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4494                "          \"cccc\");",
4495                NoBreak);
4496   verifyFormat("aaaa(qqq,\n"
4497                "     \"bbbb\"\n"
4498                "     \"cccc\");",
4499                Break);
4500   verifyFormat("aaaa(qqq,\n"
4501                "     L\"bbbb\"\n"
4502                "     L\"cccc\");",
4503                Break);
4504   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4505                "                      \"bbbb\"));",
4506                Break);
4507   verifyFormat("string s = someFunction(\n"
4508                "    \"abc\"\n"
4509                "    \"abc\");",
4510                Break);
4511 
4512   // As we break before unary operators, breaking right after them is bad.
4513   verifyFormat("string foo = abc ? \"x\"\n"
4514                "                   \"blah blah blah blah blah blah\"\n"
4515                "                 : \"y\";",
4516                Break);
4517 
4518   // Don't break if there is no column gain.
4519   verifyFormat("f(\"aaaa\"\n"
4520                "  \"bbbb\");",
4521                Break);
4522 
4523   // Treat literals with escaped newlines like multi-line string literals.
4524   EXPECT_EQ("x = \"a\\\n"
4525             "b\\\n"
4526             "c\";",
4527             format("x = \"a\\\n"
4528                    "b\\\n"
4529                    "c\";",
4530                    NoBreak));
4531   EXPECT_EQ("xxxx =\n"
4532             "    \"a\\\n"
4533             "b\\\n"
4534             "c\";",
4535             format("xxxx = \"a\\\n"
4536                    "b\\\n"
4537                    "c\";",
4538                    Break));
4539 
4540   EXPECT_EQ("NSString *const kString =\n"
4541             "    @\"aaaa\"\n"
4542             "    @\"bbbb\";",
4543             format("NSString *const kString = @\"aaaa\"\n"
4544                    "@\"bbbb\";",
4545                    Break));
4546 
4547   Break.ColumnLimit = 0;
4548   verifyFormat("const char *hello = \"hello llvm\";", Break);
4549 }
4550 
4551 TEST_F(FormatTest, AlignsPipes) {
4552   verifyFormat(
4553       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4554       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4555       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4556   verifyFormat(
4557       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4558       "                     << aaaaaaaaaaaaaaaaaaaa;");
4559   verifyFormat(
4560       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4561       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4562   verifyFormat(
4563       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4564       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4565   verifyFormat(
4566       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4567       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4568       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4569   verifyFormat(
4570       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4571       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4572       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4573   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4574                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4575                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4576                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4577   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4578                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4579   verifyFormat(
4580       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4581       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4582   verifyFormat(
4583       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
4584       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
4585 
4586   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4587                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4588   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4589                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4590                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4591                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4592   verifyFormat("LOG_IF(aaa == //\n"
4593                "       bbb)\n"
4594                "    << a << b;");
4595 
4596   // But sometimes, breaking before the first "<<" is desirable.
4597   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4598                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4599   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4600                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4601                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4602   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4603                "    << BEF << IsTemplate << Description << E->getType();");
4604   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4605                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4606                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4607   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4608                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4609                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4610                "    << aaa;");
4611 
4612   verifyFormat(
4613       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4614       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4615 
4616   // Incomplete string literal.
4617   EXPECT_EQ("llvm::errs() << \"\n"
4618             "             << a;",
4619             format("llvm::errs() << \"\n<<a;"));
4620 
4621   verifyFormat("void f() {\n"
4622                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4623                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4624                "}");
4625 
4626   // Handle 'endl'.
4627   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4628                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4629   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4630 
4631   // Handle '\n'.
4632   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4633                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4634   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4635                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4636   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4637                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4638   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4639 }
4640 
4641 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4642   verifyFormat("return out << \"somepacket = {\\n\"\n"
4643                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4644                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4645                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4646                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4647                "           << \"}\";");
4648 
4649   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4650                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4651                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4652   verifyFormat(
4653       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4654       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4655       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4656       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4657       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4658   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4659                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4660   verifyFormat(
4661       "void f() {\n"
4662       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4663       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4664       "}");
4665 
4666   // Breaking before the first "<<" is generally not desirable.
4667   verifyFormat(
4668       "llvm::errs()\n"
4669       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4670       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4671       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4672       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4673       getLLVMStyleWithColumns(70));
4674   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4675                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4676                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4677                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4678                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4679                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4680                getLLVMStyleWithColumns(70));
4681 
4682   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4683                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4684                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4685   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4686                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4687                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4688   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4689                "           (aaaa + aaaa);",
4690                getLLVMStyleWithColumns(40));
4691   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4692                "                  (aaaaaaa + aaaaa));",
4693                getLLVMStyleWithColumns(40));
4694   verifyFormat(
4695       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
4696       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
4697       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
4698 }
4699 
4700 TEST_F(FormatTest, UnderstandsEquals) {
4701   verifyFormat(
4702       "aaaaaaaaaaaaaaaaa =\n"
4703       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4704   verifyFormat(
4705       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4706       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4707   verifyFormat(
4708       "if (a) {\n"
4709       "  f();\n"
4710       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4711       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4712       "}");
4713 
4714   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4715                "        100000000 + 10000000) {\n}");
4716 }
4717 
4718 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4719   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4720                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4721 
4722   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4723                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4724 
4725   verifyFormat(
4726       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4727       "                                                          Parameter2);");
4728 
4729   verifyFormat(
4730       "ShortObject->shortFunction(\n"
4731       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4732       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4733 
4734   verifyFormat("loooooooooooooongFunction(\n"
4735                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4736 
4737   verifyFormat(
4738       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4739       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4740 
4741   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4742                "    .WillRepeatedly(Return(SomeValue));");
4743   verifyFormat("void f() {\n"
4744                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4745                "      .Times(2)\n"
4746                "      .WillRepeatedly(Return(SomeValue));\n"
4747                "}");
4748   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4749                "    ccccccccccccccccccccccc);");
4750   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4751                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4752                "          .aaaaa(aaaaa),\n"
4753                "      aaaaaaaaaaaaaaaaaaaaa);");
4754   verifyFormat("void f() {\n"
4755                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4756                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4757                "}");
4758   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4759                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4760                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4761                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4762                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4763   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4764                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4765                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4766                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4767                "}");
4768 
4769   // Here, it is not necessary to wrap at "." or "->".
4770   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4771                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4772   verifyFormat(
4773       "aaaaaaaaaaa->aaaaaaaaa(\n"
4774       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4775       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4776 
4777   verifyFormat(
4778       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4779       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4780   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4781                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4782   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4783                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4784 
4785   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4786                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4787                "    .a();");
4788 
4789   FormatStyle NoBinPacking = getLLVMStyle();
4790   NoBinPacking.BinPackParameters = false;
4791   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4792                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4793                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4794                "                         aaaaaaaaaaaaaaaaaaa,\n"
4795                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4796                NoBinPacking);
4797 
4798   // If there is a subsequent call, change to hanging indentation.
4799   verifyFormat(
4800       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4801       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4802       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4803   verifyFormat(
4804       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4805       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4806   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4807                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4808                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4809   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4810                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4811                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4812 }
4813 
4814 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4815   verifyFormat("template <typename T>\n"
4816                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4817   verifyFormat("template <typename T>\n"
4818                "// T should be one of {A, B}.\n"
4819                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4820   verifyFormat(
4821       "template <typename T>\n"
4822       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4823   verifyFormat("template <typename T>\n"
4824                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4825                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4826   verifyFormat(
4827       "template <typename T>\n"
4828       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4829       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4830   verifyFormat(
4831       "template <typename T>\n"
4832       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4833       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4834       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4835   verifyFormat("template <typename T>\n"
4836                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4837                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4838   verifyFormat(
4839       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4840       "          typename T4 = char>\n"
4841       "void f();");
4842   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4843                "          template <typename> class cccccccccccccccccccccc,\n"
4844                "          typename ddddddddddddd>\n"
4845                "class C {};");
4846   verifyFormat(
4847       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4848       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4849 
4850   verifyFormat("void f() {\n"
4851                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4852                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4853                "}");
4854 
4855   verifyFormat("template <typename T> class C {};");
4856   verifyFormat("template <typename T> void f();");
4857   verifyFormat("template <typename T> void f() {}");
4858   verifyFormat(
4859       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4860       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4861       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4862       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4863       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4864       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4865       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4866       getLLVMStyleWithColumns(72));
4867   EXPECT_EQ("static_cast<A< //\n"
4868             "    B> *>(\n"
4869             "\n"
4870             ");",
4871             format("static_cast<A<//\n"
4872                    "    B>*>(\n"
4873                    "\n"
4874                    "    );"));
4875   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4876                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4877 
4878   FormatStyle AlwaysBreak = getLLVMStyle();
4879   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4880   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4881   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4882   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4883   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4884                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4885                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4886   verifyFormat("template <template <typename> class Fooooooo,\n"
4887                "          template <typename> class Baaaaaaar>\n"
4888                "struct C {};",
4889                AlwaysBreak);
4890   verifyFormat("template <typename T> // T can be A, B or C.\n"
4891                "struct C {};",
4892                AlwaysBreak);
4893   verifyFormat("template <enum E> class A {\n"
4894                "public:\n"
4895                "  E *f();\n"
4896                "};");
4897 }
4898 
4899 TEST_F(FormatTest, WrapsTemplateParameters) {
4900   FormatStyle Style = getLLVMStyle();
4901   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4902   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4903   verifyFormat(
4904       "template <typename... a> struct q {};\n"
4905       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4906       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4907       "    y;",
4908       Style);
4909   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4910   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4911   verifyFormat(
4912       "template <typename... a> struct r {};\n"
4913       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4914       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4915       "    y;",
4916       Style);
4917   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4918   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4919   verifyFormat(
4920       "template <typename... a> struct s {};\n"
4921       "extern s<\n"
4922       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4923       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4924       "    y;",
4925       Style);
4926   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4927   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4928   verifyFormat(
4929       "template <typename... a> struct t {};\n"
4930       "extern t<\n"
4931       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4932       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4933       "    y;",
4934       Style);
4935 }
4936 
4937 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4938   verifyFormat(
4939       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4940       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4941   verifyFormat(
4942       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4943       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4944       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4945 
4946   // FIXME: Should we have the extra indent after the second break?
4947   verifyFormat(
4948       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4949       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4950       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4951 
4952   verifyFormat(
4953       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4954       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4955 
4956   // Breaking at nested name specifiers is generally not desirable.
4957   verifyFormat(
4958       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4959       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4960 
4961   verifyFormat(
4962       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
4963       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4964       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4965       "                   aaaaaaaaaaaaaaaaaaaaa);",
4966       getLLVMStyleWithColumns(74));
4967 
4968   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4969                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4970                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4971 }
4972 
4973 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4974   verifyFormat("A<int> a;");
4975   verifyFormat("A<A<A<int>>> a;");
4976   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4977   verifyFormat("bool x = a < 1 || 2 > a;");
4978   verifyFormat("bool x = 5 < f<int>();");
4979   verifyFormat("bool x = f<int>() > 5;");
4980   verifyFormat("bool x = 5 < a<int>::x;");
4981   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4982   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4983 
4984   verifyGoogleFormat("A<A<int>> a;");
4985   verifyGoogleFormat("A<A<A<int>>> a;");
4986   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4987   verifyGoogleFormat("A<A<int> > a;");
4988   verifyGoogleFormat("A<A<A<int> > > a;");
4989   verifyGoogleFormat("A<A<A<A<int> > > > a;");
4990   verifyGoogleFormat("A<::A<int>> a;");
4991   verifyGoogleFormat("A<::A> a;");
4992   verifyGoogleFormat("A< ::A> a;");
4993   verifyGoogleFormat("A< ::A<int> > a;");
4994   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4995   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4996   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4997   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4998   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
4999             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
5000 
5001   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5002 
5003   verifyFormat("test >> a >> b;");
5004   verifyFormat("test << a >> b;");
5005 
5006   verifyFormat("f<int>();");
5007   verifyFormat("template <typename T> void f() {}");
5008   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5009   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
5010                "sizeof(char)>::type>;");
5011   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
5012   verifyFormat("f(a.operator()<A>());");
5013   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5014                "      .template operator()<A>());",
5015                getLLVMStyleWithColumns(35));
5016 
5017   // Not template parameters.
5018   verifyFormat("return a < b && c > d;");
5019   verifyFormat("void f() {\n"
5020                "  while (a < b && c > d) {\n"
5021                "  }\n"
5022                "}");
5023   verifyFormat("template <typename... Types>\n"
5024                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5025 
5026   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5027                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5028                getLLVMStyleWithColumns(60));
5029   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5030   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5031   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
5032 }
5033 
5034 TEST_F(FormatTest, BitshiftOperatorWidth) {
5035   EXPECT_EQ("int a = 1 << 2; /* foo\n"
5036             "                   bar */",
5037             format("int    a=1<<2;  /* foo\n"
5038                    "                   bar */"));
5039 
5040   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
5041             "                     bar */",
5042             format("int  b  =256>>1 ;  /* foo\n"
5043                    "                      bar */"));
5044 }
5045 
5046 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5047   verifyFormat("COMPARE(a, ==, b);");
5048   verifyFormat("auto s = sizeof...(Ts) - 1;");
5049 }
5050 
5051 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5052   verifyFormat("int A::*x;");
5053   verifyFormat("int (S::*func)(void *);");
5054   verifyFormat("void f() { int (S::*func)(void *); }");
5055   verifyFormat("typedef bool *(Class::*Member)() const;");
5056   verifyFormat("void f() {\n"
5057                "  (a->*f)();\n"
5058                "  a->*x;\n"
5059                "  (a.*f)();\n"
5060                "  ((*a).*f)();\n"
5061                "  a.*x;\n"
5062                "}");
5063   verifyFormat("void f() {\n"
5064                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5065                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5066                "}");
5067   verifyFormat(
5068       "(aaaaaaaaaa->*bbbbbbb)(\n"
5069       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5070   FormatStyle Style = getLLVMStyle();
5071   Style.PointerAlignment = FormatStyle::PAS_Left;
5072   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5073 }
5074 
5075 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5076   verifyFormat("int a = -2;");
5077   verifyFormat("f(-1, -2, -3);");
5078   verifyFormat("a[-1] = 5;");
5079   verifyFormat("int a = 5 + -2;");
5080   verifyFormat("if (i == -1) {\n}");
5081   verifyFormat("if (i != -1) {\n}");
5082   verifyFormat("if (i > -1) {\n}");
5083   verifyFormat("if (i < -1) {\n}");
5084   verifyFormat("++(a->f());");
5085   verifyFormat("--(a->f());");
5086   verifyFormat("(a->f())++;");
5087   verifyFormat("a[42]++;");
5088   verifyFormat("if (!(a->f())) {\n}");
5089 
5090   verifyFormat("a-- > b;");
5091   verifyFormat("b ? -a : c;");
5092   verifyFormat("n * sizeof char16;");
5093   verifyFormat("n * alignof char16;", getGoogleStyle());
5094   verifyFormat("sizeof(char);");
5095   verifyFormat("alignof(char);", getGoogleStyle());
5096 
5097   verifyFormat("return -1;");
5098   verifyFormat("switch (a) {\n"
5099                "case -1:\n"
5100                "  break;\n"
5101                "}");
5102   verifyFormat("#define X -1");
5103   verifyFormat("#define X -kConstant");
5104 
5105   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5106   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5107 
5108   verifyFormat("int a = /* confusing comment */ -1;");
5109   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5110   verifyFormat("int a = i /* confusing comment */++;");
5111 }
5112 
5113 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5114   verifyFormat("if (!aaaaaaaaaa( // break\n"
5115                "        aaaaa)) {\n"
5116                "}");
5117   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5118                "    aaaaa));");
5119   verifyFormat("*aaa = aaaaaaa( // break\n"
5120                "    bbbbbb);");
5121 }
5122 
5123 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5124   verifyFormat("bool operator<();");
5125   verifyFormat("bool operator>();");
5126   verifyFormat("bool operator=();");
5127   verifyFormat("bool operator==();");
5128   verifyFormat("bool operator!=();");
5129   verifyFormat("int operator+();");
5130   verifyFormat("int operator++();");
5131   verifyFormat("bool operator,();");
5132   verifyFormat("bool operator();");
5133   verifyFormat("bool operator()();");
5134   verifyFormat("bool operator[]();");
5135   verifyFormat("operator bool();");
5136   verifyFormat("operator int();");
5137   verifyFormat("operator void *();");
5138   verifyFormat("operator SomeType<int>();");
5139   verifyFormat("operator SomeType<int, int>();");
5140   verifyFormat("operator SomeType<SomeType<int>>();");
5141   verifyFormat("void *operator new(std::size_t size);");
5142   verifyFormat("void *operator new[](std::size_t size);");
5143   verifyFormat("void operator delete(void *ptr);");
5144   verifyFormat("void operator delete[](void *ptr);");
5145   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5146                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5147   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
5148                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
5149 
5150   verifyFormat(
5151       "ostream &operator<<(ostream &OutputStream,\n"
5152       "                    SomeReallyLongType WithSomeReallyLongValue);");
5153   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5154                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5155                "  return left.group < right.group;\n"
5156                "}");
5157   verifyFormat("SomeType &operator=(const SomeType &S);");
5158   verifyFormat("f.template operator()<int>();");
5159 
5160   verifyGoogleFormat("operator void*();");
5161   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5162   verifyGoogleFormat("operator ::A();");
5163 
5164   verifyFormat("using A::operator+;");
5165   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
5166                "int i;");
5167 }
5168 
5169 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5170   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
5171   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
5172   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
5173   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
5174   verifyFormat("Deleted &operator=(const Deleted &) &;");
5175   verifyFormat("Deleted &operator=(const Deleted &) &&;");
5176   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
5177   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
5178   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
5179   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
5180   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
5181   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
5182   verifyFormat("template <typename T>\n"
5183                "void F(T) && = delete;",
5184                getGoogleStyle());
5185 
5186   FormatStyle AlignLeft = getLLVMStyle();
5187   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
5188   verifyFormat("void A::b() && {}", AlignLeft);
5189   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
5190   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
5191                AlignLeft);
5192   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
5193   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
5194   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
5195   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
5196   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
5197   verifyFormat("auto Function(T) & -> void;", AlignLeft);
5198   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
5199 
5200   FormatStyle Spaces = getLLVMStyle();
5201   Spaces.SpacesInCStyleCastParentheses = true;
5202   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
5203   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
5204   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
5205   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
5206 
5207   Spaces.SpacesInCStyleCastParentheses = false;
5208   Spaces.SpacesInParentheses = true;
5209   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
5210   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
5211   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
5212   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
5213 }
5214 
5215 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5216   verifyFormat("void f() {\n"
5217                "  A *a = new A;\n"
5218                "  A *a = new (placement) A;\n"
5219                "  delete a;\n"
5220                "  delete (A *)a;\n"
5221                "}");
5222   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5223                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5224   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5225                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5226                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5227   verifyFormat("delete[] h->p;");
5228 }
5229 
5230 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5231   verifyFormat("int *f(int *a) {}");
5232   verifyFormat("int main(int argc, char **argv) {}");
5233   verifyFormat("Test::Test(int b) : a(b * b) {}");
5234   verifyIndependentOfContext("f(a, *a);");
5235   verifyFormat("void g() { f(*a); }");
5236   verifyIndependentOfContext("int a = b * 10;");
5237   verifyIndependentOfContext("int a = 10 * b;");
5238   verifyIndependentOfContext("int a = b * c;");
5239   verifyIndependentOfContext("int a += b * c;");
5240   verifyIndependentOfContext("int a -= b * c;");
5241   verifyIndependentOfContext("int a *= b * c;");
5242   verifyIndependentOfContext("int a /= b * c;");
5243   verifyIndependentOfContext("int a = *b;");
5244   verifyIndependentOfContext("int a = *b * c;");
5245   verifyIndependentOfContext("int a = b * *c;");
5246   verifyIndependentOfContext("int a = b * (10);");
5247   verifyIndependentOfContext("S << b * (10);");
5248   verifyIndependentOfContext("return 10 * b;");
5249   verifyIndependentOfContext("return *b * *c;");
5250   verifyIndependentOfContext("return a & ~b;");
5251   verifyIndependentOfContext("f(b ? *c : *d);");
5252   verifyIndependentOfContext("int a = b ? *c : *d;");
5253   verifyIndependentOfContext("*b = a;");
5254   verifyIndependentOfContext("a * ~b;");
5255   verifyIndependentOfContext("a * !b;");
5256   verifyIndependentOfContext("a * +b;");
5257   verifyIndependentOfContext("a * -b;");
5258   verifyIndependentOfContext("a * ++b;");
5259   verifyIndependentOfContext("a * --b;");
5260   verifyIndependentOfContext("a[4] * b;");
5261   verifyIndependentOfContext("a[a * a] = 1;");
5262   verifyIndependentOfContext("f() * b;");
5263   verifyIndependentOfContext("a * [self dostuff];");
5264   verifyIndependentOfContext("int x = a * (a + b);");
5265   verifyIndependentOfContext("(a *)(a + b);");
5266   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5267   verifyIndependentOfContext("int *pa = (int *)&a;");
5268   verifyIndependentOfContext("return sizeof(int **);");
5269   verifyIndependentOfContext("return sizeof(int ******);");
5270   verifyIndependentOfContext("return (int **&)a;");
5271   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5272   verifyFormat("void f(Type (*parameter)[10]) {}");
5273   verifyFormat("void f(Type (&parameter)[10]) {}");
5274   verifyGoogleFormat("return sizeof(int**);");
5275   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5276   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5277   verifyFormat("auto a = [](int **&, int ***) {};");
5278   verifyFormat("auto PointerBinding = [](const char *S) {};");
5279   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5280   verifyFormat("[](const decltype(*a) &value) {}");
5281   verifyFormat("decltype(a * b) F();");
5282   verifyFormat("#define MACRO() [](A *a) { return 1; }");
5283   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
5284   verifyIndependentOfContext("typedef void (*f)(int *a);");
5285   verifyIndependentOfContext("int i{a * b};");
5286   verifyIndependentOfContext("aaa && aaa->f();");
5287   verifyIndependentOfContext("int x = ~*p;");
5288   verifyFormat("Constructor() : a(a), area(width * height) {}");
5289   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5290   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5291   verifyFormat("void f() { f(a, c * d); }");
5292   verifyFormat("void f() { f(new a(), c * d); }");
5293   verifyFormat("void f(const MyOverride &override);");
5294   verifyFormat("void f(const MyFinal &final);");
5295   verifyIndependentOfContext("bool a = f() && override.f();");
5296   verifyIndependentOfContext("bool a = f() && final.f();");
5297 
5298   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5299 
5300   verifyIndependentOfContext("A<int *> a;");
5301   verifyIndependentOfContext("A<int **> a;");
5302   verifyIndependentOfContext("A<int *, int *> a;");
5303   verifyIndependentOfContext("A<int *[]> a;");
5304   verifyIndependentOfContext(
5305       "const char *const p = reinterpret_cast<const char *const>(q);");
5306   verifyIndependentOfContext("A<int **, int **> a;");
5307   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5308   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5309   verifyFormat("for (; a && b;) {\n}");
5310   verifyFormat("bool foo = true && [] { return false; }();");
5311 
5312   verifyFormat(
5313       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5314       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5315 
5316   verifyGoogleFormat("int const* a = &b;");
5317   verifyGoogleFormat("**outparam = 1;");
5318   verifyGoogleFormat("*outparam = a * b;");
5319   verifyGoogleFormat("int main(int argc, char** argv) {}");
5320   verifyGoogleFormat("A<int*> a;");
5321   verifyGoogleFormat("A<int**> a;");
5322   verifyGoogleFormat("A<int*, int*> a;");
5323   verifyGoogleFormat("A<int**, int**> a;");
5324   verifyGoogleFormat("f(b ? *c : *d);");
5325   verifyGoogleFormat("int a = b ? *c : *d;");
5326   verifyGoogleFormat("Type* t = **x;");
5327   verifyGoogleFormat("Type* t = *++*x;");
5328   verifyGoogleFormat("*++*x;");
5329   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5330   verifyGoogleFormat("Type* t = x++ * y;");
5331   verifyGoogleFormat(
5332       "const char* const p = reinterpret_cast<const char* const>(q);");
5333   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5334   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5335   verifyGoogleFormat("template <typename T>\n"
5336                      "void f(int i = 0, SomeType** temps = NULL);");
5337 
5338   FormatStyle Left = getLLVMStyle();
5339   Left.PointerAlignment = FormatStyle::PAS_Left;
5340   verifyFormat("x = *a(x) = *a(y);", Left);
5341   verifyFormat("for (;; *a = b) {\n}", Left);
5342   verifyFormat("return *this += 1;", Left);
5343 
5344   verifyIndependentOfContext("a = *(x + y);");
5345   verifyIndependentOfContext("a = &(x + y);");
5346   verifyIndependentOfContext("*(x + y).call();");
5347   verifyIndependentOfContext("&(x + y)->call();");
5348   verifyFormat("void f() { &(*I).first; }");
5349 
5350   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5351   verifyFormat(
5352       "int *MyValues = {\n"
5353       "    *A, // Operator detection might be confused by the '{'\n"
5354       "    *BB // Operator detection might be confused by previous comment\n"
5355       "};");
5356 
5357   verifyIndependentOfContext("if (int *a = &b)");
5358   verifyIndependentOfContext("if (int &a = *b)");
5359   verifyIndependentOfContext("if (a & b[i])");
5360   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5361   verifyIndependentOfContext("if (*b[i])");
5362   verifyIndependentOfContext("if (int *a = (&b))");
5363   verifyIndependentOfContext("while (int *a = &b)");
5364   verifyIndependentOfContext("size = sizeof *a;");
5365   verifyIndependentOfContext("if (a && (b = c))");
5366   verifyFormat("void f() {\n"
5367                "  for (const int &v : Values) {\n"
5368                "  }\n"
5369                "}");
5370   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5371   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5372   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5373 
5374   verifyFormat("#define A (!a * b)");
5375   verifyFormat("#define MACRO     \\\n"
5376                "  int *i = a * b; \\\n"
5377                "  void f(a *b);",
5378                getLLVMStyleWithColumns(19));
5379 
5380   verifyIndependentOfContext("A = new SomeType *[Length];");
5381   verifyIndependentOfContext("A = new SomeType *[Length]();");
5382   verifyIndependentOfContext("T **t = new T *;");
5383   verifyIndependentOfContext("T **t = new T *();");
5384   verifyGoogleFormat("A = new SomeType*[Length]();");
5385   verifyGoogleFormat("A = new SomeType*[Length];");
5386   verifyGoogleFormat("T** t = new T*;");
5387   verifyGoogleFormat("T** t = new T*();");
5388 
5389   FormatStyle PointerLeft = getLLVMStyle();
5390   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5391   verifyFormat("delete *x;", PointerLeft);
5392   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5393   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5394   verifyFormat("template <bool a, bool b> "
5395                "typename t::if<x && y>::type f() {}");
5396   verifyFormat("template <int *y> f() {}");
5397   verifyFormat("vector<int *> v;");
5398   verifyFormat("vector<int *const> v;");
5399   verifyFormat("vector<int *const **const *> v;");
5400   verifyFormat("vector<int *volatile> v;");
5401   verifyFormat("vector<a * b> v;");
5402   verifyFormat("foo<b && false>();");
5403   verifyFormat("foo<b & 1>();");
5404   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5405   verifyFormat(
5406       "template <class T, class = typename std::enable_if<\n"
5407       "                       std::is_integral<T>::value &&\n"
5408       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5409       "void F();",
5410       getLLVMStyleWithColumns(70));
5411   verifyFormat(
5412       "template <class T,\n"
5413       "          class = typename std::enable_if<\n"
5414       "              std::is_integral<T>::value &&\n"
5415       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
5416       "          class U>\n"
5417       "void F();",
5418       getLLVMStyleWithColumns(70));
5419   verifyFormat(
5420       "template <class T,\n"
5421       "          class = typename ::std::enable_if<\n"
5422       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5423       "void F();",
5424       getGoogleStyleWithColumns(68));
5425 
5426   verifyIndependentOfContext("MACRO(int *i);");
5427   verifyIndependentOfContext("MACRO(auto *a);");
5428   verifyIndependentOfContext("MACRO(const A *a);");
5429   verifyIndependentOfContext("MACRO(A *const a);");
5430   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5431   verifyFormat("void f() { f(float{1}, a * a); }");
5432   // FIXME: Is there a way to make this work?
5433   // verifyIndependentOfContext("MACRO(A *a);");
5434 
5435   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5436   verifyFormat("return options != nullptr && operator==(*options);");
5437 
5438   EXPECT_EQ("#define OP(x)                                    \\\n"
5439             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5440             "    return s << a.DebugString();                 \\\n"
5441             "  }",
5442             format("#define OP(x) \\\n"
5443                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5444                    "    return s << a.DebugString(); \\\n"
5445                    "  }",
5446                    getLLVMStyleWithColumns(50)));
5447 
5448   // FIXME: We cannot handle this case yet; we might be able to figure out that
5449   // foo<x> d > v; doesn't make sense.
5450   verifyFormat("foo<a<b && c> d> v;");
5451 
5452   FormatStyle PointerMiddle = getLLVMStyle();
5453   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5454   verifyFormat("delete *x;", PointerMiddle);
5455   verifyFormat("int * x;", PointerMiddle);
5456   verifyFormat("template <int * y> f() {}", PointerMiddle);
5457   verifyFormat("int * f(int * a) {}", PointerMiddle);
5458   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5459   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5460   verifyFormat("A<int *> a;", PointerMiddle);
5461   verifyFormat("A<int **> a;", PointerMiddle);
5462   verifyFormat("A<int *, int *> a;", PointerMiddle);
5463   verifyFormat("A<int * []> a;", PointerMiddle);
5464   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5465   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5466   verifyFormat("T ** t = new T *;", PointerMiddle);
5467 
5468   // Member function reference qualifiers aren't binary operators.
5469   verifyFormat("string // break\n"
5470                "operator()() & {}");
5471   verifyFormat("string // break\n"
5472                "operator()() && {}");
5473   verifyGoogleFormat("template <typename T>\n"
5474                      "auto x() & -> int {}");
5475 }
5476 
5477 TEST_F(FormatTest, UnderstandsAttributes) {
5478   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5479   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5480                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5481   FormatStyle AfterType = getLLVMStyle();
5482   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5483   verifyFormat("__attribute__((nodebug)) void\n"
5484                "foo() {}\n",
5485                AfterType);
5486 }
5487 
5488 TEST_F(FormatTest, UnderstandsEllipsis) {
5489   verifyFormat("int printf(const char *fmt, ...);");
5490   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5491   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5492 
5493   FormatStyle PointersLeft = getLLVMStyle();
5494   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5495   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5496 }
5497 
5498 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5499   EXPECT_EQ("int *a;\n"
5500             "int *a;\n"
5501             "int *a;",
5502             format("int *a;\n"
5503                    "int* a;\n"
5504                    "int *a;",
5505                    getGoogleStyle()));
5506   EXPECT_EQ("int* a;\n"
5507             "int* a;\n"
5508             "int* a;",
5509             format("int* a;\n"
5510                    "int* a;\n"
5511                    "int *a;",
5512                    getGoogleStyle()));
5513   EXPECT_EQ("int *a;\n"
5514             "int *a;\n"
5515             "int *a;",
5516             format("int *a;\n"
5517                    "int * a;\n"
5518                    "int *  a;",
5519                    getGoogleStyle()));
5520   EXPECT_EQ("auto x = [] {\n"
5521             "  int *a;\n"
5522             "  int *a;\n"
5523             "  int *a;\n"
5524             "};",
5525             format("auto x=[]{int *a;\n"
5526                    "int * a;\n"
5527                    "int *  a;};",
5528                    getGoogleStyle()));
5529 }
5530 
5531 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5532   verifyFormat("int f(int &&a) {}");
5533   verifyFormat("int f(int a, char &&b) {}");
5534   verifyFormat("void f() { int &&a = b; }");
5535   verifyGoogleFormat("int f(int a, char&& b) {}");
5536   verifyGoogleFormat("void f() { int&& a = b; }");
5537 
5538   verifyIndependentOfContext("A<int &&> a;");
5539   verifyIndependentOfContext("A<int &&, int &&> a;");
5540   verifyGoogleFormat("A<int&&> a;");
5541   verifyGoogleFormat("A<int&&, int&&> a;");
5542 
5543   // Not rvalue references:
5544   verifyFormat("template <bool B, bool C> class A {\n"
5545                "  static_assert(B && C, \"Something is wrong\");\n"
5546                "};");
5547   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5548   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5549   verifyFormat("#define A(a, b) (a && b)");
5550 }
5551 
5552 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5553   verifyFormat("void f() {\n"
5554                "  x[aaaaaaaaa -\n"
5555                "    b] = 23;\n"
5556                "}",
5557                getLLVMStyleWithColumns(15));
5558 }
5559 
5560 TEST_F(FormatTest, FormatsCasts) {
5561   verifyFormat("Type *A = static_cast<Type *>(P);");
5562   verifyFormat("Type *A = (Type *)P;");
5563   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5564   verifyFormat("int a = (int)(2.0f);");
5565   verifyFormat("int a = (int)2.0f;");
5566   verifyFormat("x[(int32)y];");
5567   verifyFormat("x = (int32)y;");
5568   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5569   verifyFormat("int a = (int)*b;");
5570   verifyFormat("int a = (int)2.0f;");
5571   verifyFormat("int a = (int)~0;");
5572   verifyFormat("int a = (int)++a;");
5573   verifyFormat("int a = (int)sizeof(int);");
5574   verifyFormat("int a = (int)+2;");
5575   verifyFormat("my_int a = (my_int)2.0f;");
5576   verifyFormat("my_int a = (my_int)sizeof(int);");
5577   verifyFormat("return (my_int)aaa;");
5578   verifyFormat("#define x ((int)-1)");
5579   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5580   verifyFormat("#define p(q) ((int *)&q)");
5581   verifyFormat("fn(a)(b) + 1;");
5582 
5583   verifyFormat("void f() { my_int a = (my_int)*b; }");
5584   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5585   verifyFormat("my_int a = (my_int)~0;");
5586   verifyFormat("my_int a = (my_int)++a;");
5587   verifyFormat("my_int a = (my_int)-2;");
5588   verifyFormat("my_int a = (my_int)1;");
5589   verifyFormat("my_int a = (my_int *)1;");
5590   verifyFormat("my_int a = (const my_int)-1;");
5591   verifyFormat("my_int a = (const my_int *)-1;");
5592   verifyFormat("my_int a = (my_int)(my_int)-1;");
5593   verifyFormat("my_int a = (ns::my_int)-2;");
5594   verifyFormat("case (my_int)ONE:");
5595   verifyFormat("auto x = (X)this;");
5596 
5597   // FIXME: single value wrapped with paren will be treated as cast.
5598   verifyFormat("void f(int i = (kValue)*kMask) {}");
5599 
5600   verifyFormat("{ (void)F; }");
5601 
5602   // Don't break after a cast's
5603   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5604                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5605                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5606 
5607   // These are not casts.
5608   verifyFormat("void f(int *) {}");
5609   verifyFormat("f(foo)->b;");
5610   verifyFormat("f(foo).b;");
5611   verifyFormat("f(foo)(b);");
5612   verifyFormat("f(foo)[b];");
5613   verifyFormat("[](foo) { return 4; }(bar);");
5614   verifyFormat("(*funptr)(foo)[4];");
5615   verifyFormat("funptrs[4](foo)[4];");
5616   verifyFormat("void f(int *);");
5617   verifyFormat("void f(int *) = 0;");
5618   verifyFormat("void f(SmallVector<int>) {}");
5619   verifyFormat("void f(SmallVector<int>);");
5620   verifyFormat("void f(SmallVector<int>) = 0;");
5621   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5622   verifyFormat("int a = sizeof(int) * b;");
5623   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5624   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5625   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5626   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5627 
5628   // These are not casts, but at some point were confused with casts.
5629   verifyFormat("virtual void foo(int *) override;");
5630   verifyFormat("virtual void foo(char &) const;");
5631   verifyFormat("virtual void foo(int *a, char *) const;");
5632   verifyFormat("int a = sizeof(int *) + b;");
5633   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5634   verifyFormat("bool b = f(g<int>) && c;");
5635   verifyFormat("typedef void (*f)(int i) func;");
5636 
5637   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5638                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5639   // FIXME: The indentation here is not ideal.
5640   verifyFormat(
5641       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5642       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5643       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5644 }
5645 
5646 TEST_F(FormatTest, FormatsFunctionTypes) {
5647   verifyFormat("A<bool()> a;");
5648   verifyFormat("A<SomeType()> a;");
5649   verifyFormat("A<void (*)(int, std::string)> a;");
5650   verifyFormat("A<void *(int)>;");
5651   verifyFormat("void *(*a)(int *, SomeType *);");
5652   verifyFormat("int (*func)(void *);");
5653   verifyFormat("void f() { int (*func)(void *); }");
5654   verifyFormat("template <class CallbackClass>\n"
5655                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5656 
5657   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5658   verifyGoogleFormat("void* (*a)(int);");
5659   verifyGoogleFormat(
5660       "template <class CallbackClass>\n"
5661       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5662 
5663   // Other constructs can look somewhat like function types:
5664   verifyFormat("A<sizeof(*x)> a;");
5665   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5666   verifyFormat("some_var = function(*some_pointer_var)[0];");
5667   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5668   verifyFormat("int x = f(&h)();");
5669   verifyFormat("returnsFunction(&param1, &param2)(param);");
5670   verifyFormat("std::function<\n"
5671                "    LooooooooooongTemplatedType<\n"
5672                "        SomeType>*(\n"
5673                "        LooooooooooooooooongType type)>\n"
5674                "    function;",
5675                getGoogleStyleWithColumns(40));
5676 }
5677 
5678 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5679   verifyFormat("A (*foo_)[6];");
5680   verifyFormat("vector<int> (*foo_)[6];");
5681 }
5682 
5683 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5684   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5685                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5686   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5687                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5688   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5689                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5690 
5691   // Different ways of ()-initializiation.
5692   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5693                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5694   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5695                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5696   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5697                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5698   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5699                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5700 
5701   // Lambdas should not confuse the variable declaration heuristic.
5702   verifyFormat("LooooooooooooooooongType\n"
5703                "    variable(nullptr, [](A *a) {});",
5704                getLLVMStyleWithColumns(40));
5705 }
5706 
5707 TEST_F(FormatTest, BreaksLongDeclarations) {
5708   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5709                "    AnotherNameForTheLongType;");
5710   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5711                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5712   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5713                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5714   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5715                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5716   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5717                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5718   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5719                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5720   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5721                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5722   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5723                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5724   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5725                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
5726   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5727                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
5728   FormatStyle Indented = getLLVMStyle();
5729   Indented.IndentWrappedFunctionNames = true;
5730   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5731                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5732                Indented);
5733   verifyFormat(
5734       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5735       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5736       Indented);
5737   verifyFormat(
5738       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5739       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5740       Indented);
5741   verifyFormat(
5742       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5743       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5744       Indented);
5745 
5746   // FIXME: Without the comment, this breaks after "(".
5747   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5748                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5749                getGoogleStyle());
5750 
5751   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5752                "                  int LoooooooooooooooooooongParam2) {}");
5753   verifyFormat(
5754       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5755       "                                   SourceLocation L, IdentifierIn *II,\n"
5756       "                                   Type *T) {}");
5757   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5758                "ReallyReaaallyLongFunctionName(\n"
5759                "    const std::string &SomeParameter,\n"
5760                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5761                "        &ReallyReallyLongParameterName,\n"
5762                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5763                "        &AnotherLongParameterName) {}");
5764   verifyFormat("template <typename A>\n"
5765                "SomeLoooooooooooooooooooooongType<\n"
5766                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5767                "Function() {}");
5768 
5769   verifyGoogleFormat(
5770       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5771       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5772   verifyGoogleFormat(
5773       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5774       "                                   SourceLocation L) {}");
5775   verifyGoogleFormat(
5776       "some_namespace::LongReturnType\n"
5777       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5778       "    int first_long_parameter, int second_parameter) {}");
5779 
5780   verifyGoogleFormat("template <typename T>\n"
5781                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5782                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5783   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5784                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5785 
5786   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5787                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5788                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5789   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5790                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5791                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5792   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5793                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5794                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5795                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5796 
5797   verifyFormat("template <typename T> // Templates on own line.\n"
5798                "static int            // Some comment.\n"
5799                "MyFunction(int a);",
5800                getLLVMStyle());
5801 }
5802 
5803 TEST_F(FormatTest, FormatsArrays) {
5804   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5805                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5806   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
5807                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
5808   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5809                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
5810   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5811                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5812   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5813                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5814   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5815                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5816                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5817   verifyFormat(
5818       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5819       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5820       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5821   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
5822                "    .aaaaaaaaaaaaaaaaaaaaaa();");
5823 
5824   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5825                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5826   verifyFormat(
5827       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5828       "                                  .aaaaaaa[0]\n"
5829       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5830   verifyFormat("a[::b::c];");
5831 
5832   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5833 
5834   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
5835   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
5836 }
5837 
5838 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5839   verifyFormat("(a)->b();");
5840   verifyFormat("--a;");
5841 }
5842 
5843 TEST_F(FormatTest, HandlesIncludeDirectives) {
5844   verifyFormat("#include <string>\n"
5845                "#include <a/b/c.h>\n"
5846                "#include \"a/b/string\"\n"
5847                "#include \"string.h\"\n"
5848                "#include \"string.h\"\n"
5849                "#include <a-a>\n"
5850                "#include < path with space >\n"
5851                "#include_next <test.h>"
5852                "#include \"abc.h\" // this is included for ABC\n"
5853                "#include \"some long include\" // with a comment\n"
5854                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5855                getLLVMStyleWithColumns(35));
5856   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5857   EXPECT_EQ("#include <a>", format("#include<a>"));
5858 
5859   verifyFormat("#import <string>");
5860   verifyFormat("#import <a/b/c.h>");
5861   verifyFormat("#import \"a/b/string\"");
5862   verifyFormat("#import \"string.h\"");
5863   verifyFormat("#import \"string.h\"");
5864   verifyFormat("#if __has_include(<strstream>)\n"
5865                "#include <strstream>\n"
5866                "#endif");
5867 
5868   verifyFormat("#define MY_IMPORT <a/b>");
5869 
5870   verifyFormat("#if __has_include(<a/b>)");
5871   verifyFormat("#if __has_include_next(<a/b>)");
5872   verifyFormat("#define F __has_include(<a/b>)");
5873   verifyFormat("#define F __has_include_next(<a/b>)");
5874 
5875   // Protocol buffer definition or missing "#".
5876   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5877                getLLVMStyleWithColumns(30));
5878 
5879   FormatStyle Style = getLLVMStyle();
5880   Style.AlwaysBreakBeforeMultilineStrings = true;
5881   Style.ColumnLimit = 0;
5882   verifyFormat("#import \"abc.h\"", Style);
5883 
5884   // But 'import' might also be a regular C++ namespace.
5885   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5886                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5887 }
5888 
5889 //===----------------------------------------------------------------------===//
5890 // Error recovery tests.
5891 //===----------------------------------------------------------------------===//
5892 
5893 TEST_F(FormatTest, IncompleteParameterLists) {
5894   FormatStyle NoBinPacking = getLLVMStyle();
5895   NoBinPacking.BinPackParameters = false;
5896   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5897                "                        double *min_x,\n"
5898                "                        double *max_x,\n"
5899                "                        double *min_y,\n"
5900                "                        double *max_y,\n"
5901                "                        double *min_z,\n"
5902                "                        double *max_z, ) {}",
5903                NoBinPacking);
5904 }
5905 
5906 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5907   verifyFormat("void f() { return; }\n42");
5908   verifyFormat("void f() {\n"
5909                "  if (0)\n"
5910                "    return;\n"
5911                "}\n"
5912                "42");
5913   verifyFormat("void f() { return }\n42");
5914   verifyFormat("void f() {\n"
5915                "  if (0)\n"
5916                "    return\n"
5917                "}\n"
5918                "42");
5919 }
5920 
5921 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5922   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5923   EXPECT_EQ("void f() {\n"
5924             "  if (a)\n"
5925             "    return\n"
5926             "}",
5927             format("void  f  (  )  {  if  ( a )  return  }"));
5928   EXPECT_EQ("namespace N {\n"
5929             "void f()\n"
5930             "}",
5931             format("namespace  N  {  void f()  }"));
5932   EXPECT_EQ("namespace N {\n"
5933             "void f() {}\n"
5934             "void g()\n"
5935             "} // namespace N",
5936             format("namespace N  { void f( ) { } void g( ) }"));
5937 }
5938 
5939 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5940   verifyFormat("int aaaaaaaa =\n"
5941                "    // Overlylongcomment\n"
5942                "    b;",
5943                getLLVMStyleWithColumns(20));
5944   verifyFormat("function(\n"
5945                "    ShortArgument,\n"
5946                "    LoooooooooooongArgument);\n",
5947                getLLVMStyleWithColumns(20));
5948 }
5949 
5950 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5951   verifyFormat("public:");
5952   verifyFormat("class A {\n"
5953                "public\n"
5954                "  void f() {}\n"
5955                "};");
5956   verifyFormat("public\n"
5957                "int qwerty;");
5958   verifyFormat("public\n"
5959                "B {}");
5960   verifyFormat("public\n"
5961                "{}");
5962   verifyFormat("public\n"
5963                "B { int x; }");
5964 }
5965 
5966 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5967   verifyFormat("{");
5968   verifyFormat("#})");
5969   verifyNoCrash("(/**/[:!] ?[).");
5970 }
5971 
5972 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5973   verifyFormat("do {\n}");
5974   verifyFormat("do {\n}\n"
5975                "f();");
5976   verifyFormat("do {\n}\n"
5977                "wheeee(fun);");
5978   verifyFormat("do {\n"
5979                "  f();\n"
5980                "}");
5981 }
5982 
5983 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5984   verifyFormat("if {\n  foo;\n  foo();\n}");
5985   verifyFormat("switch {\n  foo;\n  foo();\n}");
5986   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
5987   verifyFormat("while {\n  foo;\n  foo();\n}");
5988   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5989 }
5990 
5991 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5992   verifyIncompleteFormat("namespace {\n"
5993                          "class Foo { Foo (\n"
5994                          "};\n"
5995                          "} // namespace");
5996 }
5997 
5998 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5999   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
6000   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
6001   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
6002   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
6003 
6004   EXPECT_EQ("{\n"
6005             "  {\n"
6006             "    breakme(\n"
6007             "        qwe);\n"
6008             "  }\n",
6009             format("{\n"
6010                    "    {\n"
6011                    " breakme(qwe);\n"
6012                    "}\n",
6013                    getLLVMStyleWithColumns(10)));
6014 }
6015 
6016 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6017   verifyFormat("int x = {\n"
6018                "    avariable,\n"
6019                "    b(alongervariable)};",
6020                getLLVMStyleWithColumns(25));
6021 }
6022 
6023 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6024   verifyFormat("return (a)(b){1, 2, 3};");
6025 }
6026 
6027 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6028   verifyFormat("vector<int> x{1, 2, 3, 4};");
6029   verifyFormat("vector<int> x{\n"
6030                "    1,\n"
6031                "    2,\n"
6032                "    3,\n"
6033                "    4,\n"
6034                "};");
6035   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6036   verifyFormat("f({1, 2});");
6037   verifyFormat("auto v = Foo{-1};");
6038   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6039   verifyFormat("Class::Class : member{1, 2, 3} {}");
6040   verifyFormat("new vector<int>{1, 2, 3};");
6041   verifyFormat("new int[3]{1, 2, 3};");
6042   verifyFormat("new int{1};");
6043   verifyFormat("return {arg1, arg2};");
6044   verifyFormat("return {arg1, SomeType{parameter}};");
6045   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6046   verifyFormat("new T{arg1, arg2};");
6047   verifyFormat("f(MyMap[{composite, key}]);");
6048   verifyFormat("class Class {\n"
6049                "  T member = {arg1, arg2};\n"
6050                "};");
6051   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6052   verifyFormat("const struct A a = {.a = 1, .b = 2};");
6053   verifyFormat("const struct A a = {[0] = 1, [1] = 2};");
6054   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6055   verifyFormat("int a = std::is_integral<int>{} + 0;");
6056 
6057   verifyFormat("int foo(int i) { return fo1{}(i); }");
6058   verifyFormat("int foo(int i) { return fo1{}(i); }");
6059   verifyFormat("auto i = decltype(x){};");
6060   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6061   verifyFormat("Node n{1, Node{1000}, //\n"
6062                "       2};");
6063   verifyFormat("Aaaa aaaaaaa{\n"
6064                "    {\n"
6065                "        aaaa,\n"
6066                "    },\n"
6067                "};");
6068   verifyFormat("class C : public D {\n"
6069                "  SomeClass SC{2};\n"
6070                "};");
6071   verifyFormat("class C : public A {\n"
6072                "  class D : public B {\n"
6073                "    void f() { int i{2}; }\n"
6074                "  };\n"
6075                "};");
6076   verifyFormat("#define A {a, a},");
6077 
6078   // Binpacking only if there is no trailing comma
6079   verifyFormat("const Aaaaaa aaaaa = {aaaaaaaaaa, bbbbbbbbbb,\n"
6080                "                      cccccccccc, dddddddddd};",
6081 			   getLLVMStyleWithColumns(50));
6082   verifyFormat("const Aaaaaa aaaaa = {\n"
6083                "    aaaaaaaaaaa,\n"
6084                "    bbbbbbbbbbb,\n"
6085                "    ccccccccccc,\n"
6086                "    ddddddddddd,\n"
6087                "};", getLLVMStyleWithColumns(50));
6088 
6089   // Cases where distinguising braced lists and blocks is hard.
6090   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
6091   verifyFormat("void f() {\n"
6092                "  return; // comment\n"
6093                "}\n"
6094                "SomeType t;");
6095   verifyFormat("void f() {\n"
6096                "  if (a) {\n"
6097                "    f();\n"
6098                "  }\n"
6099                "}\n"
6100                "SomeType t;");
6101 
6102   // In combination with BinPackArguments = false.
6103   FormatStyle NoBinPacking = getLLVMStyle();
6104   NoBinPacking.BinPackArguments = false;
6105   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6106                "                      bbbbb,\n"
6107                "                      ccccc,\n"
6108                "                      ddddd,\n"
6109                "                      eeeee,\n"
6110                "                      ffffff,\n"
6111                "                      ggggg,\n"
6112                "                      hhhhhh,\n"
6113                "                      iiiiii,\n"
6114                "                      jjjjjj,\n"
6115                "                      kkkkkk};",
6116                NoBinPacking);
6117   verifyFormat("const Aaaaaa aaaaa = {\n"
6118                "    aaaaa,\n"
6119                "    bbbbb,\n"
6120                "    ccccc,\n"
6121                "    ddddd,\n"
6122                "    eeeee,\n"
6123                "    ffffff,\n"
6124                "    ggggg,\n"
6125                "    hhhhhh,\n"
6126                "    iiiiii,\n"
6127                "    jjjjjj,\n"
6128                "    kkkkkk,\n"
6129                "};",
6130                NoBinPacking);
6131   verifyFormat(
6132       "const Aaaaaa aaaaa = {\n"
6133       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6134       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6135       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6136       "};",
6137       NoBinPacking);
6138 
6139   // FIXME: The alignment of these trailing comments might be bad. Then again,
6140   // this might be utterly useless in real code.
6141   verifyFormat("Constructor::Constructor()\n"
6142                "    : some_value{         //\n"
6143                "                 aaaaaaa, //\n"
6144                "                 bbbbbbb} {}");
6145 
6146   // In braced lists, the first comment is always assumed to belong to the
6147   // first element. Thus, it can be moved to the next or previous line as
6148   // appropriate.
6149   EXPECT_EQ("function({// First element:\n"
6150             "          1,\n"
6151             "          // Second element:\n"
6152             "          2});",
6153             format("function({\n"
6154                    "    // First element:\n"
6155                    "    1,\n"
6156                    "    // Second element:\n"
6157                    "    2});"));
6158   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6159             "    // First element:\n"
6160             "    1,\n"
6161             "    // Second element:\n"
6162             "    2};",
6163             format("std::vector<int> MyNumbers{// First element:\n"
6164                    "                           1,\n"
6165                    "                           // Second element:\n"
6166                    "                           2};",
6167                    getLLVMStyleWithColumns(30)));
6168   // A trailing comma should still lead to an enforced line break and no
6169   // binpacking.
6170   EXPECT_EQ("vector<int> SomeVector = {\n"
6171             "    // aaa\n"
6172             "    1,\n"
6173             "    2,\n"
6174             "};",
6175             format("vector<int> SomeVector = { // aaa\n"
6176                    "    1, 2, };"));
6177 
6178   FormatStyle ExtraSpaces = getLLVMStyle();
6179   ExtraSpaces.Cpp11BracedListStyle = false;
6180   ExtraSpaces.ColumnLimit = 75;
6181   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6182   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6183   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6184   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6185   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6186   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6187   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6188   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6189   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6190   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6191   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6192   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6193   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6194   verifyFormat("class Class {\n"
6195                "  T member = { arg1, arg2 };\n"
6196                "};",
6197                ExtraSpaces);
6198   verifyFormat(
6199       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6200       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6201       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6202       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6203       ExtraSpaces);
6204   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6205   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6206                ExtraSpaces);
6207   verifyFormat(
6208       "someFunction(OtherParam,\n"
6209       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6210       "                         param1, param2,\n"
6211       "                         // comment 2\n"
6212       "                         param3, param4 });",
6213       ExtraSpaces);
6214   verifyFormat(
6215       "std::this_thread::sleep_for(\n"
6216       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6217       ExtraSpaces);
6218   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
6219                "    aaaaaaa,\n"
6220                "    aaaaaaaaaa,\n"
6221                "    aaaaa,\n"
6222                "    aaaaaaaaaaaaaaa,\n"
6223                "    aaa,\n"
6224                "    aaaaaaaaaa,\n"
6225                "    a,\n"
6226                "    aaaaaaaaaaaaaaaaaaaaa,\n"
6227                "    aaaaaaaaaaaa,\n"
6228                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
6229                "    aaaaaaa,\n"
6230                "    a};");
6231   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6232   verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces);
6233   verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces);
6234 }
6235 
6236 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6237   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6238                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6239                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6240                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6241                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6242                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6243   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
6244                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6245                "                 1, 22, 333, 4444, 55555, //\n"
6246                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6247                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6248   verifyFormat(
6249       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6250       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6251       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6252       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6253       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6254       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6255       "                 7777777};");
6256   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6257                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6258                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6259   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6260                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6261                "    // Separating comment.\n"
6262                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
6263   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6264                "    // Leading comment\n"
6265                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6266                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6267   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6268                "                 1, 1, 1, 1};",
6269                getLLVMStyleWithColumns(39));
6270   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6271                "                 1, 1, 1, 1};",
6272                getLLVMStyleWithColumns(38));
6273   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6274                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6275                getLLVMStyleWithColumns(43));
6276   verifyFormat(
6277       "static unsigned SomeValues[10][3] = {\n"
6278       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
6279       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
6280   verifyFormat("static auto fields = new vector<string>{\n"
6281                "    \"aaaaaaaaaaaaa\",\n"
6282                "    \"aaaaaaaaaaaaa\",\n"
6283                "    \"aaaaaaaaaaaa\",\n"
6284                "    \"aaaaaaaaaaaaaa\",\n"
6285                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6286                "    \"aaaaaaaaaaaa\",\n"
6287                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
6288                "};");
6289   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
6290   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
6291                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
6292                "                 3, cccccccccccccccccccccc};",
6293                getLLVMStyleWithColumns(60));
6294 
6295   // Trailing commas.
6296   verifyFormat("vector<int> x = {\n"
6297                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6298                "};",
6299                getLLVMStyleWithColumns(39));
6300   verifyFormat("vector<int> x = {\n"
6301                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6302                "};",
6303                getLLVMStyleWithColumns(39));
6304   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6305                "                 1, 1, 1, 1,\n"
6306                "                 /**/ /**/};",
6307                getLLVMStyleWithColumns(39));
6308 
6309   // Trailing comment in the first line.
6310   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
6311                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
6312                "    111111111,  222222222,  3333333333,  444444444,  //\n"
6313                "    11111111,   22222222,   333333333,   44444444};");
6314   // Trailing comment in the last line.
6315   verifyFormat("int aaaaa[] = {\n"
6316                "    1, 2, 3, // comment\n"
6317                "    4, 5, 6  // comment\n"
6318                "};");
6319 
6320   // With nested lists, we should either format one item per line or all nested
6321   // lists one on line.
6322   // FIXME: For some nested lists, we can do better.
6323   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6324                "        {aaaaaaaaaaaaaaaaaaa},\n"
6325                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6326                "        {aaaaaaaaaaaaaaaaa}};",
6327                getLLVMStyleWithColumns(60));
6328   verifyFormat(
6329       "SomeStruct my_struct_array = {\n"
6330       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6331       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6332       "    {aaa, aaa},\n"
6333       "    {aaa, aaa},\n"
6334       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6335       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6336       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6337 
6338   // No column layout should be used here.
6339   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6340                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6341 
6342   verifyNoCrash("a<,");
6343 
6344   // No braced initializer here.
6345   verifyFormat("void f() {\n"
6346                "  struct Dummy {};\n"
6347                "  f(v);\n"
6348                "}");
6349 
6350   // Long lists should be formatted in columns even if they are nested.
6351   verifyFormat(
6352       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6353       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6354       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6355       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6356       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6357       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
6358 
6359   // Allow "single-column" layout even if that violates the column limit. There
6360   // isn't going to be a better way.
6361   verifyFormat("std::vector<int> a = {\n"
6362                "    aaaaaaaa,\n"
6363                "    aaaaaaaa,\n"
6364                "    aaaaaaaa,\n"
6365                "    aaaaaaaa,\n"
6366                "    aaaaaaaaaa,\n"
6367                "    aaaaaaaa,\n"
6368                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
6369                getLLVMStyleWithColumns(30));
6370   verifyFormat("vector<int> aaaa = {\n"
6371                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6372                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6373                "    aaaaaa.aaaaaaa,\n"
6374                "    aaaaaa.aaaaaaa,\n"
6375                "    aaaaaa.aaaaaaa,\n"
6376                "    aaaaaa.aaaaaaa,\n"
6377                "};");
6378 
6379   // Don't create hanging lists.
6380   verifyFormat("someFunction(Param, {List1, List2,\n"
6381                "                     List3});",
6382                getLLVMStyleWithColumns(35));
6383   verifyFormat("someFunction(Param, Param,\n"
6384                "             {List1, List2,\n"
6385                "              List3});",
6386                getLLVMStyleWithColumns(35));
6387   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
6388                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
6389 }
6390 
6391 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6392   FormatStyle DoNotMerge = getLLVMStyle();
6393   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6394 
6395   verifyFormat("void f() { return 42; }");
6396   verifyFormat("void f() {\n"
6397                "  return 42;\n"
6398                "}",
6399                DoNotMerge);
6400   verifyFormat("void f() {\n"
6401                "  // Comment\n"
6402                "}");
6403   verifyFormat("{\n"
6404                "#error {\n"
6405                "  int a;\n"
6406                "}");
6407   verifyFormat("{\n"
6408                "  int a;\n"
6409                "#error {\n"
6410                "}");
6411   verifyFormat("void f() {} // comment");
6412   verifyFormat("void f() { int a; } // comment");
6413   verifyFormat("void f() {\n"
6414                "} // comment",
6415                DoNotMerge);
6416   verifyFormat("void f() {\n"
6417                "  int a;\n"
6418                "} // comment",
6419                DoNotMerge);
6420   verifyFormat("void f() {\n"
6421                "} // comment",
6422                getLLVMStyleWithColumns(15));
6423 
6424   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6425   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6426 
6427   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6428   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6429   verifyFormat("class C {\n"
6430                "  C()\n"
6431                "      : iiiiiiii(nullptr),\n"
6432                "        kkkkkkk(nullptr),\n"
6433                "        mmmmmmm(nullptr),\n"
6434                "        nnnnnnn(nullptr) {}\n"
6435                "};",
6436                getGoogleStyle());
6437 
6438   FormatStyle NoColumnLimit = getLLVMStyle();
6439   NoColumnLimit.ColumnLimit = 0;
6440   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6441   EXPECT_EQ("class C {\n"
6442             "  A() : b(0) {}\n"
6443             "};",
6444             format("class C{A():b(0){}};", NoColumnLimit));
6445   EXPECT_EQ("A()\n"
6446             "    : b(0) {\n"
6447             "}",
6448             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6449 
6450   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6451   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6452       FormatStyle::SFS_None;
6453   EXPECT_EQ("A()\n"
6454             "    : b(0) {\n"
6455             "}",
6456             format("A():b(0){}", DoNotMergeNoColumnLimit));
6457   EXPECT_EQ("A()\n"
6458             "    : b(0) {\n"
6459             "}",
6460             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6461 
6462   verifyFormat("#define A          \\\n"
6463                "  void f() {       \\\n"
6464                "    int i;         \\\n"
6465                "  }",
6466                getLLVMStyleWithColumns(20));
6467   verifyFormat("#define A           \\\n"
6468                "  void f() { int i; }",
6469                getLLVMStyleWithColumns(21));
6470   verifyFormat("#define A            \\\n"
6471                "  void f() {         \\\n"
6472                "    int i;           \\\n"
6473                "  }                  \\\n"
6474                "  int j;",
6475                getLLVMStyleWithColumns(22));
6476   verifyFormat("#define A             \\\n"
6477                "  void f() { int i; } \\\n"
6478                "  int j;",
6479                getLLVMStyleWithColumns(23));
6480 }
6481 
6482 TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
6483   FormatStyle MergeEmptyOnly = getLLVMStyle();
6484   MergeEmptyOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6485   verifyFormat("class C {\n"
6486                "  int f() {}\n"
6487                "};",
6488                MergeEmptyOnly);
6489   verifyFormat("class C {\n"
6490                "  int f() {\n"
6491                "    return 42;\n"
6492                "  }\n"
6493                "};",
6494                MergeEmptyOnly);
6495   verifyFormat("int f() {}", MergeEmptyOnly);
6496   verifyFormat("int f() {\n"
6497                "  return 42;\n"
6498                "}",
6499                MergeEmptyOnly);
6500 
6501   // Also verify behavior when BraceWrapping.AfterFunction = true
6502   MergeEmptyOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6503   MergeEmptyOnly.BraceWrapping.AfterFunction = true;
6504   verifyFormat("int f() {}", MergeEmptyOnly);
6505   verifyFormat("class C {\n"
6506                "  int f() {}\n"
6507                "};",
6508                MergeEmptyOnly);
6509 }
6510 
6511 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6512   FormatStyle MergeInlineOnly = getLLVMStyle();
6513   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6514   verifyFormat("class C {\n"
6515                "  int f() { return 42; }\n"
6516                "};",
6517                MergeInlineOnly);
6518   verifyFormat("int f() {\n"
6519                "  return 42;\n"
6520                "}",
6521                MergeInlineOnly);
6522 
6523   // SFS_Inline implies SFS_Empty
6524   verifyFormat("class C {\n"
6525                "  int f() {}\n"
6526                "};",
6527                MergeInlineOnly);
6528   verifyFormat("int f() {}", MergeInlineOnly);
6529 
6530   // Also verify behavior when BraceWrapping.AfterFunction = true
6531   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6532   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6533   verifyFormat("class C {\n"
6534                "  int f() { return 42; }\n"
6535                "};",
6536                MergeInlineOnly);
6537   verifyFormat("int f()\n"
6538                "{\n"
6539                "  return 42;\n"
6540                "}",
6541                MergeInlineOnly);
6542 
6543   // SFS_Inline implies SFS_Empty
6544   verifyFormat("int f() {}", MergeInlineOnly);
6545   verifyFormat("class C {\n"
6546                "  int f() {}\n"
6547                "};",
6548                MergeInlineOnly);
6549 }
6550 
6551 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {
6552   FormatStyle MergeInlineOnly = getLLVMStyle();
6553   MergeInlineOnly.AllowShortFunctionsOnASingleLine =
6554       FormatStyle::SFS_InlineOnly;
6555   verifyFormat("class C {\n"
6556                "  int f() { return 42; }\n"
6557                "};",
6558                MergeInlineOnly);
6559   verifyFormat("int f() {\n"
6560                "  return 42;\n"
6561                "}",
6562                MergeInlineOnly);
6563 
6564   // SFS_InlineOnly does not imply SFS_Empty
6565   verifyFormat("class C {\n"
6566                "  int f() {}\n"
6567                "};",
6568                MergeInlineOnly);
6569   verifyFormat("int f() {\n"
6570                "}",
6571                MergeInlineOnly);
6572 
6573   // Also verify behavior when BraceWrapping.AfterFunction = true
6574   MergeInlineOnly.BreakBeforeBraces = FormatStyle::BS_Custom;
6575   MergeInlineOnly.BraceWrapping.AfterFunction = true;
6576   verifyFormat("class C {\n"
6577                "  int f() { return 42; }\n"
6578                "};",
6579                MergeInlineOnly);
6580   verifyFormat("int f()\n"
6581                "{\n"
6582                "  return 42;\n"
6583                "}",
6584                MergeInlineOnly);
6585 
6586   // SFS_InlineOnly does not imply SFS_Empty
6587   verifyFormat("int f()\n"
6588                "{\n"
6589                "}",
6590                MergeInlineOnly);
6591   verifyFormat("class C {\n"
6592                "  int f() {}\n"
6593                "};",
6594                MergeInlineOnly);
6595 }
6596 
6597 TEST_F(FormatTest, SplitEmptyFunction) {
6598   FormatStyle Style = getLLVMStyle();
6599   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6600   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6601   Style.BraceWrapping.AfterFunction = true;
6602   Style.BraceWrapping.SplitEmptyFunction = false;
6603   Style.ColumnLimit = 40;
6604 
6605   verifyFormat("int f()\n"
6606                "{}",
6607                Style);
6608   verifyFormat("int f()\n"
6609                "{\n"
6610                "  return 42;\n"
6611                "}",
6612                Style);
6613   verifyFormat("int f()\n"
6614                "{\n"
6615                "  // some comment\n"
6616                "}",
6617                Style);
6618 
6619   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Empty;
6620   verifyFormat("int f() {}", Style);
6621   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6622                "{}",
6623                Style);
6624   verifyFormat("int f()\n"
6625                "{\n"
6626                "  return 0;\n"
6627                "}",
6628                Style);
6629 
6630   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6631   verifyFormat("class Foo {\n"
6632                "  int f() {}\n"
6633                "};\n",
6634                Style);
6635   verifyFormat("class Foo {\n"
6636                "  int f() { return 0; }\n"
6637                "};\n",
6638                Style);
6639   verifyFormat("class Foo {\n"
6640                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6641                "  {}\n"
6642                "};\n",
6643                Style);
6644   verifyFormat("class Foo {\n"
6645                "  int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6646                "  {\n"
6647                "    return 0;\n"
6648                "  }\n"
6649                "};\n",
6650                Style);
6651 
6652   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
6653   verifyFormat("int f() {}", Style);
6654   verifyFormat("int f() { return 0; }", Style);
6655   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6656                "{}",
6657                Style);
6658   verifyFormat("int aaaaaaaaaaaaaa(int bbbbbbbbbbbbbb)\n"
6659                "{\n"
6660                "  return 0;\n"
6661                "}",
6662                Style);
6663 }
6664 
6665 TEST_F(FormatTest, SplitEmptyClass) {
6666   FormatStyle Style = getLLVMStyle();
6667   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6668   Style.BraceWrapping.AfterClass = true;
6669   Style.BraceWrapping.SplitEmptyRecord = false;
6670 
6671   verifyFormat("class Foo\n"
6672                "{};",
6673                Style);
6674   verifyFormat("/* something */ class Foo\n"
6675                "{};",
6676                Style);
6677   verifyFormat("template <typename X> class Foo\n"
6678                "{};",
6679                Style);
6680   verifyFormat("class Foo\n"
6681                "{\n"
6682                "  Foo();\n"
6683                "};",
6684                Style);
6685   verifyFormat("typedef class Foo\n"
6686                "{\n"
6687                "} Foo_t;",
6688                Style);
6689 }
6690 
6691 TEST_F(FormatTest, SplitEmptyStruct) {
6692   FormatStyle Style = getLLVMStyle();
6693   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6694   Style.BraceWrapping.AfterStruct = true;
6695   Style.BraceWrapping.SplitEmptyRecord = false;
6696 
6697   verifyFormat("struct Foo\n"
6698                "{};",
6699                Style);
6700   verifyFormat("/* something */ struct Foo\n"
6701                "{};",
6702                Style);
6703   verifyFormat("template <typename X> struct Foo\n"
6704                "{};",
6705                Style);
6706   verifyFormat("struct Foo\n"
6707                "{\n"
6708                "  Foo();\n"
6709                "};",
6710                Style);
6711   verifyFormat("typedef struct Foo\n"
6712                "{\n"
6713                "} Foo_t;",
6714                Style);
6715   //typedef struct Bar {} Bar_t;
6716 }
6717 
6718 TEST_F(FormatTest, SplitEmptyUnion) {
6719   FormatStyle Style = getLLVMStyle();
6720   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6721   Style.BraceWrapping.AfterUnion = true;
6722   Style.BraceWrapping.SplitEmptyRecord = false;
6723 
6724   verifyFormat("union Foo\n"
6725                "{};",
6726                Style);
6727   verifyFormat("/* something */ union Foo\n"
6728                "{};",
6729                Style);
6730   verifyFormat("union Foo\n"
6731                "{\n"
6732                "  A,\n"
6733                "};",
6734                Style);
6735   verifyFormat("typedef union Foo\n"
6736                "{\n"
6737                "} Foo_t;",
6738                Style);
6739 }
6740 
6741 TEST_F(FormatTest, SplitEmptyNamespace) {
6742   FormatStyle Style = getLLVMStyle();
6743   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6744   Style.BraceWrapping.AfterNamespace = true;
6745   Style.BraceWrapping.SplitEmptyNamespace = false;
6746 
6747   verifyFormat("namespace Foo\n"
6748                "{};",
6749                Style);
6750   verifyFormat("/* something */ namespace Foo\n"
6751                "{};",
6752                Style);
6753   verifyFormat("inline namespace Foo\n"
6754                "{};",
6755                Style);
6756   verifyFormat("namespace Foo\n"
6757                "{\n"
6758                "void Bar();\n"
6759                "};",
6760                Style);
6761 }
6762 
6763 TEST_F(FormatTest, NeverMergeShortRecords) {
6764   FormatStyle Style = getLLVMStyle();
6765 
6766   verifyFormat("class Foo {\n"
6767                "  Foo();\n"
6768                "};",
6769                Style);
6770   verifyFormat("typedef class Foo {\n"
6771                "  Foo();\n"
6772                "} Foo_t;",
6773                Style);
6774   verifyFormat("struct Foo {\n"
6775                "  Foo();\n"
6776                "};",
6777                Style);
6778   verifyFormat("typedef struct Foo {\n"
6779                "  Foo();\n"
6780                "} Foo_t;",
6781                Style);
6782   verifyFormat("union Foo {\n"
6783                "  A,\n"
6784                "};",
6785                Style);
6786   verifyFormat("typedef union Foo {\n"
6787                "  A,\n"
6788                "} Foo_t;",
6789                Style);
6790   verifyFormat("namespace Foo {\n"
6791                "void Bar();\n"
6792                "};",
6793                Style);
6794 
6795   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
6796   Style.BraceWrapping.AfterClass = true;
6797   Style.BraceWrapping.AfterStruct = true;
6798   Style.BraceWrapping.AfterUnion = true;
6799   Style.BraceWrapping.AfterNamespace = true;
6800   verifyFormat("class Foo\n"
6801                "{\n"
6802                "  Foo();\n"
6803                "};",
6804                Style);
6805   verifyFormat("typedef class Foo\n"
6806                "{\n"
6807                "  Foo();\n"
6808                "} Foo_t;",
6809                Style);
6810   verifyFormat("struct Foo\n"
6811                "{\n"
6812                "  Foo();\n"
6813                "};",
6814                Style);
6815   verifyFormat("typedef struct Foo\n"
6816                "{\n"
6817                "  Foo();\n"
6818                "} Foo_t;",
6819                Style);
6820   verifyFormat("union Foo\n"
6821                "{\n"
6822                "  A,\n"
6823                "};",
6824                Style);
6825   verifyFormat("typedef union Foo\n"
6826                "{\n"
6827                "  A,\n"
6828                "} Foo_t;",
6829                Style);
6830   verifyFormat("namespace Foo\n"
6831                "{\n"
6832                "void Bar();\n"
6833                "};",
6834                Style);
6835 }
6836 
6837 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6838   // Elaborate type variable declarations.
6839   verifyFormat("struct foo a = {bar};\nint n;");
6840   verifyFormat("class foo a = {bar};\nint n;");
6841   verifyFormat("union foo a = {bar};\nint n;");
6842 
6843   // Elaborate types inside function definitions.
6844   verifyFormat("struct foo f() {}\nint n;");
6845   verifyFormat("class foo f() {}\nint n;");
6846   verifyFormat("union foo f() {}\nint n;");
6847 
6848   // Templates.
6849   verifyFormat("template <class X> void f() {}\nint n;");
6850   verifyFormat("template <struct X> void f() {}\nint n;");
6851   verifyFormat("template <union X> void f() {}\nint n;");
6852 
6853   // Actual definitions...
6854   verifyFormat("struct {\n} n;");
6855   verifyFormat(
6856       "template <template <class T, class Y>, class Z> class X {\n} n;");
6857   verifyFormat("union Z {\n  int n;\n} x;");
6858   verifyFormat("class MACRO Z {\n} n;");
6859   verifyFormat("class MACRO(X) Z {\n} n;");
6860   verifyFormat("class __attribute__(X) Z {\n} n;");
6861   verifyFormat("class __declspec(X) Z {\n} n;");
6862   verifyFormat("class A##B##C {\n} n;");
6863   verifyFormat("class alignas(16) Z {\n} n;");
6864   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6865   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6866 
6867   // Redefinition from nested context:
6868   verifyFormat("class A::B::C {\n} n;");
6869 
6870   // Template definitions.
6871   verifyFormat(
6872       "template <typename F>\n"
6873       "Matcher(const Matcher<F> &Other,\n"
6874       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6875       "                             !is_same<F, T>::value>::type * = 0)\n"
6876       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6877 
6878   // FIXME: This is still incorrectly handled at the formatter side.
6879   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6880   verifyFormat("int i = SomeFunction(a<b, a> b);");
6881 
6882   // FIXME:
6883   // This now gets parsed incorrectly as class definition.
6884   // verifyFormat("class A<int> f() {\n}\nint n;");
6885 
6886   // Elaborate types where incorrectly parsing the structural element would
6887   // break the indent.
6888   verifyFormat("if (true)\n"
6889                "  class X x;\n"
6890                "else\n"
6891                "  f();\n");
6892 
6893   // This is simply incomplete. Formatting is not important, but must not crash.
6894   verifyFormat("class A:");
6895 }
6896 
6897 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6898   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6899             format("#error Leave     all         white!!!!! space* alone!\n"));
6900   EXPECT_EQ(
6901       "#warning Leave     all         white!!!!! space* alone!\n",
6902       format("#warning Leave     all         white!!!!! space* alone!\n"));
6903   EXPECT_EQ("#error 1", format("  #  error   1"));
6904   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6905 }
6906 
6907 TEST_F(FormatTest, FormatHashIfExpressions) {
6908   verifyFormat("#if AAAA && BBBB");
6909   verifyFormat("#if (AAAA && BBBB)");
6910   verifyFormat("#elif (AAAA && BBBB)");
6911   // FIXME: Come up with a better indentation for #elif.
6912   verifyFormat(
6913       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6914       "    defined(BBBBBBBB)\n"
6915       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6916       "    defined(BBBBBBBB)\n"
6917       "#endif",
6918       getLLVMStyleWithColumns(65));
6919 }
6920 
6921 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6922   FormatStyle AllowsMergedIf = getGoogleStyle();
6923   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6924   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6925   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6926   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6927   EXPECT_EQ("if (true) return 42;",
6928             format("if (true)\nreturn 42;", AllowsMergedIf));
6929   FormatStyle ShortMergedIf = AllowsMergedIf;
6930   ShortMergedIf.ColumnLimit = 25;
6931   verifyFormat("#define A \\\n"
6932                "  if (true) return 42;",
6933                ShortMergedIf);
6934   verifyFormat("#define A \\\n"
6935                "  f();    \\\n"
6936                "  if (true)\n"
6937                "#define B",
6938                ShortMergedIf);
6939   verifyFormat("#define A \\\n"
6940                "  f();    \\\n"
6941                "  if (true)\n"
6942                "g();",
6943                ShortMergedIf);
6944   verifyFormat("{\n"
6945                "#ifdef A\n"
6946                "  // Comment\n"
6947                "  if (true) continue;\n"
6948                "#endif\n"
6949                "  // Comment\n"
6950                "  if (true) continue;\n"
6951                "}",
6952                ShortMergedIf);
6953   ShortMergedIf.ColumnLimit = 33;
6954   verifyFormat("#define A \\\n"
6955                "  if constexpr (true) return 42;",
6956                ShortMergedIf);
6957   ShortMergedIf.ColumnLimit = 29;
6958   verifyFormat("#define A                   \\\n"
6959                "  if (aaaaaaaaaa) return 1; \\\n"
6960                "  return 2;",
6961                ShortMergedIf);
6962   ShortMergedIf.ColumnLimit = 28;
6963   verifyFormat("#define A         \\\n"
6964                "  if (aaaaaaaaaa) \\\n"
6965                "    return 1;     \\\n"
6966                "  return 2;",
6967                ShortMergedIf);
6968   verifyFormat("#define A                \\\n"
6969                "  if constexpr (aaaaaaa) \\\n"
6970                "    return 1;            \\\n"
6971                "  return 2;",
6972                ShortMergedIf);
6973 }
6974 
6975 TEST_F(FormatTest, FormatStarDependingOnContext) {
6976   verifyFormat("void f(int *a);");
6977   verifyFormat("void f() { f(fint * b); }");
6978   verifyFormat("class A {\n  void f(int *a);\n};");
6979   verifyFormat("class A {\n  int *a;\n};");
6980   verifyFormat("namespace a {\n"
6981                "namespace b {\n"
6982                "class A {\n"
6983                "  void f() {}\n"
6984                "  int *a;\n"
6985                "};\n"
6986                "} // namespace b\n"
6987                "} // namespace a");
6988 }
6989 
6990 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6991   verifyFormat("while");
6992   verifyFormat("operator");
6993 }
6994 
6995 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
6996   // This code would be painfully slow to format if we didn't skip it.
6997   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
6998                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6999                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7000                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7001                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
7002                    "A(1, 1)\n"
7003                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
7004                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7005                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7006                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7007                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7008                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7009                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7010                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7011                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
7012                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
7013   // Deeply nested part is untouched, rest is formatted.
7014   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
7015             format(std::string("int    i;\n") + Code + "int    j;\n",
7016                    getLLVMStyle(), SC_ExpectIncomplete));
7017 }
7018 
7019 //===----------------------------------------------------------------------===//
7020 // Objective-C tests.
7021 //===----------------------------------------------------------------------===//
7022 
7023 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
7024   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
7025   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
7026             format("-(NSUInteger)indexOfObject:(id)anObject;"));
7027   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
7028   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
7029   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
7030             format("-(NSInteger)Method3:(id)anObject;"));
7031   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
7032             format("-(NSInteger)Method4:(id)anObject;"));
7033   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
7034             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
7035   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
7036             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
7037   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7038             "forAllCells:(BOOL)flag;",
7039             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
7040                    "forAllCells:(BOOL)flag;"));
7041 
7042   // Very long objectiveC method declaration.
7043   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
7044                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
7045   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
7046                "                    inRange:(NSRange)range\n"
7047                "                   outRange:(NSRange)out_range\n"
7048                "                  outRange1:(NSRange)out_range1\n"
7049                "                  outRange2:(NSRange)out_range2\n"
7050                "                  outRange3:(NSRange)out_range3\n"
7051                "                  outRange4:(NSRange)out_range4\n"
7052                "                  outRange5:(NSRange)out_range5\n"
7053                "                  outRange6:(NSRange)out_range6\n"
7054                "                  outRange7:(NSRange)out_range7\n"
7055                "                  outRange8:(NSRange)out_range8\n"
7056                "                  outRange9:(NSRange)out_range9;");
7057 
7058   // When the function name has to be wrapped.
7059   FormatStyle Style = getLLVMStyle();
7060   Style.IndentWrappedFunctionNames = false;
7061   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7062                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7063                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7064                "}",
7065                Style);
7066   Style.IndentWrappedFunctionNames = true;
7067   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
7068                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
7069                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
7070                "}",
7071                Style);
7072 
7073   verifyFormat("- (int)sum:(vector<int>)numbers;");
7074   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
7075   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
7076   // protocol lists (but not for template classes):
7077   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
7078 
7079   verifyFormat("- (int (*)())foo:(int (*)())f;");
7080   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
7081 
7082   // If there's no return type (very rare in practice!), LLVM and Google style
7083   // agree.
7084   verifyFormat("- foo;");
7085   verifyFormat("- foo:(int)f;");
7086   verifyGoogleFormat("- foo:(int)foo;");
7087 }
7088 
7089 
7090 TEST_F(FormatTest, BreaksStringLiterals) {
7091   EXPECT_EQ("\"some text \"\n"
7092             "\"other\";",
7093             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7094   EXPECT_EQ("\"some text \"\n"
7095             "\"other\";",
7096             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7097   EXPECT_EQ(
7098       "#define A  \\\n"
7099       "  \"some \"  \\\n"
7100       "  \"text \"  \\\n"
7101       "  \"other\";",
7102       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7103   EXPECT_EQ(
7104       "#define A  \\\n"
7105       "  \"so \"    \\\n"
7106       "  \"text \"  \\\n"
7107       "  \"other\";",
7108       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7109 
7110   EXPECT_EQ("\"some text\"",
7111             format("\"some text\"", getLLVMStyleWithColumns(1)));
7112   EXPECT_EQ("\"some text\"",
7113             format("\"some text\"", getLLVMStyleWithColumns(11)));
7114   EXPECT_EQ("\"some \"\n"
7115             "\"text\"",
7116             format("\"some text\"", getLLVMStyleWithColumns(10)));
7117   EXPECT_EQ("\"some \"\n"
7118             "\"text\"",
7119             format("\"some text\"", getLLVMStyleWithColumns(7)));
7120   EXPECT_EQ("\"some\"\n"
7121             "\" tex\"\n"
7122             "\"t\"",
7123             format("\"some text\"", getLLVMStyleWithColumns(6)));
7124   EXPECT_EQ("\"some\"\n"
7125             "\" tex\"\n"
7126             "\" and\"",
7127             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7128   EXPECT_EQ("\"some\"\n"
7129             "\"/tex\"\n"
7130             "\"/and\"",
7131             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7132 
7133   EXPECT_EQ("variable =\n"
7134             "    \"long string \"\n"
7135             "    \"literal\";",
7136             format("variable = \"long string literal\";",
7137                    getLLVMStyleWithColumns(20)));
7138 
7139   EXPECT_EQ("variable = f(\n"
7140             "    \"long string \"\n"
7141             "    \"literal\",\n"
7142             "    short,\n"
7143             "    loooooooooooooooooooong);",
7144             format("variable = f(\"long string literal\", short, "
7145                    "loooooooooooooooooooong);",
7146                    getLLVMStyleWithColumns(20)));
7147 
7148   EXPECT_EQ(
7149       "f(g(\"long string \"\n"
7150       "    \"literal\"),\n"
7151       "  b);",
7152       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7153   EXPECT_EQ("f(g(\"long string \"\n"
7154             "    \"literal\",\n"
7155             "    a),\n"
7156             "  b);",
7157             format("f(g(\"long string literal\", a), b);",
7158                    getLLVMStyleWithColumns(20)));
7159   EXPECT_EQ(
7160       "f(\"one two\".split(\n"
7161       "    variable));",
7162       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7163   EXPECT_EQ("f(\"one two three four five six \"\n"
7164             "  \"seven\".split(\n"
7165             "      really_looooong_variable));",
7166             format("f(\"one two three four five six seven\"."
7167                    "split(really_looooong_variable));",
7168                    getLLVMStyleWithColumns(33)));
7169 
7170   EXPECT_EQ("f(\"some \"\n"
7171             "  \"text\",\n"
7172             "  other);",
7173             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7174 
7175   // Only break as a last resort.
7176   verifyFormat(
7177       "aaaaaaaaaaaaaaaaaaaa(\n"
7178       "    aaaaaaaaaaaaaaaaaaaa,\n"
7179       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7180 
7181   EXPECT_EQ("\"splitmea\"\n"
7182             "\"trandomp\"\n"
7183             "\"oint\"",
7184             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7185 
7186   EXPECT_EQ("\"split/\"\n"
7187             "\"pathat/\"\n"
7188             "\"slashes\"",
7189             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7190 
7191   EXPECT_EQ("\"split/\"\n"
7192             "\"pathat/\"\n"
7193             "\"slashes\"",
7194             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7195   EXPECT_EQ("\"split at \"\n"
7196             "\"spaces/at/\"\n"
7197             "\"slashes.at.any$\"\n"
7198             "\"non-alphanumeric%\"\n"
7199             "\"1111111111characte\"\n"
7200             "\"rs\"",
7201             format("\"split at "
7202                    "spaces/at/"
7203                    "slashes.at."
7204                    "any$non-"
7205                    "alphanumeric%"
7206                    "1111111111characte"
7207                    "rs\"",
7208                    getLLVMStyleWithColumns(20)));
7209 
7210   // Verify that splitting the strings understands
7211   // Style::AlwaysBreakBeforeMultilineStrings.
7212   EXPECT_EQ(
7213       "aaaaaaaaaaaa(\n"
7214       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7215       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7216       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
7217              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7218              "aaaaaaaaaaaaaaaaaaaaaa\");",
7219              getGoogleStyle()));
7220   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7221             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7222             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7223                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7224                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7225                    getGoogleStyle()));
7226   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7227             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7228             format("llvm::outs() << "
7229                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7230                    "aaaaaaaaaaaaaaaaaaa\";"));
7231   EXPECT_EQ("ffff(\n"
7232             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7233             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7234             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7235                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7236                    getGoogleStyle()));
7237 
7238   FormatStyle Style = getLLVMStyleWithColumns(12);
7239   Style.BreakStringLiterals = false;
7240   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
7241 
7242   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7243   AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7244   EXPECT_EQ("#define A \\\n"
7245             "  \"some \" \\\n"
7246             "  \"text \" \\\n"
7247             "  \"other\";",
7248             format("#define A \"some text other\";", AlignLeft));
7249 }
7250 
7251 TEST_F(FormatTest, FullyRemoveEmptyLines) {
7252   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
7253   NoEmptyLines.MaxEmptyLinesToKeep = 0;
7254   EXPECT_EQ("int i = a(b());",
7255             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
7256 }
7257 
7258 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7259   EXPECT_EQ(
7260       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7261       "(\n"
7262       "    \"x\t\");",
7263       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7264              "aaaaaaa("
7265              "\"x\t\");"));
7266 }
7267 
7268 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7269   EXPECT_EQ(
7270       "u8\"utf8 string \"\n"
7271       "u8\"literal\";",
7272       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7273   EXPECT_EQ(
7274       "u\"utf16 string \"\n"
7275       "u\"literal\";",
7276       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7277   EXPECT_EQ(
7278       "U\"utf32 string \"\n"
7279       "U\"literal\";",
7280       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7281   EXPECT_EQ("L\"wide string \"\n"
7282             "L\"literal\";",
7283             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7284   EXPECT_EQ("@\"NSString \"\n"
7285             "@\"literal\";",
7286             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7287   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
7288 
7289   // This input makes clang-format try to split the incomplete unicode escape
7290   // sequence, which used to lead to a crasher.
7291   verifyNoCrash(
7292       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7293       getLLVMStyleWithColumns(60));
7294 }
7295 
7296 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7297   FormatStyle Style = getGoogleStyleWithColumns(15);
7298   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7299   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7300   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7301   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7302   EXPECT_EQ("u8R\"x(raw literal)x\";",
7303             format("u8R\"x(raw literal)x\";", Style));
7304 }
7305 
7306 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7307   FormatStyle Style = getLLVMStyleWithColumns(20);
7308   EXPECT_EQ(
7309       "_T(\"aaaaaaaaaaaaaa\")\n"
7310       "_T(\"aaaaaaaaaaaaaa\")\n"
7311       "_T(\"aaaaaaaaaaaa\")",
7312       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7313   EXPECT_EQ("f(x,\n"
7314             "  _T(\"aaaaaaaaaaaa\")\n"
7315             "  _T(\"aaa\"),\n"
7316             "  z);",
7317             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7318 
7319   // FIXME: Handle embedded spaces in one iteration.
7320   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7321   //            "_T(\"aaaaaaaaaaaaa\")\n"
7322   //            "_T(\"aaaaaaaaaaaaa\")\n"
7323   //            "_T(\"a\")",
7324   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7325   //                   getLLVMStyleWithColumns(20)));
7326   EXPECT_EQ(
7327       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7328       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7329   EXPECT_EQ("f(\n"
7330             "#if !TEST\n"
7331             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7332             "#endif\n"
7333             ");",
7334             format("f(\n"
7335                    "#if !TEST\n"
7336                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7337                    "#endif\n"
7338                    ");"));
7339   EXPECT_EQ("f(\n"
7340             "\n"
7341             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7342             format("f(\n"
7343                    "\n"
7344                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7345 }
7346 
7347 TEST_F(FormatTest, BreaksStringLiteralOperands) {
7348   // In a function call with two operands, the second can be broken with no line
7349   // break before it.
7350   EXPECT_EQ("func(a, \"long long \"\n"
7351             "        \"long long\");",
7352             format("func(a, \"long long long long\");",
7353                    getLLVMStyleWithColumns(24)));
7354   // In a function call with three operands, the second must be broken with a
7355   // line break before it.
7356   EXPECT_EQ("func(a,\n"
7357             "     \"long long long \"\n"
7358             "     \"long\",\n"
7359             "     c);",
7360             format("func(a, \"long long long long\", c);",
7361                    getLLVMStyleWithColumns(24)));
7362   // In a function call with three operands, the third must be broken with a
7363   // line break before it.
7364   EXPECT_EQ("func(a, b,\n"
7365             "     \"long long long \"\n"
7366             "     \"long\");",
7367             format("func(a, b, \"long long long long\");",
7368                    getLLVMStyleWithColumns(24)));
7369   // In a function call with three operands, both the second and the third must
7370   // be broken with a line break before them.
7371   EXPECT_EQ("func(a,\n"
7372             "     \"long long long \"\n"
7373             "     \"long\",\n"
7374             "     \"long long long \"\n"
7375             "     \"long\");",
7376             format("func(a, \"long long long long\", \"long long long long\");",
7377                    getLLVMStyleWithColumns(24)));
7378   // In a chain of << with two operands, the second can be broken with no line
7379   // break before it.
7380   EXPECT_EQ("a << \"line line \"\n"
7381             "     \"line\";",
7382             format("a << \"line line line\";",
7383                    getLLVMStyleWithColumns(20)));
7384   // In a chain of << with three operands, the second can be broken with no line
7385   // break before it.
7386   EXPECT_EQ("abcde << \"line \"\n"
7387             "         \"line line\"\n"
7388             "      << c;",
7389             format("abcde << \"line line line\" << c;",
7390                    getLLVMStyleWithColumns(20)));
7391   // In a chain of << with three operands, the third must be broken with a line
7392   // break before it.
7393   EXPECT_EQ("a << b\n"
7394             "  << \"line line \"\n"
7395             "     \"line\";",
7396             format("a << b << \"line line line\";",
7397                    getLLVMStyleWithColumns(20)));
7398   // In a chain of << with three operands, the second can be broken with no line
7399   // break before it and the third must be broken with a line break before it.
7400   EXPECT_EQ("abcd << \"line line \"\n"
7401             "        \"line\"\n"
7402             "     << \"line line \"\n"
7403             "        \"line\";",
7404             format("abcd << \"line line line\" << \"line line line\";",
7405                    getLLVMStyleWithColumns(20)));
7406   // In a chain of binary operators with two operands, the second can be broken
7407   // with no line break before it.
7408   EXPECT_EQ("abcd + \"line line \"\n"
7409             "       \"line line\";",
7410             format("abcd + \"line line line line\";",
7411                    getLLVMStyleWithColumns(20)));
7412   // In a chain of binary operators with three operands, the second must be
7413   // broken with a line break before it.
7414   EXPECT_EQ("abcd +\n"
7415             "    \"line line \"\n"
7416             "    \"line line\" +\n"
7417             "    e;",
7418             format("abcd + \"line line line line\" + e;",
7419                    getLLVMStyleWithColumns(20)));
7420   // In a function call with two operands, with AlignAfterOpenBracket enabled,
7421   // the first must be broken with a line break before it.
7422   FormatStyle Style = getLLVMStyleWithColumns(25);
7423   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
7424   EXPECT_EQ("someFunction(\n"
7425             "    \"long long long \"\n"
7426             "    \"long\",\n"
7427             "    a);",
7428             format("someFunction(\"long long long long\", a);", Style));
7429 }
7430 
7431 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7432   EXPECT_EQ(
7433       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7434       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7435       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7436       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7437              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7438              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7439 }
7440 
7441 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7442   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7443             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7444   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7445             "multiline raw string literal xxxxxxxxxxxxxx\n"
7446             ")x\",\n"
7447             "              a),\n"
7448             "            b);",
7449             format("fffffffffff(g(R\"x(\n"
7450                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7451                    ")x\", a), b);",
7452                    getGoogleStyleWithColumns(20)));
7453   EXPECT_EQ("fffffffffff(\n"
7454             "    g(R\"x(qqq\n"
7455             "multiline raw string literal xxxxxxxxxxxxxx\n"
7456             ")x\",\n"
7457             "      a),\n"
7458             "    b);",
7459             format("fffffffffff(g(R\"x(qqq\n"
7460                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7461                    ")x\", a), b);",
7462                    getGoogleStyleWithColumns(20)));
7463 
7464   EXPECT_EQ("fffffffffff(R\"x(\n"
7465             "multiline raw string literal xxxxxxxxxxxxxx\n"
7466             ")x\");",
7467             format("fffffffffff(R\"x(\n"
7468                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7469                    ")x\");",
7470                    getGoogleStyleWithColumns(20)));
7471   EXPECT_EQ("fffffffffff(R\"x(\n"
7472             "multiline raw string literal xxxxxxxxxxxxxx\n"
7473             ")x\" + bbbbbb);",
7474             format("fffffffffff(R\"x(\n"
7475                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7476                    ")x\" +   bbbbbb);",
7477                    getGoogleStyleWithColumns(20)));
7478   EXPECT_EQ("fffffffffff(\n"
7479             "    R\"x(\n"
7480             "multiline raw string literal xxxxxxxxxxxxxx\n"
7481             ")x\" +\n"
7482             "    bbbbbb);",
7483             format("fffffffffff(\n"
7484                    " R\"x(\n"
7485                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7486                    ")x\" + bbbbbb);",
7487                    getGoogleStyleWithColumns(20)));
7488 }
7489 
7490 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7491   verifyFormat("string a = \"unterminated;");
7492   EXPECT_EQ("function(\"unterminated,\n"
7493             "         OtherParameter);",
7494             format("function(  \"unterminated,\n"
7495                    "    OtherParameter);"));
7496 }
7497 
7498 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7499   FormatStyle Style = getLLVMStyle();
7500   Style.Standard = FormatStyle::LS_Cpp03;
7501   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7502             format("#define x(_a) printf(\"foo\"_a);", Style));
7503 }
7504 
7505 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7506 
7507 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7508   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7509             "             \"ddeeefff\");",
7510             format("someFunction(\"aaabbbcccdddeeefff\");",
7511                    getLLVMStyleWithColumns(25)));
7512   EXPECT_EQ("someFunction1234567890(\n"
7513             "    \"aaabbbcccdddeeefff\");",
7514             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7515                    getLLVMStyleWithColumns(26)));
7516   EXPECT_EQ("someFunction1234567890(\n"
7517             "    \"aaabbbcccdddeeeff\"\n"
7518             "    \"f\");",
7519             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7520                    getLLVMStyleWithColumns(25)));
7521   EXPECT_EQ("someFunction1234567890(\n"
7522             "    \"aaabbbcccdddeeeff\"\n"
7523             "    \"f\");",
7524             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7525                    getLLVMStyleWithColumns(24)));
7526   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7527             "             \"ddde \"\n"
7528             "             \"efff\");",
7529             format("someFunction(\"aaabbbcc ddde efff\");",
7530                    getLLVMStyleWithColumns(25)));
7531   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7532             "             \"ddeeefff\");",
7533             format("someFunction(\"aaabbbccc ddeeefff\");",
7534                    getLLVMStyleWithColumns(25)));
7535   EXPECT_EQ("someFunction1234567890(\n"
7536             "    \"aaabb \"\n"
7537             "    \"cccdddeeefff\");",
7538             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7539                    getLLVMStyleWithColumns(25)));
7540   EXPECT_EQ("#define A          \\\n"
7541             "  string s =       \\\n"
7542             "      \"123456789\"  \\\n"
7543             "      \"0\";         \\\n"
7544             "  int i;",
7545             format("#define A string s = \"1234567890\"; int i;",
7546                    getLLVMStyleWithColumns(20)));
7547   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7548   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7549             "             \"dddeeeff\"\n"
7550             "             \"f\");",
7551             format("someFunction(\"aaabbbcc dddeeefff\");",
7552                    getLLVMStyleWithColumns(25)));
7553 }
7554 
7555 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7556   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7557   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7558   EXPECT_EQ("\"test\"\n"
7559             "\"\\n\"",
7560             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7561   EXPECT_EQ("\"tes\\\\\"\n"
7562             "\"n\"",
7563             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7564   EXPECT_EQ("\"\\\\\\\\\"\n"
7565             "\"\\n\"",
7566             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7567   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7568   EXPECT_EQ("\"\\uff01\"\n"
7569             "\"test\"",
7570             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7571   EXPECT_EQ("\"\\Uff01ff02\"",
7572             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7573   EXPECT_EQ("\"\\x000000000001\"\n"
7574             "\"next\"",
7575             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7576   EXPECT_EQ("\"\\x000000000001next\"",
7577             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7578   EXPECT_EQ("\"\\x000000000001\"",
7579             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7580   EXPECT_EQ("\"test\"\n"
7581             "\"\\000000\"\n"
7582             "\"000001\"",
7583             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7584   EXPECT_EQ("\"test\\000\"\n"
7585             "\"00000000\"\n"
7586             "\"1\"",
7587             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7588 }
7589 
7590 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7591   verifyFormat("void f() {\n"
7592                "  return g() {}\n"
7593                "  void h() {}");
7594   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7595                "g();\n"
7596                "}");
7597 }
7598 
7599 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7600   verifyFormat(
7601       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7602 }
7603 
7604 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7605   verifyFormat("class X {\n"
7606                "  void f() {\n"
7607                "  }\n"
7608                "};",
7609                getLLVMStyleWithColumns(12));
7610 }
7611 
7612 TEST_F(FormatTest, ConfigurableIndentWidth) {
7613   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7614   EightIndent.IndentWidth = 8;
7615   EightIndent.ContinuationIndentWidth = 8;
7616   verifyFormat("void f() {\n"
7617                "        someFunction();\n"
7618                "        if (true) {\n"
7619                "                f();\n"
7620                "        }\n"
7621                "}",
7622                EightIndent);
7623   verifyFormat("class X {\n"
7624                "        void f() {\n"
7625                "        }\n"
7626                "};",
7627                EightIndent);
7628   verifyFormat("int x[] = {\n"
7629                "        call(),\n"
7630                "        call()};",
7631                EightIndent);
7632 }
7633 
7634 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7635   verifyFormat("double\n"
7636                "f();",
7637                getLLVMStyleWithColumns(8));
7638 }
7639 
7640 TEST_F(FormatTest, ConfigurableUseOfTab) {
7641   FormatStyle Tab = getLLVMStyleWithColumns(42);
7642   Tab.IndentWidth = 8;
7643   Tab.UseTab = FormatStyle::UT_Always;
7644   Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left;
7645 
7646   EXPECT_EQ("if (aaaaaaaa && // q\n"
7647             "    bb)\t\t// w\n"
7648             "\t;",
7649             format("if (aaaaaaaa &&// q\n"
7650                    "bb)// w\n"
7651                    ";",
7652                    Tab));
7653   EXPECT_EQ("if (aaa && bbb) // w\n"
7654             "\t;",
7655             format("if(aaa&&bbb)// w\n"
7656                    ";",
7657                    Tab));
7658 
7659   verifyFormat("class X {\n"
7660                "\tvoid f() {\n"
7661                "\t\tsomeFunction(parameter1,\n"
7662                "\t\t\t     parameter2);\n"
7663                "\t}\n"
7664                "};",
7665                Tab);
7666   verifyFormat("#define A                        \\\n"
7667                "\tvoid f() {               \\\n"
7668                "\t\tsomeFunction(    \\\n"
7669                "\t\t    parameter1,  \\\n"
7670                "\t\t    parameter2); \\\n"
7671                "\t}",
7672                Tab);
7673 
7674   Tab.TabWidth = 4;
7675   Tab.IndentWidth = 8;
7676   verifyFormat("class TabWidth4Indent8 {\n"
7677                "\t\tvoid f() {\n"
7678                "\t\t\t\tsomeFunction(parameter1,\n"
7679                "\t\t\t\t\t\t\t parameter2);\n"
7680                "\t\t}\n"
7681                "};",
7682                Tab);
7683 
7684   Tab.TabWidth = 4;
7685   Tab.IndentWidth = 4;
7686   verifyFormat("class TabWidth4Indent4 {\n"
7687                "\tvoid f() {\n"
7688                "\t\tsomeFunction(parameter1,\n"
7689                "\t\t\t\t\t parameter2);\n"
7690                "\t}\n"
7691                "};",
7692                Tab);
7693 
7694   Tab.TabWidth = 8;
7695   Tab.IndentWidth = 4;
7696   verifyFormat("class TabWidth8Indent4 {\n"
7697                "    void f() {\n"
7698                "\tsomeFunction(parameter1,\n"
7699                "\t\t     parameter2);\n"
7700                "    }\n"
7701                "};",
7702                Tab);
7703 
7704   Tab.TabWidth = 8;
7705   Tab.IndentWidth = 8;
7706   EXPECT_EQ("/*\n"
7707             "\t      a\t\tcomment\n"
7708             "\t      in multiple lines\n"
7709             "       */",
7710             format("   /*\t \t \n"
7711                    " \t \t a\t\tcomment\t \t\n"
7712                    " \t \t in multiple lines\t\n"
7713                    " \t  */",
7714                    Tab));
7715 
7716   Tab.UseTab = FormatStyle::UT_ForIndentation;
7717   verifyFormat("{\n"
7718                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7719                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7720                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7721                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7722                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7723                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7724                "};",
7725                Tab);
7726   verifyFormat("enum AA {\n"
7727                "\ta1, // Force multiple lines\n"
7728                "\ta2,\n"
7729                "\ta3\n"
7730                "};",
7731                Tab);
7732   EXPECT_EQ("if (aaaaaaaa && // q\n"
7733             "    bb)         // w\n"
7734             "\t;",
7735             format("if (aaaaaaaa &&// q\n"
7736                    "bb)// w\n"
7737                    ";",
7738                    Tab));
7739   verifyFormat("class X {\n"
7740                "\tvoid f() {\n"
7741                "\t\tsomeFunction(parameter1,\n"
7742                "\t\t             parameter2);\n"
7743                "\t}\n"
7744                "};",
7745                Tab);
7746   verifyFormat("{\n"
7747                "\tQ(\n"
7748                "\t    {\n"
7749                "\t\t    int a;\n"
7750                "\t\t    someFunction(aaaaaaaa,\n"
7751                "\t\t                 bbbbbbb);\n"
7752                "\t    },\n"
7753                "\t    p);\n"
7754                "}",
7755                Tab);
7756   EXPECT_EQ("{\n"
7757             "\t/* aaaa\n"
7758             "\t   bbbb */\n"
7759             "}",
7760             format("{\n"
7761                    "/* aaaa\n"
7762                    "   bbbb */\n"
7763                    "}",
7764                    Tab));
7765   EXPECT_EQ("{\n"
7766             "\t/*\n"
7767             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7768             "\t  bbbbbbbbbbbbb\n"
7769             "\t*/\n"
7770             "}",
7771             format("{\n"
7772                    "/*\n"
7773                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7774                    "*/\n"
7775                    "}",
7776                    Tab));
7777   EXPECT_EQ("{\n"
7778             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7779             "\t// bbbbbbbbbbbbb\n"
7780             "}",
7781             format("{\n"
7782                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7783                    "}",
7784                    Tab));
7785   EXPECT_EQ("{\n"
7786             "\t/*\n"
7787             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7788             "\t  bbbbbbbbbbbbb\n"
7789             "\t*/\n"
7790             "}",
7791             format("{\n"
7792                    "\t/*\n"
7793                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7794                    "\t*/\n"
7795                    "}",
7796                    Tab));
7797   EXPECT_EQ("{\n"
7798             "\t/*\n"
7799             "\n"
7800             "\t*/\n"
7801             "}",
7802             format("{\n"
7803                    "\t/*\n"
7804                    "\n"
7805                    "\t*/\n"
7806                    "}",
7807                    Tab));
7808   EXPECT_EQ("{\n"
7809             "\t/*\n"
7810             " asdf\n"
7811             "\t*/\n"
7812             "}",
7813             format("{\n"
7814                    "\t/*\n"
7815                    " asdf\n"
7816                    "\t*/\n"
7817                    "}",
7818                    Tab));
7819 
7820   Tab.UseTab = FormatStyle::UT_Never;
7821   EXPECT_EQ("/*\n"
7822             "              a\t\tcomment\n"
7823             "              in multiple lines\n"
7824             "       */",
7825             format("   /*\t \t \n"
7826                    " \t \t a\t\tcomment\t \t\n"
7827                    " \t \t in multiple lines\t\n"
7828                    " \t  */",
7829                    Tab));
7830   EXPECT_EQ("/* some\n"
7831             "   comment */",
7832             format(" \t \t /* some\n"
7833                    " \t \t    comment */",
7834                    Tab));
7835   EXPECT_EQ("int a; /* some\n"
7836             "   comment */",
7837             format(" \t \t int a; /* some\n"
7838                    " \t \t    comment */",
7839                    Tab));
7840 
7841   EXPECT_EQ("int a; /* some\n"
7842             "comment */",
7843             format(" \t \t int\ta; /* some\n"
7844                    " \t \t    comment */",
7845                    Tab));
7846   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7847             "    comment */",
7848             format(" \t \t f(\"\t\t\"); /* some\n"
7849                    " \t \t    comment */",
7850                    Tab));
7851   EXPECT_EQ("{\n"
7852             "  /*\n"
7853             "   * Comment\n"
7854             "   */\n"
7855             "  int i;\n"
7856             "}",
7857             format("{\n"
7858                    "\t/*\n"
7859                    "\t * Comment\n"
7860                    "\t */\n"
7861                    "\t int i;\n"
7862                    "}"));
7863 
7864   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
7865   Tab.TabWidth = 8;
7866   Tab.IndentWidth = 8;
7867   EXPECT_EQ("if (aaaaaaaa && // q\n"
7868             "    bb)         // w\n"
7869             "\t;",
7870             format("if (aaaaaaaa &&// q\n"
7871                    "bb)// w\n"
7872                    ";",
7873                    Tab));
7874   EXPECT_EQ("if (aaa && bbb) // w\n"
7875             "\t;",
7876             format("if(aaa&&bbb)// w\n"
7877                    ";",
7878                    Tab));
7879   verifyFormat("class X {\n"
7880                "\tvoid f() {\n"
7881                "\t\tsomeFunction(parameter1,\n"
7882                "\t\t\t     parameter2);\n"
7883                "\t}\n"
7884                "};",
7885                Tab);
7886   verifyFormat("#define A                        \\\n"
7887                "\tvoid f() {               \\\n"
7888                "\t\tsomeFunction(    \\\n"
7889                "\t\t    parameter1,  \\\n"
7890                "\t\t    parameter2); \\\n"
7891                "\t}",
7892                Tab);
7893   Tab.TabWidth = 4;
7894   Tab.IndentWidth = 8;
7895   verifyFormat("class TabWidth4Indent8 {\n"
7896                "\t\tvoid f() {\n"
7897                "\t\t\t\tsomeFunction(parameter1,\n"
7898                "\t\t\t\t\t\t\t parameter2);\n"
7899                "\t\t}\n"
7900                "};",
7901                Tab);
7902   Tab.TabWidth = 4;
7903   Tab.IndentWidth = 4;
7904   verifyFormat("class TabWidth4Indent4 {\n"
7905                "\tvoid f() {\n"
7906                "\t\tsomeFunction(parameter1,\n"
7907                "\t\t\t\t\t parameter2);\n"
7908                "\t}\n"
7909                "};",
7910                Tab);
7911   Tab.TabWidth = 8;
7912   Tab.IndentWidth = 4;
7913   verifyFormat("class TabWidth8Indent4 {\n"
7914                "    void f() {\n"
7915                "\tsomeFunction(parameter1,\n"
7916                "\t\t     parameter2);\n"
7917                "    }\n"
7918                "};",
7919                Tab);
7920   Tab.TabWidth = 8;
7921   Tab.IndentWidth = 8;
7922   EXPECT_EQ("/*\n"
7923             "\t      a\t\tcomment\n"
7924             "\t      in multiple lines\n"
7925             "       */",
7926             format("   /*\t \t \n"
7927                    " \t \t a\t\tcomment\t \t\n"
7928                    " \t \t in multiple lines\t\n"
7929                    " \t  */",
7930                    Tab));
7931   verifyFormat("{\n"
7932                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7933                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7934                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7935                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7936                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7937                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7938                "};",
7939                Tab);
7940   verifyFormat("enum AA {\n"
7941                "\ta1, // Force multiple lines\n"
7942                "\ta2,\n"
7943                "\ta3\n"
7944                "};",
7945                Tab);
7946   EXPECT_EQ("if (aaaaaaaa && // q\n"
7947             "    bb)         // w\n"
7948             "\t;",
7949             format("if (aaaaaaaa &&// q\n"
7950                    "bb)// w\n"
7951                    ";",
7952                    Tab));
7953   verifyFormat("class X {\n"
7954                "\tvoid f() {\n"
7955                "\t\tsomeFunction(parameter1,\n"
7956                "\t\t\t     parameter2);\n"
7957                "\t}\n"
7958                "};",
7959                Tab);
7960   verifyFormat("{\n"
7961                "\tQ(\n"
7962                "\t    {\n"
7963                "\t\t    int a;\n"
7964                "\t\t    someFunction(aaaaaaaa,\n"
7965                "\t\t\t\t bbbbbbb);\n"
7966                "\t    },\n"
7967                "\t    p);\n"
7968                "}",
7969                Tab);
7970   EXPECT_EQ("{\n"
7971             "\t/* aaaa\n"
7972             "\t   bbbb */\n"
7973             "}",
7974             format("{\n"
7975                    "/* aaaa\n"
7976                    "   bbbb */\n"
7977                    "}",
7978                    Tab));
7979   EXPECT_EQ("{\n"
7980             "\t/*\n"
7981             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7982             "\t  bbbbbbbbbbbbb\n"
7983             "\t*/\n"
7984             "}",
7985             format("{\n"
7986                    "/*\n"
7987                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7988                    "*/\n"
7989                    "}",
7990                    Tab));
7991   EXPECT_EQ("{\n"
7992             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7993             "\t// bbbbbbbbbbbbb\n"
7994             "}",
7995             format("{\n"
7996                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7997                    "}",
7998                    Tab));
7999   EXPECT_EQ("{\n"
8000             "\t/*\n"
8001             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8002             "\t  bbbbbbbbbbbbb\n"
8003             "\t*/\n"
8004             "}",
8005             format("{\n"
8006                    "\t/*\n"
8007                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8008                    "\t*/\n"
8009                    "}",
8010                    Tab));
8011   EXPECT_EQ("{\n"
8012             "\t/*\n"
8013             "\n"
8014             "\t*/\n"
8015             "}",
8016             format("{\n"
8017                    "\t/*\n"
8018                    "\n"
8019                    "\t*/\n"
8020                    "}",
8021                    Tab));
8022   EXPECT_EQ("{\n"
8023             "\t/*\n"
8024             " asdf\n"
8025             "\t*/\n"
8026             "}",
8027             format("{\n"
8028                    "\t/*\n"
8029                    " asdf\n"
8030                    "\t*/\n"
8031                    "}",
8032                    Tab));
8033   EXPECT_EQ("/*\n"
8034             "\t      a\t\tcomment\n"
8035             "\t      in multiple lines\n"
8036             "       */",
8037             format("   /*\t \t \n"
8038                    " \t \t a\t\tcomment\t \t\n"
8039                    " \t \t in multiple lines\t\n"
8040                    " \t  */",
8041                    Tab));
8042   EXPECT_EQ("/* some\n"
8043             "   comment */",
8044             format(" \t \t /* some\n"
8045                    " \t \t    comment */",
8046                    Tab));
8047   EXPECT_EQ("int a; /* some\n"
8048             "   comment */",
8049             format(" \t \t int a; /* some\n"
8050                    " \t \t    comment */",
8051                    Tab));
8052   EXPECT_EQ("int a; /* some\n"
8053             "comment */",
8054             format(" \t \t int\ta; /* some\n"
8055                    " \t \t    comment */",
8056                    Tab));
8057   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8058             "    comment */",
8059             format(" \t \t f(\"\t\t\"); /* some\n"
8060                    " \t \t    comment */",
8061                    Tab));
8062   EXPECT_EQ("{\n"
8063             "  /*\n"
8064             "   * Comment\n"
8065             "   */\n"
8066             "  int i;\n"
8067             "}",
8068             format("{\n"
8069                    "\t/*\n"
8070                    "\t * Comment\n"
8071                    "\t */\n"
8072                    "\t int i;\n"
8073                    "}"));
8074   Tab.AlignConsecutiveAssignments = true;
8075   Tab.AlignConsecutiveDeclarations = true;
8076   Tab.TabWidth = 4;
8077   Tab.IndentWidth = 4;
8078   verifyFormat("class Assign {\n"
8079                "\tvoid f() {\n"
8080                "\t\tint         x      = 123;\n"
8081                "\t\tint         random = 4;\n"
8082                "\t\tstd::string alphabet =\n"
8083                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8084                "\t}\n"
8085                "};",
8086                Tab);
8087 }
8088 
8089 TEST_F(FormatTest, CalculatesOriginalColumn) {
8090   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8091             "q\"; /* some\n"
8092             "       comment */",
8093             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8094                    "q\"; /* some\n"
8095                    "       comment */",
8096                    getLLVMStyle()));
8097   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8098             "/* some\n"
8099             "   comment */",
8100             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8101                    " /* some\n"
8102                    "    comment */",
8103                    getLLVMStyle()));
8104   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8105             "qqq\n"
8106             "/* some\n"
8107             "   comment */",
8108             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8109                    "qqq\n"
8110                    " /* some\n"
8111                    "    comment */",
8112                    getLLVMStyle()));
8113   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8114             "wwww; /* some\n"
8115             "         comment */",
8116             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8117                    "wwww; /* some\n"
8118                    "         comment */",
8119                    getLLVMStyle()));
8120 }
8121 
8122 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8123   FormatStyle NoSpace = getLLVMStyle();
8124   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8125 
8126   verifyFormat("while(true)\n"
8127                "  continue;",
8128                NoSpace);
8129   verifyFormat("for(;;)\n"
8130                "  continue;",
8131                NoSpace);
8132   verifyFormat("if(true)\n"
8133                "  f();\n"
8134                "else if(true)\n"
8135                "  f();",
8136                NoSpace);
8137   verifyFormat("do {\n"
8138                "  do_something();\n"
8139                "} while(something());",
8140                NoSpace);
8141   verifyFormat("switch(x) {\n"
8142                "default:\n"
8143                "  break;\n"
8144                "}",
8145                NoSpace);
8146   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8147   verifyFormat("size_t x = sizeof(x);", NoSpace);
8148   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8149   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8150   verifyFormat("alignas(128) char a[128];", NoSpace);
8151   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8152   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8153   verifyFormat("int f() throw(Deprecated);", NoSpace);
8154   verifyFormat("typedef void (*cb)(int);", NoSpace);
8155   verifyFormat("T A::operator()();", NoSpace);
8156   verifyFormat("X A::operator++(T);", NoSpace);
8157 
8158   FormatStyle Space = getLLVMStyle();
8159   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8160 
8161   verifyFormat("int f ();", Space);
8162   verifyFormat("void f (int a, T b) {\n"
8163                "  while (true)\n"
8164                "    continue;\n"
8165                "}",
8166                Space);
8167   verifyFormat("if (true)\n"
8168                "  f ();\n"
8169                "else if (true)\n"
8170                "  f ();",
8171                Space);
8172   verifyFormat("do {\n"
8173                "  do_something ();\n"
8174                "} while (something ());",
8175                Space);
8176   verifyFormat("switch (x) {\n"
8177                "default:\n"
8178                "  break;\n"
8179                "}",
8180                Space);
8181   verifyFormat("A::A () : a (1) {}", Space);
8182   verifyFormat("void f () __attribute__ ((asdf));", Space);
8183   verifyFormat("*(&a + 1);\n"
8184                "&((&a)[1]);\n"
8185                "a[(b + c) * d];\n"
8186                "(((a + 1) * 2) + 3) * 4;",
8187                Space);
8188   verifyFormat("#define A(x) x", Space);
8189   verifyFormat("#define A (x) x", Space);
8190   verifyFormat("#if defined(x)\n"
8191                "#endif",
8192                Space);
8193   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8194   verifyFormat("size_t x = sizeof (x);", Space);
8195   verifyFormat("auto f (int x) -> decltype (x);", Space);
8196   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8197   verifyFormat("alignas (128) char a[128];", Space);
8198   verifyFormat("size_t x = alignof (MyType);", Space);
8199   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8200   verifyFormat("int f () throw (Deprecated);", Space);
8201   verifyFormat("typedef void (*cb) (int);", Space);
8202   verifyFormat("T A::operator() ();", Space);
8203   verifyFormat("X A::operator++ (T);", Space);
8204 }
8205 
8206 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8207   FormatStyle Spaces = getLLVMStyle();
8208 
8209   Spaces.SpacesInParentheses = true;
8210   verifyFormat("call( x, y, z );", Spaces);
8211   verifyFormat("call();", Spaces);
8212   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8213   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
8214                Spaces);
8215   verifyFormat("while ( (bool)1 )\n"
8216                "  continue;",
8217                Spaces);
8218   verifyFormat("for ( ;; )\n"
8219                "  continue;",
8220                Spaces);
8221   verifyFormat("if ( true )\n"
8222                "  f();\n"
8223                "else if ( true )\n"
8224                "  f();",
8225                Spaces);
8226   verifyFormat("do {\n"
8227                "  do_something( (int)i );\n"
8228                "} while ( something() );",
8229                Spaces);
8230   verifyFormat("switch ( x ) {\n"
8231                "default:\n"
8232                "  break;\n"
8233                "}",
8234                Spaces);
8235 
8236   Spaces.SpacesInParentheses = false;
8237   Spaces.SpacesInCStyleCastParentheses = true;
8238   verifyFormat("Type *A = ( Type * )P;", Spaces);
8239   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8240   verifyFormat("x = ( int32 )y;", Spaces);
8241   verifyFormat("int a = ( int )(2.0f);", Spaces);
8242   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8243   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8244   verifyFormat("#define x (( int )-1)", Spaces);
8245 
8246   // Run the first set of tests again with:
8247   Spaces.SpacesInParentheses = false;
8248   Spaces.SpaceInEmptyParentheses = true;
8249   Spaces.SpacesInCStyleCastParentheses = true;
8250   verifyFormat("call(x, y, z);", Spaces);
8251   verifyFormat("call( );", Spaces);
8252   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8253   verifyFormat("while (( bool )1)\n"
8254                "  continue;",
8255                Spaces);
8256   verifyFormat("for (;;)\n"
8257                "  continue;",
8258                Spaces);
8259   verifyFormat("if (true)\n"
8260                "  f( );\n"
8261                "else if (true)\n"
8262                "  f( );",
8263                Spaces);
8264   verifyFormat("do {\n"
8265                "  do_something(( int )i);\n"
8266                "} while (something( ));",
8267                Spaces);
8268   verifyFormat("switch (x) {\n"
8269                "default:\n"
8270                "  break;\n"
8271                "}",
8272                Spaces);
8273 
8274   // Run the first set of tests again with:
8275   Spaces.SpaceAfterCStyleCast = true;
8276   verifyFormat("call(x, y, z);", Spaces);
8277   verifyFormat("call( );", Spaces);
8278   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8279   verifyFormat("while (( bool ) 1)\n"
8280                "  continue;",
8281                Spaces);
8282   verifyFormat("for (;;)\n"
8283                "  continue;",
8284                Spaces);
8285   verifyFormat("if (true)\n"
8286                "  f( );\n"
8287                "else if (true)\n"
8288                "  f( );",
8289                Spaces);
8290   verifyFormat("do {\n"
8291                "  do_something(( int ) i);\n"
8292                "} while (something( ));",
8293                Spaces);
8294   verifyFormat("switch (x) {\n"
8295                "default:\n"
8296                "  break;\n"
8297                "}",
8298                Spaces);
8299 
8300   // Run subset of tests again with:
8301   Spaces.SpacesInCStyleCastParentheses = false;
8302   Spaces.SpaceAfterCStyleCast = true;
8303   verifyFormat("while ((bool) 1)\n"
8304                "  continue;",
8305                Spaces);
8306   verifyFormat("do {\n"
8307                "  do_something((int) i);\n"
8308                "} while (something( ));",
8309                Spaces);
8310 }
8311 
8312 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8313   verifyFormat("int a[5];");
8314   verifyFormat("a[3] += 42;");
8315 
8316   FormatStyle Spaces = getLLVMStyle();
8317   Spaces.SpacesInSquareBrackets = true;
8318   // Lambdas unchanged.
8319   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8320   verifyFormat("return [i, args...] {};", Spaces);
8321 
8322   // Not lambdas.
8323   verifyFormat("int a[ 5 ];", Spaces);
8324   verifyFormat("a[ 3 ] += 42;", Spaces);
8325   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8326   verifyFormat("double &operator[](int i) { return 0; }\n"
8327                "int i;",
8328                Spaces);
8329   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8330   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8331   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8332 }
8333 
8334 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8335   verifyFormat("int a = 5;");
8336   verifyFormat("a += 42;");
8337   verifyFormat("a or_eq 8;");
8338 
8339   FormatStyle Spaces = getLLVMStyle();
8340   Spaces.SpaceBeforeAssignmentOperators = false;
8341   verifyFormat("int a= 5;", Spaces);
8342   verifyFormat("a+= 42;", Spaces);
8343   verifyFormat("a or_eq 8;", Spaces);
8344 }
8345 
8346 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8347   FormatStyle Alignment = getLLVMStyle();
8348   Alignment.AlignConsecutiveAssignments = false;
8349   verifyFormat("int a = 5;\n"
8350                "int oneTwoThree = 123;",
8351                Alignment);
8352   verifyFormat("int a = 5;\n"
8353                "int oneTwoThree = 123;",
8354                Alignment);
8355 
8356   Alignment.AlignConsecutiveAssignments = true;
8357   verifyFormat("int a           = 5;\n"
8358                "int oneTwoThree = 123;",
8359                Alignment);
8360   verifyFormat("int a           = method();\n"
8361                "int oneTwoThree = 133;",
8362                Alignment);
8363   verifyFormat("a &= 5;\n"
8364                "bcd *= 5;\n"
8365                "ghtyf += 5;\n"
8366                "dvfvdb -= 5;\n"
8367                "a /= 5;\n"
8368                "vdsvsv %= 5;\n"
8369                "sfdbddfbdfbb ^= 5;\n"
8370                "dvsdsv |= 5;\n"
8371                "int dsvvdvsdvvv = 123;",
8372                Alignment);
8373   verifyFormat("int i = 1, j = 10;\n"
8374                "something = 2000;",
8375                Alignment);
8376   verifyFormat("something = 2000;\n"
8377                "int i = 1, j = 10;\n",
8378                Alignment);
8379   verifyFormat("something = 2000;\n"
8380                "another   = 911;\n"
8381                "int i = 1, j = 10;\n"
8382                "oneMore = 1;\n"
8383                "i       = 2;",
8384                Alignment);
8385   verifyFormat("int a   = 5;\n"
8386                "int one = 1;\n"
8387                "method();\n"
8388                "int oneTwoThree = 123;\n"
8389                "int oneTwo      = 12;",
8390                Alignment);
8391   verifyFormat("int oneTwoThree = 123;\n"
8392                "int oneTwo      = 12;\n"
8393                "method();\n",
8394                Alignment);
8395   verifyFormat("int oneTwoThree = 123; // comment\n"
8396                "int oneTwo      = 12;  // comment",
8397                Alignment);
8398   EXPECT_EQ("int a = 5;\n"
8399             "\n"
8400             "int oneTwoThree = 123;",
8401             format("int a       = 5;\n"
8402                    "\n"
8403                    "int oneTwoThree= 123;",
8404                    Alignment));
8405   EXPECT_EQ("int a   = 5;\n"
8406             "int one = 1;\n"
8407             "\n"
8408             "int oneTwoThree = 123;",
8409             format("int a = 5;\n"
8410                    "int one = 1;\n"
8411                    "\n"
8412                    "int oneTwoThree = 123;",
8413                    Alignment));
8414   EXPECT_EQ("int a   = 5;\n"
8415             "int one = 1;\n"
8416             "\n"
8417             "int oneTwoThree = 123;\n"
8418             "int oneTwo      = 12;",
8419             format("int a = 5;\n"
8420                    "int one = 1;\n"
8421                    "\n"
8422                    "int oneTwoThree = 123;\n"
8423                    "int oneTwo = 12;",
8424                    Alignment));
8425   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8426   verifyFormat("#define A \\\n"
8427                "  int aaaa       = 12; \\\n"
8428                "  int b          = 23; \\\n"
8429                "  int ccc        = 234; \\\n"
8430                "  int dddddddddd = 2345;",
8431                Alignment);
8432   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8433   verifyFormat("#define A               \\\n"
8434                "  int aaaa       = 12;  \\\n"
8435                "  int b          = 23;  \\\n"
8436                "  int ccc        = 234; \\\n"
8437                "  int dddddddddd = 2345;",
8438                Alignment);
8439   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8440   verifyFormat("#define A                                                      "
8441                "                \\\n"
8442                "  int aaaa       = 12;                                         "
8443                "                \\\n"
8444                "  int b          = 23;                                         "
8445                "                \\\n"
8446                "  int ccc        = 234;                                        "
8447                "                \\\n"
8448                "  int dddddddddd = 2345;",
8449                Alignment);
8450   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8451                "k = 4, int l = 5,\n"
8452                "                  int m = 6) {\n"
8453                "  int j      = 10;\n"
8454                "  otherThing = 1;\n"
8455                "}",
8456                Alignment);
8457   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8458                "  int i   = 1;\n"
8459                "  int j   = 2;\n"
8460                "  int big = 10000;\n"
8461                "}",
8462                Alignment);
8463   verifyFormat("class C {\n"
8464                "public:\n"
8465                "  int i            = 1;\n"
8466                "  virtual void f() = 0;\n"
8467                "};",
8468                Alignment);
8469   verifyFormat("int i = 1;\n"
8470                "if (SomeType t = getSomething()) {\n"
8471                "}\n"
8472                "int j   = 2;\n"
8473                "int big = 10000;",
8474                Alignment);
8475   verifyFormat("int j = 7;\n"
8476                "for (int k = 0; k < N; ++k) {\n"
8477                "}\n"
8478                "int j   = 2;\n"
8479                "int big = 10000;\n"
8480                "}",
8481                Alignment);
8482   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8483   verifyFormat("int i = 1;\n"
8484                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8485                "    = someLooooooooooooooooongFunction();\n"
8486                "int j = 2;",
8487                Alignment);
8488   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8489   verifyFormat("int i = 1;\n"
8490                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8491                "    someLooooooooooooooooongFunction();\n"
8492                "int j = 2;",
8493                Alignment);
8494 
8495   verifyFormat("auto lambda = []() {\n"
8496                "  auto i = 0;\n"
8497                "  return 0;\n"
8498                "};\n"
8499                "int i  = 0;\n"
8500                "auto v = type{\n"
8501                "    i = 1,   //\n"
8502                "    (i = 2), //\n"
8503                "    i = 3    //\n"
8504                "};",
8505                Alignment);
8506 
8507   verifyFormat(
8508       "int i      = 1;\n"
8509       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8510       "                          loooooooooooooooooooooongParameterB);\n"
8511       "int j      = 2;",
8512       Alignment);
8513 
8514   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
8515                "          typename B   = very_long_type_name_1,\n"
8516                "          typename T_2 = very_long_type_name_2>\n"
8517                "auto foo() {}\n",
8518                Alignment);
8519   verifyFormat("int a, b = 1;\n"
8520                "int c  = 2;\n"
8521                "int dd = 3;\n",
8522                Alignment);
8523   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
8524                "float b[1][] = {{3.f}};\n",
8525                Alignment);
8526   verifyFormat("for (int i = 0; i < 1; i++)\n"
8527                "  int x = 1;\n",
8528                Alignment);
8529   verifyFormat("for (i = 0; i < 1; i++)\n"
8530                "  x = 1;\n"
8531                "y = 1;\n",
8532                Alignment);
8533 }
8534 
8535 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
8536   FormatStyle Alignment = getLLVMStyle();
8537   Alignment.AlignConsecutiveDeclarations = false;
8538   verifyFormat("float const a = 5;\n"
8539                "int oneTwoThree = 123;",
8540                Alignment);
8541   verifyFormat("int a = 5;\n"
8542                "float const oneTwoThree = 123;",
8543                Alignment);
8544 
8545   Alignment.AlignConsecutiveDeclarations = true;
8546   verifyFormat("float const a = 5;\n"
8547                "int         oneTwoThree = 123;",
8548                Alignment);
8549   verifyFormat("int         a = method();\n"
8550                "float const oneTwoThree = 133;",
8551                Alignment);
8552   verifyFormat("int i = 1, j = 10;\n"
8553                "something = 2000;",
8554                Alignment);
8555   verifyFormat("something = 2000;\n"
8556                "int i = 1, j = 10;\n",
8557                Alignment);
8558   verifyFormat("float      something = 2000;\n"
8559                "double     another = 911;\n"
8560                "int        i = 1, j = 10;\n"
8561                "const int *oneMore = 1;\n"
8562                "unsigned   i = 2;",
8563                Alignment);
8564   verifyFormat("float a = 5;\n"
8565                "int   one = 1;\n"
8566                "method();\n"
8567                "const double       oneTwoThree = 123;\n"
8568                "const unsigned int oneTwo = 12;",
8569                Alignment);
8570   verifyFormat("int      oneTwoThree{0}; // comment\n"
8571                "unsigned oneTwo;         // comment",
8572                Alignment);
8573   EXPECT_EQ("float const a = 5;\n"
8574             "\n"
8575             "int oneTwoThree = 123;",
8576             format("float const   a = 5;\n"
8577                    "\n"
8578                    "int           oneTwoThree= 123;",
8579                    Alignment));
8580   EXPECT_EQ("float a = 5;\n"
8581             "int   one = 1;\n"
8582             "\n"
8583             "unsigned oneTwoThree = 123;",
8584             format("float    a = 5;\n"
8585                    "int      one = 1;\n"
8586                    "\n"
8587                    "unsigned oneTwoThree = 123;",
8588                    Alignment));
8589   EXPECT_EQ("float a = 5;\n"
8590             "int   one = 1;\n"
8591             "\n"
8592             "unsigned oneTwoThree = 123;\n"
8593             "int      oneTwo = 12;",
8594             format("float    a = 5;\n"
8595                    "int one = 1;\n"
8596                    "\n"
8597                    "unsigned oneTwoThree = 123;\n"
8598                    "int oneTwo = 12;",
8599                    Alignment));
8600   // Function prototype alignment
8601   verifyFormat("int    a();\n"
8602                "double b();",
8603                Alignment);
8604   verifyFormat("int    a(int x);\n"
8605                "double b();",
8606                Alignment);
8607   unsigned OldColumnLimit = Alignment.ColumnLimit;
8608   // We need to set ColumnLimit to zero, in order to stress nested alignments,
8609   // otherwise the function parameters will be re-flowed onto a single line.
8610   Alignment.ColumnLimit = 0;
8611   EXPECT_EQ("int    a(int   x,\n"
8612             "         float y);\n"
8613             "double b(int    x,\n"
8614             "         double y);",
8615             format("int a(int x,\n"
8616                    " float y);\n"
8617                    "double b(int x,\n"
8618                    " double y);",
8619                    Alignment));
8620   // This ensures that function parameters of function declarations are
8621   // correctly indented when their owning functions are indented.
8622   // The failure case here is for 'double y' to not be indented enough.
8623   EXPECT_EQ("double a(int x);\n"
8624             "int    b(int    y,\n"
8625             "         double z);",
8626             format("double a(int x);\n"
8627                    "int b(int y,\n"
8628                    " double z);",
8629                    Alignment));
8630   // Set ColumnLimit low so that we induce wrapping immediately after
8631   // the function name and opening paren.
8632   Alignment.ColumnLimit = 13;
8633   verifyFormat("int function(\n"
8634                "    int  x,\n"
8635                "    bool y);",
8636                Alignment);
8637   Alignment.ColumnLimit = OldColumnLimit;
8638   // Ensure function pointers don't screw up recursive alignment
8639   verifyFormat("int    a(int x, void (*fp)(int y));\n"
8640                "double b();",
8641                Alignment);
8642   Alignment.AlignConsecutiveAssignments = true;
8643   // Ensure recursive alignment is broken by function braces, so that the
8644   // "a = 1" does not align with subsequent assignments inside the function
8645   // body.
8646   verifyFormat("int func(int a = 1) {\n"
8647                "  int b  = 2;\n"
8648                "  int cc = 3;\n"
8649                "}",
8650                Alignment);
8651   verifyFormat("float      something = 2000;\n"
8652                "double     another   = 911;\n"
8653                "int        i = 1, j = 10;\n"
8654                "const int *oneMore = 1;\n"
8655                "unsigned   i       = 2;",
8656                Alignment);
8657   verifyFormat("int      oneTwoThree = {0}; // comment\n"
8658                "unsigned oneTwo      = 0;   // comment",
8659                Alignment);
8660   // Make sure that scope is correctly tracked, in the absence of braces
8661   verifyFormat("for (int i = 0; i < n; i++)\n"
8662                "  j = i;\n"
8663                "double x = 1;\n",
8664                Alignment);
8665   verifyFormat("if (int i = 0)\n"
8666                "  j = i;\n"
8667                "double x = 1;\n",
8668                Alignment);
8669   // Ensure operator[] and operator() are comprehended
8670   verifyFormat("struct test {\n"
8671                "  long long int foo();\n"
8672                "  int           operator[](int a);\n"
8673                "  double        bar();\n"
8674                "};\n",
8675                Alignment);
8676   verifyFormat("struct test {\n"
8677                "  long long int foo();\n"
8678                "  int           operator()(int a);\n"
8679                "  double        bar();\n"
8680                "};\n",
8681                Alignment);
8682   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
8683             "  int const i   = 1;\n"
8684             "  int *     j   = 2;\n"
8685             "  int       big = 10000;\n"
8686             "\n"
8687             "  unsigned oneTwoThree = 123;\n"
8688             "  int      oneTwo      = 12;\n"
8689             "  method();\n"
8690             "  float k  = 2;\n"
8691             "  int   ll = 10000;\n"
8692             "}",
8693             format("void SomeFunction(int parameter= 0) {\n"
8694                    " int const  i= 1;\n"
8695                    "  int *j=2;\n"
8696                    " int big  =  10000;\n"
8697                    "\n"
8698                    "unsigned oneTwoThree  =123;\n"
8699                    "int oneTwo = 12;\n"
8700                    "  method();\n"
8701                    "float k= 2;\n"
8702                    "int ll=10000;\n"
8703                    "}",
8704                    Alignment));
8705   Alignment.AlignConsecutiveAssignments = false;
8706   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
8707   verifyFormat("#define A \\\n"
8708                "  int       aaaa = 12; \\\n"
8709                "  float     b = 23; \\\n"
8710                "  const int ccc = 234; \\\n"
8711                "  unsigned  dddddddddd = 2345;",
8712                Alignment);
8713   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Left;
8714   verifyFormat("#define A              \\\n"
8715                "  int       aaaa = 12; \\\n"
8716                "  float     b = 23;    \\\n"
8717                "  const int ccc = 234; \\\n"
8718                "  unsigned  dddddddddd = 2345;",
8719                Alignment);
8720   Alignment.AlignEscapedNewlines = FormatStyle::ENAS_Right;
8721   Alignment.ColumnLimit = 30;
8722   verifyFormat("#define A                    \\\n"
8723                "  int       aaaa = 12;       \\\n"
8724                "  float     b = 23;          \\\n"
8725                "  const int ccc = 234;       \\\n"
8726                "  int       dddddddddd = 2345;",
8727                Alignment);
8728   Alignment.ColumnLimit = 80;
8729   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8730                "k = 4, int l = 5,\n"
8731                "                  int m = 6) {\n"
8732                "  const int j = 10;\n"
8733                "  otherThing = 1;\n"
8734                "}",
8735                Alignment);
8736   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8737                "  int const i = 1;\n"
8738                "  int *     j = 2;\n"
8739                "  int       big = 10000;\n"
8740                "}",
8741                Alignment);
8742   verifyFormat("class C {\n"
8743                "public:\n"
8744                "  int          i = 1;\n"
8745                "  virtual void f() = 0;\n"
8746                "};",
8747                Alignment);
8748   verifyFormat("float i = 1;\n"
8749                "if (SomeType t = getSomething()) {\n"
8750                "}\n"
8751                "const unsigned j = 2;\n"
8752                "int            big = 10000;",
8753                Alignment);
8754   verifyFormat("float j = 7;\n"
8755                "for (int k = 0; k < N; ++k) {\n"
8756                "}\n"
8757                "unsigned j = 2;\n"
8758                "int      big = 10000;\n"
8759                "}",
8760                Alignment);
8761   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8762   verifyFormat("float              i = 1;\n"
8763                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8764                "    = someLooooooooooooooooongFunction();\n"
8765                "int j = 2;",
8766                Alignment);
8767   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8768   verifyFormat("int                i = 1;\n"
8769                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8770                "    someLooooooooooooooooongFunction();\n"
8771                "int j = 2;",
8772                Alignment);
8773 
8774   Alignment.AlignConsecutiveAssignments = true;
8775   verifyFormat("auto lambda = []() {\n"
8776                "  auto  ii = 0;\n"
8777                "  float j  = 0;\n"
8778                "  return 0;\n"
8779                "};\n"
8780                "int   i  = 0;\n"
8781                "float i2 = 0;\n"
8782                "auto  v  = type{\n"
8783                "    i = 1,   //\n"
8784                "    (i = 2), //\n"
8785                "    i = 3    //\n"
8786                "};",
8787                Alignment);
8788   Alignment.AlignConsecutiveAssignments = false;
8789 
8790   verifyFormat(
8791       "int      i = 1;\n"
8792       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8793       "                          loooooooooooooooooooooongParameterB);\n"
8794       "int      j = 2;",
8795       Alignment);
8796 
8797   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
8798   // We expect declarations and assignments to align, as long as it doesn't
8799   // exceed the column limit, starting a new alignment sequence whenever it
8800   // happens.
8801   Alignment.AlignConsecutiveAssignments = true;
8802   Alignment.ColumnLimit = 30;
8803   verifyFormat("float    ii              = 1;\n"
8804                "unsigned j               = 2;\n"
8805                "int someVerylongVariable = 1;\n"
8806                "AnotherLongType  ll = 123456;\n"
8807                "VeryVeryLongType k  = 2;\n"
8808                "int              myvar = 1;",
8809                Alignment);
8810   Alignment.ColumnLimit = 80;
8811   Alignment.AlignConsecutiveAssignments = false;
8812 
8813   verifyFormat(
8814       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
8815       "          typename LongType, typename B>\n"
8816       "auto foo() {}\n",
8817       Alignment);
8818   verifyFormat("float a, b = 1;\n"
8819                "int   c = 2;\n"
8820                "int   dd = 3;\n",
8821                Alignment);
8822   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
8823                "float b[1][] = {{3.f}};\n",
8824                Alignment);
8825   Alignment.AlignConsecutiveAssignments = true;
8826   verifyFormat("float a, b = 1;\n"
8827                "int   c  = 2;\n"
8828                "int   dd = 3;\n",
8829                Alignment);
8830   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
8831                "float b[1][] = {{3.f}};\n",
8832                Alignment);
8833   Alignment.AlignConsecutiveAssignments = false;
8834 
8835   Alignment.ColumnLimit = 30;
8836   Alignment.BinPackParameters = false;
8837   verifyFormat("void foo(float     a,\n"
8838                "         float     b,\n"
8839                "         int       c,\n"
8840                "         uint32_t *d) {\n"
8841                "  int *  e = 0;\n"
8842                "  float  f = 0;\n"
8843                "  double g = 0;\n"
8844                "}\n"
8845                "void bar(ino_t     a,\n"
8846                "         int       b,\n"
8847                "         uint32_t *c,\n"
8848                "         bool      d) {}\n",
8849                Alignment);
8850   Alignment.BinPackParameters = true;
8851   Alignment.ColumnLimit = 80;
8852 }
8853 
8854 TEST_F(FormatTest, LinuxBraceBreaking) {
8855   FormatStyle LinuxBraceStyle = getLLVMStyle();
8856   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8857   verifyFormat("namespace a\n"
8858                "{\n"
8859                "class A\n"
8860                "{\n"
8861                "  void f()\n"
8862                "  {\n"
8863                "    if (true) {\n"
8864                "      a();\n"
8865                "      b();\n"
8866                "    } else {\n"
8867                "      a();\n"
8868                "    }\n"
8869                "  }\n"
8870                "  void g() { return; }\n"
8871                "};\n"
8872                "struct B {\n"
8873                "  int x;\n"
8874                "};\n"
8875                "}\n",
8876                LinuxBraceStyle);
8877   verifyFormat("enum X {\n"
8878                "  Y = 0,\n"
8879                "}\n",
8880                LinuxBraceStyle);
8881   verifyFormat("struct S {\n"
8882                "  int Type;\n"
8883                "  union {\n"
8884                "    int x;\n"
8885                "    double y;\n"
8886                "  } Value;\n"
8887                "  class C\n"
8888                "  {\n"
8889                "    MyFavoriteType Value;\n"
8890                "  } Class;\n"
8891                "}\n",
8892                LinuxBraceStyle);
8893 }
8894 
8895 TEST_F(FormatTest, MozillaBraceBreaking) {
8896   FormatStyle MozillaBraceStyle = getLLVMStyle();
8897   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8898   MozillaBraceStyle.FixNamespaceComments = false;
8899   verifyFormat("namespace a {\n"
8900                "class A\n"
8901                "{\n"
8902                "  void f()\n"
8903                "  {\n"
8904                "    if (true) {\n"
8905                "      a();\n"
8906                "      b();\n"
8907                "    }\n"
8908                "  }\n"
8909                "  void g() { return; }\n"
8910                "};\n"
8911                "enum E\n"
8912                "{\n"
8913                "  A,\n"
8914                "  // foo\n"
8915                "  B,\n"
8916                "  C\n"
8917                "};\n"
8918                "struct B\n"
8919                "{\n"
8920                "  int x;\n"
8921                "};\n"
8922                "}\n",
8923                MozillaBraceStyle);
8924   verifyFormat("struct S\n"
8925                "{\n"
8926                "  int Type;\n"
8927                "  union\n"
8928                "  {\n"
8929                "    int x;\n"
8930                "    double y;\n"
8931                "  } Value;\n"
8932                "  class C\n"
8933                "  {\n"
8934                "    MyFavoriteType Value;\n"
8935                "  } Class;\n"
8936                "}\n",
8937                MozillaBraceStyle);
8938 }
8939 
8940 TEST_F(FormatTest, StroustrupBraceBreaking) {
8941   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8942   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8943   verifyFormat("namespace a {\n"
8944                "class A {\n"
8945                "  void f()\n"
8946                "  {\n"
8947                "    if (true) {\n"
8948                "      a();\n"
8949                "      b();\n"
8950                "    }\n"
8951                "  }\n"
8952                "  void g() { return; }\n"
8953                "};\n"
8954                "struct B {\n"
8955                "  int x;\n"
8956                "};\n"
8957                "} // namespace a\n",
8958                StroustrupBraceStyle);
8959 
8960   verifyFormat("void foo()\n"
8961                "{\n"
8962                "  if (a) {\n"
8963                "    a();\n"
8964                "  }\n"
8965                "  else {\n"
8966                "    b();\n"
8967                "  }\n"
8968                "}\n",
8969                StroustrupBraceStyle);
8970 
8971   verifyFormat("#ifdef _DEBUG\n"
8972                "int foo(int i = 0)\n"
8973                "#else\n"
8974                "int foo(int i = 5)\n"
8975                "#endif\n"
8976                "{\n"
8977                "  return i;\n"
8978                "}",
8979                StroustrupBraceStyle);
8980 
8981   verifyFormat("void foo() {}\n"
8982                "void bar()\n"
8983                "#ifdef _DEBUG\n"
8984                "{\n"
8985                "  foo();\n"
8986                "}\n"
8987                "#else\n"
8988                "{\n"
8989                "}\n"
8990                "#endif",
8991                StroustrupBraceStyle);
8992 
8993   verifyFormat("void foobar() { int i = 5; }\n"
8994                "#ifdef _DEBUG\n"
8995                "void bar() {}\n"
8996                "#else\n"
8997                "void bar() { foobar(); }\n"
8998                "#endif",
8999                StroustrupBraceStyle);
9000 }
9001 
9002 TEST_F(FormatTest, AllmanBraceBreaking) {
9003   FormatStyle AllmanBraceStyle = getLLVMStyle();
9004   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
9005   verifyFormat("namespace a\n"
9006                "{\n"
9007                "class A\n"
9008                "{\n"
9009                "  void f()\n"
9010                "  {\n"
9011                "    if (true)\n"
9012                "    {\n"
9013                "      a();\n"
9014                "      b();\n"
9015                "    }\n"
9016                "  }\n"
9017                "  void g() { return; }\n"
9018                "};\n"
9019                "struct B\n"
9020                "{\n"
9021                "  int x;\n"
9022                "};\n"
9023                "}",
9024                AllmanBraceStyle);
9025 
9026   verifyFormat("void f()\n"
9027                "{\n"
9028                "  if (true)\n"
9029                "  {\n"
9030                "    a();\n"
9031                "  }\n"
9032                "  else if (false)\n"
9033                "  {\n"
9034                "    b();\n"
9035                "  }\n"
9036                "  else\n"
9037                "  {\n"
9038                "    c();\n"
9039                "  }\n"
9040                "}\n",
9041                AllmanBraceStyle);
9042 
9043   verifyFormat("void f()\n"
9044                "{\n"
9045                "  for (int i = 0; i < 10; ++i)\n"
9046                "  {\n"
9047                "    a();\n"
9048                "  }\n"
9049                "  while (false)\n"
9050                "  {\n"
9051                "    b();\n"
9052                "  }\n"
9053                "  do\n"
9054                "  {\n"
9055                "    c();\n"
9056                "  } while (false)\n"
9057                "}\n",
9058                AllmanBraceStyle);
9059 
9060   verifyFormat("void f(int a)\n"
9061                "{\n"
9062                "  switch (a)\n"
9063                "  {\n"
9064                "  case 0:\n"
9065                "    break;\n"
9066                "  case 1:\n"
9067                "  {\n"
9068                "    break;\n"
9069                "  }\n"
9070                "  case 2:\n"
9071                "  {\n"
9072                "  }\n"
9073                "  break;\n"
9074                "  default:\n"
9075                "    break;\n"
9076                "  }\n"
9077                "}\n",
9078                AllmanBraceStyle);
9079 
9080   verifyFormat("enum X\n"
9081                "{\n"
9082                "  Y = 0,\n"
9083                "}\n",
9084                AllmanBraceStyle);
9085   verifyFormat("enum X\n"
9086                "{\n"
9087                "  Y = 0\n"
9088                "}\n",
9089                AllmanBraceStyle);
9090 
9091   verifyFormat("@interface BSApplicationController ()\n"
9092                "{\n"
9093                "@private\n"
9094                "  id _extraIvar;\n"
9095                "}\n"
9096                "@end\n",
9097                AllmanBraceStyle);
9098 
9099   verifyFormat("#ifdef _DEBUG\n"
9100                "int foo(int i = 0)\n"
9101                "#else\n"
9102                "int foo(int i = 5)\n"
9103                "#endif\n"
9104                "{\n"
9105                "  return i;\n"
9106                "}",
9107                AllmanBraceStyle);
9108 
9109   verifyFormat("void foo() {}\n"
9110                "void bar()\n"
9111                "#ifdef _DEBUG\n"
9112                "{\n"
9113                "  foo();\n"
9114                "}\n"
9115                "#else\n"
9116                "{\n"
9117                "}\n"
9118                "#endif",
9119                AllmanBraceStyle);
9120 
9121   verifyFormat("void foobar() { int i = 5; }\n"
9122                "#ifdef _DEBUG\n"
9123                "void bar() {}\n"
9124                "#else\n"
9125                "void bar() { foobar(); }\n"
9126                "#endif",
9127                AllmanBraceStyle);
9128 
9129   // This shouldn't affect ObjC blocks..
9130   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
9131                "  // ...\n"
9132                "  int i;\n"
9133                "}];",
9134                AllmanBraceStyle);
9135   verifyFormat("void (^block)(void) = ^{\n"
9136                "  // ...\n"
9137                "  int i;\n"
9138                "};",
9139                AllmanBraceStyle);
9140   // .. or dict literals.
9141   verifyFormat("void f()\n"
9142                "{\n"
9143                "  // ...\n"
9144                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
9145                "}",
9146                AllmanBraceStyle);
9147   verifyFormat("void f()\n"
9148                "{\n"
9149                "  // ...\n"
9150                "  [object someMethod:@{a : @\"b\"}];\n"
9151                "}",
9152                AllmanBraceStyle);
9153   verifyFormat("int f()\n"
9154                "{ // comment\n"
9155                "  return 42;\n"
9156                "}",
9157                AllmanBraceStyle);
9158 
9159   AllmanBraceStyle.ColumnLimit = 19;
9160   verifyFormat("void f() { int i; }", AllmanBraceStyle);
9161   AllmanBraceStyle.ColumnLimit = 18;
9162   verifyFormat("void f()\n"
9163                "{\n"
9164                "  int i;\n"
9165                "}",
9166                AllmanBraceStyle);
9167   AllmanBraceStyle.ColumnLimit = 80;
9168 
9169   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
9170   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
9171   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
9172   verifyFormat("void f(bool b)\n"
9173                "{\n"
9174                "  if (b)\n"
9175                "  {\n"
9176                "    return;\n"
9177                "  }\n"
9178                "}\n",
9179                BreakBeforeBraceShortIfs);
9180   verifyFormat("void f(bool b)\n"
9181                "{\n"
9182                "  if constexpr (b)\n"
9183                "  {\n"
9184                "    return;\n"
9185                "  }\n"
9186                "}\n",
9187                BreakBeforeBraceShortIfs);
9188   verifyFormat("void f(bool b)\n"
9189                "{\n"
9190                "  if (b) return;\n"
9191                "}\n",
9192                BreakBeforeBraceShortIfs);
9193   verifyFormat("void f(bool b)\n"
9194                "{\n"
9195                "  if constexpr (b) return;\n"
9196                "}\n",
9197                BreakBeforeBraceShortIfs);
9198   verifyFormat("void f(bool b)\n"
9199                "{\n"
9200                "  while (b)\n"
9201                "  {\n"
9202                "    return;\n"
9203                "  }\n"
9204                "}\n",
9205                BreakBeforeBraceShortIfs);
9206 }
9207 
9208 TEST_F(FormatTest, GNUBraceBreaking) {
9209   FormatStyle GNUBraceStyle = getLLVMStyle();
9210   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
9211   verifyFormat("namespace a\n"
9212                "{\n"
9213                "class A\n"
9214                "{\n"
9215                "  void f()\n"
9216                "  {\n"
9217                "    int a;\n"
9218                "    {\n"
9219                "      int b;\n"
9220                "    }\n"
9221                "    if (true)\n"
9222                "      {\n"
9223                "        a();\n"
9224                "        b();\n"
9225                "      }\n"
9226                "  }\n"
9227                "  void g() { return; }\n"
9228                "}\n"
9229                "}",
9230                GNUBraceStyle);
9231 
9232   verifyFormat("void f()\n"
9233                "{\n"
9234                "  if (true)\n"
9235                "    {\n"
9236                "      a();\n"
9237                "    }\n"
9238                "  else if (false)\n"
9239                "    {\n"
9240                "      b();\n"
9241                "    }\n"
9242                "  else\n"
9243                "    {\n"
9244                "      c();\n"
9245                "    }\n"
9246                "}\n",
9247                GNUBraceStyle);
9248 
9249   verifyFormat("void f()\n"
9250                "{\n"
9251                "  for (int i = 0; i < 10; ++i)\n"
9252                "    {\n"
9253                "      a();\n"
9254                "    }\n"
9255                "  while (false)\n"
9256                "    {\n"
9257                "      b();\n"
9258                "    }\n"
9259                "  do\n"
9260                "    {\n"
9261                "      c();\n"
9262                "    }\n"
9263                "  while (false);\n"
9264                "}\n",
9265                GNUBraceStyle);
9266 
9267   verifyFormat("void f(int a)\n"
9268                "{\n"
9269                "  switch (a)\n"
9270                "    {\n"
9271                "    case 0:\n"
9272                "      break;\n"
9273                "    case 1:\n"
9274                "      {\n"
9275                "        break;\n"
9276                "      }\n"
9277                "    case 2:\n"
9278                "      {\n"
9279                "      }\n"
9280                "      break;\n"
9281                "    default:\n"
9282                "      break;\n"
9283                "    }\n"
9284                "}\n",
9285                GNUBraceStyle);
9286 
9287   verifyFormat("enum X\n"
9288                "{\n"
9289                "  Y = 0,\n"
9290                "}\n",
9291                GNUBraceStyle);
9292 
9293   verifyFormat("@interface BSApplicationController ()\n"
9294                "{\n"
9295                "@private\n"
9296                "  id _extraIvar;\n"
9297                "}\n"
9298                "@end\n",
9299                GNUBraceStyle);
9300 
9301   verifyFormat("#ifdef _DEBUG\n"
9302                "int foo(int i = 0)\n"
9303                "#else\n"
9304                "int foo(int i = 5)\n"
9305                "#endif\n"
9306                "{\n"
9307                "  return i;\n"
9308                "}",
9309                GNUBraceStyle);
9310 
9311   verifyFormat("void foo() {}\n"
9312                "void bar()\n"
9313                "#ifdef _DEBUG\n"
9314                "{\n"
9315                "  foo();\n"
9316                "}\n"
9317                "#else\n"
9318                "{\n"
9319                "}\n"
9320                "#endif",
9321                GNUBraceStyle);
9322 
9323   verifyFormat("void foobar() { int i = 5; }\n"
9324                "#ifdef _DEBUG\n"
9325                "void bar() {}\n"
9326                "#else\n"
9327                "void bar() { foobar(); }\n"
9328                "#endif",
9329                GNUBraceStyle);
9330 }
9331 
9332 TEST_F(FormatTest, WebKitBraceBreaking) {
9333   FormatStyle WebKitBraceStyle = getLLVMStyle();
9334   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
9335   WebKitBraceStyle.FixNamespaceComments = false;
9336   verifyFormat("namespace a {\n"
9337                "class A {\n"
9338                "  void f()\n"
9339                "  {\n"
9340                "    if (true) {\n"
9341                "      a();\n"
9342                "      b();\n"
9343                "    }\n"
9344                "  }\n"
9345                "  void g() { return; }\n"
9346                "};\n"
9347                "enum E {\n"
9348                "  A,\n"
9349                "  // foo\n"
9350                "  B,\n"
9351                "  C\n"
9352                "};\n"
9353                "struct B {\n"
9354                "  int x;\n"
9355                "};\n"
9356                "}\n",
9357                WebKitBraceStyle);
9358   verifyFormat("struct S {\n"
9359                "  int Type;\n"
9360                "  union {\n"
9361                "    int x;\n"
9362                "    double y;\n"
9363                "  } Value;\n"
9364                "  class C {\n"
9365                "    MyFavoriteType Value;\n"
9366                "  } Class;\n"
9367                "};\n",
9368                WebKitBraceStyle);
9369 }
9370 
9371 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
9372   verifyFormat("void f() {\n"
9373                "  try {\n"
9374                "  } catch (const Exception &e) {\n"
9375                "  }\n"
9376                "}\n",
9377                getLLVMStyle());
9378 }
9379 
9380 TEST_F(FormatTest, UnderstandsPragmas) {
9381   verifyFormat("#pragma omp reduction(| : var)");
9382   verifyFormat("#pragma omp reduction(+ : var)");
9383 
9384   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
9385             "(including parentheses).",
9386             format("#pragma    mark   Any non-hyphenated or hyphenated string "
9387                    "(including parentheses)."));
9388 }
9389 
9390 TEST_F(FormatTest, UnderstandPragmaOption) {
9391   verifyFormat("#pragma option -C -A");
9392 
9393   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
9394 }
9395 
9396 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
9397   for (size_t i = 1; i < Styles.size(); ++i)                                   \
9398   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
9399                                   << " differs from Style #0"
9400 
9401 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9402   SmallVector<FormatStyle, 3> Styles;
9403   Styles.resize(3);
9404 
9405   Styles[0] = getLLVMStyle();
9406   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9407   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9408   EXPECT_ALL_STYLES_EQUAL(Styles);
9409 
9410   Styles[0] = getGoogleStyle();
9411   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9412   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9413   EXPECT_ALL_STYLES_EQUAL(Styles);
9414 
9415   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9416   EXPECT_TRUE(
9417       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9418   EXPECT_TRUE(
9419       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9420   EXPECT_ALL_STYLES_EQUAL(Styles);
9421 
9422   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9423   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9424   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9425   EXPECT_ALL_STYLES_EQUAL(Styles);
9426 
9427   Styles[0] = getMozillaStyle();
9428   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9429   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9430   EXPECT_ALL_STYLES_EQUAL(Styles);
9431 
9432   Styles[0] = getWebKitStyle();
9433   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9434   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9435   EXPECT_ALL_STYLES_EQUAL(Styles);
9436 
9437   Styles[0] = getGNUStyle();
9438   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9439   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9440   EXPECT_ALL_STYLES_EQUAL(Styles);
9441 
9442   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9443 }
9444 
9445 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9446   SmallVector<FormatStyle, 8> Styles;
9447   Styles.resize(2);
9448 
9449   Styles[0] = getGoogleStyle();
9450   Styles[1] = getLLVMStyle();
9451   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9452   EXPECT_ALL_STYLES_EQUAL(Styles);
9453 
9454   Styles.resize(5);
9455   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9456   Styles[1] = getLLVMStyle();
9457   Styles[1].Language = FormatStyle::LK_JavaScript;
9458   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9459 
9460   Styles[2] = getLLVMStyle();
9461   Styles[2].Language = FormatStyle::LK_JavaScript;
9462   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9463                                   "BasedOnStyle: Google",
9464                                   &Styles[2])
9465                    .value());
9466 
9467   Styles[3] = getLLVMStyle();
9468   Styles[3].Language = FormatStyle::LK_JavaScript;
9469   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9470                                   "Language: JavaScript",
9471                                   &Styles[3])
9472                    .value());
9473 
9474   Styles[4] = getLLVMStyle();
9475   Styles[4].Language = FormatStyle::LK_JavaScript;
9476   EXPECT_EQ(0, parseConfiguration("---\n"
9477                                   "BasedOnStyle: LLVM\n"
9478                                   "IndentWidth: 123\n"
9479                                   "---\n"
9480                                   "BasedOnStyle: Google\n"
9481                                   "Language: JavaScript",
9482                                   &Styles[4])
9483                    .value());
9484   EXPECT_ALL_STYLES_EQUAL(Styles);
9485 }
9486 
9487 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9488   Style.FIELD = false;                                                         \
9489   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9490   EXPECT_TRUE(Style.FIELD);                                                    \
9491   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9492   EXPECT_FALSE(Style.FIELD);
9493 
9494 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9495 
9496 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
9497   Style.STRUCT.FIELD = false;                                                  \
9498   EXPECT_EQ(0,                                                                 \
9499             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
9500                 .value());                                                     \
9501   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
9502   EXPECT_EQ(0,                                                                 \
9503             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
9504                 .value());                                                     \
9505   EXPECT_FALSE(Style.STRUCT.FIELD);
9506 
9507 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
9508   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
9509 
9510 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9511   EXPECT_NE(VALUE, Style.FIELD);                                               \
9512   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9513   EXPECT_EQ(VALUE, Style.FIELD)
9514 
9515 TEST_F(FormatTest, ParsesConfigurationBools) {
9516   FormatStyle Style = {};
9517   Style.Language = FormatStyle::LK_Cpp;
9518   CHECK_PARSE_BOOL(AlignOperands);
9519   CHECK_PARSE_BOOL(AlignTrailingComments);
9520   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9521   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
9522   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9523   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9524   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9525   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9526   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9527   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9528   CHECK_PARSE_BOOL(BinPackArguments);
9529   CHECK_PARSE_BOOL(BinPackParameters);
9530   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
9531   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9532   CHECK_PARSE_BOOL(BreakStringLiterals);
9533   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
9534   CHECK_PARSE_BOOL(CompactNamespaces);
9535   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9536   CHECK_PARSE_BOOL(DerivePointerAlignment);
9537   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9538   CHECK_PARSE_BOOL(DisableFormat);
9539   CHECK_PARSE_BOOL(IndentCaseLabels);
9540   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9541   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9542   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9543   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9544   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9545   CHECK_PARSE_BOOL(ReflowComments);
9546   CHECK_PARSE_BOOL(SortIncludes);
9547   CHECK_PARSE_BOOL(SortUsingDeclarations);
9548   CHECK_PARSE_BOOL(SpacesInParentheses);
9549   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9550   CHECK_PARSE_BOOL(SpacesInAngles);
9551   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9552   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9553   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9554   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9555   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
9556   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9557 
9558   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
9559   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
9560   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
9561   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
9562   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
9563   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
9564   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
9565   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
9566   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
9567   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
9568   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
9569   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyFunction);
9570   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyRecord);
9571   CHECK_PARSE_NESTED_BOOL(BraceWrapping, SplitEmptyNamespace);
9572 }
9573 
9574 #undef CHECK_PARSE_BOOL
9575 
9576 TEST_F(FormatTest, ParsesConfiguration) {
9577   FormatStyle Style = {};
9578   Style.Language = FormatStyle::LK_Cpp;
9579   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9580   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9581               ConstructorInitializerIndentWidth, 1234u);
9582   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9583   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9584   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9585   CHECK_PARSE("PenaltyBreakAssignment: 1234",
9586               PenaltyBreakAssignment, 1234u);
9587   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9588               PenaltyBreakBeforeFirstCallParameter, 1234u);
9589   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9590   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9591               PenaltyReturnTypeOnItsOwnLine, 1234u);
9592   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9593               SpacesBeforeTrailingComments, 1234u);
9594   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9595   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9596   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
9597 
9598   Style.PointerAlignment = FormatStyle::PAS_Middle;
9599   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9600               FormatStyle::PAS_Left);
9601   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9602               FormatStyle::PAS_Right);
9603   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9604               FormatStyle::PAS_Middle);
9605   // For backward compatibility:
9606   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9607               FormatStyle::PAS_Left);
9608   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9609               FormatStyle::PAS_Right);
9610   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9611               FormatStyle::PAS_Middle);
9612 
9613   Style.Standard = FormatStyle::LS_Auto;
9614   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9615   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9616   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9617   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9618   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9619 
9620   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9621   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9622               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9623   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9624               FormatStyle::BOS_None);
9625   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9626               FormatStyle::BOS_All);
9627   // For backward compatibility:
9628   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9629               FormatStyle::BOS_None);
9630   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9631               FormatStyle::BOS_All);
9632 
9633   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
9634   CHECK_PARSE("BreakConstructorInitializers: BeforeComma",
9635               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9636   CHECK_PARSE("BreakConstructorInitializers: AfterColon",
9637               BreakConstructorInitializers, FormatStyle::BCIS_AfterColon);
9638   CHECK_PARSE("BreakConstructorInitializers: BeforeColon",
9639               BreakConstructorInitializers, FormatStyle::BCIS_BeforeColon);
9640   // For backward compatibility:
9641   CHECK_PARSE("BreakConstructorInitializersBeforeComma: true",
9642               BreakConstructorInitializers, FormatStyle::BCIS_BeforeComma);
9643 
9644   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9645   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
9646               FormatStyle::BAS_Align);
9647   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
9648               FormatStyle::BAS_DontAlign);
9649   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
9650               FormatStyle::BAS_AlwaysBreak);
9651   // For backward compatibility:
9652   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
9653               FormatStyle::BAS_DontAlign);
9654   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
9655               FormatStyle::BAS_Align);
9656 
9657   Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
9658   CHECK_PARSE("AlignEscapedNewlines: DontAlign", AlignEscapedNewlines,
9659               FormatStyle::ENAS_DontAlign);
9660   CHECK_PARSE("AlignEscapedNewlines: Left", AlignEscapedNewlines,
9661               FormatStyle::ENAS_Left);
9662   CHECK_PARSE("AlignEscapedNewlines: Right", AlignEscapedNewlines,
9663               FormatStyle::ENAS_Right);
9664   // For backward compatibility:
9665   CHECK_PARSE("AlignEscapedNewlinesLeft: true", AlignEscapedNewlines,
9666               FormatStyle::ENAS_Left);
9667   CHECK_PARSE("AlignEscapedNewlinesLeft: false", AlignEscapedNewlines,
9668               FormatStyle::ENAS_Right);
9669 
9670   Style.UseTab = FormatStyle::UT_ForIndentation;
9671   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9672   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9673   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9674   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
9675               FormatStyle::UT_ForContinuationAndIndentation);
9676   // For backward compatibility:
9677   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9678   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9679 
9680   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9681   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9682               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9683   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9684               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9685   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9686               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9687   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9688               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9689   // For backward compatibility:
9690   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9691               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9692   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9693               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9694 
9695   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9696   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9697               FormatStyle::SBPO_Never);
9698   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9699               FormatStyle::SBPO_Always);
9700   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9701               FormatStyle::SBPO_ControlStatements);
9702   // For backward compatibility:
9703   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9704               FormatStyle::SBPO_Never);
9705   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9706               FormatStyle::SBPO_ControlStatements);
9707 
9708   Style.ColumnLimit = 123;
9709   FormatStyle BaseStyle = getLLVMStyle();
9710   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9711   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9712 
9713   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9714   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9715               FormatStyle::BS_Attach);
9716   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9717               FormatStyle::BS_Linux);
9718   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
9719               FormatStyle::BS_Mozilla);
9720   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9721               FormatStyle::BS_Stroustrup);
9722   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9723               FormatStyle::BS_Allman);
9724   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9725   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
9726               FormatStyle::BS_WebKit);
9727   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
9728               FormatStyle::BS_Custom);
9729 
9730   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9731   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
9732               FormatStyle::RTBS_None);
9733   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
9734               FormatStyle::RTBS_All);
9735   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
9736               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
9737   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
9738               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
9739   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
9740               AlwaysBreakAfterReturnType,
9741               FormatStyle::RTBS_TopLevelDefinitions);
9742 
9743   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
9744   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
9745               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
9746   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
9747               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
9748   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
9749               AlwaysBreakAfterDefinitionReturnType,
9750               FormatStyle::DRTBS_TopLevel);
9751 
9752   Style.NamespaceIndentation = FormatStyle::NI_All;
9753   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9754               FormatStyle::NI_None);
9755   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9756               FormatStyle::NI_Inner);
9757   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9758               FormatStyle::NI_All);
9759 
9760   // FIXME: This is required because parsing a configuration simply overwrites
9761   // the first N elements of the list instead of resetting it.
9762   Style.ForEachMacros.clear();
9763   std::vector<std::string> BoostForeach;
9764   BoostForeach.push_back("BOOST_FOREACH");
9765   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9766   std::vector<std::string> BoostAndQForeach;
9767   BoostAndQForeach.push_back("BOOST_FOREACH");
9768   BoostAndQForeach.push_back("Q_FOREACH");
9769   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9770               BoostAndQForeach);
9771 
9772   Style.IncludeCategories.clear();
9773   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
9774                                                                   {".*", 1}};
9775   CHECK_PARSE("IncludeCategories:\n"
9776               "  - Regex: abc/.*\n"
9777               "    Priority: 2\n"
9778               "  - Regex: .*\n"
9779               "    Priority: 1",
9780               IncludeCategories, ExpectedCategories);
9781   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
9782 }
9783 
9784 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9785   FormatStyle Style = {};
9786   Style.Language = FormatStyle::LK_Cpp;
9787   CHECK_PARSE("Language: Cpp\n"
9788               "IndentWidth: 12",
9789               IndentWidth, 12u);
9790   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9791                                "IndentWidth: 34",
9792                                &Style),
9793             ParseError::Unsuitable);
9794   EXPECT_EQ(12u, Style.IndentWidth);
9795   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9796   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9797 
9798   Style.Language = FormatStyle::LK_JavaScript;
9799   CHECK_PARSE("Language: JavaScript\n"
9800               "IndentWidth: 12",
9801               IndentWidth, 12u);
9802   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9803   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9804                                "IndentWidth: 34",
9805                                &Style),
9806             ParseError::Unsuitable);
9807   EXPECT_EQ(23u, Style.IndentWidth);
9808   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9809   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9810 
9811   CHECK_PARSE("BasedOnStyle: LLVM\n"
9812               "IndentWidth: 67",
9813               IndentWidth, 67u);
9814 
9815   CHECK_PARSE("---\n"
9816               "Language: JavaScript\n"
9817               "IndentWidth: 12\n"
9818               "---\n"
9819               "Language: Cpp\n"
9820               "IndentWidth: 34\n"
9821               "...\n",
9822               IndentWidth, 12u);
9823 
9824   Style.Language = FormatStyle::LK_Cpp;
9825   CHECK_PARSE("---\n"
9826               "Language: JavaScript\n"
9827               "IndentWidth: 12\n"
9828               "---\n"
9829               "Language: Cpp\n"
9830               "IndentWidth: 34\n"
9831               "...\n",
9832               IndentWidth, 34u);
9833   CHECK_PARSE("---\n"
9834               "IndentWidth: 78\n"
9835               "---\n"
9836               "Language: JavaScript\n"
9837               "IndentWidth: 56\n"
9838               "...\n",
9839               IndentWidth, 78u);
9840 
9841   Style.ColumnLimit = 123;
9842   Style.IndentWidth = 234;
9843   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9844   Style.TabWidth = 345;
9845   EXPECT_FALSE(parseConfiguration("---\n"
9846                                   "IndentWidth: 456\n"
9847                                   "BreakBeforeBraces: Allman\n"
9848                                   "---\n"
9849                                   "Language: JavaScript\n"
9850                                   "IndentWidth: 111\n"
9851                                   "TabWidth: 111\n"
9852                                   "---\n"
9853                                   "Language: Cpp\n"
9854                                   "BreakBeforeBraces: Stroustrup\n"
9855                                   "TabWidth: 789\n"
9856                                   "...\n",
9857                                   &Style));
9858   EXPECT_EQ(123u, Style.ColumnLimit);
9859   EXPECT_EQ(456u, Style.IndentWidth);
9860   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9861   EXPECT_EQ(789u, Style.TabWidth);
9862 
9863   EXPECT_EQ(parseConfiguration("---\n"
9864                                "Language: JavaScript\n"
9865                                "IndentWidth: 56\n"
9866                                "---\n"
9867                                "IndentWidth: 78\n"
9868                                "...\n",
9869                                &Style),
9870             ParseError::Error);
9871   EXPECT_EQ(parseConfiguration("---\n"
9872                                "Language: JavaScript\n"
9873                                "IndentWidth: 56\n"
9874                                "---\n"
9875                                "Language: JavaScript\n"
9876                                "IndentWidth: 78\n"
9877                                "...\n",
9878                                &Style),
9879             ParseError::Error);
9880 
9881   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9882 }
9883 
9884 #undef CHECK_PARSE
9885 
9886 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9887   FormatStyle Style = {};
9888   Style.Language = FormatStyle::LK_JavaScript;
9889   Style.BreakBeforeTernaryOperators = true;
9890   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9891   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9892 
9893   Style.BreakBeforeTernaryOperators = true;
9894   EXPECT_EQ(0, parseConfiguration("---\n"
9895                                   "BasedOnStyle: Google\n"
9896                                   "---\n"
9897                                   "Language: JavaScript\n"
9898                                   "IndentWidth: 76\n"
9899                                   "...\n",
9900                                   &Style)
9901                    .value());
9902   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9903   EXPECT_EQ(76u, Style.IndentWidth);
9904   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9905 }
9906 
9907 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9908   FormatStyle Style = getLLVMStyle();
9909   std::string YAML = configurationAsText(Style);
9910   FormatStyle ParsedStyle = {};
9911   ParsedStyle.Language = FormatStyle::LK_Cpp;
9912   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9913   EXPECT_EQ(Style, ParsedStyle);
9914 }
9915 
9916 TEST_F(FormatTest, WorksFor8bitEncodings) {
9917   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9918             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9919             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9920             "\"\xef\xee\xf0\xf3...\"",
9921             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9922                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9923                    "\xef\xee\xf0\xf3...\"",
9924                    getLLVMStyleWithColumns(12)));
9925 }
9926 
9927 TEST_F(FormatTest, HandlesUTF8BOM) {
9928   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9929   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9930             format("\xef\xbb\xbf#include <iostream>"));
9931   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9932             format("\xef\xbb\xbf\n#include <iostream>"));
9933 }
9934 
9935 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9936 #if !defined(_MSC_VER)
9937 
9938 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9939   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9940                getLLVMStyleWithColumns(35));
9941   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9942                getLLVMStyleWithColumns(31));
9943   verifyFormat("// Однажды в студёную зимнюю пору...",
9944                getLLVMStyleWithColumns(36));
9945   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9946   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9947                getLLVMStyleWithColumns(39));
9948   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9949                getLLVMStyleWithColumns(35));
9950 }
9951 
9952 TEST_F(FormatTest, SplitsUTF8Strings) {
9953   // Non-printable characters' width is currently considered to be the length in
9954   // bytes in UTF8. The characters can be displayed in very different manner
9955   // (zero-width, single width with a substitution glyph, expanded to their code
9956   // (e.g. "<8d>"), so there's no single correct way to handle them.
9957   EXPECT_EQ("\"aaaaÄ\"\n"
9958             "\"\xc2\x8d\";",
9959             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9960   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9961             "\"\xc2\x8d\";",
9962             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9963   EXPECT_EQ("\"Однажды, в \"\n"
9964             "\"студёную \"\n"
9965             "\"зимнюю \"\n"
9966             "\"пору,\"",
9967             format("\"Однажды, в студёную зимнюю пору,\"",
9968                    getLLVMStyleWithColumns(13)));
9969   EXPECT_EQ(
9970       "\"一 二 三 \"\n"
9971       "\"四 五六 \"\n"
9972       "\"七 八 九 \"\n"
9973       "\"十\"",
9974       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9975   EXPECT_EQ("\"一\t二 \"\n"
9976             "\"\t三 \"\n"
9977             "\"四 五\t六 \"\n"
9978             "\"\t七 \"\n"
9979             "\"八九十\tqq\"",
9980             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9981                    getLLVMStyleWithColumns(11)));
9982 
9983   // UTF8 character in an escape sequence.
9984   EXPECT_EQ("\"aaaaaa\"\n"
9985             "\"\\\xC2\x8D\"",
9986             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9987 }
9988 
9989 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9990   EXPECT_EQ("const char *sssss =\n"
9991             "    \"一二三四五六七八\\\n"
9992             " 九 十\";",
9993             format("const char *sssss = \"一二三四五六七八\\\n"
9994                    " 九 十\";",
9995                    getLLVMStyleWithColumns(30)));
9996 }
9997 
9998 TEST_F(FormatTest, SplitsUTF8LineComments) {
9999   EXPECT_EQ("// aaaaÄ\xc2\x8d",
10000             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
10001   EXPECT_EQ("// Я из лесу\n"
10002             "// вышел; был\n"
10003             "// сильный\n"
10004             "// мороз.",
10005             format("// Я из лесу вышел; был сильный мороз.",
10006                    getLLVMStyleWithColumns(13)));
10007   EXPECT_EQ("// 一二三\n"
10008             "// 四五六七\n"
10009             "// 八  九\n"
10010             "// 十",
10011             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
10012 }
10013 
10014 TEST_F(FormatTest, SplitsUTF8BlockComments) {
10015   EXPECT_EQ("/* Гляжу,\n"
10016             " * поднимается\n"
10017             " * медленно в\n"
10018             " * гору\n"
10019             " * Лошадка,\n"
10020             " * везущая\n"
10021             " * хворосту\n"
10022             " * воз. */",
10023             format("/* Гляжу, поднимается медленно в гору\n"
10024                    " * Лошадка, везущая хворосту воз. */",
10025                    getLLVMStyleWithColumns(13)));
10026   EXPECT_EQ(
10027       "/* 一二三\n"
10028       " * 四五六七\n"
10029       " * 八  九\n"
10030       " * 十  */",
10031       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
10032   EXPECT_EQ("/* �������� ��������\n"
10033             " * ��������\n"
10034             " * ������-�� */",
10035             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
10036 }
10037 
10038 #endif // _MSC_VER
10039 
10040 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
10041   FormatStyle Style = getLLVMStyle();
10042 
10043   Style.ConstructorInitializerIndentWidth = 4;
10044   verifyFormat(
10045       "SomeClass::Constructor()\n"
10046       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10047       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10048       Style);
10049 
10050   Style.ConstructorInitializerIndentWidth = 2;
10051   verifyFormat(
10052       "SomeClass::Constructor()\n"
10053       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10054       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10055       Style);
10056 
10057   Style.ConstructorInitializerIndentWidth = 0;
10058   verifyFormat(
10059       "SomeClass::Constructor()\n"
10060       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
10061       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
10062       Style);
10063   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
10064   verifyFormat(
10065       "SomeLongTemplateVariableName<\n"
10066       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
10067       Style);
10068   verifyFormat(
10069       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
10070       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
10071       Style);
10072 }
10073 
10074 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
10075   FormatStyle Style = getLLVMStyle();
10076   Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
10077   Style.ConstructorInitializerIndentWidth = 4;
10078   verifyFormat("SomeClass::Constructor()\n"
10079                "    : a(a)\n"
10080                "    , b(b)\n"
10081                "    , c(c) {}",
10082                Style);
10083   verifyFormat("SomeClass::Constructor()\n"
10084                "    : a(a) {}",
10085                Style);
10086 
10087   Style.ColumnLimit = 0;
10088   verifyFormat("SomeClass::Constructor()\n"
10089                "    : a(a) {}",
10090                Style);
10091   verifyFormat("SomeClass::Constructor() noexcept\n"
10092                "    : a(a) {}",
10093                Style);
10094   verifyFormat("SomeClass::Constructor()\n"
10095                "    : a(a)\n"
10096                "    , b(b)\n"
10097                "    , c(c) {}",
10098                Style);
10099   verifyFormat("SomeClass::Constructor()\n"
10100                "    : a(a) {\n"
10101                "  foo();\n"
10102                "  bar();\n"
10103                "}",
10104                Style);
10105 
10106   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
10107   verifyFormat("SomeClass::Constructor()\n"
10108                "    : a(a)\n"
10109                "    , b(b)\n"
10110                "    , c(c) {\n}",
10111                Style);
10112   verifyFormat("SomeClass::Constructor()\n"
10113                "    : a(a) {\n}",
10114                Style);
10115 
10116   Style.ColumnLimit = 80;
10117   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
10118   Style.ConstructorInitializerIndentWidth = 2;
10119   verifyFormat("SomeClass::Constructor()\n"
10120                "  : a(a)\n"
10121                "  , b(b)\n"
10122                "  , c(c) {}",
10123                Style);
10124 
10125   Style.ConstructorInitializerIndentWidth = 0;
10126   verifyFormat("SomeClass::Constructor()\n"
10127                ": a(a)\n"
10128                ", b(b)\n"
10129                ", c(c) {}",
10130                Style);
10131 
10132   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
10133   Style.ConstructorInitializerIndentWidth = 4;
10134   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
10135   verifyFormat(
10136       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
10137       Style);
10138   verifyFormat(
10139       "SomeClass::Constructor()\n"
10140       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
10141       Style);
10142   Style.ConstructorInitializerIndentWidth = 4;
10143   Style.ColumnLimit = 60;
10144   verifyFormat("SomeClass::Constructor()\n"
10145                "    : aaaaaaaa(aaaaaaaa)\n"
10146                "    , aaaaaaaa(aaaaaaaa)\n"
10147                "    , aaaaaaaa(aaaaaaaa) {}",
10148                Style);
10149 }
10150 
10151 TEST_F(FormatTest, Destructors) {
10152   verifyFormat("void F(int &i) { i.~int(); }");
10153   verifyFormat("void F(int &i) { i->~int(); }");
10154 }
10155 
10156 TEST_F(FormatTest, FormatsWithWebKitStyle) {
10157   FormatStyle Style = getWebKitStyle();
10158 
10159   // Don't indent in outer namespaces.
10160   verifyFormat("namespace outer {\n"
10161                "int i;\n"
10162                "namespace inner {\n"
10163                "    int i;\n"
10164                "} // namespace inner\n"
10165                "} // namespace outer\n"
10166                "namespace other_outer {\n"
10167                "int i;\n"
10168                "}",
10169                Style);
10170 
10171   // Don't indent case labels.
10172   verifyFormat("switch (variable) {\n"
10173                "case 1:\n"
10174                "case 2:\n"
10175                "    doSomething();\n"
10176                "    break;\n"
10177                "default:\n"
10178                "    ++variable;\n"
10179                "}",
10180                Style);
10181 
10182   // Wrap before binary operators.
10183   EXPECT_EQ("void f()\n"
10184             "{\n"
10185             "    if (aaaaaaaaaaaaaaaa\n"
10186             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
10187             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10188             "        return;\n"
10189             "}",
10190             format("void f() {\n"
10191                    "if (aaaaaaaaaaaaaaaa\n"
10192                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
10193                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
10194                    "return;\n"
10195                    "}",
10196                    Style));
10197 
10198   // Allow functions on a single line.
10199   verifyFormat("void f() { return; }", Style);
10200 
10201   // Constructor initializers are formatted one per line with the "," on the
10202   // new line.
10203   verifyFormat("Constructor()\n"
10204                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
10205                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
10206                "          aaaaaaaaaaaaaa)\n"
10207                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
10208                "{\n"
10209                "}",
10210                Style);
10211   verifyFormat("SomeClass::Constructor()\n"
10212                "    : a(a)\n"
10213                "{\n"
10214                "}",
10215                Style);
10216   EXPECT_EQ("SomeClass::Constructor()\n"
10217             "    : a(a)\n"
10218             "{\n"
10219             "}",
10220             format("SomeClass::Constructor():a(a){}", Style));
10221   verifyFormat("SomeClass::Constructor()\n"
10222                "    : a(a)\n"
10223                "    , b(b)\n"
10224                "    , c(c)\n"
10225                "{\n"
10226                "}",
10227                Style);
10228   verifyFormat("SomeClass::Constructor()\n"
10229                "    : a(a)\n"
10230                "{\n"
10231                "    foo();\n"
10232                "    bar();\n"
10233                "}",
10234                Style);
10235 
10236   // Access specifiers should be aligned left.
10237   verifyFormat("class C {\n"
10238                "public:\n"
10239                "    int i;\n"
10240                "};",
10241                Style);
10242 
10243   // Do not align comments.
10244   verifyFormat("int a; // Do not\n"
10245                "double b; // align comments.",
10246                Style);
10247 
10248   // Do not align operands.
10249   EXPECT_EQ("ASSERT(aaaa\n"
10250             "    || bbbb);",
10251             format("ASSERT ( aaaa\n||bbbb);", Style));
10252 
10253   // Accept input's line breaks.
10254   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
10255             "    || bbbbbbbbbbbbbbb) {\n"
10256             "    i++;\n"
10257             "}",
10258             format("if (aaaaaaaaaaaaaaa\n"
10259                    "|| bbbbbbbbbbbbbbb) { i++; }",
10260                    Style));
10261   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
10262             "    i++;\n"
10263             "}",
10264             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
10265 
10266   // Don't automatically break all macro definitions (llvm.org/PR17842).
10267   verifyFormat("#define aNumber 10", Style);
10268   // However, generally keep the line breaks that the user authored.
10269   EXPECT_EQ("#define aNumber \\\n"
10270             "    10",
10271             format("#define aNumber \\\n"
10272                    " 10",
10273                    Style));
10274 
10275   // Keep empty and one-element array literals on a single line.
10276   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
10277             "                                  copyItems:YES];",
10278             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
10279                    "copyItems:YES];",
10280                    Style));
10281   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
10282             "                                  copyItems:YES];",
10283             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
10284                    "             copyItems:YES];",
10285                    Style));
10286   // FIXME: This does not seem right, there should be more indentation before
10287   // the array literal's entries. Nested blocks have the same problem.
10288   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10289             "    @\"a\",\n"
10290             "    @\"a\"\n"
10291             "]\n"
10292             "                                  copyItems:YES];",
10293             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
10294                    "     @\"a\",\n"
10295                    "     @\"a\"\n"
10296                    "     ]\n"
10297                    "       copyItems:YES];",
10298                    Style));
10299   EXPECT_EQ(
10300       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10301       "                                  copyItems:YES];",
10302       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
10303              "   copyItems:YES];",
10304              Style));
10305 
10306   verifyFormat("[self.a b:c c:d];", Style);
10307   EXPECT_EQ("[self.a b:c\n"
10308             "        c:d];",
10309             format("[self.a b:c\n"
10310                    "c:d];",
10311                    Style));
10312 }
10313 
10314 TEST_F(FormatTest, FormatsLambdas) {
10315   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
10316   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
10317   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
10318   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
10319   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
10320   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
10321   verifyFormat("int x = f(*+[] {});");
10322   verifyFormat("void f() {\n"
10323                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
10324                "}\n");
10325   verifyFormat("void f() {\n"
10326                "  other(x.begin(), //\n"
10327                "        x.end(),   //\n"
10328                "        [&](int, int) { return 1; });\n"
10329                "}\n");
10330   verifyFormat("SomeFunction([]() { // A cool function...\n"
10331                "  return 43;\n"
10332                "});");
10333   EXPECT_EQ("SomeFunction([]() {\n"
10334             "#define A a\n"
10335             "  return 43;\n"
10336             "});",
10337             format("SomeFunction([](){\n"
10338                    "#define A a\n"
10339                    "return 43;\n"
10340                    "});"));
10341   verifyFormat("void f() {\n"
10342                "  SomeFunction([](decltype(x), A *a) {});\n"
10343                "}");
10344   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10345                "    [](const aaaaaaaaaa &a) { return a; });");
10346   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
10347                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
10348                "});");
10349   verifyFormat("Constructor()\n"
10350                "    : Field([] { // comment\n"
10351                "        int i;\n"
10352                "      }) {}");
10353   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
10354                "  return some_parameter.size();\n"
10355                "};");
10356   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
10357                "    [](const string &s) { return s; };");
10358   verifyFormat("int i = aaaaaa ? 1 //\n"
10359                "               : [] {\n"
10360                "                   return 2; //\n"
10361                "                 }();");
10362   verifyFormat("llvm::errs() << \"number of twos is \"\n"
10363                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
10364                "                  return x == 2; // force break\n"
10365                "                });");
10366   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10367                "    [=](int iiiiiiiiiiii) {\n"
10368                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
10369                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
10370                "    });",
10371                getLLVMStyleWithColumns(60));
10372   verifyFormat("SomeFunction({[&] {\n"
10373                "                // comment\n"
10374                "              },\n"
10375                "              [&] {\n"
10376                "                // comment\n"
10377                "              }});");
10378   verifyFormat("SomeFunction({[&] {\n"
10379                "  // comment\n"
10380                "}});");
10381   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
10382                "                             [&]() { return true; },\n"
10383                "                         aaaaa aaaaaaaaa);");
10384 
10385   // Lambdas with return types.
10386   verifyFormat("int c = []() -> int { return 2; }();\n");
10387   verifyFormat("int c = []() -> int * { return 2; }();\n");
10388   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
10389   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
10390   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
10391   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
10392   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
10393   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
10394   verifyFormat("[a, a]() -> a<1> {};");
10395   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
10396                "                   int j) -> int {\n"
10397                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
10398                "};");
10399   verifyFormat(
10400       "aaaaaaaaaaaaaaaaaaaaaa(\n"
10401       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
10402       "      return aaaaaaaaaaaaaaaaa;\n"
10403       "    });",
10404       getLLVMStyleWithColumns(70));
10405   verifyFormat("[]() //\n"
10406                "    -> int {\n"
10407                "  return 1; //\n"
10408                "};");
10409 
10410   // Multiple lambdas in the same parentheses change indentation rules.
10411   verifyFormat("SomeFunction(\n"
10412                "    []() {\n"
10413                "      int i = 42;\n"
10414                "      return i;\n"
10415                "    },\n"
10416                "    []() {\n"
10417                "      int j = 43;\n"
10418                "      return j;\n"
10419                "    });");
10420 
10421   // More complex introducers.
10422   verifyFormat("return [i, args...] {};");
10423 
10424   // Not lambdas.
10425   verifyFormat("constexpr char hello[]{\"hello\"};");
10426   verifyFormat("double &operator[](int i) { return 0; }\n"
10427                "int i;");
10428   verifyFormat("std::unique_ptr<int[]> foo() {}");
10429   verifyFormat("int i = a[a][a]->f();");
10430   verifyFormat("int i = (*b)[a]->f();");
10431 
10432   // Other corner cases.
10433   verifyFormat("void f() {\n"
10434                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
10435                "  );\n"
10436                "}");
10437 
10438   // Lambdas created through weird macros.
10439   verifyFormat("void f() {\n"
10440                "  MACRO((const AA &a) { return 1; });\n"
10441                "  MACRO((AA &a) { return 1; });\n"
10442                "}");
10443 
10444   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
10445                "      doo_dah();\n"
10446                "      doo_dah();\n"
10447                "    })) {\n"
10448                "}");
10449   verifyFormat("if constexpr (blah_blah(whatever, whatever, [] {\n"
10450                "                doo_dah();\n"
10451                "                doo_dah();\n"
10452                "              })) {\n"
10453                "}");
10454   verifyFormat("auto lambda = []() {\n"
10455                "  int a = 2\n"
10456                "#if A\n"
10457                "          + 2\n"
10458                "#endif\n"
10459                "      ;\n"
10460                "};");
10461 
10462   // Lambdas with complex multiline introducers.
10463   verifyFormat(
10464       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
10465       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
10466       "        -> ::std::unordered_set<\n"
10467       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
10468       "      //\n"
10469       "    });");
10470 }
10471 
10472 TEST_F(FormatTest, FormatsBlocks) {
10473   FormatStyle ShortBlocks = getLLVMStyle();
10474   ShortBlocks.AllowShortBlocksOnASingleLine = true;
10475   verifyFormat("int (^Block)(int, int);", ShortBlocks);
10476   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
10477   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
10478   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
10479   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
10480   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
10481 
10482   verifyFormat("foo(^{ bar(); });", ShortBlocks);
10483   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
10484   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
10485 
10486   verifyFormat("[operation setCompletionBlock:^{\n"
10487                "  [self onOperationDone];\n"
10488                "}];");
10489   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
10490                "  [self onOperationDone];\n"
10491                "}]};");
10492   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
10493                "  f();\n"
10494                "}];");
10495   verifyFormat("int a = [operation block:^int(int *i) {\n"
10496                "  return 1;\n"
10497                "}];");
10498   verifyFormat("[myObject doSomethingWith:arg1\n"
10499                "                      aaa:^int(int *a) {\n"
10500                "                        return 1;\n"
10501                "                      }\n"
10502                "                      bbb:f(a * bbbbbbbb)];");
10503 
10504   verifyFormat("[operation setCompletionBlock:^{\n"
10505                "  [self.delegate newDataAvailable];\n"
10506                "}];",
10507                getLLVMStyleWithColumns(60));
10508   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
10509                "  NSString *path = [self sessionFilePath];\n"
10510                "  if (path) {\n"
10511                "    // ...\n"
10512                "  }\n"
10513                "});");
10514   verifyFormat("[[SessionService sharedService]\n"
10515                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10516                "      if (window) {\n"
10517                "        [self windowDidLoad:window];\n"
10518                "      } else {\n"
10519                "        [self errorLoadingWindow];\n"
10520                "      }\n"
10521                "    }];");
10522   verifyFormat("void (^largeBlock)(void) = ^{\n"
10523                "  // ...\n"
10524                "};\n",
10525                getLLVMStyleWithColumns(40));
10526   verifyFormat("[[SessionService sharedService]\n"
10527                "    loadWindowWithCompletionBlock: //\n"
10528                "        ^(SessionWindow *window) {\n"
10529                "          if (window) {\n"
10530                "            [self windowDidLoad:window];\n"
10531                "          } else {\n"
10532                "            [self errorLoadingWindow];\n"
10533                "          }\n"
10534                "        }];",
10535                getLLVMStyleWithColumns(60));
10536   verifyFormat("[myObject doSomethingWith:arg1\n"
10537                "    firstBlock:^(Foo *a) {\n"
10538                "      // ...\n"
10539                "      int i;\n"
10540                "    }\n"
10541                "    secondBlock:^(Bar *b) {\n"
10542                "      // ...\n"
10543                "      int i;\n"
10544                "    }\n"
10545                "    thirdBlock:^Foo(Bar *b) {\n"
10546                "      // ...\n"
10547                "      int i;\n"
10548                "    }];");
10549   verifyFormat("[myObject doSomethingWith:arg1\n"
10550                "               firstBlock:-1\n"
10551                "              secondBlock:^(Bar *b) {\n"
10552                "                // ...\n"
10553                "                int i;\n"
10554                "              }];");
10555 
10556   verifyFormat("f(^{\n"
10557                "  @autoreleasepool {\n"
10558                "    if (a) {\n"
10559                "      g();\n"
10560                "    }\n"
10561                "  }\n"
10562                "});");
10563   verifyFormat("Block b = ^int *(A *a, B *b) {}");
10564   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
10565                "};");
10566 
10567   FormatStyle FourIndent = getLLVMStyle();
10568   FourIndent.ObjCBlockIndentWidth = 4;
10569   verifyFormat("[operation setCompletionBlock:^{\n"
10570                "    [self onOperationDone];\n"
10571                "}];",
10572                FourIndent);
10573 }
10574 
10575 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
10576   FormatStyle ZeroColumn = getLLVMStyle();
10577   ZeroColumn.ColumnLimit = 0;
10578 
10579   verifyFormat("[[SessionService sharedService] "
10580                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10581                "  if (window) {\n"
10582                "    [self windowDidLoad:window];\n"
10583                "  } else {\n"
10584                "    [self errorLoadingWindow];\n"
10585                "  }\n"
10586                "}];",
10587                ZeroColumn);
10588   EXPECT_EQ("[[SessionService sharedService]\n"
10589             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10590             "      if (window) {\n"
10591             "        [self windowDidLoad:window];\n"
10592             "      } else {\n"
10593             "        [self errorLoadingWindow];\n"
10594             "      }\n"
10595             "    }];",
10596             format("[[SessionService sharedService]\n"
10597                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10598                    "                if (window) {\n"
10599                    "    [self windowDidLoad:window];\n"
10600                    "  } else {\n"
10601                    "    [self errorLoadingWindow];\n"
10602                    "  }\n"
10603                    "}];",
10604                    ZeroColumn));
10605   verifyFormat("[myObject doSomethingWith:arg1\n"
10606                "    firstBlock:^(Foo *a) {\n"
10607                "      // ...\n"
10608                "      int i;\n"
10609                "    }\n"
10610                "    secondBlock:^(Bar *b) {\n"
10611                "      // ...\n"
10612                "      int i;\n"
10613                "    }\n"
10614                "    thirdBlock:^Foo(Bar *b) {\n"
10615                "      // ...\n"
10616                "      int i;\n"
10617                "    }];",
10618                ZeroColumn);
10619   verifyFormat("f(^{\n"
10620                "  @autoreleasepool {\n"
10621                "    if (a) {\n"
10622                "      g();\n"
10623                "    }\n"
10624                "  }\n"
10625                "});",
10626                ZeroColumn);
10627   verifyFormat("void (^largeBlock)(void) = ^{\n"
10628                "  // ...\n"
10629                "};",
10630                ZeroColumn);
10631 
10632   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10633   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10634             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10635   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10636   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10637             "  int i;\n"
10638             "};",
10639             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10640 }
10641 
10642 TEST_F(FormatTest, SupportsCRLF) {
10643   EXPECT_EQ("int a;\r\n"
10644             "int b;\r\n"
10645             "int c;\r\n",
10646             format("int a;\r\n"
10647                    "  int b;\r\n"
10648                    "    int c;\r\n",
10649                    getLLVMStyle()));
10650   EXPECT_EQ("int a;\r\n"
10651             "int b;\r\n"
10652             "int c;\r\n",
10653             format("int a;\r\n"
10654                    "  int b;\n"
10655                    "    int c;\r\n",
10656                    getLLVMStyle()));
10657   EXPECT_EQ("int a;\n"
10658             "int b;\n"
10659             "int c;\n",
10660             format("int a;\r\n"
10661                    "  int b;\n"
10662                    "    int c;\n",
10663                    getLLVMStyle()));
10664   EXPECT_EQ("\"aaaaaaa \"\r\n"
10665             "\"bbbbbbb\";\r\n",
10666             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10667   EXPECT_EQ("#define A \\\r\n"
10668             "  b;      \\\r\n"
10669             "  c;      \\\r\n"
10670             "  d;\r\n",
10671             format("#define A \\\r\n"
10672                    "  b; \\\r\n"
10673                    "  c; d; \r\n",
10674                    getGoogleStyle()));
10675 
10676   EXPECT_EQ("/*\r\n"
10677             "multi line block comments\r\n"
10678             "should not introduce\r\n"
10679             "an extra carriage return\r\n"
10680             "*/\r\n",
10681             format("/*\r\n"
10682                    "multi line block comments\r\n"
10683                    "should not introduce\r\n"
10684                    "an extra carriage return\r\n"
10685                    "*/\r\n"));
10686 }
10687 
10688 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10689   verifyFormat("MY_CLASS(C) {\n"
10690                "  int i;\n"
10691                "  int j;\n"
10692                "};");
10693 }
10694 
10695 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10696   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10697   TwoIndent.ContinuationIndentWidth = 2;
10698 
10699   EXPECT_EQ("int i =\n"
10700             "  longFunction(\n"
10701             "    arg);",
10702             format("int i = longFunction(arg);", TwoIndent));
10703 
10704   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10705   SixIndent.ContinuationIndentWidth = 6;
10706 
10707   EXPECT_EQ("int i =\n"
10708             "      longFunction(\n"
10709             "            arg);",
10710             format("int i = longFunction(arg);", SixIndent));
10711 }
10712 
10713 TEST_F(FormatTest, SpacesInAngles) {
10714   FormatStyle Spaces = getLLVMStyle();
10715   Spaces.SpacesInAngles = true;
10716 
10717   verifyFormat("static_cast< int >(arg);", Spaces);
10718   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10719   verifyFormat("f< int, float >();", Spaces);
10720   verifyFormat("template <> g() {}", Spaces);
10721   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10722   verifyFormat("std::function< void(int, int) > fct;", Spaces);
10723   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
10724                Spaces);
10725 
10726   Spaces.Standard = FormatStyle::LS_Cpp03;
10727   Spaces.SpacesInAngles = true;
10728   verifyFormat("A< A< int > >();", Spaces);
10729 
10730   Spaces.SpacesInAngles = false;
10731   verifyFormat("A<A<int> >();", Spaces);
10732 
10733   Spaces.Standard = FormatStyle::LS_Cpp11;
10734   Spaces.SpacesInAngles = true;
10735   verifyFormat("A< A< int > >();", Spaces);
10736 
10737   Spaces.SpacesInAngles = false;
10738   verifyFormat("A<A<int>>();", Spaces);
10739 }
10740 
10741 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
10742   FormatStyle Style = getLLVMStyle();
10743   Style.SpaceAfterTemplateKeyword = false;
10744   verifyFormat("template<int> void foo();", Style);
10745 }
10746 
10747 TEST_F(FormatTest, TripleAngleBrackets) {
10748   verifyFormat("f<<<1, 1>>>();");
10749   verifyFormat("f<<<1, 1, 1, s>>>();");
10750   verifyFormat("f<<<a, b, c, d>>>();");
10751   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10752   verifyFormat("f<param><<<1, 1>>>();");
10753   verifyFormat("f<1><<<1, 1>>>();");
10754   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10755   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10756                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10757   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
10758                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
10759 }
10760 
10761 TEST_F(FormatTest, MergeLessLessAtEnd) {
10762   verifyFormat("<<");
10763   EXPECT_EQ("< < <", format("\\\n<<<"));
10764   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10765                "aaallvm::outs() <<");
10766   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10767                "aaaallvm::outs()\n    <<");
10768 }
10769 
10770 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10771   std::string code = "#if A\n"
10772                      "#if B\n"
10773                      "a.\n"
10774                      "#endif\n"
10775                      "    a = 1;\n"
10776                      "#else\n"
10777                      "#endif\n"
10778                      "#if C\n"
10779                      "#else\n"
10780                      "#endif\n";
10781   EXPECT_EQ(code, format(code));
10782 }
10783 
10784 TEST_F(FormatTest, HandleConflictMarkers) {
10785   // Git/SVN conflict markers.
10786   EXPECT_EQ("int a;\n"
10787             "void f() {\n"
10788             "  callme(some(parameter1,\n"
10789             "<<<<<<< text by the vcs\n"
10790             "              parameter2),\n"
10791             "||||||| text by the vcs\n"
10792             "              parameter2),\n"
10793             "         parameter3,\n"
10794             "======= text by the vcs\n"
10795             "              parameter2, parameter3),\n"
10796             ">>>>>>> text by the vcs\n"
10797             "         otherparameter);\n",
10798             format("int a;\n"
10799                    "void f() {\n"
10800                    "  callme(some(parameter1,\n"
10801                    "<<<<<<< text by the vcs\n"
10802                    "  parameter2),\n"
10803                    "||||||| text by the vcs\n"
10804                    "  parameter2),\n"
10805                    "  parameter3,\n"
10806                    "======= text by the vcs\n"
10807                    "  parameter2,\n"
10808                    "  parameter3),\n"
10809                    ">>>>>>> text by the vcs\n"
10810                    "  otherparameter);\n"));
10811 
10812   // Perforce markers.
10813   EXPECT_EQ("void f() {\n"
10814             "  function(\n"
10815             ">>>> text by the vcs\n"
10816             "      parameter,\n"
10817             "==== text by the vcs\n"
10818             "      parameter,\n"
10819             "==== text by the vcs\n"
10820             "      parameter,\n"
10821             "<<<< text by the vcs\n"
10822             "      parameter);\n",
10823             format("void f() {\n"
10824                    "  function(\n"
10825                    ">>>> text by the vcs\n"
10826                    "  parameter,\n"
10827                    "==== text by the vcs\n"
10828                    "  parameter,\n"
10829                    "==== text by the vcs\n"
10830                    "  parameter,\n"
10831                    "<<<< text by the vcs\n"
10832                    "  parameter);\n"));
10833 
10834   EXPECT_EQ("<<<<<<<\n"
10835             "|||||||\n"
10836             "=======\n"
10837             ">>>>>>>",
10838             format("<<<<<<<\n"
10839                    "|||||||\n"
10840                    "=======\n"
10841                    ">>>>>>>"));
10842 
10843   EXPECT_EQ("<<<<<<<\n"
10844             "|||||||\n"
10845             "int i;\n"
10846             "=======\n"
10847             ">>>>>>>",
10848             format("<<<<<<<\n"
10849                    "|||||||\n"
10850                    "int i;\n"
10851                    "=======\n"
10852                    ">>>>>>>"));
10853 
10854   // FIXME: Handle parsing of macros around conflict markers correctly:
10855   EXPECT_EQ("#define Macro \\\n"
10856             "<<<<<<<\n"
10857             "Something \\\n"
10858             "|||||||\n"
10859             "Else \\\n"
10860             "=======\n"
10861             "Other \\\n"
10862             ">>>>>>>\n"
10863             "    End int i;\n",
10864             format("#define Macro \\\n"
10865                    "<<<<<<<\n"
10866                    "  Something \\\n"
10867                    "|||||||\n"
10868                    "  Else \\\n"
10869                    "=======\n"
10870                    "  Other \\\n"
10871                    ">>>>>>>\n"
10872                    "  End\n"
10873                    "int i;\n"));
10874 }
10875 
10876 TEST_F(FormatTest, DisableRegions) {
10877   EXPECT_EQ("int i;\n"
10878             "// clang-format off\n"
10879             "  int j;\n"
10880             "// clang-format on\n"
10881             "int k;",
10882             format(" int  i;\n"
10883                    "   // clang-format off\n"
10884                    "  int j;\n"
10885                    " // clang-format on\n"
10886                    "   int   k;"));
10887   EXPECT_EQ("int i;\n"
10888             "/* clang-format off */\n"
10889             "  int j;\n"
10890             "/* clang-format on */\n"
10891             "int k;",
10892             format(" int  i;\n"
10893                    "   /* clang-format off */\n"
10894                    "  int j;\n"
10895                    " /* clang-format on */\n"
10896                    "   int   k;"));
10897 
10898   // Don't reflow comments within disabled regions.
10899   EXPECT_EQ(
10900       "// clang-format off\n"
10901       "// long long long long long long line\n"
10902       "/* clang-format on */\n"
10903       "/* long long long\n"
10904       " * long long long\n"
10905       " * line */\n"
10906       "int i;\n"
10907       "/* clang-format off */\n"
10908       "/* long long long long long long line */\n",
10909       format("// clang-format off\n"
10910              "// long long long long long long line\n"
10911              "/* clang-format on */\n"
10912              "/* long long long long long long line */\n"
10913              "int i;\n"
10914              "/* clang-format off */\n"
10915              "/* long long long long long long line */\n",
10916              getLLVMStyleWithColumns(20)));
10917 }
10918 
10919 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10920   format("? ) =");
10921   verifyNoCrash("#define a\\\n /**/}");
10922 }
10923 
10924 TEST_F(FormatTest, FormatsTableGenCode) {
10925   FormatStyle Style = getLLVMStyle();
10926   Style.Language = FormatStyle::LK_TableGen;
10927   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10928 }
10929 
10930 TEST_F(FormatTest, ArrayOfTemplates) {
10931   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10932             format("auto a = new unique_ptr<int > [ 10];"));
10933 
10934   FormatStyle Spaces = getLLVMStyle();
10935   Spaces.SpacesInSquareBrackets = true;
10936   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10937             format("auto a = new unique_ptr<int > [10];", Spaces));
10938 }
10939 
10940 TEST_F(FormatTest, ArrayAsTemplateType) {
10941   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10942             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10943 
10944   FormatStyle Spaces = getLLVMStyle();
10945   Spaces.SpacesInSquareBrackets = true;
10946   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10947             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10948 }
10949 
10950 TEST_F(FormatTest, NoSpaceAfterSuper) {
10951     verifyFormat("__super::FooBar();");
10952 }
10953 
10954 TEST(FormatStyle, GetStyleOfFile) {
10955   vfs::InMemoryFileSystem FS;
10956   // Test 1: format file in the same directory.
10957   ASSERT_TRUE(
10958       FS.addFile("/a/.clang-format", 0,
10959                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10960   ASSERT_TRUE(
10961       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10962   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10963   ASSERT_TRUE((bool)Style1);
10964   ASSERT_EQ(*Style1, getLLVMStyle());
10965 
10966   // Test 2.1: fallback to default.
10967   ASSERT_TRUE(
10968       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10969   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10970   ASSERT_TRUE((bool)Style2);
10971   ASSERT_EQ(*Style2, getMozillaStyle());
10972 
10973   // Test 2.2: no format on 'none' fallback style.
10974   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10975   ASSERT_TRUE((bool)Style2);
10976   ASSERT_EQ(*Style2, getNoStyle());
10977 
10978   // Test 2.3: format if config is found with no based style while fallback is
10979   // 'none'.
10980   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
10981                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
10982   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10983   ASSERT_TRUE((bool)Style2);
10984   ASSERT_EQ(*Style2, getLLVMStyle());
10985 
10986   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
10987   Style2 = getStyle("{}", "a.h", "none", "", &FS);
10988   ASSERT_TRUE((bool)Style2);
10989   ASSERT_EQ(*Style2, getLLVMStyle());
10990 
10991   // Test 3: format file in parent directory.
10992   ASSERT_TRUE(
10993       FS.addFile("/c/.clang-format", 0,
10994                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
10995   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
10996                          llvm::MemoryBuffer::getMemBuffer("int i;")));
10997   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
10998   ASSERT_TRUE((bool)Style3);
10999   ASSERT_EQ(*Style3, getGoogleStyle());
11000 
11001   // Test 4: error on invalid fallback style
11002   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
11003   ASSERT_FALSE((bool)Style4);
11004   llvm::consumeError(Style4.takeError());
11005 
11006   // Test 5: error on invalid yaml on command line
11007   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
11008   ASSERT_FALSE((bool)Style5);
11009   llvm::consumeError(Style5.takeError());
11010 
11011   // Test 6: error on invalid style
11012   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
11013   ASSERT_FALSE((bool)Style6);
11014   llvm::consumeError(Style6.takeError());
11015 
11016   // Test 7: found config file, error on parsing it
11017   ASSERT_TRUE(
11018       FS.addFile("/d/.clang-format", 0,
11019                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
11020                                                   "InvalidKey: InvalidValue")));
11021   ASSERT_TRUE(
11022       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
11023   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
11024   ASSERT_FALSE((bool)Style7);
11025   llvm::consumeError(Style7.takeError());
11026 }
11027 
11028 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
11029   // Column limit is 20.
11030   std::string Code = "Type *a =\n"
11031                      "    new Type();\n"
11032                      "g(iiiii, 0, jjjjj,\n"
11033                      "  0, kkkkk, 0, mm);\n"
11034                      "int  bad     = format   ;";
11035   std::string Expected = "auto a = new Type();\n"
11036                          "g(iiiii, nullptr,\n"
11037                          "  jjjjj, nullptr,\n"
11038                          "  kkkkk, nullptr,\n"
11039                          "  mm);\n"
11040                          "int  bad     = format   ;";
11041   FileID ID = Context.createInMemoryFile("format.cpp", Code);
11042   tooling::Replacements Replaces = toReplacements(
11043       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
11044                             "auto "),
11045        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
11046                             "nullptr"),
11047        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
11048                             "nullptr"),
11049        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
11050                             "nullptr")});
11051 
11052   format::FormatStyle Style = format::getLLVMStyle();
11053   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
11054   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11055   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11056       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11057   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11058   EXPECT_TRUE(static_cast<bool>(Result));
11059   EXPECT_EQ(Expected, *Result);
11060 }
11061 
11062 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
11063   std::string Code = "#include \"a.h\"\n"
11064                      "#include \"c.h\"\n"
11065                      "\n"
11066                      "int main() {\n"
11067                      "  return 0;\n"
11068                      "}";
11069   std::string Expected = "#include \"a.h\"\n"
11070                          "#include \"b.h\"\n"
11071                          "#include \"c.h\"\n"
11072                          "\n"
11073                          "int main() {\n"
11074                          "  return 0;\n"
11075                          "}";
11076   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
11077   tooling::Replacements Replaces = toReplacements(
11078       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
11079                             "#include \"b.h\"\n")});
11080 
11081   format::FormatStyle Style = format::getLLVMStyle();
11082   Style.SortIncludes = true;
11083   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
11084   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
11085       << llvm::toString(FormattedReplaces.takeError()) << "\n";
11086   auto Result = applyAllReplacements(Code, *FormattedReplaces);
11087   EXPECT_TRUE(static_cast<bool>(Result));
11088   EXPECT_EQ(Expected, *Result);
11089 }
11090 
11091 TEST_F(FormatTest, FormatSortsUsingDeclarations) {
11092   EXPECT_EQ("using std::cin;\n"
11093             "using std::cout;",
11094             format("using std::cout;\n"
11095                    "using std::cin;", getGoogleStyle()));
11096 }
11097 
11098 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
11099   format::FormatStyle Style = format::getLLVMStyle();
11100   Style.Standard = FormatStyle::LS_Cpp03;
11101   // cpp03 recognize this string as identifier u8 and literal character 'a'
11102   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
11103 }
11104 
11105 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
11106   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
11107   // all modes, including C++11, C++14 and C++17
11108   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
11109 }
11110 
11111 } // end namespace
11112 } // end namespace format
11113 } // end namespace clang
11114