1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum IncompleteCheck {
34     IC_ExpectComplete,
35     IC_ExpectIncomplete,
36     IC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     bool IncompleteFormat = false;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
48     if (CheckIncomplete != IC_DoNotCheck) {
49       bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
50       EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
51     }
52     ReplacementCount = Replaces.size();
53     auto Result = applyAllReplacements(Code, Replaces);
54     EXPECT_TRUE(static_cast<bool>(Result));
55     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
56     return *Result;
57   }
58 
59   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
60     FormatStyle Style = getLLVMStyle();
61     Style.ColumnLimit = ColumnLimit;
62     return Style;
63   }
64 
65   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
66     FormatStyle Style = getGoogleStyle();
67     Style.ColumnLimit = ColumnLimit;
68     return Style;
69   }
70 
71   void verifyFormat(llvm::StringRef Code,
72                     const FormatStyle &Style = getLLVMStyle()) {
73     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
74     if (Style.Language == FormatStyle::LK_Cpp) {
75       // Objective-C++ is a superset of C++, so everything checked for C++
76       // needs to be checked for Objective-C++ as well.
77       FormatStyle ObjCStyle = Style;
78       ObjCStyle.Language = FormatStyle::LK_ObjC;
79       EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle));
80     }
81   }
82 
83   void verifyIncompleteFormat(llvm::StringRef Code,
84                               const FormatStyle &Style = getLLVMStyle()) {
85     EXPECT_EQ(Code.str(),
86               format(test::messUp(Code), Style, IC_ExpectIncomplete));
87   }
88 
89   void verifyGoogleFormat(llvm::StringRef Code) {
90     verifyFormat(Code, getGoogleStyle());
91   }
92 
93   void verifyIndependentOfContext(llvm::StringRef text) {
94     verifyFormat(text);
95     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
96   }
97 
98   /// \brief Verify that clang-format does not crash on the given input.
99   void verifyNoCrash(llvm::StringRef Code,
100                      const FormatStyle &Style = getLLVMStyle()) {
101     format(Code, Style, IC_DoNotCheck);
102   }
103 
104   int ReplacementCount;
105 };
106 
107 TEST_F(FormatTest, MessUp) {
108   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
109   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
110   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
111   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
112   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
113 }
114 
115 //===----------------------------------------------------------------------===//
116 // Basic function tests.
117 //===----------------------------------------------------------------------===//
118 
119 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
120   EXPECT_EQ(";", format(";"));
121 }
122 
123 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
124   EXPECT_EQ("int i;", format("  int i;"));
125   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
126   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
127   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
128 }
129 
130 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
131   EXPECT_EQ("int i;", format("int\ni;"));
132 }
133 
134 TEST_F(FormatTest, FormatsNestedBlockStatements) {
135   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
136 }
137 
138 TEST_F(FormatTest, FormatsNestedCall) {
139   verifyFormat("Method(f1, f2(f3));");
140   verifyFormat("Method(f1(f2, f3()));");
141   verifyFormat("Method(f1(f2, (f3())));");
142 }
143 
144 TEST_F(FormatTest, NestedNameSpecifiers) {
145   verifyFormat("vector<::Type> v;");
146   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
147   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
148   verifyFormat("bool a = 2 < ::SomeFunction();");
149   verifyFormat("ALWAYS_INLINE ::std::string getName();");
150   verifyFormat("some::string getName();");
151 }
152 
153 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
154   EXPECT_EQ("if (a) {\n"
155             "  f();\n"
156             "}",
157             format("if(a){f();}"));
158   EXPECT_EQ(4, ReplacementCount);
159   EXPECT_EQ("if (a) {\n"
160             "  f();\n"
161             "}",
162             format("if (a) {\n"
163                    "  f();\n"
164                    "}"));
165   EXPECT_EQ(0, ReplacementCount);
166   EXPECT_EQ("/*\r\n"
167             "\r\n"
168             "*/\r\n",
169             format("/*\r\n"
170                    "\r\n"
171                    "*/\r\n"));
172   EXPECT_EQ(0, ReplacementCount);
173 }
174 
175 TEST_F(FormatTest, RemovesEmptyLines) {
176   EXPECT_EQ("class C {\n"
177             "  int i;\n"
178             "};",
179             format("class C {\n"
180                    " int i;\n"
181                    "\n"
182                    "};"));
183 
184   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
185   EXPECT_EQ("namespace N {\n"
186             "\n"
187             "int i;\n"
188             "}",
189             format("namespace N {\n"
190                    "\n"
191                    "int    i;\n"
192                    "}",
193                    getGoogleStyle()));
194   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
195             "\n"
196             "int i;\n"
197             "}",
198             format("extern /**/ \"C\" /**/ {\n"
199                    "\n"
200                    "int    i;\n"
201                    "}",
202                    getGoogleStyle()));
203 
204   // ...but do keep inlining and removing empty lines for non-block extern "C"
205   // functions.
206   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
207   EXPECT_EQ("extern \"C\" int f() {\n"
208             "  int i = 42;\n"
209             "  return i;\n"
210             "}",
211             format("extern \"C\" int f() {\n"
212                    "\n"
213                    "  int i = 42;\n"
214                    "  return i;\n"
215                    "}",
216                    getGoogleStyle()));
217 
218   // Remove empty lines at the beginning and end of blocks.
219   EXPECT_EQ("void f() {\n"
220             "\n"
221             "  if (a) {\n"
222             "\n"
223             "    f();\n"
224             "  }\n"
225             "}",
226             format("void f() {\n"
227                    "\n"
228                    "  if (a) {\n"
229                    "\n"
230                    "    f();\n"
231                    "\n"
232                    "  }\n"
233                    "\n"
234                    "}",
235                    getLLVMStyle()));
236   EXPECT_EQ("void f() {\n"
237             "  if (a) {\n"
238             "    f();\n"
239             "  }\n"
240             "}",
241             format("void f() {\n"
242                    "\n"
243                    "  if (a) {\n"
244                    "\n"
245                    "    f();\n"
246                    "\n"
247                    "  }\n"
248                    "\n"
249                    "}",
250                    getGoogleStyle()));
251 
252   // Don't remove empty lines in more complex control statements.
253   EXPECT_EQ("void f() {\n"
254             "  if (a) {\n"
255             "    f();\n"
256             "\n"
257             "  } else if (b) {\n"
258             "    f();\n"
259             "  }\n"
260             "}",
261             format("void f() {\n"
262                    "  if (a) {\n"
263                    "    f();\n"
264                    "\n"
265                    "  } else if (b) {\n"
266                    "    f();\n"
267                    "\n"
268                    "  }\n"
269                    "\n"
270                    "}"));
271 
272   // FIXME: This is slightly inconsistent.
273   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
274   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
275   EXPECT_EQ("namespace {\n"
276             "int i;\n"
277             "}",
278             format("namespace {\n"
279                    "int i;\n"
280                    "\n"
281                    "}", LLVMWithNoNamespaceFix));
282   EXPECT_EQ("namespace {\n"
283             "int i;\n"
284             "}",
285             format("namespace {\n"
286                    "int i;\n"
287                    "\n"
288                    "}"));
289   EXPECT_EQ("namespace {\n"
290             "int i;\n"
291             "\n"
292             "} // namespace",
293             format("namespace {\n"
294                    "int i;\n"
295                    "\n"
296                    "}  // namespace"));
297 
298   FormatStyle Style = getLLVMStyle();
299   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
300   Style.MaxEmptyLinesToKeep = 2;
301   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
302   Style.BraceWrapping.AfterClass = true;
303   Style.BraceWrapping.AfterFunction = true;
304   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
305 
306   EXPECT_EQ("class Foo\n"
307             "{\n"
308             "  Foo() {}\n"
309             "\n"
310             "  void funk() {}\n"
311             "};",
312             format("class Foo\n"
313                    "{\n"
314                    "  Foo()\n"
315                    "  {\n"
316                    "  }\n"
317                    "\n"
318                    "  void funk() {}\n"
319                    "};",
320                    Style));
321 }
322 
323 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
324   verifyFormat("x = (a) and (b);");
325   verifyFormat("x = (a) or (b);");
326   verifyFormat("x = (a) bitand (b);");
327   verifyFormat("x = (a) bitor (b);");
328   verifyFormat("x = (a) not_eq (b);");
329   verifyFormat("x = (a) and_eq (b);");
330   verifyFormat("x = (a) or_eq (b);");
331   verifyFormat("x = (a) xor (b);");
332 }
333 
334 //===----------------------------------------------------------------------===//
335 // Tests for control statements.
336 //===----------------------------------------------------------------------===//
337 
338 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
339   verifyFormat("if (true)\n  f();\ng();");
340   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
341   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
342 
343   FormatStyle AllowsMergedIf = getLLVMStyle();
344   AllowsMergedIf.AlignEscapedNewlinesLeft = true;
345   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
346   verifyFormat("if (a)\n"
347                "  // comment\n"
348                "  f();",
349                AllowsMergedIf);
350   verifyFormat("{\n"
351                "  if (a)\n"
352                "  label:\n"
353                "    f();\n"
354                "}",
355                AllowsMergedIf);
356   verifyFormat("#define A \\\n"
357                "  if (a)  \\\n"
358                "  label:  \\\n"
359                "    f()",
360                AllowsMergedIf);
361   verifyFormat("if (a)\n"
362                "  ;",
363                AllowsMergedIf);
364   verifyFormat("if (a)\n"
365                "  if (b) return;",
366                AllowsMergedIf);
367 
368   verifyFormat("if (a) // Can't merge this\n"
369                "  f();\n",
370                AllowsMergedIf);
371   verifyFormat("if (a) /* still don't merge */\n"
372                "  f();",
373                AllowsMergedIf);
374   verifyFormat("if (a) { // Never merge this\n"
375                "  f();\n"
376                "}",
377                AllowsMergedIf);
378   verifyFormat("if (a) { /* Never merge this */\n"
379                "  f();\n"
380                "}",
381                AllowsMergedIf);
382 
383   AllowsMergedIf.ColumnLimit = 14;
384   verifyFormat("if (a) return;", AllowsMergedIf);
385   verifyFormat("if (aaaaaaaaa)\n"
386                "  return;",
387                AllowsMergedIf);
388 
389   AllowsMergedIf.ColumnLimit = 13;
390   verifyFormat("if (a)\n  return;", AllowsMergedIf);
391 }
392 
393 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
394   FormatStyle AllowsMergedLoops = getLLVMStyle();
395   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
396   verifyFormat("while (true) continue;", AllowsMergedLoops);
397   verifyFormat("for (;;) continue;", AllowsMergedLoops);
398   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
399   verifyFormat("while (true)\n"
400                "  ;",
401                AllowsMergedLoops);
402   verifyFormat("for (;;)\n"
403                "  ;",
404                AllowsMergedLoops);
405   verifyFormat("for (;;)\n"
406                "  for (;;) continue;",
407                AllowsMergedLoops);
408   verifyFormat("for (;;) // Can't merge this\n"
409                "  continue;",
410                AllowsMergedLoops);
411   verifyFormat("for (;;) /* still don't merge */\n"
412                "  continue;",
413                AllowsMergedLoops);
414 }
415 
416 TEST_F(FormatTest, FormatShortBracedStatements) {
417   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
418   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
419 
420   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
421   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
422 
423   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
424   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
425   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
426   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
427   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
428   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
429   verifyFormat("if (true) { //\n"
430                "  f();\n"
431                "}",
432                AllowSimpleBracedStatements);
433   verifyFormat("if (true) {\n"
434                "  f();\n"
435                "  f();\n"
436                "}",
437                AllowSimpleBracedStatements);
438   verifyFormat("if (true) {\n"
439                "  f();\n"
440                "} else {\n"
441                "  f();\n"
442                "}",
443                AllowSimpleBracedStatements);
444 
445   verifyFormat("template <int> struct A2 {\n"
446                "  struct B {};\n"
447                "};",
448                AllowSimpleBracedStatements);
449 
450   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
451   verifyFormat("if (true) {\n"
452                "  f();\n"
453                "}",
454                AllowSimpleBracedStatements);
455   verifyFormat("if (true) {\n"
456                "  f();\n"
457                "} else {\n"
458                "  f();\n"
459                "}",
460                AllowSimpleBracedStatements);
461 
462   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
463   verifyFormat("while (true) {\n"
464                "  f();\n"
465                "}",
466                AllowSimpleBracedStatements);
467   verifyFormat("for (;;) {\n"
468                "  f();\n"
469                "}",
470                AllowSimpleBracedStatements);
471 }
472 
473 TEST_F(FormatTest, ParseIfElse) {
474   verifyFormat("if (true)\n"
475                "  if (true)\n"
476                "    if (true)\n"
477                "      f();\n"
478                "    else\n"
479                "      g();\n"
480                "  else\n"
481                "    h();\n"
482                "else\n"
483                "  i();");
484   verifyFormat("if (true)\n"
485                "  if (true)\n"
486                "    if (true) {\n"
487                "      if (true)\n"
488                "        f();\n"
489                "    } else {\n"
490                "      g();\n"
491                "    }\n"
492                "  else\n"
493                "    h();\n"
494                "else {\n"
495                "  i();\n"
496                "}");
497   verifyFormat("void f() {\n"
498                "  if (a) {\n"
499                "  } else {\n"
500                "  }\n"
501                "}");
502 }
503 
504 TEST_F(FormatTest, ElseIf) {
505   verifyFormat("if (a) {\n} else if (b) {\n}");
506   verifyFormat("if (a)\n"
507                "  f();\n"
508                "else if (b)\n"
509                "  g();\n"
510                "else\n"
511                "  h();");
512   verifyFormat("if (a) {\n"
513                "  f();\n"
514                "}\n"
515                "// or else ..\n"
516                "else {\n"
517                "  g()\n"
518                "}");
519 
520   verifyFormat("if (a) {\n"
521                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
522                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
523                "}");
524   verifyFormat("if (a) {\n"
525                "} else if (\n"
526                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
527                "}",
528                getLLVMStyleWithColumns(62));
529 }
530 
531 TEST_F(FormatTest, FormatsForLoop) {
532   verifyFormat(
533       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
534       "     ++VeryVeryLongLoopVariable)\n"
535       "  ;");
536   verifyFormat("for (;;)\n"
537                "  f();");
538   verifyFormat("for (;;) {\n}");
539   verifyFormat("for (;;) {\n"
540                "  f();\n"
541                "}");
542   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
543 
544   verifyFormat(
545       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
546       "                                          E = UnwrappedLines.end();\n"
547       "     I != E; ++I) {\n}");
548 
549   verifyFormat(
550       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
551       "     ++IIIII) {\n}");
552   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
553                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
554                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
555   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
556                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
557                "         E = FD->getDeclsInPrototypeScope().end();\n"
558                "     I != E; ++I) {\n}");
559   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
560                "         I = Container.begin(),\n"
561                "         E = Container.end();\n"
562                "     I != E; ++I) {\n}",
563                getLLVMStyleWithColumns(76));
564 
565   verifyFormat(
566       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
567       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
568       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
569       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
570       "     ++aaaaaaaaaaa) {\n}");
571   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
572                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
573                "     ++i) {\n}");
574   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
575                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
576                "}");
577   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
578                "         aaaaaaaaaa);\n"
579                "     iter; ++iter) {\n"
580                "}");
581   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
582                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
583                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
584                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
585 
586   FormatStyle NoBinPacking = getLLVMStyle();
587   NoBinPacking.BinPackParameters = false;
588   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
589                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
590                "                                           aaaaaaaaaaaaaaaa,\n"
591                "                                           aaaaaaaaaaaaaaaa,\n"
592                "                                           aaaaaaaaaaaaaaaa);\n"
593                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
594                "}",
595                NoBinPacking);
596   verifyFormat(
597       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
598       "                                          E = UnwrappedLines.end();\n"
599       "     I != E;\n"
600       "     ++I) {\n}",
601       NoBinPacking);
602 }
603 
604 TEST_F(FormatTest, RangeBasedForLoops) {
605   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
606                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
607   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
608                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
609   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
610                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
611   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
612                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
613 }
614 
615 TEST_F(FormatTest, ForEachLoops) {
616   verifyFormat("void f() {\n"
617                "  foreach (Item *item, itemlist) {}\n"
618                "  Q_FOREACH (Item *item, itemlist) {}\n"
619                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
620                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
621                "}");
622 
623   // As function-like macros.
624   verifyFormat("#define foreach(x, y)\n"
625                "#define Q_FOREACH(x, y)\n"
626                "#define BOOST_FOREACH(x, y)\n"
627                "#define UNKNOWN_FOREACH(x, y)\n");
628 
629   // Not as function-like macros.
630   verifyFormat("#define foreach (x, y)\n"
631                "#define Q_FOREACH (x, y)\n"
632                "#define BOOST_FOREACH (x, y)\n"
633                "#define UNKNOWN_FOREACH (x, y)\n");
634 }
635 
636 TEST_F(FormatTest, FormatsWhileLoop) {
637   verifyFormat("while (true) {\n}");
638   verifyFormat("while (true)\n"
639                "  f();");
640   verifyFormat("while () {\n}");
641   verifyFormat("while () {\n"
642                "  f();\n"
643                "}");
644 }
645 
646 TEST_F(FormatTest, FormatsDoWhile) {
647   verifyFormat("do {\n"
648                "  do_something();\n"
649                "} while (something());");
650   verifyFormat("do\n"
651                "  do_something();\n"
652                "while (something());");
653 }
654 
655 TEST_F(FormatTest, FormatsSwitchStatement) {
656   verifyFormat("switch (x) {\n"
657                "case 1:\n"
658                "  f();\n"
659                "  break;\n"
660                "case kFoo:\n"
661                "case ns::kBar:\n"
662                "case kBaz:\n"
663                "  break;\n"
664                "default:\n"
665                "  g();\n"
666                "  break;\n"
667                "}");
668   verifyFormat("switch (x) {\n"
669                "case 1: {\n"
670                "  f();\n"
671                "  break;\n"
672                "}\n"
673                "case 2: {\n"
674                "  break;\n"
675                "}\n"
676                "}");
677   verifyFormat("switch (x) {\n"
678                "case 1: {\n"
679                "  f();\n"
680                "  {\n"
681                "    g();\n"
682                "    h();\n"
683                "  }\n"
684                "  break;\n"
685                "}\n"
686                "}");
687   verifyFormat("switch (x) {\n"
688                "case 1: {\n"
689                "  f();\n"
690                "  if (foo) {\n"
691                "    g();\n"
692                "    h();\n"
693                "  }\n"
694                "  break;\n"
695                "}\n"
696                "}");
697   verifyFormat("switch (x) {\n"
698                "case 1: {\n"
699                "  f();\n"
700                "  g();\n"
701                "} break;\n"
702                "}");
703   verifyFormat("switch (test)\n"
704                "  ;");
705   verifyFormat("switch (x) {\n"
706                "default: {\n"
707                "  // Do nothing.\n"
708                "}\n"
709                "}");
710   verifyFormat("switch (x) {\n"
711                "// comment\n"
712                "// if 1, do f()\n"
713                "case 1:\n"
714                "  f();\n"
715                "}");
716   verifyFormat("switch (x) {\n"
717                "case 1:\n"
718                "  // Do amazing stuff\n"
719                "  {\n"
720                "    f();\n"
721                "    g();\n"
722                "  }\n"
723                "  break;\n"
724                "}");
725   verifyFormat("#define A          \\\n"
726                "  switch (x) {     \\\n"
727                "  case a:          \\\n"
728                "    foo = b;       \\\n"
729                "  }",
730                getLLVMStyleWithColumns(20));
731   verifyFormat("#define OPERATION_CASE(name)           \\\n"
732                "  case OP_name:                        \\\n"
733                "    return operations::Operation##name\n",
734                getLLVMStyleWithColumns(40));
735   verifyFormat("switch (x) {\n"
736                "case 1:;\n"
737                "default:;\n"
738                "  int i;\n"
739                "}");
740 
741   verifyGoogleFormat("switch (x) {\n"
742                      "  case 1:\n"
743                      "    f();\n"
744                      "    break;\n"
745                      "  case kFoo:\n"
746                      "  case ns::kBar:\n"
747                      "  case kBaz:\n"
748                      "    break;\n"
749                      "  default:\n"
750                      "    g();\n"
751                      "    break;\n"
752                      "}");
753   verifyGoogleFormat("switch (x) {\n"
754                      "  case 1: {\n"
755                      "    f();\n"
756                      "    break;\n"
757                      "  }\n"
758                      "}");
759   verifyGoogleFormat("switch (test)\n"
760                      "  ;");
761 
762   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
763                      "  case OP_name:              \\\n"
764                      "    return operations::Operation##name\n");
765   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
766                      "  // Get the correction operation class.\n"
767                      "  switch (OpCode) {\n"
768                      "    CASE(Add);\n"
769                      "    CASE(Subtract);\n"
770                      "    default:\n"
771                      "      return operations::Unknown;\n"
772                      "  }\n"
773                      "#undef OPERATION_CASE\n"
774                      "}");
775   verifyFormat("DEBUG({\n"
776                "  switch (x) {\n"
777                "  case A:\n"
778                "    f();\n"
779                "    break;\n"
780                "  // On B:\n"
781                "  case B:\n"
782                "    g();\n"
783                "    break;\n"
784                "  }\n"
785                "});");
786   verifyFormat("switch (a) {\n"
787                "case (b):\n"
788                "  return;\n"
789                "}");
790 
791   verifyFormat("switch (a) {\n"
792                "case some_namespace::\n"
793                "    some_constant:\n"
794                "  return;\n"
795                "}",
796                getLLVMStyleWithColumns(34));
797 }
798 
799 TEST_F(FormatTest, CaseRanges) {
800   verifyFormat("switch (x) {\n"
801                "case 'A' ... 'Z':\n"
802                "case 1 ... 5:\n"
803                "case a ... b:\n"
804                "  break;\n"
805                "}");
806 }
807 
808 TEST_F(FormatTest, ShortCaseLabels) {
809   FormatStyle Style = getLLVMStyle();
810   Style.AllowShortCaseLabelsOnASingleLine = true;
811   verifyFormat("switch (a) {\n"
812                "case 1: x = 1; break;\n"
813                "case 2: return;\n"
814                "case 3:\n"
815                "case 4:\n"
816                "case 5: return;\n"
817                "case 6: // comment\n"
818                "  return;\n"
819                "case 7:\n"
820                "  // comment\n"
821                "  return;\n"
822                "case 8:\n"
823                "  x = 8; // comment\n"
824                "  break;\n"
825                "default: y = 1; break;\n"
826                "}",
827                Style);
828   verifyFormat("switch (a) {\n"
829                "#if FOO\n"
830                "case 0: return 0;\n"
831                "#endif\n"
832                "}",
833                Style);
834   verifyFormat("switch (a) {\n"
835                "case 1: {\n"
836                "}\n"
837                "case 2: {\n"
838                "  return;\n"
839                "}\n"
840                "case 3: {\n"
841                "  x = 1;\n"
842                "  return;\n"
843                "}\n"
844                "case 4:\n"
845                "  if (x)\n"
846                "    return;\n"
847                "}",
848                Style);
849   Style.ColumnLimit = 21;
850   verifyFormat("switch (a) {\n"
851                "case 1: x = 1; break;\n"
852                "case 2: return;\n"
853                "case 3:\n"
854                "case 4:\n"
855                "case 5: return;\n"
856                "default:\n"
857                "  y = 1;\n"
858                "  break;\n"
859                "}",
860                Style);
861 }
862 
863 TEST_F(FormatTest, FormatsLabels) {
864   verifyFormat("void f() {\n"
865                "  some_code();\n"
866                "test_label:\n"
867                "  some_other_code();\n"
868                "  {\n"
869                "    some_more_code();\n"
870                "  another_label:\n"
871                "    some_more_code();\n"
872                "  }\n"
873                "}");
874   verifyFormat("{\n"
875                "  some_code();\n"
876                "test_label:\n"
877                "  some_other_code();\n"
878                "}");
879   verifyFormat("{\n"
880                "  some_code();\n"
881                "test_label:;\n"
882                "  int i = 0;\n"
883                "}");
884 }
885 
886 //===----------------------------------------------------------------------===//
887 // Tests for classes, namespaces, etc.
888 //===----------------------------------------------------------------------===//
889 
890 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
891   verifyFormat("class A {};");
892 }
893 
894 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
895   verifyFormat("class A {\n"
896                "public:\n"
897                "public: // comment\n"
898                "protected:\n"
899                "private:\n"
900                "  void f() {}\n"
901                "};");
902   verifyGoogleFormat("class A {\n"
903                      " public:\n"
904                      " protected:\n"
905                      " private:\n"
906                      "  void f() {}\n"
907                      "};");
908   verifyFormat("class A {\n"
909                "public slots:\n"
910                "  void f1() {}\n"
911                "public Q_SLOTS:\n"
912                "  void f2() {}\n"
913                "protected slots:\n"
914                "  void f3() {}\n"
915                "protected Q_SLOTS:\n"
916                "  void f4() {}\n"
917                "private slots:\n"
918                "  void f5() {}\n"
919                "private Q_SLOTS:\n"
920                "  void f6() {}\n"
921                "signals:\n"
922                "  void g1();\n"
923                "Q_SIGNALS:\n"
924                "  void g2();\n"
925                "};");
926 
927   // Don't interpret 'signals' the wrong way.
928   verifyFormat("signals.set();");
929   verifyFormat("for (Signals signals : f()) {\n}");
930   verifyFormat("{\n"
931                "  signals.set(); // This needs indentation.\n"
932                "}");
933   verifyFormat("void f() {\n"
934                "label:\n"
935                "  signals.baz();\n"
936                "}");
937 }
938 
939 TEST_F(FormatTest, SeparatesLogicalBlocks) {
940   EXPECT_EQ("class A {\n"
941             "public:\n"
942             "  void f();\n"
943             "\n"
944             "private:\n"
945             "  void g() {}\n"
946             "  // test\n"
947             "protected:\n"
948             "  int h;\n"
949             "};",
950             format("class A {\n"
951                    "public:\n"
952                    "void f();\n"
953                    "private:\n"
954                    "void g() {}\n"
955                    "// test\n"
956                    "protected:\n"
957                    "int h;\n"
958                    "};"));
959   EXPECT_EQ("class A {\n"
960             "protected:\n"
961             "public:\n"
962             "  void f();\n"
963             "};",
964             format("class A {\n"
965                    "protected:\n"
966                    "\n"
967                    "public:\n"
968                    "\n"
969                    "  void f();\n"
970                    "};"));
971 
972   // Even ensure proper spacing inside macros.
973   EXPECT_EQ("#define B     \\\n"
974             "  class A {   \\\n"
975             "   protected: \\\n"
976             "   public:    \\\n"
977             "    void f(); \\\n"
978             "  };",
979             format("#define B     \\\n"
980                    "  class A {   \\\n"
981                    "   protected: \\\n"
982                    "              \\\n"
983                    "   public:    \\\n"
984                    "              \\\n"
985                    "    void f(); \\\n"
986                    "  };",
987                    getGoogleStyle()));
988   // But don't remove empty lines after macros ending in access specifiers.
989   EXPECT_EQ("#define A private:\n"
990             "\n"
991             "int i;",
992             format("#define A         private:\n"
993                    "\n"
994                    "int              i;"));
995 }
996 
997 TEST_F(FormatTest, FormatsClasses) {
998   verifyFormat("class A : public B {};");
999   verifyFormat("class A : public ::B {};");
1000 
1001   verifyFormat(
1002       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1003       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1004   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1005                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1006                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1007   verifyFormat(
1008       "class A : public B, public C, public D, public E, public F {};");
1009   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1010                "                     public C,\n"
1011                "                     public D,\n"
1012                "                     public E,\n"
1013                "                     public F,\n"
1014                "                     public G {};");
1015 
1016   verifyFormat("class\n"
1017                "    ReallyReallyLongClassName {\n"
1018                "  int i;\n"
1019                "};",
1020                getLLVMStyleWithColumns(32));
1021   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1022                "                           aaaaaaaaaaaaaaaa> {};");
1023   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1024                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1025                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1026   verifyFormat("template <class R, class C>\n"
1027                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1028                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1029   verifyFormat("class ::A::B {};");
1030 }
1031 
1032 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1033   verifyFormat("class A {\n} a, b;");
1034   verifyFormat("struct A {\n} a, b;");
1035   verifyFormat("union A {\n} a;");
1036 }
1037 
1038 TEST_F(FormatTest, FormatsEnum) {
1039   verifyFormat("enum {\n"
1040                "  Zero,\n"
1041                "  One = 1,\n"
1042                "  Two = One + 1,\n"
1043                "  Three = (One + Two),\n"
1044                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1045                "  Five = (One, Two, Three, Four, 5)\n"
1046                "};");
1047   verifyGoogleFormat("enum {\n"
1048                      "  Zero,\n"
1049                      "  One = 1,\n"
1050                      "  Two = One + 1,\n"
1051                      "  Three = (One + Two),\n"
1052                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1053                      "  Five = (One, Two, Three, Four, 5)\n"
1054                      "};");
1055   verifyFormat("enum Enum {};");
1056   verifyFormat("enum {};");
1057   verifyFormat("enum X E {} d;");
1058   verifyFormat("enum __attribute__((...)) E {} d;");
1059   verifyFormat("enum __declspec__((...)) E {} d;");
1060   verifyFormat("enum {\n"
1061                "  Bar = Foo<int, int>::value\n"
1062                "};",
1063                getLLVMStyleWithColumns(30));
1064 
1065   verifyFormat("enum ShortEnum { A, B, C };");
1066   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1067 
1068   EXPECT_EQ("enum KeepEmptyLines {\n"
1069             "  ONE,\n"
1070             "\n"
1071             "  TWO,\n"
1072             "\n"
1073             "  THREE\n"
1074             "}",
1075             format("enum KeepEmptyLines {\n"
1076                    "  ONE,\n"
1077                    "\n"
1078                    "  TWO,\n"
1079                    "\n"
1080                    "\n"
1081                    "  THREE\n"
1082                    "}"));
1083   verifyFormat("enum E { // comment\n"
1084                "  ONE,\n"
1085                "  TWO\n"
1086                "};\n"
1087                "int i;");
1088   // Not enums.
1089   verifyFormat("enum X f() {\n"
1090                "  a();\n"
1091                "  return 42;\n"
1092                "}");
1093   verifyFormat("enum X Type::f() {\n"
1094                "  a();\n"
1095                "  return 42;\n"
1096                "}");
1097   verifyFormat("enum ::X f() {\n"
1098                "  a();\n"
1099                "  return 42;\n"
1100                "}");
1101   verifyFormat("enum ns::X f() {\n"
1102                "  a();\n"
1103                "  return 42;\n"
1104                "}");
1105 }
1106 
1107 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1108   verifyFormat("enum Type {\n"
1109                "  One = 0; // These semicolons should be commas.\n"
1110                "  Two = 1;\n"
1111                "};");
1112   verifyFormat("namespace n {\n"
1113                "enum Type {\n"
1114                "  One,\n"
1115                "  Two, // missing };\n"
1116                "  int i;\n"
1117                "}\n"
1118                "void g() {}");
1119 }
1120 
1121 TEST_F(FormatTest, FormatsEnumStruct) {
1122   verifyFormat("enum struct {\n"
1123                "  Zero,\n"
1124                "  One = 1,\n"
1125                "  Two = One + 1,\n"
1126                "  Three = (One + Two),\n"
1127                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1128                "  Five = (One, Two, Three, Four, 5)\n"
1129                "};");
1130   verifyFormat("enum struct Enum {};");
1131   verifyFormat("enum struct {};");
1132   verifyFormat("enum struct X E {} d;");
1133   verifyFormat("enum struct __attribute__((...)) E {} d;");
1134   verifyFormat("enum struct __declspec__((...)) E {} d;");
1135   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1136 }
1137 
1138 TEST_F(FormatTest, FormatsEnumClass) {
1139   verifyFormat("enum class {\n"
1140                "  Zero,\n"
1141                "  One = 1,\n"
1142                "  Two = One + 1,\n"
1143                "  Three = (One + Two),\n"
1144                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1145                "  Five = (One, Two, Three, Four, 5)\n"
1146                "};");
1147   verifyFormat("enum class Enum {};");
1148   verifyFormat("enum class {};");
1149   verifyFormat("enum class X E {} d;");
1150   verifyFormat("enum class __attribute__((...)) E {} d;");
1151   verifyFormat("enum class __declspec__((...)) E {} d;");
1152   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1153 }
1154 
1155 TEST_F(FormatTest, FormatsEnumTypes) {
1156   verifyFormat("enum X : int {\n"
1157                "  A, // Force multiple lines.\n"
1158                "  B\n"
1159                "};");
1160   verifyFormat("enum X : int { A, B };");
1161   verifyFormat("enum X : std::uint32_t { A, B };");
1162 }
1163 
1164 TEST_F(FormatTest, FormatsNSEnums) {
1165   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1166   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1167                      "  // Information about someDecentlyLongValue.\n"
1168                      "  someDecentlyLongValue,\n"
1169                      "  // Information about anotherDecentlyLongValue.\n"
1170                      "  anotherDecentlyLongValue,\n"
1171                      "  // Information about aThirdDecentlyLongValue.\n"
1172                      "  aThirdDecentlyLongValue\n"
1173                      "};");
1174   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1175                      "  a = 1,\n"
1176                      "  b = 2,\n"
1177                      "  c = 3,\n"
1178                      "};");
1179   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1180                      "  a = 1,\n"
1181                      "  b = 2,\n"
1182                      "  c = 3,\n"
1183                      "};");
1184   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1185                      "  a = 1,\n"
1186                      "  b = 2,\n"
1187                      "  c = 3,\n"
1188                      "};");
1189 }
1190 
1191 TEST_F(FormatTest, FormatsBitfields) {
1192   verifyFormat("struct Bitfields {\n"
1193                "  unsigned sClass : 8;\n"
1194                "  unsigned ValueKind : 2;\n"
1195                "};");
1196   verifyFormat("struct A {\n"
1197                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1198                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1199                "};");
1200   verifyFormat("struct MyStruct {\n"
1201                "  uchar data;\n"
1202                "  uchar : 8;\n"
1203                "  uchar : 8;\n"
1204                "  uchar other;\n"
1205                "};");
1206 }
1207 
1208 TEST_F(FormatTest, FormatsNamespaces) {
1209   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1210   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1211 
1212   verifyFormat("namespace some_namespace {\n"
1213                "class A {};\n"
1214                "void f() { f(); }\n"
1215                "}",
1216                LLVMWithNoNamespaceFix);
1217   verifyFormat("namespace {\n"
1218                "class A {};\n"
1219                "void f() { f(); }\n"
1220                "}",
1221                LLVMWithNoNamespaceFix);
1222   verifyFormat("inline namespace X {\n"
1223                "class A {};\n"
1224                "void f() { f(); }\n"
1225                "}",
1226                LLVMWithNoNamespaceFix);
1227   verifyFormat("using namespace some_namespace;\n"
1228                "class A {};\n"
1229                "void f() { f(); }",
1230                LLVMWithNoNamespaceFix);
1231 
1232   // This code is more common than we thought; if we
1233   // layout this correctly the semicolon will go into
1234   // its own line, which is undesirable.
1235   verifyFormat("namespace {};",
1236                LLVMWithNoNamespaceFix);
1237   verifyFormat("namespace {\n"
1238                "class A {};\n"
1239                "};",
1240                LLVMWithNoNamespaceFix);
1241 
1242   verifyFormat("namespace {\n"
1243                "int SomeVariable = 0; // comment\n"
1244                "} // namespace",
1245                LLVMWithNoNamespaceFix);
1246   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1247             "#define HEADER_GUARD\n"
1248             "namespace my_namespace {\n"
1249             "int i;\n"
1250             "} // my_namespace\n"
1251             "#endif // HEADER_GUARD",
1252             format("#ifndef HEADER_GUARD\n"
1253                    " #define HEADER_GUARD\n"
1254                    "   namespace my_namespace {\n"
1255                    "int i;\n"
1256                    "}    // my_namespace\n"
1257                    "#endif    // HEADER_GUARD",
1258                    LLVMWithNoNamespaceFix));
1259 
1260   EXPECT_EQ("namespace A::B {\n"
1261             "class C {};\n"
1262             "}",
1263             format("namespace A::B {\n"
1264                    "class C {};\n"
1265                    "}",
1266                    LLVMWithNoNamespaceFix));
1267 
1268   FormatStyle Style = getLLVMStyle();
1269   Style.NamespaceIndentation = FormatStyle::NI_All;
1270   EXPECT_EQ("namespace out {\n"
1271             "  int i;\n"
1272             "  namespace in {\n"
1273             "    int i;\n"
1274             "  } // namespace in\n"
1275             "} // namespace out",
1276             format("namespace out {\n"
1277                    "int i;\n"
1278                    "namespace in {\n"
1279                    "int i;\n"
1280                    "} // namespace in\n"
1281                    "} // namespace out",
1282                    Style));
1283 
1284   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1285   EXPECT_EQ("namespace out {\n"
1286             "int i;\n"
1287             "namespace in {\n"
1288             "  int i;\n"
1289             "} // namespace in\n"
1290             "} // namespace out",
1291             format("namespace out {\n"
1292                    "int i;\n"
1293                    "namespace in {\n"
1294                    "int i;\n"
1295                    "} // namespace in\n"
1296                    "} // namespace out",
1297                    Style));
1298 }
1299 
1300 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1301 
1302 TEST_F(FormatTest, FormatsInlineASM) {
1303   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1304   verifyFormat("asm(\"nop\" ::: \"memory\");");
1305   verifyFormat(
1306       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1307       "    \"cpuid\\n\\t\"\n"
1308       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1309       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1310       "    : \"a\"(value));");
1311   EXPECT_EQ(
1312       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1313       "  __asm {\n"
1314       "        mov     edx,[that] // vtable in edx\n"
1315       "        mov     eax,methodIndex\n"
1316       "        call    [edx][eax*4] // stdcall\n"
1317       "  }\n"
1318       "}",
1319       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1320              "    __asm {\n"
1321              "        mov     edx,[that] // vtable in edx\n"
1322              "        mov     eax,methodIndex\n"
1323              "        call    [edx][eax*4] // stdcall\n"
1324              "    }\n"
1325              "}"));
1326   EXPECT_EQ("_asm {\n"
1327             "  xor eax, eax;\n"
1328             "  cpuid;\n"
1329             "}",
1330             format("_asm {\n"
1331                    "  xor eax, eax;\n"
1332                    "  cpuid;\n"
1333                    "}"));
1334   verifyFormat("void function() {\n"
1335                "  // comment\n"
1336                "  asm(\"\");\n"
1337                "}");
1338   EXPECT_EQ("__asm {\n"
1339             "}\n"
1340             "int i;",
1341             format("__asm   {\n"
1342                    "}\n"
1343                    "int   i;"));
1344 }
1345 
1346 TEST_F(FormatTest, FormatTryCatch) {
1347   verifyFormat("try {\n"
1348                "  throw a * b;\n"
1349                "} catch (int a) {\n"
1350                "  // Do nothing.\n"
1351                "} catch (...) {\n"
1352                "  exit(42);\n"
1353                "}");
1354 
1355   // Function-level try statements.
1356   verifyFormat("int f() try { return 4; } catch (...) {\n"
1357                "  return 5;\n"
1358                "}");
1359   verifyFormat("class A {\n"
1360                "  int a;\n"
1361                "  A() try : a(0) {\n"
1362                "  } catch (...) {\n"
1363                "    throw;\n"
1364                "  }\n"
1365                "};\n");
1366 
1367   // Incomplete try-catch blocks.
1368   verifyIncompleteFormat("try {} catch (");
1369 }
1370 
1371 TEST_F(FormatTest, FormatSEHTryCatch) {
1372   verifyFormat("__try {\n"
1373                "  int a = b * c;\n"
1374                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1375                "  // Do nothing.\n"
1376                "}");
1377 
1378   verifyFormat("__try {\n"
1379                "  int a = b * c;\n"
1380                "} __finally {\n"
1381                "  // Do nothing.\n"
1382                "}");
1383 
1384   verifyFormat("DEBUG({\n"
1385                "  __try {\n"
1386                "  } __finally {\n"
1387                "  }\n"
1388                "});\n");
1389 }
1390 
1391 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1392   verifyFormat("try {\n"
1393                "  f();\n"
1394                "} catch {\n"
1395                "  g();\n"
1396                "}");
1397   verifyFormat("try {\n"
1398                "  f();\n"
1399                "} catch (A a) MACRO(x) {\n"
1400                "  g();\n"
1401                "} catch (B b) MACRO(x) {\n"
1402                "  g();\n"
1403                "}");
1404 }
1405 
1406 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1407   FormatStyle Style = getLLVMStyle();
1408   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1409                           FormatStyle::BS_WebKit}) {
1410     Style.BreakBeforeBraces = BraceStyle;
1411     verifyFormat("try {\n"
1412                  "  // something\n"
1413                  "} catch (...) {\n"
1414                  "  // something\n"
1415                  "}",
1416                  Style);
1417   }
1418   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1419   verifyFormat("try {\n"
1420                "  // something\n"
1421                "}\n"
1422                "catch (...) {\n"
1423                "  // something\n"
1424                "}",
1425                Style);
1426   verifyFormat("__try {\n"
1427                "  // something\n"
1428                "}\n"
1429                "__finally {\n"
1430                "  // something\n"
1431                "}",
1432                Style);
1433   verifyFormat("@try {\n"
1434                "  // something\n"
1435                "}\n"
1436                "@finally {\n"
1437                "  // something\n"
1438                "}",
1439                Style);
1440   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1441   verifyFormat("try\n"
1442                "{\n"
1443                "  // something\n"
1444                "}\n"
1445                "catch (...)\n"
1446                "{\n"
1447                "  // something\n"
1448                "}",
1449                Style);
1450   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1451   verifyFormat("try\n"
1452                "  {\n"
1453                "    // something\n"
1454                "  }\n"
1455                "catch (...)\n"
1456                "  {\n"
1457                "    // something\n"
1458                "  }",
1459                Style);
1460   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1461   Style.BraceWrapping.BeforeCatch = true;
1462   verifyFormat("try {\n"
1463                "  // something\n"
1464                "}\n"
1465                "catch (...) {\n"
1466                "  // something\n"
1467                "}",
1468                Style);
1469 }
1470 
1471 TEST_F(FormatTest, StaticInitializers) {
1472   verifyFormat("static SomeClass SC = {1, 'a'};");
1473 
1474   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1475                "    100000000, "
1476                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1477 
1478   // Here, everything other than the "}" would fit on a line.
1479   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1480                "    10000000000000000000000000};");
1481   EXPECT_EQ("S s = {a,\n"
1482             "\n"
1483             "       b};",
1484             format("S s = {\n"
1485                    "  a,\n"
1486                    "\n"
1487                    "  b\n"
1488                    "};"));
1489 
1490   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1491   // line. However, the formatting looks a bit off and this probably doesn't
1492   // happen often in practice.
1493   verifyFormat("static int Variable[1] = {\n"
1494                "    {1000000000000000000000000000000000000}};",
1495                getLLVMStyleWithColumns(40));
1496 }
1497 
1498 TEST_F(FormatTest, DesignatedInitializers) {
1499   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1500   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1501                "                    .bbbbbbbbbb = 2,\n"
1502                "                    .cccccccccc = 3,\n"
1503                "                    .dddddddddd = 4,\n"
1504                "                    .eeeeeeeeee = 5};");
1505   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1506                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1507                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1508                "    .ccccccccccccccccccccccccccc = 3,\n"
1509                "    .ddddddddddddddddddddddddddd = 4,\n"
1510                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1511 
1512   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1513 }
1514 
1515 TEST_F(FormatTest, NestedStaticInitializers) {
1516   verifyFormat("static A x = {{{}}};\n");
1517   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1518                "               {init1, init2, init3, init4}}};",
1519                getLLVMStyleWithColumns(50));
1520 
1521   verifyFormat("somes Status::global_reps[3] = {\n"
1522                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1523                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1524                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1525                getLLVMStyleWithColumns(60));
1526   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1527                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1528                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1529                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1530   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1531                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1532                "rect.fTop}};");
1533 
1534   verifyFormat(
1535       "SomeArrayOfSomeType a = {\n"
1536       "    {{1, 2, 3},\n"
1537       "     {1, 2, 3},\n"
1538       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1539       "      333333333333333333333333333333},\n"
1540       "     {1, 2, 3},\n"
1541       "     {1, 2, 3}}};");
1542   verifyFormat(
1543       "SomeArrayOfSomeType a = {\n"
1544       "    {{1, 2, 3}},\n"
1545       "    {{1, 2, 3}},\n"
1546       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1547       "      333333333333333333333333333333}},\n"
1548       "    {{1, 2, 3}},\n"
1549       "    {{1, 2, 3}}};");
1550 
1551   verifyFormat("struct {\n"
1552                "  unsigned bit;\n"
1553                "  const char *const name;\n"
1554                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1555                "                 {kOsWin, \"Windows\"},\n"
1556                "                 {kOsLinux, \"Linux\"},\n"
1557                "                 {kOsCrOS, \"Chrome OS\"}};");
1558   verifyFormat("struct {\n"
1559                "  unsigned bit;\n"
1560                "  const char *const name;\n"
1561                "} kBitsToOs[] = {\n"
1562                "    {kOsMac, \"Mac\"},\n"
1563                "    {kOsWin, \"Windows\"},\n"
1564                "    {kOsLinux, \"Linux\"},\n"
1565                "    {kOsCrOS, \"Chrome OS\"},\n"
1566                "};");
1567 }
1568 
1569 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1570   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1571                "                      \\\n"
1572                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1573 }
1574 
1575 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1576   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1577                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1578 
1579   // Do break defaulted and deleted functions.
1580   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1581                "    default;",
1582                getLLVMStyleWithColumns(40));
1583   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1584                "    delete;",
1585                getLLVMStyleWithColumns(40));
1586 }
1587 
1588 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1589   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1590                getLLVMStyleWithColumns(40));
1591   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1592                getLLVMStyleWithColumns(40));
1593   EXPECT_EQ("#define Q                              \\\n"
1594             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1595             "  \"aaaaaaaa.cpp\"",
1596             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1597                    getLLVMStyleWithColumns(40)));
1598 }
1599 
1600 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1601   EXPECT_EQ("# 123 \"A string literal\"",
1602             format("   #     123    \"A string literal\""));
1603 }
1604 
1605 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1606   EXPECT_EQ("#;", format("#;"));
1607   verifyFormat("#\n;\n;\n;");
1608 }
1609 
1610 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1611   EXPECT_EQ("#line 42 \"test\"\n",
1612             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1613   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1614                                     getLLVMStyleWithColumns(12)));
1615 }
1616 
1617 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1618   EXPECT_EQ("#line 42 \"test\"",
1619             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1620   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1621 }
1622 
1623 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1624   verifyFormat("#define A \\x20");
1625   verifyFormat("#define A \\ x20");
1626   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1627   verifyFormat("#define A ''");
1628   verifyFormat("#define A ''qqq");
1629   verifyFormat("#define A `qqq");
1630   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1631   EXPECT_EQ("const char *c = STRINGIFY(\n"
1632             "\\na : b);",
1633             format("const char * c = STRINGIFY(\n"
1634                    "\\na : b);"));
1635 
1636   verifyFormat("a\r\\");
1637   verifyFormat("a\v\\");
1638   verifyFormat("a\f\\");
1639 }
1640 
1641 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1642   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1643   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1644   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1645   // FIXME: We never break before the macro name.
1646   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1647 
1648   verifyFormat("#define A A\n#define A A");
1649   verifyFormat("#define A(X) A\n#define A A");
1650 
1651   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1652   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1653 }
1654 
1655 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1656   EXPECT_EQ("// somecomment\n"
1657             "#include \"a.h\"\n"
1658             "#define A(  \\\n"
1659             "    A, B)\n"
1660             "#include \"b.h\"\n"
1661             "// somecomment\n",
1662             format("  // somecomment\n"
1663                    "  #include \"a.h\"\n"
1664                    "#define A(A,\\\n"
1665                    "    B)\n"
1666                    "    #include \"b.h\"\n"
1667                    " // somecomment\n",
1668                    getLLVMStyleWithColumns(13)));
1669 }
1670 
1671 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1672 
1673 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1674   EXPECT_EQ("#define A    \\\n"
1675             "  c;         \\\n"
1676             "  e;\n"
1677             "f;",
1678             format("#define A c; e;\n"
1679                    "f;",
1680                    getLLVMStyleWithColumns(14)));
1681 }
1682 
1683 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1684 
1685 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1686   EXPECT_EQ("int x,\n"
1687             "#define A\n"
1688             "    y;",
1689             format("int x,\n#define A\ny;"));
1690 }
1691 
1692 TEST_F(FormatTest, HashInMacroDefinition) {
1693   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
1694   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1695   verifyFormat("#define A  \\\n"
1696                "  {        \\\n"
1697                "    f(#c); \\\n"
1698                "  }",
1699                getLLVMStyleWithColumns(11));
1700 
1701   verifyFormat("#define A(X)         \\\n"
1702                "  void function##X()",
1703                getLLVMStyleWithColumns(22));
1704 
1705   verifyFormat("#define A(a, b, c)   \\\n"
1706                "  void a##b##c()",
1707                getLLVMStyleWithColumns(22));
1708 
1709   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1710 }
1711 
1712 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1713   EXPECT_EQ("#define A (x)", format("#define A (x)"));
1714   EXPECT_EQ("#define A(x)", format("#define A(x)"));
1715 }
1716 
1717 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1718   EXPECT_EQ("#define A b;", format("#define A \\\n"
1719                                    "          \\\n"
1720                                    "  b;",
1721                                    getLLVMStyleWithColumns(25)));
1722   EXPECT_EQ("#define A \\\n"
1723             "          \\\n"
1724             "  a;      \\\n"
1725             "  b;",
1726             format("#define A \\\n"
1727                    "          \\\n"
1728                    "  a;      \\\n"
1729                    "  b;",
1730                    getLLVMStyleWithColumns(11)));
1731   EXPECT_EQ("#define A \\\n"
1732             "  a;      \\\n"
1733             "          \\\n"
1734             "  b;",
1735             format("#define A \\\n"
1736                    "  a;      \\\n"
1737                    "          \\\n"
1738                    "  b;",
1739                    getLLVMStyleWithColumns(11)));
1740 }
1741 
1742 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1743   verifyIncompleteFormat("#define A :");
1744   verifyFormat("#define SOMECASES  \\\n"
1745                "  case 1:          \\\n"
1746                "  case 2\n",
1747                getLLVMStyleWithColumns(20));
1748   verifyFormat("#define MACRO(a) \\\n"
1749                "  if (a)         \\\n"
1750                "    f();         \\\n"
1751                "  else           \\\n"
1752                "    g()",
1753                getLLVMStyleWithColumns(18));
1754   verifyFormat("#define A template <typename T>");
1755   verifyIncompleteFormat("#define STR(x) #x\n"
1756                          "f(STR(this_is_a_string_literal{));");
1757   verifyFormat("#pragma omp threadprivate( \\\n"
1758                "    y)), // expected-warning",
1759                getLLVMStyleWithColumns(28));
1760   verifyFormat("#d, = };");
1761   verifyFormat("#if \"a");
1762   verifyIncompleteFormat("({\n"
1763                          "#define b     \\\n"
1764                          "  }           \\\n"
1765                          "  a\n"
1766                          "a",
1767                          getLLVMStyleWithColumns(15));
1768   verifyFormat("#define A     \\\n"
1769                "  {           \\\n"
1770                "    {\n"
1771                "#define B     \\\n"
1772                "  }           \\\n"
1773                "  }",
1774                getLLVMStyleWithColumns(15));
1775   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
1776   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
1777   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
1778   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
1779 }
1780 
1781 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
1782   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
1783   EXPECT_EQ("class A : public QObject {\n"
1784             "  Q_OBJECT\n"
1785             "\n"
1786             "  A() {}\n"
1787             "};",
1788             format("class A  :  public QObject {\n"
1789                    "     Q_OBJECT\n"
1790                    "\n"
1791                    "  A() {\n}\n"
1792                    "}  ;"));
1793   EXPECT_EQ("MACRO\n"
1794             "/*static*/ int i;",
1795             format("MACRO\n"
1796                    " /*static*/ int   i;"));
1797   EXPECT_EQ("SOME_MACRO\n"
1798             "namespace {\n"
1799             "void f();\n"
1800             "} // namespace",
1801             format("SOME_MACRO\n"
1802                    "  namespace    {\n"
1803                    "void   f(  );\n"
1804                    "} // namespace"));
1805   // Only if the identifier contains at least 5 characters.
1806   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
1807   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
1808   // Only if everything is upper case.
1809   EXPECT_EQ("class A : public QObject {\n"
1810             "  Q_Object A() {}\n"
1811             "};",
1812             format("class A  :  public QObject {\n"
1813                    "     Q_Object\n"
1814                    "  A() {\n}\n"
1815                    "}  ;"));
1816 
1817   // Only if the next line can actually start an unwrapped line.
1818   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
1819             format("SOME_WEIRD_LOG_MACRO\n"
1820                    "<< SomeThing;"));
1821 
1822   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
1823                "(n, buffers))\n",
1824                getChromiumStyle(FormatStyle::LK_Cpp));
1825 }
1826 
1827 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
1828   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1829             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1830             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1831             "class X {};\n"
1832             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1833             "int *createScopDetectionPass() { return 0; }",
1834             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1835                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1836                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1837                    "  class X {};\n"
1838                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1839                    "  int *createScopDetectionPass() { return 0; }"));
1840   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
1841   // braces, so that inner block is indented one level more.
1842   EXPECT_EQ("int q() {\n"
1843             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1844             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1845             "  IPC_END_MESSAGE_MAP()\n"
1846             "}",
1847             format("int q() {\n"
1848                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1849                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1850                    "  IPC_END_MESSAGE_MAP()\n"
1851                    "}"));
1852 
1853   // Same inside macros.
1854   EXPECT_EQ("#define LIST(L) \\\n"
1855             "  L(A)          \\\n"
1856             "  L(B)          \\\n"
1857             "  L(C)",
1858             format("#define LIST(L) \\\n"
1859                    "  L(A) \\\n"
1860                    "  L(B) \\\n"
1861                    "  L(C)",
1862                    getGoogleStyle()));
1863 
1864   // These must not be recognized as macros.
1865   EXPECT_EQ("int q() {\n"
1866             "  f(x);\n"
1867             "  f(x) {}\n"
1868             "  f(x)->g();\n"
1869             "  f(x)->*g();\n"
1870             "  f(x).g();\n"
1871             "  f(x) = x;\n"
1872             "  f(x) += x;\n"
1873             "  f(x) -= x;\n"
1874             "  f(x) *= x;\n"
1875             "  f(x) /= x;\n"
1876             "  f(x) %= x;\n"
1877             "  f(x) &= x;\n"
1878             "  f(x) |= x;\n"
1879             "  f(x) ^= x;\n"
1880             "  f(x) >>= x;\n"
1881             "  f(x) <<= x;\n"
1882             "  f(x)[y].z();\n"
1883             "  LOG(INFO) << x;\n"
1884             "  ifstream(x) >> x;\n"
1885             "}\n",
1886             format("int q() {\n"
1887                    "  f(x)\n;\n"
1888                    "  f(x)\n {}\n"
1889                    "  f(x)\n->g();\n"
1890                    "  f(x)\n->*g();\n"
1891                    "  f(x)\n.g();\n"
1892                    "  f(x)\n = x;\n"
1893                    "  f(x)\n += x;\n"
1894                    "  f(x)\n -= x;\n"
1895                    "  f(x)\n *= x;\n"
1896                    "  f(x)\n /= x;\n"
1897                    "  f(x)\n %= x;\n"
1898                    "  f(x)\n &= x;\n"
1899                    "  f(x)\n |= x;\n"
1900                    "  f(x)\n ^= x;\n"
1901                    "  f(x)\n >>= x;\n"
1902                    "  f(x)\n <<= x;\n"
1903                    "  f(x)\n[y].z();\n"
1904                    "  LOG(INFO)\n << x;\n"
1905                    "  ifstream(x)\n >> x;\n"
1906                    "}\n"));
1907   EXPECT_EQ("int q() {\n"
1908             "  F(x)\n"
1909             "  if (1) {\n"
1910             "  }\n"
1911             "  F(x)\n"
1912             "  while (1) {\n"
1913             "  }\n"
1914             "  F(x)\n"
1915             "  G(x);\n"
1916             "  F(x)\n"
1917             "  try {\n"
1918             "    Q();\n"
1919             "  } catch (...) {\n"
1920             "  }\n"
1921             "}\n",
1922             format("int q() {\n"
1923                    "F(x)\n"
1924                    "if (1) {}\n"
1925                    "F(x)\n"
1926                    "while (1) {}\n"
1927                    "F(x)\n"
1928                    "G(x);\n"
1929                    "F(x)\n"
1930                    "try { Q(); } catch (...) {}\n"
1931                    "}\n"));
1932   EXPECT_EQ("class A {\n"
1933             "  A() : t(0) {}\n"
1934             "  A(int i) noexcept() : {}\n"
1935             "  A(X x)\n" // FIXME: function-level try blocks are broken.
1936             "  try : t(0) {\n"
1937             "  } catch (...) {\n"
1938             "  }\n"
1939             "};",
1940             format("class A {\n"
1941                    "  A()\n : t(0) {}\n"
1942                    "  A(int i)\n noexcept() : {}\n"
1943                    "  A(X x)\n"
1944                    "  try : t(0) {} catch (...) {}\n"
1945                    "};"));
1946   EXPECT_EQ("class SomeClass {\n"
1947             "public:\n"
1948             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1949             "};",
1950             format("class SomeClass {\n"
1951                    "public:\n"
1952                    "  SomeClass()\n"
1953                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1954                    "};"));
1955   EXPECT_EQ("class SomeClass {\n"
1956             "public:\n"
1957             "  SomeClass()\n"
1958             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1959             "};",
1960             format("class SomeClass {\n"
1961                    "public:\n"
1962                    "  SomeClass()\n"
1963                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1964                    "};",
1965                    getLLVMStyleWithColumns(40)));
1966 
1967   verifyFormat("MACRO(>)");
1968 }
1969 
1970 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
1971   verifyFormat("#define A \\\n"
1972                "  f({     \\\n"
1973                "    g();  \\\n"
1974                "  });",
1975                getLLVMStyleWithColumns(11));
1976 }
1977 
1978 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1979   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1980 }
1981 
1982 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1983   verifyFormat("{\n  { a #c; }\n}");
1984 }
1985 
1986 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
1987   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
1988             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
1989   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
1990             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
1991 }
1992 
1993 TEST_F(FormatTest, EscapedNewlines) {
1994   EXPECT_EQ(
1995       "#define A \\\n  int i;  \\\n  int j;",
1996       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
1997   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
1998   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
1999   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2000   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2001 }
2002 
2003 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2004   verifyFormat("#define A \\\n"
2005                "  int v(  \\\n"
2006                "      a); \\\n"
2007                "  int i;",
2008                getLLVMStyleWithColumns(11));
2009 }
2010 
2011 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2012   EXPECT_EQ(
2013       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2014       "                      \\\n"
2015       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2016       "\n"
2017       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2018       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2019       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2020              "\\\n"
2021              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2022              "  \n"
2023              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2024              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2025 }
2026 
2027 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2028   EXPECT_EQ("int\n"
2029             "#define A\n"
2030             "    a;",
2031             format("int\n#define A\na;"));
2032   verifyFormat("functionCallTo(\n"
2033                "    someOtherFunction(\n"
2034                "        withSomeParameters, whichInSequence,\n"
2035                "        areLongerThanALine(andAnotherCall,\n"
2036                "#define A B\n"
2037                "                           withMoreParamters,\n"
2038                "                           whichStronglyInfluenceTheLayout),\n"
2039                "        andMoreParameters),\n"
2040                "    trailing);",
2041                getLLVMStyleWithColumns(69));
2042   verifyFormat("Foo::Foo()\n"
2043                "#ifdef BAR\n"
2044                "    : baz(0)\n"
2045                "#endif\n"
2046                "{\n"
2047                "}");
2048   verifyFormat("void f() {\n"
2049                "  if (true)\n"
2050                "#ifdef A\n"
2051                "    f(42);\n"
2052                "  x();\n"
2053                "#else\n"
2054                "    g();\n"
2055                "  x();\n"
2056                "#endif\n"
2057                "}");
2058   verifyFormat("void f(param1, param2,\n"
2059                "       param3,\n"
2060                "#ifdef A\n"
2061                "       param4(param5,\n"
2062                "#ifdef A1\n"
2063                "              param6,\n"
2064                "#ifdef A2\n"
2065                "              param7),\n"
2066                "#else\n"
2067                "              param8),\n"
2068                "       param9,\n"
2069                "#endif\n"
2070                "       param10,\n"
2071                "#endif\n"
2072                "       param11)\n"
2073                "#else\n"
2074                "       param12)\n"
2075                "#endif\n"
2076                "{\n"
2077                "  x();\n"
2078                "}",
2079                getLLVMStyleWithColumns(28));
2080   verifyFormat("#if 1\n"
2081                "int i;");
2082   verifyFormat("#if 1\n"
2083                "#endif\n"
2084                "#if 1\n"
2085                "#else\n"
2086                "#endif\n");
2087   verifyFormat("DEBUG({\n"
2088                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2089                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2090                "});\n"
2091                "#if a\n"
2092                "#else\n"
2093                "#endif");
2094 
2095   verifyIncompleteFormat("void f(\n"
2096                          "#if A\n"
2097                          "    );\n"
2098                          "#else\n"
2099                          "#endif");
2100 }
2101 
2102 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2103   verifyFormat("#endif\n"
2104                "#if B");
2105 }
2106 
2107 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2108   FormatStyle SingleLine = getLLVMStyle();
2109   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2110   verifyFormat("#if 0\n"
2111                "#elif 1\n"
2112                "#endif\n"
2113                "void foo() {\n"
2114                "  if (test) foo2();\n"
2115                "}",
2116                SingleLine);
2117 }
2118 
2119 TEST_F(FormatTest, LayoutBlockInsideParens) {
2120   verifyFormat("functionCall({ int i; });");
2121   verifyFormat("functionCall({\n"
2122                "  int i;\n"
2123                "  int j;\n"
2124                "});");
2125   verifyFormat("functionCall(\n"
2126                "    {\n"
2127                "      int i;\n"
2128                "      int j;\n"
2129                "    },\n"
2130                "    aaaa, bbbb, cccc);");
2131   verifyFormat("functionA(functionB({\n"
2132                "            int i;\n"
2133                "            int j;\n"
2134                "          }),\n"
2135                "          aaaa, bbbb, cccc);");
2136   verifyFormat("functionCall(\n"
2137                "    {\n"
2138                "      int i;\n"
2139                "      int j;\n"
2140                "    },\n"
2141                "    aaaa, bbbb, // comment\n"
2142                "    cccc);");
2143   verifyFormat("functionA(functionB({\n"
2144                "            int i;\n"
2145                "            int j;\n"
2146                "          }),\n"
2147                "          aaaa, bbbb, // comment\n"
2148                "          cccc);");
2149   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2150   verifyFormat("functionCall(aaaa, bbbb, {\n"
2151                "  int i;\n"
2152                "  int j;\n"
2153                "});");
2154   verifyFormat(
2155       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2156       "    {\n"
2157       "      int i; // break\n"
2158       "    },\n"
2159       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2160       "                                     ccccccccccccccccc));");
2161   verifyFormat("DEBUG({\n"
2162                "  if (a)\n"
2163                "    f();\n"
2164                "});");
2165 }
2166 
2167 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2168   EXPECT_EQ("SOME_MACRO { int i; }\n"
2169             "int i;",
2170             format("  SOME_MACRO  {int i;}  int i;"));
2171 }
2172 
2173 TEST_F(FormatTest, LayoutNestedBlocks) {
2174   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2175                "  struct s {\n"
2176                "    int i;\n"
2177                "  };\n"
2178                "  s kBitsToOs[] = {{10}};\n"
2179                "  for (int i = 0; i < 10; ++i)\n"
2180                "    return;\n"
2181                "}");
2182   verifyFormat("call(parameter, {\n"
2183                "  something();\n"
2184                "  // Comment using all columns.\n"
2185                "  somethingelse();\n"
2186                "});",
2187                getLLVMStyleWithColumns(40));
2188   verifyFormat("DEBUG( //\n"
2189                "    { f(); }, a);");
2190   verifyFormat("DEBUG( //\n"
2191                "    {\n"
2192                "      f(); //\n"
2193                "    },\n"
2194                "    a);");
2195 
2196   EXPECT_EQ("call(parameter, {\n"
2197             "  something();\n"
2198             "  // Comment too\n"
2199             "  // looooooooooong.\n"
2200             "  somethingElse();\n"
2201             "});",
2202             format("call(parameter, {\n"
2203                    "  something();\n"
2204                    "  // Comment too looooooooooong.\n"
2205                    "  somethingElse();\n"
2206                    "});",
2207                    getLLVMStyleWithColumns(29)));
2208   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2209   EXPECT_EQ("DEBUG({ // comment\n"
2210             "  int i;\n"
2211             "});",
2212             format("DEBUG({ // comment\n"
2213                    "int  i;\n"
2214                    "});"));
2215   EXPECT_EQ("DEBUG({\n"
2216             "  int i;\n"
2217             "\n"
2218             "  // comment\n"
2219             "  int j;\n"
2220             "});",
2221             format("DEBUG({\n"
2222                    "  int  i;\n"
2223                    "\n"
2224                    "  // comment\n"
2225                    "  int  j;\n"
2226                    "});"));
2227 
2228   verifyFormat("DEBUG({\n"
2229                "  if (a)\n"
2230                "    return;\n"
2231                "});");
2232   verifyGoogleFormat("DEBUG({\n"
2233                      "  if (a) return;\n"
2234                      "});");
2235   FormatStyle Style = getGoogleStyle();
2236   Style.ColumnLimit = 45;
2237   verifyFormat("Debug(aaaaa,\n"
2238                "      {\n"
2239                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2240                "      },\n"
2241                "      a);",
2242                Style);
2243 
2244   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2245 
2246   verifyNoCrash("^{v^{a}}");
2247 }
2248 
2249 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2250   EXPECT_EQ("#define MACRO()                     \\\n"
2251             "  Debug(aaa, /* force line break */ \\\n"
2252             "        {                           \\\n"
2253             "          int i;                    \\\n"
2254             "          int j;                    \\\n"
2255             "        })",
2256             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2257                    "          {  int   i;  int  j;   })",
2258                    getGoogleStyle()));
2259 
2260   EXPECT_EQ("#define A                                       \\\n"
2261             "  [] {                                          \\\n"
2262             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2263             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2264             "  }",
2265             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2266                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2267                    getGoogleStyle()));
2268 }
2269 
2270 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2271   EXPECT_EQ("{}", format("{}"));
2272   verifyFormat("enum E {};");
2273   verifyFormat("enum E {}");
2274 }
2275 
2276 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2277   FormatStyle Style = getLLVMStyle();
2278   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2279   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2280   verifyFormat("FOO_BEGIN\n"
2281                "  FOO_ENTRY\n"
2282                "FOO_END", Style);
2283   verifyFormat("FOO_BEGIN\n"
2284                "  NESTED_FOO_BEGIN\n"
2285                "    NESTED_FOO_ENTRY\n"
2286                "  NESTED_FOO_END\n"
2287                "FOO_END", Style);
2288   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2289                "  int x;\n"
2290                "  x = 1;\n"
2291                "FOO_END(Baz)", Style);
2292 }
2293 
2294 //===----------------------------------------------------------------------===//
2295 // Line break tests.
2296 //===----------------------------------------------------------------------===//
2297 
2298 TEST_F(FormatTest, PreventConfusingIndents) {
2299   verifyFormat(
2300       "void f() {\n"
2301       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2302       "                         parameter, parameter, parameter)),\n"
2303       "                     SecondLongCall(parameter));\n"
2304       "}");
2305   verifyFormat(
2306       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2307       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2308       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2309       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2310   verifyFormat(
2311       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2312       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2313       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2314       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2315   verifyFormat(
2316       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2317       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2318       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2319       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2320   verifyFormat("int a = bbbb && ccc &&\n"
2321                "        fffff(\n"
2322                "#define A Just forcing a new line\n"
2323                "            ddd);");
2324 }
2325 
2326 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2327   verifyFormat(
2328       "bool aaaaaaa =\n"
2329       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2330       "    bbbbbbbb();");
2331   verifyFormat(
2332       "bool aaaaaaa =\n"
2333       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2334       "    bbbbbbbb();");
2335 
2336   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2337                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2338                "    ccccccccc == ddddddddddd;");
2339   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2340                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2341                "    ccccccccc == ddddddddddd;");
2342   verifyFormat(
2343       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2344       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2345       "    ccccccccc == ddddddddddd;");
2346 
2347   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2348                "                 aaaaaa) &&\n"
2349                "         bbbbbb && cccccc;");
2350   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2351                "                 aaaaaa) >>\n"
2352                "         bbbbbb;");
2353   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2354                "    SourceMgr.getSpellingColumnNumber(\n"
2355                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2356                "    1);");
2357 
2358   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2359                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2360                "    cccccc) {\n}");
2361   verifyFormat("b = a &&\n"
2362                "    // Comment\n"
2363                "    b.c && d;");
2364 
2365   // If the LHS of a comparison is not a binary expression itself, the
2366   // additional linebreak confuses many people.
2367   verifyFormat(
2368       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2369       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2370       "}");
2371   verifyFormat(
2372       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2373       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2374       "}");
2375   verifyFormat(
2376       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2377       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2378       "}");
2379   // Even explicit parentheses stress the precedence enough to make the
2380   // additional break unnecessary.
2381   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2382                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2383                "}");
2384   // This cases is borderline, but with the indentation it is still readable.
2385   verifyFormat(
2386       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2387       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2388       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2389       "}",
2390       getLLVMStyleWithColumns(75));
2391 
2392   // If the LHS is a binary expression, we should still use the additional break
2393   // as otherwise the formatting hides the operator precedence.
2394   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2395                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2396                "    5) {\n"
2397                "}");
2398 
2399   FormatStyle OnePerLine = getLLVMStyle();
2400   OnePerLine.BinPackParameters = false;
2401   verifyFormat(
2402       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2403       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2404       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2405       OnePerLine);
2406 
2407   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2408                "                .aaa(aaaaaaaaaaaaa) *\n"
2409                "            aaaaaaa +\n"
2410                "        aaaaaaa;",
2411                getLLVMStyleWithColumns(40));
2412 }
2413 
2414 TEST_F(FormatTest, ExpressionIndentation) {
2415   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2416                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2417                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2418                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2419                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2420                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2421                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2422                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2423                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2424   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2425                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2426                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2427                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2428   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2429                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2430                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2431                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2432   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2433                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2434                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2435                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2436   verifyFormat("if () {\n"
2437                "} else if (aaaaa && bbbbb > // break\n"
2438                "                        ccccc) {\n"
2439                "}");
2440   verifyFormat("if () {\n"
2441                "} else if (aaaaa &&\n"
2442                "           bbbbb > // break\n"
2443                "               ccccc &&\n"
2444                "           ddddd) {\n"
2445                "}");
2446 
2447   // Presence of a trailing comment used to change indentation of b.
2448   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2449                "       b;\n"
2450                "return aaaaaaaaaaaaaaaaaaa +\n"
2451                "       b; //",
2452                getLLVMStyleWithColumns(30));
2453 }
2454 
2455 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2456   // Not sure what the best system is here. Like this, the LHS can be found
2457   // immediately above an operator (everything with the same or a higher
2458   // indent). The RHS is aligned right of the operator and so compasses
2459   // everything until something with the same indent as the operator is found.
2460   // FIXME: Is this a good system?
2461   FormatStyle Style = getLLVMStyle();
2462   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
2463   verifyFormat(
2464       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2465       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2466       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2467       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2468       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2469       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2470       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2471       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2472       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
2473       Style);
2474   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2475                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2476                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2477                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2478                Style);
2479   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2480                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2481                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2482                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2483                Style);
2484   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2485                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2486                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2487                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2488                Style);
2489   verifyFormat("if () {\n"
2490                "} else if (aaaaa\n"
2491                "           && bbbbb // break\n"
2492                "                  > ccccc) {\n"
2493                "}",
2494                Style);
2495   verifyFormat("return (a)\n"
2496                "       // comment\n"
2497                "       + b;",
2498                Style);
2499   verifyFormat(
2500       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2501       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2502       "             + cc;",
2503       Style);
2504 
2505   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2506                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
2507                Style);
2508 
2509   // Forced by comments.
2510   verifyFormat(
2511       "unsigned ContentSize =\n"
2512       "    sizeof(int16_t)   // DWARF ARange version number\n"
2513       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2514       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2515       "    + sizeof(int8_t); // Segment Size (in bytes)");
2516 
2517   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2518                "       == boost::fusion::at_c<1>(iiii).second;",
2519                Style);
2520 
2521   Style.ColumnLimit = 60;
2522   verifyFormat("zzzzzzzzzz\n"
2523                "    = bbbbbbbbbbbbbbbbb\n"
2524                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
2525                Style);
2526 }
2527 
2528 TEST_F(FormatTest, EnforcedOperatorWraps) {
2529   // Here we'd like to wrap after the || operators, but a comment is forcing an
2530   // earlier wrap.
2531   verifyFormat("bool x = aaaaa //\n"
2532                "         || bbbbb\n"
2533                "         //\n"
2534                "         || cccc;");
2535 }
2536 
2537 TEST_F(FormatTest, NoOperandAlignment) {
2538   FormatStyle Style = getLLVMStyle();
2539   Style.AlignOperands = false;
2540   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
2541                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2542                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2543                Style);
2544   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2545   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2546                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2547                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2548                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2549                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2550                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2551                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2552                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2553                "        > ccccccccccccccccccccccccccccccccccccccccc;",
2554                Style);
2555 
2556   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2557                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2558                "    + cc;",
2559                Style);
2560   verifyFormat("int a = aa\n"
2561                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2562                "        * cccccccccccccccccccccccccccccccccccc;\n",
2563                Style);
2564 
2565   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
2566   verifyFormat("return (a > b\n"
2567                "    // comment1\n"
2568                "    // comment2\n"
2569                "    || c);",
2570                Style);
2571 }
2572 
2573 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
2574   FormatStyle Style = getLLVMStyle();
2575   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2576   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2577                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2578                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2579                Style);
2580 }
2581 
2582 TEST_F(FormatTest, ConstructorInitializers) {
2583   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2584   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2585                getLLVMStyleWithColumns(45));
2586   verifyFormat("Constructor()\n"
2587                "    : Inttializer(FitsOnTheLine) {}",
2588                getLLVMStyleWithColumns(44));
2589   verifyFormat("Constructor()\n"
2590                "    : Inttializer(FitsOnTheLine) {}",
2591                getLLVMStyleWithColumns(43));
2592 
2593   verifyFormat("template <typename T>\n"
2594                "Constructor() : Initializer(FitsOnTheLine) {}",
2595                getLLVMStyleWithColumns(45));
2596 
2597   verifyFormat(
2598       "SomeClass::Constructor()\n"
2599       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2600 
2601   verifyFormat(
2602       "SomeClass::Constructor()\n"
2603       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2604       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2605   verifyFormat(
2606       "SomeClass::Constructor()\n"
2607       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2608       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2609   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2610                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2611                "    : aaaaaaaaaa(aaaaaa) {}");
2612 
2613   verifyFormat("Constructor()\n"
2614                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2615                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2616                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2617                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2618 
2619   verifyFormat("Constructor()\n"
2620                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2621                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2622 
2623   verifyFormat("Constructor(int Parameter = 0)\n"
2624                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2625                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2626   verifyFormat("Constructor()\n"
2627                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2628                "}",
2629                getLLVMStyleWithColumns(60));
2630   verifyFormat("Constructor()\n"
2631                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2632                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
2633 
2634   // Here a line could be saved by splitting the second initializer onto two
2635   // lines, but that is not desirable.
2636   verifyFormat("Constructor()\n"
2637                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2638                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2639                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2640 
2641   FormatStyle OnePerLine = getLLVMStyle();
2642   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2643   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2644   verifyFormat("SomeClass::Constructor()\n"
2645                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2646                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2647                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2648                OnePerLine);
2649   verifyFormat("SomeClass::Constructor()\n"
2650                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2651                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2652                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2653                OnePerLine);
2654   verifyFormat("MyClass::MyClass(int var)\n"
2655                "    : some_var_(var),            // 4 space indent\n"
2656                "      some_other_var_(var + 1) { // lined up\n"
2657                "}",
2658                OnePerLine);
2659   verifyFormat("Constructor()\n"
2660                "    : aaaaa(aaaaaa),\n"
2661                "      aaaaa(aaaaaa),\n"
2662                "      aaaaa(aaaaaa),\n"
2663                "      aaaaa(aaaaaa),\n"
2664                "      aaaaa(aaaaaa) {}",
2665                OnePerLine);
2666   verifyFormat("Constructor()\n"
2667                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2668                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2669                OnePerLine);
2670   OnePerLine.BinPackParameters = false;
2671   verifyFormat(
2672       "Constructor()\n"
2673       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2674       "          aaaaaaaaaaa().aaa(),\n"
2675       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2676       OnePerLine);
2677   OnePerLine.ColumnLimit = 60;
2678   verifyFormat("Constructor()\n"
2679                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
2680                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2681                OnePerLine);
2682 
2683   EXPECT_EQ("Constructor()\n"
2684             "    : // Comment forcing unwanted break.\n"
2685             "      aaaa(aaaa) {}",
2686             format("Constructor() :\n"
2687                    "    // Comment forcing unwanted break.\n"
2688                    "    aaaa(aaaa) {}"));
2689 }
2690 
2691 TEST_F(FormatTest, MemoizationTests) {
2692   // This breaks if the memoization lookup does not take \c Indent and
2693   // \c LastSpace into account.
2694   verifyFormat(
2695       "extern CFRunLoopTimerRef\n"
2696       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
2697       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
2698       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
2699       "                     CFRunLoopTimerContext *context) {}");
2700 
2701   // Deep nesting somewhat works around our memoization.
2702   verifyFormat(
2703       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2704       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2705       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2706       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2707       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
2708       getLLVMStyleWithColumns(65));
2709   verifyFormat(
2710       "aaaaa(\n"
2711       "    aaaaa,\n"
2712       "    aaaaa(\n"
2713       "        aaaaa,\n"
2714       "        aaaaa(\n"
2715       "            aaaaa,\n"
2716       "            aaaaa(\n"
2717       "                aaaaa,\n"
2718       "                aaaaa(\n"
2719       "                    aaaaa,\n"
2720       "                    aaaaa(\n"
2721       "                        aaaaa,\n"
2722       "                        aaaaa(\n"
2723       "                            aaaaa,\n"
2724       "                            aaaaa(\n"
2725       "                                aaaaa,\n"
2726       "                                aaaaa(\n"
2727       "                                    aaaaa,\n"
2728       "                                    aaaaa(\n"
2729       "                                        aaaaa,\n"
2730       "                                        aaaaa(\n"
2731       "                                            aaaaa,\n"
2732       "                                            aaaaa(\n"
2733       "                                                aaaaa,\n"
2734       "                                                aaaaa))))))))))));",
2735       getLLVMStyleWithColumns(65));
2736   verifyFormat(
2737       "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"
2738       "                                  a),\n"
2739       "                                a),\n"
2740       "                              a),\n"
2741       "                            a),\n"
2742       "                          a),\n"
2743       "                        a),\n"
2744       "                      a),\n"
2745       "                    a),\n"
2746       "                  a),\n"
2747       "                a),\n"
2748       "              a),\n"
2749       "            a),\n"
2750       "          a),\n"
2751       "        a),\n"
2752       "      a),\n"
2753       "    a),\n"
2754       "  a)",
2755       getLLVMStyleWithColumns(65));
2756 
2757   // This test takes VERY long when memoization is broken.
2758   FormatStyle OnePerLine = getLLVMStyle();
2759   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2760   OnePerLine.BinPackParameters = false;
2761   std::string input = "Constructor()\n"
2762                       "    : aaaa(a,\n";
2763   for (unsigned i = 0, e = 80; i != e; ++i) {
2764     input += "           a,\n";
2765   }
2766   input += "           a) {}";
2767   verifyFormat(input, OnePerLine);
2768 }
2769 
2770 TEST_F(FormatTest, BreaksAsHighAsPossible) {
2771   verifyFormat(
2772       "void f() {\n"
2773       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
2774       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
2775       "    f();\n"
2776       "}");
2777   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
2778                "    Intervals[i - 1].getRange().getLast()) {\n}");
2779 }
2780 
2781 TEST_F(FormatTest, BreaksFunctionDeclarations) {
2782   // Principially, we break function declarations in a certain order:
2783   // 1) break amongst arguments.
2784   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
2785                "                              Cccccccccccccc cccccccccccccc);");
2786   verifyFormat("template <class TemplateIt>\n"
2787                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
2788                "                            TemplateIt *stop) {}");
2789 
2790   // 2) break after return type.
2791   verifyFormat(
2792       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2793       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
2794       getGoogleStyle());
2795 
2796   // 3) break after (.
2797   verifyFormat(
2798       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
2799       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
2800       getGoogleStyle());
2801 
2802   // 4) break before after nested name specifiers.
2803   verifyFormat(
2804       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2805       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
2806       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
2807       getGoogleStyle());
2808 
2809   // However, there are exceptions, if a sufficient amount of lines can be
2810   // saved.
2811   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
2812   // more adjusting.
2813   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2814                "                                  Cccccccccccccc cccccccccc,\n"
2815                "                                  Cccccccccccccc cccccccccc,\n"
2816                "                                  Cccccccccccccc cccccccccc,\n"
2817                "                                  Cccccccccccccc cccccccccc);");
2818   verifyFormat(
2819       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2820       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2821       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2822       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
2823       getGoogleStyle());
2824   verifyFormat(
2825       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2826       "                                          Cccccccccccccc cccccccccc,\n"
2827       "                                          Cccccccccccccc cccccccccc,\n"
2828       "                                          Cccccccccccccc cccccccccc,\n"
2829       "                                          Cccccccccccccc cccccccccc,\n"
2830       "                                          Cccccccccccccc cccccccccc,\n"
2831       "                                          Cccccccccccccc cccccccccc);");
2832   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
2833                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2834                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2835                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2836                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
2837 
2838   // Break after multi-line parameters.
2839   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2840                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2841                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2842                "    bbbb bbbb);");
2843   verifyFormat("void SomeLoooooooooooongFunction(\n"
2844                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
2845                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2846                "    int bbbbbbbbbbbbb);");
2847 
2848   // Treat overloaded operators like other functions.
2849   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2850                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
2851   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2852                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
2853   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2854                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
2855   verifyGoogleFormat(
2856       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
2857       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
2858   verifyGoogleFormat(
2859       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
2860       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
2861   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2862                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
2863   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
2864                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
2865   verifyGoogleFormat(
2866       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
2867       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2868       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
2869   verifyGoogleFormat(
2870       "template <typename T>\n"
2871       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2872       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
2873       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
2874 
2875   FormatStyle Style = getLLVMStyle();
2876   Style.PointerAlignment = FormatStyle::PAS_Left;
2877   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2878                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
2879                Style);
2880   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
2881                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2882                Style);
2883 }
2884 
2885 TEST_F(FormatTest, TrailingReturnType) {
2886   verifyFormat("auto foo() -> int;\n");
2887   verifyFormat("struct S {\n"
2888                "  auto bar() const -> int;\n"
2889                "};");
2890   verifyFormat("template <size_t Order, typename T>\n"
2891                "auto load_img(const std::string &filename)\n"
2892                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
2893   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
2894                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
2895   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
2896   verifyFormat("template <typename T>\n"
2897                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
2898                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
2899 
2900   // Not trailing return types.
2901   verifyFormat("void f() { auto a = b->c(); }");
2902 }
2903 
2904 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
2905   // Avoid breaking before trailing 'const' or other trailing annotations, if
2906   // they are not function-like.
2907   FormatStyle Style = getGoogleStyle();
2908   Style.ColumnLimit = 47;
2909   verifyFormat("void someLongFunction(\n"
2910                "    int someLoooooooooooooongParameter) const {\n}",
2911                getLLVMStyleWithColumns(47));
2912   verifyFormat("LoooooongReturnType\n"
2913                "someLoooooooongFunction() const {}",
2914                getLLVMStyleWithColumns(47));
2915   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
2916                "    const {}",
2917                Style);
2918   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2919                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
2920   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2921                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
2922   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2923                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
2924   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
2925                "                   aaaaaaaaaaa aaaaa) const override;");
2926   verifyGoogleFormat(
2927       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
2928       "    const override;");
2929 
2930   // Even if the first parameter has to be wrapped.
2931   verifyFormat("void someLongFunction(\n"
2932                "    int someLongParameter) const {}",
2933                getLLVMStyleWithColumns(46));
2934   verifyFormat("void someLongFunction(\n"
2935                "    int someLongParameter) const {}",
2936                Style);
2937   verifyFormat("void someLongFunction(\n"
2938                "    int someLongParameter) override {}",
2939                Style);
2940   verifyFormat("void someLongFunction(\n"
2941                "    int someLongParameter) OVERRIDE {}",
2942                Style);
2943   verifyFormat("void someLongFunction(\n"
2944                "    int someLongParameter) final {}",
2945                Style);
2946   verifyFormat("void someLongFunction(\n"
2947                "    int someLongParameter) FINAL {}",
2948                Style);
2949   verifyFormat("void someLongFunction(\n"
2950                "    int parameter) const override {}",
2951                Style);
2952 
2953   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2954   verifyFormat("void someLongFunction(\n"
2955                "    int someLongParameter) const\n"
2956                "{\n"
2957                "}",
2958                Style);
2959 
2960   // Unless these are unknown annotations.
2961   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
2962                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2963                "    LONG_AND_UGLY_ANNOTATION;");
2964 
2965   // Breaking before function-like trailing annotations is fine to keep them
2966   // close to their arguments.
2967   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2968                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2969   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2970                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2971   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2972                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
2973   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
2974                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
2975   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
2976 
2977   verifyFormat(
2978       "void aaaaaaaaaaaaaaaaaa()\n"
2979       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
2980       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
2981   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2982                "    __attribute__((unused));");
2983   verifyGoogleFormat(
2984       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2985       "    GUARDED_BY(aaaaaaaaaaaa);");
2986   verifyGoogleFormat(
2987       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2988       "    GUARDED_BY(aaaaaaaaaaaa);");
2989   verifyGoogleFormat(
2990       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
2991       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
2992   verifyGoogleFormat(
2993       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
2994       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
2995 }
2996 
2997 TEST_F(FormatTest, FunctionAnnotations) {
2998   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
2999                "int OldFunction(const string &parameter) {}");
3000   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3001                "string OldFunction(const string &parameter) {}");
3002   verifyFormat("template <typename T>\n"
3003                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3004                "string OldFunction(const string &parameter) {}");
3005 
3006   // Not function annotations.
3007   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3008                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3009   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3010                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3011   verifyFormat("MACRO(abc).function() // wrap\n"
3012                "    << abc;");
3013   verifyFormat("MACRO(abc)->function() // wrap\n"
3014                "    << abc;");
3015   verifyFormat("MACRO(abc)::function() // wrap\n"
3016                "    << abc;");
3017 }
3018 
3019 TEST_F(FormatTest, BreaksDesireably) {
3020   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3021                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3022                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3023   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3024                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3025                "}");
3026 
3027   verifyFormat(
3028       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3029       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3030 
3031   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3032                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3033                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3034 
3035   verifyFormat(
3036       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3037       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3038       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3039       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3040       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3041 
3042   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3043                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3044 
3045   verifyFormat(
3046       "void f() {\n"
3047       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3048       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3049       "}");
3050   verifyFormat(
3051       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3052       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3053   verifyFormat(
3054       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3055       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3056   verifyFormat(
3057       "aaaaaa(aaa,\n"
3058       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3059       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3060       "       aaaa);");
3061   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3062                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3063                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3064 
3065   // Indent consistently independent of call expression and unary operator.
3066   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3067                "    dddddddddddddddddddddddddddddd));");
3068   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3069                "    dddddddddddddddddddddddddddddd));");
3070   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3071                "    dddddddddddddddddddddddddddddd));");
3072 
3073   // This test case breaks on an incorrect memoization, i.e. an optimization not
3074   // taking into account the StopAt value.
3075   verifyFormat(
3076       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3077       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3078       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3079       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3080 
3081   verifyFormat("{\n  {\n    {\n"
3082                "      Annotation.SpaceRequiredBefore =\n"
3083                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3084                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3085                "    }\n  }\n}");
3086 
3087   // Break on an outer level if there was a break on an inner level.
3088   EXPECT_EQ("f(g(h(a, // comment\n"
3089             "      b, c),\n"
3090             "    d, e),\n"
3091             "  x, y);",
3092             format("f(g(h(a, // comment\n"
3093                    "    b, c), d, e), x, y);"));
3094 
3095   // Prefer breaking similar line breaks.
3096   verifyFormat(
3097       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3098       "                             NSTrackingMouseEnteredAndExited |\n"
3099       "                             NSTrackingActiveAlways;");
3100 }
3101 
3102 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3103   FormatStyle NoBinPacking = getGoogleStyle();
3104   NoBinPacking.BinPackParameters = false;
3105   NoBinPacking.BinPackArguments = true;
3106   verifyFormat("void f() {\n"
3107                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3108                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3109                "}",
3110                NoBinPacking);
3111   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3112                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3113                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3114                NoBinPacking);
3115 
3116   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3117   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3118                "                        vector<int> bbbbbbbbbbbbbbb);",
3119                NoBinPacking);
3120   // FIXME: This behavior difference is probably not wanted. However, currently
3121   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3122   // template arguments from BreakBeforeParameter being set because of the
3123   // one-per-line formatting.
3124   verifyFormat(
3125       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3126       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3127       NoBinPacking);
3128   verifyFormat(
3129       "void fffffffffff(\n"
3130       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3131       "        aaaaaaaaaa);");
3132 }
3133 
3134 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3135   FormatStyle NoBinPacking = getGoogleStyle();
3136   NoBinPacking.BinPackParameters = false;
3137   NoBinPacking.BinPackArguments = false;
3138   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3139                "  aaaaaaaaaaaaaaaaaaaa,\n"
3140                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3141                NoBinPacking);
3142   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3143                "        aaaaaaaaaaaaa,\n"
3144                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3145                NoBinPacking);
3146   verifyFormat(
3147       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3148       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3149       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3150       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3151       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3152       NoBinPacking);
3153   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3154                "    .aaaaaaaaaaaaaaaaaa();",
3155                NoBinPacking);
3156   verifyFormat("void f() {\n"
3157                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3158                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3159                "}",
3160                NoBinPacking);
3161 
3162   verifyFormat(
3163       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3164       "             aaaaaaaaaaaa,\n"
3165       "             aaaaaaaaaaaa);",
3166       NoBinPacking);
3167   verifyFormat(
3168       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3169       "                               ddddddddddddddddddddddddddddd),\n"
3170       "             test);",
3171       NoBinPacking);
3172 
3173   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3174                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3175                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3176                "    aaaaaaaaaaaaaaaaaa;",
3177                NoBinPacking);
3178   verifyFormat("a(\"a\"\n"
3179                "  \"a\",\n"
3180                "  a);");
3181 
3182   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3183   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3184                "                aaaaaaaaa,\n"
3185                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3186                NoBinPacking);
3187   verifyFormat(
3188       "void f() {\n"
3189       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3190       "      .aaaaaaa();\n"
3191       "}",
3192       NoBinPacking);
3193   verifyFormat(
3194       "template <class SomeType, class SomeOtherType>\n"
3195       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3196       NoBinPacking);
3197 }
3198 
3199 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3200   FormatStyle Style = getLLVMStyleWithColumns(15);
3201   Style.ExperimentalAutoDetectBinPacking = true;
3202   EXPECT_EQ("aaa(aaaa,\n"
3203             "    aaaa,\n"
3204             "    aaaa);\n"
3205             "aaa(aaaa,\n"
3206             "    aaaa,\n"
3207             "    aaaa);",
3208             format("aaa(aaaa,\n" // one-per-line
3209                    "  aaaa,\n"
3210                    "    aaaa  );\n"
3211                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3212                    Style));
3213   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3214             "    aaaa);\n"
3215             "aaa(aaaa, aaaa,\n"
3216             "    aaaa);",
3217             format("aaa(aaaa,  aaaa,\n" // bin-packed
3218                    "    aaaa  );\n"
3219                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3220                    Style));
3221 }
3222 
3223 TEST_F(FormatTest, FormatsBuilderPattern) {
3224   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3225                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3226                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3227                "    .StartsWith(\".init\", ORDER_INIT)\n"
3228                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3229                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3230                "    .Default(ORDER_TEXT);\n");
3231 
3232   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3233                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3234   verifyFormat(
3235       "aaaaaaa->aaaaaaa\n"
3236       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3237       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3238       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3239   verifyFormat(
3240       "aaaaaaa->aaaaaaa\n"
3241       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3242       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3243   verifyFormat(
3244       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3245       "    aaaaaaaaaaaaaa);");
3246   verifyFormat(
3247       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3248       "    aaaaaa->aaaaaaaaaaaa()\n"
3249       "        ->aaaaaaaaaaaaaaaa(\n"
3250       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3251       "        ->aaaaaaaaaaaaaaaaa();");
3252   verifyGoogleFormat(
3253       "void f() {\n"
3254       "  someo->Add((new util::filetools::Handler(dir))\n"
3255       "                 ->OnEvent1(NewPermanentCallback(\n"
3256       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3257       "                 ->OnEvent2(NewPermanentCallback(\n"
3258       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3259       "                 ->OnEvent3(NewPermanentCallback(\n"
3260       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3261       "                 ->OnEvent5(NewPermanentCallback(\n"
3262       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3263       "                 ->OnEvent6(NewPermanentCallback(\n"
3264       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3265       "}");
3266 
3267   verifyFormat(
3268       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3269   verifyFormat("aaaaaaaaaaaaaaa()\n"
3270                "    .aaaaaaaaaaaaaaa()\n"
3271                "    .aaaaaaaaaaaaaaa()\n"
3272                "    .aaaaaaaaaaaaaaa()\n"
3273                "    .aaaaaaaaaaaaaaa();");
3274   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3275                "    .aaaaaaaaaaaaaaa()\n"
3276                "    .aaaaaaaaaaaaaaa()\n"
3277                "    .aaaaaaaaaaaaaaa();");
3278   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3279                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3280                "    .aaaaaaaaaaaaaaa();");
3281   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3282                "    ->aaaaaaaaaaaaaae(0)\n"
3283                "    ->aaaaaaaaaaaaaaa();");
3284 
3285   // Don't linewrap after very short segments.
3286   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3287                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3288                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3289   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3290                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3291                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3292   verifyFormat("aaa()\n"
3293                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3294                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3295                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3296 
3297   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3298                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3299                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3300   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3301                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3302                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3303 
3304   // Prefer not to break after empty parentheses.
3305   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3306                "    First->LastNewlineOffset);");
3307 
3308   // Prefer not to create "hanging" indents.
3309   verifyFormat(
3310       "return !soooooooooooooome_map\n"
3311       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3312       "            .second;");
3313   verifyFormat(
3314       "return aaaaaaaaaaaaaaaa\n"
3315       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
3316       "    .aaaa(aaaaaaaaaaaaaa);");
3317   // No hanging indent here.
3318   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
3319                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3320   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
3321                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3322   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3323                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3324                getLLVMStyleWithColumns(60));
3325   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
3326                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3327                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3328                getLLVMStyleWithColumns(59));
3329   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3330                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3331                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3332 }
3333 
3334 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3335   verifyFormat(
3336       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3337       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3338   verifyFormat(
3339       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3340       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3341 
3342   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3343                "    ccccccccccccccccccccccccc) {\n}");
3344   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3345                "    ccccccccccccccccccccccccc) {\n}");
3346 
3347   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3348                "    ccccccccccccccccccccccccc) {\n}");
3349   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3350                "    ccccccccccccccccccccccccc) {\n}");
3351 
3352   verifyFormat(
3353       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3354       "    ccccccccccccccccccccccccc) {\n}");
3355   verifyFormat(
3356       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3357       "    ccccccccccccccccccccccccc) {\n}");
3358 
3359   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3360                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3361                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3362                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3363   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3364                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3365                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3366                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3367 
3368   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3369                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3370                "    aaaaaaaaaaaaaaa != aa) {\n}");
3371   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3372                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3373                "    aaaaaaaaaaaaaaa != aa) {\n}");
3374 }
3375 
3376 TEST_F(FormatTest, BreaksAfterAssignments) {
3377   verifyFormat(
3378       "unsigned Cost =\n"
3379       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3380       "                        SI->getPointerAddressSpaceee());\n");
3381   verifyFormat(
3382       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3383       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3384 
3385   verifyFormat(
3386       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3387       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3388   verifyFormat("unsigned OriginalStartColumn =\n"
3389                "    SourceMgr.getSpellingColumnNumber(\n"
3390                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3391                "    1;");
3392 }
3393 
3394 TEST_F(FormatTest, AlignsAfterAssignments) {
3395   verifyFormat(
3396       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3397       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3398   verifyFormat(
3399       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3400       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3401   verifyFormat(
3402       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3403       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3404   verifyFormat(
3405       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3406       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3407   verifyFormat(
3408       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3409       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3410       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3411 }
3412 
3413 TEST_F(FormatTest, AlignsAfterReturn) {
3414   verifyFormat(
3415       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3416       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3417   verifyFormat(
3418       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3419       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3420   verifyFormat(
3421       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3422       "       aaaaaaaaaaaaaaaaaaaaaa();");
3423   verifyFormat(
3424       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3425       "        aaaaaaaaaaaaaaaaaaaaaa());");
3426   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3427                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3428   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3429                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3430                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3431   verifyFormat("return\n"
3432                "    // true if code is one of a or b.\n"
3433                "    code == a || code == b;");
3434 }
3435 
3436 TEST_F(FormatTest, AlignsAfterOpenBracket) {
3437   verifyFormat(
3438       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3439       "                                                aaaaaaaaa aaaaaaa) {}");
3440   verifyFormat(
3441       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3442       "                                               aaaaaaaaaaa aaaaaaaaa);");
3443   verifyFormat(
3444       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3445       "                                             aaaaaaaaaaaaaaaaaaaaa));");
3446   FormatStyle Style = getLLVMStyle();
3447   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3448   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3449                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
3450                Style);
3451   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3452                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
3453                Style);
3454   verifyFormat("SomeLongVariableName->someFunction(\n"
3455                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
3456                Style);
3457   verifyFormat(
3458       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3459       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3460       Style);
3461   verifyFormat(
3462       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3463       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3464       Style);
3465   verifyFormat(
3466       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3467       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3468       Style);
3469 
3470   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
3471                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
3472                "        b));",
3473                Style);
3474 
3475   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
3476   Style.BinPackArguments = false;
3477   Style.BinPackParameters = false;
3478   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3479                "    aaaaaaaaaaa aaaaaaaa,\n"
3480                "    aaaaaaaaa aaaaaaa,\n"
3481                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3482                Style);
3483   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3484                "    aaaaaaaaaaa aaaaaaaaa,\n"
3485                "    aaaaaaaaaaa aaaaaaaaa,\n"
3486                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3487                Style);
3488   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
3489                "    aaaaaaaaaaaaaaa,\n"
3490                "    aaaaaaaaaaaaaaaaaaaaa,\n"
3491                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3492                Style);
3493   verifyFormat(
3494       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
3495       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3496       Style);
3497   verifyFormat(
3498       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
3499       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3500       Style);
3501   verifyFormat(
3502       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3503       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3504       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
3505       "    aaaaaaaaaaaaaaaa);",
3506       Style);
3507   verifyFormat(
3508       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3509       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3510       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
3511       "    aaaaaaaaaaaaaaaa);",
3512       Style);
3513 }
3514 
3515 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
3516   FormatStyle Style = getLLVMStyleWithColumns(40);
3517   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3518                "          bbbbbbbbbbbbbbbbbbbbbb);",
3519                Style);
3520   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
3521   Style.AlignOperands = false;
3522   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3523                "          bbbbbbbbbbbbbbbbbbbbbb);",
3524                Style);
3525   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3526   Style.AlignOperands = true;
3527   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3528                "          bbbbbbbbbbbbbbbbbbbbbb);",
3529                Style);
3530   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3531   Style.AlignOperands = false;
3532   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3533                "    bbbbbbbbbbbbbbbbbbbbbb);",
3534                Style);
3535 }
3536 
3537 TEST_F(FormatTest, BreaksConditionalExpressions) {
3538   verifyFormat(
3539       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3540       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3541       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3542   verifyFormat(
3543       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3544       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3545       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3546   verifyFormat(
3547       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3548       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3549   verifyFormat(
3550       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
3551       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3552       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3553   verifyFormat(
3554       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
3555       "                                                    : aaaaaaaaaaaaa);");
3556   verifyFormat(
3557       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3558       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3559       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3560       "                   aaaaaaaaaaaaa);");
3561   verifyFormat(
3562       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3563       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3564       "                   aaaaaaaaaaaaa);");
3565   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3566                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3567                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3568                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3569                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3570   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3571                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3572                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3573                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3574                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3575                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3576                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3577   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3578                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3579                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3580                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3581                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3582   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3583                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3584                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3585   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3586                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3587                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3588                "        : aaaaaaaaaaaaaaaa;");
3589   verifyFormat(
3590       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3591       "    ? aaaaaaaaaaaaaaa\n"
3592       "    : aaaaaaaaaaaaaaa;");
3593   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3594                "          aaaaaaaaa\n"
3595                "      ? b\n"
3596                "      : c);");
3597   verifyFormat("return aaaa == bbbb\n"
3598                "           // comment\n"
3599                "           ? aaaa\n"
3600                "           : bbbb;");
3601   verifyFormat("unsigned Indent =\n"
3602                "    format(TheLine.First,\n"
3603                "           IndentForLevel[TheLine.Level] >= 0\n"
3604                "               ? IndentForLevel[TheLine.Level]\n"
3605                "               : TheLine * 2,\n"
3606                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3607                getLLVMStyleWithColumns(60));
3608   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3609                "                  ? aaaaaaaaaaaaaaa\n"
3610                "                  : bbbbbbbbbbbbbbb //\n"
3611                "                        ? ccccccccccccccc\n"
3612                "                        : ddddddddddddddd;");
3613   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3614                "                  ? aaaaaaaaaaaaaaa\n"
3615                "                  : (bbbbbbbbbbbbbbb //\n"
3616                "                         ? ccccccccccccccc\n"
3617                "                         : ddddddddddddddd);");
3618   verifyFormat(
3619       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3620       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3621       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
3622       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
3623       "                                      : aaaaaaaaaa;");
3624   verifyFormat(
3625       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3626       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
3627       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3628 
3629   FormatStyle NoBinPacking = getLLVMStyle();
3630   NoBinPacking.BinPackArguments = false;
3631   verifyFormat(
3632       "void f() {\n"
3633       "  g(aaa,\n"
3634       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3635       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3636       "        ? aaaaaaaaaaaaaaa\n"
3637       "        : aaaaaaaaaaaaaaa);\n"
3638       "}",
3639       NoBinPacking);
3640   verifyFormat(
3641       "void f() {\n"
3642       "  g(aaa,\n"
3643       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3644       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3645       "        ?: aaaaaaaaaaaaaaa);\n"
3646       "}",
3647       NoBinPacking);
3648 
3649   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
3650                "             // comment.\n"
3651                "             ccccccccccccccccccccccccccccccccccccccc\n"
3652                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3653                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
3654 
3655   // Assignments in conditional expressions. Apparently not uncommon :-(.
3656   verifyFormat("return a != b\n"
3657                "           // comment\n"
3658                "           ? a = b\n"
3659                "           : a = b;");
3660   verifyFormat("return a != b\n"
3661                "           // comment\n"
3662                "           ? a = a != b\n"
3663                "                     // comment\n"
3664                "                     ? a = b\n"
3665                "                     : a\n"
3666                "           : a;\n");
3667   verifyFormat("return a != b\n"
3668                "           // comment\n"
3669                "           ? a\n"
3670                "           : a = a != b\n"
3671                "                     // comment\n"
3672                "                     ? a = b\n"
3673                "                     : a;");
3674 }
3675 
3676 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
3677   FormatStyle Style = getLLVMStyle();
3678   Style.BreakBeforeTernaryOperators = false;
3679   Style.ColumnLimit = 70;
3680   verifyFormat(
3681       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3682       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3683       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3684       Style);
3685   verifyFormat(
3686       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3687       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3688       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3689       Style);
3690   verifyFormat(
3691       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3692       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3693       Style);
3694   verifyFormat(
3695       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
3696       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3697       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3698       Style);
3699   verifyFormat(
3700       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
3701       "                                                      aaaaaaaaaaaaa);",
3702       Style);
3703   verifyFormat(
3704       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3705       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3706       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3707       "                   aaaaaaaaaaaaa);",
3708       Style);
3709   verifyFormat(
3710       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3711       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3712       "                   aaaaaaaaaaaaa);",
3713       Style);
3714   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3715                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3716                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3717                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3718                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3719                Style);
3720   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3721                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3722                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3723                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3724                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3725                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3726                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3727                Style);
3728   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3729                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
3730                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3731                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3732                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3733                Style);
3734   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3735                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3736                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3737                Style);
3738   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3739                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3740                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3741                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3742                Style);
3743   verifyFormat(
3744       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3745       "    aaaaaaaaaaaaaaa :\n"
3746       "    aaaaaaaaaaaaaaa;",
3747       Style);
3748   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3749                "          aaaaaaaaa ?\n"
3750                "      b :\n"
3751                "      c);",
3752                Style);
3753   verifyFormat("unsigned Indent =\n"
3754                "    format(TheLine.First,\n"
3755                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
3756                "               IndentForLevel[TheLine.Level] :\n"
3757                "               TheLine * 2,\n"
3758                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3759                Style);
3760   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3761                "                  aaaaaaaaaaaaaaa :\n"
3762                "                  bbbbbbbbbbbbbbb ? //\n"
3763                "                      ccccccccccccccc :\n"
3764                "                      ddddddddddddddd;",
3765                Style);
3766   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3767                "                  aaaaaaaaaaaaaaa :\n"
3768                "                  (bbbbbbbbbbbbbbb ? //\n"
3769                "                       ccccccccccccccc :\n"
3770                "                       ddddddddddddddd);",
3771                Style);
3772   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3773                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
3774                "            ccccccccccccccccccccccccccc;",
3775                Style);
3776   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3777                "           aaaaa :\n"
3778                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
3779                Style);
3780 }
3781 
3782 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
3783   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
3784                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
3785   verifyFormat("bool a = true, b = false;");
3786 
3787   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3788                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
3789                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
3790                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
3791   verifyFormat(
3792       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3793       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
3794       "     d = e && f;");
3795   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
3796                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
3797   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3798                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
3799   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
3800                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
3801 
3802   FormatStyle Style = getGoogleStyle();
3803   Style.PointerAlignment = FormatStyle::PAS_Left;
3804   Style.DerivePointerAlignment = false;
3805   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3806                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
3807                "    *b = bbbbbbbbbbbbbbbbbbb;",
3808                Style);
3809   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3810                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
3811                Style);
3812   verifyFormat("vector<int*> a, b;", Style);
3813   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
3814 }
3815 
3816 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
3817   verifyFormat("arr[foo ? bar : baz];");
3818   verifyFormat("f()[foo ? bar : baz];");
3819   verifyFormat("(a + b)[foo ? bar : baz];");
3820   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
3821 }
3822 
3823 TEST_F(FormatTest, AlignsStringLiterals) {
3824   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
3825                "                                      \"short literal\");");
3826   verifyFormat(
3827       "looooooooooooooooooooooooongFunction(\n"
3828       "    \"short literal\"\n"
3829       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
3830   verifyFormat("someFunction(\"Always break between multi-line\"\n"
3831                "             \" string literals\",\n"
3832                "             and, other, parameters);");
3833   EXPECT_EQ("fun + \"1243\" /* comment */\n"
3834             "      \"5678\";",
3835             format("fun + \"1243\" /* comment */\n"
3836                    "    \"5678\";",
3837                    getLLVMStyleWithColumns(28)));
3838   EXPECT_EQ(
3839       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
3840       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
3841       "         \"aaaaaaaaaaaaaaaa\";",
3842       format("aaaaaa ="
3843              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
3844              "aaaaaaaaaaaaaaaaaaaaa\" "
3845              "\"aaaaaaaaaaaaaaaa\";"));
3846   verifyFormat("a = a + \"a\"\n"
3847                "        \"a\"\n"
3848                "        \"a\";");
3849   verifyFormat("f(\"a\", \"b\"\n"
3850                "       \"c\");");
3851 
3852   verifyFormat(
3853       "#define LL_FORMAT \"ll\"\n"
3854       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
3855       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
3856 
3857   verifyFormat("#define A(X)          \\\n"
3858                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
3859                "  \"ccccc\"",
3860                getLLVMStyleWithColumns(23));
3861   verifyFormat("#define A \"def\"\n"
3862                "f(\"abc\" A \"ghi\"\n"
3863                "  \"jkl\");");
3864 
3865   verifyFormat("f(L\"a\"\n"
3866                "  L\"b\");");
3867   verifyFormat("#define A(X)            \\\n"
3868                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
3869                "  L\"ccccc\"",
3870                getLLVMStyleWithColumns(25));
3871 
3872   verifyFormat("f(@\"a\"\n"
3873                "  @\"b\");");
3874   verifyFormat("NSString s = @\"a\"\n"
3875                "             @\"b\"\n"
3876                "             @\"c\";");
3877   verifyFormat("NSString s = @\"a\"\n"
3878                "              \"b\"\n"
3879                "              \"c\";");
3880 }
3881 
3882 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
3883   FormatStyle Style = getLLVMStyle();
3884   // No declarations or definitions should be moved to own line.
3885   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
3886   verifyFormat("class A {\n"
3887                "  int f() { return 1; }\n"
3888                "  int g();\n"
3889                "};\n"
3890                "int f() { return 1; }\n"
3891                "int g();\n",
3892                Style);
3893 
3894   // All declarations and definitions should have the return type moved to its
3895   // own
3896   // line.
3897   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
3898   verifyFormat("class E {\n"
3899                "  int\n"
3900                "  f() {\n"
3901                "    return 1;\n"
3902                "  }\n"
3903                "  int\n"
3904                "  g();\n"
3905                "};\n"
3906                "int\n"
3907                "f() {\n"
3908                "  return 1;\n"
3909                "}\n"
3910                "int\n"
3911                "g();\n",
3912                Style);
3913 
3914   // Top-level definitions, and no kinds of declarations should have the
3915   // return type moved to its own line.
3916   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
3917   verifyFormat("class B {\n"
3918                "  int f() { return 1; }\n"
3919                "  int g();\n"
3920                "};\n"
3921                "int\n"
3922                "f() {\n"
3923                "  return 1;\n"
3924                "}\n"
3925                "int g();\n",
3926                Style);
3927 
3928   // Top-level definitions and declarations should have the return type moved
3929   // to its own line.
3930   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
3931   verifyFormat("class C {\n"
3932                "  int f() { return 1; }\n"
3933                "  int g();\n"
3934                "};\n"
3935                "int\n"
3936                "f() {\n"
3937                "  return 1;\n"
3938                "}\n"
3939                "int\n"
3940                "g();\n",
3941                Style);
3942 
3943   // All definitions should have the return type moved to its own line, but no
3944   // kinds of declarations.
3945   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
3946   verifyFormat("class D {\n"
3947                "  int\n"
3948                "  f() {\n"
3949                "    return 1;\n"
3950                "  }\n"
3951                "  int g();\n"
3952                "};\n"
3953                "int\n"
3954                "f() {\n"
3955                "  return 1;\n"
3956                "}\n"
3957                "int g();\n",
3958                Style);
3959   verifyFormat("const char *\n"
3960                "f(void) {\n" // Break here.
3961                "  return \"\";\n"
3962                "}\n"
3963                "const char *bar(void);\n", // No break here.
3964                Style);
3965   verifyFormat("template <class T>\n"
3966                "T *\n"
3967                "f(T &c) {\n" // Break here.
3968                "  return NULL;\n"
3969                "}\n"
3970                "template <class T> T *f(T &c);\n", // No break here.
3971                Style);
3972   verifyFormat("class C {\n"
3973                "  int\n"
3974                "  operator+() {\n"
3975                "    return 1;\n"
3976                "  }\n"
3977                "  int\n"
3978                "  operator()() {\n"
3979                "    return 1;\n"
3980                "  }\n"
3981                "};\n",
3982                Style);
3983   verifyFormat("void\n"
3984                "A::operator()() {}\n"
3985                "void\n"
3986                "A::operator>>() {}\n"
3987                "void\n"
3988                "A::operator+() {}\n",
3989                Style);
3990   verifyFormat("void *operator new(std::size_t s);", // No break here.
3991                Style);
3992   verifyFormat("void *\n"
3993                "operator new(std::size_t s) {}",
3994                Style);
3995   verifyFormat("void *\n"
3996                "operator delete[](void *ptr) {}",
3997                Style);
3998   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
3999   verifyFormat("const char *\n"
4000                "f(void)\n" // Break here.
4001                "{\n"
4002                "  return \"\";\n"
4003                "}\n"
4004                "const char *bar(void);\n", // No break here.
4005                Style);
4006   verifyFormat("template <class T>\n"
4007                "T *\n"     // Problem here: no line break
4008                "f(T &c)\n" // Break here.
4009                "{\n"
4010                "  return NULL;\n"
4011                "}\n"
4012                "template <class T> T *f(T &c);\n", // No break here.
4013                Style);
4014 }
4015 
4016 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4017   FormatStyle NoBreak = getLLVMStyle();
4018   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4019   FormatStyle Break = getLLVMStyle();
4020   Break.AlwaysBreakBeforeMultilineStrings = true;
4021   verifyFormat("aaaa = \"bbbb\"\n"
4022                "       \"cccc\";",
4023                NoBreak);
4024   verifyFormat("aaaa =\n"
4025                "    \"bbbb\"\n"
4026                "    \"cccc\";",
4027                Break);
4028   verifyFormat("aaaa(\"bbbb\"\n"
4029                "     \"cccc\");",
4030                NoBreak);
4031   verifyFormat("aaaa(\n"
4032                "    \"bbbb\"\n"
4033                "    \"cccc\");",
4034                Break);
4035   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4036                "          \"cccc\");",
4037                NoBreak);
4038   verifyFormat("aaaa(qqq,\n"
4039                "     \"bbbb\"\n"
4040                "     \"cccc\");",
4041                Break);
4042   verifyFormat("aaaa(qqq,\n"
4043                "     L\"bbbb\"\n"
4044                "     L\"cccc\");",
4045                Break);
4046   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4047                "                      \"bbbb\"));",
4048                Break);
4049   verifyFormat("string s = someFunction(\n"
4050                "    \"abc\"\n"
4051                "    \"abc\");",
4052                Break);
4053 
4054   // As we break before unary operators, breaking right after them is bad.
4055   verifyFormat("string foo = abc ? \"x\"\n"
4056                "                   \"blah blah blah blah blah blah\"\n"
4057                "                 : \"y\";",
4058                Break);
4059 
4060   // Don't break if there is no column gain.
4061   verifyFormat("f(\"aaaa\"\n"
4062                "  \"bbbb\");",
4063                Break);
4064 
4065   // Treat literals with escaped newlines like multi-line string literals.
4066   EXPECT_EQ("x = \"a\\\n"
4067             "b\\\n"
4068             "c\";",
4069             format("x = \"a\\\n"
4070                    "b\\\n"
4071                    "c\";",
4072                    NoBreak));
4073   EXPECT_EQ("xxxx =\n"
4074             "    \"a\\\n"
4075             "b\\\n"
4076             "c\";",
4077             format("xxxx = \"a\\\n"
4078                    "b\\\n"
4079                    "c\";",
4080                    Break));
4081 
4082   // Exempt ObjC strings for now.
4083   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4084             "                          @\"bbbb\";",
4085             format("NSString *const kString = @\"aaaa\"\n"
4086                    "@\"bbbb\";",
4087                    Break));
4088 
4089   Break.ColumnLimit = 0;
4090   verifyFormat("const char *hello = \"hello llvm\";", Break);
4091 }
4092 
4093 TEST_F(FormatTest, AlignsPipes) {
4094   verifyFormat(
4095       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4096       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4097       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4098   verifyFormat(
4099       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4100       "                     << aaaaaaaaaaaaaaaaaaaa;");
4101   verifyFormat(
4102       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4103       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4104   verifyFormat(
4105       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4106       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4107   verifyFormat(
4108       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4109       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4110       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4111   verifyFormat(
4112       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4113       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4114       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4115   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4116                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4117                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4118                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4119   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4120                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4121   verifyFormat(
4122       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4123       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4124 
4125   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4126                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4127   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4128                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4129                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4130                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4131   verifyFormat("LOG_IF(aaa == //\n"
4132                "       bbb)\n"
4133                "    << a << b;");
4134 
4135   // But sometimes, breaking before the first "<<" is desirable.
4136   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4137                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4138   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4139                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4140                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4141   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4142                "    << BEF << IsTemplate << Description << E->getType();");
4143   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4144                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4145                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4146   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4147                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4148                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4149                "    << aaa;");
4150 
4151   verifyFormat(
4152       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4153       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4154 
4155   // Incomplete string literal.
4156   EXPECT_EQ("llvm::errs() << \"\n"
4157             "             << a;",
4158             format("llvm::errs() << \"\n<<a;"));
4159 
4160   verifyFormat("void f() {\n"
4161                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4162                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4163                "}");
4164 
4165   // Handle 'endl'.
4166   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4167                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4168   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4169 
4170   // Handle '\n'.
4171   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4172                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4173   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4174                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4175   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4176                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4177   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4178 }
4179 
4180 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4181   verifyFormat("return out << \"somepacket = {\\n\"\n"
4182                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4183                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4184                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4185                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4186                "           << \"}\";");
4187 
4188   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4189                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4190                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4191   verifyFormat(
4192       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4193       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4194       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4195       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4196       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4197   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4198                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4199   verifyFormat(
4200       "void f() {\n"
4201       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4202       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4203       "}");
4204 
4205   // Breaking before the first "<<" is generally not desirable.
4206   verifyFormat(
4207       "llvm::errs()\n"
4208       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4209       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4210       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4211       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4212       getLLVMStyleWithColumns(70));
4213   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4214                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4215                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4216                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4217                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4218                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4219                getLLVMStyleWithColumns(70));
4220 
4221   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4222                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4223                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4224   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4225                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4226                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4227   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4228                "           (aaaa + aaaa);",
4229                getLLVMStyleWithColumns(40));
4230   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4231                "                  (aaaaaaa + aaaaa));",
4232                getLLVMStyleWithColumns(40));
4233   verifyFormat(
4234       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
4235       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
4236       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
4237 }
4238 
4239 TEST_F(FormatTest, UnderstandsEquals) {
4240   verifyFormat(
4241       "aaaaaaaaaaaaaaaaa =\n"
4242       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4243   verifyFormat(
4244       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4245       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4246   verifyFormat(
4247       "if (a) {\n"
4248       "  f();\n"
4249       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4250       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4251       "}");
4252 
4253   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4254                "        100000000 + 10000000) {\n}");
4255 }
4256 
4257 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4258   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4259                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4260 
4261   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4262                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4263 
4264   verifyFormat(
4265       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4266       "                                                          Parameter2);");
4267 
4268   verifyFormat(
4269       "ShortObject->shortFunction(\n"
4270       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4271       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4272 
4273   verifyFormat("loooooooooooooongFunction(\n"
4274                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4275 
4276   verifyFormat(
4277       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4278       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4279 
4280   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4281                "    .WillRepeatedly(Return(SomeValue));");
4282   verifyFormat("void f() {\n"
4283                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4284                "      .Times(2)\n"
4285                "      .WillRepeatedly(Return(SomeValue));\n"
4286                "}");
4287   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4288                "    ccccccccccccccccccccccc);");
4289   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4290                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4291                "          .aaaaa(aaaaa),\n"
4292                "      aaaaaaaaaaaaaaaaaaaaa);");
4293   verifyFormat("void f() {\n"
4294                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4295                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4296                "}");
4297   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4298                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4299                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4300                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4301                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4302   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4303                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4304                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4305                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4306                "}");
4307 
4308   // Here, it is not necessary to wrap at "." or "->".
4309   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4310                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4311   verifyFormat(
4312       "aaaaaaaaaaa->aaaaaaaaa(\n"
4313       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4314       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4315 
4316   verifyFormat(
4317       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4318       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4319   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4320                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4321   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4322                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4323 
4324   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4325                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4326                "    .a();");
4327 
4328   FormatStyle NoBinPacking = getLLVMStyle();
4329   NoBinPacking.BinPackParameters = false;
4330   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4331                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4332                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4333                "                         aaaaaaaaaaaaaaaaaaa,\n"
4334                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4335                NoBinPacking);
4336 
4337   // If there is a subsequent call, change to hanging indentation.
4338   verifyFormat(
4339       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4340       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4341       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4342   verifyFormat(
4343       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4344       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4345   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4346                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4347                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4348   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4349                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4350                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4351 }
4352 
4353 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4354   verifyFormat("template <typename T>\n"
4355                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4356   verifyFormat("template <typename T>\n"
4357                "// T should be one of {A, B}.\n"
4358                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4359   verifyFormat(
4360       "template <typename T>\n"
4361       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4362   verifyFormat("template <typename T>\n"
4363                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4364                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4365   verifyFormat(
4366       "template <typename T>\n"
4367       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4368       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4369   verifyFormat(
4370       "template <typename T>\n"
4371       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4372       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4373       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4374   verifyFormat("template <typename T>\n"
4375                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4376                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4377   verifyFormat(
4378       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4379       "          typename T4 = char>\n"
4380       "void f();");
4381   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4382                "          template <typename> class cccccccccccccccccccccc,\n"
4383                "          typename ddddddddddddd>\n"
4384                "class C {};");
4385   verifyFormat(
4386       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4387       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4388 
4389   verifyFormat("void f() {\n"
4390                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4391                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4392                "}");
4393 
4394   verifyFormat("template <typename T> class C {};");
4395   verifyFormat("template <typename T> void f();");
4396   verifyFormat("template <typename T> void f() {}");
4397   verifyFormat(
4398       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4399       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4400       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4401       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4402       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4403       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4404       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4405       getLLVMStyleWithColumns(72));
4406   EXPECT_EQ("static_cast<A< //\n"
4407             "    B> *>(\n"
4408             "\n"
4409             "    );",
4410             format("static_cast<A<//\n"
4411                    "    B>*>(\n"
4412                    "\n"
4413                    "    );"));
4414   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4415                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4416 
4417   FormatStyle AlwaysBreak = getLLVMStyle();
4418   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4419   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4420   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4421   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4422   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4423                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4424                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4425   verifyFormat("template <template <typename> class Fooooooo,\n"
4426                "          template <typename> class Baaaaaaar>\n"
4427                "struct C {};",
4428                AlwaysBreak);
4429   verifyFormat("template <typename T> // T can be A, B or C.\n"
4430                "struct C {};",
4431                AlwaysBreak);
4432   verifyFormat("template <enum E> class A {\n"
4433                "public:\n"
4434                "  E *f();\n"
4435                "};");
4436 }
4437 
4438 TEST_F(FormatTest, WrapsTemplateParameters) {
4439   FormatStyle Style = getLLVMStyle();
4440   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4441   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4442   verifyFormat(
4443       "template <typename... a> struct q {};\n"
4444       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4445       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4446       "    y;",
4447       Style);
4448   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4449   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4450   verifyFormat(
4451       "template <typename... a> struct r {};\n"
4452       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4453       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4454       "    y;",
4455       Style);
4456   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4457   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4458   verifyFormat(
4459       "template <typename... a> struct s {};\n"
4460       "extern s<\n"
4461       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4462       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4463       "    y;",
4464       Style);
4465   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4466   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4467   verifyFormat(
4468       "template <typename... a> struct t {};\n"
4469       "extern t<\n"
4470       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4471       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4472       "    y;",
4473       Style);
4474 }
4475 
4476 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4477   verifyFormat(
4478       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4479       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4480   verifyFormat(
4481       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4482       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4483       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4484 
4485   // FIXME: Should we have the extra indent after the second break?
4486   verifyFormat(
4487       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4488       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4489       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4490 
4491   verifyFormat(
4492       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4493       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4494 
4495   // Breaking at nested name specifiers is generally not desirable.
4496   verifyFormat(
4497       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4498       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4499 
4500   verifyFormat(
4501       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
4502       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4503       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4504       "                   aaaaaaaaaaaaaaaaaaaaa);",
4505       getLLVMStyleWithColumns(74));
4506 
4507   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4508                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4509                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4510 }
4511 
4512 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4513   verifyFormat("A<int> a;");
4514   verifyFormat("A<A<A<int>>> a;");
4515   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4516   verifyFormat("bool x = a < 1 || 2 > a;");
4517   verifyFormat("bool x = 5 < f<int>();");
4518   verifyFormat("bool x = f<int>() > 5;");
4519   verifyFormat("bool x = 5 < a<int>::x;");
4520   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4521   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4522 
4523   verifyGoogleFormat("A<A<int>> a;");
4524   verifyGoogleFormat("A<A<A<int>>> a;");
4525   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4526   verifyGoogleFormat("A<A<int> > a;");
4527   verifyGoogleFormat("A<A<A<int> > > a;");
4528   verifyGoogleFormat("A<A<A<A<int> > > > a;");
4529   verifyGoogleFormat("A<::A<int>> a;");
4530   verifyGoogleFormat("A<::A> a;");
4531   verifyGoogleFormat("A< ::A> a;");
4532   verifyGoogleFormat("A< ::A<int> > a;");
4533   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4534   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4535   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4536   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4537   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
4538             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
4539 
4540   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
4541 
4542   verifyFormat("test >> a >> b;");
4543   verifyFormat("test << a >> b;");
4544 
4545   verifyFormat("f<int>();");
4546   verifyFormat("template <typename T> void f() {}");
4547   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
4548   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
4549                "sizeof(char)>::type>;");
4550   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
4551   verifyFormat("f(a.operator()<A>());");
4552   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4553                "      .template operator()<A>());",
4554                getLLVMStyleWithColumns(35));
4555 
4556   // Not template parameters.
4557   verifyFormat("return a < b && c > d;");
4558   verifyFormat("void f() {\n"
4559                "  while (a < b && c > d) {\n"
4560                "  }\n"
4561                "}");
4562   verifyFormat("template <typename... Types>\n"
4563                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
4564 
4565   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4566                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
4567                getLLVMStyleWithColumns(60));
4568   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
4569   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
4570   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
4571 }
4572 
4573 TEST_F(FormatTest, BitshiftOperatorWidth) {
4574   EXPECT_EQ("int a = 1 << 2; /* foo\n"
4575             "                   bar */",
4576             format("int    a=1<<2;  /* foo\n"
4577                    "                   bar */"));
4578 
4579   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
4580             "                     bar */",
4581             format("int  b  =256>>1 ;  /* foo\n"
4582                    "                      bar */"));
4583 }
4584 
4585 TEST_F(FormatTest, UnderstandsBinaryOperators) {
4586   verifyFormat("COMPARE(a, ==, b);");
4587   verifyFormat("auto s = sizeof...(Ts) - 1;");
4588 }
4589 
4590 TEST_F(FormatTest, UnderstandsPointersToMembers) {
4591   verifyFormat("int A::*x;");
4592   verifyFormat("int (S::*func)(void *);");
4593   verifyFormat("void f() { int (S::*func)(void *); }");
4594   verifyFormat("typedef bool *(Class::*Member)() const;");
4595   verifyFormat("void f() {\n"
4596                "  (a->*f)();\n"
4597                "  a->*x;\n"
4598                "  (a.*f)();\n"
4599                "  ((*a).*f)();\n"
4600                "  a.*x;\n"
4601                "}");
4602   verifyFormat("void f() {\n"
4603                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
4604                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
4605                "}");
4606   verifyFormat(
4607       "(aaaaaaaaaa->*bbbbbbb)(\n"
4608       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4609   FormatStyle Style = getLLVMStyle();
4610   Style.PointerAlignment = FormatStyle::PAS_Left;
4611   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
4612 }
4613 
4614 TEST_F(FormatTest, UnderstandsUnaryOperators) {
4615   verifyFormat("int a = -2;");
4616   verifyFormat("f(-1, -2, -3);");
4617   verifyFormat("a[-1] = 5;");
4618   verifyFormat("int a = 5 + -2;");
4619   verifyFormat("if (i == -1) {\n}");
4620   verifyFormat("if (i != -1) {\n}");
4621   verifyFormat("if (i > -1) {\n}");
4622   verifyFormat("if (i < -1) {\n}");
4623   verifyFormat("++(a->f());");
4624   verifyFormat("--(a->f());");
4625   verifyFormat("(a->f())++;");
4626   verifyFormat("a[42]++;");
4627   verifyFormat("if (!(a->f())) {\n}");
4628 
4629   verifyFormat("a-- > b;");
4630   verifyFormat("b ? -a : c;");
4631   verifyFormat("n * sizeof char16;");
4632   verifyFormat("n * alignof char16;", getGoogleStyle());
4633   verifyFormat("sizeof(char);");
4634   verifyFormat("alignof(char);", getGoogleStyle());
4635 
4636   verifyFormat("return -1;");
4637   verifyFormat("switch (a) {\n"
4638                "case -1:\n"
4639                "  break;\n"
4640                "}");
4641   verifyFormat("#define X -1");
4642   verifyFormat("#define X -kConstant");
4643 
4644   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
4645   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
4646 
4647   verifyFormat("int a = /* confusing comment */ -1;");
4648   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
4649   verifyFormat("int a = i /* confusing comment */++;");
4650 }
4651 
4652 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
4653   verifyFormat("if (!aaaaaaaaaa( // break\n"
4654                "        aaaaa)) {\n"
4655                "}");
4656   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
4657                "    aaaaa));");
4658   verifyFormat("*aaa = aaaaaaa( // break\n"
4659                "    bbbbbb);");
4660 }
4661 
4662 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
4663   verifyFormat("bool operator<();");
4664   verifyFormat("bool operator>();");
4665   verifyFormat("bool operator=();");
4666   verifyFormat("bool operator==();");
4667   verifyFormat("bool operator!=();");
4668   verifyFormat("int operator+();");
4669   verifyFormat("int operator++();");
4670   verifyFormat("bool operator,();");
4671   verifyFormat("bool operator();");
4672   verifyFormat("bool operator()();");
4673   verifyFormat("bool operator[]();");
4674   verifyFormat("operator bool();");
4675   verifyFormat("operator int();");
4676   verifyFormat("operator void *();");
4677   verifyFormat("operator SomeType<int>();");
4678   verifyFormat("operator SomeType<int, int>();");
4679   verifyFormat("operator SomeType<SomeType<int>>();");
4680   verifyFormat("void *operator new(std::size_t size);");
4681   verifyFormat("void *operator new[](std::size_t size);");
4682   verifyFormat("void operator delete(void *ptr);");
4683   verifyFormat("void operator delete[](void *ptr);");
4684   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
4685                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
4686   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
4687                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
4688 
4689   verifyFormat(
4690       "ostream &operator<<(ostream &OutputStream,\n"
4691       "                    SomeReallyLongType WithSomeReallyLongValue);");
4692   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
4693                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
4694                "  return left.group < right.group;\n"
4695                "}");
4696   verifyFormat("SomeType &operator=(const SomeType &S);");
4697   verifyFormat("f.template operator()<int>();");
4698 
4699   verifyGoogleFormat("operator void*();");
4700   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
4701   verifyGoogleFormat("operator ::A();");
4702 
4703   verifyFormat("using A::operator+;");
4704   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
4705                "int i;");
4706 }
4707 
4708 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
4709   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
4710   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
4711   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
4712   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
4713   verifyFormat("Deleted &operator=(const Deleted &) &;");
4714   verifyFormat("Deleted &operator=(const Deleted &) &&;");
4715   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
4716   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
4717   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
4718   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
4719   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
4720   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
4721   verifyFormat("template <typename T>\n"
4722                "void F(T) && = delete;",
4723                getGoogleStyle());
4724 
4725   FormatStyle AlignLeft = getLLVMStyle();
4726   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
4727   verifyFormat("void A::b() && {}", AlignLeft);
4728   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
4729   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
4730                AlignLeft);
4731   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
4732   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
4733   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
4734   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
4735   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
4736   verifyFormat("auto Function(T) & -> void;", AlignLeft);
4737   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
4738 
4739   FormatStyle Spaces = getLLVMStyle();
4740   Spaces.SpacesInCStyleCastParentheses = true;
4741   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
4742   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
4743   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
4744   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
4745 
4746   Spaces.SpacesInCStyleCastParentheses = false;
4747   Spaces.SpacesInParentheses = true;
4748   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
4749   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
4750   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
4751   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
4752 }
4753 
4754 TEST_F(FormatTest, UnderstandsNewAndDelete) {
4755   verifyFormat("void f() {\n"
4756                "  A *a = new A;\n"
4757                "  A *a = new (placement) A;\n"
4758                "  delete a;\n"
4759                "  delete (A *)a;\n"
4760                "}");
4761   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
4762                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
4763   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4764                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
4765                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
4766   verifyFormat("delete[] h->p;");
4767 }
4768 
4769 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
4770   verifyFormat("int *f(int *a) {}");
4771   verifyFormat("int main(int argc, char **argv) {}");
4772   verifyFormat("Test::Test(int b) : a(b * b) {}");
4773   verifyIndependentOfContext("f(a, *a);");
4774   verifyFormat("void g() { f(*a); }");
4775   verifyIndependentOfContext("int a = b * 10;");
4776   verifyIndependentOfContext("int a = 10 * b;");
4777   verifyIndependentOfContext("int a = b * c;");
4778   verifyIndependentOfContext("int a += b * c;");
4779   verifyIndependentOfContext("int a -= b * c;");
4780   verifyIndependentOfContext("int a *= b * c;");
4781   verifyIndependentOfContext("int a /= b * c;");
4782   verifyIndependentOfContext("int a = *b;");
4783   verifyIndependentOfContext("int a = *b * c;");
4784   verifyIndependentOfContext("int a = b * *c;");
4785   verifyIndependentOfContext("int a = b * (10);");
4786   verifyIndependentOfContext("S << b * (10);");
4787   verifyIndependentOfContext("return 10 * b;");
4788   verifyIndependentOfContext("return *b * *c;");
4789   verifyIndependentOfContext("return a & ~b;");
4790   verifyIndependentOfContext("f(b ? *c : *d);");
4791   verifyIndependentOfContext("int a = b ? *c : *d;");
4792   verifyIndependentOfContext("*b = a;");
4793   verifyIndependentOfContext("a * ~b;");
4794   verifyIndependentOfContext("a * !b;");
4795   verifyIndependentOfContext("a * +b;");
4796   verifyIndependentOfContext("a * -b;");
4797   verifyIndependentOfContext("a * ++b;");
4798   verifyIndependentOfContext("a * --b;");
4799   verifyIndependentOfContext("a[4] * b;");
4800   verifyIndependentOfContext("a[a * a] = 1;");
4801   verifyIndependentOfContext("f() * b;");
4802   verifyIndependentOfContext("a * [self dostuff];");
4803   verifyIndependentOfContext("int x = a * (a + b);");
4804   verifyIndependentOfContext("(a *)(a + b);");
4805   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
4806   verifyIndependentOfContext("int *pa = (int *)&a;");
4807   verifyIndependentOfContext("return sizeof(int **);");
4808   verifyIndependentOfContext("return sizeof(int ******);");
4809   verifyIndependentOfContext("return (int **&)a;");
4810   verifyIndependentOfContext("f((*PointerToArray)[10]);");
4811   verifyFormat("void f(Type (*parameter)[10]) {}");
4812   verifyFormat("void f(Type (&parameter)[10]) {}");
4813   verifyGoogleFormat("return sizeof(int**);");
4814   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
4815   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
4816   verifyFormat("auto a = [](int **&, int ***) {};");
4817   verifyFormat("auto PointerBinding = [](const char *S) {};");
4818   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
4819   verifyFormat("[](const decltype(*a) &value) {}");
4820   verifyFormat("decltype(a * b) F();");
4821   verifyFormat("#define MACRO() [](A *a) { return 1; }");
4822   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
4823   verifyIndependentOfContext("typedef void (*f)(int *a);");
4824   verifyIndependentOfContext("int i{a * b};");
4825   verifyIndependentOfContext("aaa && aaa->f();");
4826   verifyIndependentOfContext("int x = ~*p;");
4827   verifyFormat("Constructor() : a(a), area(width * height) {}");
4828   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
4829   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
4830   verifyFormat("void f() { f(a, c * d); }");
4831   verifyFormat("void f() { f(new a(), c * d); }");
4832   verifyFormat("void f(const MyOverride &override);");
4833   verifyFormat("void f(const MyFinal &final);");
4834   verifyIndependentOfContext("bool a = f() && override.f();");
4835   verifyIndependentOfContext("bool a = f() && final.f();");
4836 
4837   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
4838 
4839   verifyIndependentOfContext("A<int *> a;");
4840   verifyIndependentOfContext("A<int **> a;");
4841   verifyIndependentOfContext("A<int *, int *> a;");
4842   verifyIndependentOfContext("A<int *[]> a;");
4843   verifyIndependentOfContext(
4844       "const char *const p = reinterpret_cast<const char *const>(q);");
4845   verifyIndependentOfContext("A<int **, int **> a;");
4846   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
4847   verifyFormat("for (char **a = b; *a; ++a) {\n}");
4848   verifyFormat("for (; a && b;) {\n}");
4849   verifyFormat("bool foo = true && [] { return false; }();");
4850 
4851   verifyFormat(
4852       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4853       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4854 
4855   verifyGoogleFormat("int const* a = &b;");
4856   verifyGoogleFormat("**outparam = 1;");
4857   verifyGoogleFormat("*outparam = a * b;");
4858   verifyGoogleFormat("int main(int argc, char** argv) {}");
4859   verifyGoogleFormat("A<int*> a;");
4860   verifyGoogleFormat("A<int**> a;");
4861   verifyGoogleFormat("A<int*, int*> a;");
4862   verifyGoogleFormat("A<int**, int**> a;");
4863   verifyGoogleFormat("f(b ? *c : *d);");
4864   verifyGoogleFormat("int a = b ? *c : *d;");
4865   verifyGoogleFormat("Type* t = **x;");
4866   verifyGoogleFormat("Type* t = *++*x;");
4867   verifyGoogleFormat("*++*x;");
4868   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
4869   verifyGoogleFormat("Type* t = x++ * y;");
4870   verifyGoogleFormat(
4871       "const char* const p = reinterpret_cast<const char* const>(q);");
4872   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
4873   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
4874   verifyGoogleFormat("template <typename T>\n"
4875                      "void f(int i = 0, SomeType** temps = NULL);");
4876 
4877   FormatStyle Left = getLLVMStyle();
4878   Left.PointerAlignment = FormatStyle::PAS_Left;
4879   verifyFormat("x = *a(x) = *a(y);", Left);
4880   verifyFormat("for (;; *a = b) {\n}", Left);
4881   verifyFormat("return *this += 1;", Left);
4882 
4883   verifyIndependentOfContext("a = *(x + y);");
4884   verifyIndependentOfContext("a = &(x + y);");
4885   verifyIndependentOfContext("*(x + y).call();");
4886   verifyIndependentOfContext("&(x + y)->call();");
4887   verifyFormat("void f() { &(*I).first; }");
4888 
4889   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
4890   verifyFormat(
4891       "int *MyValues = {\n"
4892       "    *A, // Operator detection might be confused by the '{'\n"
4893       "    *BB // Operator detection might be confused by previous comment\n"
4894       "};");
4895 
4896   verifyIndependentOfContext("if (int *a = &b)");
4897   verifyIndependentOfContext("if (int &a = *b)");
4898   verifyIndependentOfContext("if (a & b[i])");
4899   verifyIndependentOfContext("if (a::b::c::d & b[i])");
4900   verifyIndependentOfContext("if (*b[i])");
4901   verifyIndependentOfContext("if (int *a = (&b))");
4902   verifyIndependentOfContext("while (int *a = &b)");
4903   verifyIndependentOfContext("size = sizeof *a;");
4904   verifyIndependentOfContext("if (a && (b = c))");
4905   verifyFormat("void f() {\n"
4906                "  for (const int &v : Values) {\n"
4907                "  }\n"
4908                "}");
4909   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
4910   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
4911   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
4912 
4913   verifyFormat("#define A (!a * b)");
4914   verifyFormat("#define MACRO     \\\n"
4915                "  int *i = a * b; \\\n"
4916                "  void f(a *b);",
4917                getLLVMStyleWithColumns(19));
4918 
4919   verifyIndependentOfContext("A = new SomeType *[Length];");
4920   verifyIndependentOfContext("A = new SomeType *[Length]();");
4921   verifyIndependentOfContext("T **t = new T *;");
4922   verifyIndependentOfContext("T **t = new T *();");
4923   verifyGoogleFormat("A = new SomeType*[Length]();");
4924   verifyGoogleFormat("A = new SomeType*[Length];");
4925   verifyGoogleFormat("T** t = new T*;");
4926   verifyGoogleFormat("T** t = new T*();");
4927 
4928   FormatStyle PointerLeft = getLLVMStyle();
4929   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
4930   verifyFormat("delete *x;", PointerLeft);
4931   verifyFormat("STATIC_ASSERT((a & b) == 0);");
4932   verifyFormat("STATIC_ASSERT(0 == (a & b));");
4933   verifyFormat("template <bool a, bool b> "
4934                "typename t::if<x && y>::type f() {}");
4935   verifyFormat("template <int *y> f() {}");
4936   verifyFormat("vector<int *> v;");
4937   verifyFormat("vector<int *const> v;");
4938   verifyFormat("vector<int *const **const *> v;");
4939   verifyFormat("vector<int *volatile> v;");
4940   verifyFormat("vector<a * b> v;");
4941   verifyFormat("foo<b && false>();");
4942   verifyFormat("foo<b & 1>();");
4943   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
4944   verifyFormat(
4945       "template <class T, class = typename std::enable_if<\n"
4946       "                       std::is_integral<T>::value &&\n"
4947       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
4948       "void F();",
4949       getLLVMStyleWithColumns(70));
4950   verifyFormat(
4951       "template <class T,\n"
4952       "          class = typename std::enable_if<\n"
4953       "              std::is_integral<T>::value &&\n"
4954       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
4955       "          class U>\n"
4956       "void F();",
4957       getLLVMStyleWithColumns(70));
4958   verifyFormat(
4959       "template <class T,\n"
4960       "          class = typename ::std::enable_if<\n"
4961       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
4962       "void F();",
4963       getGoogleStyleWithColumns(68));
4964 
4965   verifyIndependentOfContext("MACRO(int *i);");
4966   verifyIndependentOfContext("MACRO(auto *a);");
4967   verifyIndependentOfContext("MACRO(const A *a);");
4968   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
4969   verifyFormat("void f() { f(float{1}, a * a); }");
4970   // FIXME: Is there a way to make this work?
4971   // verifyIndependentOfContext("MACRO(A *a);");
4972 
4973   verifyFormat("DatumHandle const *operator->() const { return input_; }");
4974   verifyFormat("return options != nullptr && operator==(*options);");
4975 
4976   EXPECT_EQ("#define OP(x)                                    \\\n"
4977             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
4978             "    return s << a.DebugString();                 \\\n"
4979             "  }",
4980             format("#define OP(x) \\\n"
4981                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
4982                    "    return s << a.DebugString(); \\\n"
4983                    "  }",
4984                    getLLVMStyleWithColumns(50)));
4985 
4986   // FIXME: We cannot handle this case yet; we might be able to figure out that
4987   // foo<x> d > v; doesn't make sense.
4988   verifyFormat("foo<a<b && c> d> v;");
4989 
4990   FormatStyle PointerMiddle = getLLVMStyle();
4991   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
4992   verifyFormat("delete *x;", PointerMiddle);
4993   verifyFormat("int * x;", PointerMiddle);
4994   verifyFormat("template <int * y> f() {}", PointerMiddle);
4995   verifyFormat("int * f(int * a) {}", PointerMiddle);
4996   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
4997   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
4998   verifyFormat("A<int *> a;", PointerMiddle);
4999   verifyFormat("A<int **> a;", PointerMiddle);
5000   verifyFormat("A<int *, int *> a;", PointerMiddle);
5001   verifyFormat("A<int * []> a;", PointerMiddle);
5002   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5003   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5004   verifyFormat("T ** t = new T *;", PointerMiddle);
5005 
5006   // Member function reference qualifiers aren't binary operators.
5007   verifyFormat("string // break\n"
5008                "operator()() & {}");
5009   verifyFormat("string // break\n"
5010                "operator()() && {}");
5011   verifyGoogleFormat("template <typename T>\n"
5012                      "auto x() & -> int {}");
5013 }
5014 
5015 TEST_F(FormatTest, UnderstandsAttributes) {
5016   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5017   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5018                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5019   FormatStyle AfterType = getLLVMStyle();
5020   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5021   verifyFormat("__attribute__((nodebug)) void\n"
5022                "foo() {}\n",
5023                AfterType);
5024 }
5025 
5026 TEST_F(FormatTest, UnderstandsEllipsis) {
5027   verifyFormat("int printf(const char *fmt, ...);");
5028   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5029   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5030 
5031   FormatStyle PointersLeft = getLLVMStyle();
5032   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5033   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5034 }
5035 
5036 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5037   EXPECT_EQ("int *a;\n"
5038             "int *a;\n"
5039             "int *a;",
5040             format("int *a;\n"
5041                    "int* a;\n"
5042                    "int *a;",
5043                    getGoogleStyle()));
5044   EXPECT_EQ("int* a;\n"
5045             "int* a;\n"
5046             "int* a;",
5047             format("int* a;\n"
5048                    "int* a;\n"
5049                    "int *a;",
5050                    getGoogleStyle()));
5051   EXPECT_EQ("int *a;\n"
5052             "int *a;\n"
5053             "int *a;",
5054             format("int *a;\n"
5055                    "int * a;\n"
5056                    "int *  a;",
5057                    getGoogleStyle()));
5058   EXPECT_EQ("auto x = [] {\n"
5059             "  int *a;\n"
5060             "  int *a;\n"
5061             "  int *a;\n"
5062             "};",
5063             format("auto x=[]{int *a;\n"
5064                    "int * a;\n"
5065                    "int *  a;};",
5066                    getGoogleStyle()));
5067 }
5068 
5069 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5070   verifyFormat("int f(int &&a) {}");
5071   verifyFormat("int f(int a, char &&b) {}");
5072   verifyFormat("void f() { int &&a = b; }");
5073   verifyGoogleFormat("int f(int a, char&& b) {}");
5074   verifyGoogleFormat("void f() { int&& a = b; }");
5075 
5076   verifyIndependentOfContext("A<int &&> a;");
5077   verifyIndependentOfContext("A<int &&, int &&> a;");
5078   verifyGoogleFormat("A<int&&> a;");
5079   verifyGoogleFormat("A<int&&, int&&> a;");
5080 
5081   // Not rvalue references:
5082   verifyFormat("template <bool B, bool C> class A {\n"
5083                "  static_assert(B && C, \"Something is wrong\");\n"
5084                "};");
5085   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5086   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5087   verifyFormat("#define A(a, b) (a && b)");
5088 }
5089 
5090 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5091   verifyFormat("void f() {\n"
5092                "  x[aaaaaaaaa -\n"
5093                "    b] = 23;\n"
5094                "}",
5095                getLLVMStyleWithColumns(15));
5096 }
5097 
5098 TEST_F(FormatTest, FormatsCasts) {
5099   verifyFormat("Type *A = static_cast<Type *>(P);");
5100   verifyFormat("Type *A = (Type *)P;");
5101   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5102   verifyFormat("int a = (int)(2.0f);");
5103   verifyFormat("int a = (int)2.0f;");
5104   verifyFormat("x[(int32)y];");
5105   verifyFormat("x = (int32)y;");
5106   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5107   verifyFormat("int a = (int)*b;");
5108   verifyFormat("int a = (int)2.0f;");
5109   verifyFormat("int a = (int)~0;");
5110   verifyFormat("int a = (int)++a;");
5111   verifyFormat("int a = (int)sizeof(int);");
5112   verifyFormat("int a = (int)+2;");
5113   verifyFormat("my_int a = (my_int)2.0f;");
5114   verifyFormat("my_int a = (my_int)sizeof(int);");
5115   verifyFormat("return (my_int)aaa;");
5116   verifyFormat("#define x ((int)-1)");
5117   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5118   verifyFormat("#define p(q) ((int *)&q)");
5119   verifyFormat("fn(a)(b) + 1;");
5120 
5121   verifyFormat("void f() { my_int a = (my_int)*b; }");
5122   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5123   verifyFormat("my_int a = (my_int)~0;");
5124   verifyFormat("my_int a = (my_int)++a;");
5125   verifyFormat("my_int a = (my_int)-2;");
5126   verifyFormat("my_int a = (my_int)1;");
5127   verifyFormat("my_int a = (my_int *)1;");
5128   verifyFormat("my_int a = (const my_int)-1;");
5129   verifyFormat("my_int a = (const my_int *)-1;");
5130   verifyFormat("my_int a = (my_int)(my_int)-1;");
5131   verifyFormat("my_int a = (ns::my_int)-2;");
5132   verifyFormat("case (my_int)ONE:");
5133   verifyFormat("auto x = (X)this;");
5134 
5135   // FIXME: single value wrapped with paren will be treated as cast.
5136   verifyFormat("void f(int i = (kValue)*kMask) {}");
5137 
5138   verifyFormat("{ (void)F; }");
5139 
5140   // Don't break after a cast's
5141   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5142                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5143                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5144 
5145   // These are not casts.
5146   verifyFormat("void f(int *) {}");
5147   verifyFormat("f(foo)->b;");
5148   verifyFormat("f(foo).b;");
5149   verifyFormat("f(foo)(b);");
5150   verifyFormat("f(foo)[b];");
5151   verifyFormat("[](foo) { return 4; }(bar);");
5152   verifyFormat("(*funptr)(foo)[4];");
5153   verifyFormat("funptrs[4](foo)[4];");
5154   verifyFormat("void f(int *);");
5155   verifyFormat("void f(int *) = 0;");
5156   verifyFormat("void f(SmallVector<int>) {}");
5157   verifyFormat("void f(SmallVector<int>);");
5158   verifyFormat("void f(SmallVector<int>) = 0;");
5159   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5160   verifyFormat("int a = sizeof(int) * b;");
5161   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5162   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5163   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5164   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5165 
5166   // These are not casts, but at some point were confused with casts.
5167   verifyFormat("virtual void foo(int *) override;");
5168   verifyFormat("virtual void foo(char &) const;");
5169   verifyFormat("virtual void foo(int *a, char *) const;");
5170   verifyFormat("int a = sizeof(int *) + b;");
5171   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5172   verifyFormat("bool b = f(g<int>) && c;");
5173   verifyFormat("typedef void (*f)(int i) func;");
5174 
5175   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5176                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5177   // FIXME: The indentation here is not ideal.
5178   verifyFormat(
5179       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5180       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5181       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5182 }
5183 
5184 TEST_F(FormatTest, FormatsFunctionTypes) {
5185   verifyFormat("A<bool()> a;");
5186   verifyFormat("A<SomeType()> a;");
5187   verifyFormat("A<void (*)(int, std::string)> a;");
5188   verifyFormat("A<void *(int)>;");
5189   verifyFormat("void *(*a)(int *, SomeType *);");
5190   verifyFormat("int (*func)(void *);");
5191   verifyFormat("void f() { int (*func)(void *); }");
5192   verifyFormat("template <class CallbackClass>\n"
5193                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5194 
5195   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5196   verifyGoogleFormat("void* (*a)(int);");
5197   verifyGoogleFormat(
5198       "template <class CallbackClass>\n"
5199       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5200 
5201   // Other constructs can look somewhat like function types:
5202   verifyFormat("A<sizeof(*x)> a;");
5203   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5204   verifyFormat("some_var = function(*some_pointer_var)[0];");
5205   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5206   verifyFormat("int x = f(&h)();");
5207   verifyFormat("returnsFunction(&param1, &param2)(param);");
5208 }
5209 
5210 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5211   verifyFormat("A (*foo_)[6];");
5212   verifyFormat("vector<int> (*foo_)[6];");
5213 }
5214 
5215 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5216   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5217                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5218   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5219                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5220   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5221                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5222 
5223   // Different ways of ()-initializiation.
5224   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5225                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5226   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5227                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5228   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5229                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5230   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5231                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5232 
5233   // Lambdas should not confuse the variable declaration heuristic.
5234   verifyFormat("LooooooooooooooooongType\n"
5235                "    variable(nullptr, [](A *a) {});",
5236                getLLVMStyleWithColumns(40));
5237 }
5238 
5239 TEST_F(FormatTest, BreaksLongDeclarations) {
5240   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5241                "    AnotherNameForTheLongType;");
5242   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5243                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5244   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5245                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5246   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5247                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5248   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5249                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5250   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5251                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5252   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5253                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5254   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5255                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5256   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5257                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
5258   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5259                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
5260   FormatStyle Indented = getLLVMStyle();
5261   Indented.IndentWrappedFunctionNames = true;
5262   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5263                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5264                Indented);
5265   verifyFormat(
5266       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5267       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5268       Indented);
5269   verifyFormat(
5270       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5271       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5272       Indented);
5273   verifyFormat(
5274       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5275       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5276       Indented);
5277 
5278   // FIXME: Without the comment, this breaks after "(".
5279   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5280                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5281                getGoogleStyle());
5282 
5283   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5284                "                  int LoooooooooooooooooooongParam2) {}");
5285   verifyFormat(
5286       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5287       "                                   SourceLocation L, IdentifierIn *II,\n"
5288       "                                   Type *T) {}");
5289   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5290                "ReallyReaaallyLongFunctionName(\n"
5291                "    const std::string &SomeParameter,\n"
5292                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5293                "        &ReallyReallyLongParameterName,\n"
5294                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5295                "        &AnotherLongParameterName) {}");
5296   verifyFormat("template <typename A>\n"
5297                "SomeLoooooooooooooooooooooongType<\n"
5298                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5299                "Function() {}");
5300 
5301   verifyGoogleFormat(
5302       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5303       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5304   verifyGoogleFormat(
5305       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5306       "                                   SourceLocation L) {}");
5307   verifyGoogleFormat(
5308       "some_namespace::LongReturnType\n"
5309       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5310       "    int first_long_parameter, int second_parameter) {}");
5311 
5312   verifyGoogleFormat("template <typename T>\n"
5313                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5314                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5315   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5316                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5317 
5318   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5319                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5320                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5321   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5322                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5323                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5324   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5325                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5326                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5327                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5328 }
5329 
5330 TEST_F(FormatTest, FormatsArrays) {
5331   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5332                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5333   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
5334                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
5335   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5336                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
5337   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5338                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5339   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5340                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5341   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5342                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5343                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5344   verifyFormat(
5345       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5346       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5347       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5348   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
5349                "    .aaaaaaaaaaaaaaaaaaaaaa();");
5350 
5351   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5352                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5353   verifyFormat(
5354       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5355       "                                  .aaaaaaa[0]\n"
5356       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5357   verifyFormat("a[::b::c];");
5358 
5359   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5360 
5361   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
5362   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
5363 }
5364 
5365 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5366   verifyFormat("(a)->b();");
5367   verifyFormat("--a;");
5368 }
5369 
5370 TEST_F(FormatTest, HandlesIncludeDirectives) {
5371   verifyFormat("#include <string>\n"
5372                "#include <a/b/c.h>\n"
5373                "#include \"a/b/string\"\n"
5374                "#include \"string.h\"\n"
5375                "#include \"string.h\"\n"
5376                "#include <a-a>\n"
5377                "#include < path with space >\n"
5378                "#include_next <test.h>"
5379                "#include \"abc.h\" // this is included for ABC\n"
5380                "#include \"some long include\" // with a comment\n"
5381                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5382                getLLVMStyleWithColumns(35));
5383   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5384   EXPECT_EQ("#include <a>", format("#include<a>"));
5385 
5386   verifyFormat("#import <string>");
5387   verifyFormat("#import <a/b/c.h>");
5388   verifyFormat("#import \"a/b/string\"");
5389   verifyFormat("#import \"string.h\"");
5390   verifyFormat("#import \"string.h\"");
5391   verifyFormat("#if __has_include(<strstream>)\n"
5392                "#include <strstream>\n"
5393                "#endif");
5394 
5395   verifyFormat("#define MY_IMPORT <a/b>");
5396 
5397   verifyFormat("#if __has_include(<a/b>)");
5398   verifyFormat("#if __has_include_next(<a/b>)");
5399   verifyFormat("#define F __has_include(<a/b>)");
5400   verifyFormat("#define F __has_include_next(<a/b>)");
5401 
5402   // Protocol buffer definition or missing "#".
5403   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5404                getLLVMStyleWithColumns(30));
5405 
5406   FormatStyle Style = getLLVMStyle();
5407   Style.AlwaysBreakBeforeMultilineStrings = true;
5408   Style.ColumnLimit = 0;
5409   verifyFormat("#import \"abc.h\"", Style);
5410 
5411   // But 'import' might also be a regular C++ namespace.
5412   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5413                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5414 }
5415 
5416 //===----------------------------------------------------------------------===//
5417 // Error recovery tests.
5418 //===----------------------------------------------------------------------===//
5419 
5420 TEST_F(FormatTest, IncompleteParameterLists) {
5421   FormatStyle NoBinPacking = getLLVMStyle();
5422   NoBinPacking.BinPackParameters = false;
5423   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5424                "                        double *min_x,\n"
5425                "                        double *max_x,\n"
5426                "                        double *min_y,\n"
5427                "                        double *max_y,\n"
5428                "                        double *min_z,\n"
5429                "                        double *max_z, ) {}",
5430                NoBinPacking);
5431 }
5432 
5433 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5434   verifyFormat("void f() { return; }\n42");
5435   verifyFormat("void f() {\n"
5436                "  if (0)\n"
5437                "    return;\n"
5438                "}\n"
5439                "42");
5440   verifyFormat("void f() { return }\n42");
5441   verifyFormat("void f() {\n"
5442                "  if (0)\n"
5443                "    return\n"
5444                "}\n"
5445                "42");
5446 }
5447 
5448 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5449   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5450   EXPECT_EQ("void f() {\n"
5451             "  if (a)\n"
5452             "    return\n"
5453             "}",
5454             format("void  f  (  )  {  if  ( a )  return  }"));
5455   EXPECT_EQ("namespace N {\n"
5456             "void f()\n"
5457             "}",
5458             format("namespace  N  {  void f()  }"));
5459   EXPECT_EQ("namespace N {\n"
5460             "void f() {}\n"
5461             "void g()\n"
5462             "} // namespace N",
5463             format("namespace N  { void f( ) { } void g( ) }"));
5464 }
5465 
5466 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5467   verifyFormat("int aaaaaaaa =\n"
5468                "    // Overlylongcomment\n"
5469                "    b;",
5470                getLLVMStyleWithColumns(20));
5471   verifyFormat("function(\n"
5472                "    ShortArgument,\n"
5473                "    LoooooooooooongArgument);\n",
5474                getLLVMStyleWithColumns(20));
5475 }
5476 
5477 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5478   verifyFormat("public:");
5479   verifyFormat("class A {\n"
5480                "public\n"
5481                "  void f() {}\n"
5482                "};");
5483   verifyFormat("public\n"
5484                "int qwerty;");
5485   verifyFormat("public\n"
5486                "B {}");
5487   verifyFormat("public\n"
5488                "{}");
5489   verifyFormat("public\n"
5490                "B { int x; }");
5491 }
5492 
5493 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5494   verifyFormat("{");
5495   verifyFormat("#})");
5496   verifyNoCrash("(/**/[:!] ?[).");
5497 }
5498 
5499 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5500   verifyFormat("do {\n}");
5501   verifyFormat("do {\n}\n"
5502                "f();");
5503   verifyFormat("do {\n}\n"
5504                "wheeee(fun);");
5505   verifyFormat("do {\n"
5506                "  f();\n"
5507                "}");
5508 }
5509 
5510 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5511   verifyFormat("if {\n  foo;\n  foo();\n}");
5512   verifyFormat("switch {\n  foo;\n  foo();\n}");
5513   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
5514   verifyFormat("while {\n  foo;\n  foo();\n}");
5515   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5516 }
5517 
5518 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5519   verifyIncompleteFormat("namespace {\n"
5520                          "class Foo { Foo (\n"
5521                          "};\n"
5522                          "} // namespace");
5523 }
5524 
5525 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5526   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5527   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5528   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5529   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5530 
5531   EXPECT_EQ("{\n"
5532             "  {\n"
5533             "    breakme(\n"
5534             "        qwe);\n"
5535             "  }\n",
5536             format("{\n"
5537                    "    {\n"
5538                    " breakme(qwe);\n"
5539                    "}\n",
5540                    getLLVMStyleWithColumns(10)));
5541 }
5542 
5543 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5544   verifyFormat("int x = {\n"
5545                "    avariable,\n"
5546                "    b(alongervariable)};",
5547                getLLVMStyleWithColumns(25));
5548 }
5549 
5550 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5551   verifyFormat("return (a)(b){1, 2, 3};");
5552 }
5553 
5554 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5555   verifyFormat("vector<int> x{1, 2, 3, 4};");
5556   verifyFormat("vector<int> x{\n"
5557                "    1, 2, 3, 4,\n"
5558                "};");
5559   verifyFormat("vector<T> x{{}, {}, {}, {}};");
5560   verifyFormat("f({1, 2});");
5561   verifyFormat("auto v = Foo{-1};");
5562   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5563   verifyFormat("Class::Class : member{1, 2, 3} {}");
5564   verifyFormat("new vector<int>{1, 2, 3};");
5565   verifyFormat("new int[3]{1, 2, 3};");
5566   verifyFormat("new int{1};");
5567   verifyFormat("return {arg1, arg2};");
5568   verifyFormat("return {arg1, SomeType{parameter}};");
5569   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5570   verifyFormat("new T{arg1, arg2};");
5571   verifyFormat("f(MyMap[{composite, key}]);");
5572   verifyFormat("class Class {\n"
5573                "  T member = {arg1, arg2};\n"
5574                "};");
5575   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5576   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5577   verifyFormat("int a = std::is_integral<int>{} + 0;");
5578 
5579   verifyFormat("int foo(int i) { return fo1{}(i); }");
5580   verifyFormat("int foo(int i) { return fo1{}(i); }");
5581   verifyFormat("auto i = decltype(x){};");
5582   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
5583   verifyFormat("Node n{1, Node{1000}, //\n"
5584                "       2};");
5585   verifyFormat("Aaaa aaaaaaa{\n"
5586                "    {\n"
5587                "        aaaa,\n"
5588                "    },\n"
5589                "};");
5590   verifyFormat("class C : public D {\n"
5591                "  SomeClass SC{2};\n"
5592                "};");
5593   verifyFormat("class C : public A {\n"
5594                "  class D : public B {\n"
5595                "    void f() { int i{2}; }\n"
5596                "  };\n"
5597                "};");
5598   verifyFormat("#define A {a, a},");
5599 
5600   // Cases where distinguising braced lists and blocks is hard.
5601   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
5602   verifyFormat("void f() {\n"
5603                "  return; // comment\n"
5604                "}\n"
5605                "SomeType t;");
5606   verifyFormat("void f() {\n"
5607                "  if (a) {\n"
5608                "    f();\n"
5609                "  }\n"
5610                "}\n"
5611                "SomeType t;");
5612 
5613   // In combination with BinPackArguments = false.
5614   FormatStyle NoBinPacking = getLLVMStyle();
5615   NoBinPacking.BinPackArguments = false;
5616   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5617                "                      bbbbb,\n"
5618                "                      ccccc,\n"
5619                "                      ddddd,\n"
5620                "                      eeeee,\n"
5621                "                      ffffff,\n"
5622                "                      ggggg,\n"
5623                "                      hhhhhh,\n"
5624                "                      iiiiii,\n"
5625                "                      jjjjjj,\n"
5626                "                      kkkkkk};",
5627                NoBinPacking);
5628   verifyFormat("const Aaaaaa aaaaa = {\n"
5629                "    aaaaa,\n"
5630                "    bbbbb,\n"
5631                "    ccccc,\n"
5632                "    ddddd,\n"
5633                "    eeeee,\n"
5634                "    ffffff,\n"
5635                "    ggggg,\n"
5636                "    hhhhhh,\n"
5637                "    iiiiii,\n"
5638                "    jjjjjj,\n"
5639                "    kkkkkk,\n"
5640                "};",
5641                NoBinPacking);
5642   verifyFormat(
5643       "const Aaaaaa aaaaa = {\n"
5644       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
5645       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
5646       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5647       "};",
5648       NoBinPacking);
5649 
5650   // FIXME: The alignment of these trailing comments might be bad. Then again,
5651   // this might be utterly useless in real code.
5652   verifyFormat("Constructor::Constructor()\n"
5653                "    : some_value{         //\n"
5654                "                 aaaaaaa, //\n"
5655                "                 bbbbbbb} {}");
5656 
5657   // In braced lists, the first comment is always assumed to belong to the
5658   // first element. Thus, it can be moved to the next or previous line as
5659   // appropriate.
5660   EXPECT_EQ("function({// First element:\n"
5661             "          1,\n"
5662             "          // Second element:\n"
5663             "          2});",
5664             format("function({\n"
5665                    "    // First element:\n"
5666                    "    1,\n"
5667                    "    // Second element:\n"
5668                    "    2});"));
5669   EXPECT_EQ("std::vector<int> MyNumbers{\n"
5670             "    // First element:\n"
5671             "    1,\n"
5672             "    // Second element:\n"
5673             "    2};",
5674             format("std::vector<int> MyNumbers{// First element:\n"
5675                    "                           1,\n"
5676                    "                           // Second element:\n"
5677                    "                           2};",
5678                    getLLVMStyleWithColumns(30)));
5679   // A trailing comma should still lead to an enforced line break.
5680   EXPECT_EQ("vector<int> SomeVector = {\n"
5681             "    // aaa\n"
5682             "    1, 2,\n"
5683             "};",
5684             format("vector<int> SomeVector = { // aaa\n"
5685                    "    1, 2, };"));
5686 
5687   FormatStyle ExtraSpaces = getLLVMStyle();
5688   ExtraSpaces.Cpp11BracedListStyle = false;
5689   ExtraSpaces.ColumnLimit = 75;
5690   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
5691   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
5692   verifyFormat("f({ 1, 2 });", ExtraSpaces);
5693   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
5694   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
5695   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
5696   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
5697   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
5698   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
5699   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
5700   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
5701   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
5702   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
5703   verifyFormat("class Class {\n"
5704                "  T member = { arg1, arg2 };\n"
5705                "};",
5706                ExtraSpaces);
5707   verifyFormat(
5708       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5709       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
5710       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5711       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
5712       ExtraSpaces);
5713   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
5714   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
5715                ExtraSpaces);
5716   verifyFormat(
5717       "someFunction(OtherParam,\n"
5718       "             BracedList{ // comment 1 (Forcing interesting break)\n"
5719       "                         param1, param2,\n"
5720       "                         // comment 2\n"
5721       "                         param3, param4 });",
5722       ExtraSpaces);
5723   verifyFormat(
5724       "std::this_thread::sleep_for(\n"
5725       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
5726       ExtraSpaces);
5727   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
5728                "    aaaaaaa,\n"
5729                "    aaaaaaaaaa,\n"
5730                "    aaaaa,\n"
5731                "    aaaaaaaaaaaaaaa,\n"
5732                "    aaa,\n"
5733                "    aaaaaaaaaa,\n"
5734                "    a,\n"
5735                "    aaaaaaaaaaaaaaaaaaaaa,\n"
5736                "    aaaaaaaaaaaa,\n"
5737                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
5738                "    aaaaaaa,\n"
5739                "    a};");
5740   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
5741 }
5742 
5743 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
5744   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5745                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5746                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5747                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5748                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5749                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5750   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
5751                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5752                "                 1, 22, 333, 4444, 55555, //\n"
5753                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5754                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5755   verifyFormat(
5756       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5757       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5758       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
5759       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5760       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5761       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5762       "                 7777777};");
5763   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5764                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5765                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
5766   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5767                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5768                "    // Separating comment.\n"
5769                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
5770   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5771                "    // Leading comment\n"
5772                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5773                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
5774   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5775                "                 1, 1, 1, 1};",
5776                getLLVMStyleWithColumns(39));
5777   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5778                "                 1, 1, 1, 1};",
5779                getLLVMStyleWithColumns(38));
5780   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
5781                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
5782                getLLVMStyleWithColumns(43));
5783   verifyFormat(
5784       "static unsigned SomeValues[10][3] = {\n"
5785       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
5786       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
5787   verifyFormat("static auto fields = new vector<string>{\n"
5788                "    \"aaaaaaaaaaaaa\",\n"
5789                "    \"aaaaaaaaaaaaa\",\n"
5790                "    \"aaaaaaaaaaaa\",\n"
5791                "    \"aaaaaaaaaaaaaa\",\n"
5792                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
5793                "    \"aaaaaaaaaaaa\",\n"
5794                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
5795                "};");
5796   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
5797   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
5798                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
5799                "                 3, cccccccccccccccccccccc};",
5800                getLLVMStyleWithColumns(60));
5801 
5802   // Trailing commas.
5803   verifyFormat("vector<int> x = {\n"
5804                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
5805                "};",
5806                getLLVMStyleWithColumns(39));
5807   verifyFormat("vector<int> x = {\n"
5808                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
5809                "};",
5810                getLLVMStyleWithColumns(39));
5811   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5812                "                 1, 1, 1, 1,\n"
5813                "                 /**/ /**/};",
5814                getLLVMStyleWithColumns(39));
5815 
5816   // Trailing comment in the first line.
5817   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
5818                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
5819                "    111111111,  222222222,  3333333333,  444444444,  //\n"
5820                "    11111111,   22222222,   333333333,   44444444};");
5821   // Trailing comment in the last line.
5822   verifyFormat("int aaaaa[] = {\n"
5823                "    1, 2, 3, // comment\n"
5824                "    4, 5, 6  // comment\n"
5825                "};");
5826 
5827   // With nested lists, we should either format one item per line or all nested
5828   // lists one on line.
5829   // FIXME: For some nested lists, we can do better.
5830   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
5831                "        {aaaaaaaaaaaaaaaaaaa},\n"
5832                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
5833                "        {aaaaaaaaaaaaaaaaa}};",
5834                getLLVMStyleWithColumns(60));
5835   verifyFormat(
5836       "SomeStruct my_struct_array = {\n"
5837       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
5838       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
5839       "    {aaa, aaa},\n"
5840       "    {aaa, aaa},\n"
5841       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
5842       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5843       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
5844 
5845   // No column layout should be used here.
5846   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
5847                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
5848 
5849   verifyNoCrash("a<,");
5850 
5851   // No braced initializer here.
5852   verifyFormat("void f() {\n"
5853                "  struct Dummy {};\n"
5854                "  f(v);\n"
5855                "}");
5856 
5857   // Long lists should be formatted in columns even if they are nested.
5858   verifyFormat(
5859       "vector<int> x = function({1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5860       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5861       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5862       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5863       "                          1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5864       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
5865 
5866   // Allow "single-column" layout even if that violates the column limit. There
5867   // isn't going to be a better way.
5868   verifyFormat("std::vector<int> a = {\n"
5869                "    aaaaaaaa,\n"
5870                "    aaaaaaaa,\n"
5871                "    aaaaaaaa,\n"
5872                "    aaaaaaaa,\n"
5873                "    aaaaaaaaaa,\n"
5874                "    aaaaaaaa,\n"
5875                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
5876                getLLVMStyleWithColumns(30));
5877   verifyFormat("vector<int> aaaa = {\n"
5878                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5879                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5880                "    aaaaaa.aaaaaaa,\n"
5881                "    aaaaaa.aaaaaaa,\n"
5882                "    aaaaaa.aaaaaaa,\n"
5883                "    aaaaaa.aaaaaaa,\n"
5884                "};");
5885 
5886   // Don't create hanging lists.
5887   verifyFormat("someFunction(Param, {List1, List2,\n"
5888                "                     List3});",
5889                getLLVMStyleWithColumns(35));
5890   verifyFormat("someFunction(Param, Param,\n"
5891                "             {List1, List2,\n"
5892                "              List3});",
5893                getLLVMStyleWithColumns(35));
5894   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
5895                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
5896 }
5897 
5898 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
5899   FormatStyle DoNotMerge = getLLVMStyle();
5900   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5901 
5902   verifyFormat("void f() { return 42; }");
5903   verifyFormat("void f() {\n"
5904                "  return 42;\n"
5905                "}",
5906                DoNotMerge);
5907   verifyFormat("void f() {\n"
5908                "  // Comment\n"
5909                "}");
5910   verifyFormat("{\n"
5911                "#error {\n"
5912                "  int a;\n"
5913                "}");
5914   verifyFormat("{\n"
5915                "  int a;\n"
5916                "#error {\n"
5917                "}");
5918   verifyFormat("void f() {} // comment");
5919   verifyFormat("void f() { int a; } // comment");
5920   verifyFormat("void f() {\n"
5921                "} // comment",
5922                DoNotMerge);
5923   verifyFormat("void f() {\n"
5924                "  int a;\n"
5925                "} // comment",
5926                DoNotMerge);
5927   verifyFormat("void f() {\n"
5928                "} // comment",
5929                getLLVMStyleWithColumns(15));
5930 
5931   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
5932   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
5933 
5934   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
5935   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
5936   verifyFormat("class C {\n"
5937                "  C()\n"
5938                "      : iiiiiiii(nullptr),\n"
5939                "        kkkkkkk(nullptr),\n"
5940                "        mmmmmmm(nullptr),\n"
5941                "        nnnnnnn(nullptr) {}\n"
5942                "};",
5943                getGoogleStyle());
5944 
5945   FormatStyle NoColumnLimit = getLLVMStyle();
5946   NoColumnLimit.ColumnLimit = 0;
5947   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
5948   EXPECT_EQ("class C {\n"
5949             "  A() : b(0) {}\n"
5950             "};",
5951             format("class C{A():b(0){}};", NoColumnLimit));
5952   EXPECT_EQ("A()\n"
5953             "    : b(0) {\n"
5954             "}",
5955             format("A()\n:b(0)\n{\n}", NoColumnLimit));
5956 
5957   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
5958   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
5959       FormatStyle::SFS_None;
5960   EXPECT_EQ("A()\n"
5961             "    : b(0) {\n"
5962             "}",
5963             format("A():b(0){}", DoNotMergeNoColumnLimit));
5964   EXPECT_EQ("A()\n"
5965             "    : b(0) {\n"
5966             "}",
5967             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
5968 
5969   verifyFormat("#define A          \\\n"
5970                "  void f() {       \\\n"
5971                "    int i;         \\\n"
5972                "  }",
5973                getLLVMStyleWithColumns(20));
5974   verifyFormat("#define A           \\\n"
5975                "  void f() { int i; }",
5976                getLLVMStyleWithColumns(21));
5977   verifyFormat("#define A            \\\n"
5978                "  void f() {         \\\n"
5979                "    int i;           \\\n"
5980                "  }                  \\\n"
5981                "  int j;",
5982                getLLVMStyleWithColumns(22));
5983   verifyFormat("#define A             \\\n"
5984                "  void f() { int i; } \\\n"
5985                "  int j;",
5986                getLLVMStyleWithColumns(23));
5987 }
5988 
5989 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
5990   FormatStyle MergeInlineOnly = getLLVMStyle();
5991   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
5992   verifyFormat("class C {\n"
5993                "  int f() { return 42; }\n"
5994                "};",
5995                MergeInlineOnly);
5996   verifyFormat("int f() {\n"
5997                "  return 42;\n"
5998                "}",
5999                MergeInlineOnly);
6000 }
6001 
6002 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6003   // Elaborate type variable declarations.
6004   verifyFormat("struct foo a = {bar};\nint n;");
6005   verifyFormat("class foo a = {bar};\nint n;");
6006   verifyFormat("union foo a = {bar};\nint n;");
6007 
6008   // Elaborate types inside function definitions.
6009   verifyFormat("struct foo f() {}\nint n;");
6010   verifyFormat("class foo f() {}\nint n;");
6011   verifyFormat("union foo f() {}\nint n;");
6012 
6013   // Templates.
6014   verifyFormat("template <class X> void f() {}\nint n;");
6015   verifyFormat("template <struct X> void f() {}\nint n;");
6016   verifyFormat("template <union X> void f() {}\nint n;");
6017 
6018   // Actual definitions...
6019   verifyFormat("struct {\n} n;");
6020   verifyFormat(
6021       "template <template <class T, class Y>, class Z> class X {\n} n;");
6022   verifyFormat("union Z {\n  int n;\n} x;");
6023   verifyFormat("class MACRO Z {\n} n;");
6024   verifyFormat("class MACRO(X) Z {\n} n;");
6025   verifyFormat("class __attribute__(X) Z {\n} n;");
6026   verifyFormat("class __declspec(X) Z {\n} n;");
6027   verifyFormat("class A##B##C {\n} n;");
6028   verifyFormat("class alignas(16) Z {\n} n;");
6029   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6030   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6031 
6032   // Redefinition from nested context:
6033   verifyFormat("class A::B::C {\n} n;");
6034 
6035   // Template definitions.
6036   verifyFormat(
6037       "template <typename F>\n"
6038       "Matcher(const Matcher<F> &Other,\n"
6039       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6040       "                             !is_same<F, T>::value>::type * = 0)\n"
6041       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6042 
6043   // FIXME: This is still incorrectly handled at the formatter side.
6044   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6045   verifyFormat("int i = SomeFunction(a<b, a> b);");
6046 
6047   // FIXME:
6048   // This now gets parsed incorrectly as class definition.
6049   // verifyFormat("class A<int> f() {\n}\nint n;");
6050 
6051   // Elaborate types where incorrectly parsing the structural element would
6052   // break the indent.
6053   verifyFormat("if (true)\n"
6054                "  class X x;\n"
6055                "else\n"
6056                "  f();\n");
6057 
6058   // This is simply incomplete. Formatting is not important, but must not crash.
6059   verifyFormat("class A:");
6060 }
6061 
6062 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6063   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6064             format("#error Leave     all         white!!!!! space* alone!\n"));
6065   EXPECT_EQ(
6066       "#warning Leave     all         white!!!!! space* alone!\n",
6067       format("#warning Leave     all         white!!!!! space* alone!\n"));
6068   EXPECT_EQ("#error 1", format("  #  error   1"));
6069   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6070 }
6071 
6072 TEST_F(FormatTest, FormatHashIfExpressions) {
6073   verifyFormat("#if AAAA && BBBB");
6074   verifyFormat("#if (AAAA && BBBB)");
6075   verifyFormat("#elif (AAAA && BBBB)");
6076   // FIXME: Come up with a better indentation for #elif.
6077   verifyFormat(
6078       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6079       "    defined(BBBBBBBB)\n"
6080       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6081       "    defined(BBBBBBBB)\n"
6082       "#endif",
6083       getLLVMStyleWithColumns(65));
6084 }
6085 
6086 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6087   FormatStyle AllowsMergedIf = getGoogleStyle();
6088   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6089   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6090   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6091   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6092   EXPECT_EQ("if (true) return 42;",
6093             format("if (true)\nreturn 42;", AllowsMergedIf));
6094   FormatStyle ShortMergedIf = AllowsMergedIf;
6095   ShortMergedIf.ColumnLimit = 25;
6096   verifyFormat("#define A \\\n"
6097                "  if (true) return 42;",
6098                ShortMergedIf);
6099   verifyFormat("#define A \\\n"
6100                "  f();    \\\n"
6101                "  if (true)\n"
6102                "#define B",
6103                ShortMergedIf);
6104   verifyFormat("#define A \\\n"
6105                "  f();    \\\n"
6106                "  if (true)\n"
6107                "g();",
6108                ShortMergedIf);
6109   verifyFormat("{\n"
6110                "#ifdef A\n"
6111                "  // Comment\n"
6112                "  if (true) continue;\n"
6113                "#endif\n"
6114                "  // Comment\n"
6115                "  if (true) continue;\n"
6116                "}",
6117                ShortMergedIf);
6118   ShortMergedIf.ColumnLimit = 29;
6119   verifyFormat("#define A                   \\\n"
6120                "  if (aaaaaaaaaa) return 1; \\\n"
6121                "  return 2;",
6122                ShortMergedIf);
6123   ShortMergedIf.ColumnLimit = 28;
6124   verifyFormat("#define A         \\\n"
6125                "  if (aaaaaaaaaa) \\\n"
6126                "    return 1;     \\\n"
6127                "  return 2;",
6128                ShortMergedIf);
6129 }
6130 
6131 TEST_F(FormatTest, FormatStarDependingOnContext) {
6132   verifyFormat("void f(int *a);");
6133   verifyFormat("void f() { f(fint * b); }");
6134   verifyFormat("class A {\n  void f(int *a);\n};");
6135   verifyFormat("class A {\n  int *a;\n};");
6136   verifyFormat("namespace a {\n"
6137                "namespace b {\n"
6138                "class A {\n"
6139                "  void f() {}\n"
6140                "  int *a;\n"
6141                "};\n"
6142                "} // namespace b\n"
6143                "} // namespace a");
6144 }
6145 
6146 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6147   verifyFormat("while");
6148   verifyFormat("operator");
6149 }
6150 
6151 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
6152   // This code would be painfully slow to format if we didn't skip it.
6153   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
6154                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6155                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6156                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6157                    "A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6158                    "A(1, 1)\n"
6159                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
6160                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6161                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6162                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6163                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6164                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6165                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6166                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6167                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n"
6168                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
6169   // Deeply nested part is untouched, rest is formatted.
6170   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
6171             format(std::string("int    i;\n") + Code + "int    j;\n",
6172                    getLLVMStyle(), IC_ExpectIncomplete));
6173 }
6174 
6175 //===----------------------------------------------------------------------===//
6176 // Objective-C tests.
6177 //===----------------------------------------------------------------------===//
6178 
6179 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6180   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6181   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6182             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6183   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6184   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6185   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6186             format("-(NSInteger)Method3:(id)anObject;"));
6187   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6188             format("-(NSInteger)Method4:(id)anObject;"));
6189   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6190             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6191   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6192             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6193   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6194             "forAllCells:(BOOL)flag;",
6195             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6196                    "forAllCells:(BOOL)flag;"));
6197 
6198   // Very long objectiveC method declaration.
6199   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6200                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6201   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6202                "                    inRange:(NSRange)range\n"
6203                "                   outRange:(NSRange)out_range\n"
6204                "                  outRange1:(NSRange)out_range1\n"
6205                "                  outRange2:(NSRange)out_range2\n"
6206                "                  outRange3:(NSRange)out_range3\n"
6207                "                  outRange4:(NSRange)out_range4\n"
6208                "                  outRange5:(NSRange)out_range5\n"
6209                "                  outRange6:(NSRange)out_range6\n"
6210                "                  outRange7:(NSRange)out_range7\n"
6211                "                  outRange8:(NSRange)out_range8\n"
6212                "                  outRange9:(NSRange)out_range9;");
6213 
6214   // When the function name has to be wrapped.
6215   FormatStyle Style = getLLVMStyle();
6216   Style.IndentWrappedFunctionNames = false;
6217   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6218                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6219                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6220                "}",
6221                Style);
6222   Style.IndentWrappedFunctionNames = true;
6223   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6224                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6225                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6226                "}",
6227                Style);
6228 
6229   verifyFormat("- (int)sum:(vector<int>)numbers;");
6230   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6231   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6232   // protocol lists (but not for template classes):
6233   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6234 
6235   verifyFormat("- (int (*)())foo:(int (*)())f;");
6236   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6237 
6238   // If there's no return type (very rare in practice!), LLVM and Google style
6239   // agree.
6240   verifyFormat("- foo;");
6241   verifyFormat("- foo:(int)f;");
6242   verifyGoogleFormat("- foo:(int)foo;");
6243 }
6244 
6245 
6246 TEST_F(FormatTest, BreaksStringLiterals) {
6247   EXPECT_EQ("\"some text \"\n"
6248             "\"other\";",
6249             format("\"some text other\";", getLLVMStyleWithColumns(12)));
6250   EXPECT_EQ("\"some text \"\n"
6251             "\"other\";",
6252             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
6253   EXPECT_EQ(
6254       "#define A  \\\n"
6255       "  \"some \"  \\\n"
6256       "  \"text \"  \\\n"
6257       "  \"other\";",
6258       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
6259   EXPECT_EQ(
6260       "#define A  \\\n"
6261       "  \"so \"    \\\n"
6262       "  \"text \"  \\\n"
6263       "  \"other\";",
6264       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
6265 
6266   EXPECT_EQ("\"some text\"",
6267             format("\"some text\"", getLLVMStyleWithColumns(1)));
6268   EXPECT_EQ("\"some text\"",
6269             format("\"some text\"", getLLVMStyleWithColumns(11)));
6270   EXPECT_EQ("\"some \"\n"
6271             "\"text\"",
6272             format("\"some text\"", getLLVMStyleWithColumns(10)));
6273   EXPECT_EQ("\"some \"\n"
6274             "\"text\"",
6275             format("\"some text\"", getLLVMStyleWithColumns(7)));
6276   EXPECT_EQ("\"some\"\n"
6277             "\" tex\"\n"
6278             "\"t\"",
6279             format("\"some text\"", getLLVMStyleWithColumns(6)));
6280   EXPECT_EQ("\"some\"\n"
6281             "\" tex\"\n"
6282             "\" and\"",
6283             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
6284   EXPECT_EQ("\"some\"\n"
6285             "\"/tex\"\n"
6286             "\"/and\"",
6287             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
6288 
6289   EXPECT_EQ("variable =\n"
6290             "    \"long string \"\n"
6291             "    \"literal\";",
6292             format("variable = \"long string literal\";",
6293                    getLLVMStyleWithColumns(20)));
6294 
6295   EXPECT_EQ("variable = f(\n"
6296             "    \"long string \"\n"
6297             "    \"literal\",\n"
6298             "    short,\n"
6299             "    loooooooooooooooooooong);",
6300             format("variable = f(\"long string literal\", short, "
6301                    "loooooooooooooooooooong);",
6302                    getLLVMStyleWithColumns(20)));
6303 
6304   EXPECT_EQ(
6305       "f(g(\"long string \"\n"
6306       "    \"literal\"),\n"
6307       "  b);",
6308       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
6309   EXPECT_EQ("f(g(\"long string \"\n"
6310             "    \"literal\",\n"
6311             "    a),\n"
6312             "  b);",
6313             format("f(g(\"long string literal\", a), b);",
6314                    getLLVMStyleWithColumns(20)));
6315   EXPECT_EQ(
6316       "f(\"one two\".split(\n"
6317       "    variable));",
6318       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
6319   EXPECT_EQ("f(\"one two three four five six \"\n"
6320             "  \"seven\".split(\n"
6321             "      really_looooong_variable));",
6322             format("f(\"one two three four five six seven\"."
6323                    "split(really_looooong_variable));",
6324                    getLLVMStyleWithColumns(33)));
6325 
6326   EXPECT_EQ("f(\"some \"\n"
6327             "  \"text\",\n"
6328             "  other);",
6329             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
6330 
6331   // Only break as a last resort.
6332   verifyFormat(
6333       "aaaaaaaaaaaaaaaaaaaa(\n"
6334       "    aaaaaaaaaaaaaaaaaaaa,\n"
6335       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
6336 
6337   EXPECT_EQ("\"splitmea\"\n"
6338             "\"trandomp\"\n"
6339             "\"oint\"",
6340             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
6341 
6342   EXPECT_EQ("\"split/\"\n"
6343             "\"pathat/\"\n"
6344             "\"slashes\"",
6345             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6346 
6347   EXPECT_EQ("\"split/\"\n"
6348             "\"pathat/\"\n"
6349             "\"slashes\"",
6350             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6351   EXPECT_EQ("\"split at \"\n"
6352             "\"spaces/at/\"\n"
6353             "\"slashes.at.any$\"\n"
6354             "\"non-alphanumeric%\"\n"
6355             "\"1111111111characte\"\n"
6356             "\"rs\"",
6357             format("\"split at "
6358                    "spaces/at/"
6359                    "slashes.at."
6360                    "any$non-"
6361                    "alphanumeric%"
6362                    "1111111111characte"
6363                    "rs\"",
6364                    getLLVMStyleWithColumns(20)));
6365 
6366   // Verify that splitting the strings understands
6367   // Style::AlwaysBreakBeforeMultilineStrings.
6368   EXPECT_EQ(
6369       "aaaaaaaaaaaa(\n"
6370       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
6371       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
6372       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
6373              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6374              "aaaaaaaaaaaaaaaaaaaaaa\");",
6375              getGoogleStyle()));
6376   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6377             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
6378             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
6379                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6380                    "aaaaaaaaaaaaaaaaaaaaaa\";",
6381                    getGoogleStyle()));
6382   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6383             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6384             format("llvm::outs() << "
6385                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
6386                    "aaaaaaaaaaaaaaaaaaa\";"));
6387   EXPECT_EQ("ffff(\n"
6388             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6389             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6390             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6391                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6392                    getGoogleStyle()));
6393 
6394   FormatStyle Style = getLLVMStyleWithColumns(12);
6395   Style.BreakStringLiterals = false;
6396   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
6397 
6398   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
6399   AlignLeft.AlignEscapedNewlinesLeft = true;
6400   EXPECT_EQ("#define A \\\n"
6401             "  \"some \" \\\n"
6402             "  \"text \" \\\n"
6403             "  \"other\";",
6404             format("#define A \"some text other\";", AlignLeft));
6405 }
6406 
6407 TEST_F(FormatTest, FullyRemoveEmptyLines) {
6408   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
6409   NoEmptyLines.MaxEmptyLinesToKeep = 0;
6410   EXPECT_EQ("int i = a(b());",
6411             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
6412 }
6413 
6414 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
6415   EXPECT_EQ(
6416       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6417       "(\n"
6418       "    \"x\t\");",
6419       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6420              "aaaaaaa("
6421              "\"x\t\");"));
6422 }
6423 
6424 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
6425   EXPECT_EQ(
6426       "u8\"utf8 string \"\n"
6427       "u8\"literal\";",
6428       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
6429   EXPECT_EQ(
6430       "u\"utf16 string \"\n"
6431       "u\"literal\";",
6432       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
6433   EXPECT_EQ(
6434       "U\"utf32 string \"\n"
6435       "U\"literal\";",
6436       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
6437   EXPECT_EQ("L\"wide string \"\n"
6438             "L\"literal\";",
6439             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
6440   EXPECT_EQ("@\"NSString \"\n"
6441             "@\"literal\";",
6442             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
6443 
6444   // This input makes clang-format try to split the incomplete unicode escape
6445   // sequence, which used to lead to a crasher.
6446   verifyNoCrash(
6447       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6448       getLLVMStyleWithColumns(60));
6449 }
6450 
6451 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
6452   FormatStyle Style = getGoogleStyleWithColumns(15);
6453   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
6454   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
6455   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
6456   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
6457   EXPECT_EQ("u8R\"x(raw literal)x\";",
6458             format("u8R\"x(raw literal)x\";", Style));
6459 }
6460 
6461 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
6462   FormatStyle Style = getLLVMStyleWithColumns(20);
6463   EXPECT_EQ(
6464       "_T(\"aaaaaaaaaaaaaa\")\n"
6465       "_T(\"aaaaaaaaaaaaaa\")\n"
6466       "_T(\"aaaaaaaaaaaa\")",
6467       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
6468   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
6469             "     _T(\"aaaaaa\"),\n"
6470             "  z);",
6471             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
6472 
6473   // FIXME: Handle embedded spaces in one iteration.
6474   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
6475   //            "_T(\"aaaaaaaaaaaaa\")\n"
6476   //            "_T(\"aaaaaaaaaaaaa\")\n"
6477   //            "_T(\"a\")",
6478   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6479   //                   getLLVMStyleWithColumns(20)));
6480   EXPECT_EQ(
6481       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6482       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
6483   EXPECT_EQ("f(\n"
6484             "#if !TEST\n"
6485             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6486             "#endif\n"
6487             "    );",
6488             format("f(\n"
6489                    "#if !TEST\n"
6490                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6491                    "#endif\n"
6492                    ");"));
6493   EXPECT_EQ("f(\n"
6494             "\n"
6495             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
6496             format("f(\n"
6497                    "\n"
6498                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
6499 }
6500 
6501 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
6502   EXPECT_EQ(
6503       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6504       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6505       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6506       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6507              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6508              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
6509 }
6510 
6511 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
6512   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
6513             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
6514   EXPECT_EQ("fffffffffff(g(R\"x(\n"
6515             "multiline raw string literal xxxxxxxxxxxxxx\n"
6516             ")x\",\n"
6517             "              a),\n"
6518             "            b);",
6519             format("fffffffffff(g(R\"x(\n"
6520                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6521                    ")x\", a), b);",
6522                    getGoogleStyleWithColumns(20)));
6523   EXPECT_EQ("fffffffffff(\n"
6524             "    g(R\"x(qqq\n"
6525             "multiline raw string literal xxxxxxxxxxxxxx\n"
6526             ")x\",\n"
6527             "      a),\n"
6528             "    b);",
6529             format("fffffffffff(g(R\"x(qqq\n"
6530                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6531                    ")x\", a), b);",
6532                    getGoogleStyleWithColumns(20)));
6533 
6534   EXPECT_EQ("fffffffffff(R\"x(\n"
6535             "multiline raw string literal xxxxxxxxxxxxxx\n"
6536             ")x\");",
6537             format("fffffffffff(R\"x(\n"
6538                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6539                    ")x\");",
6540                    getGoogleStyleWithColumns(20)));
6541   EXPECT_EQ("fffffffffff(R\"x(\n"
6542             "multiline raw string literal xxxxxxxxxxxxxx\n"
6543             ")x\" + bbbbbb);",
6544             format("fffffffffff(R\"x(\n"
6545                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6546                    ")x\" +   bbbbbb);",
6547                    getGoogleStyleWithColumns(20)));
6548   EXPECT_EQ("fffffffffff(\n"
6549             "    R\"x(\n"
6550             "multiline raw string literal xxxxxxxxxxxxxx\n"
6551             ")x\" +\n"
6552             "    bbbbbb);",
6553             format("fffffffffff(\n"
6554                    " R\"x(\n"
6555                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6556                    ")x\" + bbbbbb);",
6557                    getGoogleStyleWithColumns(20)));
6558 }
6559 
6560 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
6561   verifyFormat("string a = \"unterminated;");
6562   EXPECT_EQ("function(\"unterminated,\n"
6563             "         OtherParameter);",
6564             format("function(  \"unterminated,\n"
6565                    "    OtherParameter);"));
6566 }
6567 
6568 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
6569   FormatStyle Style = getLLVMStyle();
6570   Style.Standard = FormatStyle::LS_Cpp03;
6571   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
6572             format("#define x(_a) printf(\"foo\"_a);", Style));
6573 }
6574 
6575 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
6576 
6577 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
6578   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
6579             "             \"ddeeefff\");",
6580             format("someFunction(\"aaabbbcccdddeeefff\");",
6581                    getLLVMStyleWithColumns(25)));
6582   EXPECT_EQ("someFunction1234567890(\n"
6583             "    \"aaabbbcccdddeeefff\");",
6584             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6585                    getLLVMStyleWithColumns(26)));
6586   EXPECT_EQ("someFunction1234567890(\n"
6587             "    \"aaabbbcccdddeeeff\"\n"
6588             "    \"f\");",
6589             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6590                    getLLVMStyleWithColumns(25)));
6591   EXPECT_EQ("someFunction1234567890(\n"
6592             "    \"aaabbbcccdddeeeff\"\n"
6593             "    \"f\");",
6594             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6595                    getLLVMStyleWithColumns(24)));
6596   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6597             "             \"ddde \"\n"
6598             "             \"efff\");",
6599             format("someFunction(\"aaabbbcc ddde efff\");",
6600                    getLLVMStyleWithColumns(25)));
6601   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
6602             "             \"ddeeefff\");",
6603             format("someFunction(\"aaabbbccc ddeeefff\");",
6604                    getLLVMStyleWithColumns(25)));
6605   EXPECT_EQ("someFunction1234567890(\n"
6606             "    \"aaabb \"\n"
6607             "    \"cccdddeeefff\");",
6608             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
6609                    getLLVMStyleWithColumns(25)));
6610   EXPECT_EQ("#define A          \\\n"
6611             "  string s =       \\\n"
6612             "      \"123456789\"  \\\n"
6613             "      \"0\";         \\\n"
6614             "  int i;",
6615             format("#define A string s = \"1234567890\"; int i;",
6616                    getLLVMStyleWithColumns(20)));
6617   // FIXME: Put additional penalties on breaking at non-whitespace locations.
6618   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6619             "             \"dddeeeff\"\n"
6620             "             \"f\");",
6621             format("someFunction(\"aaabbbcc dddeeefff\");",
6622                    getLLVMStyleWithColumns(25)));
6623 }
6624 
6625 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
6626   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
6627   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
6628   EXPECT_EQ("\"test\"\n"
6629             "\"\\n\"",
6630             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
6631   EXPECT_EQ("\"tes\\\\\"\n"
6632             "\"n\"",
6633             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
6634   EXPECT_EQ("\"\\\\\\\\\"\n"
6635             "\"\\n\"",
6636             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
6637   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
6638   EXPECT_EQ("\"\\uff01\"\n"
6639             "\"test\"",
6640             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
6641   EXPECT_EQ("\"\\Uff01ff02\"",
6642             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
6643   EXPECT_EQ("\"\\x000000000001\"\n"
6644             "\"next\"",
6645             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
6646   EXPECT_EQ("\"\\x000000000001next\"",
6647             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
6648   EXPECT_EQ("\"\\x000000000001\"",
6649             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
6650   EXPECT_EQ("\"test\"\n"
6651             "\"\\000000\"\n"
6652             "\"000001\"",
6653             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
6654   EXPECT_EQ("\"test\\000\"\n"
6655             "\"00000000\"\n"
6656             "\"1\"",
6657             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
6658 }
6659 
6660 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
6661   verifyFormat("void f() {\n"
6662                "  return g() {}\n"
6663                "  void h() {}");
6664   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
6665                "g();\n"
6666                "}");
6667 }
6668 
6669 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
6670   verifyFormat(
6671       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
6672 }
6673 
6674 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
6675   verifyFormat("class X {\n"
6676                "  void f() {\n"
6677                "  }\n"
6678                "};",
6679                getLLVMStyleWithColumns(12));
6680 }
6681 
6682 TEST_F(FormatTest, ConfigurableIndentWidth) {
6683   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
6684   EightIndent.IndentWidth = 8;
6685   EightIndent.ContinuationIndentWidth = 8;
6686   verifyFormat("void f() {\n"
6687                "        someFunction();\n"
6688                "        if (true) {\n"
6689                "                f();\n"
6690                "        }\n"
6691                "}",
6692                EightIndent);
6693   verifyFormat("class X {\n"
6694                "        void f() {\n"
6695                "        }\n"
6696                "};",
6697                EightIndent);
6698   verifyFormat("int x[] = {\n"
6699                "        call(),\n"
6700                "        call()};",
6701                EightIndent);
6702 }
6703 
6704 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
6705   verifyFormat("double\n"
6706                "f();",
6707                getLLVMStyleWithColumns(8));
6708 }
6709 
6710 TEST_F(FormatTest, ConfigurableUseOfTab) {
6711   FormatStyle Tab = getLLVMStyleWithColumns(42);
6712   Tab.IndentWidth = 8;
6713   Tab.UseTab = FormatStyle::UT_Always;
6714   Tab.AlignEscapedNewlinesLeft = true;
6715 
6716   EXPECT_EQ("if (aaaaaaaa && // q\n"
6717             "    bb)\t\t// w\n"
6718             "\t;",
6719             format("if (aaaaaaaa &&// q\n"
6720                    "bb)// w\n"
6721                    ";",
6722                    Tab));
6723   EXPECT_EQ("if (aaa && bbb) // w\n"
6724             "\t;",
6725             format("if(aaa&&bbb)// w\n"
6726                    ";",
6727                    Tab));
6728 
6729   verifyFormat("class X {\n"
6730                "\tvoid f() {\n"
6731                "\t\tsomeFunction(parameter1,\n"
6732                "\t\t\t     parameter2);\n"
6733                "\t}\n"
6734                "};",
6735                Tab);
6736   verifyFormat("#define A                        \\\n"
6737                "\tvoid f() {               \\\n"
6738                "\t\tsomeFunction(    \\\n"
6739                "\t\t    parameter1,  \\\n"
6740                "\t\t    parameter2); \\\n"
6741                "\t}",
6742                Tab);
6743 
6744   Tab.TabWidth = 4;
6745   Tab.IndentWidth = 8;
6746   verifyFormat("class TabWidth4Indent8 {\n"
6747                "\t\tvoid f() {\n"
6748                "\t\t\t\tsomeFunction(parameter1,\n"
6749                "\t\t\t\t\t\t\t parameter2);\n"
6750                "\t\t}\n"
6751                "};",
6752                Tab);
6753 
6754   Tab.TabWidth = 4;
6755   Tab.IndentWidth = 4;
6756   verifyFormat("class TabWidth4Indent4 {\n"
6757                "\tvoid f() {\n"
6758                "\t\tsomeFunction(parameter1,\n"
6759                "\t\t\t\t\t parameter2);\n"
6760                "\t}\n"
6761                "};",
6762                Tab);
6763 
6764   Tab.TabWidth = 8;
6765   Tab.IndentWidth = 4;
6766   verifyFormat("class TabWidth8Indent4 {\n"
6767                "    void f() {\n"
6768                "\tsomeFunction(parameter1,\n"
6769                "\t\t     parameter2);\n"
6770                "    }\n"
6771                "};",
6772                Tab);
6773 
6774   Tab.TabWidth = 8;
6775   Tab.IndentWidth = 8;
6776   EXPECT_EQ("/*\n"
6777             "\t      a\t\tcomment\n"
6778             "\t      in multiple lines\n"
6779             "       */",
6780             format("   /*\t \t \n"
6781                    " \t \t a\t\tcomment\t \t\n"
6782                    " \t \t in multiple lines\t\n"
6783                    " \t  */",
6784                    Tab));
6785 
6786   Tab.UseTab = FormatStyle::UT_ForIndentation;
6787   verifyFormat("{\n"
6788                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6789                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6790                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6791                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6792                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6793                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6794                "};",
6795                Tab);
6796   verifyFormat("enum AA {\n"
6797                "\ta1, // Force multiple lines\n"
6798                "\ta2,\n"
6799                "\ta3\n"
6800                "};",
6801                Tab);
6802   EXPECT_EQ("if (aaaaaaaa && // q\n"
6803             "    bb)         // w\n"
6804             "\t;",
6805             format("if (aaaaaaaa &&// q\n"
6806                    "bb)// w\n"
6807                    ";",
6808                    Tab));
6809   verifyFormat("class X {\n"
6810                "\tvoid f() {\n"
6811                "\t\tsomeFunction(parameter1,\n"
6812                "\t\t             parameter2);\n"
6813                "\t}\n"
6814                "};",
6815                Tab);
6816   verifyFormat("{\n"
6817                "\tQ(\n"
6818                "\t    {\n"
6819                "\t\t    int a;\n"
6820                "\t\t    someFunction(aaaaaaaa,\n"
6821                "\t\t                 bbbbbbb);\n"
6822                "\t    },\n"
6823                "\t    p);\n"
6824                "}",
6825                Tab);
6826   EXPECT_EQ("{\n"
6827             "\t/* aaaa\n"
6828             "\t   bbbb */\n"
6829             "}",
6830             format("{\n"
6831                    "/* aaaa\n"
6832                    "   bbbb */\n"
6833                    "}",
6834                    Tab));
6835   EXPECT_EQ("{\n"
6836             "\t/*\n"
6837             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6838             "\t  bbbbbbbbbbbbb\n"
6839             "\t*/\n"
6840             "}",
6841             format("{\n"
6842                    "/*\n"
6843                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6844                    "*/\n"
6845                    "}",
6846                    Tab));
6847   EXPECT_EQ("{\n"
6848             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6849             "\t// bbbbbbbbbbbbb\n"
6850             "}",
6851             format("{\n"
6852                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6853                    "}",
6854                    Tab));
6855   EXPECT_EQ("{\n"
6856             "\t/*\n"
6857             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6858             "\t  bbbbbbbbbbbbb\n"
6859             "\t*/\n"
6860             "}",
6861             format("{\n"
6862                    "\t/*\n"
6863                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6864                    "\t*/\n"
6865                    "}",
6866                    Tab));
6867   EXPECT_EQ("{\n"
6868             "\t/*\n"
6869             "\n"
6870             "\t*/\n"
6871             "}",
6872             format("{\n"
6873                    "\t/*\n"
6874                    "\n"
6875                    "\t*/\n"
6876                    "}",
6877                    Tab));
6878   EXPECT_EQ("{\n"
6879             "\t/*\n"
6880             " asdf\n"
6881             "\t*/\n"
6882             "}",
6883             format("{\n"
6884                    "\t/*\n"
6885                    " asdf\n"
6886                    "\t*/\n"
6887                    "}",
6888                    Tab));
6889 
6890   Tab.UseTab = FormatStyle::UT_Never;
6891   EXPECT_EQ("/*\n"
6892             "              a\t\tcomment\n"
6893             "              in multiple lines\n"
6894             "       */",
6895             format("   /*\t \t \n"
6896                    " \t \t a\t\tcomment\t \t\n"
6897                    " \t \t in multiple lines\t\n"
6898                    " \t  */",
6899                    Tab));
6900   EXPECT_EQ("/* some\n"
6901             "   comment */",
6902             format(" \t \t /* some\n"
6903                    " \t \t    comment */",
6904                    Tab));
6905   EXPECT_EQ("int a; /* some\n"
6906             "   comment */",
6907             format(" \t \t int a; /* some\n"
6908                    " \t \t    comment */",
6909                    Tab));
6910 
6911   EXPECT_EQ("int a; /* some\n"
6912             "comment */",
6913             format(" \t \t int\ta; /* some\n"
6914                    " \t \t    comment */",
6915                    Tab));
6916   EXPECT_EQ("f(\"\t\t\"); /* some\n"
6917             "    comment */",
6918             format(" \t \t f(\"\t\t\"); /* some\n"
6919                    " \t \t    comment */",
6920                    Tab));
6921   EXPECT_EQ("{\n"
6922             "  /*\n"
6923             "   * Comment\n"
6924             "   */\n"
6925             "  int i;\n"
6926             "}",
6927             format("{\n"
6928                    "\t/*\n"
6929                    "\t * Comment\n"
6930                    "\t */\n"
6931                    "\t int i;\n"
6932                    "}"));
6933 
6934   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
6935   Tab.TabWidth = 8;
6936   Tab.IndentWidth = 8;
6937   EXPECT_EQ("if (aaaaaaaa && // q\n"
6938             "    bb)         // w\n"
6939             "\t;",
6940             format("if (aaaaaaaa &&// q\n"
6941                    "bb)// w\n"
6942                    ";",
6943                    Tab));
6944   EXPECT_EQ("if (aaa && bbb) // w\n"
6945             "\t;",
6946             format("if(aaa&&bbb)// w\n"
6947                    ";",
6948                    Tab));
6949   verifyFormat("class X {\n"
6950                "\tvoid f() {\n"
6951                "\t\tsomeFunction(parameter1,\n"
6952                "\t\t\t     parameter2);\n"
6953                "\t}\n"
6954                "};",
6955                Tab);
6956   verifyFormat("#define A                        \\\n"
6957                "\tvoid f() {               \\\n"
6958                "\t\tsomeFunction(    \\\n"
6959                "\t\t    parameter1,  \\\n"
6960                "\t\t    parameter2); \\\n"
6961                "\t}",
6962                Tab);
6963   Tab.TabWidth = 4;
6964   Tab.IndentWidth = 8;
6965   verifyFormat("class TabWidth4Indent8 {\n"
6966                "\t\tvoid f() {\n"
6967                "\t\t\t\tsomeFunction(parameter1,\n"
6968                "\t\t\t\t\t\t\t parameter2);\n"
6969                "\t\t}\n"
6970                "};",
6971                Tab);
6972   Tab.TabWidth = 4;
6973   Tab.IndentWidth = 4;
6974   verifyFormat("class TabWidth4Indent4 {\n"
6975                "\tvoid f() {\n"
6976                "\t\tsomeFunction(parameter1,\n"
6977                "\t\t\t\t\t parameter2);\n"
6978                "\t}\n"
6979                "};",
6980                Tab);
6981   Tab.TabWidth = 8;
6982   Tab.IndentWidth = 4;
6983   verifyFormat("class TabWidth8Indent4 {\n"
6984                "    void f() {\n"
6985                "\tsomeFunction(parameter1,\n"
6986                "\t\t     parameter2);\n"
6987                "    }\n"
6988                "};",
6989                Tab);
6990   Tab.TabWidth = 8;
6991   Tab.IndentWidth = 8;
6992   EXPECT_EQ("/*\n"
6993             "\t      a\t\tcomment\n"
6994             "\t      in multiple lines\n"
6995             "       */",
6996             format("   /*\t \t \n"
6997                    " \t \t a\t\tcomment\t \t\n"
6998                    " \t \t in multiple lines\t\n"
6999                    " \t  */",
7000                    Tab));
7001   verifyFormat("{\n"
7002                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7003                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7004                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7005                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7006                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7007                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7008                "};",
7009                Tab);
7010   verifyFormat("enum AA {\n"
7011                "\ta1, // Force multiple lines\n"
7012                "\ta2,\n"
7013                "\ta3\n"
7014                "};",
7015                Tab);
7016   EXPECT_EQ("if (aaaaaaaa && // q\n"
7017             "    bb)         // w\n"
7018             "\t;",
7019             format("if (aaaaaaaa &&// q\n"
7020                    "bb)// w\n"
7021                    ";",
7022                    Tab));
7023   verifyFormat("class X {\n"
7024                "\tvoid f() {\n"
7025                "\t\tsomeFunction(parameter1,\n"
7026                "\t\t\t     parameter2);\n"
7027                "\t}\n"
7028                "};",
7029                Tab);
7030   verifyFormat("{\n"
7031                "\tQ(\n"
7032                "\t    {\n"
7033                "\t\t    int a;\n"
7034                "\t\t    someFunction(aaaaaaaa,\n"
7035                "\t\t\t\t bbbbbbb);\n"
7036                "\t    },\n"
7037                "\t    p);\n"
7038                "}",
7039                Tab);
7040   EXPECT_EQ("{\n"
7041             "\t/* aaaa\n"
7042             "\t   bbbb */\n"
7043             "}",
7044             format("{\n"
7045                    "/* aaaa\n"
7046                    "   bbbb */\n"
7047                    "}",
7048                    Tab));
7049   EXPECT_EQ("{\n"
7050             "\t/*\n"
7051             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7052             "\t  bbbbbbbbbbbbb\n"
7053             "\t*/\n"
7054             "}",
7055             format("{\n"
7056                    "/*\n"
7057                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7058                    "*/\n"
7059                    "}",
7060                    Tab));
7061   EXPECT_EQ("{\n"
7062             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7063             "\t// bbbbbbbbbbbbb\n"
7064             "}",
7065             format("{\n"
7066                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7067                    "}",
7068                    Tab));
7069   EXPECT_EQ("{\n"
7070             "\t/*\n"
7071             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7072             "\t  bbbbbbbbbbbbb\n"
7073             "\t*/\n"
7074             "}",
7075             format("{\n"
7076                    "\t/*\n"
7077                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7078                    "\t*/\n"
7079                    "}",
7080                    Tab));
7081   EXPECT_EQ("{\n"
7082             "\t/*\n"
7083             "\n"
7084             "\t*/\n"
7085             "}",
7086             format("{\n"
7087                    "\t/*\n"
7088                    "\n"
7089                    "\t*/\n"
7090                    "}",
7091                    Tab));
7092   EXPECT_EQ("{\n"
7093             "\t/*\n"
7094             " asdf\n"
7095             "\t*/\n"
7096             "}",
7097             format("{\n"
7098                    "\t/*\n"
7099                    " asdf\n"
7100                    "\t*/\n"
7101                    "}",
7102                    Tab));
7103   EXPECT_EQ("/*\n"
7104             "\t      a\t\tcomment\n"
7105             "\t      in multiple lines\n"
7106             "       */",
7107             format("   /*\t \t \n"
7108                    " \t \t a\t\tcomment\t \t\n"
7109                    " \t \t in multiple lines\t\n"
7110                    " \t  */",
7111                    Tab));
7112   EXPECT_EQ("/* some\n"
7113             "   comment */",
7114             format(" \t \t /* some\n"
7115                    " \t \t    comment */",
7116                    Tab));
7117   EXPECT_EQ("int a; /* some\n"
7118             "   comment */",
7119             format(" \t \t int a; /* some\n"
7120                    " \t \t    comment */",
7121                    Tab));
7122   EXPECT_EQ("int a; /* some\n"
7123             "comment */",
7124             format(" \t \t int\ta; /* some\n"
7125                    " \t \t    comment */",
7126                    Tab));
7127   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7128             "    comment */",
7129             format(" \t \t f(\"\t\t\"); /* some\n"
7130                    " \t \t    comment */",
7131                    Tab));
7132   EXPECT_EQ("{\n"
7133             "  /*\n"
7134             "   * Comment\n"
7135             "   */\n"
7136             "  int i;\n"
7137             "}",
7138             format("{\n"
7139                    "\t/*\n"
7140                    "\t * Comment\n"
7141                    "\t */\n"
7142                    "\t int i;\n"
7143                    "}"));
7144   Tab.AlignConsecutiveAssignments = true;
7145   Tab.AlignConsecutiveDeclarations = true;
7146   Tab.TabWidth = 4;
7147   Tab.IndentWidth = 4;
7148   verifyFormat("class Assign {\n"
7149                "\tvoid f() {\n"
7150                "\t\tint         x      = 123;\n"
7151                "\t\tint         random = 4;\n"
7152                "\t\tstd::string alphabet =\n"
7153                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
7154                "\t}\n"
7155                "};",
7156                Tab);
7157 }
7158 
7159 TEST_F(FormatTest, CalculatesOriginalColumn) {
7160   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7161             "q\"; /* some\n"
7162             "       comment */",
7163             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7164                    "q\"; /* some\n"
7165                    "       comment */",
7166                    getLLVMStyle()));
7167   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7168             "/* some\n"
7169             "   comment */",
7170             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7171                    " /* some\n"
7172                    "    comment */",
7173                    getLLVMStyle()));
7174   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7175             "qqq\n"
7176             "/* some\n"
7177             "   comment */",
7178             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7179                    "qqq\n"
7180                    " /* some\n"
7181                    "    comment */",
7182                    getLLVMStyle()));
7183   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7184             "wwww; /* some\n"
7185             "         comment */",
7186             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7187                    "wwww; /* some\n"
7188                    "         comment */",
7189                    getLLVMStyle()));
7190 }
7191 
7192 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
7193   FormatStyle NoSpace = getLLVMStyle();
7194   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
7195 
7196   verifyFormat("while(true)\n"
7197                "  continue;",
7198                NoSpace);
7199   verifyFormat("for(;;)\n"
7200                "  continue;",
7201                NoSpace);
7202   verifyFormat("if(true)\n"
7203                "  f();\n"
7204                "else if(true)\n"
7205                "  f();",
7206                NoSpace);
7207   verifyFormat("do {\n"
7208                "  do_something();\n"
7209                "} while(something());",
7210                NoSpace);
7211   verifyFormat("switch(x) {\n"
7212                "default:\n"
7213                "  break;\n"
7214                "}",
7215                NoSpace);
7216   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
7217   verifyFormat("size_t x = sizeof(x);", NoSpace);
7218   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
7219   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
7220   verifyFormat("alignas(128) char a[128];", NoSpace);
7221   verifyFormat("size_t x = alignof(MyType);", NoSpace);
7222   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
7223   verifyFormat("int f() throw(Deprecated);", NoSpace);
7224   verifyFormat("typedef void (*cb)(int);", NoSpace);
7225   verifyFormat("T A::operator()();", NoSpace);
7226   verifyFormat("X A::operator++(T);", NoSpace);
7227 
7228   FormatStyle Space = getLLVMStyle();
7229   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
7230 
7231   verifyFormat("int f ();", Space);
7232   verifyFormat("void f (int a, T b) {\n"
7233                "  while (true)\n"
7234                "    continue;\n"
7235                "}",
7236                Space);
7237   verifyFormat("if (true)\n"
7238                "  f ();\n"
7239                "else if (true)\n"
7240                "  f ();",
7241                Space);
7242   verifyFormat("do {\n"
7243                "  do_something ();\n"
7244                "} while (something ());",
7245                Space);
7246   verifyFormat("switch (x) {\n"
7247                "default:\n"
7248                "  break;\n"
7249                "}",
7250                Space);
7251   verifyFormat("A::A () : a (1) {}", Space);
7252   verifyFormat("void f () __attribute__ ((asdf));", Space);
7253   verifyFormat("*(&a + 1);\n"
7254                "&((&a)[1]);\n"
7255                "a[(b + c) * d];\n"
7256                "(((a + 1) * 2) + 3) * 4;",
7257                Space);
7258   verifyFormat("#define A(x) x", Space);
7259   verifyFormat("#define A (x) x", Space);
7260   verifyFormat("#if defined(x)\n"
7261                "#endif",
7262                Space);
7263   verifyFormat("auto i = std::make_unique<int> (5);", Space);
7264   verifyFormat("size_t x = sizeof (x);", Space);
7265   verifyFormat("auto f (int x) -> decltype (x);", Space);
7266   verifyFormat("int f (T x) noexcept (x.create ());", Space);
7267   verifyFormat("alignas (128) char a[128];", Space);
7268   verifyFormat("size_t x = alignof (MyType);", Space);
7269   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
7270   verifyFormat("int f () throw (Deprecated);", Space);
7271   verifyFormat("typedef void (*cb) (int);", Space);
7272   verifyFormat("T A::operator() ();", Space);
7273   verifyFormat("X A::operator++ (T);", Space);
7274 }
7275 
7276 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
7277   FormatStyle Spaces = getLLVMStyle();
7278 
7279   Spaces.SpacesInParentheses = true;
7280   verifyFormat("call( x, y, z );", Spaces);
7281   verifyFormat("call();", Spaces);
7282   verifyFormat("std::function<void( int, int )> callback;", Spaces);
7283   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
7284                Spaces);
7285   verifyFormat("while ( (bool)1 )\n"
7286                "  continue;",
7287                Spaces);
7288   verifyFormat("for ( ;; )\n"
7289                "  continue;",
7290                Spaces);
7291   verifyFormat("if ( true )\n"
7292                "  f();\n"
7293                "else if ( true )\n"
7294                "  f();",
7295                Spaces);
7296   verifyFormat("do {\n"
7297                "  do_something( (int)i );\n"
7298                "} while ( something() );",
7299                Spaces);
7300   verifyFormat("switch ( x ) {\n"
7301                "default:\n"
7302                "  break;\n"
7303                "}",
7304                Spaces);
7305 
7306   Spaces.SpacesInParentheses = false;
7307   Spaces.SpacesInCStyleCastParentheses = true;
7308   verifyFormat("Type *A = ( Type * )P;", Spaces);
7309   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
7310   verifyFormat("x = ( int32 )y;", Spaces);
7311   verifyFormat("int a = ( int )(2.0f);", Spaces);
7312   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
7313   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
7314   verifyFormat("#define x (( int )-1)", Spaces);
7315 
7316   // Run the first set of tests again with:
7317   Spaces.SpacesInParentheses = false;
7318   Spaces.SpaceInEmptyParentheses = true;
7319   Spaces.SpacesInCStyleCastParentheses = true;
7320   verifyFormat("call(x, y, z);", Spaces);
7321   verifyFormat("call( );", Spaces);
7322   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7323   verifyFormat("while (( bool )1)\n"
7324                "  continue;",
7325                Spaces);
7326   verifyFormat("for (;;)\n"
7327                "  continue;",
7328                Spaces);
7329   verifyFormat("if (true)\n"
7330                "  f( );\n"
7331                "else if (true)\n"
7332                "  f( );",
7333                Spaces);
7334   verifyFormat("do {\n"
7335                "  do_something(( int )i);\n"
7336                "} while (something( ));",
7337                Spaces);
7338   verifyFormat("switch (x) {\n"
7339                "default:\n"
7340                "  break;\n"
7341                "}",
7342                Spaces);
7343 
7344   // Run the first set of tests again with:
7345   Spaces.SpaceAfterCStyleCast = true;
7346   verifyFormat("call(x, y, z);", Spaces);
7347   verifyFormat("call( );", Spaces);
7348   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7349   verifyFormat("while (( bool ) 1)\n"
7350                "  continue;",
7351                Spaces);
7352   verifyFormat("for (;;)\n"
7353                "  continue;",
7354                Spaces);
7355   verifyFormat("if (true)\n"
7356                "  f( );\n"
7357                "else if (true)\n"
7358                "  f( );",
7359                Spaces);
7360   verifyFormat("do {\n"
7361                "  do_something(( int ) i);\n"
7362                "} while (something( ));",
7363                Spaces);
7364   verifyFormat("switch (x) {\n"
7365                "default:\n"
7366                "  break;\n"
7367                "}",
7368                Spaces);
7369 
7370   // Run subset of tests again with:
7371   Spaces.SpacesInCStyleCastParentheses = false;
7372   Spaces.SpaceAfterCStyleCast = true;
7373   verifyFormat("while ((bool) 1)\n"
7374                "  continue;",
7375                Spaces);
7376   verifyFormat("do {\n"
7377                "  do_something((int) i);\n"
7378                "} while (something( ));",
7379                Spaces);
7380 }
7381 
7382 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
7383   verifyFormat("int a[5];");
7384   verifyFormat("a[3] += 42;");
7385 
7386   FormatStyle Spaces = getLLVMStyle();
7387   Spaces.SpacesInSquareBrackets = true;
7388   // Lambdas unchanged.
7389   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
7390   verifyFormat("return [i, args...] {};", Spaces);
7391 
7392   // Not lambdas.
7393   verifyFormat("int a[ 5 ];", Spaces);
7394   verifyFormat("a[ 3 ] += 42;", Spaces);
7395   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
7396   verifyFormat("double &operator[](int i) { return 0; }\n"
7397                "int i;",
7398                Spaces);
7399   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
7400   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
7401   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
7402 }
7403 
7404 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
7405   verifyFormat("int a = 5;");
7406   verifyFormat("a += 42;");
7407   verifyFormat("a or_eq 8;");
7408 
7409   FormatStyle Spaces = getLLVMStyle();
7410   Spaces.SpaceBeforeAssignmentOperators = false;
7411   verifyFormat("int a= 5;", Spaces);
7412   verifyFormat("a+= 42;", Spaces);
7413   verifyFormat("a or_eq 8;", Spaces);
7414 }
7415 
7416 TEST_F(FormatTest, AlignConsecutiveAssignments) {
7417   FormatStyle Alignment = getLLVMStyle();
7418   Alignment.AlignConsecutiveAssignments = false;
7419   verifyFormat("int a = 5;\n"
7420                "int oneTwoThree = 123;",
7421                Alignment);
7422   verifyFormat("int a = 5;\n"
7423                "int oneTwoThree = 123;",
7424                Alignment);
7425 
7426   Alignment.AlignConsecutiveAssignments = true;
7427   verifyFormat("int a           = 5;\n"
7428                "int oneTwoThree = 123;",
7429                Alignment);
7430   verifyFormat("int a           = method();\n"
7431                "int oneTwoThree = 133;",
7432                Alignment);
7433   verifyFormat("a &= 5;\n"
7434                "bcd *= 5;\n"
7435                "ghtyf += 5;\n"
7436                "dvfvdb -= 5;\n"
7437                "a /= 5;\n"
7438                "vdsvsv %= 5;\n"
7439                "sfdbddfbdfbb ^= 5;\n"
7440                "dvsdsv |= 5;\n"
7441                "int dsvvdvsdvvv = 123;",
7442                Alignment);
7443   verifyFormat("int i = 1, j = 10;\n"
7444                "something = 2000;",
7445                Alignment);
7446   verifyFormat("something = 2000;\n"
7447                "int i = 1, j = 10;\n",
7448                Alignment);
7449   verifyFormat("something = 2000;\n"
7450                "another   = 911;\n"
7451                "int i = 1, j = 10;\n"
7452                "oneMore = 1;\n"
7453                "i       = 2;",
7454                Alignment);
7455   verifyFormat("int a   = 5;\n"
7456                "int one = 1;\n"
7457                "method();\n"
7458                "int oneTwoThree = 123;\n"
7459                "int oneTwo      = 12;",
7460                Alignment);
7461   verifyFormat("int oneTwoThree = 123;\n"
7462                "int oneTwo      = 12;\n"
7463                "method();\n",
7464                Alignment);
7465   verifyFormat("int oneTwoThree = 123; // comment\n"
7466                "int oneTwo      = 12;  // comment",
7467                Alignment);
7468   EXPECT_EQ("int a = 5;\n"
7469             "\n"
7470             "int oneTwoThree = 123;",
7471             format("int a       = 5;\n"
7472                    "\n"
7473                    "int oneTwoThree= 123;",
7474                    Alignment));
7475   EXPECT_EQ("int a   = 5;\n"
7476             "int one = 1;\n"
7477             "\n"
7478             "int oneTwoThree = 123;",
7479             format("int a = 5;\n"
7480                    "int one = 1;\n"
7481                    "\n"
7482                    "int oneTwoThree = 123;",
7483                    Alignment));
7484   EXPECT_EQ("int a   = 5;\n"
7485             "int one = 1;\n"
7486             "\n"
7487             "int oneTwoThree = 123;\n"
7488             "int oneTwo      = 12;",
7489             format("int a = 5;\n"
7490                    "int one = 1;\n"
7491                    "\n"
7492                    "int oneTwoThree = 123;\n"
7493                    "int oneTwo = 12;",
7494                    Alignment));
7495   Alignment.AlignEscapedNewlinesLeft = true;
7496   verifyFormat("#define A               \\\n"
7497                "  int aaaa       = 12;  \\\n"
7498                "  int b          = 23;  \\\n"
7499                "  int ccc        = 234; \\\n"
7500                "  int dddddddddd = 2345;",
7501                Alignment);
7502   Alignment.AlignEscapedNewlinesLeft = false;
7503   verifyFormat("#define A                                                      "
7504                "                \\\n"
7505                "  int aaaa       = 12;                                         "
7506                "                \\\n"
7507                "  int b          = 23;                                         "
7508                "                \\\n"
7509                "  int ccc        = 234;                                        "
7510                "                \\\n"
7511                "  int dddddddddd = 2345;",
7512                Alignment);
7513   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
7514                "k = 4, int l = 5,\n"
7515                "                  int m = 6) {\n"
7516                "  int j      = 10;\n"
7517                "  otherThing = 1;\n"
7518                "}",
7519                Alignment);
7520   verifyFormat("void SomeFunction(int parameter = 0) {\n"
7521                "  int i   = 1;\n"
7522                "  int j   = 2;\n"
7523                "  int big = 10000;\n"
7524                "}",
7525                Alignment);
7526   verifyFormat("class C {\n"
7527                "public:\n"
7528                "  int i            = 1;\n"
7529                "  virtual void f() = 0;\n"
7530                "};",
7531                Alignment);
7532   verifyFormat("int i = 1;\n"
7533                "if (SomeType t = getSomething()) {\n"
7534                "}\n"
7535                "int j   = 2;\n"
7536                "int big = 10000;",
7537                Alignment);
7538   verifyFormat("int j = 7;\n"
7539                "for (int k = 0; k < N; ++k) {\n"
7540                "}\n"
7541                "int j   = 2;\n"
7542                "int big = 10000;\n"
7543                "}",
7544                Alignment);
7545   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7546   verifyFormat("int i = 1;\n"
7547                "LooooooooooongType loooooooooooooooooooooongVariable\n"
7548                "    = someLooooooooooooooooongFunction();\n"
7549                "int j = 2;",
7550                Alignment);
7551   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7552   verifyFormat("int i = 1;\n"
7553                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
7554                "    someLooooooooooooooooongFunction();\n"
7555                "int j = 2;",
7556                Alignment);
7557 
7558   verifyFormat("auto lambda = []() {\n"
7559                "  auto i = 0;\n"
7560                "  return 0;\n"
7561                "};\n"
7562                "int i  = 0;\n"
7563                "auto v = type{\n"
7564                "    i = 1,   //\n"
7565                "    (i = 2), //\n"
7566                "    i = 3    //\n"
7567                "};",
7568                Alignment);
7569 
7570   // FIXME: Should align all three assignments
7571   verifyFormat(
7572       "int i      = 1;\n"
7573       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
7574       "                          loooooooooooooooooooooongParameterB);\n"
7575       "int j = 2;",
7576       Alignment);
7577 
7578   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
7579                "          typename B   = very_long_type_name_1,\n"
7580                "          typename T_2 = very_long_type_name_2>\n"
7581                "auto foo() {}\n",
7582                Alignment);
7583   verifyFormat("int a, b = 1;\n"
7584                "int c  = 2;\n"
7585                "int dd = 3;\n",
7586                Alignment);
7587   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
7588                "float b[1][] = {{3.f}};\n",
7589                Alignment);
7590 }
7591 
7592 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
7593   FormatStyle Alignment = getLLVMStyle();
7594   Alignment.AlignConsecutiveDeclarations = false;
7595   verifyFormat("float const a = 5;\n"
7596                "int oneTwoThree = 123;",
7597                Alignment);
7598   verifyFormat("int a = 5;\n"
7599                "float const oneTwoThree = 123;",
7600                Alignment);
7601 
7602   Alignment.AlignConsecutiveDeclarations = true;
7603   verifyFormat("float const a = 5;\n"
7604                "int         oneTwoThree = 123;",
7605                Alignment);
7606   verifyFormat("int         a = method();\n"
7607                "float const oneTwoThree = 133;",
7608                Alignment);
7609   verifyFormat("int i = 1, j = 10;\n"
7610                "something = 2000;",
7611                Alignment);
7612   verifyFormat("something = 2000;\n"
7613                "int i = 1, j = 10;\n",
7614                Alignment);
7615   verifyFormat("float      something = 2000;\n"
7616                "double     another = 911;\n"
7617                "int        i = 1, j = 10;\n"
7618                "const int *oneMore = 1;\n"
7619                "unsigned   i = 2;",
7620                Alignment);
7621   verifyFormat("float a = 5;\n"
7622                "int   one = 1;\n"
7623                "method();\n"
7624                "const double       oneTwoThree = 123;\n"
7625                "const unsigned int oneTwo = 12;",
7626                Alignment);
7627   verifyFormat("int      oneTwoThree{0}; // comment\n"
7628                "unsigned oneTwo;         // comment",
7629                Alignment);
7630   EXPECT_EQ("float const a = 5;\n"
7631             "\n"
7632             "int oneTwoThree = 123;",
7633             format("float const   a = 5;\n"
7634                    "\n"
7635                    "int           oneTwoThree= 123;",
7636                    Alignment));
7637   EXPECT_EQ("float a = 5;\n"
7638             "int   one = 1;\n"
7639             "\n"
7640             "unsigned oneTwoThree = 123;",
7641             format("float    a = 5;\n"
7642                    "int      one = 1;\n"
7643                    "\n"
7644                    "unsigned oneTwoThree = 123;",
7645                    Alignment));
7646   EXPECT_EQ("float a = 5;\n"
7647             "int   one = 1;\n"
7648             "\n"
7649             "unsigned oneTwoThree = 123;\n"
7650             "int      oneTwo = 12;",
7651             format("float    a = 5;\n"
7652                    "int one = 1;\n"
7653                    "\n"
7654                    "unsigned oneTwoThree = 123;\n"
7655                    "int oneTwo = 12;",
7656                    Alignment));
7657   Alignment.AlignConsecutiveAssignments = true;
7658   verifyFormat("float      something = 2000;\n"
7659                "double     another   = 911;\n"
7660                "int        i = 1, j = 10;\n"
7661                "const int *oneMore = 1;\n"
7662                "unsigned   i       = 2;",
7663                Alignment);
7664   verifyFormat("int      oneTwoThree = {0}; // comment\n"
7665                "unsigned oneTwo      = 0;   // comment",
7666                Alignment);
7667   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
7668             "  int const i   = 1;\n"
7669             "  int *     j   = 2;\n"
7670             "  int       big = 10000;\n"
7671             "\n"
7672             "  unsigned oneTwoThree = 123;\n"
7673             "  int      oneTwo      = 12;\n"
7674             "  method();\n"
7675             "  float k  = 2;\n"
7676             "  int   ll = 10000;\n"
7677             "}",
7678             format("void SomeFunction(int parameter= 0) {\n"
7679                    " int const  i= 1;\n"
7680                    "  int *j=2;\n"
7681                    " int big  =  10000;\n"
7682                    "\n"
7683                    "unsigned oneTwoThree  =123;\n"
7684                    "int oneTwo = 12;\n"
7685                    "  method();\n"
7686                    "float k= 2;\n"
7687                    "int ll=10000;\n"
7688                    "}",
7689                    Alignment));
7690   Alignment.AlignConsecutiveAssignments = false;
7691   Alignment.AlignEscapedNewlinesLeft = true;
7692   verifyFormat("#define A              \\\n"
7693                "  int       aaaa = 12; \\\n"
7694                "  float     b = 23;    \\\n"
7695                "  const int ccc = 234; \\\n"
7696                "  unsigned  dddddddddd = 2345;",
7697                Alignment);
7698   Alignment.AlignEscapedNewlinesLeft = false;
7699   Alignment.ColumnLimit = 30;
7700   verifyFormat("#define A                    \\\n"
7701                "  int       aaaa = 12;       \\\n"
7702                "  float     b = 23;          \\\n"
7703                "  const int ccc = 234;       \\\n"
7704                "  int       dddddddddd = 2345;",
7705                Alignment);
7706   Alignment.ColumnLimit = 80;
7707   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
7708                "k = 4, int l = 5,\n"
7709                "                  int m = 6) {\n"
7710                "  const int j = 10;\n"
7711                "  otherThing = 1;\n"
7712                "}",
7713                Alignment);
7714   verifyFormat("void SomeFunction(int parameter = 0) {\n"
7715                "  int const i = 1;\n"
7716                "  int *     j = 2;\n"
7717                "  int       big = 10000;\n"
7718                "}",
7719                Alignment);
7720   verifyFormat("class C {\n"
7721                "public:\n"
7722                "  int          i = 1;\n"
7723                "  virtual void f() = 0;\n"
7724                "};",
7725                Alignment);
7726   verifyFormat("float i = 1;\n"
7727                "if (SomeType t = getSomething()) {\n"
7728                "}\n"
7729                "const unsigned j = 2;\n"
7730                "int            big = 10000;",
7731                Alignment);
7732   verifyFormat("float j = 7;\n"
7733                "for (int k = 0; k < N; ++k) {\n"
7734                "}\n"
7735                "unsigned j = 2;\n"
7736                "int      big = 10000;\n"
7737                "}",
7738                Alignment);
7739   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7740   verifyFormat("float              i = 1;\n"
7741                "LooooooooooongType loooooooooooooooooooooongVariable\n"
7742                "    = someLooooooooooooooooongFunction();\n"
7743                "int j = 2;",
7744                Alignment);
7745   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7746   verifyFormat("int                i = 1;\n"
7747                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
7748                "    someLooooooooooooooooongFunction();\n"
7749                "int j = 2;",
7750                Alignment);
7751 
7752   Alignment.AlignConsecutiveAssignments = true;
7753   verifyFormat("auto lambda = []() {\n"
7754                "  auto  ii = 0;\n"
7755                "  float j  = 0;\n"
7756                "  return 0;\n"
7757                "};\n"
7758                "int   i  = 0;\n"
7759                "float i2 = 0;\n"
7760                "auto  v  = type{\n"
7761                "    i = 1,   //\n"
7762                "    (i = 2), //\n"
7763                "    i = 3    //\n"
7764                "};",
7765                Alignment);
7766   Alignment.AlignConsecutiveAssignments = false;
7767 
7768   // FIXME: Should align all three declarations
7769   verifyFormat(
7770       "int      i = 1;\n"
7771       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
7772       "                          loooooooooooooooooooooongParameterB);\n"
7773       "int j = 2;",
7774       Alignment);
7775 
7776   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
7777   // We expect declarations and assignments to align, as long as it doesn't
7778   // exceed the column limit, starting a new alignemnt sequence whenever it
7779   // happens.
7780   Alignment.AlignConsecutiveAssignments = true;
7781   Alignment.ColumnLimit = 30;
7782   verifyFormat("float    ii              = 1;\n"
7783                "unsigned j               = 2;\n"
7784                "int someVerylongVariable = 1;\n"
7785                "AnotherLongType  ll = 123456;\n"
7786                "VeryVeryLongType k  = 2;\n"
7787                "int              myvar = 1;",
7788                Alignment);
7789   Alignment.ColumnLimit = 80;
7790   Alignment.AlignConsecutiveAssignments = false;
7791 
7792   verifyFormat(
7793       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
7794       "          typename LongType, typename B>\n"
7795       "auto foo() {}\n",
7796       Alignment);
7797   verifyFormat("float a, b = 1;\n"
7798                "int   c = 2;\n"
7799                "int   dd = 3;\n",
7800                Alignment);
7801   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
7802                "float b[1][] = {{3.f}};\n",
7803                Alignment);
7804   Alignment.AlignConsecutiveAssignments = true;
7805   verifyFormat("float a, b = 1;\n"
7806                "int   c  = 2;\n"
7807                "int   dd = 3;\n",
7808                Alignment);
7809   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
7810                "float b[1][] = {{3.f}};\n",
7811                Alignment);
7812   Alignment.AlignConsecutiveAssignments = false;
7813 
7814   Alignment.ColumnLimit = 30;
7815   Alignment.BinPackParameters = false;
7816   verifyFormat("void foo(float     a,\n"
7817                "         float     b,\n"
7818                "         int       c,\n"
7819                "         uint32_t *d) {\n"
7820                "  int *  e = 0;\n"
7821                "  float  f = 0;\n"
7822                "  double g = 0;\n"
7823                "}\n"
7824                "void bar(ino_t     a,\n"
7825                "         int       b,\n"
7826                "         uint32_t *c,\n"
7827                "         bool      d) {}\n",
7828                Alignment);
7829   Alignment.BinPackParameters = true;
7830   Alignment.ColumnLimit = 80;
7831 }
7832 
7833 TEST_F(FormatTest, LinuxBraceBreaking) {
7834   FormatStyle LinuxBraceStyle = getLLVMStyle();
7835   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
7836   verifyFormat("namespace a\n"
7837                "{\n"
7838                "class A\n"
7839                "{\n"
7840                "  void f()\n"
7841                "  {\n"
7842                "    if (true) {\n"
7843                "      a();\n"
7844                "      b();\n"
7845                "    } else {\n"
7846                "      a();\n"
7847                "    }\n"
7848                "  }\n"
7849                "  void g() { return; }\n"
7850                "};\n"
7851                "struct B {\n"
7852                "  int x;\n"
7853                "};\n"
7854                "}\n",
7855                LinuxBraceStyle);
7856   verifyFormat("enum X {\n"
7857                "  Y = 0,\n"
7858                "}\n",
7859                LinuxBraceStyle);
7860   verifyFormat("struct S {\n"
7861                "  int Type;\n"
7862                "  union {\n"
7863                "    int x;\n"
7864                "    double y;\n"
7865                "  } Value;\n"
7866                "  class C\n"
7867                "  {\n"
7868                "    MyFavoriteType Value;\n"
7869                "  } Class;\n"
7870                "}\n",
7871                LinuxBraceStyle);
7872 }
7873 
7874 TEST_F(FormatTest, MozillaBraceBreaking) {
7875   FormatStyle MozillaBraceStyle = getLLVMStyle();
7876   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
7877   MozillaBraceStyle.FixNamespaceComments = false;
7878   verifyFormat("namespace a {\n"
7879                "class A\n"
7880                "{\n"
7881                "  void f()\n"
7882                "  {\n"
7883                "    if (true) {\n"
7884                "      a();\n"
7885                "      b();\n"
7886                "    }\n"
7887                "  }\n"
7888                "  void g() { return; }\n"
7889                "};\n"
7890                "enum E\n"
7891                "{\n"
7892                "  A,\n"
7893                "  // foo\n"
7894                "  B,\n"
7895                "  C\n"
7896                "};\n"
7897                "struct B\n"
7898                "{\n"
7899                "  int x;\n"
7900                "};\n"
7901                "}\n",
7902                MozillaBraceStyle);
7903   verifyFormat("struct S\n"
7904                "{\n"
7905                "  int Type;\n"
7906                "  union\n"
7907                "  {\n"
7908                "    int x;\n"
7909                "    double y;\n"
7910                "  } Value;\n"
7911                "  class C\n"
7912                "  {\n"
7913                "    MyFavoriteType Value;\n"
7914                "  } Class;\n"
7915                "}\n",
7916                MozillaBraceStyle);
7917 }
7918 
7919 TEST_F(FormatTest, StroustrupBraceBreaking) {
7920   FormatStyle StroustrupBraceStyle = getLLVMStyle();
7921   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7922   verifyFormat("namespace a {\n"
7923                "class A {\n"
7924                "  void f()\n"
7925                "  {\n"
7926                "    if (true) {\n"
7927                "      a();\n"
7928                "      b();\n"
7929                "    }\n"
7930                "  }\n"
7931                "  void g() { return; }\n"
7932                "};\n"
7933                "struct B {\n"
7934                "  int x;\n"
7935                "};\n"
7936                "} // namespace a\n",
7937                StroustrupBraceStyle);
7938 
7939   verifyFormat("void foo()\n"
7940                "{\n"
7941                "  if (a) {\n"
7942                "    a();\n"
7943                "  }\n"
7944                "  else {\n"
7945                "    b();\n"
7946                "  }\n"
7947                "}\n",
7948                StroustrupBraceStyle);
7949 
7950   verifyFormat("#ifdef _DEBUG\n"
7951                "int foo(int i = 0)\n"
7952                "#else\n"
7953                "int foo(int i = 5)\n"
7954                "#endif\n"
7955                "{\n"
7956                "  return i;\n"
7957                "}",
7958                StroustrupBraceStyle);
7959 
7960   verifyFormat("void foo() {}\n"
7961                "void bar()\n"
7962                "#ifdef _DEBUG\n"
7963                "{\n"
7964                "  foo();\n"
7965                "}\n"
7966                "#else\n"
7967                "{\n"
7968                "}\n"
7969                "#endif",
7970                StroustrupBraceStyle);
7971 
7972   verifyFormat("void foobar() { int i = 5; }\n"
7973                "#ifdef _DEBUG\n"
7974                "void bar() {}\n"
7975                "#else\n"
7976                "void bar() { foobar(); }\n"
7977                "#endif",
7978                StroustrupBraceStyle);
7979 }
7980 
7981 TEST_F(FormatTest, AllmanBraceBreaking) {
7982   FormatStyle AllmanBraceStyle = getLLVMStyle();
7983   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
7984   verifyFormat("namespace a\n"
7985                "{\n"
7986                "class A\n"
7987                "{\n"
7988                "  void f()\n"
7989                "  {\n"
7990                "    if (true)\n"
7991                "    {\n"
7992                "      a();\n"
7993                "      b();\n"
7994                "    }\n"
7995                "  }\n"
7996                "  void g() { return; }\n"
7997                "};\n"
7998                "struct B\n"
7999                "{\n"
8000                "  int x;\n"
8001                "};\n"
8002                "}",
8003                AllmanBraceStyle);
8004 
8005   verifyFormat("void f()\n"
8006                "{\n"
8007                "  if (true)\n"
8008                "  {\n"
8009                "    a();\n"
8010                "  }\n"
8011                "  else if (false)\n"
8012                "  {\n"
8013                "    b();\n"
8014                "  }\n"
8015                "  else\n"
8016                "  {\n"
8017                "    c();\n"
8018                "  }\n"
8019                "}\n",
8020                AllmanBraceStyle);
8021 
8022   verifyFormat("void f()\n"
8023                "{\n"
8024                "  for (int i = 0; i < 10; ++i)\n"
8025                "  {\n"
8026                "    a();\n"
8027                "  }\n"
8028                "  while (false)\n"
8029                "  {\n"
8030                "    b();\n"
8031                "  }\n"
8032                "  do\n"
8033                "  {\n"
8034                "    c();\n"
8035                "  } while (false)\n"
8036                "}\n",
8037                AllmanBraceStyle);
8038 
8039   verifyFormat("void f(int a)\n"
8040                "{\n"
8041                "  switch (a)\n"
8042                "  {\n"
8043                "  case 0:\n"
8044                "    break;\n"
8045                "  case 1:\n"
8046                "  {\n"
8047                "    break;\n"
8048                "  }\n"
8049                "  case 2:\n"
8050                "  {\n"
8051                "  }\n"
8052                "  break;\n"
8053                "  default:\n"
8054                "    break;\n"
8055                "  }\n"
8056                "}\n",
8057                AllmanBraceStyle);
8058 
8059   verifyFormat("enum X\n"
8060                "{\n"
8061                "  Y = 0,\n"
8062                "}\n",
8063                AllmanBraceStyle);
8064   verifyFormat("enum X\n"
8065                "{\n"
8066                "  Y = 0\n"
8067                "}\n",
8068                AllmanBraceStyle);
8069 
8070   verifyFormat("@interface BSApplicationController ()\n"
8071                "{\n"
8072                "@private\n"
8073                "  id _extraIvar;\n"
8074                "}\n"
8075                "@end\n",
8076                AllmanBraceStyle);
8077 
8078   verifyFormat("#ifdef _DEBUG\n"
8079                "int foo(int i = 0)\n"
8080                "#else\n"
8081                "int foo(int i = 5)\n"
8082                "#endif\n"
8083                "{\n"
8084                "  return i;\n"
8085                "}",
8086                AllmanBraceStyle);
8087 
8088   verifyFormat("void foo() {}\n"
8089                "void bar()\n"
8090                "#ifdef _DEBUG\n"
8091                "{\n"
8092                "  foo();\n"
8093                "}\n"
8094                "#else\n"
8095                "{\n"
8096                "}\n"
8097                "#endif",
8098                AllmanBraceStyle);
8099 
8100   verifyFormat("void foobar() { int i = 5; }\n"
8101                "#ifdef _DEBUG\n"
8102                "void bar() {}\n"
8103                "#else\n"
8104                "void bar() { foobar(); }\n"
8105                "#endif",
8106                AllmanBraceStyle);
8107 
8108   // This shouldn't affect ObjC blocks..
8109   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8110                "  // ...\n"
8111                "  int i;\n"
8112                "}];",
8113                AllmanBraceStyle);
8114   verifyFormat("void (^block)(void) = ^{\n"
8115                "  // ...\n"
8116                "  int i;\n"
8117                "};",
8118                AllmanBraceStyle);
8119   // .. or dict literals.
8120   verifyFormat("void f()\n"
8121                "{\n"
8122                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8123                "}",
8124                AllmanBraceStyle);
8125   verifyFormat("int f()\n"
8126                "{ // comment\n"
8127                "  return 42;\n"
8128                "}",
8129                AllmanBraceStyle);
8130 
8131   AllmanBraceStyle.ColumnLimit = 19;
8132   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8133   AllmanBraceStyle.ColumnLimit = 18;
8134   verifyFormat("void f()\n"
8135                "{\n"
8136                "  int i;\n"
8137                "}",
8138                AllmanBraceStyle);
8139   AllmanBraceStyle.ColumnLimit = 80;
8140 
8141   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8142   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8143   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8144   verifyFormat("void f(bool b)\n"
8145                "{\n"
8146                "  if (b)\n"
8147                "  {\n"
8148                "    return;\n"
8149                "  }\n"
8150                "}\n",
8151                BreakBeforeBraceShortIfs);
8152   verifyFormat("void f(bool b)\n"
8153                "{\n"
8154                "  if (b) return;\n"
8155                "}\n",
8156                BreakBeforeBraceShortIfs);
8157   verifyFormat("void f(bool b)\n"
8158                "{\n"
8159                "  while (b)\n"
8160                "  {\n"
8161                "    return;\n"
8162                "  }\n"
8163                "}\n",
8164                BreakBeforeBraceShortIfs);
8165 }
8166 
8167 TEST_F(FormatTest, GNUBraceBreaking) {
8168   FormatStyle GNUBraceStyle = getLLVMStyle();
8169   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8170   verifyFormat("namespace a\n"
8171                "{\n"
8172                "class A\n"
8173                "{\n"
8174                "  void f()\n"
8175                "  {\n"
8176                "    int a;\n"
8177                "    {\n"
8178                "      int b;\n"
8179                "    }\n"
8180                "    if (true)\n"
8181                "      {\n"
8182                "        a();\n"
8183                "        b();\n"
8184                "      }\n"
8185                "  }\n"
8186                "  void g() { return; }\n"
8187                "}\n"
8188                "}",
8189                GNUBraceStyle);
8190 
8191   verifyFormat("void f()\n"
8192                "{\n"
8193                "  if (true)\n"
8194                "    {\n"
8195                "      a();\n"
8196                "    }\n"
8197                "  else if (false)\n"
8198                "    {\n"
8199                "      b();\n"
8200                "    }\n"
8201                "  else\n"
8202                "    {\n"
8203                "      c();\n"
8204                "    }\n"
8205                "}\n",
8206                GNUBraceStyle);
8207 
8208   verifyFormat("void f()\n"
8209                "{\n"
8210                "  for (int i = 0; i < 10; ++i)\n"
8211                "    {\n"
8212                "      a();\n"
8213                "    }\n"
8214                "  while (false)\n"
8215                "    {\n"
8216                "      b();\n"
8217                "    }\n"
8218                "  do\n"
8219                "    {\n"
8220                "      c();\n"
8221                "    }\n"
8222                "  while (false);\n"
8223                "}\n",
8224                GNUBraceStyle);
8225 
8226   verifyFormat("void f(int a)\n"
8227                "{\n"
8228                "  switch (a)\n"
8229                "    {\n"
8230                "    case 0:\n"
8231                "      break;\n"
8232                "    case 1:\n"
8233                "      {\n"
8234                "        break;\n"
8235                "      }\n"
8236                "    case 2:\n"
8237                "      {\n"
8238                "      }\n"
8239                "      break;\n"
8240                "    default:\n"
8241                "      break;\n"
8242                "    }\n"
8243                "}\n",
8244                GNUBraceStyle);
8245 
8246   verifyFormat("enum X\n"
8247                "{\n"
8248                "  Y = 0,\n"
8249                "}\n",
8250                GNUBraceStyle);
8251 
8252   verifyFormat("@interface BSApplicationController ()\n"
8253                "{\n"
8254                "@private\n"
8255                "  id _extraIvar;\n"
8256                "}\n"
8257                "@end\n",
8258                GNUBraceStyle);
8259 
8260   verifyFormat("#ifdef _DEBUG\n"
8261                "int foo(int i = 0)\n"
8262                "#else\n"
8263                "int foo(int i = 5)\n"
8264                "#endif\n"
8265                "{\n"
8266                "  return i;\n"
8267                "}",
8268                GNUBraceStyle);
8269 
8270   verifyFormat("void foo() {}\n"
8271                "void bar()\n"
8272                "#ifdef _DEBUG\n"
8273                "{\n"
8274                "  foo();\n"
8275                "}\n"
8276                "#else\n"
8277                "{\n"
8278                "}\n"
8279                "#endif",
8280                GNUBraceStyle);
8281 
8282   verifyFormat("void foobar() { int i = 5; }\n"
8283                "#ifdef _DEBUG\n"
8284                "void bar() {}\n"
8285                "#else\n"
8286                "void bar() { foobar(); }\n"
8287                "#endif",
8288                GNUBraceStyle);
8289 }
8290 
8291 TEST_F(FormatTest, WebKitBraceBreaking) {
8292   FormatStyle WebKitBraceStyle = getLLVMStyle();
8293   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
8294   WebKitBraceStyle.FixNamespaceComments = false;
8295   verifyFormat("namespace a {\n"
8296                "class A {\n"
8297                "  void f()\n"
8298                "  {\n"
8299                "    if (true) {\n"
8300                "      a();\n"
8301                "      b();\n"
8302                "    }\n"
8303                "  }\n"
8304                "  void g() { return; }\n"
8305                "};\n"
8306                "enum E {\n"
8307                "  A,\n"
8308                "  // foo\n"
8309                "  B,\n"
8310                "  C\n"
8311                "};\n"
8312                "struct B {\n"
8313                "  int x;\n"
8314                "};\n"
8315                "}\n",
8316                WebKitBraceStyle);
8317   verifyFormat("struct S {\n"
8318                "  int Type;\n"
8319                "  union {\n"
8320                "    int x;\n"
8321                "    double y;\n"
8322                "  } Value;\n"
8323                "  class C {\n"
8324                "    MyFavoriteType Value;\n"
8325                "  } Class;\n"
8326                "};\n",
8327                WebKitBraceStyle);
8328 }
8329 
8330 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8331   verifyFormat("void f() {\n"
8332                "  try {\n"
8333                "  } catch (const Exception &e) {\n"
8334                "  }\n"
8335                "}\n",
8336                getLLVMStyle());
8337 }
8338 
8339 TEST_F(FormatTest, UnderstandsPragmas) {
8340   verifyFormat("#pragma omp reduction(| : var)");
8341   verifyFormat("#pragma omp reduction(+ : var)");
8342 
8343   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8344             "(including parentheses).",
8345             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8346                    "(including parentheses)."));
8347 }
8348 
8349 TEST_F(FormatTest, UnderstandPragmaOption) {
8350   verifyFormat("#pragma option -C -A");
8351 
8352   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
8353 }
8354 
8355 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8356   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8357   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
8358                                   << " differs from Style #0"
8359 
8360 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8361   SmallVector<FormatStyle, 3> Styles;
8362   Styles.resize(3);
8363 
8364   Styles[0] = getLLVMStyle();
8365   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8366   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8367   EXPECT_ALL_STYLES_EQUAL(Styles);
8368 
8369   Styles[0] = getGoogleStyle();
8370   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8371   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8372   EXPECT_ALL_STYLES_EQUAL(Styles);
8373 
8374   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8375   EXPECT_TRUE(
8376       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8377   EXPECT_TRUE(
8378       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8379   EXPECT_ALL_STYLES_EQUAL(Styles);
8380 
8381   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8382   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8383   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8384   EXPECT_ALL_STYLES_EQUAL(Styles);
8385 
8386   Styles[0] = getMozillaStyle();
8387   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8388   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8389   EXPECT_ALL_STYLES_EQUAL(Styles);
8390 
8391   Styles[0] = getWebKitStyle();
8392   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8393   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8394   EXPECT_ALL_STYLES_EQUAL(Styles);
8395 
8396   Styles[0] = getGNUStyle();
8397   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8398   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8399   EXPECT_ALL_STYLES_EQUAL(Styles);
8400 
8401   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8402 }
8403 
8404 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8405   SmallVector<FormatStyle, 8> Styles;
8406   Styles.resize(2);
8407 
8408   Styles[0] = getGoogleStyle();
8409   Styles[1] = getLLVMStyle();
8410   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8411   EXPECT_ALL_STYLES_EQUAL(Styles);
8412 
8413   Styles.resize(5);
8414   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8415   Styles[1] = getLLVMStyle();
8416   Styles[1].Language = FormatStyle::LK_JavaScript;
8417   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8418 
8419   Styles[2] = getLLVMStyle();
8420   Styles[2].Language = FormatStyle::LK_JavaScript;
8421   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8422                                   "BasedOnStyle: Google",
8423                                   &Styles[2])
8424                    .value());
8425 
8426   Styles[3] = getLLVMStyle();
8427   Styles[3].Language = FormatStyle::LK_JavaScript;
8428   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8429                                   "Language: JavaScript",
8430                                   &Styles[3])
8431                    .value());
8432 
8433   Styles[4] = getLLVMStyle();
8434   Styles[4].Language = FormatStyle::LK_JavaScript;
8435   EXPECT_EQ(0, parseConfiguration("---\n"
8436                                   "BasedOnStyle: LLVM\n"
8437                                   "IndentWidth: 123\n"
8438                                   "---\n"
8439                                   "BasedOnStyle: Google\n"
8440                                   "Language: JavaScript",
8441                                   &Styles[4])
8442                    .value());
8443   EXPECT_ALL_STYLES_EQUAL(Styles);
8444 }
8445 
8446 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
8447   Style.FIELD = false;                                                         \
8448   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
8449   EXPECT_TRUE(Style.FIELD);                                                    \
8450   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
8451   EXPECT_FALSE(Style.FIELD);
8452 
8453 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8454 
8455 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
8456   Style.STRUCT.FIELD = false;                                                  \
8457   EXPECT_EQ(0,                                                                 \
8458             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
8459                 .value());                                                     \
8460   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
8461   EXPECT_EQ(0,                                                                 \
8462             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
8463                 .value());                                                     \
8464   EXPECT_FALSE(Style.STRUCT.FIELD);
8465 
8466 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
8467   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
8468 
8469 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
8470   EXPECT_NE(VALUE, Style.FIELD);                                               \
8471   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
8472   EXPECT_EQ(VALUE, Style.FIELD)
8473 
8474 TEST_F(FormatTest, ParsesConfigurationBools) {
8475   FormatStyle Style = {};
8476   Style.Language = FormatStyle::LK_Cpp;
8477   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8478   CHECK_PARSE_BOOL(AlignOperands);
8479   CHECK_PARSE_BOOL(AlignTrailingComments);
8480   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
8481   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
8482   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8483   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8484   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8485   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8486   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8487   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8488   CHECK_PARSE_BOOL(BinPackArguments);
8489   CHECK_PARSE_BOOL(BinPackParameters);
8490   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
8491   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8492   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8493   CHECK_PARSE_BOOL(BreakStringLiterals);
8494   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8495   CHECK_PARSE_BOOL(DerivePointerAlignment);
8496   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8497   CHECK_PARSE_BOOL(DisableFormat);
8498   CHECK_PARSE_BOOL(IndentCaseLabels);
8499   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8500   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8501   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8502   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8503   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8504   CHECK_PARSE_BOOL(ReflowComments);
8505   CHECK_PARSE_BOOL(SortIncludes);
8506   CHECK_PARSE_BOOL(SpacesInParentheses);
8507   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8508   CHECK_PARSE_BOOL(SpacesInAngles);
8509   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8510   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8511   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8512   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8513   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
8514   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8515 
8516   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
8517   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
8518   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
8519   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
8520   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
8521   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
8522   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
8523   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
8524   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
8525   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
8526   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
8527 }
8528 
8529 #undef CHECK_PARSE_BOOL
8530 
8531 TEST_F(FormatTest, ParsesConfiguration) {
8532   FormatStyle Style = {};
8533   Style.Language = FormatStyle::LK_Cpp;
8534   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8535   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8536               ConstructorInitializerIndentWidth, 1234u);
8537   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8538   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8539   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8540   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8541               PenaltyBreakBeforeFirstCallParameter, 1234u);
8542   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8543   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8544               PenaltyReturnTypeOnItsOwnLine, 1234u);
8545   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8546               SpacesBeforeTrailingComments, 1234u);
8547   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8548   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8549   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
8550 
8551   Style.PointerAlignment = FormatStyle::PAS_Middle;
8552   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8553               FormatStyle::PAS_Left);
8554   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8555               FormatStyle::PAS_Right);
8556   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8557               FormatStyle::PAS_Middle);
8558   // For backward compatibility:
8559   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8560               FormatStyle::PAS_Left);
8561   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
8562               FormatStyle::PAS_Right);
8563   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
8564               FormatStyle::PAS_Middle);
8565 
8566   Style.Standard = FormatStyle::LS_Auto;
8567   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8568   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8569   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8570   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8571   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8572 
8573   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8574   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
8575               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
8576   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
8577               FormatStyle::BOS_None);
8578   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
8579               FormatStyle::BOS_All);
8580   // For backward compatibility:
8581   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
8582               FormatStyle::BOS_None);
8583   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
8584               FormatStyle::BOS_All);
8585 
8586   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8587   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
8588               FormatStyle::BAS_Align);
8589   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
8590               FormatStyle::BAS_DontAlign);
8591   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
8592               FormatStyle::BAS_AlwaysBreak);
8593   // For backward compatibility:
8594   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
8595               FormatStyle::BAS_DontAlign);
8596   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
8597               FormatStyle::BAS_Align);
8598 
8599   Style.UseTab = FormatStyle::UT_ForIndentation;
8600   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8601   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8602   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8603   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
8604               FormatStyle::UT_ForContinuationAndIndentation);
8605   // For backward compatibility:
8606   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8607   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
8608 
8609   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8610   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
8611               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8612   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
8613               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
8614   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
8615               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
8616   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
8617               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8618   // For backward compatibility:
8619   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
8620               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8621   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
8622               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8623 
8624   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
8625   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
8626               FormatStyle::SBPO_Never);
8627   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
8628               FormatStyle::SBPO_Always);
8629   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
8630               FormatStyle::SBPO_ControlStatements);
8631   // For backward compatibility:
8632   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
8633               FormatStyle::SBPO_Never);
8634   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
8635               FormatStyle::SBPO_ControlStatements);
8636 
8637   Style.ColumnLimit = 123;
8638   FormatStyle BaseStyle = getLLVMStyle();
8639   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
8640   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
8641 
8642   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8643   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
8644               FormatStyle::BS_Attach);
8645   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
8646               FormatStyle::BS_Linux);
8647   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
8648               FormatStyle::BS_Mozilla);
8649   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
8650               FormatStyle::BS_Stroustrup);
8651   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
8652               FormatStyle::BS_Allman);
8653   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
8654   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
8655               FormatStyle::BS_WebKit);
8656   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
8657               FormatStyle::BS_Custom);
8658 
8659   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8660   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
8661               FormatStyle::RTBS_None);
8662   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
8663               FormatStyle::RTBS_All);
8664   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
8665               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
8666   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
8667               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
8668   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
8669               AlwaysBreakAfterReturnType,
8670               FormatStyle::RTBS_TopLevelDefinitions);
8671 
8672   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
8673   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
8674               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
8675   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
8676               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
8677   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
8678               AlwaysBreakAfterDefinitionReturnType,
8679               FormatStyle::DRTBS_TopLevel);
8680 
8681   Style.NamespaceIndentation = FormatStyle::NI_All;
8682   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
8683               FormatStyle::NI_None);
8684   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
8685               FormatStyle::NI_Inner);
8686   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
8687               FormatStyle::NI_All);
8688 
8689   // FIXME: This is required because parsing a configuration simply overwrites
8690   // the first N elements of the list instead of resetting it.
8691   Style.ForEachMacros.clear();
8692   std::vector<std::string> BoostForeach;
8693   BoostForeach.push_back("BOOST_FOREACH");
8694   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
8695   std::vector<std::string> BoostAndQForeach;
8696   BoostAndQForeach.push_back("BOOST_FOREACH");
8697   BoostAndQForeach.push_back("Q_FOREACH");
8698   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
8699               BoostAndQForeach);
8700 
8701   Style.IncludeCategories.clear();
8702   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
8703                                                                   {".*", 1}};
8704   CHECK_PARSE("IncludeCategories:\n"
8705               "  - Regex: abc/.*\n"
8706               "    Priority: 2\n"
8707               "  - Regex: .*\n"
8708               "    Priority: 1",
8709               IncludeCategories, ExpectedCategories);
8710   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
8711 }
8712 
8713 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
8714   FormatStyle Style = {};
8715   Style.Language = FormatStyle::LK_Cpp;
8716   CHECK_PARSE("Language: Cpp\n"
8717               "IndentWidth: 12",
8718               IndentWidth, 12u);
8719   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
8720                                "IndentWidth: 34",
8721                                &Style),
8722             ParseError::Unsuitable);
8723   EXPECT_EQ(12u, Style.IndentWidth);
8724   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8725   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8726 
8727   Style.Language = FormatStyle::LK_JavaScript;
8728   CHECK_PARSE("Language: JavaScript\n"
8729               "IndentWidth: 12",
8730               IndentWidth, 12u);
8731   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
8732   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
8733                                "IndentWidth: 34",
8734                                &Style),
8735             ParseError::Unsuitable);
8736   EXPECT_EQ(23u, Style.IndentWidth);
8737   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8738   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8739 
8740   CHECK_PARSE("BasedOnStyle: LLVM\n"
8741               "IndentWidth: 67",
8742               IndentWidth, 67u);
8743 
8744   CHECK_PARSE("---\n"
8745               "Language: JavaScript\n"
8746               "IndentWidth: 12\n"
8747               "---\n"
8748               "Language: Cpp\n"
8749               "IndentWidth: 34\n"
8750               "...\n",
8751               IndentWidth, 12u);
8752 
8753   Style.Language = FormatStyle::LK_Cpp;
8754   CHECK_PARSE("---\n"
8755               "Language: JavaScript\n"
8756               "IndentWidth: 12\n"
8757               "---\n"
8758               "Language: Cpp\n"
8759               "IndentWidth: 34\n"
8760               "...\n",
8761               IndentWidth, 34u);
8762   CHECK_PARSE("---\n"
8763               "IndentWidth: 78\n"
8764               "---\n"
8765               "Language: JavaScript\n"
8766               "IndentWidth: 56\n"
8767               "...\n",
8768               IndentWidth, 78u);
8769 
8770   Style.ColumnLimit = 123;
8771   Style.IndentWidth = 234;
8772   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
8773   Style.TabWidth = 345;
8774   EXPECT_FALSE(parseConfiguration("---\n"
8775                                   "IndentWidth: 456\n"
8776                                   "BreakBeforeBraces: Allman\n"
8777                                   "---\n"
8778                                   "Language: JavaScript\n"
8779                                   "IndentWidth: 111\n"
8780                                   "TabWidth: 111\n"
8781                                   "---\n"
8782                                   "Language: Cpp\n"
8783                                   "BreakBeforeBraces: Stroustrup\n"
8784                                   "TabWidth: 789\n"
8785                                   "...\n",
8786                                   &Style));
8787   EXPECT_EQ(123u, Style.ColumnLimit);
8788   EXPECT_EQ(456u, Style.IndentWidth);
8789   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
8790   EXPECT_EQ(789u, Style.TabWidth);
8791 
8792   EXPECT_EQ(parseConfiguration("---\n"
8793                                "Language: JavaScript\n"
8794                                "IndentWidth: 56\n"
8795                                "---\n"
8796                                "IndentWidth: 78\n"
8797                                "...\n",
8798                                &Style),
8799             ParseError::Error);
8800   EXPECT_EQ(parseConfiguration("---\n"
8801                                "Language: JavaScript\n"
8802                                "IndentWidth: 56\n"
8803                                "---\n"
8804                                "Language: JavaScript\n"
8805                                "IndentWidth: 78\n"
8806                                "...\n",
8807                                &Style),
8808             ParseError::Error);
8809 
8810   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8811 }
8812 
8813 #undef CHECK_PARSE
8814 
8815 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
8816   FormatStyle Style = {};
8817   Style.Language = FormatStyle::LK_JavaScript;
8818   Style.BreakBeforeTernaryOperators = true;
8819   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
8820   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
8821 
8822   Style.BreakBeforeTernaryOperators = true;
8823   EXPECT_EQ(0, parseConfiguration("---\n"
8824                                   "BasedOnStyle: Google\n"
8825                                   "---\n"
8826                                   "Language: JavaScript\n"
8827                                   "IndentWidth: 76\n"
8828                                   "...\n",
8829                                   &Style)
8830                    .value());
8831   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
8832   EXPECT_EQ(76u, Style.IndentWidth);
8833   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8834 }
8835 
8836 TEST_F(FormatTest, ConfigurationRoundTripTest) {
8837   FormatStyle Style = getLLVMStyle();
8838   std::string YAML = configurationAsText(Style);
8839   FormatStyle ParsedStyle = {};
8840   ParsedStyle.Language = FormatStyle::LK_Cpp;
8841   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
8842   EXPECT_EQ(Style, ParsedStyle);
8843 }
8844 
8845 TEST_F(FormatTest, WorksFor8bitEncodings) {
8846   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
8847             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
8848             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
8849             "\"\xef\xee\xf0\xf3...\"",
8850             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
8851                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
8852                    "\xef\xee\xf0\xf3...\"",
8853                    getLLVMStyleWithColumns(12)));
8854 }
8855 
8856 TEST_F(FormatTest, HandlesUTF8BOM) {
8857   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
8858   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
8859             format("\xef\xbb\xbf#include <iostream>"));
8860   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
8861             format("\xef\xbb\xbf\n#include <iostream>"));
8862 }
8863 
8864 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
8865 #if !defined(_MSC_VER)
8866 
8867 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
8868   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
8869                getLLVMStyleWithColumns(35));
8870   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
8871                getLLVMStyleWithColumns(31));
8872   verifyFormat("// Однажды в студёную зимнюю пору...",
8873                getLLVMStyleWithColumns(36));
8874   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
8875   verifyFormat("/* Однажды в студёную зимнюю пору... */",
8876                getLLVMStyleWithColumns(39));
8877   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
8878                getLLVMStyleWithColumns(35));
8879 }
8880 
8881 TEST_F(FormatTest, SplitsUTF8Strings) {
8882   // Non-printable characters' width is currently considered to be the length in
8883   // bytes in UTF8. The characters can be displayed in very different manner
8884   // (zero-width, single width with a substitution glyph, expanded to their code
8885   // (e.g. "<8d>"), so there's no single correct way to handle them.
8886   EXPECT_EQ("\"aaaaÄ\"\n"
8887             "\"\xc2\x8d\";",
8888             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
8889   EXPECT_EQ("\"aaaaaaaÄ\"\n"
8890             "\"\xc2\x8d\";",
8891             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
8892   EXPECT_EQ("\"Однажды, в \"\n"
8893             "\"студёную \"\n"
8894             "\"зимнюю \"\n"
8895             "\"пору,\"",
8896             format("\"Однажды, в студёную зимнюю пору,\"",
8897                    getLLVMStyleWithColumns(13)));
8898   EXPECT_EQ(
8899       "\"一 二 三 \"\n"
8900       "\"四 五六 \"\n"
8901       "\"七 八 九 \"\n"
8902       "\"十\"",
8903       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
8904   EXPECT_EQ("\"一\t二 \"\n"
8905             "\"\t三 \"\n"
8906             "\"四 五\t六 \"\n"
8907             "\"\t七 \"\n"
8908             "\"八九十\tqq\"",
8909             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
8910                    getLLVMStyleWithColumns(11)));
8911 
8912   // UTF8 character in an escape sequence.
8913   EXPECT_EQ("\"aaaaaa\"\n"
8914             "\"\\\xC2\x8D\"",
8915             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
8916 }
8917 
8918 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
8919   EXPECT_EQ("const char *sssss =\n"
8920             "    \"一二三四五六七八\\\n"
8921             " 九 十\";",
8922             format("const char *sssss = \"一二三四五六七八\\\n"
8923                    " 九 十\";",
8924                    getLLVMStyleWithColumns(30)));
8925 }
8926 
8927 TEST_F(FormatTest, SplitsUTF8LineComments) {
8928   EXPECT_EQ("// aaaaÄ\xc2\x8d",
8929             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
8930   EXPECT_EQ("// Я из лесу\n"
8931             "// вышел; был\n"
8932             "// сильный\n"
8933             "// мороз.",
8934             format("// Я из лесу вышел; был сильный мороз.",
8935                    getLLVMStyleWithColumns(13)));
8936   EXPECT_EQ("// 一二三\n"
8937             "// 四五六七\n"
8938             "// 八  九\n"
8939             "// 十",
8940             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
8941 }
8942 
8943 TEST_F(FormatTest, SplitsUTF8BlockComments) {
8944   EXPECT_EQ("/* Гляжу,\n"
8945             " * поднимается\n"
8946             " * медленно в\n"
8947             " * гору\n"
8948             " * Лошадка,\n"
8949             " * везущая\n"
8950             " * хворосту\n"
8951             " * воз. */",
8952             format("/* Гляжу, поднимается медленно в гору\n"
8953                    " * Лошадка, везущая хворосту воз. */",
8954                    getLLVMStyleWithColumns(13)));
8955   EXPECT_EQ(
8956       "/* 一二三\n"
8957       " * 四五六七\n"
8958       " * 八  九\n"
8959       " * 十  */",
8960       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
8961   EXPECT_EQ("/* �������� ��������\n"
8962             " * ��������\n"
8963             " * ������-�� */",
8964             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
8965 }
8966 
8967 #endif // _MSC_VER
8968 
8969 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
8970   FormatStyle Style = getLLVMStyle();
8971 
8972   Style.ConstructorInitializerIndentWidth = 4;
8973   verifyFormat(
8974       "SomeClass::Constructor()\n"
8975       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8976       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8977       Style);
8978 
8979   Style.ConstructorInitializerIndentWidth = 2;
8980   verifyFormat(
8981       "SomeClass::Constructor()\n"
8982       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8983       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8984       Style);
8985 
8986   Style.ConstructorInitializerIndentWidth = 0;
8987   verifyFormat(
8988       "SomeClass::Constructor()\n"
8989       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
8990       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
8991       Style);
8992   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8993   verifyFormat(
8994       "SomeLongTemplateVariableName<\n"
8995       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
8996       Style);
8997   verifyFormat(
8998       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
8999       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9000       Style);
9001 }
9002 
9003 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9004   FormatStyle Style = getLLVMStyle();
9005   Style.BreakConstructorInitializersBeforeComma = true;
9006   Style.ConstructorInitializerIndentWidth = 4;
9007   verifyFormat("SomeClass::Constructor()\n"
9008                "    : a(a)\n"
9009                "    , b(b)\n"
9010                "    , c(c) {}",
9011                Style);
9012   verifyFormat("SomeClass::Constructor()\n"
9013                "    : a(a) {}",
9014                Style);
9015 
9016   Style.ColumnLimit = 0;
9017   verifyFormat("SomeClass::Constructor()\n"
9018                "    : a(a) {}",
9019                Style);
9020   verifyFormat("SomeClass::Constructor() noexcept\n"
9021                "    : a(a) {}",
9022                Style);
9023   verifyFormat("SomeClass::Constructor()\n"
9024                "    : a(a)\n"
9025                "    , b(b)\n"
9026                "    , c(c) {}",
9027                Style);
9028   verifyFormat("SomeClass::Constructor()\n"
9029                "    : a(a) {\n"
9030                "  foo();\n"
9031                "  bar();\n"
9032                "}",
9033                Style);
9034 
9035   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9036   verifyFormat("SomeClass::Constructor()\n"
9037                "    : a(a)\n"
9038                "    , b(b)\n"
9039                "    , c(c) {\n}",
9040                Style);
9041   verifyFormat("SomeClass::Constructor()\n"
9042                "    : a(a) {\n}",
9043                Style);
9044 
9045   Style.ColumnLimit = 80;
9046   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9047   Style.ConstructorInitializerIndentWidth = 2;
9048   verifyFormat("SomeClass::Constructor()\n"
9049                "  : a(a)\n"
9050                "  , b(b)\n"
9051                "  , c(c) {}",
9052                Style);
9053 
9054   Style.ConstructorInitializerIndentWidth = 0;
9055   verifyFormat("SomeClass::Constructor()\n"
9056                ": a(a)\n"
9057                ", b(b)\n"
9058                ", c(c) {}",
9059                Style);
9060 
9061   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9062   Style.ConstructorInitializerIndentWidth = 4;
9063   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9064   verifyFormat(
9065       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9066       Style);
9067   verifyFormat(
9068       "SomeClass::Constructor()\n"
9069       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9070       Style);
9071   Style.ConstructorInitializerIndentWidth = 4;
9072   Style.ColumnLimit = 60;
9073   verifyFormat("SomeClass::Constructor()\n"
9074                "    : aaaaaaaa(aaaaaaaa)\n"
9075                "    , aaaaaaaa(aaaaaaaa)\n"
9076                "    , aaaaaaaa(aaaaaaaa) {}",
9077                Style);
9078 }
9079 
9080 TEST_F(FormatTest, Destructors) {
9081   verifyFormat("void F(int &i) { i.~int(); }");
9082   verifyFormat("void F(int &i) { i->~int(); }");
9083 }
9084 
9085 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9086   FormatStyle Style = getWebKitStyle();
9087 
9088   // Don't indent in outer namespaces.
9089   verifyFormat("namespace outer {\n"
9090                "int i;\n"
9091                "namespace inner {\n"
9092                "    int i;\n"
9093                "} // namespace inner\n"
9094                "} // namespace outer\n"
9095                "namespace other_outer {\n"
9096                "int i;\n"
9097                "}",
9098                Style);
9099 
9100   // Don't indent case labels.
9101   verifyFormat("switch (variable) {\n"
9102                "case 1:\n"
9103                "case 2:\n"
9104                "    doSomething();\n"
9105                "    break;\n"
9106                "default:\n"
9107                "    ++variable;\n"
9108                "}",
9109                Style);
9110 
9111   // Wrap before binary operators.
9112   EXPECT_EQ("void f()\n"
9113             "{\n"
9114             "    if (aaaaaaaaaaaaaaaa\n"
9115             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9116             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9117             "        return;\n"
9118             "}",
9119             format("void f() {\n"
9120                    "if (aaaaaaaaaaaaaaaa\n"
9121                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9122                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9123                    "return;\n"
9124                    "}",
9125                    Style));
9126 
9127   // Allow functions on a single line.
9128   verifyFormat("void f() { return; }", Style);
9129 
9130   // Constructor initializers are formatted one per line with the "," on the
9131   // new line.
9132   verifyFormat("Constructor()\n"
9133                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9134                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9135                "          aaaaaaaaaaaaaa)\n"
9136                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9137                "{\n"
9138                "}",
9139                Style);
9140   verifyFormat("SomeClass::Constructor()\n"
9141                "    : a(a)\n"
9142                "{\n"
9143                "}",
9144                Style);
9145   EXPECT_EQ("SomeClass::Constructor()\n"
9146             "    : a(a)\n"
9147             "{\n"
9148             "}",
9149             format("SomeClass::Constructor():a(a){}", Style));
9150   verifyFormat("SomeClass::Constructor()\n"
9151                "    : a(a)\n"
9152                "    , b(b)\n"
9153                "    , c(c)\n"
9154                "{\n"
9155                "}",
9156                Style);
9157   verifyFormat("SomeClass::Constructor()\n"
9158                "    : a(a)\n"
9159                "{\n"
9160                "    foo();\n"
9161                "    bar();\n"
9162                "}",
9163                Style);
9164 
9165   // Access specifiers should be aligned left.
9166   verifyFormat("class C {\n"
9167                "public:\n"
9168                "    int i;\n"
9169                "};",
9170                Style);
9171 
9172   // Do not align comments.
9173   verifyFormat("int a; // Do not\n"
9174                "double b; // align comments.",
9175                Style);
9176 
9177   // Do not align operands.
9178   EXPECT_EQ("ASSERT(aaaa\n"
9179             "    || bbbb);",
9180             format("ASSERT ( aaaa\n||bbbb);", Style));
9181 
9182   // Accept input's line breaks.
9183   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9184             "    || bbbbbbbbbbbbbbb) {\n"
9185             "    i++;\n"
9186             "}",
9187             format("if (aaaaaaaaaaaaaaa\n"
9188                    "|| bbbbbbbbbbbbbbb) { i++; }",
9189                    Style));
9190   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9191             "    i++;\n"
9192             "}",
9193             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9194 
9195   // Don't automatically break all macro definitions (llvm.org/PR17842).
9196   verifyFormat("#define aNumber 10", Style);
9197   // However, generally keep the line breaks that the user authored.
9198   EXPECT_EQ("#define aNumber \\\n"
9199             "    10",
9200             format("#define aNumber \\\n"
9201                    " 10",
9202                    Style));
9203 
9204   // Keep empty and one-element array literals on a single line.
9205   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9206             "                                  copyItems:YES];",
9207             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9208                    "copyItems:YES];",
9209                    Style));
9210   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9211             "                                  copyItems:YES];",
9212             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9213                    "             copyItems:YES];",
9214                    Style));
9215   // FIXME: This does not seem right, there should be more indentation before
9216   // the array literal's entries. Nested blocks have the same problem.
9217   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9218             "    @\"a\",\n"
9219             "    @\"a\"\n"
9220             "]\n"
9221             "                                  copyItems:YES];",
9222             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9223                    "     @\"a\",\n"
9224                    "     @\"a\"\n"
9225                    "     ]\n"
9226                    "       copyItems:YES];",
9227                    Style));
9228   EXPECT_EQ(
9229       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9230       "                                  copyItems:YES];",
9231       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9232              "   copyItems:YES];",
9233              Style));
9234 
9235   verifyFormat("[self.a b:c c:d];", Style);
9236   EXPECT_EQ("[self.a b:c\n"
9237             "        c:d];",
9238             format("[self.a b:c\n"
9239                    "c:d];",
9240                    Style));
9241 }
9242 
9243 TEST_F(FormatTest, FormatsLambdas) {
9244   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9245   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9246   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9247   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9248   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9249   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9250   verifyFormat("int x = f(*+[] {});");
9251   verifyFormat("void f() {\n"
9252                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9253                "}\n");
9254   verifyFormat("void f() {\n"
9255                "  other(x.begin(), //\n"
9256                "        x.end(),   //\n"
9257                "        [&](int, int) { return 1; });\n"
9258                "}\n");
9259   verifyFormat("SomeFunction([]() { // A cool function...\n"
9260                "  return 43;\n"
9261                "});");
9262   EXPECT_EQ("SomeFunction([]() {\n"
9263             "#define A a\n"
9264             "  return 43;\n"
9265             "});",
9266             format("SomeFunction([](){\n"
9267                    "#define A a\n"
9268                    "return 43;\n"
9269                    "});"));
9270   verifyFormat("void f() {\n"
9271                "  SomeFunction([](decltype(x), A *a) {});\n"
9272                "}");
9273   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9274                "    [](const aaaaaaaaaa &a) { return a; });");
9275   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9276                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9277                "});");
9278   verifyFormat("Constructor()\n"
9279                "    : Field([] { // comment\n"
9280                "        int i;\n"
9281                "      }) {}");
9282   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9283                "  return some_parameter.size();\n"
9284                "};");
9285   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
9286                "    [](const string &s) { return s; };");
9287   verifyFormat("int i = aaaaaa ? 1 //\n"
9288                "               : [] {\n"
9289                "                   return 2; //\n"
9290                "                 }();");
9291   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9292                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9293                "                  return x == 2; // force break\n"
9294                "                });");
9295   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9296                "    [=](int iiiiiiiiiiii) {\n"
9297                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
9298                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
9299                "    });",
9300                getLLVMStyleWithColumns(60));
9301   verifyFormat("SomeFunction({[&] {\n"
9302                "                // comment\n"
9303                "              },\n"
9304                "              [&] {\n"
9305                "                // comment\n"
9306                "              }});");
9307   verifyFormat("SomeFunction({[&] {\n"
9308                "  // comment\n"
9309                "}});");
9310   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
9311                "                             [&]() { return true; },\n"
9312                "                         aaaaa aaaaaaaaa);");
9313 
9314   // Lambdas with return types.
9315   verifyFormat("int c = []() -> int { return 2; }();\n");
9316   verifyFormat("int c = []() -> int * { return 2; }();\n");
9317   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9318   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9319   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9320   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9321   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9322   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9323   verifyFormat("[a, a]() -> a<1> {};");
9324   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9325                "                   int j) -> int {\n"
9326                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9327                "};");
9328   verifyFormat(
9329       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9330       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9331       "      return aaaaaaaaaaaaaaaaa;\n"
9332       "    });",
9333       getLLVMStyleWithColumns(70));
9334   verifyFormat("[]() //\n"
9335                "    -> int {\n"
9336                "  return 1; //\n"
9337                "};");
9338 
9339   // Multiple lambdas in the same parentheses change indentation rules.
9340   verifyFormat("SomeFunction(\n"
9341                "    []() {\n"
9342                "      int i = 42;\n"
9343                "      return i;\n"
9344                "    },\n"
9345                "    []() {\n"
9346                "      int j = 43;\n"
9347                "      return j;\n"
9348                "    });");
9349 
9350   // More complex introducers.
9351   verifyFormat("return [i, args...] {};");
9352 
9353   // Not lambdas.
9354   verifyFormat("constexpr char hello[]{\"hello\"};");
9355   verifyFormat("double &operator[](int i) { return 0; }\n"
9356                "int i;");
9357   verifyFormat("std::unique_ptr<int[]> foo() {}");
9358   verifyFormat("int i = a[a][a]->f();");
9359   verifyFormat("int i = (*b)[a]->f();");
9360 
9361   // Other corner cases.
9362   verifyFormat("void f() {\n"
9363                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9364                "      );\n"
9365                "}");
9366 
9367   // Lambdas created through weird macros.
9368   verifyFormat("void f() {\n"
9369                "  MACRO((const AA &a) { return 1; });\n"
9370                "  MACRO((AA &a) { return 1; });\n"
9371                "}");
9372 
9373   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9374                "      doo_dah();\n"
9375                "      doo_dah();\n"
9376                "    })) {\n"
9377                "}");
9378   verifyFormat("auto lambda = []() {\n"
9379                "  int a = 2\n"
9380                "#if A\n"
9381                "          + 2\n"
9382                "#endif\n"
9383                "      ;\n"
9384                "};");
9385 
9386   // Lambdas with complex multiline introducers.
9387   verifyFormat(
9388       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9389       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
9390       "        -> ::std::unordered_set<\n"
9391       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
9392       "      //\n"
9393       "    });");
9394 }
9395 
9396 TEST_F(FormatTest, FormatsBlocks) {
9397   FormatStyle ShortBlocks = getLLVMStyle();
9398   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9399   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9400   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9401   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9402   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9403   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9404   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9405 
9406   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9407   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9408   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9409 
9410   verifyFormat("[operation setCompletionBlock:^{\n"
9411                "  [self onOperationDone];\n"
9412                "}];");
9413   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9414                "  [self onOperationDone];\n"
9415                "}]};");
9416   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9417                "  f();\n"
9418                "}];");
9419   verifyFormat("int a = [operation block:^int(int *i) {\n"
9420                "  return 1;\n"
9421                "}];");
9422   verifyFormat("[myObject doSomethingWith:arg1\n"
9423                "                      aaa:^int(int *a) {\n"
9424                "                        return 1;\n"
9425                "                      }\n"
9426                "                      bbb:f(a * bbbbbbbb)];");
9427 
9428   verifyFormat("[operation setCompletionBlock:^{\n"
9429                "  [self.delegate newDataAvailable];\n"
9430                "}];",
9431                getLLVMStyleWithColumns(60));
9432   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9433                "  NSString *path = [self sessionFilePath];\n"
9434                "  if (path) {\n"
9435                "    // ...\n"
9436                "  }\n"
9437                "});");
9438   verifyFormat("[[SessionService sharedService]\n"
9439                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9440                "      if (window) {\n"
9441                "        [self windowDidLoad:window];\n"
9442                "      } else {\n"
9443                "        [self errorLoadingWindow];\n"
9444                "      }\n"
9445                "    }];");
9446   verifyFormat("void (^largeBlock)(void) = ^{\n"
9447                "  // ...\n"
9448                "};\n",
9449                getLLVMStyleWithColumns(40));
9450   verifyFormat("[[SessionService sharedService]\n"
9451                "    loadWindowWithCompletionBlock: //\n"
9452                "        ^(SessionWindow *window) {\n"
9453                "          if (window) {\n"
9454                "            [self windowDidLoad:window];\n"
9455                "          } else {\n"
9456                "            [self errorLoadingWindow];\n"
9457                "          }\n"
9458                "        }];",
9459                getLLVMStyleWithColumns(60));
9460   verifyFormat("[myObject doSomethingWith:arg1\n"
9461                "    firstBlock:^(Foo *a) {\n"
9462                "      // ...\n"
9463                "      int i;\n"
9464                "    }\n"
9465                "    secondBlock:^(Bar *b) {\n"
9466                "      // ...\n"
9467                "      int i;\n"
9468                "    }\n"
9469                "    thirdBlock:^Foo(Bar *b) {\n"
9470                "      // ...\n"
9471                "      int i;\n"
9472                "    }];");
9473   verifyFormat("[myObject doSomethingWith:arg1\n"
9474                "               firstBlock:-1\n"
9475                "              secondBlock:^(Bar *b) {\n"
9476                "                // ...\n"
9477                "                int i;\n"
9478                "              }];");
9479 
9480   verifyFormat("f(^{\n"
9481                "  @autoreleasepool {\n"
9482                "    if (a) {\n"
9483                "      g();\n"
9484                "    }\n"
9485                "  }\n"
9486                "});");
9487   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9488   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
9489                "};");
9490 
9491   FormatStyle FourIndent = getLLVMStyle();
9492   FourIndent.ObjCBlockIndentWidth = 4;
9493   verifyFormat("[operation setCompletionBlock:^{\n"
9494                "    [self onOperationDone];\n"
9495                "}];",
9496                FourIndent);
9497 }
9498 
9499 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
9500   FormatStyle ZeroColumn = getLLVMStyle();
9501   ZeroColumn.ColumnLimit = 0;
9502 
9503   verifyFormat("[[SessionService sharedService] "
9504                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9505                "  if (window) {\n"
9506                "    [self windowDidLoad:window];\n"
9507                "  } else {\n"
9508                "    [self errorLoadingWindow];\n"
9509                "  }\n"
9510                "}];",
9511                ZeroColumn);
9512   EXPECT_EQ("[[SessionService sharedService]\n"
9513             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9514             "      if (window) {\n"
9515             "        [self windowDidLoad:window];\n"
9516             "      } else {\n"
9517             "        [self errorLoadingWindow];\n"
9518             "      }\n"
9519             "    }];",
9520             format("[[SessionService sharedService]\n"
9521                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9522                    "                if (window) {\n"
9523                    "    [self windowDidLoad:window];\n"
9524                    "  } else {\n"
9525                    "    [self errorLoadingWindow];\n"
9526                    "  }\n"
9527                    "}];",
9528                    ZeroColumn));
9529   verifyFormat("[myObject doSomethingWith:arg1\n"
9530                "    firstBlock:^(Foo *a) {\n"
9531                "      // ...\n"
9532                "      int i;\n"
9533                "    }\n"
9534                "    secondBlock:^(Bar *b) {\n"
9535                "      // ...\n"
9536                "      int i;\n"
9537                "    }\n"
9538                "    thirdBlock:^Foo(Bar *b) {\n"
9539                "      // ...\n"
9540                "      int i;\n"
9541                "    }];",
9542                ZeroColumn);
9543   verifyFormat("f(^{\n"
9544                "  @autoreleasepool {\n"
9545                "    if (a) {\n"
9546                "      g();\n"
9547                "    }\n"
9548                "  }\n"
9549                "});",
9550                ZeroColumn);
9551   verifyFormat("void (^largeBlock)(void) = ^{\n"
9552                "  // ...\n"
9553                "};",
9554                ZeroColumn);
9555 
9556   ZeroColumn.AllowShortBlocksOnASingleLine = true;
9557   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
9558             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
9559   ZeroColumn.AllowShortBlocksOnASingleLine = false;
9560   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
9561             "  int i;\n"
9562             "};",
9563             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
9564 }
9565 
9566 TEST_F(FormatTest, SupportsCRLF) {
9567   EXPECT_EQ("int a;\r\n"
9568             "int b;\r\n"
9569             "int c;\r\n",
9570             format("int a;\r\n"
9571                    "  int b;\r\n"
9572                    "    int c;\r\n",
9573                    getLLVMStyle()));
9574   EXPECT_EQ("int a;\r\n"
9575             "int b;\r\n"
9576             "int c;\r\n",
9577             format("int a;\r\n"
9578                    "  int b;\n"
9579                    "    int c;\r\n",
9580                    getLLVMStyle()));
9581   EXPECT_EQ("int a;\n"
9582             "int b;\n"
9583             "int c;\n",
9584             format("int a;\r\n"
9585                    "  int b;\n"
9586                    "    int c;\n",
9587                    getLLVMStyle()));
9588   EXPECT_EQ("\"aaaaaaa \"\r\n"
9589             "\"bbbbbbb\";\r\n",
9590             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
9591   EXPECT_EQ("#define A \\\r\n"
9592             "  b;      \\\r\n"
9593             "  c;      \\\r\n"
9594             "  d;\r\n",
9595             format("#define A \\\r\n"
9596                    "  b; \\\r\n"
9597                    "  c; d; \r\n",
9598                    getGoogleStyle()));
9599 
9600   EXPECT_EQ("/*\r\n"
9601             "multi line block comments\r\n"
9602             "should not introduce\r\n"
9603             "an extra carriage return\r\n"
9604             "*/\r\n",
9605             format("/*\r\n"
9606                    "multi line block comments\r\n"
9607                    "should not introduce\r\n"
9608                    "an extra carriage return\r\n"
9609                    "*/\r\n"));
9610 }
9611 
9612 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
9613   verifyFormat("MY_CLASS(C) {\n"
9614                "  int i;\n"
9615                "  int j;\n"
9616                "};");
9617 }
9618 
9619 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
9620   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
9621   TwoIndent.ContinuationIndentWidth = 2;
9622 
9623   EXPECT_EQ("int i =\n"
9624             "  longFunction(\n"
9625             "    arg);",
9626             format("int i = longFunction(arg);", TwoIndent));
9627 
9628   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
9629   SixIndent.ContinuationIndentWidth = 6;
9630 
9631   EXPECT_EQ("int i =\n"
9632             "      longFunction(\n"
9633             "            arg);",
9634             format("int i = longFunction(arg);", SixIndent));
9635 }
9636 
9637 TEST_F(FormatTest, SpacesInAngles) {
9638   FormatStyle Spaces = getLLVMStyle();
9639   Spaces.SpacesInAngles = true;
9640 
9641   verifyFormat("static_cast< int >(arg);", Spaces);
9642   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
9643   verifyFormat("f< int, float >();", Spaces);
9644   verifyFormat("template <> g() {}", Spaces);
9645   verifyFormat("template < std::vector< int > > f() {}", Spaces);
9646   verifyFormat("std::function< void(int, int) > fct;", Spaces);
9647   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
9648                Spaces);
9649 
9650   Spaces.Standard = FormatStyle::LS_Cpp03;
9651   Spaces.SpacesInAngles = true;
9652   verifyFormat("A< A< int > >();", Spaces);
9653 
9654   Spaces.SpacesInAngles = false;
9655   verifyFormat("A<A<int> >();", Spaces);
9656 
9657   Spaces.Standard = FormatStyle::LS_Cpp11;
9658   Spaces.SpacesInAngles = true;
9659   verifyFormat("A< A< int > >();", Spaces);
9660 
9661   Spaces.SpacesInAngles = false;
9662   verifyFormat("A<A<int>>();", Spaces);
9663 }
9664 
9665 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
9666   FormatStyle Style = getLLVMStyle();
9667   Style.SpaceAfterTemplateKeyword = false;
9668   verifyFormat("template<int> void foo();", Style);
9669 }
9670 
9671 TEST_F(FormatTest, TripleAngleBrackets) {
9672   verifyFormat("f<<<1, 1>>>();");
9673   verifyFormat("f<<<1, 1, 1, s>>>();");
9674   verifyFormat("f<<<a, b, c, d>>>();");
9675   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
9676   verifyFormat("f<param><<<1, 1>>>();");
9677   verifyFormat("f<1><<<1, 1>>>();");
9678   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
9679   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9680                "aaaaaaaaaaa<<<\n    1, 1>>>();");
9681   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
9682                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
9683 }
9684 
9685 TEST_F(FormatTest, MergeLessLessAtEnd) {
9686   verifyFormat("<<");
9687   EXPECT_EQ("< < <", format("\\\n<<<"));
9688   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9689                "aaallvm::outs() <<");
9690   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9691                "aaaallvm::outs()\n    <<");
9692 }
9693 
9694 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
9695   std::string code = "#if A\n"
9696                      "#if B\n"
9697                      "a.\n"
9698                      "#endif\n"
9699                      "    a = 1;\n"
9700                      "#else\n"
9701                      "#endif\n"
9702                      "#if C\n"
9703                      "#else\n"
9704                      "#endif\n";
9705   EXPECT_EQ(code, format(code));
9706 }
9707 
9708 TEST_F(FormatTest, HandleConflictMarkers) {
9709   // Git/SVN conflict markers.
9710   EXPECT_EQ("int a;\n"
9711             "void f() {\n"
9712             "  callme(some(parameter1,\n"
9713             "<<<<<<< text by the vcs\n"
9714             "              parameter2),\n"
9715             "||||||| text by the vcs\n"
9716             "              parameter2),\n"
9717             "         parameter3,\n"
9718             "======= text by the vcs\n"
9719             "              parameter2, parameter3),\n"
9720             ">>>>>>> text by the vcs\n"
9721             "         otherparameter);\n",
9722             format("int a;\n"
9723                    "void f() {\n"
9724                    "  callme(some(parameter1,\n"
9725                    "<<<<<<< text by the vcs\n"
9726                    "  parameter2),\n"
9727                    "||||||| text by the vcs\n"
9728                    "  parameter2),\n"
9729                    "  parameter3,\n"
9730                    "======= text by the vcs\n"
9731                    "  parameter2,\n"
9732                    "  parameter3),\n"
9733                    ">>>>>>> text by the vcs\n"
9734                    "  otherparameter);\n"));
9735 
9736   // Perforce markers.
9737   EXPECT_EQ("void f() {\n"
9738             "  function(\n"
9739             ">>>> text by the vcs\n"
9740             "      parameter,\n"
9741             "==== text by the vcs\n"
9742             "      parameter,\n"
9743             "==== text by the vcs\n"
9744             "      parameter,\n"
9745             "<<<< text by the vcs\n"
9746             "      parameter);\n",
9747             format("void f() {\n"
9748                    "  function(\n"
9749                    ">>>> text by the vcs\n"
9750                    "  parameter,\n"
9751                    "==== text by the vcs\n"
9752                    "  parameter,\n"
9753                    "==== text by the vcs\n"
9754                    "  parameter,\n"
9755                    "<<<< text by the vcs\n"
9756                    "  parameter);\n"));
9757 
9758   EXPECT_EQ("<<<<<<<\n"
9759             "|||||||\n"
9760             "=======\n"
9761             ">>>>>>>",
9762             format("<<<<<<<\n"
9763                    "|||||||\n"
9764                    "=======\n"
9765                    ">>>>>>>"));
9766 
9767   EXPECT_EQ("<<<<<<<\n"
9768             "|||||||\n"
9769             "int i;\n"
9770             "=======\n"
9771             ">>>>>>>",
9772             format("<<<<<<<\n"
9773                    "|||||||\n"
9774                    "int i;\n"
9775                    "=======\n"
9776                    ">>>>>>>"));
9777 
9778   // FIXME: Handle parsing of macros around conflict markers correctly:
9779   EXPECT_EQ("#define Macro \\\n"
9780             "<<<<<<<\n"
9781             "Something \\\n"
9782             "|||||||\n"
9783             "Else \\\n"
9784             "=======\n"
9785             "Other \\\n"
9786             ">>>>>>>\n"
9787             "    End int i;\n",
9788             format("#define Macro \\\n"
9789                    "<<<<<<<\n"
9790                    "  Something \\\n"
9791                    "|||||||\n"
9792                    "  Else \\\n"
9793                    "=======\n"
9794                    "  Other \\\n"
9795                    ">>>>>>>\n"
9796                    "  End\n"
9797                    "int i;\n"));
9798 }
9799 
9800 TEST_F(FormatTest, DisableRegions) {
9801   EXPECT_EQ("int i;\n"
9802             "// clang-format off\n"
9803             "  int j;\n"
9804             "// clang-format on\n"
9805             "int k;",
9806             format(" int  i;\n"
9807                    "   // clang-format off\n"
9808                    "  int j;\n"
9809                    " // clang-format on\n"
9810                    "   int   k;"));
9811   EXPECT_EQ("int i;\n"
9812             "/* clang-format off */\n"
9813             "  int j;\n"
9814             "/* clang-format on */\n"
9815             "int k;",
9816             format(" int  i;\n"
9817                    "   /* clang-format off */\n"
9818                    "  int j;\n"
9819                    " /* clang-format on */\n"
9820                    "   int   k;"));
9821 
9822   // Don't reflow comments within disabled regions.
9823   EXPECT_EQ(
9824       "// clang-format off\n"
9825       "// long long long long long long line\n"
9826       "/* clang-format on */\n"
9827       "/* long long long\n"
9828       " * long long long\n"
9829       " * line */\n"
9830       "int i;\n"
9831       "/* clang-format off */\n"
9832       "/* long long long long long long line */\n",
9833       format("// clang-format off\n"
9834              "// long long long long long long line\n"
9835              "/* clang-format on */\n"
9836              "/* long long long long long long line */\n"
9837              "int i;\n"
9838              "/* clang-format off */\n"
9839              "/* long long long long long long line */\n",
9840              getLLVMStyleWithColumns(20)));
9841 }
9842 
9843 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
9844   format("? ) =");
9845   verifyNoCrash("#define a\\\n /**/}");
9846 }
9847 
9848 TEST_F(FormatTest, FormatsTableGenCode) {
9849   FormatStyle Style = getLLVMStyle();
9850   Style.Language = FormatStyle::LK_TableGen;
9851   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
9852 }
9853 
9854 TEST_F(FormatTest, ArrayOfTemplates) {
9855   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
9856             format("auto a = new unique_ptr<int > [ 10];"));
9857 
9858   FormatStyle Spaces = getLLVMStyle();
9859   Spaces.SpacesInSquareBrackets = true;
9860   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
9861             format("auto a = new unique_ptr<int > [10];", Spaces));
9862 }
9863 
9864 TEST_F(FormatTest, ArrayAsTemplateType) {
9865   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
9866             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
9867 
9868   FormatStyle Spaces = getLLVMStyle();
9869   Spaces.SpacesInSquareBrackets = true;
9870   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
9871             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
9872 }
9873 
9874 TEST(FormatStyle, GetStyleOfFile) {
9875   vfs::InMemoryFileSystem FS;
9876   // Test 1: format file in the same directory.
9877   ASSERT_TRUE(
9878       FS.addFile("/a/.clang-format", 0,
9879                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
9880   ASSERT_TRUE(
9881       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
9882   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
9883   ASSERT_TRUE((bool)Style1);
9884   ASSERT_EQ(*Style1, getLLVMStyle());
9885 
9886   // Test 2.1: fallback to default.
9887   ASSERT_TRUE(
9888       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
9889   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
9890   ASSERT_TRUE((bool)Style2);
9891   ASSERT_EQ(*Style2, getMozillaStyle());
9892 
9893   // Test 2.2: no format on 'none' fallback style.
9894   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
9895   ASSERT_TRUE((bool)Style2);
9896   ASSERT_EQ(*Style2, getNoStyle());
9897 
9898   // Test 2.3: format if config is found with no based style while fallback is
9899   // 'none'.
9900   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
9901                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
9902   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
9903   ASSERT_TRUE((bool)Style2);
9904   ASSERT_EQ(*Style2, getLLVMStyle());
9905 
9906   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
9907   Style2 = getStyle("{}", "a.h", "none", "", &FS);
9908   ASSERT_TRUE((bool)Style2);
9909   ASSERT_EQ(*Style2, getLLVMStyle());
9910 
9911   // Test 3: format file in parent directory.
9912   ASSERT_TRUE(
9913       FS.addFile("/c/.clang-format", 0,
9914                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
9915   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
9916                          llvm::MemoryBuffer::getMemBuffer("int i;")));
9917   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
9918   ASSERT_TRUE((bool)Style3);
9919   ASSERT_EQ(*Style3, getGoogleStyle());
9920 
9921   // Test 4: error on invalid fallback style
9922   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
9923   ASSERT_FALSE((bool)Style4);
9924   llvm::consumeError(Style4.takeError());
9925 
9926   // Test 5: error on invalid yaml on command line
9927   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
9928   ASSERT_FALSE((bool)Style5);
9929   llvm::consumeError(Style5.takeError());
9930 
9931   // Test 6: error on invalid style
9932   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
9933   ASSERT_FALSE((bool)Style6);
9934   llvm::consumeError(Style6.takeError());
9935 
9936   // Test 7: found config file, error on parsing it
9937   ASSERT_TRUE(
9938       FS.addFile("/d/.clang-format", 0,
9939                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
9940                                                   "InvalidKey: InvalidValue")));
9941   ASSERT_TRUE(
9942       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
9943   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
9944   ASSERT_FALSE((bool)Style7);
9945   llvm::consumeError(Style7.takeError());
9946 }
9947 
9948 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
9949   // Column limit is 20.
9950   std::string Code = "Type *a =\n"
9951                      "    new Type();\n"
9952                      "g(iiiii, 0, jjjjj,\n"
9953                      "  0, kkkkk, 0, mm);\n"
9954                      "int  bad     = format   ;";
9955   std::string Expected = "auto a = new Type();\n"
9956                          "g(iiiii, nullptr,\n"
9957                          "  jjjjj, nullptr,\n"
9958                          "  kkkkk, nullptr,\n"
9959                          "  mm);\n"
9960                          "int  bad     = format   ;";
9961   FileID ID = Context.createInMemoryFile("format.cpp", Code);
9962   tooling::Replacements Replaces = toReplacements(
9963       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
9964                             "auto "),
9965        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
9966                             "nullptr"),
9967        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
9968                             "nullptr"),
9969        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
9970                             "nullptr")});
9971 
9972   format::FormatStyle Style = format::getLLVMStyle();
9973   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
9974   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
9975   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
9976       << llvm::toString(FormattedReplaces.takeError()) << "\n";
9977   auto Result = applyAllReplacements(Code, *FormattedReplaces);
9978   EXPECT_TRUE(static_cast<bool>(Result));
9979   EXPECT_EQ(Expected, *Result);
9980 }
9981 
9982 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
9983   std::string Code = "#include \"a.h\"\n"
9984                      "#include \"c.h\"\n"
9985                      "\n"
9986                      "int main() {\n"
9987                      "  return 0;\n"
9988                      "}";
9989   std::string Expected = "#include \"a.h\"\n"
9990                          "#include \"b.h\"\n"
9991                          "#include \"c.h\"\n"
9992                          "\n"
9993                          "int main() {\n"
9994                          "  return 0;\n"
9995                          "}";
9996   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
9997   tooling::Replacements Replaces = toReplacements(
9998       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
9999                             "#include \"b.h\"\n")});
10000 
10001   format::FormatStyle Style = format::getLLVMStyle();
10002   Style.SortIncludes = true;
10003   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10004   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10005       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10006   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10007   EXPECT_TRUE(static_cast<bool>(Result));
10008   EXPECT_EQ(Expected, *Result);
10009 }
10010 
10011 } // end namespace
10012 } // end namespace format
10013 } // end namespace clang
10014