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