1 //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Format/Format.h"
11 
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
14 
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
19 
20 #define DEBUG_TYPE "format-test"
21 
22 using clang::tooling::ReplacementTest;
23 using clang::tooling::toReplacements;
24 
25 namespace clang {
26 namespace format {
27 namespace {
28 
29 FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
30 
31 class FormatTest : public ::testing::Test {
32 protected:
33   enum StatusCheck {
34     SC_ExpectComplete,
35     SC_ExpectIncomplete,
36     SC_DoNotCheck
37   };
38 
39   std::string format(llvm::StringRef Code,
40                      const FormatStyle &Style = getLLVMStyle(),
41                      StatusCheck CheckComplete = SC_ExpectComplete) {
42     DEBUG(llvm::errs() << "---\n");
43     DEBUG(llvm::errs() << Code << "\n\n");
44     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
45     FormattingAttemptStatus Status;
46     tooling::Replacements Replaces =
47         reformat(Style, Code, Ranges, "<stdin>", &Status);
48     if (CheckComplete != SC_DoNotCheck) {
49       bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
50       EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
51           << Code << "\n\n";
52     }
53     ReplacementCount = Replaces.size();
54     auto Result = applyAllReplacements(Code, Replaces);
55     EXPECT_TRUE(static_cast<bool>(Result));
56     DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57     return *Result;
58   }
59 
60   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
61     FormatStyle Style = getLLVMStyle();
62     Style.ColumnLimit = ColumnLimit;
63     return Style;
64   }
65 
66   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
67     FormatStyle Style = getGoogleStyle();
68     Style.ColumnLimit = ColumnLimit;
69     return Style;
70   }
71 
72   void verifyFormat(llvm::StringRef Code,
73                     const FormatStyle &Style = getLLVMStyle()) {
74     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
75     if (Style.Language == FormatStyle::LK_Cpp) {
76       // Objective-C++ is a superset of C++, so everything checked for C++
77       // needs to be checked for Objective-C++ as well.
78       FormatStyle ObjCStyle = Style;
79       ObjCStyle.Language = FormatStyle::LK_ObjC;
80       EXPECT_EQ(Code.str(), format(test::messUp(Code), ObjCStyle));
81     }
82   }
83 
84   void verifyIncompleteFormat(llvm::StringRef Code,
85                               const FormatStyle &Style = getLLVMStyle()) {
86     EXPECT_EQ(Code.str(),
87               format(test::messUp(Code), Style, SC_ExpectIncomplete));
88   }
89 
90   void verifyGoogleFormat(llvm::StringRef Code) {
91     verifyFormat(Code, getGoogleStyle());
92   }
93 
94   void verifyIndependentOfContext(llvm::StringRef text) {
95     verifyFormat(text);
96     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
97   }
98 
99   /// \brief Verify that clang-format does not crash on the given input.
100   void verifyNoCrash(llvm::StringRef Code,
101                      const FormatStyle &Style = getLLVMStyle()) {
102     format(Code, Style, SC_DoNotCheck);
103   }
104 
105   int ReplacementCount;
106 };
107 
108 TEST_F(FormatTest, MessUp) {
109   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
110   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
111   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
112   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
113   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
114 }
115 
116 //===----------------------------------------------------------------------===//
117 // Basic function tests.
118 //===----------------------------------------------------------------------===//
119 
120 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
121   EXPECT_EQ(";", format(";"));
122 }
123 
124 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
125   EXPECT_EQ("int i;", format("  int i;"));
126   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
127   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
128   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
129 }
130 
131 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
132   EXPECT_EQ("int i;", format("int\ni;"));
133 }
134 
135 TEST_F(FormatTest, FormatsNestedBlockStatements) {
136   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
137 }
138 
139 TEST_F(FormatTest, FormatsNestedCall) {
140   verifyFormat("Method(f1, f2(f3));");
141   verifyFormat("Method(f1(f2, f3()));");
142   verifyFormat("Method(f1(f2, (f3())));");
143 }
144 
145 TEST_F(FormatTest, NestedNameSpecifiers) {
146   verifyFormat("vector<::Type> v;");
147   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
148   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
149   verifyFormat("bool a = 2 < ::SomeFunction();");
150   verifyFormat("ALWAYS_INLINE ::std::string getName();");
151   verifyFormat("some::string getName();");
152 }
153 
154 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
155   EXPECT_EQ("if (a) {\n"
156             "  f();\n"
157             "}",
158             format("if(a){f();}"));
159   EXPECT_EQ(4, ReplacementCount);
160   EXPECT_EQ("if (a) {\n"
161             "  f();\n"
162             "}",
163             format("if (a) {\n"
164                    "  f();\n"
165                    "}"));
166   EXPECT_EQ(0, ReplacementCount);
167   EXPECT_EQ("/*\r\n"
168             "\r\n"
169             "*/\r\n",
170             format("/*\r\n"
171                    "\r\n"
172                    "*/\r\n"));
173   EXPECT_EQ(0, ReplacementCount);
174 }
175 
176 TEST_F(FormatTest, RemovesEmptyLines) {
177   EXPECT_EQ("class C {\n"
178             "  int i;\n"
179             "};",
180             format("class C {\n"
181                    " int i;\n"
182                    "\n"
183                    "};"));
184 
185   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
186   EXPECT_EQ("namespace N {\n"
187             "\n"
188             "int i;\n"
189             "}",
190             format("namespace N {\n"
191                    "\n"
192                    "int    i;\n"
193                    "}",
194                    getGoogleStyle()));
195   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
196             "\n"
197             "int i;\n"
198             "}",
199             format("extern /**/ \"C\" /**/ {\n"
200                    "\n"
201                    "int    i;\n"
202                    "}",
203                    getGoogleStyle()));
204 
205   // ...but do keep inlining and removing empty lines for non-block extern "C"
206   // functions.
207   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
208   EXPECT_EQ("extern \"C\" int f() {\n"
209             "  int i = 42;\n"
210             "  return i;\n"
211             "}",
212             format("extern \"C\" int f() {\n"
213                    "\n"
214                    "  int i = 42;\n"
215                    "  return i;\n"
216                    "}",
217                    getGoogleStyle()));
218 
219   // Remove empty lines at the beginning and end of blocks.
220   EXPECT_EQ("void f() {\n"
221             "\n"
222             "  if (a) {\n"
223             "\n"
224             "    f();\n"
225             "  }\n"
226             "}",
227             format("void f() {\n"
228                    "\n"
229                    "  if (a) {\n"
230                    "\n"
231                    "    f();\n"
232                    "\n"
233                    "  }\n"
234                    "\n"
235                    "}",
236                    getLLVMStyle()));
237   EXPECT_EQ("void f() {\n"
238             "  if (a) {\n"
239             "    f();\n"
240             "  }\n"
241             "}",
242             format("void f() {\n"
243                    "\n"
244                    "  if (a) {\n"
245                    "\n"
246                    "    f();\n"
247                    "\n"
248                    "  }\n"
249                    "\n"
250                    "}",
251                    getGoogleStyle()));
252 
253   // Don't remove empty lines in more complex control statements.
254   EXPECT_EQ("void f() {\n"
255             "  if (a) {\n"
256             "    f();\n"
257             "\n"
258             "  } else if (b) {\n"
259             "    f();\n"
260             "  }\n"
261             "}",
262             format("void f() {\n"
263                    "  if (a) {\n"
264                    "    f();\n"
265                    "\n"
266                    "  } else if (b) {\n"
267                    "    f();\n"
268                    "\n"
269                    "  }\n"
270                    "\n"
271                    "}"));
272 
273   // FIXME: This is slightly inconsistent.
274   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
275   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
276   EXPECT_EQ("namespace {\n"
277             "int i;\n"
278             "}",
279             format("namespace {\n"
280                    "int i;\n"
281                    "\n"
282                    "}", LLVMWithNoNamespaceFix));
283   EXPECT_EQ("namespace {\n"
284             "int i;\n"
285             "}",
286             format("namespace {\n"
287                    "int i;\n"
288                    "\n"
289                    "}"));
290   EXPECT_EQ("namespace {\n"
291             "int i;\n"
292             "\n"
293             "} // namespace",
294             format("namespace {\n"
295                    "int i;\n"
296                    "\n"
297                    "}  // namespace"));
298 
299   FormatStyle Style = getLLVMStyle();
300   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
301   Style.MaxEmptyLinesToKeep = 2;
302   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
303   Style.BraceWrapping.AfterClass = true;
304   Style.BraceWrapping.AfterFunction = true;
305   Style.KeepEmptyLinesAtTheStartOfBlocks = false;
306 
307   EXPECT_EQ("class Foo\n"
308             "{\n"
309             "  Foo() {}\n"
310             "\n"
311             "  void funk() {}\n"
312             "};",
313             format("class Foo\n"
314                    "{\n"
315                    "  Foo()\n"
316                    "  {\n"
317                    "  }\n"
318                    "\n"
319                    "  void funk() {}\n"
320                    "};",
321                    Style));
322 }
323 
324 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
325   verifyFormat("x = (a) and (b);");
326   verifyFormat("x = (a) or (b);");
327   verifyFormat("x = (a) bitand (b);");
328   verifyFormat("x = (a) bitor (b);");
329   verifyFormat("x = (a) not_eq (b);");
330   verifyFormat("x = (a) and_eq (b);");
331   verifyFormat("x = (a) or_eq (b);");
332   verifyFormat("x = (a) xor (b);");
333 }
334 
335 //===----------------------------------------------------------------------===//
336 // Tests for control statements.
337 //===----------------------------------------------------------------------===//
338 
339 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
340   verifyFormat("if (true)\n  f();\ng();");
341   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
342   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
343 
344   FormatStyle AllowsMergedIf = getLLVMStyle();
345   AllowsMergedIf.AlignEscapedNewlinesLeft = true;
346   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
347   verifyFormat("if (a)\n"
348                "  // comment\n"
349                "  f();",
350                AllowsMergedIf);
351   verifyFormat("{\n"
352                "  if (a)\n"
353                "  label:\n"
354                "    f();\n"
355                "}",
356                AllowsMergedIf);
357   verifyFormat("#define A \\\n"
358                "  if (a)  \\\n"
359                "  label:  \\\n"
360                "    f()",
361                AllowsMergedIf);
362   verifyFormat("if (a)\n"
363                "  ;",
364                AllowsMergedIf);
365   verifyFormat("if (a)\n"
366                "  if (b) return;",
367                AllowsMergedIf);
368 
369   verifyFormat("if (a) // Can't merge this\n"
370                "  f();\n",
371                AllowsMergedIf);
372   verifyFormat("if (a) /* still don't merge */\n"
373                "  f();",
374                AllowsMergedIf);
375   verifyFormat("if (a) { // Never merge this\n"
376                "  f();\n"
377                "}",
378                AllowsMergedIf);
379   verifyFormat("if (a) { /* Never merge this */\n"
380                "  f();\n"
381                "}",
382                AllowsMergedIf);
383 
384   AllowsMergedIf.ColumnLimit = 14;
385   verifyFormat("if (a) return;", AllowsMergedIf);
386   verifyFormat("if (aaaaaaaaa)\n"
387                "  return;",
388                AllowsMergedIf);
389 
390   AllowsMergedIf.ColumnLimit = 13;
391   verifyFormat("if (a)\n  return;", AllowsMergedIf);
392 }
393 
394 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
395   FormatStyle AllowsMergedLoops = getLLVMStyle();
396   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
397   verifyFormat("while (true) continue;", AllowsMergedLoops);
398   verifyFormat("for (;;) continue;", AllowsMergedLoops);
399   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
400   verifyFormat("while (true)\n"
401                "  ;",
402                AllowsMergedLoops);
403   verifyFormat("for (;;)\n"
404                "  ;",
405                AllowsMergedLoops);
406   verifyFormat("for (;;)\n"
407                "  for (;;) continue;",
408                AllowsMergedLoops);
409   verifyFormat("for (;;) // Can't merge this\n"
410                "  continue;",
411                AllowsMergedLoops);
412   verifyFormat("for (;;) /* still don't merge */\n"
413                "  continue;",
414                AllowsMergedLoops);
415 }
416 
417 TEST_F(FormatTest, FormatShortBracedStatements) {
418   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
419   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
420 
421   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
422   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
423 
424   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
425   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
426   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
427   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
428   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
429   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
430   verifyFormat("if (true) { //\n"
431                "  f();\n"
432                "}",
433                AllowSimpleBracedStatements);
434   verifyFormat("if (true) {\n"
435                "  f();\n"
436                "  f();\n"
437                "}",
438                AllowSimpleBracedStatements);
439   verifyFormat("if (true) {\n"
440                "  f();\n"
441                "} else {\n"
442                "  f();\n"
443                "}",
444                AllowSimpleBracedStatements);
445 
446   verifyFormat("template <int> struct A2 {\n"
447                "  struct B {};\n"
448                "};",
449                AllowSimpleBracedStatements);
450 
451   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
452   verifyFormat("if (true) {\n"
453                "  f();\n"
454                "}",
455                AllowSimpleBracedStatements);
456   verifyFormat("if (true) {\n"
457                "  f();\n"
458                "} else {\n"
459                "  f();\n"
460                "}",
461                AllowSimpleBracedStatements);
462 
463   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
464   verifyFormat("while (true) {\n"
465                "  f();\n"
466                "}",
467                AllowSimpleBracedStatements);
468   verifyFormat("for (;;) {\n"
469                "  f();\n"
470                "}",
471                AllowSimpleBracedStatements);
472 }
473 
474 TEST_F(FormatTest, ParseIfElse) {
475   verifyFormat("if (true)\n"
476                "  if (true)\n"
477                "    if (true)\n"
478                "      f();\n"
479                "    else\n"
480                "      g();\n"
481                "  else\n"
482                "    h();\n"
483                "else\n"
484                "  i();");
485   verifyFormat("if (true)\n"
486                "  if (true)\n"
487                "    if (true) {\n"
488                "      if (true)\n"
489                "        f();\n"
490                "    } else {\n"
491                "      g();\n"
492                "    }\n"
493                "  else\n"
494                "    h();\n"
495                "else {\n"
496                "  i();\n"
497                "}");
498   verifyFormat("void f() {\n"
499                "  if (a) {\n"
500                "  } else {\n"
501                "  }\n"
502                "}");
503 }
504 
505 TEST_F(FormatTest, ElseIf) {
506   verifyFormat("if (a) {\n} else if (b) {\n}");
507   verifyFormat("if (a)\n"
508                "  f();\n"
509                "else if (b)\n"
510                "  g();\n"
511                "else\n"
512                "  h();");
513   verifyFormat("if (a) {\n"
514                "  f();\n"
515                "}\n"
516                "// or else ..\n"
517                "else {\n"
518                "  g()\n"
519                "}");
520 
521   verifyFormat("if (a) {\n"
522                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
523                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
524                "}");
525   verifyFormat("if (a) {\n"
526                "} else if (\n"
527                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
528                "}",
529                getLLVMStyleWithColumns(62));
530 }
531 
532 TEST_F(FormatTest, FormatsForLoop) {
533   verifyFormat(
534       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
535       "     ++VeryVeryLongLoopVariable)\n"
536       "  ;");
537   verifyFormat("for (;;)\n"
538                "  f();");
539   verifyFormat("for (;;) {\n}");
540   verifyFormat("for (;;) {\n"
541                "  f();\n"
542                "}");
543   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
544 
545   verifyFormat(
546       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
547       "                                          E = UnwrappedLines.end();\n"
548       "     I != E; ++I) {\n}");
549 
550   verifyFormat(
551       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
552       "     ++IIIII) {\n}");
553   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
554                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
555                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
556   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
557                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
558                "         E = FD->getDeclsInPrototypeScope().end();\n"
559                "     I != E; ++I) {\n}");
560   verifyFormat("for (SmallVectorImpl<TemplateIdAnnotationn *>::iterator\n"
561                "         I = Container.begin(),\n"
562                "         E = Container.end();\n"
563                "     I != E; ++I) {\n}",
564                getLLVMStyleWithColumns(76));
565 
566   verifyFormat(
567       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
568       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
569       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
570       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
571       "     ++aaaaaaaaaaa) {\n}");
572   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
573                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
574                "     ++i) {\n}");
575   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
576                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
577                "}");
578   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
579                "         aaaaaaaaaa);\n"
580                "     iter; ++iter) {\n"
581                "}");
582   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
583                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
584                "     aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n"
585                "     ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {");
586 
587   FormatStyle NoBinPacking = getLLVMStyle();
588   NoBinPacking.BinPackParameters = false;
589   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
590                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
591                "                                           aaaaaaaaaaaaaaaa,\n"
592                "                                           aaaaaaaaaaaaaaaa,\n"
593                "                                           aaaaaaaaaaaaaaaa);\n"
594                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
595                "}",
596                NoBinPacking);
597   verifyFormat(
598       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
599       "                                          E = UnwrappedLines.end();\n"
600       "     I != E;\n"
601       "     ++I) {\n}",
602       NoBinPacking);
603 }
604 
605 TEST_F(FormatTest, RangeBasedForLoops) {
606   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
607                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
608   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
609                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
610   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
611                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
612   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
613                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
614 }
615 
616 TEST_F(FormatTest, ForEachLoops) {
617   verifyFormat("void f() {\n"
618                "  foreach (Item *item, itemlist) {}\n"
619                "  Q_FOREACH (Item *item, itemlist) {}\n"
620                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
621                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
622                "}");
623 
624   // As function-like macros.
625   verifyFormat("#define foreach(x, y)\n"
626                "#define Q_FOREACH(x, y)\n"
627                "#define BOOST_FOREACH(x, y)\n"
628                "#define UNKNOWN_FOREACH(x, y)\n");
629 
630   // Not as function-like macros.
631   verifyFormat("#define foreach (x, y)\n"
632                "#define Q_FOREACH (x, y)\n"
633                "#define BOOST_FOREACH (x, y)\n"
634                "#define UNKNOWN_FOREACH (x, y)\n");
635 }
636 
637 TEST_F(FormatTest, FormatsWhileLoop) {
638   verifyFormat("while (true) {\n}");
639   verifyFormat("while (true)\n"
640                "  f();");
641   verifyFormat("while () {\n}");
642   verifyFormat("while () {\n"
643                "  f();\n"
644                "}");
645 }
646 
647 TEST_F(FormatTest, FormatsDoWhile) {
648   verifyFormat("do {\n"
649                "  do_something();\n"
650                "} while (something());");
651   verifyFormat("do\n"
652                "  do_something();\n"
653                "while (something());");
654 }
655 
656 TEST_F(FormatTest, FormatsSwitchStatement) {
657   verifyFormat("switch (x) {\n"
658                "case 1:\n"
659                "  f();\n"
660                "  break;\n"
661                "case kFoo:\n"
662                "case ns::kBar:\n"
663                "case kBaz:\n"
664                "  break;\n"
665                "default:\n"
666                "  g();\n"
667                "  break;\n"
668                "}");
669   verifyFormat("switch (x) {\n"
670                "case 1: {\n"
671                "  f();\n"
672                "  break;\n"
673                "}\n"
674                "case 2: {\n"
675                "  break;\n"
676                "}\n"
677                "}");
678   verifyFormat("switch (x) {\n"
679                "case 1: {\n"
680                "  f();\n"
681                "  {\n"
682                "    g();\n"
683                "    h();\n"
684                "  }\n"
685                "  break;\n"
686                "}\n"
687                "}");
688   verifyFormat("switch (x) {\n"
689                "case 1: {\n"
690                "  f();\n"
691                "  if (foo) {\n"
692                "    g();\n"
693                "    h();\n"
694                "  }\n"
695                "  break;\n"
696                "}\n"
697                "}");
698   verifyFormat("switch (x) {\n"
699                "case 1: {\n"
700                "  f();\n"
701                "  g();\n"
702                "} break;\n"
703                "}");
704   verifyFormat("switch (test)\n"
705                "  ;");
706   verifyFormat("switch (x) {\n"
707                "default: {\n"
708                "  // Do nothing.\n"
709                "}\n"
710                "}");
711   verifyFormat("switch (x) {\n"
712                "// comment\n"
713                "// if 1, do f()\n"
714                "case 1:\n"
715                "  f();\n"
716                "}");
717   verifyFormat("switch (x) {\n"
718                "case 1:\n"
719                "  // Do amazing stuff\n"
720                "  {\n"
721                "    f();\n"
722                "    g();\n"
723                "  }\n"
724                "  break;\n"
725                "}");
726   verifyFormat("#define A          \\\n"
727                "  switch (x) {     \\\n"
728                "  case a:          \\\n"
729                "    foo = b;       \\\n"
730                "  }",
731                getLLVMStyleWithColumns(20));
732   verifyFormat("#define OPERATION_CASE(name)           \\\n"
733                "  case OP_name:                        \\\n"
734                "    return operations::Operation##name\n",
735                getLLVMStyleWithColumns(40));
736   verifyFormat("switch (x) {\n"
737                "case 1:;\n"
738                "default:;\n"
739                "  int i;\n"
740                "}");
741 
742   verifyGoogleFormat("switch (x) {\n"
743                      "  case 1:\n"
744                      "    f();\n"
745                      "    break;\n"
746                      "  case kFoo:\n"
747                      "  case ns::kBar:\n"
748                      "  case kBaz:\n"
749                      "    break;\n"
750                      "  default:\n"
751                      "    g();\n"
752                      "    break;\n"
753                      "}");
754   verifyGoogleFormat("switch (x) {\n"
755                      "  case 1: {\n"
756                      "    f();\n"
757                      "    break;\n"
758                      "  }\n"
759                      "}");
760   verifyGoogleFormat("switch (test)\n"
761                      "  ;");
762 
763   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
764                      "  case OP_name:              \\\n"
765                      "    return operations::Operation##name\n");
766   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
767                      "  // Get the correction operation class.\n"
768                      "  switch (OpCode) {\n"
769                      "    CASE(Add);\n"
770                      "    CASE(Subtract);\n"
771                      "    default:\n"
772                      "      return operations::Unknown;\n"
773                      "  }\n"
774                      "#undef OPERATION_CASE\n"
775                      "}");
776   verifyFormat("DEBUG({\n"
777                "  switch (x) {\n"
778                "  case A:\n"
779                "    f();\n"
780                "    break;\n"
781                "  // On B:\n"
782                "  case B:\n"
783                "    g();\n"
784                "    break;\n"
785                "  }\n"
786                "});");
787   verifyFormat("switch (a) {\n"
788                "case (b):\n"
789                "  return;\n"
790                "}");
791 
792   verifyFormat("switch (a) {\n"
793                "case some_namespace::\n"
794                "    some_constant:\n"
795                "  return;\n"
796                "}",
797                getLLVMStyleWithColumns(34));
798 }
799 
800 TEST_F(FormatTest, CaseRanges) {
801   verifyFormat("switch (x) {\n"
802                "case 'A' ... 'Z':\n"
803                "case 1 ... 5:\n"
804                "case a ... b:\n"
805                "  break;\n"
806                "}");
807 }
808 
809 TEST_F(FormatTest, ShortCaseLabels) {
810   FormatStyle Style = getLLVMStyle();
811   Style.AllowShortCaseLabelsOnASingleLine = true;
812   verifyFormat("switch (a) {\n"
813                "case 1: x = 1; break;\n"
814                "case 2: return;\n"
815                "case 3:\n"
816                "case 4:\n"
817                "case 5: return;\n"
818                "case 6: // comment\n"
819                "  return;\n"
820                "case 7:\n"
821                "  // comment\n"
822                "  return;\n"
823                "case 8:\n"
824                "  x = 8; // comment\n"
825                "  break;\n"
826                "default: y = 1; break;\n"
827                "}",
828                Style);
829   verifyFormat("switch (a) {\n"
830                "#if FOO\n"
831                "case 0: return 0;\n"
832                "#endif\n"
833                "}",
834                Style);
835   verifyFormat("switch (a) {\n"
836                "case 1: {\n"
837                "}\n"
838                "case 2: {\n"
839                "  return;\n"
840                "}\n"
841                "case 3: {\n"
842                "  x = 1;\n"
843                "  return;\n"
844                "}\n"
845                "case 4:\n"
846                "  if (x)\n"
847                "    return;\n"
848                "}",
849                Style);
850   Style.ColumnLimit = 21;
851   verifyFormat("switch (a) {\n"
852                "case 1: x = 1; break;\n"
853                "case 2: return;\n"
854                "case 3:\n"
855                "case 4:\n"
856                "case 5: return;\n"
857                "default:\n"
858                "  y = 1;\n"
859                "  break;\n"
860                "}",
861                Style);
862 }
863 
864 TEST_F(FormatTest, FormatsLabels) {
865   verifyFormat("void f() {\n"
866                "  some_code();\n"
867                "test_label:\n"
868                "  some_other_code();\n"
869                "  {\n"
870                "    some_more_code();\n"
871                "  another_label:\n"
872                "    some_more_code();\n"
873                "  }\n"
874                "}");
875   verifyFormat("{\n"
876                "  some_code();\n"
877                "test_label:\n"
878                "  some_other_code();\n"
879                "}");
880   verifyFormat("{\n"
881                "  some_code();\n"
882                "test_label:;\n"
883                "  int i = 0;\n"
884                "}");
885 }
886 
887 //===----------------------------------------------------------------------===//
888 // Tests for classes, namespaces, etc.
889 //===----------------------------------------------------------------------===//
890 
891 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
892   verifyFormat("class A {};");
893 }
894 
895 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
896   verifyFormat("class A {\n"
897                "public:\n"
898                "public: // comment\n"
899                "protected:\n"
900                "private:\n"
901                "  void f() {}\n"
902                "};");
903   verifyGoogleFormat("class A {\n"
904                      " public:\n"
905                      " protected:\n"
906                      " private:\n"
907                      "  void f() {}\n"
908                      "};");
909   verifyFormat("class A {\n"
910                "public slots:\n"
911                "  void f1() {}\n"
912                "public Q_SLOTS:\n"
913                "  void f2() {}\n"
914                "protected slots:\n"
915                "  void f3() {}\n"
916                "protected Q_SLOTS:\n"
917                "  void f4() {}\n"
918                "private slots:\n"
919                "  void f5() {}\n"
920                "private Q_SLOTS:\n"
921                "  void f6() {}\n"
922                "signals:\n"
923                "  void g1();\n"
924                "Q_SIGNALS:\n"
925                "  void g2();\n"
926                "};");
927 
928   // Don't interpret 'signals' the wrong way.
929   verifyFormat("signals.set();");
930   verifyFormat("for (Signals signals : f()) {\n}");
931   verifyFormat("{\n"
932                "  signals.set(); // This needs indentation.\n"
933                "}");
934   verifyFormat("void f() {\n"
935                "label:\n"
936                "  signals.baz();\n"
937                "}");
938 }
939 
940 TEST_F(FormatTest, SeparatesLogicalBlocks) {
941   EXPECT_EQ("class A {\n"
942             "public:\n"
943             "  void f();\n"
944             "\n"
945             "private:\n"
946             "  void g() {}\n"
947             "  // test\n"
948             "protected:\n"
949             "  int h;\n"
950             "};",
951             format("class A {\n"
952                    "public:\n"
953                    "void f();\n"
954                    "private:\n"
955                    "void g() {}\n"
956                    "// test\n"
957                    "protected:\n"
958                    "int h;\n"
959                    "};"));
960   EXPECT_EQ("class A {\n"
961             "protected:\n"
962             "public:\n"
963             "  void f();\n"
964             "};",
965             format("class A {\n"
966                    "protected:\n"
967                    "\n"
968                    "public:\n"
969                    "\n"
970                    "  void f();\n"
971                    "};"));
972 
973   // Even ensure proper spacing inside macros.
974   EXPECT_EQ("#define B     \\\n"
975             "  class A {   \\\n"
976             "   protected: \\\n"
977             "   public:    \\\n"
978             "    void f(); \\\n"
979             "  };",
980             format("#define B     \\\n"
981                    "  class A {   \\\n"
982                    "   protected: \\\n"
983                    "              \\\n"
984                    "   public:    \\\n"
985                    "              \\\n"
986                    "    void f(); \\\n"
987                    "  };",
988                    getGoogleStyle()));
989   // But don't remove empty lines after macros ending in access specifiers.
990   EXPECT_EQ("#define A private:\n"
991             "\n"
992             "int i;",
993             format("#define A         private:\n"
994                    "\n"
995                    "int              i;"));
996 }
997 
998 TEST_F(FormatTest, FormatsClasses) {
999   verifyFormat("class A : public B {};");
1000   verifyFormat("class A : public ::B {};");
1001 
1002   verifyFormat(
1003       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1004       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1005   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1006                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1007                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1008   verifyFormat(
1009       "class A : public B, public C, public D, public E, public F {};");
1010   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1011                "                     public C,\n"
1012                "                     public D,\n"
1013                "                     public E,\n"
1014                "                     public F,\n"
1015                "                     public G {};");
1016 
1017   verifyFormat("class\n"
1018                "    ReallyReallyLongClassName {\n"
1019                "  int i;\n"
1020                "};",
1021                getLLVMStyleWithColumns(32));
1022   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
1023                "                           aaaaaaaaaaaaaaaa> {};");
1024   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
1025                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
1026                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
1027   verifyFormat("template <class R, class C>\n"
1028                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
1029                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
1030   verifyFormat("class ::A::B {};");
1031 }
1032 
1033 TEST_F(FormatTest, BreakBeforeInheritanceComma) {
1034   FormatStyle StyleWithInheritanceBreak = getLLVMStyle();
1035   StyleWithInheritanceBreak.BreakBeforeInheritanceComma = true;
1036 
1037   verifyFormat("class MyClass : public X {};", StyleWithInheritanceBreak);
1038   verifyFormat("class MyClass\n"
1039                "    : public X\n"
1040                "    , public Y {};",
1041                StyleWithInheritanceBreak);
1042 }
1043 
1044 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
1045   verifyFormat("class A {\n} a, b;");
1046   verifyFormat("struct A {\n} a, b;");
1047   verifyFormat("union A {\n} a;");
1048 }
1049 
1050 TEST_F(FormatTest, FormatsEnum) {
1051   verifyFormat("enum {\n"
1052                "  Zero,\n"
1053                "  One = 1,\n"
1054                "  Two = One + 1,\n"
1055                "  Three = (One + Two),\n"
1056                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1057                "  Five = (One, Two, Three, Four, 5)\n"
1058                "};");
1059   verifyGoogleFormat("enum {\n"
1060                      "  Zero,\n"
1061                      "  One = 1,\n"
1062                      "  Two = One + 1,\n"
1063                      "  Three = (One + Two),\n"
1064                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1065                      "  Five = (One, Two, Three, Four, 5)\n"
1066                      "};");
1067   verifyFormat("enum Enum {};");
1068   verifyFormat("enum {};");
1069   verifyFormat("enum X E {} d;");
1070   verifyFormat("enum __attribute__((...)) E {} d;");
1071   verifyFormat("enum __declspec__((...)) E {} d;");
1072   verifyFormat("enum {\n"
1073                "  Bar = Foo<int, int>::value\n"
1074                "};",
1075                getLLVMStyleWithColumns(30));
1076 
1077   verifyFormat("enum ShortEnum { A, B, C };");
1078   verifyGoogleFormat("enum ShortEnum { A, B, C };");
1079 
1080   EXPECT_EQ("enum KeepEmptyLines {\n"
1081             "  ONE,\n"
1082             "\n"
1083             "  TWO,\n"
1084             "\n"
1085             "  THREE\n"
1086             "}",
1087             format("enum KeepEmptyLines {\n"
1088                    "  ONE,\n"
1089                    "\n"
1090                    "  TWO,\n"
1091                    "\n"
1092                    "\n"
1093                    "  THREE\n"
1094                    "}"));
1095   verifyFormat("enum E { // comment\n"
1096                "  ONE,\n"
1097                "  TWO\n"
1098                "};\n"
1099                "int i;");
1100   // Not enums.
1101   verifyFormat("enum X f() {\n"
1102                "  a();\n"
1103                "  return 42;\n"
1104                "}");
1105   verifyFormat("enum X Type::f() {\n"
1106                "  a();\n"
1107                "  return 42;\n"
1108                "}");
1109   verifyFormat("enum ::X f() {\n"
1110                "  a();\n"
1111                "  return 42;\n"
1112                "}");
1113   verifyFormat("enum ns::X f() {\n"
1114                "  a();\n"
1115                "  return 42;\n"
1116                "}");
1117 }
1118 
1119 TEST_F(FormatTest, FormatsEnumsWithErrors) {
1120   verifyFormat("enum Type {\n"
1121                "  One = 0; // These semicolons should be commas.\n"
1122                "  Two = 1;\n"
1123                "};");
1124   verifyFormat("namespace n {\n"
1125                "enum Type {\n"
1126                "  One,\n"
1127                "  Two, // missing };\n"
1128                "  int i;\n"
1129                "}\n"
1130                "void g() {}");
1131 }
1132 
1133 TEST_F(FormatTest, FormatsEnumStruct) {
1134   verifyFormat("enum struct {\n"
1135                "  Zero,\n"
1136                "  One = 1,\n"
1137                "  Two = One + 1,\n"
1138                "  Three = (One + Two),\n"
1139                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1140                "  Five = (One, Two, Three, Four, 5)\n"
1141                "};");
1142   verifyFormat("enum struct Enum {};");
1143   verifyFormat("enum struct {};");
1144   verifyFormat("enum struct X E {} d;");
1145   verifyFormat("enum struct __attribute__((...)) E {} d;");
1146   verifyFormat("enum struct __declspec__((...)) E {} d;");
1147   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
1148 }
1149 
1150 TEST_F(FormatTest, FormatsEnumClass) {
1151   verifyFormat("enum class {\n"
1152                "  Zero,\n"
1153                "  One = 1,\n"
1154                "  Two = One + 1,\n"
1155                "  Three = (One + Two),\n"
1156                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
1157                "  Five = (One, Two, Three, Four, 5)\n"
1158                "};");
1159   verifyFormat("enum class Enum {};");
1160   verifyFormat("enum class {};");
1161   verifyFormat("enum class X E {} d;");
1162   verifyFormat("enum class __attribute__((...)) E {} d;");
1163   verifyFormat("enum class __declspec__((...)) E {} d;");
1164   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
1165 }
1166 
1167 TEST_F(FormatTest, FormatsEnumTypes) {
1168   verifyFormat("enum X : int {\n"
1169                "  A, // Force multiple lines.\n"
1170                "  B\n"
1171                "};");
1172   verifyFormat("enum X : int { A, B };");
1173   verifyFormat("enum X : std::uint32_t { A, B };");
1174 }
1175 
1176 TEST_F(FormatTest, FormatsNSEnums) {
1177   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
1178   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
1179                      "  // Information about someDecentlyLongValue.\n"
1180                      "  someDecentlyLongValue,\n"
1181                      "  // Information about anotherDecentlyLongValue.\n"
1182                      "  anotherDecentlyLongValue,\n"
1183                      "  // Information about aThirdDecentlyLongValue.\n"
1184                      "  aThirdDecentlyLongValue\n"
1185                      "};");
1186   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
1187                      "  a = 1,\n"
1188                      "  b = 2,\n"
1189                      "  c = 3,\n"
1190                      "};");
1191   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
1192                      "  a = 1,\n"
1193                      "  b = 2,\n"
1194                      "  c = 3,\n"
1195                      "};");
1196   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
1197                      "  a = 1,\n"
1198                      "  b = 2,\n"
1199                      "  c = 3,\n"
1200                      "};");
1201 }
1202 
1203 TEST_F(FormatTest, FormatsBitfields) {
1204   verifyFormat("struct Bitfields {\n"
1205                "  unsigned sClass : 8;\n"
1206                "  unsigned ValueKind : 2;\n"
1207                "};");
1208   verifyFormat("struct A {\n"
1209                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
1210                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
1211                "};");
1212   verifyFormat("struct MyStruct {\n"
1213                "  uchar data;\n"
1214                "  uchar : 8;\n"
1215                "  uchar : 8;\n"
1216                "  uchar other;\n"
1217                "};");
1218 }
1219 
1220 TEST_F(FormatTest, FormatsNamespaces) {
1221   FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle();
1222   LLVMWithNoNamespaceFix.FixNamespaceComments = false;
1223 
1224   verifyFormat("namespace some_namespace {\n"
1225                "class A {};\n"
1226                "void f() { f(); }\n"
1227                "}",
1228                LLVMWithNoNamespaceFix);
1229   verifyFormat("namespace {\n"
1230                "class A {};\n"
1231                "void f() { f(); }\n"
1232                "}",
1233                LLVMWithNoNamespaceFix);
1234   verifyFormat("inline namespace X {\n"
1235                "class A {};\n"
1236                "void f() { f(); }\n"
1237                "}",
1238                LLVMWithNoNamespaceFix);
1239   verifyFormat("using namespace some_namespace;\n"
1240                "class A {};\n"
1241                "void f() { f(); }",
1242                LLVMWithNoNamespaceFix);
1243 
1244   // This code is more common than we thought; if we
1245   // layout this correctly the semicolon will go into
1246   // its own line, which is undesirable.
1247   verifyFormat("namespace {};",
1248                LLVMWithNoNamespaceFix);
1249   verifyFormat("namespace {\n"
1250                "class A {};\n"
1251                "};",
1252                LLVMWithNoNamespaceFix);
1253 
1254   verifyFormat("namespace {\n"
1255                "int SomeVariable = 0; // comment\n"
1256                "} // namespace",
1257                LLVMWithNoNamespaceFix);
1258   EXPECT_EQ("#ifndef HEADER_GUARD\n"
1259             "#define HEADER_GUARD\n"
1260             "namespace my_namespace {\n"
1261             "int i;\n"
1262             "} // my_namespace\n"
1263             "#endif // HEADER_GUARD",
1264             format("#ifndef HEADER_GUARD\n"
1265                    " #define HEADER_GUARD\n"
1266                    "   namespace my_namespace {\n"
1267                    "int i;\n"
1268                    "}    // my_namespace\n"
1269                    "#endif    // HEADER_GUARD",
1270                    LLVMWithNoNamespaceFix));
1271 
1272   EXPECT_EQ("namespace A::B {\n"
1273             "class C {};\n"
1274             "}",
1275             format("namespace A::B {\n"
1276                    "class C {};\n"
1277                    "}",
1278                    LLVMWithNoNamespaceFix));
1279 
1280   FormatStyle Style = getLLVMStyle();
1281   Style.NamespaceIndentation = FormatStyle::NI_All;
1282   EXPECT_EQ("namespace out {\n"
1283             "  int i;\n"
1284             "  namespace in {\n"
1285             "    int i;\n"
1286             "  } // namespace in\n"
1287             "} // namespace out",
1288             format("namespace out {\n"
1289                    "int i;\n"
1290                    "namespace in {\n"
1291                    "int i;\n"
1292                    "} // namespace in\n"
1293                    "} // namespace out",
1294                    Style));
1295 
1296   Style.NamespaceIndentation = FormatStyle::NI_Inner;
1297   EXPECT_EQ("namespace out {\n"
1298             "int i;\n"
1299             "namespace in {\n"
1300             "  int i;\n"
1301             "} // namespace in\n"
1302             "} // namespace out",
1303             format("namespace out {\n"
1304                    "int i;\n"
1305                    "namespace in {\n"
1306                    "int i;\n"
1307                    "} // namespace in\n"
1308                    "} // namespace out",
1309                    Style));
1310 }
1311 
1312 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
1313 
1314 TEST_F(FormatTest, FormatsInlineASM) {
1315   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
1316   verifyFormat("asm(\"nop\" ::: \"memory\");");
1317   verifyFormat(
1318       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
1319       "    \"cpuid\\n\\t\"\n"
1320       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
1321       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
1322       "    : \"a\"(value));");
1323   EXPECT_EQ(
1324       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
1325       "  __asm {\n"
1326       "        mov     edx,[that] // vtable in edx\n"
1327       "        mov     eax,methodIndex\n"
1328       "        call    [edx][eax*4] // stdcall\n"
1329       "  }\n"
1330       "}",
1331       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
1332              "    __asm {\n"
1333              "        mov     edx,[that] // vtable in edx\n"
1334              "        mov     eax,methodIndex\n"
1335              "        call    [edx][eax*4] // stdcall\n"
1336              "    }\n"
1337              "}"));
1338   EXPECT_EQ("_asm {\n"
1339             "  xor eax, eax;\n"
1340             "  cpuid;\n"
1341             "}",
1342             format("_asm {\n"
1343                    "  xor eax, eax;\n"
1344                    "  cpuid;\n"
1345                    "}"));
1346   verifyFormat("void function() {\n"
1347                "  // comment\n"
1348                "  asm(\"\");\n"
1349                "}");
1350   EXPECT_EQ("__asm {\n"
1351             "}\n"
1352             "int i;",
1353             format("__asm   {\n"
1354                    "}\n"
1355                    "int   i;"));
1356 }
1357 
1358 TEST_F(FormatTest, FormatTryCatch) {
1359   verifyFormat("try {\n"
1360                "  throw a * b;\n"
1361                "} catch (int a) {\n"
1362                "  // Do nothing.\n"
1363                "} catch (...) {\n"
1364                "  exit(42);\n"
1365                "}");
1366 
1367   // Function-level try statements.
1368   verifyFormat("int f() try { return 4; } catch (...) {\n"
1369                "  return 5;\n"
1370                "}");
1371   verifyFormat("class A {\n"
1372                "  int a;\n"
1373                "  A() try : a(0) {\n"
1374                "  } catch (...) {\n"
1375                "    throw;\n"
1376                "  }\n"
1377                "};\n");
1378 
1379   // Incomplete try-catch blocks.
1380   verifyIncompleteFormat("try {} catch (");
1381 }
1382 
1383 TEST_F(FormatTest, FormatSEHTryCatch) {
1384   verifyFormat("__try {\n"
1385                "  int a = b * c;\n"
1386                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
1387                "  // Do nothing.\n"
1388                "}");
1389 
1390   verifyFormat("__try {\n"
1391                "  int a = b * c;\n"
1392                "} __finally {\n"
1393                "  // Do nothing.\n"
1394                "}");
1395 
1396   verifyFormat("DEBUG({\n"
1397                "  __try {\n"
1398                "  } __finally {\n"
1399                "  }\n"
1400                "});\n");
1401 }
1402 
1403 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
1404   verifyFormat("try {\n"
1405                "  f();\n"
1406                "} catch {\n"
1407                "  g();\n"
1408                "}");
1409   verifyFormat("try {\n"
1410                "  f();\n"
1411                "} catch (A a) MACRO(x) {\n"
1412                "  g();\n"
1413                "} catch (B b) MACRO(x) {\n"
1414                "  g();\n"
1415                "}");
1416 }
1417 
1418 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
1419   FormatStyle Style = getLLVMStyle();
1420   for (auto BraceStyle : {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla,
1421                           FormatStyle::BS_WebKit}) {
1422     Style.BreakBeforeBraces = BraceStyle;
1423     verifyFormat("try {\n"
1424                  "  // something\n"
1425                  "} catch (...) {\n"
1426                  "  // something\n"
1427                  "}",
1428                  Style);
1429   }
1430   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
1431   verifyFormat("try {\n"
1432                "  // something\n"
1433                "}\n"
1434                "catch (...) {\n"
1435                "  // something\n"
1436                "}",
1437                Style);
1438   verifyFormat("__try {\n"
1439                "  // something\n"
1440                "}\n"
1441                "__finally {\n"
1442                "  // something\n"
1443                "}",
1444                Style);
1445   verifyFormat("@try {\n"
1446                "  // something\n"
1447                "}\n"
1448                "@finally {\n"
1449                "  // something\n"
1450                "}",
1451                Style);
1452   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
1453   verifyFormat("try\n"
1454                "{\n"
1455                "  // something\n"
1456                "}\n"
1457                "catch (...)\n"
1458                "{\n"
1459                "  // something\n"
1460                "}",
1461                Style);
1462   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
1463   verifyFormat("try\n"
1464                "  {\n"
1465                "    // something\n"
1466                "  }\n"
1467                "catch (...)\n"
1468                "  {\n"
1469                "    // something\n"
1470                "  }",
1471                Style);
1472   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1473   Style.BraceWrapping.BeforeCatch = true;
1474   verifyFormat("try {\n"
1475                "  // something\n"
1476                "}\n"
1477                "catch (...) {\n"
1478                "  // something\n"
1479                "}",
1480                Style);
1481 }
1482 
1483 TEST_F(FormatTest, StaticInitializers) {
1484   verifyFormat("static SomeClass SC = {1, 'a'};");
1485 
1486   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
1487                "    100000000, "
1488                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
1489 
1490   // Here, everything other than the "}" would fit on a line.
1491   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
1492                "    10000000000000000000000000};");
1493   EXPECT_EQ("S s = {a,\n"
1494             "\n"
1495             "       b};",
1496             format("S s = {\n"
1497                    "  a,\n"
1498                    "\n"
1499                    "  b\n"
1500                    "};"));
1501 
1502   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
1503   // line. However, the formatting looks a bit off and this probably doesn't
1504   // happen often in practice.
1505   verifyFormat("static int Variable[1] = {\n"
1506                "    {1000000000000000000000000000000000000}};",
1507                getLLVMStyleWithColumns(40));
1508 }
1509 
1510 TEST_F(FormatTest, DesignatedInitializers) {
1511   verifyFormat("const struct A a = {.a = 1, .b = 2};");
1512   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
1513                "                    .bbbbbbbbbb = 2,\n"
1514                "                    .cccccccccc = 3,\n"
1515                "                    .dddddddddd = 4,\n"
1516                "                    .eeeeeeeeee = 5};");
1517   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
1518                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
1519                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
1520                "    .ccccccccccccccccccccccccccc = 3,\n"
1521                "    .ddddddddddddddddddddddddddd = 4,\n"
1522                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
1523 
1524   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
1525 }
1526 
1527 TEST_F(FormatTest, NestedStaticInitializers) {
1528   verifyFormat("static A x = {{{}}};\n");
1529   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
1530                "               {init1, init2, init3, init4}}};",
1531                getLLVMStyleWithColumns(50));
1532 
1533   verifyFormat("somes Status::global_reps[3] = {\n"
1534                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1535                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1536                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
1537                getLLVMStyleWithColumns(60));
1538   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
1539                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
1540                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
1541                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
1542   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
1543                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
1544                "rect.fTop}};");
1545 
1546   verifyFormat(
1547       "SomeArrayOfSomeType a = {\n"
1548       "    {{1, 2, 3},\n"
1549       "     {1, 2, 3},\n"
1550       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
1551       "      333333333333333333333333333333},\n"
1552       "     {1, 2, 3},\n"
1553       "     {1, 2, 3}}};");
1554   verifyFormat(
1555       "SomeArrayOfSomeType a = {\n"
1556       "    {{1, 2, 3}},\n"
1557       "    {{1, 2, 3}},\n"
1558       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
1559       "      333333333333333333333333333333}},\n"
1560       "    {{1, 2, 3}},\n"
1561       "    {{1, 2, 3}}};");
1562 
1563   verifyFormat("struct {\n"
1564                "  unsigned bit;\n"
1565                "  const char *const name;\n"
1566                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
1567                "                 {kOsWin, \"Windows\"},\n"
1568                "                 {kOsLinux, \"Linux\"},\n"
1569                "                 {kOsCrOS, \"Chrome OS\"}};");
1570   verifyFormat("struct {\n"
1571                "  unsigned bit;\n"
1572                "  const char *const name;\n"
1573                "} kBitsToOs[] = {\n"
1574                "    {kOsMac, \"Mac\"},\n"
1575                "    {kOsWin, \"Windows\"},\n"
1576                "    {kOsLinux, \"Linux\"},\n"
1577                "    {kOsCrOS, \"Chrome OS\"},\n"
1578                "};");
1579 }
1580 
1581 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
1582   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
1583                "                      \\\n"
1584                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
1585 }
1586 
1587 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
1588   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
1589                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
1590 
1591   // Do break defaulted and deleted functions.
1592   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1593                "    default;",
1594                getLLVMStyleWithColumns(40));
1595   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
1596                "    delete;",
1597                getLLVMStyleWithColumns(40));
1598 }
1599 
1600 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
1601   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
1602                getLLVMStyleWithColumns(40));
1603   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1604                getLLVMStyleWithColumns(40));
1605   EXPECT_EQ("#define Q                              \\\n"
1606             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
1607             "  \"aaaaaaaa.cpp\"",
1608             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
1609                    getLLVMStyleWithColumns(40)));
1610 }
1611 
1612 TEST_F(FormatTest, UnderstandsLinePPDirective) {
1613   EXPECT_EQ("# 123 \"A string literal\"",
1614             format("   #     123    \"A string literal\""));
1615 }
1616 
1617 TEST_F(FormatTest, LayoutUnknownPPDirective) {
1618   EXPECT_EQ("#;", format("#;"));
1619   verifyFormat("#\n;\n;\n;");
1620 }
1621 
1622 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
1623   EXPECT_EQ("#line 42 \"test\"\n",
1624             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
1625   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
1626                                     getLLVMStyleWithColumns(12)));
1627 }
1628 
1629 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
1630   EXPECT_EQ("#line 42 \"test\"",
1631             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
1632   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
1633 }
1634 
1635 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
1636   verifyFormat("#define A \\x20");
1637   verifyFormat("#define A \\ x20");
1638   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
1639   verifyFormat("#define A ''");
1640   verifyFormat("#define A ''qqq");
1641   verifyFormat("#define A `qqq");
1642   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
1643   EXPECT_EQ("const char *c = STRINGIFY(\n"
1644             "\\na : b);",
1645             format("const char * c = STRINGIFY(\n"
1646                    "\\na : b);"));
1647 
1648   verifyFormat("a\r\\");
1649   verifyFormat("a\v\\");
1650   verifyFormat("a\f\\");
1651 }
1652 
1653 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
1654   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
1655   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
1656   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
1657   // FIXME: We never break before the macro name.
1658   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
1659 
1660   verifyFormat("#define A A\n#define A A");
1661   verifyFormat("#define A(X) A\n#define A A");
1662 
1663   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
1664   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
1665 }
1666 
1667 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
1668   EXPECT_EQ("// somecomment\n"
1669             "#include \"a.h\"\n"
1670             "#define A(  \\\n"
1671             "    A, B)\n"
1672             "#include \"b.h\"\n"
1673             "// somecomment\n",
1674             format("  // somecomment\n"
1675                    "  #include \"a.h\"\n"
1676                    "#define A(A,\\\n"
1677                    "    B)\n"
1678                    "    #include \"b.h\"\n"
1679                    " // somecomment\n",
1680                    getLLVMStyleWithColumns(13)));
1681 }
1682 
1683 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
1684 
1685 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
1686   EXPECT_EQ("#define A    \\\n"
1687             "  c;         \\\n"
1688             "  e;\n"
1689             "f;",
1690             format("#define A c; e;\n"
1691                    "f;",
1692                    getLLVMStyleWithColumns(14)));
1693 }
1694 
1695 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
1696 
1697 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
1698   EXPECT_EQ("int x,\n"
1699             "#define A\n"
1700             "    y;",
1701             format("int x,\n#define A\ny;"));
1702 }
1703 
1704 TEST_F(FormatTest, HashInMacroDefinition) {
1705   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
1706   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
1707   verifyFormat("#define A  \\\n"
1708                "  {        \\\n"
1709                "    f(#c); \\\n"
1710                "  }",
1711                getLLVMStyleWithColumns(11));
1712 
1713   verifyFormat("#define A(X)         \\\n"
1714                "  void function##X()",
1715                getLLVMStyleWithColumns(22));
1716 
1717   verifyFormat("#define A(a, b, c)   \\\n"
1718                "  void a##b##c()",
1719                getLLVMStyleWithColumns(22));
1720 
1721   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
1722 }
1723 
1724 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
1725   EXPECT_EQ("#define A (x)", format("#define A (x)"));
1726   EXPECT_EQ("#define A(x)", format("#define A(x)"));
1727 }
1728 
1729 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
1730   EXPECT_EQ("#define A b;", format("#define A \\\n"
1731                                    "          \\\n"
1732                                    "  b;",
1733                                    getLLVMStyleWithColumns(25)));
1734   EXPECT_EQ("#define A \\\n"
1735             "          \\\n"
1736             "  a;      \\\n"
1737             "  b;",
1738             format("#define A \\\n"
1739                    "          \\\n"
1740                    "  a;      \\\n"
1741                    "  b;",
1742                    getLLVMStyleWithColumns(11)));
1743   EXPECT_EQ("#define A \\\n"
1744             "  a;      \\\n"
1745             "          \\\n"
1746             "  b;",
1747             format("#define A \\\n"
1748                    "  a;      \\\n"
1749                    "          \\\n"
1750                    "  b;",
1751                    getLLVMStyleWithColumns(11)));
1752 }
1753 
1754 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
1755   verifyIncompleteFormat("#define A :");
1756   verifyFormat("#define SOMECASES  \\\n"
1757                "  case 1:          \\\n"
1758                "  case 2\n",
1759                getLLVMStyleWithColumns(20));
1760   verifyFormat("#define MACRO(a) \\\n"
1761                "  if (a)         \\\n"
1762                "    f();         \\\n"
1763                "  else           \\\n"
1764                "    g()",
1765                getLLVMStyleWithColumns(18));
1766   verifyFormat("#define A template <typename T>");
1767   verifyIncompleteFormat("#define STR(x) #x\n"
1768                          "f(STR(this_is_a_string_literal{));");
1769   verifyFormat("#pragma omp threadprivate( \\\n"
1770                "    y)), // expected-warning",
1771                getLLVMStyleWithColumns(28));
1772   verifyFormat("#d, = };");
1773   verifyFormat("#if \"a");
1774   verifyIncompleteFormat("({\n"
1775                          "#define b     \\\n"
1776                          "  }           \\\n"
1777                          "  a\n"
1778                          "a",
1779                          getLLVMStyleWithColumns(15));
1780   verifyFormat("#define A     \\\n"
1781                "  {           \\\n"
1782                "    {\n"
1783                "#define B     \\\n"
1784                "  }           \\\n"
1785                "  }",
1786                getLLVMStyleWithColumns(15));
1787   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
1788   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
1789   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
1790   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
1791 }
1792 
1793 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
1794   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
1795   EXPECT_EQ("class A : public QObject {\n"
1796             "  Q_OBJECT\n"
1797             "\n"
1798             "  A() {}\n"
1799             "};",
1800             format("class A  :  public QObject {\n"
1801                    "     Q_OBJECT\n"
1802                    "\n"
1803                    "  A() {\n}\n"
1804                    "}  ;"));
1805   EXPECT_EQ("MACRO\n"
1806             "/*static*/ int i;",
1807             format("MACRO\n"
1808                    " /*static*/ int   i;"));
1809   EXPECT_EQ("SOME_MACRO\n"
1810             "namespace {\n"
1811             "void f();\n"
1812             "} // namespace",
1813             format("SOME_MACRO\n"
1814                    "  namespace    {\n"
1815                    "void   f(  );\n"
1816                    "} // namespace"));
1817   // Only if the identifier contains at least 5 characters.
1818   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
1819   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
1820   // Only if everything is upper case.
1821   EXPECT_EQ("class A : public QObject {\n"
1822             "  Q_Object A() {}\n"
1823             "};",
1824             format("class A  :  public QObject {\n"
1825                    "     Q_Object\n"
1826                    "  A() {\n}\n"
1827                    "}  ;"));
1828 
1829   // Only if the next line can actually start an unwrapped line.
1830   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
1831             format("SOME_WEIRD_LOG_MACRO\n"
1832                    "<< SomeThing;"));
1833 
1834   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
1835                "(n, buffers))\n",
1836                getChromiumStyle(FormatStyle::LK_Cpp));
1837 }
1838 
1839 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
1840   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1841             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1842             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1843             "class X {};\n"
1844             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1845             "int *createScopDetectionPass() { return 0; }",
1846             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
1847                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
1848                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
1849                    "  class X {};\n"
1850                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
1851                    "  int *createScopDetectionPass() { return 0; }"));
1852   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
1853   // braces, so that inner block is indented one level more.
1854   EXPECT_EQ("int q() {\n"
1855             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1856             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1857             "  IPC_END_MESSAGE_MAP()\n"
1858             "}",
1859             format("int q() {\n"
1860                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
1861                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
1862                    "  IPC_END_MESSAGE_MAP()\n"
1863                    "}"));
1864 
1865   // Same inside macros.
1866   EXPECT_EQ("#define LIST(L) \\\n"
1867             "  L(A)          \\\n"
1868             "  L(B)          \\\n"
1869             "  L(C)",
1870             format("#define LIST(L) \\\n"
1871                    "  L(A) \\\n"
1872                    "  L(B) \\\n"
1873                    "  L(C)",
1874                    getGoogleStyle()));
1875 
1876   // These must not be recognized as macros.
1877   EXPECT_EQ("int q() {\n"
1878             "  f(x);\n"
1879             "  f(x) {}\n"
1880             "  f(x)->g();\n"
1881             "  f(x)->*g();\n"
1882             "  f(x).g();\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) <<= x;\n"
1894             "  f(x)[y].z();\n"
1895             "  LOG(INFO) << x;\n"
1896             "  ifstream(x) >> x;\n"
1897             "}\n",
1898             format("int q() {\n"
1899                    "  f(x)\n;\n"
1900                    "  f(x)\n {}\n"
1901                    "  f(x)\n->g();\n"
1902                    "  f(x)\n->*g();\n"
1903                    "  f(x)\n.g();\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 <<= x;\n"
1915                    "  f(x)\n[y].z();\n"
1916                    "  LOG(INFO)\n << x;\n"
1917                    "  ifstream(x)\n >> x;\n"
1918                    "}\n"));
1919   EXPECT_EQ("int q() {\n"
1920             "  F(x)\n"
1921             "  if (1) {\n"
1922             "  }\n"
1923             "  F(x)\n"
1924             "  while (1) {\n"
1925             "  }\n"
1926             "  F(x)\n"
1927             "  G(x);\n"
1928             "  F(x)\n"
1929             "  try {\n"
1930             "    Q();\n"
1931             "  } catch (...) {\n"
1932             "  }\n"
1933             "}\n",
1934             format("int q() {\n"
1935                    "F(x)\n"
1936                    "if (1) {}\n"
1937                    "F(x)\n"
1938                    "while (1) {}\n"
1939                    "F(x)\n"
1940                    "G(x);\n"
1941                    "F(x)\n"
1942                    "try { Q(); } catch (...) {}\n"
1943                    "}\n"));
1944   EXPECT_EQ("class A {\n"
1945             "  A() : t(0) {}\n"
1946             "  A(int i) noexcept() : {}\n"
1947             "  A(X x)\n" // FIXME: function-level try blocks are broken.
1948             "  try : t(0) {\n"
1949             "  } catch (...) {\n"
1950             "  }\n"
1951             "};",
1952             format("class A {\n"
1953                    "  A()\n : t(0) {}\n"
1954                    "  A(int i)\n noexcept() : {}\n"
1955                    "  A(X x)\n"
1956                    "  try : t(0) {} catch (...) {}\n"
1957                    "};"));
1958   EXPECT_EQ("class SomeClass {\n"
1959             "public:\n"
1960             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1961             "};",
1962             format("class SomeClass {\n"
1963                    "public:\n"
1964                    "  SomeClass()\n"
1965                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1966                    "};"));
1967   EXPECT_EQ("class SomeClass {\n"
1968             "public:\n"
1969             "  SomeClass()\n"
1970             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1971             "};",
1972             format("class SomeClass {\n"
1973                    "public:\n"
1974                    "  SomeClass()\n"
1975                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
1976                    "};",
1977                    getLLVMStyleWithColumns(40)));
1978 
1979   verifyFormat("MACRO(>)");
1980 }
1981 
1982 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
1983   verifyFormat("#define A \\\n"
1984                "  f({     \\\n"
1985                "    g();  \\\n"
1986                "  });",
1987                getLLVMStyleWithColumns(11));
1988 }
1989 
1990 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
1991   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
1992 }
1993 
1994 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
1995   verifyFormat("{\n  { a #c; }\n}");
1996 }
1997 
1998 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
1999   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2000             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2001   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2002             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2003 }
2004 
2005 TEST_F(FormatTest, EscapedNewlines) {
2006   EXPECT_EQ(
2007       "#define A \\\n  int i;  \\\n  int j;",
2008       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2009   EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
2010   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2011   EXPECT_EQ("/* \\  \\  \\\n */", format("\\\n/* \\  \\  \\\n */"));
2012   EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
2013 }
2014 
2015 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2016   verifyFormat("#define A \\\n"
2017                "  int v(  \\\n"
2018                "      a); \\\n"
2019                "  int i;",
2020                getLLVMStyleWithColumns(11));
2021 }
2022 
2023 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2024   EXPECT_EQ(
2025       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2026       "                      \\\n"
2027       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2028       "\n"
2029       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2030       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2031       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2032              "\\\n"
2033              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2034              "  \n"
2035              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2036              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2037 }
2038 
2039 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2040   EXPECT_EQ("int\n"
2041             "#define A\n"
2042             "    a;",
2043             format("int\n#define A\na;"));
2044   verifyFormat("functionCallTo(\n"
2045                "    someOtherFunction(\n"
2046                "        withSomeParameters, whichInSequence,\n"
2047                "        areLongerThanALine(andAnotherCall,\n"
2048                "#define A B\n"
2049                "                           withMoreParamters,\n"
2050                "                           whichStronglyInfluenceTheLayout),\n"
2051                "        andMoreParameters),\n"
2052                "    trailing);",
2053                getLLVMStyleWithColumns(69));
2054   verifyFormat("Foo::Foo()\n"
2055                "#ifdef BAR\n"
2056                "    : baz(0)\n"
2057                "#endif\n"
2058                "{\n"
2059                "}");
2060   verifyFormat("void f() {\n"
2061                "  if (true)\n"
2062                "#ifdef A\n"
2063                "    f(42);\n"
2064                "  x();\n"
2065                "#else\n"
2066                "    g();\n"
2067                "  x();\n"
2068                "#endif\n"
2069                "}");
2070   verifyFormat("void f(param1, param2,\n"
2071                "       param3,\n"
2072                "#ifdef A\n"
2073                "       param4(param5,\n"
2074                "#ifdef A1\n"
2075                "              param6,\n"
2076                "#ifdef A2\n"
2077                "              param7),\n"
2078                "#else\n"
2079                "              param8),\n"
2080                "       param9,\n"
2081                "#endif\n"
2082                "       param10,\n"
2083                "#endif\n"
2084                "       param11)\n"
2085                "#else\n"
2086                "       param12)\n"
2087                "#endif\n"
2088                "{\n"
2089                "  x();\n"
2090                "}",
2091                getLLVMStyleWithColumns(28));
2092   verifyFormat("#if 1\n"
2093                "int i;");
2094   verifyFormat("#if 1\n"
2095                "#endif\n"
2096                "#if 1\n"
2097                "#else\n"
2098                "#endif\n");
2099   verifyFormat("DEBUG({\n"
2100                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2101                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
2102                "});\n"
2103                "#if a\n"
2104                "#else\n"
2105                "#endif");
2106 
2107   verifyIncompleteFormat("void f(\n"
2108                          "#if A\n"
2109                          "    );\n"
2110                          "#else\n"
2111                          "#endif");
2112 }
2113 
2114 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
2115   verifyFormat("#endif\n"
2116                "#if B");
2117 }
2118 
2119 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
2120   FormatStyle SingleLine = getLLVMStyle();
2121   SingleLine.AllowShortIfStatementsOnASingleLine = true;
2122   verifyFormat("#if 0\n"
2123                "#elif 1\n"
2124                "#endif\n"
2125                "void foo() {\n"
2126                "  if (test) foo2();\n"
2127                "}",
2128                SingleLine);
2129 }
2130 
2131 TEST_F(FormatTest, LayoutBlockInsideParens) {
2132   verifyFormat("functionCall({ int i; });");
2133   verifyFormat("functionCall({\n"
2134                "  int i;\n"
2135                "  int j;\n"
2136                "});");
2137   verifyFormat("functionCall(\n"
2138                "    {\n"
2139                "      int i;\n"
2140                "      int j;\n"
2141                "    },\n"
2142                "    aaaa, bbbb, cccc);");
2143   verifyFormat("functionA(functionB({\n"
2144                "            int i;\n"
2145                "            int j;\n"
2146                "          }),\n"
2147                "          aaaa, bbbb, cccc);");
2148   verifyFormat("functionCall(\n"
2149                "    {\n"
2150                "      int i;\n"
2151                "      int j;\n"
2152                "    },\n"
2153                "    aaaa, bbbb, // comment\n"
2154                "    cccc);");
2155   verifyFormat("functionA(functionB({\n"
2156                "            int i;\n"
2157                "            int j;\n"
2158                "          }),\n"
2159                "          aaaa, bbbb, // comment\n"
2160                "          cccc);");
2161   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
2162   verifyFormat("functionCall(aaaa, bbbb, {\n"
2163                "  int i;\n"
2164                "  int j;\n"
2165                "});");
2166   verifyFormat(
2167       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
2168       "    {\n"
2169       "      int i; // break\n"
2170       "    },\n"
2171       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
2172       "                                     ccccccccccccccccc));");
2173   verifyFormat("DEBUG({\n"
2174                "  if (a)\n"
2175                "    f();\n"
2176                "});");
2177 }
2178 
2179 TEST_F(FormatTest, LayoutBlockInsideStatement) {
2180   EXPECT_EQ("SOME_MACRO { int i; }\n"
2181             "int i;",
2182             format("  SOME_MACRO  {int i;}  int i;"));
2183 }
2184 
2185 TEST_F(FormatTest, LayoutNestedBlocks) {
2186   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
2187                "  struct s {\n"
2188                "    int i;\n"
2189                "  };\n"
2190                "  s kBitsToOs[] = {{10}};\n"
2191                "  for (int i = 0; i < 10; ++i)\n"
2192                "    return;\n"
2193                "}");
2194   verifyFormat("call(parameter, {\n"
2195                "  something();\n"
2196                "  // Comment using all columns.\n"
2197                "  somethingelse();\n"
2198                "});",
2199                getLLVMStyleWithColumns(40));
2200   verifyFormat("DEBUG( //\n"
2201                "    { f(); }, a);");
2202   verifyFormat("DEBUG( //\n"
2203                "    {\n"
2204                "      f(); //\n"
2205                "    },\n"
2206                "    a);");
2207 
2208   EXPECT_EQ("call(parameter, {\n"
2209             "  something();\n"
2210             "  // Comment too\n"
2211             "  // looooooooooong.\n"
2212             "  somethingElse();\n"
2213             "});",
2214             format("call(parameter, {\n"
2215                    "  something();\n"
2216                    "  // Comment too looooooooooong.\n"
2217                    "  somethingElse();\n"
2218                    "});",
2219                    getLLVMStyleWithColumns(29)));
2220   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
2221   EXPECT_EQ("DEBUG({ // comment\n"
2222             "  int i;\n"
2223             "});",
2224             format("DEBUG({ // comment\n"
2225                    "int  i;\n"
2226                    "});"));
2227   EXPECT_EQ("DEBUG({\n"
2228             "  int i;\n"
2229             "\n"
2230             "  // comment\n"
2231             "  int j;\n"
2232             "});",
2233             format("DEBUG({\n"
2234                    "  int  i;\n"
2235                    "\n"
2236                    "  // comment\n"
2237                    "  int  j;\n"
2238                    "});"));
2239 
2240   verifyFormat("DEBUG({\n"
2241                "  if (a)\n"
2242                "    return;\n"
2243                "});");
2244   verifyGoogleFormat("DEBUG({\n"
2245                      "  if (a) return;\n"
2246                      "});");
2247   FormatStyle Style = getGoogleStyle();
2248   Style.ColumnLimit = 45;
2249   verifyFormat("Debug(aaaaa,\n"
2250                "      {\n"
2251                "        if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
2252                "      },\n"
2253                "      a);",
2254                Style);
2255 
2256   verifyFormat("SomeFunction({MACRO({ return output; }), b});");
2257 
2258   verifyNoCrash("^{v^{a}}");
2259 }
2260 
2261 TEST_F(FormatTest, FormatNestedBlocksInMacros) {
2262   EXPECT_EQ("#define MACRO()                     \\\n"
2263             "  Debug(aaa, /* force line break */ \\\n"
2264             "        {                           \\\n"
2265             "          int i;                    \\\n"
2266             "          int j;                    \\\n"
2267             "        })",
2268             format("#define   MACRO()   Debug(aaa,  /* force line break */ \\\n"
2269                    "          {  int   i;  int  j;   })",
2270                    getGoogleStyle()));
2271 
2272   EXPECT_EQ("#define A                                       \\\n"
2273             "  [] {                                          \\\n"
2274             "    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(        \\\n"
2275             "        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
2276             "  }",
2277             format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n"
2278                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }",
2279                    getGoogleStyle()));
2280 }
2281 
2282 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
2283   EXPECT_EQ("{}", format("{}"));
2284   verifyFormat("enum E {};");
2285   verifyFormat("enum E {}");
2286 }
2287 
2288 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
2289   FormatStyle Style = getLLVMStyle();
2290   Style.MacroBlockBegin = "^[A-Z_]+_BEGIN$";
2291   Style.MacroBlockEnd = "^[A-Z_]+_END$";
2292   verifyFormat("FOO_BEGIN\n"
2293                "  FOO_ENTRY\n"
2294                "FOO_END", Style);
2295   verifyFormat("FOO_BEGIN\n"
2296                "  NESTED_FOO_BEGIN\n"
2297                "    NESTED_FOO_ENTRY\n"
2298                "  NESTED_FOO_END\n"
2299                "FOO_END", Style);
2300   verifyFormat("FOO_BEGIN(Foo, Bar)\n"
2301                "  int x;\n"
2302                "  x = 1;\n"
2303                "FOO_END(Baz)", Style);
2304 }
2305 
2306 //===----------------------------------------------------------------------===//
2307 // Line break tests.
2308 //===----------------------------------------------------------------------===//
2309 
2310 TEST_F(FormatTest, PreventConfusingIndents) {
2311   verifyFormat(
2312       "void f() {\n"
2313       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
2314       "                         parameter, parameter, parameter)),\n"
2315       "                     SecondLongCall(parameter));\n"
2316       "}");
2317   verifyFormat(
2318       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2319       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2320       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2321       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
2322   verifyFormat(
2323       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2324       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
2325       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
2326       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
2327   verifyFormat(
2328       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
2329       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
2330       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
2331       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
2332   verifyFormat("int a = bbbb && ccc &&\n"
2333                "        fffff(\n"
2334                "#define A Just forcing a new line\n"
2335                "            ddd);");
2336 }
2337 
2338 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
2339   verifyFormat(
2340       "bool aaaaaaa =\n"
2341       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
2342       "    bbbbbbbb();");
2343   verifyFormat(
2344       "bool aaaaaaa =\n"
2345       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
2346       "    bbbbbbbb();");
2347 
2348   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2349                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
2350                "    ccccccccc == ddddddddddd;");
2351   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
2352                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
2353                "    ccccccccc == ddddddddddd;");
2354   verifyFormat(
2355       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
2356       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
2357       "    ccccccccc == ddddddddddd;");
2358 
2359   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2360                "                 aaaaaa) &&\n"
2361                "         bbbbbb && cccccc;");
2362   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
2363                "                 aaaaaa) >>\n"
2364                "         bbbbbb;");
2365   verifyFormat("aa = Whitespaces.addUntouchableComment(\n"
2366                "    SourceMgr.getSpellingColumnNumber(\n"
2367                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
2368                "    1);");
2369 
2370   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2371                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
2372                "    cccccc) {\n}");
2373   verifyFormat("b = a &&\n"
2374                "    // Comment\n"
2375                "    b.c && d;");
2376 
2377   // If the LHS of a comparison is not a binary expression itself, the
2378   // additional linebreak confuses many people.
2379   verifyFormat(
2380       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2381       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
2382       "}");
2383   verifyFormat(
2384       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2385       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2386       "}");
2387   verifyFormat(
2388       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
2389       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2390       "}");
2391   // Even explicit parentheses stress the precedence enough to make the
2392   // additional break unnecessary.
2393   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2394                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
2395                "}");
2396   // This cases is borderline, but with the indentation it is still readable.
2397   verifyFormat(
2398       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2399       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2400       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
2401       "}",
2402       getLLVMStyleWithColumns(75));
2403 
2404   // If the LHS is a binary expression, we should still use the additional break
2405   // as otherwise the formatting hides the operator precedence.
2406   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2407                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2408                "    5) {\n"
2409                "}");
2410 
2411   FormatStyle OnePerLine = getLLVMStyle();
2412   OnePerLine.BinPackParameters = false;
2413   verifyFormat(
2414       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2415       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
2416       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
2417       OnePerLine);
2418 
2419   verifyFormat("int i = someFunction(aaaaaaa, 0)\n"
2420                "                .aaa(aaaaaaaaaaaaa) *\n"
2421                "            aaaaaaa +\n"
2422                "        aaaaaaa;",
2423                getLLVMStyleWithColumns(40));
2424 }
2425 
2426 TEST_F(FormatTest, ExpressionIndentation) {
2427   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2428                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2429                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2430                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2431                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
2432                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
2433                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2434                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
2435                "                 ccccccccccccccccccccccccccccccccccccccccc;");
2436   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2437                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2438                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2439                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2440   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2441                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2442                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2443                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2444   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
2445                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
2446                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2447                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
2448   verifyFormat("if () {\n"
2449                "} else if (aaaaa && bbbbb > // break\n"
2450                "                        ccccc) {\n"
2451                "}");
2452   verifyFormat("if () {\n"
2453                "} else if (aaaaa &&\n"
2454                "           bbbbb > // break\n"
2455                "               ccccc &&\n"
2456                "           ddddd) {\n"
2457                "}");
2458 
2459   // Presence of a trailing comment used to change indentation of b.
2460   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
2461                "       b;\n"
2462                "return aaaaaaaaaaaaaaaaaaa +\n"
2463                "       b; //",
2464                getLLVMStyleWithColumns(30));
2465 }
2466 
2467 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
2468   // Not sure what the best system is here. Like this, the LHS can be found
2469   // immediately above an operator (everything with the same or a higher
2470   // indent). The RHS is aligned right of the operator and so compasses
2471   // everything until something with the same indent as the operator is found.
2472   // FIXME: Is this a good system?
2473   FormatStyle Style = getLLVMStyle();
2474   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
2475   verifyFormat(
2476       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2477       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2478       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2479       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2480       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2481       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2482       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2483       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2484       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
2485       Style);
2486   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2487                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2488                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2489                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2490                Style);
2491   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2492                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2493                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2494                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2495                Style);
2496   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2497                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2498                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2499                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
2500                Style);
2501   verifyFormat("if () {\n"
2502                "} else if (aaaaa\n"
2503                "           && bbbbb // break\n"
2504                "                  > ccccc) {\n"
2505                "}",
2506                Style);
2507   verifyFormat("return (a)\n"
2508                "       // comment\n"
2509                "       + b;",
2510                Style);
2511   verifyFormat(
2512       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2513       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2514       "             + cc;",
2515       Style);
2516 
2517   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2518                "    = aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
2519                Style);
2520 
2521   // Forced by comments.
2522   verifyFormat(
2523       "unsigned ContentSize =\n"
2524       "    sizeof(int16_t)   // DWARF ARange version number\n"
2525       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2526       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2527       "    + sizeof(int8_t); // Segment Size (in bytes)");
2528 
2529   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2530                "       == boost::fusion::at_c<1>(iiii).second;",
2531                Style);
2532 
2533   Style.ColumnLimit = 60;
2534   verifyFormat("zzzzzzzzzz\n"
2535                "    = bbbbbbbbbbbbbbbbb\n"
2536                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
2537                Style);
2538 }
2539 
2540 TEST_F(FormatTest, EnforcedOperatorWraps) {
2541   // Here we'd like to wrap after the || operators, but a comment is forcing an
2542   // earlier wrap.
2543   verifyFormat("bool x = aaaaa //\n"
2544                "         || bbbbb\n"
2545                "         //\n"
2546                "         || cccc;");
2547 }
2548 
2549 TEST_F(FormatTest, NoOperandAlignment) {
2550   FormatStyle Style = getLLVMStyle();
2551   Style.AlignOperands = false;
2552   verifyFormat("aaaaaaaaaaaaaa(aaaaaaaaaaaa,\n"
2553                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
2554                "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
2555                Style);
2556   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2557   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2558                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2559                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2560                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2561                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2562                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2563                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2564                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2565                "        > ccccccccccccccccccccccccccccccccccccccccc;",
2566                Style);
2567 
2568   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2569                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2570                "    + cc;",
2571                Style);
2572   verifyFormat("int a = aa\n"
2573                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
2574                "        * cccccccccccccccccccccccccccccccccccc;\n",
2575                Style);
2576 
2577   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
2578   verifyFormat("return (a > b\n"
2579                "    // comment1\n"
2580                "    // comment2\n"
2581                "    || c);",
2582                Style);
2583 }
2584 
2585 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
2586   FormatStyle Style = getLLVMStyle();
2587   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
2588   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
2589                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2590                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
2591                Style);
2592 }
2593 
2594 TEST_F(FormatTest, ConstructorInitializers) {
2595   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2596   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2597                getLLVMStyleWithColumns(45));
2598   verifyFormat("Constructor()\n"
2599                "    : Inttializer(FitsOnTheLine) {}",
2600                getLLVMStyleWithColumns(44));
2601   verifyFormat("Constructor()\n"
2602                "    : Inttializer(FitsOnTheLine) {}",
2603                getLLVMStyleWithColumns(43));
2604 
2605   verifyFormat("template <typename T>\n"
2606                "Constructor() : Initializer(FitsOnTheLine) {}",
2607                getLLVMStyleWithColumns(45));
2608 
2609   verifyFormat(
2610       "SomeClass::Constructor()\n"
2611       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2612 
2613   verifyFormat(
2614       "SomeClass::Constructor()\n"
2615       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2616       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2617   verifyFormat(
2618       "SomeClass::Constructor()\n"
2619       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2620       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2621   verifyFormat("Constructor(aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2622                "            aaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2623                "    : aaaaaaaaaa(aaaaaa) {}");
2624 
2625   verifyFormat("Constructor()\n"
2626                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2627                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2628                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2629                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2630 
2631   verifyFormat("Constructor()\n"
2632                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2633                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2634 
2635   verifyFormat("Constructor(int Parameter = 0)\n"
2636                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2637                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2638   verifyFormat("Constructor()\n"
2639                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
2640                "}",
2641                getLLVMStyleWithColumns(60));
2642   verifyFormat("Constructor()\n"
2643                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2644                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
2645 
2646   // Here a line could be saved by splitting the second initializer onto two
2647   // lines, but that is not desirable.
2648   verifyFormat("Constructor()\n"
2649                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
2650                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
2651                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2652 
2653   FormatStyle OnePerLine = getLLVMStyle();
2654   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2655   OnePerLine.AllowAllParametersOfDeclarationOnNextLine = false;
2656   verifyFormat("SomeClass::Constructor()\n"
2657                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2658                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2659                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2660                OnePerLine);
2661   verifyFormat("SomeClass::Constructor()\n"
2662                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
2663                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2664                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
2665                OnePerLine);
2666   verifyFormat("MyClass::MyClass(int var)\n"
2667                "    : some_var_(var),            // 4 space indent\n"
2668                "      some_other_var_(var + 1) { // lined up\n"
2669                "}",
2670                OnePerLine);
2671   verifyFormat("Constructor()\n"
2672                "    : aaaaa(aaaaaa),\n"
2673                "      aaaaa(aaaaaa),\n"
2674                "      aaaaa(aaaaaa),\n"
2675                "      aaaaa(aaaaaa),\n"
2676                "      aaaaa(aaaaaa) {}",
2677                OnePerLine);
2678   verifyFormat("Constructor()\n"
2679                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
2680                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
2681                OnePerLine);
2682   OnePerLine.BinPackParameters = false;
2683   verifyFormat(
2684       "Constructor()\n"
2685       "    : aaaaaaaaaaaaaaaaaaaaaaaa(\n"
2686       "          aaaaaaaaaaa().aaa(),\n"
2687       "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2688       OnePerLine);
2689   OnePerLine.ColumnLimit = 60;
2690   verifyFormat("Constructor()\n"
2691                "    : aaaaaaaaaaaaaaaaaaaa(a),\n"
2692                "      bbbbbbbbbbbbbbbbbbbbbbbb(b) {}",
2693                OnePerLine);
2694 
2695   EXPECT_EQ("Constructor()\n"
2696             "    : // Comment forcing unwanted break.\n"
2697             "      aaaa(aaaa) {}",
2698             format("Constructor() :\n"
2699                    "    // Comment forcing unwanted break.\n"
2700                    "    aaaa(aaaa) {}"));
2701 }
2702 
2703 TEST_F(FormatTest, MemoizationTests) {
2704   // This breaks if the memoization lookup does not take \c Indent and
2705   // \c LastSpace into account.
2706   verifyFormat(
2707       "extern CFRunLoopTimerRef\n"
2708       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
2709       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
2710       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
2711       "                     CFRunLoopTimerContext *context) {}");
2712 
2713   // Deep nesting somewhat works around our memoization.
2714   verifyFormat(
2715       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2716       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2717       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2718       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
2719       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
2720       getLLVMStyleWithColumns(65));
2721   verifyFormat(
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,\n"
2746       "                                                aaaaa))))))))))));",
2747       getLLVMStyleWithColumns(65));
2748   verifyFormat(
2749       "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"
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),\n"
2766       "  a)",
2767       getLLVMStyleWithColumns(65));
2768 
2769   // This test takes VERY long when memoization is broken.
2770   FormatStyle OnePerLine = getLLVMStyle();
2771   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
2772   OnePerLine.BinPackParameters = false;
2773   std::string input = "Constructor()\n"
2774                       "    : aaaa(a,\n";
2775   for (unsigned i = 0, e = 80; i != e; ++i) {
2776     input += "           a,\n";
2777   }
2778   input += "           a) {}";
2779   verifyFormat(input, OnePerLine);
2780 }
2781 
2782 TEST_F(FormatTest, BreaksAsHighAsPossible) {
2783   verifyFormat(
2784       "void f() {\n"
2785       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
2786       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
2787       "    f();\n"
2788       "}");
2789   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
2790                "    Intervals[i - 1].getRange().getLast()) {\n}");
2791 }
2792 
2793 TEST_F(FormatTest, BreaksFunctionDeclarations) {
2794   // Principially, we break function declarations in a certain order:
2795   // 1) break amongst arguments.
2796   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
2797                "                              Cccccccccccccc cccccccccccccc);");
2798   verifyFormat("template <class TemplateIt>\n"
2799                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
2800                "                            TemplateIt *stop) {}");
2801 
2802   // 2) break after return type.
2803   verifyFormat(
2804       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2805       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
2806       getGoogleStyle());
2807 
2808   // 3) break after (.
2809   verifyFormat(
2810       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
2811       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
2812       getGoogleStyle());
2813 
2814   // 4) break before after nested name specifiers.
2815   verifyFormat(
2816       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2817       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
2818       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
2819       getGoogleStyle());
2820 
2821   // However, there are exceptions, if a sufficient amount of lines can be
2822   // saved.
2823   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
2824   // more adjusting.
2825   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2826                "                                  Cccccccccccccc cccccccccc,\n"
2827                "                                  Cccccccccccccc cccccccccc,\n"
2828                "                                  Cccccccccccccc cccccccccc,\n"
2829                "                                  Cccccccccccccc cccccccccc);");
2830   verifyFormat(
2831       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2832       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2833       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2834       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
2835       getGoogleStyle());
2836   verifyFormat(
2837       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
2838       "                                          Cccccccccccccc cccccccccc,\n"
2839       "                                          Cccccccccccccc cccccccccc,\n"
2840       "                                          Cccccccccccccc cccccccccc,\n"
2841       "                                          Cccccccccccccc cccccccccc,\n"
2842       "                                          Cccccccccccccc cccccccccc,\n"
2843       "                                          Cccccccccccccc cccccccccc);");
2844   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
2845                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2846                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2847                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
2848                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
2849 
2850   // Break after multi-line parameters.
2851   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2852                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2853                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2854                "    bbbb bbbb);");
2855   verifyFormat("void SomeLoooooooooooongFunction(\n"
2856                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
2857                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2858                "    int bbbbbbbbbbbbb);");
2859 
2860   // Treat overloaded operators like other functions.
2861   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2862                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
2863   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2864                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
2865   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
2866                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
2867   verifyGoogleFormat(
2868       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
2869       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
2870   verifyGoogleFormat(
2871       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
2872       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
2873   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2874                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
2875   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
2876                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
2877   verifyGoogleFormat(
2878       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
2879       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2880       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
2881   verifyGoogleFormat(
2882       "template <typename T>\n"
2883       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2884       "aaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaaaaa(\n"
2885       "    aaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaa);");
2886 
2887   FormatStyle Style = getLLVMStyle();
2888   Style.PointerAlignment = FormatStyle::PAS_Left;
2889   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2890                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
2891                Style);
2892   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
2893                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
2894                Style);
2895 }
2896 
2897 TEST_F(FormatTest, TrailingReturnType) {
2898   verifyFormat("auto foo() -> int;\n");
2899   verifyFormat("struct S {\n"
2900                "  auto bar() const -> int;\n"
2901                "};");
2902   verifyFormat("template <size_t Order, typename T>\n"
2903                "auto load_img(const std::string &filename)\n"
2904                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
2905   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
2906                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
2907   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
2908   verifyFormat("template <typename T>\n"
2909                "auto aaaaaaaaaaaaaaaaaaaaaa(T t)\n"
2910                "    -> decltype(eaaaaaaaaaaaaaaa<T>(t.a).aaaaaaaa());");
2911 
2912   // Not trailing return types.
2913   verifyFormat("void f() { auto a = b->c(); }");
2914 }
2915 
2916 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
2917   // Avoid breaking before trailing 'const' or other trailing annotations, if
2918   // they are not function-like.
2919   FormatStyle Style = getGoogleStyle();
2920   Style.ColumnLimit = 47;
2921   verifyFormat("void someLongFunction(\n"
2922                "    int someLoooooooooooooongParameter) const {\n}",
2923                getLLVMStyleWithColumns(47));
2924   verifyFormat("LoooooongReturnType\n"
2925                "someLoooooooongFunction() const {}",
2926                getLLVMStyleWithColumns(47));
2927   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
2928                "    const {}",
2929                Style);
2930   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2931                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
2932   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2933                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
2934   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
2935                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
2936   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
2937                "                   aaaaaaaaaaa aaaaa) const override;");
2938   verifyGoogleFormat(
2939       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
2940       "    const override;");
2941 
2942   // Even if the first parameter has to be wrapped.
2943   verifyFormat("void someLongFunction(\n"
2944                "    int someLongParameter) const {}",
2945                getLLVMStyleWithColumns(46));
2946   verifyFormat("void someLongFunction(\n"
2947                "    int someLongParameter) const {}",
2948                Style);
2949   verifyFormat("void someLongFunction(\n"
2950                "    int someLongParameter) override {}",
2951                Style);
2952   verifyFormat("void someLongFunction(\n"
2953                "    int someLongParameter) OVERRIDE {}",
2954                Style);
2955   verifyFormat("void someLongFunction(\n"
2956                "    int someLongParameter) final {}",
2957                Style);
2958   verifyFormat("void someLongFunction(\n"
2959                "    int someLongParameter) FINAL {}",
2960                Style);
2961   verifyFormat("void someLongFunction(\n"
2962                "    int parameter) const override {}",
2963                Style);
2964 
2965   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2966   verifyFormat("void someLongFunction(\n"
2967                "    int someLongParameter) const\n"
2968                "{\n"
2969                "}",
2970                Style);
2971 
2972   // Unless these are unknown annotations.
2973   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
2974                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2975                "    LONG_AND_UGLY_ANNOTATION;");
2976 
2977   // Breaking before function-like trailing annotations is fine to keep them
2978   // close to their arguments.
2979   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
2980                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2981   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2982                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
2983   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
2984                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
2985   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
2986                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
2987   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
2988 
2989   verifyFormat(
2990       "void aaaaaaaaaaaaaaaaaa()\n"
2991       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
2992       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
2993   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2994                "    __attribute__((unused));");
2995   verifyGoogleFormat(
2996       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
2997       "    GUARDED_BY(aaaaaaaaaaaa);");
2998   verifyGoogleFormat(
2999       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3000       "    GUARDED_BY(aaaaaaaaaaaa);");
3001   verifyGoogleFormat(
3002       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3003       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3004   verifyGoogleFormat(
3005       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3006       "    aaaaaaaaaaaaaaaaaaaaaaaaa;");
3007 }
3008 
3009 TEST_F(FormatTest, FunctionAnnotations) {
3010   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3011                "int OldFunction(const string &parameter) {}");
3012   verifyFormat("DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3013                "string OldFunction(const string &parameter) {}");
3014   verifyFormat("template <typename T>\n"
3015                "DEPRECATED(\"Use NewClass::NewFunction instead.\")\n"
3016                "string OldFunction(const string &parameter) {}");
3017 
3018   // Not function annotations.
3019   verifyFormat("ASSERT(\"aaaaa\") << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3020                "                << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
3021   verifyFormat("TEST_F(ThisIsATestFixtureeeeeeeeeeeee,\n"
3022                "       ThisIsATestWithAReallyReallyReallyReallyLongName) {}");
3023   verifyFormat("MACRO(abc).function() // wrap\n"
3024                "    << abc;");
3025   verifyFormat("MACRO(abc)->function() // wrap\n"
3026                "    << abc;");
3027   verifyFormat("MACRO(abc)::function() // wrap\n"
3028                "    << abc;");
3029 }
3030 
3031 TEST_F(FormatTest, BreaksDesireably) {
3032   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3033                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3034                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3035   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3036                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3037                "}");
3038 
3039   verifyFormat(
3040       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3041       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3042 
3043   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3044                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3045                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3046 
3047   verifyFormat(
3048       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3049       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3050       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3051       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3052       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3053 
3054   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3055                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3056 
3057   verifyFormat(
3058       "void f() {\n"
3059       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3060       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3061       "}");
3062   verifyFormat(
3063       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3064       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3065   verifyFormat(
3066       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3067       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3068   verifyFormat(
3069       "aaaaaa(aaa,\n"
3070       "       new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3071       "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3072       "       aaaa);");
3073   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3074                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3075                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3076 
3077   // Indent consistently independent of call expression and unary operator.
3078   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3079                "    dddddddddddddddddddddddddddddd));");
3080   verifyFormat("aaaaaaaaaaa(!bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3081                "    dddddddddddddddddddddddddddddd));");
3082   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3083                "    dddddddddddddddddddddddddddddd));");
3084 
3085   // This test case breaks on an incorrect memoization, i.e. an optimization not
3086   // taking into account the StopAt value.
3087   verifyFormat(
3088       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3089       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3090       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3091       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3092 
3093   verifyFormat("{\n  {\n    {\n"
3094                "      Annotation.SpaceRequiredBefore =\n"
3095                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3096                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3097                "    }\n  }\n}");
3098 
3099   // Break on an outer level if there was a break on an inner level.
3100   EXPECT_EQ("f(g(h(a, // comment\n"
3101             "      b, c),\n"
3102             "    d, e),\n"
3103             "  x, y);",
3104             format("f(g(h(a, // comment\n"
3105                    "    b, c), d, e), x, y);"));
3106 
3107   // Prefer breaking similar line breaks.
3108   verifyFormat(
3109       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3110       "                             NSTrackingMouseEnteredAndExited |\n"
3111       "                             NSTrackingActiveAlways;");
3112 }
3113 
3114 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
3115   FormatStyle NoBinPacking = getGoogleStyle();
3116   NoBinPacking.BinPackParameters = false;
3117   NoBinPacking.BinPackArguments = true;
3118   verifyFormat("void f() {\n"
3119                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
3120                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3121                "}",
3122                NoBinPacking);
3123   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
3124                "       int aaaaaaaaaaaaaaaaaaaa,\n"
3125                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3126                NoBinPacking);
3127 
3128   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3129   verifyFormat("void aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3130                "                        vector<int> bbbbbbbbbbbbbbb);",
3131                NoBinPacking);
3132   // FIXME: This behavior difference is probably not wanted. However, currently
3133   // we cannot distinguish BreakBeforeParameter being set because of the wrapped
3134   // template arguments from BreakBeforeParameter being set because of the
3135   // one-per-line formatting.
3136   verifyFormat(
3137       "void fffffffffff(aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3138       "                                             aaaaaaaaaa> aaaaaaaaaa);",
3139       NoBinPacking);
3140   verifyFormat(
3141       "void fffffffffff(\n"
3142       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaa>\n"
3143       "        aaaaaaaaaa);");
3144 }
3145 
3146 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3147   FormatStyle NoBinPacking = getGoogleStyle();
3148   NoBinPacking.BinPackParameters = false;
3149   NoBinPacking.BinPackArguments = false;
3150   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3151                "  aaaaaaaaaaaaaaaaaaaa,\n"
3152                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3153                NoBinPacking);
3154   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3155                "        aaaaaaaaaaaaa,\n"
3156                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3157                NoBinPacking);
3158   verifyFormat(
3159       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3160       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3161       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3162       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3163       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3164       NoBinPacking);
3165   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3166                "    .aaaaaaaaaaaaaaaaaa();",
3167                NoBinPacking);
3168   verifyFormat("void f() {\n"
3169                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3170                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3171                "}",
3172                NoBinPacking);
3173 
3174   verifyFormat(
3175       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3176       "             aaaaaaaaaaaa,\n"
3177       "             aaaaaaaaaaaa);",
3178       NoBinPacking);
3179   verifyFormat(
3180       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3181       "                               ddddddddddddddddddddddddddddd),\n"
3182       "             test);",
3183       NoBinPacking);
3184 
3185   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3186                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3187                "            aaaaaaaaaaaaaaaaaaaaaaa>\n"
3188                "    aaaaaaaaaaaaaaaaaa;",
3189                NoBinPacking);
3190   verifyFormat("a(\"a\"\n"
3191                "  \"a\",\n"
3192                "  a);");
3193 
3194   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3195   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3196                "                aaaaaaaaa,\n"
3197                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3198                NoBinPacking);
3199   verifyFormat(
3200       "void f() {\n"
3201       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3202       "      .aaaaaaa();\n"
3203       "}",
3204       NoBinPacking);
3205   verifyFormat(
3206       "template <class SomeType, class SomeOtherType>\n"
3207       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3208       NoBinPacking);
3209 }
3210 
3211 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3212   FormatStyle Style = getLLVMStyleWithColumns(15);
3213   Style.ExperimentalAutoDetectBinPacking = true;
3214   EXPECT_EQ("aaa(aaaa,\n"
3215             "    aaaa,\n"
3216             "    aaaa);\n"
3217             "aaa(aaaa,\n"
3218             "    aaaa,\n"
3219             "    aaaa);",
3220             format("aaa(aaaa,\n" // one-per-line
3221                    "  aaaa,\n"
3222                    "    aaaa  );\n"
3223                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3224                    Style));
3225   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3226             "    aaaa);\n"
3227             "aaa(aaaa, aaaa,\n"
3228             "    aaaa);",
3229             format("aaa(aaaa,  aaaa,\n" // bin-packed
3230                    "    aaaa  );\n"
3231                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3232                    Style));
3233 }
3234 
3235 TEST_F(FormatTest, FormatsBuilderPattern) {
3236   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
3237                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3238                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3239                "    .StartsWith(\".init\", ORDER_INIT)\n"
3240                "    .StartsWith(\".fini\", ORDER_FINI)\n"
3241                "    .StartsWith(\".hash\", ORDER_HASH)\n"
3242                "    .Default(ORDER_TEXT);\n");
3243 
3244   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3245                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3246   verifyFormat(
3247       "aaaaaaa->aaaaaaa\n"
3248       "    ->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3249       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3250       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3251   verifyFormat(
3252       "aaaaaaa->aaaaaaa\n"
3253       "    ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3254       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3255   verifyFormat(
3256       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3257       "    aaaaaaaaaaaaaa);");
3258   verifyFormat(
3259       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3260       "    aaaaaa->aaaaaaaaaaaa()\n"
3261       "        ->aaaaaaaaaaaaaaaa(\n"
3262       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3263       "        ->aaaaaaaaaaaaaaaaa();");
3264   verifyGoogleFormat(
3265       "void f() {\n"
3266       "  someo->Add((new util::filetools::Handler(dir))\n"
3267       "                 ->OnEvent1(NewPermanentCallback(\n"
3268       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3269       "                 ->OnEvent2(NewPermanentCallback(\n"
3270       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3271       "                 ->OnEvent3(NewPermanentCallback(\n"
3272       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3273       "                 ->OnEvent5(NewPermanentCallback(\n"
3274       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3275       "                 ->OnEvent6(NewPermanentCallback(\n"
3276       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3277       "}");
3278 
3279   verifyFormat(
3280       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3281   verifyFormat("aaaaaaaaaaaaaaa()\n"
3282                "    .aaaaaaaaaaaaaaa()\n"
3283                "    .aaaaaaaaaaaaaaa()\n"
3284                "    .aaaaaaaaaaaaaaa()\n"
3285                "    .aaaaaaaaaaaaaaa();");
3286   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3287                "    .aaaaaaaaaaaaaaa()\n"
3288                "    .aaaaaaaaaaaaaaa()\n"
3289                "    .aaaaaaaaaaaaaaa();");
3290   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3291                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3292                "    .aaaaaaaaaaaaaaa();");
3293   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3294                "    ->aaaaaaaaaaaaaae(0)\n"
3295                "    ->aaaaaaaaaaaaaaa();");
3296 
3297   // Don't linewrap after very short segments.
3298   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3299                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3300                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3301   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3302                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3303                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3304   verifyFormat("aaa()\n"
3305                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3306                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3307                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
3308 
3309   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3310                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3311                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3312   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3313                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3314                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3315 
3316   // Prefer not to break after empty parentheses.
3317   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3318                "    First->LastNewlineOffset);");
3319 
3320   // Prefer not to create "hanging" indents.
3321   verifyFormat(
3322       "return !soooooooooooooome_map\n"
3323       "            .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3324       "            .second;");
3325   verifyFormat(
3326       "return aaaaaaaaaaaaaaaa\n"
3327       "    .aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)\n"
3328       "    .aaaa(aaaaaaaaaaaaaa);");
3329   // No hanging indent here.
3330   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa.aaaaaaaaaaaaaaa(\n"
3331                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3332   verifyFormat("aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaa().aaaaaaaaaaaaaaa(\n"
3333                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3334   verifyFormat("aaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3335                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3336                getLLVMStyleWithColumns(60));
3337   verifyFormat("aaaaaaaaaaaaaaaaaa\n"
3338                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa)\n"
3339                "    .aaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3340                getLLVMStyleWithColumns(59));
3341   verifyFormat("aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3342                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3343                "    .aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3344 }
3345 
3346 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3347   verifyFormat(
3348       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3349       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3350   verifyFormat(
3351       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
3352       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
3353 
3354   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3355                "    ccccccccccccccccccccccccc) {\n}");
3356   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
3357                "    ccccccccccccccccccccccccc) {\n}");
3358 
3359   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3360                "    ccccccccccccccccccccccccc) {\n}");
3361   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
3362                "    ccccccccccccccccccccccccc) {\n}");
3363 
3364   verifyFormat(
3365       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3366       "    ccccccccccccccccccccccccc) {\n}");
3367   verifyFormat(
3368       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
3369       "    ccccccccccccccccccccccccc) {\n}");
3370 
3371   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3372                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3373                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3374                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3375   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
3376                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
3377                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
3378                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3379 
3380   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3381                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3382                "    aaaaaaaaaaaaaaa != aa) {\n}");
3383   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
3384                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
3385                "    aaaaaaaaaaaaaaa != aa) {\n}");
3386 }
3387 
3388 TEST_F(FormatTest, BreaksAfterAssignments) {
3389   verifyFormat(
3390       "unsigned Cost =\n"
3391       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3392       "                        SI->getPointerAddressSpaceee());\n");
3393   verifyFormat(
3394       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3395       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3396 
3397   verifyFormat(
3398       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3399       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3400   verifyFormat("unsigned OriginalStartColumn =\n"
3401                "    SourceMgr.getSpellingColumnNumber(\n"
3402                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3403                "    1;");
3404 }
3405 
3406 TEST_F(FormatTest, AlignsAfterAssignments) {
3407   verifyFormat(
3408       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3409       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3410   verifyFormat(
3411       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3412       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3413   verifyFormat(
3414       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3415       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3416   verifyFormat(
3417       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3418       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3419   verifyFormat(
3420       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3421       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3422       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3423 }
3424 
3425 TEST_F(FormatTest, AlignsAfterReturn) {
3426   verifyFormat(
3427       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3428       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3429   verifyFormat(
3430       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3431       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3432   verifyFormat(
3433       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3434       "       aaaaaaaaaaaaaaaaaaaaaa();");
3435   verifyFormat(
3436       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3437       "        aaaaaaaaaaaaaaaaaaaaaa());");
3438   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3439                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3440   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3441                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3442                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3443   verifyFormat("return\n"
3444                "    // true if code is one of a or b.\n"
3445                "    code == a || code == b;");
3446 }
3447 
3448 TEST_F(FormatTest, AlignsAfterOpenBracket) {
3449   verifyFormat(
3450       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3451       "                                                aaaaaaaaa aaaaaaa) {}");
3452   verifyFormat(
3453       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3454       "                                               aaaaaaaaaaa aaaaaaaaa);");
3455   verifyFormat(
3456       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3457       "                                             aaaaaaaaaaaaaaaaaaaaa));");
3458   FormatStyle Style = getLLVMStyle();
3459   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3460   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3461                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
3462                Style);
3463   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3464                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
3465                Style);
3466   verifyFormat("SomeLongVariableName->someFunction(\n"
3467                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
3468                Style);
3469   verifyFormat(
3470       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
3471       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3472       Style);
3473   verifyFormat(
3474       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
3475       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3476       Style);
3477   verifyFormat(
3478       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
3479       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3480       Style);
3481 
3482   verifyFormat("bbbbbbbbbbbb(aaaaaaaaaaaaaaaaaaaaaaaa, //\n"
3483                "    ccccccc(aaaaaaaaaaaaaaaaa,         //\n"
3484                "        b));",
3485                Style);
3486 
3487   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
3488   Style.BinPackArguments = false;
3489   Style.BinPackParameters = false;
3490   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3491                "    aaaaaaaaaaa aaaaaaaa,\n"
3492                "    aaaaaaaaa aaaaaaa,\n"
3493                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3494                Style);
3495   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
3496                "    aaaaaaaaaaa aaaaaaaaa,\n"
3497                "    aaaaaaaaaaa aaaaaaaaa,\n"
3498                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3499                Style);
3500   verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n"
3501                "    aaaaaaaaaaaaaaa,\n"
3502                "    aaaaaaaaaaaaaaaaaaaaa,\n"
3503                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
3504                Style);
3505   verifyFormat(
3506       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n"
3507       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3508       Style);
3509   verifyFormat(
3510       "aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n"
3511       "    aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)));",
3512       Style);
3513   verifyFormat(
3514       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3515       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3516       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)),\n"
3517       "    aaaaaaaaaaaaaaaa);",
3518       Style);
3519   verifyFormat(
3520       "aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3521       "    aaaaaaaaaaaaaaaaaaaaa(\n"
3522       "        aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n"
3523       "    aaaaaaaaaaaaaaaa);",
3524       Style);
3525 }
3526 
3527 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
3528   FormatStyle Style = getLLVMStyleWithColumns(40);
3529   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3530                "          bbbbbbbbbbbbbbbbbbbbbb);",
3531                Style);
3532   Style.AlignAfterOpenBracket = FormatStyle::BAS_Align;
3533   Style.AlignOperands = false;
3534   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3535                "          bbbbbbbbbbbbbbbbbbbbbb);",
3536                Style);
3537   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3538   Style.AlignOperands = true;
3539   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3540                "          bbbbbbbbbbbbbbbbbbbbbb);",
3541                Style);
3542   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
3543   Style.AlignOperands = false;
3544   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
3545                "    bbbbbbbbbbbbbbbbbbbbbb);",
3546                Style);
3547 }
3548 
3549 TEST_F(FormatTest, BreaksConditionalExpressions) {
3550   verifyFormat(
3551       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3552       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3553       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3554   verifyFormat(
3555       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3556       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3557       "                                : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3558   verifyFormat(
3559       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3560       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3561   verifyFormat(
3562       "aaaa(aaaaaaaaa, aaaaaaaaa,\n"
3563       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3564       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3565   verifyFormat(
3566       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
3567       "                                                    : aaaaaaaaaaaaa);");
3568   verifyFormat(
3569       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3570       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3571       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3572       "                   aaaaaaaaaaaaa);");
3573   verifyFormat(
3574       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3575       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3576       "                   aaaaaaaaaaaaa);");
3577   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3578                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3579                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3580                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3581                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3582   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3583                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3584                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3585                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3586                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3587                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3588                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3589   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3590                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3591                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3592                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3593                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3594   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3595                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3596                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3597   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3598                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3599                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3600                "        : aaaaaaaaaaaaaaaa;");
3601   verifyFormat(
3602       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3603       "    ? aaaaaaaaaaaaaaa\n"
3604       "    : aaaaaaaaaaaaaaa;");
3605   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3606                "          aaaaaaaaa\n"
3607                "      ? b\n"
3608                "      : c);");
3609   verifyFormat("return aaaa == bbbb\n"
3610                "           // comment\n"
3611                "           ? aaaa\n"
3612                "           : bbbb;");
3613   verifyFormat("unsigned Indent =\n"
3614                "    format(TheLine.First,\n"
3615                "           IndentForLevel[TheLine.Level] >= 0\n"
3616                "               ? IndentForLevel[TheLine.Level]\n"
3617                "               : TheLine * 2,\n"
3618                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3619                getLLVMStyleWithColumns(60));
3620   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3621                "                  ? aaaaaaaaaaaaaaa\n"
3622                "                  : bbbbbbbbbbbbbbb //\n"
3623                "                        ? ccccccccccccccc\n"
3624                "                        : ddddddddddddddd;");
3625   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3626                "                  ? aaaaaaaaaaaaaaa\n"
3627                "                  : (bbbbbbbbbbbbbbb //\n"
3628                "                         ? ccccccccccccccc\n"
3629                "                         : ddddddddddddddd);");
3630   verifyFormat(
3631       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3632       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3633       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
3634       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
3635       "                                      : aaaaaaaaaa;");
3636   verifyFormat(
3637       "aaaaaa = aaaaaaaaaaaa ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3638       "                                   : aaaaaaaaaaaaaaaaaaaaaa\n"
3639       "                      : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3640 
3641   FormatStyle NoBinPacking = getLLVMStyle();
3642   NoBinPacking.BinPackArguments = false;
3643   verifyFormat(
3644       "void f() {\n"
3645       "  g(aaa,\n"
3646       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3647       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3648       "        ? aaaaaaaaaaaaaaa\n"
3649       "        : aaaaaaaaaaaaaaa);\n"
3650       "}",
3651       NoBinPacking);
3652   verifyFormat(
3653       "void f() {\n"
3654       "  g(aaa,\n"
3655       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3656       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3657       "        ?: aaaaaaaaaaaaaaa);\n"
3658       "}",
3659       NoBinPacking);
3660 
3661   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
3662                "             // comment.\n"
3663                "             ccccccccccccccccccccccccccccccccccccccc\n"
3664                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3665                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
3666 
3667   // Assignments in conditional expressions. Apparently not uncommon :-(.
3668   verifyFormat("return a != b\n"
3669                "           // comment\n"
3670                "           ? a = b\n"
3671                "           : a = b;");
3672   verifyFormat("return a != b\n"
3673                "           // comment\n"
3674                "           ? a = a != b\n"
3675                "                     // comment\n"
3676                "                     ? a = b\n"
3677                "                     : a\n"
3678                "           : a;\n");
3679   verifyFormat("return a != b\n"
3680                "           // comment\n"
3681                "           ? a\n"
3682                "           : a = a != b\n"
3683                "                     // comment\n"
3684                "                     ? a = b\n"
3685                "                     : a;");
3686 }
3687 
3688 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
3689   FormatStyle Style = getLLVMStyle();
3690   Style.BreakBeforeTernaryOperators = false;
3691   Style.ColumnLimit = 70;
3692   verifyFormat(
3693       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3694       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3695       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3696       Style);
3697   verifyFormat(
3698       "aaaa(aaaaaaaaaa, aaaaaaaa,\n"
3699       "     aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3700       "                                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3701       Style);
3702   verifyFormat(
3703       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3704       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3705       Style);
3706   verifyFormat(
3707       "aaaa(aaaaaaaa, aaaaaaaaaa,\n"
3708       "     aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3709       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3710       Style);
3711   verifyFormat(
3712       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
3713       "                                                      aaaaaaaaaaaaa);",
3714       Style);
3715   verifyFormat(
3716       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3717       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3718       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3719       "                   aaaaaaaaaaaaa);",
3720       Style);
3721   verifyFormat(
3722       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3723       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3724       "                   aaaaaaaaaaaaa);",
3725       Style);
3726   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3727                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3728                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3729                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3730                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3731                Style);
3732   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3733                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3734                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3735                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3736                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3737                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3738                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3739                Style);
3740   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3741                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
3742                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3743                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3744                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3745                Style);
3746   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3747                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3748                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3749                Style);
3750   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3751                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3752                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3753                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3754                Style);
3755   verifyFormat(
3756       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3757       "    aaaaaaaaaaaaaaa :\n"
3758       "    aaaaaaaaaaaaaaa;",
3759       Style);
3760   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3761                "          aaaaaaaaa ?\n"
3762                "      b :\n"
3763                "      c);",
3764                Style);
3765   verifyFormat("unsigned Indent =\n"
3766                "    format(TheLine.First,\n"
3767                "           IndentForLevel[TheLine.Level] >= 0 ?\n"
3768                "               IndentForLevel[TheLine.Level] :\n"
3769                "               TheLine * 2,\n"
3770                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3771                Style);
3772   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3773                "                  aaaaaaaaaaaaaaa :\n"
3774                "                  bbbbbbbbbbbbbbb ? //\n"
3775                "                      ccccccccccccccc :\n"
3776                "                      ddddddddddddddd;",
3777                Style);
3778   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3779                "                  aaaaaaaaaaaaaaa :\n"
3780                "                  (bbbbbbbbbbbbbbb ? //\n"
3781                "                       ccccccccccccccc :\n"
3782                "                       ddddddddddddddd);",
3783                Style);
3784   verifyFormat("int i = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3785                "            /*bbbbbbbbbbbbbbb=*/bbbbbbbbbbbbbbbbbbbbbbbbb :\n"
3786                "            ccccccccccccccccccccccccccc;",
3787                Style);
3788   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3789                "           aaaaa :\n"
3790                "           bbbbbbbbbbbbbbb + cccccccccccccccc;",
3791                Style);
3792 }
3793 
3794 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
3795   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
3796                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
3797   verifyFormat("bool a = true, b = false;");
3798 
3799   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3800                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
3801                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
3802                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
3803   verifyFormat(
3804       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3805       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
3806       "     d = e && f;");
3807   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
3808                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
3809   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3810                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
3811   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
3812                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
3813 
3814   FormatStyle Style = getGoogleStyle();
3815   Style.PointerAlignment = FormatStyle::PAS_Left;
3816   Style.DerivePointerAlignment = false;
3817   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3818                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
3819                "    *b = bbbbbbbbbbbbbbbbbbb;",
3820                Style);
3821   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3822                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
3823                Style);
3824   verifyFormat("vector<int*> a, b;", Style);
3825   verifyFormat("for (int *p, *q; p != q; p = p->next) {\n}", Style);
3826 }
3827 
3828 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
3829   verifyFormat("arr[foo ? bar : baz];");
3830   verifyFormat("f()[foo ? bar : baz];");
3831   verifyFormat("(a + b)[foo ? bar : baz];");
3832   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
3833 }
3834 
3835 TEST_F(FormatTest, AlignsStringLiterals) {
3836   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
3837                "                                      \"short literal\");");
3838   verifyFormat(
3839       "looooooooooooooooooooooooongFunction(\n"
3840       "    \"short literal\"\n"
3841       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
3842   verifyFormat("someFunction(\"Always break between multi-line\"\n"
3843                "             \" string literals\",\n"
3844                "             and, other, parameters);");
3845   EXPECT_EQ("fun + \"1243\" /* comment */\n"
3846             "      \"5678\";",
3847             format("fun + \"1243\" /* comment */\n"
3848                    "    \"5678\";",
3849                    getLLVMStyleWithColumns(28)));
3850   EXPECT_EQ(
3851       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
3852       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
3853       "         \"aaaaaaaaaaaaaaaa\";",
3854       format("aaaaaa ="
3855              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
3856              "aaaaaaaaaaaaaaaaaaaaa\" "
3857              "\"aaaaaaaaaaaaaaaa\";"));
3858   verifyFormat("a = a + \"a\"\n"
3859                "        \"a\"\n"
3860                "        \"a\";");
3861   verifyFormat("f(\"a\", \"b\"\n"
3862                "       \"c\");");
3863 
3864   verifyFormat(
3865       "#define LL_FORMAT \"ll\"\n"
3866       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
3867       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
3868 
3869   verifyFormat("#define A(X)          \\\n"
3870                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
3871                "  \"ccccc\"",
3872                getLLVMStyleWithColumns(23));
3873   verifyFormat("#define A \"def\"\n"
3874                "f(\"abc\" A \"ghi\"\n"
3875                "  \"jkl\");");
3876 
3877   verifyFormat("f(L\"a\"\n"
3878                "  L\"b\");");
3879   verifyFormat("#define A(X)            \\\n"
3880                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
3881                "  L\"ccccc\"",
3882                getLLVMStyleWithColumns(25));
3883 
3884   verifyFormat("f(@\"a\"\n"
3885                "  @\"b\");");
3886   verifyFormat("NSString s = @\"a\"\n"
3887                "             @\"b\"\n"
3888                "             @\"c\";");
3889   verifyFormat("NSString s = @\"a\"\n"
3890                "              \"b\"\n"
3891                "              \"c\";");
3892 }
3893 
3894 TEST_F(FormatTest, ReturnTypeBreakingStyle) {
3895   FormatStyle Style = getLLVMStyle();
3896   // No declarations or definitions should be moved to own line.
3897   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
3898   verifyFormat("class A {\n"
3899                "  int f() { return 1; }\n"
3900                "  int g();\n"
3901                "};\n"
3902                "int f() { return 1; }\n"
3903                "int g();\n",
3904                Style);
3905 
3906   // All declarations and definitions should have the return type moved to its
3907   // own
3908   // line.
3909   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
3910   verifyFormat("class E {\n"
3911                "  int\n"
3912                "  f() {\n"
3913                "    return 1;\n"
3914                "  }\n"
3915                "  int\n"
3916                "  g();\n"
3917                "};\n"
3918                "int\n"
3919                "f() {\n"
3920                "  return 1;\n"
3921                "}\n"
3922                "int\n"
3923                "g();\n",
3924                Style);
3925 
3926   // Top-level definitions, and no kinds of declarations should have the
3927   // return type moved to its own line.
3928   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
3929   verifyFormat("class B {\n"
3930                "  int f() { return 1; }\n"
3931                "  int g();\n"
3932                "};\n"
3933                "int\n"
3934                "f() {\n"
3935                "  return 1;\n"
3936                "}\n"
3937                "int g();\n",
3938                Style);
3939 
3940   // Top-level definitions and declarations should have the return type moved
3941   // to its own line.
3942   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
3943   verifyFormat("class C {\n"
3944                "  int f() { return 1; }\n"
3945                "  int g();\n"
3946                "};\n"
3947                "int\n"
3948                "f() {\n"
3949                "  return 1;\n"
3950                "}\n"
3951                "int\n"
3952                "g();\n",
3953                Style);
3954 
3955   // All definitions should have the return type moved to its own line, but no
3956   // kinds of declarations.
3957   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
3958   verifyFormat("class D {\n"
3959                "  int\n"
3960                "  f() {\n"
3961                "    return 1;\n"
3962                "  }\n"
3963                "  int g();\n"
3964                "};\n"
3965                "int\n"
3966                "f() {\n"
3967                "  return 1;\n"
3968                "}\n"
3969                "int g();\n",
3970                Style);
3971   verifyFormat("const char *\n"
3972                "f(void) {\n" // Break here.
3973                "  return \"\";\n"
3974                "}\n"
3975                "const char *bar(void);\n", // No break here.
3976                Style);
3977   verifyFormat("template <class T>\n"
3978                "T *\n"
3979                "f(T &c) {\n" // Break here.
3980                "  return NULL;\n"
3981                "}\n"
3982                "template <class T> T *f(T &c);\n", // No break here.
3983                Style);
3984   verifyFormat("class C {\n"
3985                "  int\n"
3986                "  operator+() {\n"
3987                "    return 1;\n"
3988                "  }\n"
3989                "  int\n"
3990                "  operator()() {\n"
3991                "    return 1;\n"
3992                "  }\n"
3993                "};\n",
3994                Style);
3995   verifyFormat("void\n"
3996                "A::operator()() {}\n"
3997                "void\n"
3998                "A::operator>>() {}\n"
3999                "void\n"
4000                "A::operator+() {}\n",
4001                Style);
4002   verifyFormat("void *operator new(std::size_t s);", // No break here.
4003                Style);
4004   verifyFormat("void *\n"
4005                "operator new(std::size_t s) {}",
4006                Style);
4007   verifyFormat("void *\n"
4008                "operator delete[](void *ptr) {}",
4009                Style);
4010   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4011   verifyFormat("const char *\n"
4012                "f(void)\n" // Break here.
4013                "{\n"
4014                "  return \"\";\n"
4015                "}\n"
4016                "const char *bar(void);\n", // No break here.
4017                Style);
4018   verifyFormat("template <class T>\n"
4019                "T *\n"     // Problem here: no line break
4020                "f(T &c)\n" // Break here.
4021                "{\n"
4022                "  return NULL;\n"
4023                "}\n"
4024                "template <class T> T *f(T &c);\n", // No break here.
4025                Style);
4026 }
4027 
4028 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4029   FormatStyle NoBreak = getLLVMStyle();
4030   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4031   FormatStyle Break = getLLVMStyle();
4032   Break.AlwaysBreakBeforeMultilineStrings = true;
4033   verifyFormat("aaaa = \"bbbb\"\n"
4034                "       \"cccc\";",
4035                NoBreak);
4036   verifyFormat("aaaa =\n"
4037                "    \"bbbb\"\n"
4038                "    \"cccc\";",
4039                Break);
4040   verifyFormat("aaaa(\"bbbb\"\n"
4041                "     \"cccc\");",
4042                NoBreak);
4043   verifyFormat("aaaa(\n"
4044                "    \"bbbb\"\n"
4045                "    \"cccc\");",
4046                Break);
4047   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4048                "          \"cccc\");",
4049                NoBreak);
4050   verifyFormat("aaaa(qqq,\n"
4051                "     \"bbbb\"\n"
4052                "     \"cccc\");",
4053                Break);
4054   verifyFormat("aaaa(qqq,\n"
4055                "     L\"bbbb\"\n"
4056                "     L\"cccc\");",
4057                Break);
4058   verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
4059                "                      \"bbbb\"));",
4060                Break);
4061   verifyFormat("string s = someFunction(\n"
4062                "    \"abc\"\n"
4063                "    \"abc\");",
4064                Break);
4065 
4066   // As we break before unary operators, breaking right after them is bad.
4067   verifyFormat("string foo = abc ? \"x\"\n"
4068                "                   \"blah blah blah blah blah blah\"\n"
4069                "                 : \"y\";",
4070                Break);
4071 
4072   // Don't break if there is no column gain.
4073   verifyFormat("f(\"aaaa\"\n"
4074                "  \"bbbb\");",
4075                Break);
4076 
4077   // Treat literals with escaped newlines like multi-line string literals.
4078   EXPECT_EQ("x = \"a\\\n"
4079             "b\\\n"
4080             "c\";",
4081             format("x = \"a\\\n"
4082                    "b\\\n"
4083                    "c\";",
4084                    NoBreak));
4085   EXPECT_EQ("xxxx =\n"
4086             "    \"a\\\n"
4087             "b\\\n"
4088             "c\";",
4089             format("xxxx = \"a\\\n"
4090                    "b\\\n"
4091                    "c\";",
4092                    Break));
4093 
4094   EXPECT_EQ("NSString *const kString =\n"
4095             "    @\"aaaa\"\n"
4096             "    @\"bbbb\";",
4097             format("NSString *const kString = @\"aaaa\"\n"
4098                    "@\"bbbb\";",
4099                    Break));
4100 
4101   Break.ColumnLimit = 0;
4102   verifyFormat("const char *hello = \"hello llvm\";", Break);
4103 }
4104 
4105 TEST_F(FormatTest, AlignsPipes) {
4106   verifyFormat(
4107       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4108       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4109       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4110   verifyFormat(
4111       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4112       "                     << aaaaaaaaaaaaaaaaaaaa;");
4113   verifyFormat(
4114       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4115       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4116   verifyFormat(
4117       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4118       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4119   verifyFormat(
4120       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4121       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4122       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4123   verifyFormat(
4124       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4125       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4126       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4127   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4128                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4129                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4130                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4131   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaaaaaa: \"\n"
4132                "             << aaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaa);");
4133   verifyFormat(
4134       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4135       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4136   verifyFormat(
4137       "auto Diag = diag() << aaaaaaaaaaaaaaaa(aaaaaaaaaaaa, aaaaaaaaaaaaa,\n"
4138       "                                       aaaaaaaaaaaaaaaaaaaaaaaaaa);");
4139 
4140   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4141                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4142   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4143                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4144                "                    aaaaaaaaaaaaaaaaaaaaa)\n"
4145                "             << aaaaaaaaaaaaaaaaaaaaaaaaaa;");
4146   verifyFormat("LOG_IF(aaa == //\n"
4147                "       bbb)\n"
4148                "    << a << b;");
4149 
4150   // But sometimes, breaking before the first "<<" is desirable.
4151   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4152                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4153   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4154                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4155                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4156   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4157                "    << BEF << IsTemplate << Description << E->getType();");
4158   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4159                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4160                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4161   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4162                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4163                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4164                "    << aaa;");
4165 
4166   verifyFormat(
4167       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4168       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4169 
4170   // Incomplete string literal.
4171   EXPECT_EQ("llvm::errs() << \"\n"
4172             "             << a;",
4173             format("llvm::errs() << \"\n<<a;"));
4174 
4175   verifyFormat("void f() {\n"
4176                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4177                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4178                "}");
4179 
4180   // Handle 'endl'.
4181   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4182                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4183   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4184 
4185   // Handle '\n'.
4186   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \"\\n\"\n"
4187                "             << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4188   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << \'\\n\'\n"
4189                "             << bbbbbbbbbbbbbbbbbbbbbb << \'\\n\';");
4190   verifyFormat("llvm::errs() << aaaa << \"aaaaaaaaaaaaaaaaaa\\n\"\n"
4191                "             << bbbb << \"bbbbbbbbbbbbbbbbbb\\n\";");
4192   verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
4193 }
4194 
4195 TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
4196   verifyFormat("return out << \"somepacket = {\\n\"\n"
4197                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4198                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4199                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4200                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4201                "           << \"}\";");
4202 
4203   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4204                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4205                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4206   verifyFormat(
4207       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4208       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4209       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4210       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4211       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4212   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4213                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4214   verifyFormat(
4215       "void f() {\n"
4216       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4217       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4218       "}");
4219 
4220   // Breaking before the first "<<" is generally not desirable.
4221   verifyFormat(
4222       "llvm::errs()\n"
4223       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4224       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4225       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4226       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4227       getLLVMStyleWithColumns(70));
4228   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4229                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4230                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4231                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4232                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4233                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4234                getLLVMStyleWithColumns(70));
4235 
4236   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4237                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
4238                "           \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
4239   verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4240                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
4241                "                  \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
4242   verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" +\n"
4243                "           (aaaa + aaaa);",
4244                getLLVMStyleWithColumns(40));
4245   verifyFormat("string v = StrCat(\"aaaaaaaaaaaa: \" +\n"
4246                "                  (aaaaaaa + aaaaa));",
4247                getLLVMStyleWithColumns(40));
4248   verifyFormat(
4249       "string v = StrCat(\"aaaaaaaaaaaaaaaaaaaaaaaaaaa: \",\n"
4250       "                  SomeFunction(aaaaaaaaaaaa, aaaaaaaa.aaaaaaa),\n"
4251       "                  bbbbbbbbbbbbbbbbbbbbbbb);");
4252 }
4253 
4254 TEST_F(FormatTest, UnderstandsEquals) {
4255   verifyFormat(
4256       "aaaaaaaaaaaaaaaaa =\n"
4257       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4258   verifyFormat(
4259       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4260       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4261   verifyFormat(
4262       "if (a) {\n"
4263       "  f();\n"
4264       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4265       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4266       "}");
4267 
4268   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4269                "        100000000 + 10000000) {\n}");
4270 }
4271 
4272 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4273   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4274                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4275 
4276   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4277                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4278 
4279   verifyFormat(
4280       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4281       "                                                          Parameter2);");
4282 
4283   verifyFormat(
4284       "ShortObject->shortFunction(\n"
4285       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4286       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4287 
4288   verifyFormat("loooooooooooooongFunction(\n"
4289                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4290 
4291   verifyFormat(
4292       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4293       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4294 
4295   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4296                "    .WillRepeatedly(Return(SomeValue));");
4297   verifyFormat("void f() {\n"
4298                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4299                "      .Times(2)\n"
4300                "      .WillRepeatedly(Return(SomeValue));\n"
4301                "}");
4302   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4303                "    ccccccccccccccccccccccc);");
4304   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4305                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4306                "          .aaaaa(aaaaa),\n"
4307                "      aaaaaaaaaaaaaaaaaaaaa);");
4308   verifyFormat("void f() {\n"
4309                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4310                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4311                "}");
4312   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4313                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4314                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4315                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4316                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4317   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4318                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4319                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4320                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4321                "}");
4322 
4323   // Here, it is not necessary to wrap at "." or "->".
4324   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4325                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4326   verifyFormat(
4327       "aaaaaaaaaaa->aaaaaaaaa(\n"
4328       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4329       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4330 
4331   verifyFormat(
4332       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4333       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4334   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4335                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4336   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4337                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4338 
4339   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4340                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4341                "    .a();");
4342 
4343   FormatStyle NoBinPacking = getLLVMStyle();
4344   NoBinPacking.BinPackParameters = false;
4345   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4346                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4347                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4348                "                         aaaaaaaaaaaaaaaaaaa,\n"
4349                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4350                NoBinPacking);
4351 
4352   // If there is a subsequent call, change to hanging indentation.
4353   verifyFormat(
4354       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4355       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4356       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4357   verifyFormat(
4358       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4359       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4360   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4361                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4362                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4363   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4364                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4365                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4366 }
4367 
4368 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4369   verifyFormat("template <typename T>\n"
4370                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4371   verifyFormat("template <typename T>\n"
4372                "// T should be one of {A, B}.\n"
4373                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4374   verifyFormat(
4375       "template <typename T>\n"
4376       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4377   verifyFormat("template <typename T>\n"
4378                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4379                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4380   verifyFormat(
4381       "template <typename T>\n"
4382       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4383       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4384   verifyFormat(
4385       "template <typename T>\n"
4386       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4387       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4388       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4389   verifyFormat("template <typename T>\n"
4390                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4391                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4392   verifyFormat(
4393       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4394       "          typename T4 = char>\n"
4395       "void f();");
4396   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4397                "          template <typename> class cccccccccccccccccccccc,\n"
4398                "          typename ddddddddddddd>\n"
4399                "class C {};");
4400   verifyFormat(
4401       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4402       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4403 
4404   verifyFormat("void f() {\n"
4405                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4406                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4407                "}");
4408 
4409   verifyFormat("template <typename T> class C {};");
4410   verifyFormat("template <typename T> void f();");
4411   verifyFormat("template <typename T> void f() {}");
4412   verifyFormat(
4413       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4414       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4415       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4416       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4417       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4418       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4419       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4420       getLLVMStyleWithColumns(72));
4421   EXPECT_EQ("static_cast<A< //\n"
4422             "    B> *>(\n"
4423             "\n"
4424             "    );",
4425             format("static_cast<A<//\n"
4426                    "    B>*>(\n"
4427                    "\n"
4428                    "    );"));
4429   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4430                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4431 
4432   FormatStyle AlwaysBreak = getLLVMStyle();
4433   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4434   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4435   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4436   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4437   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4438                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4439                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4440   verifyFormat("template <template <typename> class Fooooooo,\n"
4441                "          template <typename> class Baaaaaaar>\n"
4442                "struct C {};",
4443                AlwaysBreak);
4444   verifyFormat("template <typename T> // T can be A, B or C.\n"
4445                "struct C {};",
4446                AlwaysBreak);
4447   verifyFormat("template <enum E> class A {\n"
4448                "public:\n"
4449                "  E *f();\n"
4450                "};");
4451 }
4452 
4453 TEST_F(FormatTest, WrapsTemplateParameters) {
4454   FormatStyle Style = getLLVMStyle();
4455   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4456   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4457   verifyFormat(
4458       "template <typename... a> struct q {};\n"
4459       "extern q<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4460       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4461       "    y;",
4462       Style);
4463   Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign;
4464   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4465   verifyFormat(
4466       "template <typename... a> struct r {};\n"
4467       "extern r<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa,\n"
4468       "    aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>\n"
4469       "    y;",
4470       Style);
4471   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4472   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
4473   verifyFormat(
4474       "template <typename... a> struct s {};\n"
4475       "extern s<\n"
4476       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4477       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4478       "    y;",
4479       Style);
4480   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
4481   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
4482   verifyFormat(
4483       "template <typename... a> struct t {};\n"
4484       "extern t<\n"
4485       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
4486       "    aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa>\n"
4487       "    y;",
4488       Style);
4489 }
4490 
4491 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4492   verifyFormat(
4493       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4494       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4495   verifyFormat(
4496       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4497       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4498       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4499 
4500   // FIXME: Should we have the extra indent after the second break?
4501   verifyFormat(
4502       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4503       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4504       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4505 
4506   verifyFormat(
4507       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4508       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4509 
4510   // Breaking at nested name specifiers is generally not desirable.
4511   verifyFormat(
4512       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4513       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4514 
4515   verifyFormat(
4516       "aaaaaaaaaaaaaaaaaa(aaaaaaaa,\n"
4517       "                   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4518       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4519       "                   aaaaaaaaaaaaaaaaaaaaa);",
4520       getLLVMStyleWithColumns(74));
4521 
4522   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4523                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4524                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4525 }
4526 
4527 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4528   verifyFormat("A<int> a;");
4529   verifyFormat("A<A<A<int>>> a;");
4530   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4531   verifyFormat("bool x = a < 1 || 2 > a;");
4532   verifyFormat("bool x = 5 < f<int>();");
4533   verifyFormat("bool x = f<int>() > 5;");
4534   verifyFormat("bool x = 5 < a<int>::x;");
4535   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4536   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4537 
4538   verifyGoogleFormat("A<A<int>> a;");
4539   verifyGoogleFormat("A<A<A<int>>> a;");
4540   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4541   verifyGoogleFormat("A<A<int> > a;");
4542   verifyGoogleFormat("A<A<A<int> > > a;");
4543   verifyGoogleFormat("A<A<A<A<int> > > > a;");
4544   verifyGoogleFormat("A<::A<int>> a;");
4545   verifyGoogleFormat("A<::A> a;");
4546   verifyGoogleFormat("A< ::A> a;");
4547   verifyGoogleFormat("A< ::A<int> > a;");
4548   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4549   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4550   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4551   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4552   EXPECT_EQ("auto x = [] { A<A<A<A>>> a; };",
4553             format("auto x=[]{A<A<A<A> >> a;};", getGoogleStyle()));
4554 
4555   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
4556 
4557   verifyFormat("test >> a >> b;");
4558   verifyFormat("test << a >> b;");
4559 
4560   verifyFormat("f<int>();");
4561   verifyFormat("template <typename T> void f() {}");
4562   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
4563   verifyFormat("struct A<std::enable_if<sizeof(T2) ? sizeof(int32) : "
4564                "sizeof(char)>::type>;");
4565   verifyFormat("template <class T> struct S<std::is_arithmetic<T>{}> {};");
4566   verifyFormat("f(a.operator()<A>());");
4567   verifyFormat("f(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4568                "      .template operator()<A>());",
4569                getLLVMStyleWithColumns(35));
4570 
4571   // Not template parameters.
4572   verifyFormat("return a < b && c > d;");
4573   verifyFormat("void f() {\n"
4574                "  while (a < b && c > d) {\n"
4575                "  }\n"
4576                "}");
4577   verifyFormat("template <typename... Types>\n"
4578                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
4579 
4580   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4581                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
4582                getLLVMStyleWithColumns(60));
4583   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
4584   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
4585   verifyFormat("< < < < < < < < < < < < < < < < < < < < < < < < < < < < < <");
4586 }
4587 
4588 TEST_F(FormatTest, BitshiftOperatorWidth) {
4589   EXPECT_EQ("int a = 1 << 2; /* foo\n"
4590             "                   bar */",
4591             format("int    a=1<<2;  /* foo\n"
4592                    "                   bar */"));
4593 
4594   EXPECT_EQ("int b = 256 >> 1; /* foo\n"
4595             "                     bar */",
4596             format("int  b  =256>>1 ;  /* foo\n"
4597                    "                      bar */"));
4598 }
4599 
4600 TEST_F(FormatTest, UnderstandsBinaryOperators) {
4601   verifyFormat("COMPARE(a, ==, b);");
4602   verifyFormat("auto s = sizeof...(Ts) - 1;");
4603 }
4604 
4605 TEST_F(FormatTest, UnderstandsPointersToMembers) {
4606   verifyFormat("int A::*x;");
4607   verifyFormat("int (S::*func)(void *);");
4608   verifyFormat("void f() { int (S::*func)(void *); }");
4609   verifyFormat("typedef bool *(Class::*Member)() const;");
4610   verifyFormat("void f() {\n"
4611                "  (a->*f)();\n"
4612                "  a->*x;\n"
4613                "  (a.*f)();\n"
4614                "  ((*a).*f)();\n"
4615                "  a.*x;\n"
4616                "}");
4617   verifyFormat("void f() {\n"
4618                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
4619                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
4620                "}");
4621   verifyFormat(
4622       "(aaaaaaaaaa->*bbbbbbb)(\n"
4623       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4624   FormatStyle Style = getLLVMStyle();
4625   Style.PointerAlignment = FormatStyle::PAS_Left;
4626   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
4627 }
4628 
4629 TEST_F(FormatTest, UnderstandsUnaryOperators) {
4630   verifyFormat("int a = -2;");
4631   verifyFormat("f(-1, -2, -3);");
4632   verifyFormat("a[-1] = 5;");
4633   verifyFormat("int a = 5 + -2;");
4634   verifyFormat("if (i == -1) {\n}");
4635   verifyFormat("if (i != -1) {\n}");
4636   verifyFormat("if (i > -1) {\n}");
4637   verifyFormat("if (i < -1) {\n}");
4638   verifyFormat("++(a->f());");
4639   verifyFormat("--(a->f());");
4640   verifyFormat("(a->f())++;");
4641   verifyFormat("a[42]++;");
4642   verifyFormat("if (!(a->f())) {\n}");
4643 
4644   verifyFormat("a-- > b;");
4645   verifyFormat("b ? -a : c;");
4646   verifyFormat("n * sizeof char16;");
4647   verifyFormat("n * alignof char16;", getGoogleStyle());
4648   verifyFormat("sizeof(char);");
4649   verifyFormat("alignof(char);", getGoogleStyle());
4650 
4651   verifyFormat("return -1;");
4652   verifyFormat("switch (a) {\n"
4653                "case -1:\n"
4654                "  break;\n"
4655                "}");
4656   verifyFormat("#define X -1");
4657   verifyFormat("#define X -kConstant");
4658 
4659   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
4660   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
4661 
4662   verifyFormat("int a = /* confusing comment */ -1;");
4663   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
4664   verifyFormat("int a = i /* confusing comment */++;");
4665 }
4666 
4667 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
4668   verifyFormat("if (!aaaaaaaaaa( // break\n"
4669                "        aaaaa)) {\n"
4670                "}");
4671   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
4672                "    aaaaa));");
4673   verifyFormat("*aaa = aaaaaaa( // break\n"
4674                "    bbbbbb);");
4675 }
4676 
4677 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
4678   verifyFormat("bool operator<();");
4679   verifyFormat("bool operator>();");
4680   verifyFormat("bool operator=();");
4681   verifyFormat("bool operator==();");
4682   verifyFormat("bool operator!=();");
4683   verifyFormat("int operator+();");
4684   verifyFormat("int operator++();");
4685   verifyFormat("bool operator,();");
4686   verifyFormat("bool operator();");
4687   verifyFormat("bool operator()();");
4688   verifyFormat("bool operator[]();");
4689   verifyFormat("operator bool();");
4690   verifyFormat("operator int();");
4691   verifyFormat("operator void *();");
4692   verifyFormat("operator SomeType<int>();");
4693   verifyFormat("operator SomeType<int, int>();");
4694   verifyFormat("operator SomeType<SomeType<int>>();");
4695   verifyFormat("void *operator new(std::size_t size);");
4696   verifyFormat("void *operator new[](std::size_t size);");
4697   verifyFormat("void operator delete(void *ptr);");
4698   verifyFormat("void operator delete[](void *ptr);");
4699   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
4700                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
4701   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa operator,(\n"
4702                "    aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaaaaaaaaaaaaaaaaaaa) const;");
4703 
4704   verifyFormat(
4705       "ostream &operator<<(ostream &OutputStream,\n"
4706       "                    SomeReallyLongType WithSomeReallyLongValue);");
4707   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
4708                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
4709                "  return left.group < right.group;\n"
4710                "}");
4711   verifyFormat("SomeType &operator=(const SomeType &S);");
4712   verifyFormat("f.template operator()<int>();");
4713 
4714   verifyGoogleFormat("operator void*();");
4715   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
4716   verifyGoogleFormat("operator ::A();");
4717 
4718   verifyFormat("using A::operator+;");
4719   verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
4720                "int i;");
4721 }
4722 
4723 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
4724   verifyFormat("Deleted &operator=(const Deleted &) & = default;");
4725   verifyFormat("Deleted &operator=(const Deleted &) && = delete;");
4726   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;");
4727   verifyFormat("SomeType MemberFunction(const Deleted &) && = delete;");
4728   verifyFormat("Deleted &operator=(const Deleted &) &;");
4729   verifyFormat("Deleted &operator=(const Deleted &) &&;");
4730   verifyFormat("SomeType MemberFunction(const Deleted &) &;");
4731   verifyFormat("SomeType MemberFunction(const Deleted &) &&;");
4732   verifyFormat("SomeType MemberFunction(const Deleted &) && {}");
4733   verifyFormat("SomeType MemberFunction(const Deleted &) && final {}");
4734   verifyFormat("SomeType MemberFunction(const Deleted &) && override {}");
4735   verifyFormat("SomeType MemberFunction(const Deleted &) const &;");
4736   verifyFormat("template <typename T>\n"
4737                "void F(T) && = delete;",
4738                getGoogleStyle());
4739 
4740   FormatStyle AlignLeft = getLLVMStyle();
4741   AlignLeft.PointerAlignment = FormatStyle::PAS_Left;
4742   verifyFormat("void A::b() && {}", AlignLeft);
4743   verifyFormat("Deleted& operator=(const Deleted&) & = default;", AlignLeft);
4744   verifyFormat("SomeType MemberFunction(const Deleted&) & = delete;",
4745                AlignLeft);
4746   verifyFormat("Deleted& operator=(const Deleted&) &;", AlignLeft);
4747   verifyFormat("SomeType MemberFunction(const Deleted&) &;", AlignLeft);
4748   verifyFormat("auto Function(T t) & -> void {}", AlignLeft);
4749   verifyFormat("auto Function(T... t) & -> void {}", AlignLeft);
4750   verifyFormat("auto Function(T) & -> void {}", AlignLeft);
4751   verifyFormat("auto Function(T) & -> void;", AlignLeft);
4752   verifyFormat("SomeType MemberFunction(const Deleted&) const &;", AlignLeft);
4753 
4754   FormatStyle Spaces = getLLVMStyle();
4755   Spaces.SpacesInCStyleCastParentheses = true;
4756   verifyFormat("Deleted &operator=(const Deleted &) & = default;", Spaces);
4757   verifyFormat("SomeType MemberFunction(const Deleted &) & = delete;", Spaces);
4758   verifyFormat("Deleted &operator=(const Deleted &) &;", Spaces);
4759   verifyFormat("SomeType MemberFunction(const Deleted &) &;", Spaces);
4760 
4761   Spaces.SpacesInCStyleCastParentheses = false;
4762   Spaces.SpacesInParentheses = true;
4763   verifyFormat("Deleted &operator=( const Deleted & ) & = default;", Spaces);
4764   verifyFormat("SomeType MemberFunction( const Deleted & ) & = delete;", Spaces);
4765   verifyFormat("Deleted &operator=( const Deleted & ) &;", Spaces);
4766   verifyFormat("SomeType MemberFunction( const Deleted & ) &;", Spaces);
4767 }
4768 
4769 TEST_F(FormatTest, UnderstandsNewAndDelete) {
4770   verifyFormat("void f() {\n"
4771                "  A *a = new A;\n"
4772                "  A *a = new (placement) A;\n"
4773                "  delete a;\n"
4774                "  delete (A *)a;\n"
4775                "}");
4776   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
4777                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
4778   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4779                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
4780                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
4781   verifyFormat("delete[] h->p;");
4782 }
4783 
4784 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
4785   verifyFormat("int *f(int *a) {}");
4786   verifyFormat("int main(int argc, char **argv) {}");
4787   verifyFormat("Test::Test(int b) : a(b * b) {}");
4788   verifyIndependentOfContext("f(a, *a);");
4789   verifyFormat("void g() { f(*a); }");
4790   verifyIndependentOfContext("int a = b * 10;");
4791   verifyIndependentOfContext("int a = 10 * b;");
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 * c;");
4797   verifyIndependentOfContext("int a = *b;");
4798   verifyIndependentOfContext("int a = *b * c;");
4799   verifyIndependentOfContext("int a = b * *c;");
4800   verifyIndependentOfContext("int a = b * (10);");
4801   verifyIndependentOfContext("S << b * (10);");
4802   verifyIndependentOfContext("return 10 * b;");
4803   verifyIndependentOfContext("return *b * *c;");
4804   verifyIndependentOfContext("return a & ~b;");
4805   verifyIndependentOfContext("f(b ? *c : *d);");
4806   verifyIndependentOfContext("int a = b ? *c : *d;");
4807   verifyIndependentOfContext("*b = a;");
4808   verifyIndependentOfContext("a * ~b;");
4809   verifyIndependentOfContext("a * !b;");
4810   verifyIndependentOfContext("a * +b;");
4811   verifyIndependentOfContext("a * -b;");
4812   verifyIndependentOfContext("a * ++b;");
4813   verifyIndependentOfContext("a * --b;");
4814   verifyIndependentOfContext("a[4] * b;");
4815   verifyIndependentOfContext("a[a * a] = 1;");
4816   verifyIndependentOfContext("f() * b;");
4817   verifyIndependentOfContext("a * [self dostuff];");
4818   verifyIndependentOfContext("int x = a * (a + b);");
4819   verifyIndependentOfContext("(a *)(a + b);");
4820   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
4821   verifyIndependentOfContext("int *pa = (int *)&a;");
4822   verifyIndependentOfContext("return sizeof(int **);");
4823   verifyIndependentOfContext("return sizeof(int ******);");
4824   verifyIndependentOfContext("return (int **&)a;");
4825   verifyIndependentOfContext("f((*PointerToArray)[10]);");
4826   verifyFormat("void f(Type (*parameter)[10]) {}");
4827   verifyFormat("void f(Type (&parameter)[10]) {}");
4828   verifyGoogleFormat("return sizeof(int**);");
4829   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
4830   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
4831   verifyFormat("auto a = [](int **&, int ***) {};");
4832   verifyFormat("auto PointerBinding = [](const char *S) {};");
4833   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
4834   verifyFormat("[](const decltype(*a) &value) {}");
4835   verifyFormat("decltype(a * b) F();");
4836   verifyFormat("#define MACRO() [](A *a) { return 1; }");
4837   verifyFormat("Constructor() : member([](A *a, B *b) {}) {}");
4838   verifyIndependentOfContext("typedef void (*f)(int *a);");
4839   verifyIndependentOfContext("int i{a * b};");
4840   verifyIndependentOfContext("aaa && aaa->f();");
4841   verifyIndependentOfContext("int x = ~*p;");
4842   verifyFormat("Constructor() : a(a), area(width * height) {}");
4843   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
4844   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
4845   verifyFormat("void f() { f(a, c * d); }");
4846   verifyFormat("void f() { f(new a(), c * d); }");
4847   verifyFormat("void f(const MyOverride &override);");
4848   verifyFormat("void f(const MyFinal &final);");
4849   verifyIndependentOfContext("bool a = f() && override.f();");
4850   verifyIndependentOfContext("bool a = f() && final.f();");
4851 
4852   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
4853 
4854   verifyIndependentOfContext("A<int *> a;");
4855   verifyIndependentOfContext("A<int **> a;");
4856   verifyIndependentOfContext("A<int *, int *> a;");
4857   verifyIndependentOfContext("A<int *[]> a;");
4858   verifyIndependentOfContext(
4859       "const char *const p = reinterpret_cast<const char *const>(q);");
4860   verifyIndependentOfContext("A<int **, int **> a;");
4861   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
4862   verifyFormat("for (char **a = b; *a; ++a) {\n}");
4863   verifyFormat("for (; a && b;) {\n}");
4864   verifyFormat("bool foo = true && [] { return false; }();");
4865 
4866   verifyFormat(
4867       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4868       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4869 
4870   verifyGoogleFormat("int const* a = &b;");
4871   verifyGoogleFormat("**outparam = 1;");
4872   verifyGoogleFormat("*outparam = a * b;");
4873   verifyGoogleFormat("int main(int argc, char** argv) {}");
4874   verifyGoogleFormat("A<int*> a;");
4875   verifyGoogleFormat("A<int**> a;");
4876   verifyGoogleFormat("A<int*, int*> a;");
4877   verifyGoogleFormat("A<int**, int**> a;");
4878   verifyGoogleFormat("f(b ? *c : *d);");
4879   verifyGoogleFormat("int a = b ? *c : *d;");
4880   verifyGoogleFormat("Type* t = **x;");
4881   verifyGoogleFormat("Type* t = *++*x;");
4882   verifyGoogleFormat("*++*x;");
4883   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
4884   verifyGoogleFormat("Type* t = x++ * y;");
4885   verifyGoogleFormat(
4886       "const char* const p = reinterpret_cast<const char* const>(q);");
4887   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
4888   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
4889   verifyGoogleFormat("template <typename T>\n"
4890                      "void f(int i = 0, SomeType** temps = NULL);");
4891 
4892   FormatStyle Left = getLLVMStyle();
4893   Left.PointerAlignment = FormatStyle::PAS_Left;
4894   verifyFormat("x = *a(x) = *a(y);", Left);
4895   verifyFormat("for (;; *a = b) {\n}", Left);
4896   verifyFormat("return *this += 1;", Left);
4897 
4898   verifyIndependentOfContext("a = *(x + y);");
4899   verifyIndependentOfContext("a = &(x + y);");
4900   verifyIndependentOfContext("*(x + y).call();");
4901   verifyIndependentOfContext("&(x + y)->call();");
4902   verifyFormat("void f() { &(*I).first; }");
4903 
4904   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
4905   verifyFormat(
4906       "int *MyValues = {\n"
4907       "    *A, // Operator detection might be confused by the '{'\n"
4908       "    *BB // Operator detection might be confused by previous comment\n"
4909       "};");
4910 
4911   verifyIndependentOfContext("if (int *a = &b)");
4912   verifyIndependentOfContext("if (int &a = *b)");
4913   verifyIndependentOfContext("if (a & b[i])");
4914   verifyIndependentOfContext("if (a::b::c::d & b[i])");
4915   verifyIndependentOfContext("if (*b[i])");
4916   verifyIndependentOfContext("if (int *a = (&b))");
4917   verifyIndependentOfContext("while (int *a = &b)");
4918   verifyIndependentOfContext("size = sizeof *a;");
4919   verifyIndependentOfContext("if (a && (b = c))");
4920   verifyFormat("void f() {\n"
4921                "  for (const int &v : Values) {\n"
4922                "  }\n"
4923                "}");
4924   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
4925   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
4926   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
4927 
4928   verifyFormat("#define A (!a * b)");
4929   verifyFormat("#define MACRO     \\\n"
4930                "  int *i = a * b; \\\n"
4931                "  void f(a *b);",
4932                getLLVMStyleWithColumns(19));
4933 
4934   verifyIndependentOfContext("A = new SomeType *[Length];");
4935   verifyIndependentOfContext("A = new SomeType *[Length]();");
4936   verifyIndependentOfContext("T **t = new T *;");
4937   verifyIndependentOfContext("T **t = new T *();");
4938   verifyGoogleFormat("A = new SomeType*[Length]();");
4939   verifyGoogleFormat("A = new SomeType*[Length];");
4940   verifyGoogleFormat("T** t = new T*;");
4941   verifyGoogleFormat("T** t = new T*();");
4942 
4943   FormatStyle PointerLeft = getLLVMStyle();
4944   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
4945   verifyFormat("delete *x;", PointerLeft);
4946   verifyFormat("STATIC_ASSERT((a & b) == 0);");
4947   verifyFormat("STATIC_ASSERT(0 == (a & b));");
4948   verifyFormat("template <bool a, bool b> "
4949                "typename t::if<x && y>::type f() {}");
4950   verifyFormat("template <int *y> f() {}");
4951   verifyFormat("vector<int *> v;");
4952   verifyFormat("vector<int *const> v;");
4953   verifyFormat("vector<int *const **const *> v;");
4954   verifyFormat("vector<int *volatile> v;");
4955   verifyFormat("vector<a * b> v;");
4956   verifyFormat("foo<b && false>();");
4957   verifyFormat("foo<b & 1>();");
4958   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
4959   verifyFormat(
4960       "template <class T, class = typename std::enable_if<\n"
4961       "                       std::is_integral<T>::value &&\n"
4962       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
4963       "void F();",
4964       getLLVMStyleWithColumns(70));
4965   verifyFormat(
4966       "template <class T,\n"
4967       "          class = typename std::enable_if<\n"
4968       "              std::is_integral<T>::value &&\n"
4969       "              (sizeof(T) > 1 || sizeof(T) < 8)>::type,\n"
4970       "          class U>\n"
4971       "void F();",
4972       getLLVMStyleWithColumns(70));
4973   verifyFormat(
4974       "template <class T,\n"
4975       "          class = typename ::std::enable_if<\n"
4976       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
4977       "void F();",
4978       getGoogleStyleWithColumns(68));
4979 
4980   verifyIndependentOfContext("MACRO(int *i);");
4981   verifyIndependentOfContext("MACRO(auto *a);");
4982   verifyIndependentOfContext("MACRO(const A *a);");
4983   verifyIndependentOfContext("MACRO(A *const a);");
4984   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
4985   verifyFormat("void f() { f(float{1}, a * a); }");
4986   // FIXME: Is there a way to make this work?
4987   // verifyIndependentOfContext("MACRO(A *a);");
4988 
4989   verifyFormat("DatumHandle const *operator->() const { return input_; }");
4990   verifyFormat("return options != nullptr && operator==(*options);");
4991 
4992   EXPECT_EQ("#define OP(x)                                    \\\n"
4993             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
4994             "    return s << a.DebugString();                 \\\n"
4995             "  }",
4996             format("#define OP(x) \\\n"
4997                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
4998                    "    return s << a.DebugString(); \\\n"
4999                    "  }",
5000                    getLLVMStyleWithColumns(50)));
5001 
5002   // FIXME: We cannot handle this case yet; we might be able to figure out that
5003   // foo<x> d > v; doesn't make sense.
5004   verifyFormat("foo<a<b && c> d> v;");
5005 
5006   FormatStyle PointerMiddle = getLLVMStyle();
5007   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5008   verifyFormat("delete *x;", PointerMiddle);
5009   verifyFormat("int * x;", PointerMiddle);
5010   verifyFormat("template <int * y> f() {}", PointerMiddle);
5011   verifyFormat("int * f(int * a) {}", PointerMiddle);
5012   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5013   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5014   verifyFormat("A<int *> a;", PointerMiddle);
5015   verifyFormat("A<int **> a;", PointerMiddle);
5016   verifyFormat("A<int *, int *> a;", PointerMiddle);
5017   verifyFormat("A<int * []> a;", PointerMiddle);
5018   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5019   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5020   verifyFormat("T ** t = new T *;", PointerMiddle);
5021 
5022   // Member function reference qualifiers aren't binary operators.
5023   verifyFormat("string // break\n"
5024                "operator()() & {}");
5025   verifyFormat("string // break\n"
5026                "operator()() && {}");
5027   verifyGoogleFormat("template <typename T>\n"
5028                      "auto x() & -> int {}");
5029 }
5030 
5031 TEST_F(FormatTest, UnderstandsAttributes) {
5032   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5033   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5034                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5035   FormatStyle AfterType = getLLVMStyle();
5036   AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
5037   verifyFormat("__attribute__((nodebug)) void\n"
5038                "foo() {}\n",
5039                AfterType);
5040 }
5041 
5042 TEST_F(FormatTest, UnderstandsEllipsis) {
5043   verifyFormat("int printf(const char *fmt, ...);");
5044   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5045   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5046 
5047   FormatStyle PointersLeft = getLLVMStyle();
5048   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5049   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5050 }
5051 
5052 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5053   EXPECT_EQ("int *a;\n"
5054             "int *a;\n"
5055             "int *a;",
5056             format("int *a;\n"
5057                    "int* a;\n"
5058                    "int *a;",
5059                    getGoogleStyle()));
5060   EXPECT_EQ("int* a;\n"
5061             "int* a;\n"
5062             "int* a;",
5063             format("int* a;\n"
5064                    "int* a;\n"
5065                    "int *a;",
5066                    getGoogleStyle()));
5067   EXPECT_EQ("int *a;\n"
5068             "int *a;\n"
5069             "int *a;",
5070             format("int *a;\n"
5071                    "int * a;\n"
5072                    "int *  a;",
5073                    getGoogleStyle()));
5074   EXPECT_EQ("auto x = [] {\n"
5075             "  int *a;\n"
5076             "  int *a;\n"
5077             "  int *a;\n"
5078             "};",
5079             format("auto x=[]{int *a;\n"
5080                    "int * a;\n"
5081                    "int *  a;};",
5082                    getGoogleStyle()));
5083 }
5084 
5085 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5086   verifyFormat("int f(int &&a) {}");
5087   verifyFormat("int f(int a, char &&b) {}");
5088   verifyFormat("void f() { int &&a = b; }");
5089   verifyGoogleFormat("int f(int a, char&& b) {}");
5090   verifyGoogleFormat("void f() { int&& a = b; }");
5091 
5092   verifyIndependentOfContext("A<int &&> a;");
5093   verifyIndependentOfContext("A<int &&, int &&> a;");
5094   verifyGoogleFormat("A<int&&> a;");
5095   verifyGoogleFormat("A<int&&, int&&> a;");
5096 
5097   // Not rvalue references:
5098   verifyFormat("template <bool B, bool C> class A {\n"
5099                "  static_assert(B && C, \"Something is wrong\");\n"
5100                "};");
5101   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5102   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5103   verifyFormat("#define A(a, b) (a && b)");
5104 }
5105 
5106 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5107   verifyFormat("void f() {\n"
5108                "  x[aaaaaaaaa -\n"
5109                "    b] = 23;\n"
5110                "}",
5111                getLLVMStyleWithColumns(15));
5112 }
5113 
5114 TEST_F(FormatTest, FormatsCasts) {
5115   verifyFormat("Type *A = static_cast<Type *>(P);");
5116   verifyFormat("Type *A = (Type *)P;");
5117   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5118   verifyFormat("int a = (int)(2.0f);");
5119   verifyFormat("int a = (int)2.0f;");
5120   verifyFormat("x[(int32)y];");
5121   verifyFormat("x = (int32)y;");
5122   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5123   verifyFormat("int a = (int)*b;");
5124   verifyFormat("int a = (int)2.0f;");
5125   verifyFormat("int a = (int)~0;");
5126   verifyFormat("int a = (int)++a;");
5127   verifyFormat("int a = (int)sizeof(int);");
5128   verifyFormat("int a = (int)+2;");
5129   verifyFormat("my_int a = (my_int)2.0f;");
5130   verifyFormat("my_int a = (my_int)sizeof(int);");
5131   verifyFormat("return (my_int)aaa;");
5132   verifyFormat("#define x ((int)-1)");
5133   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5134   verifyFormat("#define p(q) ((int *)&q)");
5135   verifyFormat("fn(a)(b) + 1;");
5136 
5137   verifyFormat("void f() { my_int a = (my_int)*b; }");
5138   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5139   verifyFormat("my_int a = (my_int)~0;");
5140   verifyFormat("my_int a = (my_int)++a;");
5141   verifyFormat("my_int a = (my_int)-2;");
5142   verifyFormat("my_int a = (my_int)1;");
5143   verifyFormat("my_int a = (my_int *)1;");
5144   verifyFormat("my_int a = (const my_int)-1;");
5145   verifyFormat("my_int a = (const my_int *)-1;");
5146   verifyFormat("my_int a = (my_int)(my_int)-1;");
5147   verifyFormat("my_int a = (ns::my_int)-2;");
5148   verifyFormat("case (my_int)ONE:");
5149   verifyFormat("auto x = (X)this;");
5150 
5151   // FIXME: single value wrapped with paren will be treated as cast.
5152   verifyFormat("void f(int i = (kValue)*kMask) {}");
5153 
5154   verifyFormat("{ (void)F; }");
5155 
5156   // Don't break after a cast's
5157   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5158                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5159                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5160 
5161   // These are not casts.
5162   verifyFormat("void f(int *) {}");
5163   verifyFormat("f(foo)->b;");
5164   verifyFormat("f(foo).b;");
5165   verifyFormat("f(foo)(b);");
5166   verifyFormat("f(foo)[b];");
5167   verifyFormat("[](foo) { return 4; }(bar);");
5168   verifyFormat("(*funptr)(foo)[4];");
5169   verifyFormat("funptrs[4](foo)[4];");
5170   verifyFormat("void f(int *);");
5171   verifyFormat("void f(int *) = 0;");
5172   verifyFormat("void f(SmallVector<int>) {}");
5173   verifyFormat("void f(SmallVector<int>);");
5174   verifyFormat("void f(SmallVector<int>) = 0;");
5175   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5176   verifyFormat("int a = sizeof(int) * b;");
5177   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5178   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5179   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5180   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5181 
5182   // These are not casts, but at some point were confused with casts.
5183   verifyFormat("virtual void foo(int *) override;");
5184   verifyFormat("virtual void foo(char &) const;");
5185   verifyFormat("virtual void foo(int *a, char *) const;");
5186   verifyFormat("int a = sizeof(int *) + b;");
5187   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5188   verifyFormat("bool b = f(g<int>) && c;");
5189   verifyFormat("typedef void (*f)(int i) func;");
5190 
5191   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5192                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5193   // FIXME: The indentation here is not ideal.
5194   verifyFormat(
5195       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5196       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5197       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5198 }
5199 
5200 TEST_F(FormatTest, FormatsFunctionTypes) {
5201   verifyFormat("A<bool()> a;");
5202   verifyFormat("A<SomeType()> a;");
5203   verifyFormat("A<void (*)(int, std::string)> a;");
5204   verifyFormat("A<void *(int)>;");
5205   verifyFormat("void *(*a)(int *, SomeType *);");
5206   verifyFormat("int (*func)(void *);");
5207   verifyFormat("void f() { int (*func)(void *); }");
5208   verifyFormat("template <class CallbackClass>\n"
5209                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5210 
5211   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5212   verifyGoogleFormat("void* (*a)(int);");
5213   verifyGoogleFormat(
5214       "template <class CallbackClass>\n"
5215       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5216 
5217   // Other constructs can look somewhat like function types:
5218   verifyFormat("A<sizeof(*x)> a;");
5219   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5220   verifyFormat("some_var = function(*some_pointer_var)[0];");
5221   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5222   verifyFormat("int x = f(&h)();");
5223   verifyFormat("returnsFunction(&param1, &param2)(param);");
5224 }
5225 
5226 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5227   verifyFormat("A (*foo_)[6];");
5228   verifyFormat("vector<int> (*foo_)[6];");
5229 }
5230 
5231 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5232   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5233                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5234   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5235                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5236   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5237                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5238 
5239   // Different ways of ()-initializiation.
5240   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5241                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5242   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5243                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5244   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5245                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5246   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5247                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5248 
5249   // Lambdas should not confuse the variable declaration heuristic.
5250   verifyFormat("LooooooooooooooooongType\n"
5251                "    variable(nullptr, [](A *a) {});",
5252                getLLVMStyleWithColumns(40));
5253 }
5254 
5255 TEST_F(FormatTest, BreaksLongDeclarations) {
5256   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5257                "    AnotherNameForTheLongType;");
5258   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5259                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5260   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5261                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5262   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5263                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5264   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5265                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5266   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5267                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5268   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5269                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5270   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5271                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5272   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5273                "LooooooooooooooooooooooooooongFunctionDeclaration(T... t);");
5274   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5275                "LooooooooooooooooooooooooooongFunctionDeclaration(T /*t*/) {}");
5276   FormatStyle Indented = getLLVMStyle();
5277   Indented.IndentWrappedFunctionNames = true;
5278   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5279                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5280                Indented);
5281   verifyFormat(
5282       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5283       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5284       Indented);
5285   verifyFormat(
5286       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5287       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5288       Indented);
5289   verifyFormat(
5290       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5291       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5292       Indented);
5293 
5294   // FIXME: Without the comment, this breaks after "(".
5295   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5296                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5297                getGoogleStyle());
5298 
5299   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5300                "                  int LoooooooooooooooooooongParam2) {}");
5301   verifyFormat(
5302       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5303       "                                   SourceLocation L, IdentifierIn *II,\n"
5304       "                                   Type *T) {}");
5305   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5306                "ReallyReaaallyLongFunctionName(\n"
5307                "    const std::string &SomeParameter,\n"
5308                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5309                "        &ReallyReallyLongParameterName,\n"
5310                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5311                "        &AnotherLongParameterName) {}");
5312   verifyFormat("template <typename A>\n"
5313                "SomeLoooooooooooooooooooooongType<\n"
5314                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5315                "Function() {}");
5316 
5317   verifyGoogleFormat(
5318       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5319       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5320   verifyGoogleFormat(
5321       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5322       "                                   SourceLocation L) {}");
5323   verifyGoogleFormat(
5324       "some_namespace::LongReturnType\n"
5325       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5326       "    int first_long_parameter, int second_parameter) {}");
5327 
5328   verifyGoogleFormat("template <typename T>\n"
5329                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5330                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5331   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5332                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5333 
5334   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5335                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5336                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5337   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5338                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5339                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5340   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5341                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5342                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5343                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5344 
5345   verifyFormat("template <typename T> // Templates on own line.\n"
5346                "static int            // Some comment.\n"
5347                "MyFunction(int a);",
5348                getLLVMStyle());
5349 }
5350 
5351 TEST_F(FormatTest, FormatsArrays) {
5352   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5353                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5354   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaa(aaaaaaaaaaaa)]\n"
5355                "                         [bbbbbbbbbbb(bbbbbbbbbbbb)] = c;");
5356   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaa &&\n"
5357                "    aaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaa][aaaaaaaaaaaaa]) {\n}");
5358   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5359                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5360   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5361                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5362   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5363                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5364                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5365   verifyFormat(
5366       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5367       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5368       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5369   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaa][a]\n"
5370                "    .aaaaaaaaaaaaaaaaaaaaaa();");
5371 
5372   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5373                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5374   verifyFormat(
5375       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5376       "                                  .aaaaaaa[0]\n"
5377       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5378   verifyFormat("a[::b::c];");
5379 
5380   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5381 
5382   FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0);
5383   verifyFormat("aaaaa[bbbbbb].cccccc()", NoColumnLimit);
5384 }
5385 
5386 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5387   verifyFormat("(a)->b();");
5388   verifyFormat("--a;");
5389 }
5390 
5391 TEST_F(FormatTest, HandlesIncludeDirectives) {
5392   verifyFormat("#include <string>\n"
5393                "#include <a/b/c.h>\n"
5394                "#include \"a/b/string\"\n"
5395                "#include \"string.h\"\n"
5396                "#include \"string.h\"\n"
5397                "#include <a-a>\n"
5398                "#include < path with space >\n"
5399                "#include_next <test.h>"
5400                "#include \"abc.h\" // this is included for ABC\n"
5401                "#include \"some long include\" // with a comment\n"
5402                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5403                getLLVMStyleWithColumns(35));
5404   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5405   EXPECT_EQ("#include <a>", format("#include<a>"));
5406 
5407   verifyFormat("#import <string>");
5408   verifyFormat("#import <a/b/c.h>");
5409   verifyFormat("#import \"a/b/string\"");
5410   verifyFormat("#import \"string.h\"");
5411   verifyFormat("#import \"string.h\"");
5412   verifyFormat("#if __has_include(<strstream>)\n"
5413                "#include <strstream>\n"
5414                "#endif");
5415 
5416   verifyFormat("#define MY_IMPORT <a/b>");
5417 
5418   verifyFormat("#if __has_include(<a/b>)");
5419   verifyFormat("#if __has_include_next(<a/b>)");
5420   verifyFormat("#define F __has_include(<a/b>)");
5421   verifyFormat("#define F __has_include_next(<a/b>)");
5422 
5423   // Protocol buffer definition or missing "#".
5424   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5425                getLLVMStyleWithColumns(30));
5426 
5427   FormatStyle Style = getLLVMStyle();
5428   Style.AlwaysBreakBeforeMultilineStrings = true;
5429   Style.ColumnLimit = 0;
5430   verifyFormat("#import \"abc.h\"", Style);
5431 
5432   // But 'import' might also be a regular C++ namespace.
5433   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5434                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5435 }
5436 
5437 //===----------------------------------------------------------------------===//
5438 // Error recovery tests.
5439 //===----------------------------------------------------------------------===//
5440 
5441 TEST_F(FormatTest, IncompleteParameterLists) {
5442   FormatStyle NoBinPacking = getLLVMStyle();
5443   NoBinPacking.BinPackParameters = false;
5444   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5445                "                        double *min_x,\n"
5446                "                        double *max_x,\n"
5447                "                        double *min_y,\n"
5448                "                        double *max_y,\n"
5449                "                        double *min_z,\n"
5450                "                        double *max_z, ) {}",
5451                NoBinPacking);
5452 }
5453 
5454 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5455   verifyFormat("void f() { return; }\n42");
5456   verifyFormat("void f() {\n"
5457                "  if (0)\n"
5458                "    return;\n"
5459                "}\n"
5460                "42");
5461   verifyFormat("void f() { return }\n42");
5462   verifyFormat("void f() {\n"
5463                "  if (0)\n"
5464                "    return\n"
5465                "}\n"
5466                "42");
5467 }
5468 
5469 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5470   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5471   EXPECT_EQ("void f() {\n"
5472             "  if (a)\n"
5473             "    return\n"
5474             "}",
5475             format("void  f  (  )  {  if  ( a )  return  }"));
5476   EXPECT_EQ("namespace N {\n"
5477             "void f()\n"
5478             "}",
5479             format("namespace  N  {  void f()  }"));
5480   EXPECT_EQ("namespace N {\n"
5481             "void f() {}\n"
5482             "void g()\n"
5483             "} // namespace N",
5484             format("namespace N  { void f( ) { } void g( ) }"));
5485 }
5486 
5487 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5488   verifyFormat("int aaaaaaaa =\n"
5489                "    // Overlylongcomment\n"
5490                "    b;",
5491                getLLVMStyleWithColumns(20));
5492   verifyFormat("function(\n"
5493                "    ShortArgument,\n"
5494                "    LoooooooooooongArgument);\n",
5495                getLLVMStyleWithColumns(20));
5496 }
5497 
5498 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5499   verifyFormat("public:");
5500   verifyFormat("class A {\n"
5501                "public\n"
5502                "  void f() {}\n"
5503                "};");
5504   verifyFormat("public\n"
5505                "int qwerty;");
5506   verifyFormat("public\n"
5507                "B {}");
5508   verifyFormat("public\n"
5509                "{}");
5510   verifyFormat("public\n"
5511                "B { int x; }");
5512 }
5513 
5514 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5515   verifyFormat("{");
5516   verifyFormat("#})");
5517   verifyNoCrash("(/**/[:!] ?[).");
5518 }
5519 
5520 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5521   verifyFormat("do {\n}");
5522   verifyFormat("do {\n}\n"
5523                "f();");
5524   verifyFormat("do {\n}\n"
5525                "wheeee(fun);");
5526   verifyFormat("do {\n"
5527                "  f();\n"
5528                "}");
5529 }
5530 
5531 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5532   verifyFormat("if {\n  foo;\n  foo();\n}");
5533   verifyFormat("switch {\n  foo;\n  foo();\n}");
5534   verifyIncompleteFormat("for {\n  foo;\n  foo();\n}");
5535   verifyFormat("while {\n  foo;\n  foo();\n}");
5536   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5537 }
5538 
5539 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5540   verifyIncompleteFormat("namespace {\n"
5541                          "class Foo { Foo (\n"
5542                          "};\n"
5543                          "} // namespace");
5544 }
5545 
5546 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5547   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5548   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5549   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5550   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5551 
5552   EXPECT_EQ("{\n"
5553             "  {\n"
5554             "    breakme(\n"
5555             "        qwe);\n"
5556             "  }\n",
5557             format("{\n"
5558                    "    {\n"
5559                    " breakme(qwe);\n"
5560                    "}\n",
5561                    getLLVMStyleWithColumns(10)));
5562 }
5563 
5564 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5565   verifyFormat("int x = {\n"
5566                "    avariable,\n"
5567                "    b(alongervariable)};",
5568                getLLVMStyleWithColumns(25));
5569 }
5570 
5571 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5572   verifyFormat("return (a)(b){1, 2, 3};");
5573 }
5574 
5575 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5576   verifyFormat("vector<int> x{1, 2, 3, 4};");
5577   verifyFormat("vector<int> x{\n"
5578                "    1, 2, 3, 4,\n"
5579                "};");
5580   verifyFormat("vector<T> x{{}, {}, {}, {}};");
5581   verifyFormat("f({1, 2});");
5582   verifyFormat("auto v = Foo{-1};");
5583   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5584   verifyFormat("Class::Class : member{1, 2, 3} {}");
5585   verifyFormat("new vector<int>{1, 2, 3};");
5586   verifyFormat("new int[3]{1, 2, 3};");
5587   verifyFormat("new int{1};");
5588   verifyFormat("return {arg1, arg2};");
5589   verifyFormat("return {arg1, SomeType{parameter}};");
5590   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5591   verifyFormat("new T{arg1, arg2};");
5592   verifyFormat("f(MyMap[{composite, key}]);");
5593   verifyFormat("class Class {\n"
5594                "  T member = {arg1, arg2};\n"
5595                "};");
5596   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5597   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5598   verifyFormat("int a = std::is_integral<int>{} + 0;");
5599 
5600   verifyFormat("int foo(int i) { return fo1{}(i); }");
5601   verifyFormat("int foo(int i) { return fo1{}(i); }");
5602   verifyFormat("auto i = decltype(x){};");
5603   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
5604   verifyFormat("Node n{1, Node{1000}, //\n"
5605                "       2};");
5606   verifyFormat("Aaaa aaaaaaa{\n"
5607                "    {\n"
5608                "        aaaa,\n"
5609                "    },\n"
5610                "};");
5611   verifyFormat("class C : public D {\n"
5612                "  SomeClass SC{2};\n"
5613                "};");
5614   verifyFormat("class C : public A {\n"
5615                "  class D : public B {\n"
5616                "    void f() { int i{2}; }\n"
5617                "  };\n"
5618                "};");
5619   verifyFormat("#define A {a, a},");
5620 
5621   // Cases where distinguising braced lists and blocks is hard.
5622   verifyFormat("vector<int> v{12} GUARDED_BY(mutex);");
5623   verifyFormat("void f() {\n"
5624                "  return; // comment\n"
5625                "}\n"
5626                "SomeType t;");
5627   verifyFormat("void f() {\n"
5628                "  if (a) {\n"
5629                "    f();\n"
5630                "  }\n"
5631                "}\n"
5632                "SomeType t;");
5633 
5634   // In combination with BinPackArguments = false.
5635   FormatStyle NoBinPacking = getLLVMStyle();
5636   NoBinPacking.BinPackArguments = false;
5637   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5638                "                      bbbbb,\n"
5639                "                      ccccc,\n"
5640                "                      ddddd,\n"
5641                "                      eeeee,\n"
5642                "                      ffffff,\n"
5643                "                      ggggg,\n"
5644                "                      hhhhhh,\n"
5645                "                      iiiiii,\n"
5646                "                      jjjjjj,\n"
5647                "                      kkkkkk};",
5648                NoBinPacking);
5649   verifyFormat("const Aaaaaa aaaaa = {\n"
5650                "    aaaaa,\n"
5651                "    bbbbb,\n"
5652                "    ccccc,\n"
5653                "    ddddd,\n"
5654                "    eeeee,\n"
5655                "    ffffff,\n"
5656                "    ggggg,\n"
5657                "    hhhhhh,\n"
5658                "    iiiiii,\n"
5659                "    jjjjjj,\n"
5660                "    kkkkkk,\n"
5661                "};",
5662                NoBinPacking);
5663   verifyFormat(
5664       "const Aaaaaa aaaaa = {\n"
5665       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
5666       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
5667       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5668       "};",
5669       NoBinPacking);
5670 
5671   // FIXME: The alignment of these trailing comments might be bad. Then again,
5672   // this might be utterly useless in real code.
5673   verifyFormat("Constructor::Constructor()\n"
5674                "    : some_value{         //\n"
5675                "                 aaaaaaa, //\n"
5676                "                 bbbbbbb} {}");
5677 
5678   // In braced lists, the first comment is always assumed to belong to the
5679   // first element. Thus, it can be moved to the next or previous line as
5680   // appropriate.
5681   EXPECT_EQ("function({// First element:\n"
5682             "          1,\n"
5683             "          // Second element:\n"
5684             "          2});",
5685             format("function({\n"
5686                    "    // First element:\n"
5687                    "    1,\n"
5688                    "    // Second element:\n"
5689                    "    2});"));
5690   EXPECT_EQ("std::vector<int> MyNumbers{\n"
5691             "    // First element:\n"
5692             "    1,\n"
5693             "    // Second element:\n"
5694             "    2};",
5695             format("std::vector<int> MyNumbers{// First element:\n"
5696                    "                           1,\n"
5697                    "                           // Second element:\n"
5698                    "                           2};",
5699                    getLLVMStyleWithColumns(30)));
5700   // A trailing comma should still lead to an enforced line break.
5701   EXPECT_EQ("vector<int> SomeVector = {\n"
5702             "    // aaa\n"
5703             "    1, 2,\n"
5704             "};",
5705             format("vector<int> SomeVector = { // aaa\n"
5706                    "    1, 2, };"));
5707 
5708   FormatStyle ExtraSpaces = getLLVMStyle();
5709   ExtraSpaces.Cpp11BracedListStyle = false;
5710   ExtraSpaces.ColumnLimit = 75;
5711   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
5712   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
5713   verifyFormat("f({ 1, 2 });", ExtraSpaces);
5714   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
5715   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
5716   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
5717   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
5718   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
5719   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
5720   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
5721   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
5722   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
5723   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
5724   verifyFormat("class Class {\n"
5725                "  T member = { arg1, arg2 };\n"
5726                "};",
5727                ExtraSpaces);
5728   verifyFormat(
5729       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5730       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
5731       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5732       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
5733       ExtraSpaces);
5734   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
5735   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
5736                ExtraSpaces);
5737   verifyFormat(
5738       "someFunction(OtherParam,\n"
5739       "             BracedList{ // comment 1 (Forcing interesting break)\n"
5740       "                         param1, param2,\n"
5741       "                         // comment 2\n"
5742       "                         param3, param4 });",
5743       ExtraSpaces);
5744   verifyFormat(
5745       "std::this_thread::sleep_for(\n"
5746       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
5747       ExtraSpaces);
5748   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa{\n"
5749                "    aaaaaaa,\n"
5750                "    aaaaaaaaaa,\n"
5751                "    aaaaa,\n"
5752                "    aaaaaaaaaaaaaaa,\n"
5753                "    aaa,\n"
5754                "    aaaaaaaaaa,\n"
5755                "    a,\n"
5756                "    aaaaaaaaaaaaaaaaaaaaa,\n"
5757                "    aaaaaaaaaaaa,\n"
5758                "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
5759                "    aaaaaaa,\n"
5760                "    a};");
5761   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
5762 }
5763 
5764 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
5765   verifyFormat("vector<int> x = {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,\n"
5770                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5771   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777, //\n"
5772                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5773                "                 1, 22, 333, 4444, 55555, //\n"
5774                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5775                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5776   verifyFormat(
5777       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5778       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5779       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
5780       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5781       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5782       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5783       "                 7777777};");
5784   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5785                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5786                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
5787   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5788                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5789                "    // Separating comment.\n"
5790                "    X86::R8, X86::R9, X86::R10, X86::R11, 0};");
5791   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5792                "    // Leading comment\n"
5793                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5794                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
5795   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5796                "                 1, 1, 1, 1};",
5797                getLLVMStyleWithColumns(39));
5798   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5799                "                 1, 1, 1, 1};",
5800                getLLVMStyleWithColumns(38));
5801   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
5802                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
5803                getLLVMStyleWithColumns(43));
5804   verifyFormat(
5805       "static unsigned SomeValues[10][3] = {\n"
5806       "    {1, 4, 0},  {4, 9, 0},  {4, 5, 9},  {8, 5, 4}, {1, 8, 4},\n"
5807       "    {10, 1, 6}, {11, 0, 9}, {2, 11, 9}, {5, 2, 9}, {11, 2, 7}};");
5808   verifyFormat("static auto fields = new vector<string>{\n"
5809                "    \"aaaaaaaaaaaaa\",\n"
5810                "    \"aaaaaaaaaaaaa\",\n"
5811                "    \"aaaaaaaaaaaa\",\n"
5812                "    \"aaaaaaaaaaaaaa\",\n"
5813                "    \"aaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
5814                "    \"aaaaaaaaaaaa\",\n"
5815                "    \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\",\n"
5816                "};");
5817   verifyFormat("vector<int> x = {1, 2, 3, 4, aaaaaaaaaaaaaaaaa, 6};");
5818   verifyFormat("vector<int> x = {1, aaaaaaaaaaaaaaaaaaaaaa,\n"
5819                "                 2, bbbbbbbbbbbbbbbbbbbbbb,\n"
5820                "                 3, cccccccccccccccccccccc};",
5821                getLLVMStyleWithColumns(60));
5822 
5823   // Trailing commas.
5824   verifyFormat("vector<int> x = {\n"
5825                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
5826                "};",
5827                getLLVMStyleWithColumns(39));
5828   verifyFormat("vector<int> x = {\n"
5829                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
5830                "};",
5831                getLLVMStyleWithColumns(39));
5832   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5833                "                 1, 1, 1, 1,\n"
5834                "                 /**/ /**/};",
5835                getLLVMStyleWithColumns(39));
5836 
5837   // Trailing comment in the first line.
5838   verifyFormat("vector<int> iiiiiiiiiiiiiii = {                      //\n"
5839                "    1111111111, 2222222222, 33333333333, 4444444444, //\n"
5840                "    111111111,  222222222,  3333333333,  444444444,  //\n"
5841                "    11111111,   22222222,   333333333,   44444444};");
5842   // Trailing comment in the last line.
5843   verifyFormat("int aaaaa[] = {\n"
5844                "    1, 2, 3, // comment\n"
5845                "    4, 5, 6  // comment\n"
5846                "};");
5847 
5848   // With nested lists, we should either format one item per line or all nested
5849   // lists one on line.
5850   // FIXME: For some nested lists, we can do better.
5851   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
5852                "        {aaaaaaaaaaaaaaaaaaa},\n"
5853                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
5854                "        {aaaaaaaaaaaaaaaaa}};",
5855                getLLVMStyleWithColumns(60));
5856   verifyFormat(
5857       "SomeStruct my_struct_array = {\n"
5858       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
5859       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
5860       "    {aaa, aaa},\n"
5861       "    {aaa, aaa},\n"
5862       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
5863       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5864       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
5865 
5866   // No column layout should be used here.
5867   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
5868                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
5869 
5870   verifyNoCrash("a<,");
5871 
5872   // No braced initializer here.
5873   verifyFormat("void f() {\n"
5874                "  struct Dummy {};\n"
5875                "  f(v);\n"
5876                "}");
5877 
5878   // Long lists should be formatted in columns even if they are nested.
5879   verifyFormat(
5880       "vector<int> x = function({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,\n"
5885       "                          1, 22, 333, 4444, 55555, 666666, 7777777});");
5886 
5887   // Allow "single-column" layout even if that violates the column limit. There
5888   // isn't going to be a better way.
5889   verifyFormat("std::vector<int> a = {\n"
5890                "    aaaaaaaa,\n"
5891                "    aaaaaaaa,\n"
5892                "    aaaaaaaa,\n"
5893                "    aaaaaaaa,\n"
5894                "    aaaaaaaaaa,\n"
5895                "    aaaaaaaa,\n"
5896                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa};",
5897                getLLVMStyleWithColumns(30));
5898   verifyFormat("vector<int> aaaa = {\n"
5899                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5900                "    aaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5901                "    aaaaaa.aaaaaaa,\n"
5902                "    aaaaaa.aaaaaaa,\n"
5903                "    aaaaaa.aaaaaaa,\n"
5904                "    aaaaaa.aaaaaaa,\n"
5905                "};");
5906 
5907   // Don't create hanging lists.
5908   verifyFormat("someFunction(Param, {List1, List2,\n"
5909                "                     List3});",
5910                getLLVMStyleWithColumns(35));
5911   verifyFormat("someFunction(Param, Param,\n"
5912                "             {List1, List2,\n"
5913                "              List3});",
5914                getLLVMStyleWithColumns(35));
5915   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa, {},\n"
5916                "                               aaaaaaaaaaaaaaaaaaaaaaa);");
5917 }
5918 
5919 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
5920   FormatStyle DoNotMerge = getLLVMStyle();
5921   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5922 
5923   verifyFormat("void f() { return 42; }");
5924   verifyFormat("void f() {\n"
5925                "  return 42;\n"
5926                "}",
5927                DoNotMerge);
5928   verifyFormat("void f() {\n"
5929                "  // Comment\n"
5930                "}");
5931   verifyFormat("{\n"
5932                "#error {\n"
5933                "  int a;\n"
5934                "}");
5935   verifyFormat("{\n"
5936                "  int a;\n"
5937                "#error {\n"
5938                "}");
5939   verifyFormat("void f() {} // comment");
5940   verifyFormat("void f() { int a; } // comment");
5941   verifyFormat("void f() {\n"
5942                "} // comment",
5943                DoNotMerge);
5944   verifyFormat("void f() {\n"
5945                "  int a;\n"
5946                "} // comment",
5947                DoNotMerge);
5948   verifyFormat("void f() {\n"
5949                "} // comment",
5950                getLLVMStyleWithColumns(15));
5951 
5952   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
5953   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
5954 
5955   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
5956   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
5957   verifyFormat("class C {\n"
5958                "  C()\n"
5959                "      : iiiiiiii(nullptr),\n"
5960                "        kkkkkkk(nullptr),\n"
5961                "        mmmmmmm(nullptr),\n"
5962                "        nnnnnnn(nullptr) {}\n"
5963                "};",
5964                getGoogleStyle());
5965 
5966   FormatStyle NoColumnLimit = getLLVMStyle();
5967   NoColumnLimit.ColumnLimit = 0;
5968   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
5969   EXPECT_EQ("class C {\n"
5970             "  A() : b(0) {}\n"
5971             "};",
5972             format("class C{A():b(0){}};", NoColumnLimit));
5973   EXPECT_EQ("A()\n"
5974             "    : b(0) {\n"
5975             "}",
5976             format("A()\n:b(0)\n{\n}", NoColumnLimit));
5977 
5978   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
5979   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
5980       FormatStyle::SFS_None;
5981   EXPECT_EQ("A()\n"
5982             "    : b(0) {\n"
5983             "}",
5984             format("A():b(0){}", DoNotMergeNoColumnLimit));
5985   EXPECT_EQ("A()\n"
5986             "    : b(0) {\n"
5987             "}",
5988             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
5989 
5990   verifyFormat("#define A          \\\n"
5991                "  void f() {       \\\n"
5992                "    int i;         \\\n"
5993                "  }",
5994                getLLVMStyleWithColumns(20));
5995   verifyFormat("#define A           \\\n"
5996                "  void f() { int i; }",
5997                getLLVMStyleWithColumns(21));
5998   verifyFormat("#define A            \\\n"
5999                "  void f() {         \\\n"
6000                "    int i;           \\\n"
6001                "  }                  \\\n"
6002                "  int j;",
6003                getLLVMStyleWithColumns(22));
6004   verifyFormat("#define A             \\\n"
6005                "  void f() { int i; } \\\n"
6006                "  int j;",
6007                getLLVMStyleWithColumns(23));
6008 }
6009 
6010 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6011   FormatStyle MergeInlineOnly = getLLVMStyle();
6012   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6013   verifyFormat("class C {\n"
6014                "  int f() { return 42; }\n"
6015                "};",
6016                MergeInlineOnly);
6017   verifyFormat("int f() {\n"
6018                "  return 42;\n"
6019                "}",
6020                MergeInlineOnly);
6021 }
6022 
6023 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6024   // Elaborate type variable declarations.
6025   verifyFormat("struct foo a = {bar};\nint n;");
6026   verifyFormat("class foo a = {bar};\nint n;");
6027   verifyFormat("union foo a = {bar};\nint n;");
6028 
6029   // Elaborate types inside function definitions.
6030   verifyFormat("struct foo f() {}\nint n;");
6031   verifyFormat("class foo f() {}\nint n;");
6032   verifyFormat("union foo f() {}\nint n;");
6033 
6034   // Templates.
6035   verifyFormat("template <class X> void f() {}\nint n;");
6036   verifyFormat("template <struct X> void f() {}\nint n;");
6037   verifyFormat("template <union X> void f() {}\nint n;");
6038 
6039   // Actual definitions...
6040   verifyFormat("struct {\n} n;");
6041   verifyFormat(
6042       "template <template <class T, class Y>, class Z> class X {\n} n;");
6043   verifyFormat("union Z {\n  int n;\n} x;");
6044   verifyFormat("class MACRO Z {\n} n;");
6045   verifyFormat("class MACRO(X) Z {\n} n;");
6046   verifyFormat("class __attribute__(X) Z {\n} n;");
6047   verifyFormat("class __declspec(X) Z {\n} n;");
6048   verifyFormat("class A##B##C {\n} n;");
6049   verifyFormat("class alignas(16) Z {\n} n;");
6050   verifyFormat("class MACRO(X) alignas(16) Z {\n} n;");
6051   verifyFormat("class MACROA MACRO(X) Z {\n} n;");
6052 
6053   // Redefinition from nested context:
6054   verifyFormat("class A::B::C {\n} n;");
6055 
6056   // Template definitions.
6057   verifyFormat(
6058       "template <typename F>\n"
6059       "Matcher(const Matcher<F> &Other,\n"
6060       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6061       "                             !is_same<F, T>::value>::type * = 0)\n"
6062       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6063 
6064   // FIXME: This is still incorrectly handled at the formatter side.
6065   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6066   verifyFormat("int i = SomeFunction(a<b, a> b);");
6067 
6068   // FIXME:
6069   // This now gets parsed incorrectly as class definition.
6070   // verifyFormat("class A<int> f() {\n}\nint n;");
6071 
6072   // Elaborate types where incorrectly parsing the structural element would
6073   // break the indent.
6074   verifyFormat("if (true)\n"
6075                "  class X x;\n"
6076                "else\n"
6077                "  f();\n");
6078 
6079   // This is simply incomplete. Formatting is not important, but must not crash.
6080   verifyFormat("class A:");
6081 }
6082 
6083 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6084   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6085             format("#error Leave     all         white!!!!! space* alone!\n"));
6086   EXPECT_EQ(
6087       "#warning Leave     all         white!!!!! space* alone!\n",
6088       format("#warning Leave     all         white!!!!! space* alone!\n"));
6089   EXPECT_EQ("#error 1", format("  #  error   1"));
6090   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6091 }
6092 
6093 TEST_F(FormatTest, FormatHashIfExpressions) {
6094   verifyFormat("#if AAAA && BBBB");
6095   verifyFormat("#if (AAAA && BBBB)");
6096   verifyFormat("#elif (AAAA && BBBB)");
6097   // FIXME: Come up with a better indentation for #elif.
6098   verifyFormat(
6099       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6100       "    defined(BBBBBBBB)\n"
6101       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6102       "    defined(BBBBBBBB)\n"
6103       "#endif",
6104       getLLVMStyleWithColumns(65));
6105 }
6106 
6107 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6108   FormatStyle AllowsMergedIf = getGoogleStyle();
6109   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6110   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6111   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6112   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6113   EXPECT_EQ("if (true) return 42;",
6114             format("if (true)\nreturn 42;", AllowsMergedIf));
6115   FormatStyle ShortMergedIf = AllowsMergedIf;
6116   ShortMergedIf.ColumnLimit = 25;
6117   verifyFormat("#define A \\\n"
6118                "  if (true) return 42;",
6119                ShortMergedIf);
6120   verifyFormat("#define A \\\n"
6121                "  f();    \\\n"
6122                "  if (true)\n"
6123                "#define B",
6124                ShortMergedIf);
6125   verifyFormat("#define A \\\n"
6126                "  f();    \\\n"
6127                "  if (true)\n"
6128                "g();",
6129                ShortMergedIf);
6130   verifyFormat("{\n"
6131                "#ifdef A\n"
6132                "  // Comment\n"
6133                "  if (true) continue;\n"
6134                "#endif\n"
6135                "  // Comment\n"
6136                "  if (true) continue;\n"
6137                "}",
6138                ShortMergedIf);
6139   ShortMergedIf.ColumnLimit = 29;
6140   verifyFormat("#define A                   \\\n"
6141                "  if (aaaaaaaaaa) return 1; \\\n"
6142                "  return 2;",
6143                ShortMergedIf);
6144   ShortMergedIf.ColumnLimit = 28;
6145   verifyFormat("#define A         \\\n"
6146                "  if (aaaaaaaaaa) \\\n"
6147                "    return 1;     \\\n"
6148                "  return 2;",
6149                ShortMergedIf);
6150 }
6151 
6152 TEST_F(FormatTest, FormatStarDependingOnContext) {
6153   verifyFormat("void f(int *a);");
6154   verifyFormat("void f() { f(fint * b); }");
6155   verifyFormat("class A {\n  void f(int *a);\n};");
6156   verifyFormat("class A {\n  int *a;\n};");
6157   verifyFormat("namespace a {\n"
6158                "namespace b {\n"
6159                "class A {\n"
6160                "  void f() {}\n"
6161                "  int *a;\n"
6162                "};\n"
6163                "} // namespace b\n"
6164                "} // namespace a");
6165 }
6166 
6167 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6168   verifyFormat("while");
6169   verifyFormat("operator");
6170 }
6171 
6172 TEST_F(FormatTest, SkipsDeeplyNestedLines) {
6173   // This code would be painfully slow to format if we didn't skip it.
6174   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
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(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(A(\n"
6179                    "A(1, 1)\n"
6180                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1)\n" // 10x
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                    ", 1), 1), 1), 1), 1), 1), 1), 1), 1), 1);\n");
6190   // Deeply nested part is untouched, rest is formatted.
6191   EXPECT_EQ(std::string("int i;\n") + Code + "int j;\n",
6192             format(std::string("int    i;\n") + Code + "int    j;\n",
6193                    getLLVMStyle(), SC_ExpectIncomplete));
6194 }
6195 
6196 //===----------------------------------------------------------------------===//
6197 // Objective-C tests.
6198 //===----------------------------------------------------------------------===//
6199 
6200 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6201   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6202   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6203             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6204   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6205   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6206   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6207             format("-(NSInteger)Method3:(id)anObject;"));
6208   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6209             format("-(NSInteger)Method4:(id)anObject;"));
6210   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6211             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6212   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6213             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6214   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6215             "forAllCells:(BOOL)flag;",
6216             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6217                    "forAllCells:(BOOL)flag;"));
6218 
6219   // Very long objectiveC method declaration.
6220   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6221                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6222   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6223                "                    inRange:(NSRange)range\n"
6224                "                   outRange:(NSRange)out_range\n"
6225                "                  outRange1:(NSRange)out_range1\n"
6226                "                  outRange2:(NSRange)out_range2\n"
6227                "                  outRange3:(NSRange)out_range3\n"
6228                "                  outRange4:(NSRange)out_range4\n"
6229                "                  outRange5:(NSRange)out_range5\n"
6230                "                  outRange6:(NSRange)out_range6\n"
6231                "                  outRange7:(NSRange)out_range7\n"
6232                "                  outRange8:(NSRange)out_range8\n"
6233                "                  outRange9:(NSRange)out_range9;");
6234 
6235   // When the function name has to be wrapped.
6236   FormatStyle Style = getLLVMStyle();
6237   Style.IndentWrappedFunctionNames = false;
6238   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6239                "veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6240                "           anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6241                "}",
6242                Style);
6243   Style.IndentWrappedFunctionNames = true;
6244   verifyFormat("- (SomeLooooooooooooooooooooongType *)\n"
6245                "    veryLooooooooooongName:(NSString)aaaaaaaaaaaaaa\n"
6246                "               anotherName:(NSString)bbbbbbbbbbbbbb {\n"
6247                "}",
6248                Style);
6249 
6250   verifyFormat("- (int)sum:(vector<int>)numbers;");
6251   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6252   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6253   // protocol lists (but not for template classes):
6254   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6255 
6256   verifyFormat("- (int (*)())foo:(int (*)())f;");
6257   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6258 
6259   // If there's no return type (very rare in practice!), LLVM and Google style
6260   // agree.
6261   verifyFormat("- foo;");
6262   verifyFormat("- foo:(int)f;");
6263   verifyGoogleFormat("- foo:(int)foo;");
6264 }
6265 
6266 
6267 TEST_F(FormatTest, BreaksStringLiterals) {
6268   EXPECT_EQ("\"some text \"\n"
6269             "\"other\";",
6270             format("\"some text other\";", getLLVMStyleWithColumns(12)));
6271   EXPECT_EQ("\"some text \"\n"
6272             "\"other\";",
6273             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
6274   EXPECT_EQ(
6275       "#define A  \\\n"
6276       "  \"some \"  \\\n"
6277       "  \"text \"  \\\n"
6278       "  \"other\";",
6279       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
6280   EXPECT_EQ(
6281       "#define A  \\\n"
6282       "  \"so \"    \\\n"
6283       "  \"text \"  \\\n"
6284       "  \"other\";",
6285       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
6286 
6287   EXPECT_EQ("\"some text\"",
6288             format("\"some text\"", getLLVMStyleWithColumns(1)));
6289   EXPECT_EQ("\"some text\"",
6290             format("\"some text\"", getLLVMStyleWithColumns(11)));
6291   EXPECT_EQ("\"some \"\n"
6292             "\"text\"",
6293             format("\"some text\"", getLLVMStyleWithColumns(10)));
6294   EXPECT_EQ("\"some \"\n"
6295             "\"text\"",
6296             format("\"some text\"", getLLVMStyleWithColumns(7)));
6297   EXPECT_EQ("\"some\"\n"
6298             "\" tex\"\n"
6299             "\"t\"",
6300             format("\"some text\"", getLLVMStyleWithColumns(6)));
6301   EXPECT_EQ("\"some\"\n"
6302             "\" tex\"\n"
6303             "\" and\"",
6304             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
6305   EXPECT_EQ("\"some\"\n"
6306             "\"/tex\"\n"
6307             "\"/and\"",
6308             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
6309 
6310   EXPECT_EQ("variable =\n"
6311             "    \"long string \"\n"
6312             "    \"literal\";",
6313             format("variable = \"long string literal\";",
6314                    getLLVMStyleWithColumns(20)));
6315 
6316   EXPECT_EQ("variable = f(\n"
6317             "    \"long string \"\n"
6318             "    \"literal\",\n"
6319             "    short,\n"
6320             "    loooooooooooooooooooong);",
6321             format("variable = f(\"long string literal\", short, "
6322                    "loooooooooooooooooooong);",
6323                    getLLVMStyleWithColumns(20)));
6324 
6325   EXPECT_EQ(
6326       "f(g(\"long string \"\n"
6327       "    \"literal\"),\n"
6328       "  b);",
6329       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
6330   EXPECT_EQ("f(g(\"long string \"\n"
6331             "    \"literal\",\n"
6332             "    a),\n"
6333             "  b);",
6334             format("f(g(\"long string literal\", a), b);",
6335                    getLLVMStyleWithColumns(20)));
6336   EXPECT_EQ(
6337       "f(\"one two\".split(\n"
6338       "    variable));",
6339       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
6340   EXPECT_EQ("f(\"one two three four five six \"\n"
6341             "  \"seven\".split(\n"
6342             "      really_looooong_variable));",
6343             format("f(\"one two three four five six seven\"."
6344                    "split(really_looooong_variable));",
6345                    getLLVMStyleWithColumns(33)));
6346 
6347   EXPECT_EQ("f(\"some \"\n"
6348             "  \"text\",\n"
6349             "  other);",
6350             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
6351 
6352   // Only break as a last resort.
6353   verifyFormat(
6354       "aaaaaaaaaaaaaaaaaaaa(\n"
6355       "    aaaaaaaaaaaaaaaaaaaa,\n"
6356       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
6357 
6358   EXPECT_EQ("\"splitmea\"\n"
6359             "\"trandomp\"\n"
6360             "\"oint\"",
6361             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
6362 
6363   EXPECT_EQ("\"split/\"\n"
6364             "\"pathat/\"\n"
6365             "\"slashes\"",
6366             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6367 
6368   EXPECT_EQ("\"split/\"\n"
6369             "\"pathat/\"\n"
6370             "\"slashes\"",
6371             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6372   EXPECT_EQ("\"split at \"\n"
6373             "\"spaces/at/\"\n"
6374             "\"slashes.at.any$\"\n"
6375             "\"non-alphanumeric%\"\n"
6376             "\"1111111111characte\"\n"
6377             "\"rs\"",
6378             format("\"split at "
6379                    "spaces/at/"
6380                    "slashes.at."
6381                    "any$non-"
6382                    "alphanumeric%"
6383                    "1111111111characte"
6384                    "rs\"",
6385                    getLLVMStyleWithColumns(20)));
6386 
6387   // Verify that splitting the strings understands
6388   // Style::AlwaysBreakBeforeMultilineStrings.
6389   EXPECT_EQ(
6390       "aaaaaaaaaaaa(\n"
6391       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
6392       "    \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
6393       format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa "
6394              "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6395              "aaaaaaaaaaaaaaaaaaaaaa\");",
6396              getGoogleStyle()));
6397   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6398             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
6399             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
6400                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6401                    "aaaaaaaaaaaaaaaaaaaaaa\";",
6402                    getGoogleStyle()));
6403   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6404             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6405             format("llvm::outs() << "
6406                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
6407                    "aaaaaaaaaaaaaaaaaaa\";"));
6408   EXPECT_EQ("ffff(\n"
6409             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6410             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6411             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6412                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6413                    getGoogleStyle()));
6414 
6415   FormatStyle Style = getLLVMStyleWithColumns(12);
6416   Style.BreakStringLiterals = false;
6417   EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
6418 
6419   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
6420   AlignLeft.AlignEscapedNewlinesLeft = true;
6421   EXPECT_EQ("#define A \\\n"
6422             "  \"some \" \\\n"
6423             "  \"text \" \\\n"
6424             "  \"other\";",
6425             format("#define A \"some text other\";", AlignLeft));
6426 }
6427 
6428 TEST_F(FormatTest, FullyRemoveEmptyLines) {
6429   FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80);
6430   NoEmptyLines.MaxEmptyLinesToKeep = 0;
6431   EXPECT_EQ("int i = a(b());",
6432             format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines));
6433 }
6434 
6435 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
6436   EXPECT_EQ(
6437       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6438       "(\n"
6439       "    \"x\t\");",
6440       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
6441              "aaaaaaa("
6442              "\"x\t\");"));
6443 }
6444 
6445 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
6446   EXPECT_EQ(
6447       "u8\"utf8 string \"\n"
6448       "u8\"literal\";",
6449       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
6450   EXPECT_EQ(
6451       "u\"utf16 string \"\n"
6452       "u\"literal\";",
6453       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
6454   EXPECT_EQ(
6455       "U\"utf32 string \"\n"
6456       "U\"literal\";",
6457       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
6458   EXPECT_EQ("L\"wide string \"\n"
6459             "L\"literal\";",
6460             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
6461   EXPECT_EQ("@\"NSString \"\n"
6462             "@\"literal\";",
6463             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
6464   verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26));
6465 
6466   // This input makes clang-format try to split the incomplete unicode escape
6467   // sequence, which used to lead to a crasher.
6468   verifyNoCrash(
6469       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6470       getLLVMStyleWithColumns(60));
6471 }
6472 
6473 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
6474   FormatStyle Style = getGoogleStyleWithColumns(15);
6475   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
6476   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
6477   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
6478   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
6479   EXPECT_EQ("u8R\"x(raw literal)x\";",
6480             format("u8R\"x(raw literal)x\";", Style));
6481 }
6482 
6483 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
6484   FormatStyle Style = getLLVMStyleWithColumns(20);
6485   EXPECT_EQ(
6486       "_T(\"aaaaaaaaaaaaaa\")\n"
6487       "_T(\"aaaaaaaaaaaaaa\")\n"
6488       "_T(\"aaaaaaaaaaaa\")",
6489       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
6490   EXPECT_EQ("f(x,\n"
6491             "  _T(\"aaaaaaaaaaaa\")\n"
6492             "  _T(\"aaa\"),\n"
6493             "  z);",
6494             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
6495 
6496   // FIXME: Handle embedded spaces in one iteration.
6497   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
6498   //            "_T(\"aaaaaaaaaaaaa\")\n"
6499   //            "_T(\"aaaaaaaaaaaaa\")\n"
6500   //            "_T(\"a\")",
6501   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6502   //                   getLLVMStyleWithColumns(20)));
6503   EXPECT_EQ(
6504       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6505       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
6506   EXPECT_EQ("f(\n"
6507             "#if !TEST\n"
6508             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6509             "#endif\n"
6510             "    );",
6511             format("f(\n"
6512                    "#if !TEST\n"
6513                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
6514                    "#endif\n"
6515                    ");"));
6516   EXPECT_EQ("f(\n"
6517             "\n"
6518             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
6519             format("f(\n"
6520                    "\n"
6521                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
6522 }
6523 
6524 TEST_F(FormatTest, BreaksStringLiteralOperands) {
6525   // In a function call with two operands, the second can be broken with no line
6526   // break before it.
6527   EXPECT_EQ("func(a, \"long long \"\n"
6528             "        \"long long\");",
6529             format("func(a, \"long long long long\");",
6530                    getLLVMStyleWithColumns(24)));
6531   // In a function call with three operands, the second must be broken with a
6532   // line break before it.
6533   EXPECT_EQ("func(a,\n"
6534             "     \"long long long \"\n"
6535             "     \"long\",\n"
6536             "     c);",
6537             format("func(a, \"long long long long\", c);",
6538                    getLLVMStyleWithColumns(24)));
6539   // In a function call with three operands, the third must be broken with a
6540   // line break before it.
6541   EXPECT_EQ("func(a, b,\n"
6542             "     \"long long long \"\n"
6543             "     \"long\");",
6544             format("func(a, b, \"long long long long\");",
6545                    getLLVMStyleWithColumns(24)));
6546   // In a function call with three operands, both the second and the third must
6547   // be broken with a line break before them.
6548   EXPECT_EQ("func(a,\n"
6549             "     \"long long long \"\n"
6550             "     \"long\",\n"
6551             "     \"long long long \"\n"
6552             "     \"long\");",
6553             format("func(a, \"long long long long\", \"long long long long\");",
6554                    getLLVMStyleWithColumns(24)));
6555   // In a chain of << with two operands, the second can be broken with no line
6556   // break before it.
6557   EXPECT_EQ("a << \"line line \"\n"
6558             "     \"line\";",
6559             format("a << \"line line line\";",
6560                    getLLVMStyleWithColumns(20)));
6561   // In a chain of << with three operands, the second can be broken with no line
6562   // break before it.
6563   EXPECT_EQ("abcde << \"line \"\n"
6564             "         \"line line\"\n"
6565             "      << c;",
6566             format("abcde << \"line line line\" << c;",
6567                    getLLVMStyleWithColumns(20)));
6568   // In a chain of << with three operands, the third must be broken with a line
6569   // break before it.
6570   EXPECT_EQ("a << b\n"
6571             "  << \"line line \"\n"
6572             "     \"line\";",
6573             format("a << b << \"line line line\";",
6574                    getLLVMStyleWithColumns(20)));
6575   // In a chain of << with three operands, the second can be broken with no line
6576   // break before it and the third must be broken with a line break before it.
6577   EXPECT_EQ("abcd << \"line line \"\n"
6578             "        \"line\"\n"
6579             "     << \"line line \"\n"
6580             "        \"line\";",
6581             format("abcd << \"line line line\" << \"line line line\";",
6582                    getLLVMStyleWithColumns(20)));
6583   // In a chain of binary operators with two operands, the second can be broken
6584   // with no line break before it.
6585   EXPECT_EQ("abcd + \"line line \"\n"
6586             "       \"line line\";",
6587             format("abcd + \"line line line line\";",
6588                    getLLVMStyleWithColumns(20)));
6589   // In a chain of binary operators with three operands, the second must be
6590   // broken with a line break before it.
6591   EXPECT_EQ("abcd +\n"
6592             "    \"line line \"\n"
6593             "    \"line line\" +\n"
6594             "    e;",
6595             format("abcd + \"line line line line\" + e;",
6596                    getLLVMStyleWithColumns(20)));
6597   // In a function call with two operands, with AlignAfterOpenBracket enabled,
6598   // the first must be broken with a line break before it.
6599   FormatStyle Style = getLLVMStyleWithColumns(25);
6600   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
6601   EXPECT_EQ("someFunction(\n"
6602             "    \"long long long \"\n"
6603             "    \"long\",\n"
6604             "    a);",
6605             format("someFunction(\"long long long long\", a);", Style));
6606 }
6607 
6608 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
6609   EXPECT_EQ(
6610       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6611       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6612       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6613       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6614              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6615              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
6616 }
6617 
6618 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
6619   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
6620             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
6621   EXPECT_EQ("fffffffffff(g(R\"x(\n"
6622             "multiline raw string literal xxxxxxxxxxxxxx\n"
6623             ")x\",\n"
6624             "              a),\n"
6625             "            b);",
6626             format("fffffffffff(g(R\"x(\n"
6627                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6628                    ")x\", a), b);",
6629                    getGoogleStyleWithColumns(20)));
6630   EXPECT_EQ("fffffffffff(\n"
6631             "    g(R\"x(qqq\n"
6632             "multiline raw string literal xxxxxxxxxxxxxx\n"
6633             ")x\",\n"
6634             "      a),\n"
6635             "    b);",
6636             format("fffffffffff(g(R\"x(qqq\n"
6637                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6638                    ")x\", a), b);",
6639                    getGoogleStyleWithColumns(20)));
6640 
6641   EXPECT_EQ("fffffffffff(R\"x(\n"
6642             "multiline raw string literal xxxxxxxxxxxxxx\n"
6643             ")x\");",
6644             format("fffffffffff(R\"x(\n"
6645                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6646                    ")x\");",
6647                    getGoogleStyleWithColumns(20)));
6648   EXPECT_EQ("fffffffffff(R\"x(\n"
6649             "multiline raw string literal xxxxxxxxxxxxxx\n"
6650             ")x\" + bbbbbb);",
6651             format("fffffffffff(R\"x(\n"
6652                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6653                    ")x\" +   bbbbbb);",
6654                    getGoogleStyleWithColumns(20)));
6655   EXPECT_EQ("fffffffffff(\n"
6656             "    R\"x(\n"
6657             "multiline raw string literal xxxxxxxxxxxxxx\n"
6658             ")x\" +\n"
6659             "    bbbbbb);",
6660             format("fffffffffff(\n"
6661                    " R\"x(\n"
6662                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6663                    ")x\" + bbbbbb);",
6664                    getGoogleStyleWithColumns(20)));
6665 }
6666 
6667 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
6668   verifyFormat("string a = \"unterminated;");
6669   EXPECT_EQ("function(\"unterminated,\n"
6670             "         OtherParameter);",
6671             format("function(  \"unterminated,\n"
6672                    "    OtherParameter);"));
6673 }
6674 
6675 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
6676   FormatStyle Style = getLLVMStyle();
6677   Style.Standard = FormatStyle::LS_Cpp03;
6678   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
6679             format("#define x(_a) printf(\"foo\"_a);", Style));
6680 }
6681 
6682 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
6683 
6684 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
6685   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
6686             "             \"ddeeefff\");",
6687             format("someFunction(\"aaabbbcccdddeeefff\");",
6688                    getLLVMStyleWithColumns(25)));
6689   EXPECT_EQ("someFunction1234567890(\n"
6690             "    \"aaabbbcccdddeeefff\");",
6691             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6692                    getLLVMStyleWithColumns(26)));
6693   EXPECT_EQ("someFunction1234567890(\n"
6694             "    \"aaabbbcccdddeeeff\"\n"
6695             "    \"f\");",
6696             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6697                    getLLVMStyleWithColumns(25)));
6698   EXPECT_EQ("someFunction1234567890(\n"
6699             "    \"aaabbbcccdddeeeff\"\n"
6700             "    \"f\");",
6701             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6702                    getLLVMStyleWithColumns(24)));
6703   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6704             "             \"ddde \"\n"
6705             "             \"efff\");",
6706             format("someFunction(\"aaabbbcc ddde efff\");",
6707                    getLLVMStyleWithColumns(25)));
6708   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
6709             "             \"ddeeefff\");",
6710             format("someFunction(\"aaabbbccc ddeeefff\");",
6711                    getLLVMStyleWithColumns(25)));
6712   EXPECT_EQ("someFunction1234567890(\n"
6713             "    \"aaabb \"\n"
6714             "    \"cccdddeeefff\");",
6715             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
6716                    getLLVMStyleWithColumns(25)));
6717   EXPECT_EQ("#define A          \\\n"
6718             "  string s =       \\\n"
6719             "      \"123456789\"  \\\n"
6720             "      \"0\";         \\\n"
6721             "  int i;",
6722             format("#define A string s = \"1234567890\"; int i;",
6723                    getLLVMStyleWithColumns(20)));
6724   // FIXME: Put additional penalties on breaking at non-whitespace locations.
6725   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6726             "             \"dddeeeff\"\n"
6727             "             \"f\");",
6728             format("someFunction(\"aaabbbcc dddeeefff\");",
6729                    getLLVMStyleWithColumns(25)));
6730 }
6731 
6732 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
6733   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
6734   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
6735   EXPECT_EQ("\"test\"\n"
6736             "\"\\n\"",
6737             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
6738   EXPECT_EQ("\"tes\\\\\"\n"
6739             "\"n\"",
6740             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
6741   EXPECT_EQ("\"\\\\\\\\\"\n"
6742             "\"\\n\"",
6743             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
6744   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
6745   EXPECT_EQ("\"\\uff01\"\n"
6746             "\"test\"",
6747             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
6748   EXPECT_EQ("\"\\Uff01ff02\"",
6749             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
6750   EXPECT_EQ("\"\\x000000000001\"\n"
6751             "\"next\"",
6752             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
6753   EXPECT_EQ("\"\\x000000000001next\"",
6754             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
6755   EXPECT_EQ("\"\\x000000000001\"",
6756             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
6757   EXPECT_EQ("\"test\"\n"
6758             "\"\\000000\"\n"
6759             "\"000001\"",
6760             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
6761   EXPECT_EQ("\"test\\000\"\n"
6762             "\"00000000\"\n"
6763             "\"1\"",
6764             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
6765 }
6766 
6767 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
6768   verifyFormat("void f() {\n"
6769                "  return g() {}\n"
6770                "  void h() {}");
6771   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
6772                "g();\n"
6773                "}");
6774 }
6775 
6776 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
6777   verifyFormat(
6778       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
6779 }
6780 
6781 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
6782   verifyFormat("class X {\n"
6783                "  void f() {\n"
6784                "  }\n"
6785                "};",
6786                getLLVMStyleWithColumns(12));
6787 }
6788 
6789 TEST_F(FormatTest, ConfigurableIndentWidth) {
6790   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
6791   EightIndent.IndentWidth = 8;
6792   EightIndent.ContinuationIndentWidth = 8;
6793   verifyFormat("void f() {\n"
6794                "        someFunction();\n"
6795                "        if (true) {\n"
6796                "                f();\n"
6797                "        }\n"
6798                "}",
6799                EightIndent);
6800   verifyFormat("class X {\n"
6801                "        void f() {\n"
6802                "        }\n"
6803                "};",
6804                EightIndent);
6805   verifyFormat("int x[] = {\n"
6806                "        call(),\n"
6807                "        call()};",
6808                EightIndent);
6809 }
6810 
6811 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
6812   verifyFormat("double\n"
6813                "f();",
6814                getLLVMStyleWithColumns(8));
6815 }
6816 
6817 TEST_F(FormatTest, ConfigurableUseOfTab) {
6818   FormatStyle Tab = getLLVMStyleWithColumns(42);
6819   Tab.IndentWidth = 8;
6820   Tab.UseTab = FormatStyle::UT_Always;
6821   Tab.AlignEscapedNewlinesLeft = true;
6822 
6823   EXPECT_EQ("if (aaaaaaaa && // q\n"
6824             "    bb)\t\t// w\n"
6825             "\t;",
6826             format("if (aaaaaaaa &&// q\n"
6827                    "bb)// w\n"
6828                    ";",
6829                    Tab));
6830   EXPECT_EQ("if (aaa && bbb) // w\n"
6831             "\t;",
6832             format("if(aaa&&bbb)// w\n"
6833                    ";",
6834                    Tab));
6835 
6836   verifyFormat("class X {\n"
6837                "\tvoid f() {\n"
6838                "\t\tsomeFunction(parameter1,\n"
6839                "\t\t\t     parameter2);\n"
6840                "\t}\n"
6841                "};",
6842                Tab);
6843   verifyFormat("#define A                        \\\n"
6844                "\tvoid f() {               \\\n"
6845                "\t\tsomeFunction(    \\\n"
6846                "\t\t    parameter1,  \\\n"
6847                "\t\t    parameter2); \\\n"
6848                "\t}",
6849                Tab);
6850 
6851   Tab.TabWidth = 4;
6852   Tab.IndentWidth = 8;
6853   verifyFormat("class TabWidth4Indent8 {\n"
6854                "\t\tvoid f() {\n"
6855                "\t\t\t\tsomeFunction(parameter1,\n"
6856                "\t\t\t\t\t\t\t parameter2);\n"
6857                "\t\t}\n"
6858                "};",
6859                Tab);
6860 
6861   Tab.TabWidth = 4;
6862   Tab.IndentWidth = 4;
6863   verifyFormat("class TabWidth4Indent4 {\n"
6864                "\tvoid f() {\n"
6865                "\t\tsomeFunction(parameter1,\n"
6866                "\t\t\t\t\t parameter2);\n"
6867                "\t}\n"
6868                "};",
6869                Tab);
6870 
6871   Tab.TabWidth = 8;
6872   Tab.IndentWidth = 4;
6873   verifyFormat("class TabWidth8Indent4 {\n"
6874                "    void f() {\n"
6875                "\tsomeFunction(parameter1,\n"
6876                "\t\t     parameter2);\n"
6877                "    }\n"
6878                "};",
6879                Tab);
6880 
6881   Tab.TabWidth = 8;
6882   Tab.IndentWidth = 8;
6883   EXPECT_EQ("/*\n"
6884             "\t      a\t\tcomment\n"
6885             "\t      in multiple lines\n"
6886             "       */",
6887             format("   /*\t \t \n"
6888                    " \t \t a\t\tcomment\t \t\n"
6889                    " \t \t in multiple lines\t\n"
6890                    " \t  */",
6891                    Tab));
6892 
6893   Tab.UseTab = FormatStyle::UT_ForIndentation;
6894   verifyFormat("{\n"
6895                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6896                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6897                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6898                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6899                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6900                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6901                "};",
6902                Tab);
6903   verifyFormat("enum AA {\n"
6904                "\ta1, // Force multiple lines\n"
6905                "\ta2,\n"
6906                "\ta3\n"
6907                "};",
6908                Tab);
6909   EXPECT_EQ("if (aaaaaaaa && // q\n"
6910             "    bb)         // w\n"
6911             "\t;",
6912             format("if (aaaaaaaa &&// q\n"
6913                    "bb)// w\n"
6914                    ";",
6915                    Tab));
6916   verifyFormat("class X {\n"
6917                "\tvoid f() {\n"
6918                "\t\tsomeFunction(parameter1,\n"
6919                "\t\t             parameter2);\n"
6920                "\t}\n"
6921                "};",
6922                Tab);
6923   verifyFormat("{\n"
6924                "\tQ(\n"
6925                "\t    {\n"
6926                "\t\t    int a;\n"
6927                "\t\t    someFunction(aaaaaaaa,\n"
6928                "\t\t                 bbbbbbb);\n"
6929                "\t    },\n"
6930                "\t    p);\n"
6931                "}",
6932                Tab);
6933   EXPECT_EQ("{\n"
6934             "\t/* aaaa\n"
6935             "\t   bbbb */\n"
6936             "}",
6937             format("{\n"
6938                    "/* aaaa\n"
6939                    "   bbbb */\n"
6940                    "}",
6941                    Tab));
6942   EXPECT_EQ("{\n"
6943             "\t/*\n"
6944             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6945             "\t  bbbbbbbbbbbbb\n"
6946             "\t*/\n"
6947             "}",
6948             format("{\n"
6949                    "/*\n"
6950                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6951                    "*/\n"
6952                    "}",
6953                    Tab));
6954   EXPECT_EQ("{\n"
6955             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6956             "\t// bbbbbbbbbbbbb\n"
6957             "}",
6958             format("{\n"
6959                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6960                    "}",
6961                    Tab));
6962   EXPECT_EQ("{\n"
6963             "\t/*\n"
6964             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6965             "\t  bbbbbbbbbbbbb\n"
6966             "\t*/\n"
6967             "}",
6968             format("{\n"
6969                    "\t/*\n"
6970                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6971                    "\t*/\n"
6972                    "}",
6973                    Tab));
6974   EXPECT_EQ("{\n"
6975             "\t/*\n"
6976             "\n"
6977             "\t*/\n"
6978             "}",
6979             format("{\n"
6980                    "\t/*\n"
6981                    "\n"
6982                    "\t*/\n"
6983                    "}",
6984                    Tab));
6985   EXPECT_EQ("{\n"
6986             "\t/*\n"
6987             " asdf\n"
6988             "\t*/\n"
6989             "}",
6990             format("{\n"
6991                    "\t/*\n"
6992                    " asdf\n"
6993                    "\t*/\n"
6994                    "}",
6995                    Tab));
6996 
6997   Tab.UseTab = FormatStyle::UT_Never;
6998   EXPECT_EQ("/*\n"
6999             "              a\t\tcomment\n"
7000             "              in multiple lines\n"
7001             "       */",
7002             format("   /*\t \t \n"
7003                    " \t \t a\t\tcomment\t \t\n"
7004                    " \t \t in multiple lines\t\n"
7005                    " \t  */",
7006                    Tab));
7007   EXPECT_EQ("/* some\n"
7008             "   comment */",
7009             format(" \t \t /* some\n"
7010                    " \t \t    comment */",
7011                    Tab));
7012   EXPECT_EQ("int a; /* some\n"
7013             "   comment */",
7014             format(" \t \t int a; /* some\n"
7015                    " \t \t    comment */",
7016                    Tab));
7017 
7018   EXPECT_EQ("int a; /* some\n"
7019             "comment */",
7020             format(" \t \t int\ta; /* some\n"
7021                    " \t \t    comment */",
7022                    Tab));
7023   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7024             "    comment */",
7025             format(" \t \t f(\"\t\t\"); /* some\n"
7026                    " \t \t    comment */",
7027                    Tab));
7028   EXPECT_EQ("{\n"
7029             "  /*\n"
7030             "   * Comment\n"
7031             "   */\n"
7032             "  int i;\n"
7033             "}",
7034             format("{\n"
7035                    "\t/*\n"
7036                    "\t * Comment\n"
7037                    "\t */\n"
7038                    "\t int i;\n"
7039                    "}"));
7040 
7041   Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
7042   Tab.TabWidth = 8;
7043   Tab.IndentWidth = 8;
7044   EXPECT_EQ("if (aaaaaaaa && // q\n"
7045             "    bb)         // w\n"
7046             "\t;",
7047             format("if (aaaaaaaa &&// q\n"
7048                    "bb)// w\n"
7049                    ";",
7050                    Tab));
7051   EXPECT_EQ("if (aaa && bbb) // w\n"
7052             "\t;",
7053             format("if(aaa&&bbb)// w\n"
7054                    ";",
7055                    Tab));
7056   verifyFormat("class X {\n"
7057                "\tvoid f() {\n"
7058                "\t\tsomeFunction(parameter1,\n"
7059                "\t\t\t     parameter2);\n"
7060                "\t}\n"
7061                "};",
7062                Tab);
7063   verifyFormat("#define A                        \\\n"
7064                "\tvoid f() {               \\\n"
7065                "\t\tsomeFunction(    \\\n"
7066                "\t\t    parameter1,  \\\n"
7067                "\t\t    parameter2); \\\n"
7068                "\t}",
7069                Tab);
7070   Tab.TabWidth = 4;
7071   Tab.IndentWidth = 8;
7072   verifyFormat("class TabWidth4Indent8 {\n"
7073                "\t\tvoid f() {\n"
7074                "\t\t\t\tsomeFunction(parameter1,\n"
7075                "\t\t\t\t\t\t\t parameter2);\n"
7076                "\t\t}\n"
7077                "};",
7078                Tab);
7079   Tab.TabWidth = 4;
7080   Tab.IndentWidth = 4;
7081   verifyFormat("class TabWidth4Indent4 {\n"
7082                "\tvoid f() {\n"
7083                "\t\tsomeFunction(parameter1,\n"
7084                "\t\t\t\t\t parameter2);\n"
7085                "\t}\n"
7086                "};",
7087                Tab);
7088   Tab.TabWidth = 8;
7089   Tab.IndentWidth = 4;
7090   verifyFormat("class TabWidth8Indent4 {\n"
7091                "    void f() {\n"
7092                "\tsomeFunction(parameter1,\n"
7093                "\t\t     parameter2);\n"
7094                "    }\n"
7095                "};",
7096                Tab);
7097   Tab.TabWidth = 8;
7098   Tab.IndentWidth = 8;
7099   EXPECT_EQ("/*\n"
7100             "\t      a\t\tcomment\n"
7101             "\t      in multiple lines\n"
7102             "       */",
7103             format("   /*\t \t \n"
7104                    " \t \t a\t\tcomment\t \t\n"
7105                    " \t \t in multiple lines\t\n"
7106                    " \t  */",
7107                    Tab));
7108   verifyFormat("{\n"
7109                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7110                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7111                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7112                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7113                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7114                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7115                "};",
7116                Tab);
7117   verifyFormat("enum AA {\n"
7118                "\ta1, // Force multiple lines\n"
7119                "\ta2,\n"
7120                "\ta3\n"
7121                "};",
7122                Tab);
7123   EXPECT_EQ("if (aaaaaaaa && // q\n"
7124             "    bb)         // w\n"
7125             "\t;",
7126             format("if (aaaaaaaa &&// q\n"
7127                    "bb)// w\n"
7128                    ";",
7129                    Tab));
7130   verifyFormat("class X {\n"
7131                "\tvoid f() {\n"
7132                "\t\tsomeFunction(parameter1,\n"
7133                "\t\t\t     parameter2);\n"
7134                "\t}\n"
7135                "};",
7136                Tab);
7137   verifyFormat("{\n"
7138                "\tQ(\n"
7139                "\t    {\n"
7140                "\t\t    int a;\n"
7141                "\t\t    someFunction(aaaaaaaa,\n"
7142                "\t\t\t\t bbbbbbb);\n"
7143                "\t    },\n"
7144                "\t    p);\n"
7145                "}",
7146                Tab);
7147   EXPECT_EQ("{\n"
7148             "\t/* aaaa\n"
7149             "\t   bbbb */\n"
7150             "}",
7151             format("{\n"
7152                    "/* aaaa\n"
7153                    "   bbbb */\n"
7154                    "}",
7155                    Tab));
7156   EXPECT_EQ("{\n"
7157             "\t/*\n"
7158             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7159             "\t  bbbbbbbbbbbbb\n"
7160             "\t*/\n"
7161             "}",
7162             format("{\n"
7163                    "/*\n"
7164                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7165                    "*/\n"
7166                    "}",
7167                    Tab));
7168   EXPECT_EQ("{\n"
7169             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7170             "\t// bbbbbbbbbbbbb\n"
7171             "}",
7172             format("{\n"
7173                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7174                    "}",
7175                    Tab));
7176   EXPECT_EQ("{\n"
7177             "\t/*\n"
7178             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7179             "\t  bbbbbbbbbbbbb\n"
7180             "\t*/\n"
7181             "}",
7182             format("{\n"
7183                    "\t/*\n"
7184                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7185                    "\t*/\n"
7186                    "}",
7187                    Tab));
7188   EXPECT_EQ("{\n"
7189             "\t/*\n"
7190             "\n"
7191             "\t*/\n"
7192             "}",
7193             format("{\n"
7194                    "\t/*\n"
7195                    "\n"
7196                    "\t*/\n"
7197                    "}",
7198                    Tab));
7199   EXPECT_EQ("{\n"
7200             "\t/*\n"
7201             " asdf\n"
7202             "\t*/\n"
7203             "}",
7204             format("{\n"
7205                    "\t/*\n"
7206                    " asdf\n"
7207                    "\t*/\n"
7208                    "}",
7209                    Tab));
7210   EXPECT_EQ("/*\n"
7211             "\t      a\t\tcomment\n"
7212             "\t      in multiple lines\n"
7213             "       */",
7214             format("   /*\t \t \n"
7215                    " \t \t a\t\tcomment\t \t\n"
7216                    " \t \t in multiple lines\t\n"
7217                    " \t  */",
7218                    Tab));
7219   EXPECT_EQ("/* some\n"
7220             "   comment */",
7221             format(" \t \t /* some\n"
7222                    " \t \t    comment */",
7223                    Tab));
7224   EXPECT_EQ("int a; /* some\n"
7225             "   comment */",
7226             format(" \t \t int a; /* some\n"
7227                    " \t \t    comment */",
7228                    Tab));
7229   EXPECT_EQ("int a; /* some\n"
7230             "comment */",
7231             format(" \t \t int\ta; /* some\n"
7232                    " \t \t    comment */",
7233                    Tab));
7234   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7235             "    comment */",
7236             format(" \t \t f(\"\t\t\"); /* some\n"
7237                    " \t \t    comment */",
7238                    Tab));
7239   EXPECT_EQ("{\n"
7240             "  /*\n"
7241             "   * Comment\n"
7242             "   */\n"
7243             "  int i;\n"
7244             "}",
7245             format("{\n"
7246                    "\t/*\n"
7247                    "\t * Comment\n"
7248                    "\t */\n"
7249                    "\t int i;\n"
7250                    "}"));
7251   Tab.AlignConsecutiveAssignments = true;
7252   Tab.AlignConsecutiveDeclarations = true;
7253   Tab.TabWidth = 4;
7254   Tab.IndentWidth = 4;
7255   verifyFormat("class Assign {\n"
7256                "\tvoid f() {\n"
7257                "\t\tint         x      = 123;\n"
7258                "\t\tint         random = 4;\n"
7259                "\t\tstd::string alphabet =\n"
7260                "\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
7261                "\t}\n"
7262                "};",
7263                Tab);
7264 }
7265 
7266 TEST_F(FormatTest, CalculatesOriginalColumn) {
7267   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7268             "q\"; /* some\n"
7269             "       comment */",
7270             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7271                    "q\"; /* some\n"
7272                    "       comment */",
7273                    getLLVMStyle()));
7274   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7275             "/* some\n"
7276             "   comment */",
7277             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7278                    " /* some\n"
7279                    "    comment */",
7280                    getLLVMStyle()));
7281   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7282             "qqq\n"
7283             "/* some\n"
7284             "   comment */",
7285             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7286                    "qqq\n"
7287                    " /* some\n"
7288                    "    comment */",
7289                    getLLVMStyle()));
7290   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7291             "wwww; /* some\n"
7292             "         comment */",
7293             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7294                    "wwww; /* some\n"
7295                    "         comment */",
7296                    getLLVMStyle()));
7297 }
7298 
7299 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
7300   FormatStyle NoSpace = getLLVMStyle();
7301   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
7302 
7303   verifyFormat("while(true)\n"
7304                "  continue;",
7305                NoSpace);
7306   verifyFormat("for(;;)\n"
7307                "  continue;",
7308                NoSpace);
7309   verifyFormat("if(true)\n"
7310                "  f();\n"
7311                "else if(true)\n"
7312                "  f();",
7313                NoSpace);
7314   verifyFormat("do {\n"
7315                "  do_something();\n"
7316                "} while(something());",
7317                NoSpace);
7318   verifyFormat("switch(x) {\n"
7319                "default:\n"
7320                "  break;\n"
7321                "}",
7322                NoSpace);
7323   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
7324   verifyFormat("size_t x = sizeof(x);", NoSpace);
7325   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
7326   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
7327   verifyFormat("alignas(128) char a[128];", NoSpace);
7328   verifyFormat("size_t x = alignof(MyType);", NoSpace);
7329   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
7330   verifyFormat("int f() throw(Deprecated);", NoSpace);
7331   verifyFormat("typedef void (*cb)(int);", NoSpace);
7332   verifyFormat("T A::operator()();", NoSpace);
7333   verifyFormat("X A::operator++(T);", NoSpace);
7334 
7335   FormatStyle Space = getLLVMStyle();
7336   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
7337 
7338   verifyFormat("int f ();", Space);
7339   verifyFormat("void f (int a, T b) {\n"
7340                "  while (true)\n"
7341                "    continue;\n"
7342                "}",
7343                Space);
7344   verifyFormat("if (true)\n"
7345                "  f ();\n"
7346                "else if (true)\n"
7347                "  f ();",
7348                Space);
7349   verifyFormat("do {\n"
7350                "  do_something ();\n"
7351                "} while (something ());",
7352                Space);
7353   verifyFormat("switch (x) {\n"
7354                "default:\n"
7355                "  break;\n"
7356                "}",
7357                Space);
7358   verifyFormat("A::A () : a (1) {}", Space);
7359   verifyFormat("void f () __attribute__ ((asdf));", Space);
7360   verifyFormat("*(&a + 1);\n"
7361                "&((&a)[1]);\n"
7362                "a[(b + c) * d];\n"
7363                "(((a + 1) * 2) + 3) * 4;",
7364                Space);
7365   verifyFormat("#define A(x) x", Space);
7366   verifyFormat("#define A (x) x", Space);
7367   verifyFormat("#if defined(x)\n"
7368                "#endif",
7369                Space);
7370   verifyFormat("auto i = std::make_unique<int> (5);", Space);
7371   verifyFormat("size_t x = sizeof (x);", Space);
7372   verifyFormat("auto f (int x) -> decltype (x);", Space);
7373   verifyFormat("int f (T x) noexcept (x.create ());", Space);
7374   verifyFormat("alignas (128) char a[128];", Space);
7375   verifyFormat("size_t x = alignof (MyType);", Space);
7376   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
7377   verifyFormat("int f () throw (Deprecated);", Space);
7378   verifyFormat("typedef void (*cb) (int);", Space);
7379   verifyFormat("T A::operator() ();", Space);
7380   verifyFormat("X A::operator++ (T);", Space);
7381 }
7382 
7383 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
7384   FormatStyle Spaces = getLLVMStyle();
7385 
7386   Spaces.SpacesInParentheses = true;
7387   verifyFormat("call( x, y, z );", Spaces);
7388   verifyFormat("call();", Spaces);
7389   verifyFormat("std::function<void( int, int )> callback;", Spaces);
7390   verifyFormat("void inFunction() { std::function<void( int, int )> fct; }",
7391                Spaces);
7392   verifyFormat("while ( (bool)1 )\n"
7393                "  continue;",
7394                Spaces);
7395   verifyFormat("for ( ;; )\n"
7396                "  continue;",
7397                Spaces);
7398   verifyFormat("if ( true )\n"
7399                "  f();\n"
7400                "else if ( true )\n"
7401                "  f();",
7402                Spaces);
7403   verifyFormat("do {\n"
7404                "  do_something( (int)i );\n"
7405                "} while ( something() );",
7406                Spaces);
7407   verifyFormat("switch ( x ) {\n"
7408                "default:\n"
7409                "  break;\n"
7410                "}",
7411                Spaces);
7412 
7413   Spaces.SpacesInParentheses = false;
7414   Spaces.SpacesInCStyleCastParentheses = true;
7415   verifyFormat("Type *A = ( Type * )P;", Spaces);
7416   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
7417   verifyFormat("x = ( int32 )y;", Spaces);
7418   verifyFormat("int a = ( int )(2.0f);", Spaces);
7419   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
7420   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
7421   verifyFormat("#define x (( int )-1)", Spaces);
7422 
7423   // Run the first set of tests again with:
7424   Spaces.SpacesInParentheses = false;
7425   Spaces.SpaceInEmptyParentheses = true;
7426   Spaces.SpacesInCStyleCastParentheses = true;
7427   verifyFormat("call(x, y, z);", Spaces);
7428   verifyFormat("call( );", Spaces);
7429   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7430   verifyFormat("while (( bool )1)\n"
7431                "  continue;",
7432                Spaces);
7433   verifyFormat("for (;;)\n"
7434                "  continue;",
7435                Spaces);
7436   verifyFormat("if (true)\n"
7437                "  f( );\n"
7438                "else if (true)\n"
7439                "  f( );",
7440                Spaces);
7441   verifyFormat("do {\n"
7442                "  do_something(( int )i);\n"
7443                "} while (something( ));",
7444                Spaces);
7445   verifyFormat("switch (x) {\n"
7446                "default:\n"
7447                "  break;\n"
7448                "}",
7449                Spaces);
7450 
7451   // Run the first set of tests again with:
7452   Spaces.SpaceAfterCStyleCast = true;
7453   verifyFormat("call(x, y, z);", Spaces);
7454   verifyFormat("call( );", Spaces);
7455   verifyFormat("std::function<void(int, int)> callback;", Spaces);
7456   verifyFormat("while (( bool ) 1)\n"
7457                "  continue;",
7458                Spaces);
7459   verifyFormat("for (;;)\n"
7460                "  continue;",
7461                Spaces);
7462   verifyFormat("if (true)\n"
7463                "  f( );\n"
7464                "else if (true)\n"
7465                "  f( );",
7466                Spaces);
7467   verifyFormat("do {\n"
7468                "  do_something(( int ) i);\n"
7469                "} while (something( ));",
7470                Spaces);
7471   verifyFormat("switch (x) {\n"
7472                "default:\n"
7473                "  break;\n"
7474                "}",
7475                Spaces);
7476 
7477   // Run subset of tests again with:
7478   Spaces.SpacesInCStyleCastParentheses = false;
7479   Spaces.SpaceAfterCStyleCast = true;
7480   verifyFormat("while ((bool) 1)\n"
7481                "  continue;",
7482                Spaces);
7483   verifyFormat("do {\n"
7484                "  do_something((int) i);\n"
7485                "} while (something( ));",
7486                Spaces);
7487 }
7488 
7489 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
7490   verifyFormat("int a[5];");
7491   verifyFormat("a[3] += 42;");
7492 
7493   FormatStyle Spaces = getLLVMStyle();
7494   Spaces.SpacesInSquareBrackets = true;
7495   // Lambdas unchanged.
7496   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
7497   verifyFormat("return [i, args...] {};", Spaces);
7498 
7499   // Not lambdas.
7500   verifyFormat("int a[ 5 ];", Spaces);
7501   verifyFormat("a[ 3 ] += 42;", Spaces);
7502   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
7503   verifyFormat("double &operator[](int i) { return 0; }\n"
7504                "int i;",
7505                Spaces);
7506   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
7507   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
7508   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
7509 }
7510 
7511 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
7512   verifyFormat("int a = 5;");
7513   verifyFormat("a += 42;");
7514   verifyFormat("a or_eq 8;");
7515 
7516   FormatStyle Spaces = getLLVMStyle();
7517   Spaces.SpaceBeforeAssignmentOperators = false;
7518   verifyFormat("int a= 5;", Spaces);
7519   verifyFormat("a+= 42;", Spaces);
7520   verifyFormat("a or_eq 8;", Spaces);
7521 }
7522 
7523 TEST_F(FormatTest, AlignConsecutiveAssignments) {
7524   FormatStyle Alignment = getLLVMStyle();
7525   Alignment.AlignConsecutiveAssignments = false;
7526   verifyFormat("int a = 5;\n"
7527                "int oneTwoThree = 123;",
7528                Alignment);
7529   verifyFormat("int a = 5;\n"
7530                "int oneTwoThree = 123;",
7531                Alignment);
7532 
7533   Alignment.AlignConsecutiveAssignments = true;
7534   verifyFormat("int a           = 5;\n"
7535                "int oneTwoThree = 123;",
7536                Alignment);
7537   verifyFormat("int a           = method();\n"
7538                "int oneTwoThree = 133;",
7539                Alignment);
7540   verifyFormat("a &= 5;\n"
7541                "bcd *= 5;\n"
7542                "ghtyf += 5;\n"
7543                "dvfvdb -= 5;\n"
7544                "a /= 5;\n"
7545                "vdsvsv %= 5;\n"
7546                "sfdbddfbdfbb ^= 5;\n"
7547                "dvsdsv |= 5;\n"
7548                "int dsvvdvsdvvv = 123;",
7549                Alignment);
7550   verifyFormat("int i = 1, j = 10;\n"
7551                "something = 2000;",
7552                Alignment);
7553   verifyFormat("something = 2000;\n"
7554                "int i = 1, j = 10;\n",
7555                Alignment);
7556   verifyFormat("something = 2000;\n"
7557                "another   = 911;\n"
7558                "int i = 1, j = 10;\n"
7559                "oneMore = 1;\n"
7560                "i       = 2;",
7561                Alignment);
7562   verifyFormat("int a   = 5;\n"
7563                "int one = 1;\n"
7564                "method();\n"
7565                "int oneTwoThree = 123;\n"
7566                "int oneTwo      = 12;",
7567                Alignment);
7568   verifyFormat("int oneTwoThree = 123;\n"
7569                "int oneTwo      = 12;\n"
7570                "method();\n",
7571                Alignment);
7572   verifyFormat("int oneTwoThree = 123; // comment\n"
7573                "int oneTwo      = 12;  // comment",
7574                Alignment);
7575   EXPECT_EQ("int a = 5;\n"
7576             "\n"
7577             "int oneTwoThree = 123;",
7578             format("int a       = 5;\n"
7579                    "\n"
7580                    "int oneTwoThree= 123;",
7581                    Alignment));
7582   EXPECT_EQ("int a   = 5;\n"
7583             "int one = 1;\n"
7584             "\n"
7585             "int oneTwoThree = 123;",
7586             format("int a = 5;\n"
7587                    "int one = 1;\n"
7588                    "\n"
7589                    "int oneTwoThree = 123;",
7590                    Alignment));
7591   EXPECT_EQ("int a   = 5;\n"
7592             "int one = 1;\n"
7593             "\n"
7594             "int oneTwoThree = 123;\n"
7595             "int oneTwo      = 12;",
7596             format("int a = 5;\n"
7597                    "int one = 1;\n"
7598                    "\n"
7599                    "int oneTwoThree = 123;\n"
7600                    "int oneTwo = 12;",
7601                    Alignment));
7602   Alignment.AlignEscapedNewlinesLeft = true;
7603   verifyFormat("#define A               \\\n"
7604                "  int aaaa       = 12;  \\\n"
7605                "  int b          = 23;  \\\n"
7606                "  int ccc        = 234; \\\n"
7607                "  int dddddddddd = 2345;",
7608                Alignment);
7609   Alignment.AlignEscapedNewlinesLeft = false;
7610   verifyFormat("#define A                                                      "
7611                "                \\\n"
7612                "  int aaaa       = 12;                                         "
7613                "                \\\n"
7614                "  int b          = 23;                                         "
7615                "                \\\n"
7616                "  int ccc        = 234;                                        "
7617                "                \\\n"
7618                "  int dddddddddd = 2345;",
7619                Alignment);
7620   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
7621                "k = 4, int l = 5,\n"
7622                "                  int m = 6) {\n"
7623                "  int j      = 10;\n"
7624                "  otherThing = 1;\n"
7625                "}",
7626                Alignment);
7627   verifyFormat("void SomeFunction(int parameter = 0) {\n"
7628                "  int i   = 1;\n"
7629                "  int j   = 2;\n"
7630                "  int big = 10000;\n"
7631                "}",
7632                Alignment);
7633   verifyFormat("class C {\n"
7634                "public:\n"
7635                "  int i            = 1;\n"
7636                "  virtual void f() = 0;\n"
7637                "};",
7638                Alignment);
7639   verifyFormat("int i = 1;\n"
7640                "if (SomeType t = getSomething()) {\n"
7641                "}\n"
7642                "int j   = 2;\n"
7643                "int big = 10000;",
7644                Alignment);
7645   verifyFormat("int j = 7;\n"
7646                "for (int k = 0; k < N; ++k) {\n"
7647                "}\n"
7648                "int j   = 2;\n"
7649                "int big = 10000;\n"
7650                "}",
7651                Alignment);
7652   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7653   verifyFormat("int i = 1;\n"
7654                "LooooooooooongType loooooooooooooooooooooongVariable\n"
7655                "    = someLooooooooooooooooongFunction();\n"
7656                "int j = 2;",
7657                Alignment);
7658   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7659   verifyFormat("int i = 1;\n"
7660                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
7661                "    someLooooooooooooooooongFunction();\n"
7662                "int j = 2;",
7663                Alignment);
7664 
7665   verifyFormat("auto lambda = []() {\n"
7666                "  auto i = 0;\n"
7667                "  return 0;\n"
7668                "};\n"
7669                "int i  = 0;\n"
7670                "auto v = type{\n"
7671                "    i = 1,   //\n"
7672                "    (i = 2), //\n"
7673                "    i = 3    //\n"
7674                "};",
7675                Alignment);
7676 
7677   verifyFormat(
7678       "int i      = 1;\n"
7679       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
7680       "                          loooooooooooooooooooooongParameterB);\n"
7681       "int j      = 2;",
7682       Alignment);
7683 
7684   verifyFormat("template <typename T, typename T_0 = very_long_type_name_0,\n"
7685                "          typename B   = very_long_type_name_1,\n"
7686                "          typename T_2 = very_long_type_name_2>\n"
7687                "auto foo() {}\n",
7688                Alignment);
7689   verifyFormat("int a, b = 1;\n"
7690                "int c  = 2;\n"
7691                "int dd = 3;\n",
7692                Alignment);
7693   verifyFormat("int aa       = ((1 > 2) ? 3 : 4);\n"
7694                "float b[1][] = {{3.f}};\n",
7695                Alignment);
7696   verifyFormat("for (int i = 0; i < 1; i++)\n"
7697                "  int x = 1;\n",
7698                Alignment);
7699   verifyFormat("for (i = 0; i < 1; i++)\n"
7700                "  x = 1;\n"
7701                "y = 1;\n",
7702                Alignment);
7703 }
7704 
7705 TEST_F(FormatTest, AlignConsecutiveDeclarations) {
7706   FormatStyle Alignment = getLLVMStyle();
7707   Alignment.AlignConsecutiveDeclarations = false;
7708   verifyFormat("float const a = 5;\n"
7709                "int oneTwoThree = 123;",
7710                Alignment);
7711   verifyFormat("int a = 5;\n"
7712                "float const oneTwoThree = 123;",
7713                Alignment);
7714 
7715   Alignment.AlignConsecutiveDeclarations = true;
7716   verifyFormat("float const a = 5;\n"
7717                "int         oneTwoThree = 123;",
7718                Alignment);
7719   verifyFormat("int         a = method();\n"
7720                "float const oneTwoThree = 133;",
7721                Alignment);
7722   verifyFormat("int i = 1, j = 10;\n"
7723                "something = 2000;",
7724                Alignment);
7725   verifyFormat("something = 2000;\n"
7726                "int i = 1, j = 10;\n",
7727                Alignment);
7728   verifyFormat("float      something = 2000;\n"
7729                "double     another = 911;\n"
7730                "int        i = 1, j = 10;\n"
7731                "const int *oneMore = 1;\n"
7732                "unsigned   i = 2;",
7733                Alignment);
7734   verifyFormat("float a = 5;\n"
7735                "int   one = 1;\n"
7736                "method();\n"
7737                "const double       oneTwoThree = 123;\n"
7738                "const unsigned int oneTwo = 12;",
7739                Alignment);
7740   verifyFormat("int      oneTwoThree{0}; // comment\n"
7741                "unsigned oneTwo;         // comment",
7742                Alignment);
7743   EXPECT_EQ("float const a = 5;\n"
7744             "\n"
7745             "int oneTwoThree = 123;",
7746             format("float const   a = 5;\n"
7747                    "\n"
7748                    "int           oneTwoThree= 123;",
7749                    Alignment));
7750   EXPECT_EQ("float a = 5;\n"
7751             "int   one = 1;\n"
7752             "\n"
7753             "unsigned oneTwoThree = 123;",
7754             format("float    a = 5;\n"
7755                    "int      one = 1;\n"
7756                    "\n"
7757                    "unsigned oneTwoThree = 123;",
7758                    Alignment));
7759   EXPECT_EQ("float a = 5;\n"
7760             "int   one = 1;\n"
7761             "\n"
7762             "unsigned oneTwoThree = 123;\n"
7763             "int      oneTwo = 12;",
7764             format("float    a = 5;\n"
7765                    "int one = 1;\n"
7766                    "\n"
7767                    "unsigned oneTwoThree = 123;\n"
7768                    "int oneTwo = 12;",
7769                    Alignment));
7770   // Function prototype alignment
7771   verifyFormat("int    a();\n"
7772                "double b();",
7773                Alignment);
7774   verifyFormat("int    a(int x);\n"
7775                "double b();",
7776                Alignment);
7777   unsigned OldColumnLimit = Alignment.ColumnLimit;
7778   // We need to set ColumnLimit to zero, in order to stress nested alignments,
7779   // otherwise the function parameters will be re-flowed onto a single line.
7780   Alignment.ColumnLimit = 0;
7781   EXPECT_EQ("int    a(int   x,\n"
7782             "         float y);\n"
7783             "double b(int    x,\n"
7784             "         double y);",
7785             format("int a(int x,\n"
7786                    " float y);\n"
7787                    "double b(int x,\n"
7788                    " double y);",
7789                    Alignment));
7790   // This ensures that function parameters of function declarations are
7791   // correctly indented when their owning functions are indented.
7792   // The failure case here is for 'double y' to not be indented enough.
7793   EXPECT_EQ("double a(int x);\n"
7794             "int    b(int    y,\n"
7795             "         double z);",
7796             format("double a(int x);\n"
7797                    "int b(int y,\n"
7798                    " double z);",
7799                    Alignment));
7800   // Set ColumnLimit low so that we induce wrapping immediately after
7801   // the function name and opening paren.
7802   Alignment.ColumnLimit = 13;
7803   verifyFormat("int function(\n"
7804                "    int  x,\n"
7805                "    bool y);",
7806                Alignment);
7807   Alignment.ColumnLimit = OldColumnLimit;
7808   // Ensure function pointers don't screw up recursive alignment
7809   verifyFormat("int    a(int x, void (*fp)(int y));\n"
7810                "double b();",
7811                Alignment);
7812   Alignment.AlignConsecutiveAssignments = true;
7813   // Ensure recursive alignment is broken by function braces, so that the
7814   // "a = 1" does not align with subsequent assignments inside the function
7815   // body.
7816   verifyFormat("int func(int a = 1) {\n"
7817                "  int b  = 2;\n"
7818                "  int cc = 3;\n"
7819                "}",
7820                Alignment);
7821   verifyFormat("float      something = 2000;\n"
7822                "double     another   = 911;\n"
7823                "int        i = 1, j = 10;\n"
7824                "const int *oneMore = 1;\n"
7825                "unsigned   i       = 2;",
7826                Alignment);
7827   verifyFormat("int      oneTwoThree = {0}; // comment\n"
7828                "unsigned oneTwo      = 0;   // comment",
7829                Alignment);
7830   // Make sure that scope is correctly tracked, in the absence of braces
7831   verifyFormat("for (int i = 0; i < n; i++)\n"
7832                "  j = i;\n"
7833                "double x = 1;\n",
7834                Alignment);
7835   verifyFormat("if (int i = 0)\n"
7836                "  j = i;\n"
7837                "double x = 1;\n",
7838                Alignment);
7839   // Ensure operator[] and operator() are comprehended
7840   verifyFormat("struct test {\n"
7841                "  long long int foo();\n"
7842                "  int           operator[](int a);\n"
7843                "  double        bar();\n"
7844                "};\n",
7845                Alignment);
7846   verifyFormat("struct test {\n"
7847                "  long long int foo();\n"
7848                "  int           operator()(int a);\n"
7849                "  double        bar();\n"
7850                "};\n",
7851                Alignment);
7852   EXPECT_EQ("void SomeFunction(int parameter = 0) {\n"
7853             "  int const i   = 1;\n"
7854             "  int *     j   = 2;\n"
7855             "  int       big = 10000;\n"
7856             "\n"
7857             "  unsigned oneTwoThree = 123;\n"
7858             "  int      oneTwo      = 12;\n"
7859             "  method();\n"
7860             "  float k  = 2;\n"
7861             "  int   ll = 10000;\n"
7862             "}",
7863             format("void SomeFunction(int parameter= 0) {\n"
7864                    " int const  i= 1;\n"
7865                    "  int *j=2;\n"
7866                    " int big  =  10000;\n"
7867                    "\n"
7868                    "unsigned oneTwoThree  =123;\n"
7869                    "int oneTwo = 12;\n"
7870                    "  method();\n"
7871                    "float k= 2;\n"
7872                    "int ll=10000;\n"
7873                    "}",
7874                    Alignment));
7875   Alignment.AlignConsecutiveAssignments = false;
7876   Alignment.AlignEscapedNewlinesLeft = true;
7877   verifyFormat("#define A              \\\n"
7878                "  int       aaaa = 12; \\\n"
7879                "  float     b = 23;    \\\n"
7880                "  const int ccc = 234; \\\n"
7881                "  unsigned  dddddddddd = 2345;",
7882                Alignment);
7883   Alignment.AlignEscapedNewlinesLeft = false;
7884   Alignment.ColumnLimit = 30;
7885   verifyFormat("#define A                    \\\n"
7886                "  int       aaaa = 12;       \\\n"
7887                "  float     b = 23;          \\\n"
7888                "  const int ccc = 234;       \\\n"
7889                "  int       dddddddddd = 2345;",
7890                Alignment);
7891   Alignment.ColumnLimit = 80;
7892   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
7893                "k = 4, int l = 5,\n"
7894                "                  int m = 6) {\n"
7895                "  const int j = 10;\n"
7896                "  otherThing = 1;\n"
7897                "}",
7898                Alignment);
7899   verifyFormat("void SomeFunction(int parameter = 0) {\n"
7900                "  int const i = 1;\n"
7901                "  int *     j = 2;\n"
7902                "  int       big = 10000;\n"
7903                "}",
7904                Alignment);
7905   verifyFormat("class C {\n"
7906                "public:\n"
7907                "  int          i = 1;\n"
7908                "  virtual void f() = 0;\n"
7909                "};",
7910                Alignment);
7911   verifyFormat("float i = 1;\n"
7912                "if (SomeType t = getSomething()) {\n"
7913                "}\n"
7914                "const unsigned j = 2;\n"
7915                "int            big = 10000;",
7916                Alignment);
7917   verifyFormat("float j = 7;\n"
7918                "for (int k = 0; k < N; ++k) {\n"
7919                "}\n"
7920                "unsigned j = 2;\n"
7921                "int      big = 10000;\n"
7922                "}",
7923                Alignment);
7924   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
7925   verifyFormat("float              i = 1;\n"
7926                "LooooooooooongType loooooooooooooooooooooongVariable\n"
7927                "    = someLooooooooooooooooongFunction();\n"
7928                "int j = 2;",
7929                Alignment);
7930   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
7931   verifyFormat("int                i = 1;\n"
7932                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
7933                "    someLooooooooooooooooongFunction();\n"
7934                "int j = 2;",
7935                Alignment);
7936 
7937   Alignment.AlignConsecutiveAssignments = true;
7938   verifyFormat("auto lambda = []() {\n"
7939                "  auto  ii = 0;\n"
7940                "  float j  = 0;\n"
7941                "  return 0;\n"
7942                "};\n"
7943                "int   i  = 0;\n"
7944                "float i2 = 0;\n"
7945                "auto  v  = type{\n"
7946                "    i = 1,   //\n"
7947                "    (i = 2), //\n"
7948                "    i = 3    //\n"
7949                "};",
7950                Alignment);
7951   Alignment.AlignConsecutiveAssignments = false;
7952 
7953   verifyFormat(
7954       "int      i = 1;\n"
7955       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
7956       "                          loooooooooooooooooooooongParameterB);\n"
7957       "int      j = 2;",
7958       Alignment);
7959 
7960   // Test interactions with ColumnLimit and AlignConsecutiveAssignments:
7961   // We expect declarations and assignments to align, as long as it doesn't
7962   // exceed the column limit, starting a new alignment sequence whenever it
7963   // happens.
7964   Alignment.AlignConsecutiveAssignments = true;
7965   Alignment.ColumnLimit = 30;
7966   verifyFormat("float    ii              = 1;\n"
7967                "unsigned j               = 2;\n"
7968                "int someVerylongVariable = 1;\n"
7969                "AnotherLongType  ll = 123456;\n"
7970                "VeryVeryLongType k  = 2;\n"
7971                "int              myvar = 1;",
7972                Alignment);
7973   Alignment.ColumnLimit = 80;
7974   Alignment.AlignConsecutiveAssignments = false;
7975 
7976   verifyFormat(
7977       "template <typename LongTemplate, typename VeryLongTemplateTypeName,\n"
7978       "          typename LongType, typename B>\n"
7979       "auto foo() {}\n",
7980       Alignment);
7981   verifyFormat("float a, b = 1;\n"
7982                "int   c = 2;\n"
7983                "int   dd = 3;\n",
7984                Alignment);
7985   verifyFormat("int   aa = ((1 > 2) ? 3 : 4);\n"
7986                "float b[1][] = {{3.f}};\n",
7987                Alignment);
7988   Alignment.AlignConsecutiveAssignments = true;
7989   verifyFormat("float a, b = 1;\n"
7990                "int   c  = 2;\n"
7991                "int   dd = 3;\n",
7992                Alignment);
7993   verifyFormat("int   aa     = ((1 > 2) ? 3 : 4);\n"
7994                "float b[1][] = {{3.f}};\n",
7995                Alignment);
7996   Alignment.AlignConsecutiveAssignments = false;
7997 
7998   Alignment.ColumnLimit = 30;
7999   Alignment.BinPackParameters = false;
8000   verifyFormat("void foo(float     a,\n"
8001                "         float     b,\n"
8002                "         int       c,\n"
8003                "         uint32_t *d) {\n"
8004                "  int *  e = 0;\n"
8005                "  float  f = 0;\n"
8006                "  double g = 0;\n"
8007                "}\n"
8008                "void bar(ino_t     a,\n"
8009                "         int       b,\n"
8010                "         uint32_t *c,\n"
8011                "         bool      d) {}\n",
8012                Alignment);
8013   Alignment.BinPackParameters = true;
8014   Alignment.ColumnLimit = 80;
8015 }
8016 
8017 TEST_F(FormatTest, LinuxBraceBreaking) {
8018   FormatStyle LinuxBraceStyle = getLLVMStyle();
8019   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8020   verifyFormat("namespace a\n"
8021                "{\n"
8022                "class A\n"
8023                "{\n"
8024                "  void f()\n"
8025                "  {\n"
8026                "    if (true) {\n"
8027                "      a();\n"
8028                "      b();\n"
8029                "    } else {\n"
8030                "      a();\n"
8031                "    }\n"
8032                "  }\n"
8033                "  void g() { return; }\n"
8034                "};\n"
8035                "struct B {\n"
8036                "  int x;\n"
8037                "};\n"
8038                "}\n",
8039                LinuxBraceStyle);
8040   verifyFormat("enum X {\n"
8041                "  Y = 0,\n"
8042                "}\n",
8043                LinuxBraceStyle);
8044   verifyFormat("struct S {\n"
8045                "  int Type;\n"
8046                "  union {\n"
8047                "    int x;\n"
8048                "    double y;\n"
8049                "  } Value;\n"
8050                "  class C\n"
8051                "  {\n"
8052                "    MyFavoriteType Value;\n"
8053                "  } Class;\n"
8054                "}\n",
8055                LinuxBraceStyle);
8056 }
8057 
8058 TEST_F(FormatTest, MozillaBraceBreaking) {
8059   FormatStyle MozillaBraceStyle = getLLVMStyle();
8060   MozillaBraceStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
8061   MozillaBraceStyle.FixNamespaceComments = false;
8062   verifyFormat("namespace a {\n"
8063                "class A\n"
8064                "{\n"
8065                "  void f()\n"
8066                "  {\n"
8067                "    if (true) {\n"
8068                "      a();\n"
8069                "      b();\n"
8070                "    }\n"
8071                "  }\n"
8072                "  void g() { return; }\n"
8073                "};\n"
8074                "enum E\n"
8075                "{\n"
8076                "  A,\n"
8077                "  // foo\n"
8078                "  B,\n"
8079                "  C\n"
8080                "};\n"
8081                "struct B\n"
8082                "{\n"
8083                "  int x;\n"
8084                "};\n"
8085                "}\n",
8086                MozillaBraceStyle);
8087   verifyFormat("struct S\n"
8088                "{\n"
8089                "  int Type;\n"
8090                "  union\n"
8091                "  {\n"
8092                "    int x;\n"
8093                "    double y;\n"
8094                "  } Value;\n"
8095                "  class C\n"
8096                "  {\n"
8097                "    MyFavoriteType Value;\n"
8098                "  } Class;\n"
8099                "}\n",
8100                MozillaBraceStyle);
8101 }
8102 
8103 TEST_F(FormatTest, StroustrupBraceBreaking) {
8104   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8105   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8106   verifyFormat("namespace a {\n"
8107                "class A {\n"
8108                "  void f()\n"
8109                "  {\n"
8110                "    if (true) {\n"
8111                "      a();\n"
8112                "      b();\n"
8113                "    }\n"
8114                "  }\n"
8115                "  void g() { return; }\n"
8116                "};\n"
8117                "struct B {\n"
8118                "  int x;\n"
8119                "};\n"
8120                "} // namespace a\n",
8121                StroustrupBraceStyle);
8122 
8123   verifyFormat("void foo()\n"
8124                "{\n"
8125                "  if (a) {\n"
8126                "    a();\n"
8127                "  }\n"
8128                "  else {\n"
8129                "    b();\n"
8130                "  }\n"
8131                "}\n",
8132                StroustrupBraceStyle);
8133 
8134   verifyFormat("#ifdef _DEBUG\n"
8135                "int foo(int i = 0)\n"
8136                "#else\n"
8137                "int foo(int i = 5)\n"
8138                "#endif\n"
8139                "{\n"
8140                "  return i;\n"
8141                "}",
8142                StroustrupBraceStyle);
8143 
8144   verifyFormat("void foo() {}\n"
8145                "void bar()\n"
8146                "#ifdef _DEBUG\n"
8147                "{\n"
8148                "  foo();\n"
8149                "}\n"
8150                "#else\n"
8151                "{\n"
8152                "}\n"
8153                "#endif",
8154                StroustrupBraceStyle);
8155 
8156   verifyFormat("void foobar() { int i = 5; }\n"
8157                "#ifdef _DEBUG\n"
8158                "void bar() {}\n"
8159                "#else\n"
8160                "void bar() { foobar(); }\n"
8161                "#endif",
8162                StroustrupBraceStyle);
8163 }
8164 
8165 TEST_F(FormatTest, AllmanBraceBreaking) {
8166   FormatStyle AllmanBraceStyle = getLLVMStyle();
8167   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8168   verifyFormat("namespace a\n"
8169                "{\n"
8170                "class A\n"
8171                "{\n"
8172                "  void f()\n"
8173                "  {\n"
8174                "    if (true)\n"
8175                "    {\n"
8176                "      a();\n"
8177                "      b();\n"
8178                "    }\n"
8179                "  }\n"
8180                "  void g() { return; }\n"
8181                "};\n"
8182                "struct B\n"
8183                "{\n"
8184                "  int x;\n"
8185                "};\n"
8186                "}",
8187                AllmanBraceStyle);
8188 
8189   verifyFormat("void f()\n"
8190                "{\n"
8191                "  if (true)\n"
8192                "  {\n"
8193                "    a();\n"
8194                "  }\n"
8195                "  else if (false)\n"
8196                "  {\n"
8197                "    b();\n"
8198                "  }\n"
8199                "  else\n"
8200                "  {\n"
8201                "    c();\n"
8202                "  }\n"
8203                "}\n",
8204                AllmanBraceStyle);
8205 
8206   verifyFormat("void f()\n"
8207                "{\n"
8208                "  for (int i = 0; i < 10; ++i)\n"
8209                "  {\n"
8210                "    a();\n"
8211                "  }\n"
8212                "  while (false)\n"
8213                "  {\n"
8214                "    b();\n"
8215                "  }\n"
8216                "  do\n"
8217                "  {\n"
8218                "    c();\n"
8219                "  } while (false)\n"
8220                "}\n",
8221                AllmanBraceStyle);
8222 
8223   verifyFormat("void f(int a)\n"
8224                "{\n"
8225                "  switch (a)\n"
8226                "  {\n"
8227                "  case 0:\n"
8228                "    break;\n"
8229                "  case 1:\n"
8230                "  {\n"
8231                "    break;\n"
8232                "  }\n"
8233                "  case 2:\n"
8234                "  {\n"
8235                "  }\n"
8236                "  break;\n"
8237                "  default:\n"
8238                "    break;\n"
8239                "  }\n"
8240                "}\n",
8241                AllmanBraceStyle);
8242 
8243   verifyFormat("enum X\n"
8244                "{\n"
8245                "  Y = 0,\n"
8246                "}\n",
8247                AllmanBraceStyle);
8248   verifyFormat("enum X\n"
8249                "{\n"
8250                "  Y = 0\n"
8251                "}\n",
8252                AllmanBraceStyle);
8253 
8254   verifyFormat("@interface BSApplicationController ()\n"
8255                "{\n"
8256                "@private\n"
8257                "  id _extraIvar;\n"
8258                "}\n"
8259                "@end\n",
8260                AllmanBraceStyle);
8261 
8262   verifyFormat("#ifdef _DEBUG\n"
8263                "int foo(int i = 0)\n"
8264                "#else\n"
8265                "int foo(int i = 5)\n"
8266                "#endif\n"
8267                "{\n"
8268                "  return i;\n"
8269                "}",
8270                AllmanBraceStyle);
8271 
8272   verifyFormat("void foo() {}\n"
8273                "void bar()\n"
8274                "#ifdef _DEBUG\n"
8275                "{\n"
8276                "  foo();\n"
8277                "}\n"
8278                "#else\n"
8279                "{\n"
8280                "}\n"
8281                "#endif",
8282                AllmanBraceStyle);
8283 
8284   verifyFormat("void foobar() { int i = 5; }\n"
8285                "#ifdef _DEBUG\n"
8286                "void bar() {}\n"
8287                "#else\n"
8288                "void bar() { foobar(); }\n"
8289                "#endif",
8290                AllmanBraceStyle);
8291 
8292   // This shouldn't affect ObjC blocks..
8293   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8294                "  // ...\n"
8295                "  int i;\n"
8296                "}];",
8297                AllmanBraceStyle);
8298   verifyFormat("void (^block)(void) = ^{\n"
8299                "  // ...\n"
8300                "  int i;\n"
8301                "};",
8302                AllmanBraceStyle);
8303   // .. or dict literals.
8304   verifyFormat("void f()\n"
8305                "{\n"
8306                "  // ...\n"
8307                "  [object someMethod:@{@\"a\" : @\"b\"}];\n"
8308                "}",
8309                AllmanBraceStyle);
8310   verifyFormat("void f()\n"
8311                "{\n"
8312                "  // ...\n"
8313                "  [object someMethod:@{a : @\"b\"}];\n"
8314                "}",
8315                AllmanBraceStyle);
8316   verifyFormat("int f()\n"
8317                "{ // comment\n"
8318                "  return 42;\n"
8319                "}",
8320                AllmanBraceStyle);
8321 
8322   AllmanBraceStyle.ColumnLimit = 19;
8323   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8324   AllmanBraceStyle.ColumnLimit = 18;
8325   verifyFormat("void f()\n"
8326                "{\n"
8327                "  int i;\n"
8328                "}",
8329                AllmanBraceStyle);
8330   AllmanBraceStyle.ColumnLimit = 80;
8331 
8332   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8333   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8334   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8335   verifyFormat("void f(bool b)\n"
8336                "{\n"
8337                "  if (b)\n"
8338                "  {\n"
8339                "    return;\n"
8340                "  }\n"
8341                "}\n",
8342                BreakBeforeBraceShortIfs);
8343   verifyFormat("void f(bool b)\n"
8344                "{\n"
8345                "  if (b) return;\n"
8346                "}\n",
8347                BreakBeforeBraceShortIfs);
8348   verifyFormat("void f(bool b)\n"
8349                "{\n"
8350                "  while (b)\n"
8351                "  {\n"
8352                "    return;\n"
8353                "  }\n"
8354                "}\n",
8355                BreakBeforeBraceShortIfs);
8356 }
8357 
8358 TEST_F(FormatTest, GNUBraceBreaking) {
8359   FormatStyle GNUBraceStyle = getLLVMStyle();
8360   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8361   verifyFormat("namespace a\n"
8362                "{\n"
8363                "class A\n"
8364                "{\n"
8365                "  void f()\n"
8366                "  {\n"
8367                "    int a;\n"
8368                "    {\n"
8369                "      int b;\n"
8370                "    }\n"
8371                "    if (true)\n"
8372                "      {\n"
8373                "        a();\n"
8374                "        b();\n"
8375                "      }\n"
8376                "  }\n"
8377                "  void g() { return; }\n"
8378                "}\n"
8379                "}",
8380                GNUBraceStyle);
8381 
8382   verifyFormat("void f()\n"
8383                "{\n"
8384                "  if (true)\n"
8385                "    {\n"
8386                "      a();\n"
8387                "    }\n"
8388                "  else if (false)\n"
8389                "    {\n"
8390                "      b();\n"
8391                "    }\n"
8392                "  else\n"
8393                "    {\n"
8394                "      c();\n"
8395                "    }\n"
8396                "}\n",
8397                GNUBraceStyle);
8398 
8399   verifyFormat("void f()\n"
8400                "{\n"
8401                "  for (int i = 0; i < 10; ++i)\n"
8402                "    {\n"
8403                "      a();\n"
8404                "    }\n"
8405                "  while (false)\n"
8406                "    {\n"
8407                "      b();\n"
8408                "    }\n"
8409                "  do\n"
8410                "    {\n"
8411                "      c();\n"
8412                "    }\n"
8413                "  while (false);\n"
8414                "}\n",
8415                GNUBraceStyle);
8416 
8417   verifyFormat("void f(int a)\n"
8418                "{\n"
8419                "  switch (a)\n"
8420                "    {\n"
8421                "    case 0:\n"
8422                "      break;\n"
8423                "    case 1:\n"
8424                "      {\n"
8425                "        break;\n"
8426                "      }\n"
8427                "    case 2:\n"
8428                "      {\n"
8429                "      }\n"
8430                "      break;\n"
8431                "    default:\n"
8432                "      break;\n"
8433                "    }\n"
8434                "}\n",
8435                GNUBraceStyle);
8436 
8437   verifyFormat("enum X\n"
8438                "{\n"
8439                "  Y = 0,\n"
8440                "}\n",
8441                GNUBraceStyle);
8442 
8443   verifyFormat("@interface BSApplicationController ()\n"
8444                "{\n"
8445                "@private\n"
8446                "  id _extraIvar;\n"
8447                "}\n"
8448                "@end\n",
8449                GNUBraceStyle);
8450 
8451   verifyFormat("#ifdef _DEBUG\n"
8452                "int foo(int i = 0)\n"
8453                "#else\n"
8454                "int foo(int i = 5)\n"
8455                "#endif\n"
8456                "{\n"
8457                "  return i;\n"
8458                "}",
8459                GNUBraceStyle);
8460 
8461   verifyFormat("void foo() {}\n"
8462                "void bar()\n"
8463                "#ifdef _DEBUG\n"
8464                "{\n"
8465                "  foo();\n"
8466                "}\n"
8467                "#else\n"
8468                "{\n"
8469                "}\n"
8470                "#endif",
8471                GNUBraceStyle);
8472 
8473   verifyFormat("void foobar() { int i = 5; }\n"
8474                "#ifdef _DEBUG\n"
8475                "void bar() {}\n"
8476                "#else\n"
8477                "void bar() { foobar(); }\n"
8478                "#endif",
8479                GNUBraceStyle);
8480 }
8481 
8482 TEST_F(FormatTest, WebKitBraceBreaking) {
8483   FormatStyle WebKitBraceStyle = getLLVMStyle();
8484   WebKitBraceStyle.BreakBeforeBraces = FormatStyle::BS_WebKit;
8485   WebKitBraceStyle.FixNamespaceComments = false;
8486   verifyFormat("namespace a {\n"
8487                "class A {\n"
8488                "  void f()\n"
8489                "  {\n"
8490                "    if (true) {\n"
8491                "      a();\n"
8492                "      b();\n"
8493                "    }\n"
8494                "  }\n"
8495                "  void g() { return; }\n"
8496                "};\n"
8497                "enum E {\n"
8498                "  A,\n"
8499                "  // foo\n"
8500                "  B,\n"
8501                "  C\n"
8502                "};\n"
8503                "struct B {\n"
8504                "  int x;\n"
8505                "};\n"
8506                "}\n",
8507                WebKitBraceStyle);
8508   verifyFormat("struct S {\n"
8509                "  int Type;\n"
8510                "  union {\n"
8511                "    int x;\n"
8512                "    double y;\n"
8513                "  } Value;\n"
8514                "  class C {\n"
8515                "    MyFavoriteType Value;\n"
8516                "  } Class;\n"
8517                "};\n",
8518                WebKitBraceStyle);
8519 }
8520 
8521 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8522   verifyFormat("void f() {\n"
8523                "  try {\n"
8524                "  } catch (const Exception &e) {\n"
8525                "  }\n"
8526                "}\n",
8527                getLLVMStyle());
8528 }
8529 
8530 TEST_F(FormatTest, UnderstandsPragmas) {
8531   verifyFormat("#pragma omp reduction(| : var)");
8532   verifyFormat("#pragma omp reduction(+ : var)");
8533 
8534   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8535             "(including parentheses).",
8536             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8537                    "(including parentheses)."));
8538 }
8539 
8540 TEST_F(FormatTest, UnderstandPragmaOption) {
8541   verifyFormat("#pragma option -C -A");
8542 
8543   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
8544 }
8545 
8546 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8547   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8548   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
8549                                   << " differs from Style #0"
8550 
8551 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8552   SmallVector<FormatStyle, 3> Styles;
8553   Styles.resize(3);
8554 
8555   Styles[0] = getLLVMStyle();
8556   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8557   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8558   EXPECT_ALL_STYLES_EQUAL(Styles);
8559 
8560   Styles[0] = getGoogleStyle();
8561   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8562   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8563   EXPECT_ALL_STYLES_EQUAL(Styles);
8564 
8565   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8566   EXPECT_TRUE(
8567       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8568   EXPECT_TRUE(
8569       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8570   EXPECT_ALL_STYLES_EQUAL(Styles);
8571 
8572   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8573   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8574   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8575   EXPECT_ALL_STYLES_EQUAL(Styles);
8576 
8577   Styles[0] = getMozillaStyle();
8578   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8579   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8580   EXPECT_ALL_STYLES_EQUAL(Styles);
8581 
8582   Styles[0] = getWebKitStyle();
8583   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8584   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8585   EXPECT_ALL_STYLES_EQUAL(Styles);
8586 
8587   Styles[0] = getGNUStyle();
8588   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8589   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8590   EXPECT_ALL_STYLES_EQUAL(Styles);
8591 
8592   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8593 }
8594 
8595 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8596   SmallVector<FormatStyle, 8> Styles;
8597   Styles.resize(2);
8598 
8599   Styles[0] = getGoogleStyle();
8600   Styles[1] = getLLVMStyle();
8601   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8602   EXPECT_ALL_STYLES_EQUAL(Styles);
8603 
8604   Styles.resize(5);
8605   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8606   Styles[1] = getLLVMStyle();
8607   Styles[1].Language = FormatStyle::LK_JavaScript;
8608   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8609 
8610   Styles[2] = getLLVMStyle();
8611   Styles[2].Language = FormatStyle::LK_JavaScript;
8612   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8613                                   "BasedOnStyle: Google",
8614                                   &Styles[2])
8615                    .value());
8616 
8617   Styles[3] = getLLVMStyle();
8618   Styles[3].Language = FormatStyle::LK_JavaScript;
8619   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8620                                   "Language: JavaScript",
8621                                   &Styles[3])
8622                    .value());
8623 
8624   Styles[4] = getLLVMStyle();
8625   Styles[4].Language = FormatStyle::LK_JavaScript;
8626   EXPECT_EQ(0, parseConfiguration("---\n"
8627                                   "BasedOnStyle: LLVM\n"
8628                                   "IndentWidth: 123\n"
8629                                   "---\n"
8630                                   "BasedOnStyle: Google\n"
8631                                   "Language: JavaScript",
8632                                   &Styles[4])
8633                    .value());
8634   EXPECT_ALL_STYLES_EQUAL(Styles);
8635 }
8636 
8637 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
8638   Style.FIELD = false;                                                         \
8639   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
8640   EXPECT_TRUE(Style.FIELD);                                                    \
8641   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
8642   EXPECT_FALSE(Style.FIELD);
8643 
8644 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8645 
8646 #define CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, CONFIG_NAME)              \
8647   Style.STRUCT.FIELD = false;                                                  \
8648   EXPECT_EQ(0,                                                                 \
8649             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": true", &Style)   \
8650                 .value());                                                     \
8651   EXPECT_TRUE(Style.STRUCT.FIELD);                                             \
8652   EXPECT_EQ(0,                                                                 \
8653             parseConfiguration(#STRUCT ":\n  " CONFIG_NAME ": false", &Style)  \
8654                 .value());                                                     \
8655   EXPECT_FALSE(Style.STRUCT.FIELD);
8656 
8657 #define CHECK_PARSE_NESTED_BOOL(STRUCT, FIELD)                                 \
8658   CHECK_PARSE_NESTED_BOOL_FIELD(STRUCT, FIELD, #FIELD)
8659 
8660 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
8661   EXPECT_NE(VALUE, Style.FIELD);                                               \
8662   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
8663   EXPECT_EQ(VALUE, Style.FIELD)
8664 
8665 TEST_F(FormatTest, ParsesConfigurationBools) {
8666   FormatStyle Style = {};
8667   Style.Language = FormatStyle::LK_Cpp;
8668   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8669   CHECK_PARSE_BOOL(AlignOperands);
8670   CHECK_PARSE_BOOL(AlignTrailingComments);
8671   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
8672   CHECK_PARSE_BOOL(AlignConsecutiveDeclarations);
8673   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8674   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8675   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8676   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8677   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8678   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8679   CHECK_PARSE_BOOL(BinPackArguments);
8680   CHECK_PARSE_BOOL(BinPackParameters);
8681   CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
8682   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8683   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8684   CHECK_PARSE_BOOL(BreakStringLiterals);
8685   CHECK_PARSE_BOOL(BreakBeforeInheritanceComma)
8686   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8687   CHECK_PARSE_BOOL(DerivePointerAlignment);
8688   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8689   CHECK_PARSE_BOOL(DisableFormat);
8690   CHECK_PARSE_BOOL(IndentCaseLabels);
8691   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8692   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8693   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8694   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8695   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8696   CHECK_PARSE_BOOL(ReflowComments);
8697   CHECK_PARSE_BOOL(SortIncludes);
8698   CHECK_PARSE_BOOL(SpacesInParentheses);
8699   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8700   CHECK_PARSE_BOOL(SpacesInAngles);
8701   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8702   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8703   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8704   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8705   CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword);
8706   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8707 
8708   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterClass);
8709   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterControlStatement);
8710   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
8711   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
8712   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
8713   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
8714   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterStruct);
8715   CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterUnion);
8716   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeCatch);
8717   CHECK_PARSE_NESTED_BOOL(BraceWrapping, BeforeElse);
8718   CHECK_PARSE_NESTED_BOOL(BraceWrapping, IndentBraces);
8719 }
8720 
8721 #undef CHECK_PARSE_BOOL
8722 
8723 TEST_F(FormatTest, ParsesConfiguration) {
8724   FormatStyle Style = {};
8725   Style.Language = FormatStyle::LK_Cpp;
8726   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8727   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8728               ConstructorInitializerIndentWidth, 1234u);
8729   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8730   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8731   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8732   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8733               PenaltyBreakBeforeFirstCallParameter, 1234u);
8734   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8735   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8736               PenaltyReturnTypeOnItsOwnLine, 1234u);
8737   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8738               SpacesBeforeTrailingComments, 1234u);
8739   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8740   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8741   CHECK_PARSE("CommentPragmas: '// abc$'", CommentPragmas, "// abc$");
8742 
8743   Style.PointerAlignment = FormatStyle::PAS_Middle;
8744   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8745               FormatStyle::PAS_Left);
8746   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8747               FormatStyle::PAS_Right);
8748   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8749               FormatStyle::PAS_Middle);
8750   // For backward compatibility:
8751   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8752               FormatStyle::PAS_Left);
8753   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
8754               FormatStyle::PAS_Right);
8755   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
8756               FormatStyle::PAS_Middle);
8757 
8758   Style.Standard = FormatStyle::LS_Auto;
8759   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8760   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8761   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8762   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8763   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8764 
8765   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8766   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
8767               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
8768   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
8769               FormatStyle::BOS_None);
8770   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
8771               FormatStyle::BOS_All);
8772   // For backward compatibility:
8773   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
8774               FormatStyle::BOS_None);
8775   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
8776               FormatStyle::BOS_All);
8777 
8778   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
8779   CHECK_PARSE("AlignAfterOpenBracket: Align", AlignAfterOpenBracket,
8780               FormatStyle::BAS_Align);
8781   CHECK_PARSE("AlignAfterOpenBracket: DontAlign", AlignAfterOpenBracket,
8782               FormatStyle::BAS_DontAlign);
8783   CHECK_PARSE("AlignAfterOpenBracket: AlwaysBreak", AlignAfterOpenBracket,
8784               FormatStyle::BAS_AlwaysBreak);
8785   // For backward compatibility:
8786   CHECK_PARSE("AlignAfterOpenBracket: false", AlignAfterOpenBracket,
8787               FormatStyle::BAS_DontAlign);
8788   CHECK_PARSE("AlignAfterOpenBracket: true", AlignAfterOpenBracket,
8789               FormatStyle::BAS_Align);
8790 
8791   Style.UseTab = FormatStyle::UT_ForIndentation;
8792   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8793   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8794   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8795   CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
8796               FormatStyle::UT_ForContinuationAndIndentation);
8797   // For backward compatibility:
8798   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8799   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
8800 
8801   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8802   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
8803               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8804   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
8805               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
8806   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
8807               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
8808   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
8809               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8810   // For backward compatibility:
8811   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
8812               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8813   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
8814               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8815 
8816   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
8817   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
8818               FormatStyle::SBPO_Never);
8819   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
8820               FormatStyle::SBPO_Always);
8821   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
8822               FormatStyle::SBPO_ControlStatements);
8823   // For backward compatibility:
8824   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
8825               FormatStyle::SBPO_Never);
8826   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
8827               FormatStyle::SBPO_ControlStatements);
8828 
8829   Style.ColumnLimit = 123;
8830   FormatStyle BaseStyle = getLLVMStyle();
8831   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
8832   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
8833 
8834   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8835   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
8836               FormatStyle::BS_Attach);
8837   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
8838               FormatStyle::BS_Linux);
8839   CHECK_PARSE("BreakBeforeBraces: Mozilla", BreakBeforeBraces,
8840               FormatStyle::BS_Mozilla);
8841   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
8842               FormatStyle::BS_Stroustrup);
8843   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
8844               FormatStyle::BS_Allman);
8845   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
8846   CHECK_PARSE("BreakBeforeBraces: WebKit", BreakBeforeBraces,
8847               FormatStyle::BS_WebKit);
8848   CHECK_PARSE("BreakBeforeBraces: Custom", BreakBeforeBraces,
8849               FormatStyle::BS_Custom);
8850 
8851   Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
8852   CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
8853               FormatStyle::RTBS_None);
8854   CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
8855               FormatStyle::RTBS_All);
8856   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
8857               AlwaysBreakAfterReturnType, FormatStyle::RTBS_TopLevel);
8858   CHECK_PARSE("AlwaysBreakAfterReturnType: AllDefinitions",
8859               AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllDefinitions);
8860   CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevelDefinitions",
8861               AlwaysBreakAfterReturnType,
8862               FormatStyle::RTBS_TopLevelDefinitions);
8863 
8864   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
8865   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
8866               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_None);
8867   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: All",
8868               AlwaysBreakAfterDefinitionReturnType, FormatStyle::DRTBS_All);
8869   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: TopLevel",
8870               AlwaysBreakAfterDefinitionReturnType,
8871               FormatStyle::DRTBS_TopLevel);
8872 
8873   Style.NamespaceIndentation = FormatStyle::NI_All;
8874   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
8875               FormatStyle::NI_None);
8876   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
8877               FormatStyle::NI_Inner);
8878   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
8879               FormatStyle::NI_All);
8880 
8881   // FIXME: This is required because parsing a configuration simply overwrites
8882   // the first N elements of the list instead of resetting it.
8883   Style.ForEachMacros.clear();
8884   std::vector<std::string> BoostForeach;
8885   BoostForeach.push_back("BOOST_FOREACH");
8886   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
8887   std::vector<std::string> BoostAndQForeach;
8888   BoostAndQForeach.push_back("BOOST_FOREACH");
8889   BoostAndQForeach.push_back("Q_FOREACH");
8890   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
8891               BoostAndQForeach);
8892 
8893   Style.IncludeCategories.clear();
8894   std::vector<FormatStyle::IncludeCategory> ExpectedCategories = {{"abc/.*", 2},
8895                                                                   {".*", 1}};
8896   CHECK_PARSE("IncludeCategories:\n"
8897               "  - Regex: abc/.*\n"
8898               "    Priority: 2\n"
8899               "  - Regex: .*\n"
8900               "    Priority: 1",
8901               IncludeCategories, ExpectedCategories);
8902   CHECK_PARSE("IncludeIsMainRegex: 'abc$'", IncludeIsMainRegex, "abc$");
8903 }
8904 
8905 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
8906   FormatStyle Style = {};
8907   Style.Language = FormatStyle::LK_Cpp;
8908   CHECK_PARSE("Language: Cpp\n"
8909               "IndentWidth: 12",
8910               IndentWidth, 12u);
8911   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
8912                                "IndentWidth: 34",
8913                                &Style),
8914             ParseError::Unsuitable);
8915   EXPECT_EQ(12u, Style.IndentWidth);
8916   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8917   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8918 
8919   Style.Language = FormatStyle::LK_JavaScript;
8920   CHECK_PARSE("Language: JavaScript\n"
8921               "IndentWidth: 12",
8922               IndentWidth, 12u);
8923   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
8924   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
8925                                "IndentWidth: 34",
8926                                &Style),
8927             ParseError::Unsuitable);
8928   EXPECT_EQ(23u, Style.IndentWidth);
8929   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8930   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8931 
8932   CHECK_PARSE("BasedOnStyle: LLVM\n"
8933               "IndentWidth: 67",
8934               IndentWidth, 67u);
8935 
8936   CHECK_PARSE("---\n"
8937               "Language: JavaScript\n"
8938               "IndentWidth: 12\n"
8939               "---\n"
8940               "Language: Cpp\n"
8941               "IndentWidth: 34\n"
8942               "...\n",
8943               IndentWidth, 12u);
8944 
8945   Style.Language = FormatStyle::LK_Cpp;
8946   CHECK_PARSE("---\n"
8947               "Language: JavaScript\n"
8948               "IndentWidth: 12\n"
8949               "---\n"
8950               "Language: Cpp\n"
8951               "IndentWidth: 34\n"
8952               "...\n",
8953               IndentWidth, 34u);
8954   CHECK_PARSE("---\n"
8955               "IndentWidth: 78\n"
8956               "---\n"
8957               "Language: JavaScript\n"
8958               "IndentWidth: 56\n"
8959               "...\n",
8960               IndentWidth, 78u);
8961 
8962   Style.ColumnLimit = 123;
8963   Style.IndentWidth = 234;
8964   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
8965   Style.TabWidth = 345;
8966   EXPECT_FALSE(parseConfiguration("---\n"
8967                                   "IndentWidth: 456\n"
8968                                   "BreakBeforeBraces: Allman\n"
8969                                   "---\n"
8970                                   "Language: JavaScript\n"
8971                                   "IndentWidth: 111\n"
8972                                   "TabWidth: 111\n"
8973                                   "---\n"
8974                                   "Language: Cpp\n"
8975                                   "BreakBeforeBraces: Stroustrup\n"
8976                                   "TabWidth: 789\n"
8977                                   "...\n",
8978                                   &Style));
8979   EXPECT_EQ(123u, Style.ColumnLimit);
8980   EXPECT_EQ(456u, Style.IndentWidth);
8981   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
8982   EXPECT_EQ(789u, Style.TabWidth);
8983 
8984   EXPECT_EQ(parseConfiguration("---\n"
8985                                "Language: JavaScript\n"
8986                                "IndentWidth: 56\n"
8987                                "---\n"
8988                                "IndentWidth: 78\n"
8989                                "...\n",
8990                                &Style),
8991             ParseError::Error);
8992   EXPECT_EQ(parseConfiguration("---\n"
8993                                "Language: JavaScript\n"
8994                                "IndentWidth: 56\n"
8995                                "---\n"
8996                                "Language: JavaScript\n"
8997                                "IndentWidth: 78\n"
8998                                "...\n",
8999                                &Style),
9000             ParseError::Error);
9001 
9002   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9003 }
9004 
9005 #undef CHECK_PARSE
9006 
9007 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9008   FormatStyle Style = {};
9009   Style.Language = FormatStyle::LK_JavaScript;
9010   Style.BreakBeforeTernaryOperators = true;
9011   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9012   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9013 
9014   Style.BreakBeforeTernaryOperators = true;
9015   EXPECT_EQ(0, parseConfiguration("---\n"
9016                                   "BasedOnStyle: Google\n"
9017                                   "---\n"
9018                                   "Language: JavaScript\n"
9019                                   "IndentWidth: 76\n"
9020                                   "...\n",
9021                                   &Style)
9022                    .value());
9023   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9024   EXPECT_EQ(76u, Style.IndentWidth);
9025   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9026 }
9027 
9028 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9029   FormatStyle Style = getLLVMStyle();
9030   std::string YAML = configurationAsText(Style);
9031   FormatStyle ParsedStyle = {};
9032   ParsedStyle.Language = FormatStyle::LK_Cpp;
9033   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9034   EXPECT_EQ(Style, ParsedStyle);
9035 }
9036 
9037 TEST_F(FormatTest, WorksFor8bitEncodings) {
9038   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9039             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9040             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9041             "\"\xef\xee\xf0\xf3...\"",
9042             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9043                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9044                    "\xef\xee\xf0\xf3...\"",
9045                    getLLVMStyleWithColumns(12)));
9046 }
9047 
9048 TEST_F(FormatTest, HandlesUTF8BOM) {
9049   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9050   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9051             format("\xef\xbb\xbf#include <iostream>"));
9052   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9053             format("\xef\xbb\xbf\n#include <iostream>"));
9054 }
9055 
9056 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9057 #if !defined(_MSC_VER)
9058 
9059 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9060   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9061                getLLVMStyleWithColumns(35));
9062   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9063                getLLVMStyleWithColumns(31));
9064   verifyFormat("// Однажды в студёную зимнюю пору...",
9065                getLLVMStyleWithColumns(36));
9066   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9067   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9068                getLLVMStyleWithColumns(39));
9069   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9070                getLLVMStyleWithColumns(35));
9071 }
9072 
9073 TEST_F(FormatTest, SplitsUTF8Strings) {
9074   // Non-printable characters' width is currently considered to be the length in
9075   // bytes in UTF8. The characters can be displayed in very different manner
9076   // (zero-width, single width with a substitution glyph, expanded to their code
9077   // (e.g. "<8d>"), so there's no single correct way to handle them.
9078   EXPECT_EQ("\"aaaaÄ\"\n"
9079             "\"\xc2\x8d\";",
9080             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9081   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9082             "\"\xc2\x8d\";",
9083             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9084   EXPECT_EQ("\"Однажды, в \"\n"
9085             "\"студёную \"\n"
9086             "\"зимнюю \"\n"
9087             "\"пору,\"",
9088             format("\"Однажды, в студёную зимнюю пору,\"",
9089                    getLLVMStyleWithColumns(13)));
9090   EXPECT_EQ(
9091       "\"一 二 三 \"\n"
9092       "\"四 五六 \"\n"
9093       "\"七 八 九 \"\n"
9094       "\"十\"",
9095       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9096   EXPECT_EQ("\"一\t二 \"\n"
9097             "\"\t三 \"\n"
9098             "\"四 五\t六 \"\n"
9099             "\"\t七 \"\n"
9100             "\"八九十\tqq\"",
9101             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9102                    getLLVMStyleWithColumns(11)));
9103 
9104   // UTF8 character in an escape sequence.
9105   EXPECT_EQ("\"aaaaaa\"\n"
9106             "\"\\\xC2\x8D\"",
9107             format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10)));
9108 }
9109 
9110 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9111   EXPECT_EQ("const char *sssss =\n"
9112             "    \"一二三四五六七八\\\n"
9113             " 九 十\";",
9114             format("const char *sssss = \"一二三四五六七八\\\n"
9115                    " 九 十\";",
9116                    getLLVMStyleWithColumns(30)));
9117 }
9118 
9119 TEST_F(FormatTest, SplitsUTF8LineComments) {
9120   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9121             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9122   EXPECT_EQ("// Я из лесу\n"
9123             "// вышел; был\n"
9124             "// сильный\n"
9125             "// мороз.",
9126             format("// Я из лесу вышел; был сильный мороз.",
9127                    getLLVMStyleWithColumns(13)));
9128   EXPECT_EQ("// 一二三\n"
9129             "// 四五六七\n"
9130             "// 八  九\n"
9131             "// 十",
9132             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9133 }
9134 
9135 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9136   EXPECT_EQ("/* Гляжу,\n"
9137             " * поднимается\n"
9138             " * медленно в\n"
9139             " * гору\n"
9140             " * Лошадка,\n"
9141             " * везущая\n"
9142             " * хворосту\n"
9143             " * воз. */",
9144             format("/* Гляжу, поднимается медленно в гору\n"
9145                    " * Лошадка, везущая хворосту воз. */",
9146                    getLLVMStyleWithColumns(13)));
9147   EXPECT_EQ(
9148       "/* 一二三\n"
9149       " * 四五六七\n"
9150       " * 八  九\n"
9151       " * 十  */",
9152       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9153   EXPECT_EQ("/* �������� ��������\n"
9154             " * ��������\n"
9155             " * ������-�� */",
9156             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9157 }
9158 
9159 #endif // _MSC_VER
9160 
9161 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9162   FormatStyle Style = getLLVMStyle();
9163 
9164   Style.ConstructorInitializerIndentWidth = 4;
9165   verifyFormat(
9166       "SomeClass::Constructor()\n"
9167       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9168       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9169       Style);
9170 
9171   Style.ConstructorInitializerIndentWidth = 2;
9172   verifyFormat(
9173       "SomeClass::Constructor()\n"
9174       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9175       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9176       Style);
9177 
9178   Style.ConstructorInitializerIndentWidth = 0;
9179   verifyFormat(
9180       "SomeClass::Constructor()\n"
9181       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9182       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9183       Style);
9184   Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
9185   verifyFormat(
9186       "SomeLongTemplateVariableName<\n"
9187       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>",
9188       Style);
9189   verifyFormat(
9190       "bool smaller = 1 < bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
9191       "                       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
9192       Style);
9193 }
9194 
9195 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9196   FormatStyle Style = getLLVMStyle();
9197   Style.BreakConstructorInitializersBeforeComma = true;
9198   Style.ConstructorInitializerIndentWidth = 4;
9199   verifyFormat("SomeClass::Constructor()\n"
9200                "    : a(a)\n"
9201                "    , b(b)\n"
9202                "    , c(c) {}",
9203                Style);
9204   verifyFormat("SomeClass::Constructor()\n"
9205                "    : a(a) {}",
9206                Style);
9207 
9208   Style.ColumnLimit = 0;
9209   verifyFormat("SomeClass::Constructor()\n"
9210                "    : a(a) {}",
9211                Style);
9212   verifyFormat("SomeClass::Constructor() noexcept\n"
9213                "    : a(a) {}",
9214                Style);
9215   verifyFormat("SomeClass::Constructor()\n"
9216                "    : a(a)\n"
9217                "    , b(b)\n"
9218                "    , c(c) {}",
9219                Style);
9220   verifyFormat("SomeClass::Constructor()\n"
9221                "    : a(a) {\n"
9222                "  foo();\n"
9223                "  bar();\n"
9224                "}",
9225                Style);
9226 
9227   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9228   verifyFormat("SomeClass::Constructor()\n"
9229                "    : a(a)\n"
9230                "    , b(b)\n"
9231                "    , c(c) {\n}",
9232                Style);
9233   verifyFormat("SomeClass::Constructor()\n"
9234                "    : a(a) {\n}",
9235                Style);
9236 
9237   Style.ColumnLimit = 80;
9238   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9239   Style.ConstructorInitializerIndentWidth = 2;
9240   verifyFormat("SomeClass::Constructor()\n"
9241                "  : a(a)\n"
9242                "  , b(b)\n"
9243                "  , c(c) {}",
9244                Style);
9245 
9246   Style.ConstructorInitializerIndentWidth = 0;
9247   verifyFormat("SomeClass::Constructor()\n"
9248                ": a(a)\n"
9249                ", b(b)\n"
9250                ", c(c) {}",
9251                Style);
9252 
9253   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9254   Style.ConstructorInitializerIndentWidth = 4;
9255   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9256   verifyFormat(
9257       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9258       Style);
9259   verifyFormat(
9260       "SomeClass::Constructor()\n"
9261       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9262       Style);
9263   Style.ConstructorInitializerIndentWidth = 4;
9264   Style.ColumnLimit = 60;
9265   verifyFormat("SomeClass::Constructor()\n"
9266                "    : aaaaaaaa(aaaaaaaa)\n"
9267                "    , aaaaaaaa(aaaaaaaa)\n"
9268                "    , aaaaaaaa(aaaaaaaa) {}",
9269                Style);
9270 }
9271 
9272 TEST_F(FormatTest, Destructors) {
9273   verifyFormat("void F(int &i) { i.~int(); }");
9274   verifyFormat("void F(int &i) { i->~int(); }");
9275 }
9276 
9277 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9278   FormatStyle Style = getWebKitStyle();
9279 
9280   // Don't indent in outer namespaces.
9281   verifyFormat("namespace outer {\n"
9282                "int i;\n"
9283                "namespace inner {\n"
9284                "    int i;\n"
9285                "} // namespace inner\n"
9286                "} // namespace outer\n"
9287                "namespace other_outer {\n"
9288                "int i;\n"
9289                "}",
9290                Style);
9291 
9292   // Don't indent case labels.
9293   verifyFormat("switch (variable) {\n"
9294                "case 1:\n"
9295                "case 2:\n"
9296                "    doSomething();\n"
9297                "    break;\n"
9298                "default:\n"
9299                "    ++variable;\n"
9300                "}",
9301                Style);
9302 
9303   // Wrap before binary operators.
9304   EXPECT_EQ("void f()\n"
9305             "{\n"
9306             "    if (aaaaaaaaaaaaaaaa\n"
9307             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9308             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9309             "        return;\n"
9310             "}",
9311             format("void f() {\n"
9312                    "if (aaaaaaaaaaaaaaaa\n"
9313                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9314                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9315                    "return;\n"
9316                    "}",
9317                    Style));
9318 
9319   // Allow functions on a single line.
9320   verifyFormat("void f() { return; }", Style);
9321 
9322   // Constructor initializers are formatted one per line with the "," on the
9323   // new line.
9324   verifyFormat("Constructor()\n"
9325                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9326                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9327                "          aaaaaaaaaaaaaa)\n"
9328                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9329                "{\n"
9330                "}",
9331                Style);
9332   verifyFormat("SomeClass::Constructor()\n"
9333                "    : a(a)\n"
9334                "{\n"
9335                "}",
9336                Style);
9337   EXPECT_EQ("SomeClass::Constructor()\n"
9338             "    : a(a)\n"
9339             "{\n"
9340             "}",
9341             format("SomeClass::Constructor():a(a){}", Style));
9342   verifyFormat("SomeClass::Constructor()\n"
9343                "    : a(a)\n"
9344                "    , b(b)\n"
9345                "    , c(c)\n"
9346                "{\n"
9347                "}",
9348                Style);
9349   verifyFormat("SomeClass::Constructor()\n"
9350                "    : a(a)\n"
9351                "{\n"
9352                "    foo();\n"
9353                "    bar();\n"
9354                "}",
9355                Style);
9356 
9357   // Access specifiers should be aligned left.
9358   verifyFormat("class C {\n"
9359                "public:\n"
9360                "    int i;\n"
9361                "};",
9362                Style);
9363 
9364   // Do not align comments.
9365   verifyFormat("int a; // Do not\n"
9366                "double b; // align comments.",
9367                Style);
9368 
9369   // Do not align operands.
9370   EXPECT_EQ("ASSERT(aaaa\n"
9371             "    || bbbb);",
9372             format("ASSERT ( aaaa\n||bbbb);", Style));
9373 
9374   // Accept input's line breaks.
9375   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9376             "    || bbbbbbbbbbbbbbb) {\n"
9377             "    i++;\n"
9378             "}",
9379             format("if (aaaaaaaaaaaaaaa\n"
9380                    "|| bbbbbbbbbbbbbbb) { i++; }",
9381                    Style));
9382   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9383             "    i++;\n"
9384             "}",
9385             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9386 
9387   // Don't automatically break all macro definitions (llvm.org/PR17842).
9388   verifyFormat("#define aNumber 10", Style);
9389   // However, generally keep the line breaks that the user authored.
9390   EXPECT_EQ("#define aNumber \\\n"
9391             "    10",
9392             format("#define aNumber \\\n"
9393                    " 10",
9394                    Style));
9395 
9396   // Keep empty and one-element array literals on a single line.
9397   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9398             "                                  copyItems:YES];",
9399             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9400                    "copyItems:YES];",
9401                    Style));
9402   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9403             "                                  copyItems:YES];",
9404             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9405                    "             copyItems:YES];",
9406                    Style));
9407   // FIXME: This does not seem right, there should be more indentation before
9408   // the array literal's entries. Nested blocks have the same problem.
9409   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9410             "    @\"a\",\n"
9411             "    @\"a\"\n"
9412             "]\n"
9413             "                                  copyItems:YES];",
9414             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9415                    "     @\"a\",\n"
9416                    "     @\"a\"\n"
9417                    "     ]\n"
9418                    "       copyItems:YES];",
9419                    Style));
9420   EXPECT_EQ(
9421       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9422       "                                  copyItems:YES];",
9423       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9424              "   copyItems:YES];",
9425              Style));
9426 
9427   verifyFormat("[self.a b:c c:d];", Style);
9428   EXPECT_EQ("[self.a b:c\n"
9429             "        c:d];",
9430             format("[self.a b:c\n"
9431                    "c:d];",
9432                    Style));
9433 }
9434 
9435 TEST_F(FormatTest, FormatsLambdas) {
9436   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9437   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9438   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9439   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9440   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9441   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9442   verifyFormat("int x = f(*+[] {});");
9443   verifyFormat("void f() {\n"
9444                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9445                "}\n");
9446   verifyFormat("void f() {\n"
9447                "  other(x.begin(), //\n"
9448                "        x.end(),   //\n"
9449                "        [&](int, int) { return 1; });\n"
9450                "}\n");
9451   verifyFormat("SomeFunction([]() { // A cool function...\n"
9452                "  return 43;\n"
9453                "});");
9454   EXPECT_EQ("SomeFunction([]() {\n"
9455             "#define A a\n"
9456             "  return 43;\n"
9457             "});",
9458             format("SomeFunction([](){\n"
9459                    "#define A a\n"
9460                    "return 43;\n"
9461                    "});"));
9462   verifyFormat("void f() {\n"
9463                "  SomeFunction([](decltype(x), A *a) {});\n"
9464                "}");
9465   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9466                "    [](const aaaaaaaaaa &a) { return a; });");
9467   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9468                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9469                "});");
9470   verifyFormat("Constructor()\n"
9471                "    : Field([] { // comment\n"
9472                "        int i;\n"
9473                "      }) {}");
9474   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
9475                "  return some_parameter.size();\n"
9476                "};");
9477   verifyFormat("std::function<std::string(const std::string &)> my_lambda =\n"
9478                "    [](const string &s) { return s; };");
9479   verifyFormat("int i = aaaaaa ? 1 //\n"
9480                "               : [] {\n"
9481                "                   return 2; //\n"
9482                "                 }();");
9483   verifyFormat("llvm::errs() << \"number of twos is \"\n"
9484                "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
9485                "                  return x == 2; // force break\n"
9486                "                });");
9487   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9488                "    [=](int iiiiiiiiiiii) {\n"
9489                "      return aaaaaaaaaaaaaaaaaaaaaaa !=\n"
9490                "             aaaaaaaaaaaaaaaaaaaaaaa;\n"
9491                "    });",
9492                getLLVMStyleWithColumns(60));
9493   verifyFormat("SomeFunction({[&] {\n"
9494                "                // comment\n"
9495                "              },\n"
9496                "              [&] {\n"
9497                "                // comment\n"
9498                "              }});");
9499   verifyFormat("SomeFunction({[&] {\n"
9500                "  // comment\n"
9501                "}});");
9502   verifyFormat("virtual aaaaaaaaaaaaaaaa(std::function<bool()> bbbbbbbbbbbb =\n"
9503                "                             [&]() { return true; },\n"
9504                "                         aaaaa aaaaaaaaa);");
9505 
9506   // Lambdas with return types.
9507   verifyFormat("int c = []() -> int { return 2; }();\n");
9508   verifyFormat("int c = []() -> int * { return 2; }();\n");
9509   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9510   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9511   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9512   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9513   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9514   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9515   verifyFormat("[a, a]() -> a<1> {};");
9516   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9517                "                   int j) -> int {\n"
9518                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9519                "};");
9520   verifyFormat(
9521       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9522       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9523       "      return aaaaaaaaaaaaaaaaa;\n"
9524       "    });",
9525       getLLVMStyleWithColumns(70));
9526   verifyFormat("[]() //\n"
9527                "    -> int {\n"
9528                "  return 1; //\n"
9529                "};");
9530 
9531   // Multiple lambdas in the same parentheses change indentation rules.
9532   verifyFormat("SomeFunction(\n"
9533                "    []() {\n"
9534                "      int i = 42;\n"
9535                "      return i;\n"
9536                "    },\n"
9537                "    []() {\n"
9538                "      int j = 43;\n"
9539                "      return j;\n"
9540                "    });");
9541 
9542   // More complex introducers.
9543   verifyFormat("return [i, args...] {};");
9544 
9545   // Not lambdas.
9546   verifyFormat("constexpr char hello[]{\"hello\"};");
9547   verifyFormat("double &operator[](int i) { return 0; }\n"
9548                "int i;");
9549   verifyFormat("std::unique_ptr<int[]> foo() {}");
9550   verifyFormat("int i = a[a][a]->f();");
9551   verifyFormat("int i = (*b)[a]->f();");
9552 
9553   // Other corner cases.
9554   verifyFormat("void f() {\n"
9555                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9556                "      );\n"
9557                "}");
9558 
9559   // Lambdas created through weird macros.
9560   verifyFormat("void f() {\n"
9561                "  MACRO((const AA &a) { return 1; });\n"
9562                "  MACRO((AA &a) { return 1; });\n"
9563                "}");
9564 
9565   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9566                "      doo_dah();\n"
9567                "      doo_dah();\n"
9568                "    })) {\n"
9569                "}");
9570   verifyFormat("auto lambda = []() {\n"
9571                "  int a = 2\n"
9572                "#if A\n"
9573                "          + 2\n"
9574                "#endif\n"
9575                "      ;\n"
9576                "};");
9577 
9578   // Lambdas with complex multiline introducers.
9579   verifyFormat(
9580       "aaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9581       "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]()\n"
9582       "        -> ::std::unordered_set<\n"
9583       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
9584       "      //\n"
9585       "    });");
9586 }
9587 
9588 TEST_F(FormatTest, FormatsBlocks) {
9589   FormatStyle ShortBlocks = getLLVMStyle();
9590   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9591   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9592   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9593   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9594   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9595   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9596   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9597 
9598   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9599   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9600   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9601 
9602   verifyFormat("[operation setCompletionBlock:^{\n"
9603                "  [self onOperationDone];\n"
9604                "}];");
9605   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9606                "  [self onOperationDone];\n"
9607                "}]};");
9608   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9609                "  f();\n"
9610                "}];");
9611   verifyFormat("int a = [operation block:^int(int *i) {\n"
9612                "  return 1;\n"
9613                "}];");
9614   verifyFormat("[myObject doSomethingWith:arg1\n"
9615                "                      aaa:^int(int *a) {\n"
9616                "                        return 1;\n"
9617                "                      }\n"
9618                "                      bbb:f(a * bbbbbbbb)];");
9619 
9620   verifyFormat("[operation setCompletionBlock:^{\n"
9621                "  [self.delegate newDataAvailable];\n"
9622                "}];",
9623                getLLVMStyleWithColumns(60));
9624   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9625                "  NSString *path = [self sessionFilePath];\n"
9626                "  if (path) {\n"
9627                "    // ...\n"
9628                "  }\n"
9629                "});");
9630   verifyFormat("[[SessionService sharedService]\n"
9631                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9632                "      if (window) {\n"
9633                "        [self windowDidLoad:window];\n"
9634                "      } else {\n"
9635                "        [self errorLoadingWindow];\n"
9636                "      }\n"
9637                "    }];");
9638   verifyFormat("void (^largeBlock)(void) = ^{\n"
9639                "  // ...\n"
9640                "};\n",
9641                getLLVMStyleWithColumns(40));
9642   verifyFormat("[[SessionService sharedService]\n"
9643                "    loadWindowWithCompletionBlock: //\n"
9644                "        ^(SessionWindow *window) {\n"
9645                "          if (window) {\n"
9646                "            [self windowDidLoad:window];\n"
9647                "          } else {\n"
9648                "            [self errorLoadingWindow];\n"
9649                "          }\n"
9650                "        }];",
9651                getLLVMStyleWithColumns(60));
9652   verifyFormat("[myObject doSomethingWith:arg1\n"
9653                "    firstBlock:^(Foo *a) {\n"
9654                "      // ...\n"
9655                "      int i;\n"
9656                "    }\n"
9657                "    secondBlock:^(Bar *b) {\n"
9658                "      // ...\n"
9659                "      int i;\n"
9660                "    }\n"
9661                "    thirdBlock:^Foo(Bar *b) {\n"
9662                "      // ...\n"
9663                "      int i;\n"
9664                "    }];");
9665   verifyFormat("[myObject doSomethingWith:arg1\n"
9666                "               firstBlock:-1\n"
9667                "              secondBlock:^(Bar *b) {\n"
9668                "                // ...\n"
9669                "                int i;\n"
9670                "              }];");
9671 
9672   verifyFormat("f(^{\n"
9673                "  @autoreleasepool {\n"
9674                "    if (a) {\n"
9675                "      g();\n"
9676                "    }\n"
9677                "  }\n"
9678                "});");
9679   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9680   verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
9681                "};");
9682 
9683   FormatStyle FourIndent = getLLVMStyle();
9684   FourIndent.ObjCBlockIndentWidth = 4;
9685   verifyFormat("[operation setCompletionBlock:^{\n"
9686                "    [self onOperationDone];\n"
9687                "}];",
9688                FourIndent);
9689 }
9690 
9691 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
9692   FormatStyle ZeroColumn = getLLVMStyle();
9693   ZeroColumn.ColumnLimit = 0;
9694 
9695   verifyFormat("[[SessionService sharedService] "
9696                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9697                "  if (window) {\n"
9698                "    [self windowDidLoad:window];\n"
9699                "  } else {\n"
9700                "    [self errorLoadingWindow];\n"
9701                "  }\n"
9702                "}];",
9703                ZeroColumn);
9704   EXPECT_EQ("[[SessionService sharedService]\n"
9705             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9706             "      if (window) {\n"
9707             "        [self windowDidLoad:window];\n"
9708             "      } else {\n"
9709             "        [self errorLoadingWindow];\n"
9710             "      }\n"
9711             "    }];",
9712             format("[[SessionService sharedService]\n"
9713                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9714                    "                if (window) {\n"
9715                    "    [self windowDidLoad:window];\n"
9716                    "  } else {\n"
9717                    "    [self errorLoadingWindow];\n"
9718                    "  }\n"
9719                    "}];",
9720                    ZeroColumn));
9721   verifyFormat("[myObject doSomethingWith:arg1\n"
9722                "    firstBlock:^(Foo *a) {\n"
9723                "      // ...\n"
9724                "      int i;\n"
9725                "    }\n"
9726                "    secondBlock:^(Bar *b) {\n"
9727                "      // ...\n"
9728                "      int i;\n"
9729                "    }\n"
9730                "    thirdBlock:^Foo(Bar *b) {\n"
9731                "      // ...\n"
9732                "      int i;\n"
9733                "    }];",
9734                ZeroColumn);
9735   verifyFormat("f(^{\n"
9736                "  @autoreleasepool {\n"
9737                "    if (a) {\n"
9738                "      g();\n"
9739                "    }\n"
9740                "  }\n"
9741                "});",
9742                ZeroColumn);
9743   verifyFormat("void (^largeBlock)(void) = ^{\n"
9744                "  // ...\n"
9745                "};",
9746                ZeroColumn);
9747 
9748   ZeroColumn.AllowShortBlocksOnASingleLine = true;
9749   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
9750             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
9751   ZeroColumn.AllowShortBlocksOnASingleLine = false;
9752   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
9753             "  int i;\n"
9754             "};",
9755             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
9756 }
9757 
9758 TEST_F(FormatTest, SupportsCRLF) {
9759   EXPECT_EQ("int a;\r\n"
9760             "int b;\r\n"
9761             "int c;\r\n",
9762             format("int a;\r\n"
9763                    "  int b;\r\n"
9764                    "    int c;\r\n",
9765                    getLLVMStyle()));
9766   EXPECT_EQ("int a;\r\n"
9767             "int b;\r\n"
9768             "int c;\r\n",
9769             format("int a;\r\n"
9770                    "  int b;\n"
9771                    "    int c;\r\n",
9772                    getLLVMStyle()));
9773   EXPECT_EQ("int a;\n"
9774             "int b;\n"
9775             "int c;\n",
9776             format("int a;\r\n"
9777                    "  int b;\n"
9778                    "    int c;\n",
9779                    getLLVMStyle()));
9780   EXPECT_EQ("\"aaaaaaa \"\r\n"
9781             "\"bbbbbbb\";\r\n",
9782             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
9783   EXPECT_EQ("#define A \\\r\n"
9784             "  b;      \\\r\n"
9785             "  c;      \\\r\n"
9786             "  d;\r\n",
9787             format("#define A \\\r\n"
9788                    "  b; \\\r\n"
9789                    "  c; d; \r\n",
9790                    getGoogleStyle()));
9791 
9792   EXPECT_EQ("/*\r\n"
9793             "multi line block comments\r\n"
9794             "should not introduce\r\n"
9795             "an extra carriage return\r\n"
9796             "*/\r\n",
9797             format("/*\r\n"
9798                    "multi line block comments\r\n"
9799                    "should not introduce\r\n"
9800                    "an extra carriage return\r\n"
9801                    "*/\r\n"));
9802 }
9803 
9804 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
9805   verifyFormat("MY_CLASS(C) {\n"
9806                "  int i;\n"
9807                "  int j;\n"
9808                "};");
9809 }
9810 
9811 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
9812   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
9813   TwoIndent.ContinuationIndentWidth = 2;
9814 
9815   EXPECT_EQ("int i =\n"
9816             "  longFunction(\n"
9817             "    arg);",
9818             format("int i = longFunction(arg);", TwoIndent));
9819 
9820   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
9821   SixIndent.ContinuationIndentWidth = 6;
9822 
9823   EXPECT_EQ("int i =\n"
9824             "      longFunction(\n"
9825             "            arg);",
9826             format("int i = longFunction(arg);", SixIndent));
9827 }
9828 
9829 TEST_F(FormatTest, SpacesInAngles) {
9830   FormatStyle Spaces = getLLVMStyle();
9831   Spaces.SpacesInAngles = true;
9832 
9833   verifyFormat("static_cast< int >(arg);", Spaces);
9834   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
9835   verifyFormat("f< int, float >();", Spaces);
9836   verifyFormat("template <> g() {}", Spaces);
9837   verifyFormat("template < std::vector< int > > f() {}", Spaces);
9838   verifyFormat("std::function< void(int, int) > fct;", Spaces);
9839   verifyFormat("void inFunction() { std::function< void(int, int) > fct; }",
9840                Spaces);
9841 
9842   Spaces.Standard = FormatStyle::LS_Cpp03;
9843   Spaces.SpacesInAngles = true;
9844   verifyFormat("A< A< int > >();", Spaces);
9845 
9846   Spaces.SpacesInAngles = false;
9847   verifyFormat("A<A<int> >();", Spaces);
9848 
9849   Spaces.Standard = FormatStyle::LS_Cpp11;
9850   Spaces.SpacesInAngles = true;
9851   verifyFormat("A< A< int > >();", Spaces);
9852 
9853   Spaces.SpacesInAngles = false;
9854   verifyFormat("A<A<int>>();", Spaces);
9855 }
9856 
9857 TEST_F(FormatTest, SpaceAfterTemplateKeyword) {
9858   FormatStyle Style = getLLVMStyle();
9859   Style.SpaceAfterTemplateKeyword = false;
9860   verifyFormat("template<int> void foo();", Style);
9861 }
9862 
9863 TEST_F(FormatTest, TripleAngleBrackets) {
9864   verifyFormat("f<<<1, 1>>>();");
9865   verifyFormat("f<<<1, 1, 1, s>>>();");
9866   verifyFormat("f<<<a, b, c, d>>>();");
9867   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
9868   verifyFormat("f<param><<<1, 1>>>();");
9869   verifyFormat("f<1><<<1, 1>>>();");
9870   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
9871   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9872                "aaaaaaaaaaa<<<\n    1, 1>>>();");
9873   verifyFormat("aaaaaaaaaaaaaaa<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaa>\n"
9874                "    <<<aaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaaaaaaaaa>>>();");
9875 }
9876 
9877 TEST_F(FormatTest, MergeLessLessAtEnd) {
9878   verifyFormat("<<");
9879   EXPECT_EQ("< < <", format("\\\n<<<"));
9880   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9881                "aaallvm::outs() <<");
9882   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9883                "aaaallvm::outs()\n    <<");
9884 }
9885 
9886 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
9887   std::string code = "#if A\n"
9888                      "#if B\n"
9889                      "a.\n"
9890                      "#endif\n"
9891                      "    a = 1;\n"
9892                      "#else\n"
9893                      "#endif\n"
9894                      "#if C\n"
9895                      "#else\n"
9896                      "#endif\n";
9897   EXPECT_EQ(code, format(code));
9898 }
9899 
9900 TEST_F(FormatTest, HandleConflictMarkers) {
9901   // Git/SVN conflict markers.
9902   EXPECT_EQ("int a;\n"
9903             "void f() {\n"
9904             "  callme(some(parameter1,\n"
9905             "<<<<<<< text by the vcs\n"
9906             "              parameter2),\n"
9907             "||||||| text by the vcs\n"
9908             "              parameter2),\n"
9909             "         parameter3,\n"
9910             "======= text by the vcs\n"
9911             "              parameter2, parameter3),\n"
9912             ">>>>>>> text by the vcs\n"
9913             "         otherparameter);\n",
9914             format("int a;\n"
9915                    "void f() {\n"
9916                    "  callme(some(parameter1,\n"
9917                    "<<<<<<< text by the vcs\n"
9918                    "  parameter2),\n"
9919                    "||||||| text by the vcs\n"
9920                    "  parameter2),\n"
9921                    "  parameter3,\n"
9922                    "======= text by the vcs\n"
9923                    "  parameter2,\n"
9924                    "  parameter3),\n"
9925                    ">>>>>>> text by the vcs\n"
9926                    "  otherparameter);\n"));
9927 
9928   // Perforce markers.
9929   EXPECT_EQ("void f() {\n"
9930             "  function(\n"
9931             ">>>> text by the vcs\n"
9932             "      parameter,\n"
9933             "==== text by the vcs\n"
9934             "      parameter,\n"
9935             "==== text by the vcs\n"
9936             "      parameter,\n"
9937             "<<<< text by the vcs\n"
9938             "      parameter);\n",
9939             format("void f() {\n"
9940                    "  function(\n"
9941                    ">>>> text by the vcs\n"
9942                    "  parameter,\n"
9943                    "==== text by the vcs\n"
9944                    "  parameter,\n"
9945                    "==== text by the vcs\n"
9946                    "  parameter,\n"
9947                    "<<<< text by the vcs\n"
9948                    "  parameter);\n"));
9949 
9950   EXPECT_EQ("<<<<<<<\n"
9951             "|||||||\n"
9952             "=======\n"
9953             ">>>>>>>",
9954             format("<<<<<<<\n"
9955                    "|||||||\n"
9956                    "=======\n"
9957                    ">>>>>>>"));
9958 
9959   EXPECT_EQ("<<<<<<<\n"
9960             "|||||||\n"
9961             "int i;\n"
9962             "=======\n"
9963             ">>>>>>>",
9964             format("<<<<<<<\n"
9965                    "|||||||\n"
9966                    "int i;\n"
9967                    "=======\n"
9968                    ">>>>>>>"));
9969 
9970   // FIXME: Handle parsing of macros around conflict markers correctly:
9971   EXPECT_EQ("#define Macro \\\n"
9972             "<<<<<<<\n"
9973             "Something \\\n"
9974             "|||||||\n"
9975             "Else \\\n"
9976             "=======\n"
9977             "Other \\\n"
9978             ">>>>>>>\n"
9979             "    End int i;\n",
9980             format("#define Macro \\\n"
9981                    "<<<<<<<\n"
9982                    "  Something \\\n"
9983                    "|||||||\n"
9984                    "  Else \\\n"
9985                    "=======\n"
9986                    "  Other \\\n"
9987                    ">>>>>>>\n"
9988                    "  End\n"
9989                    "int i;\n"));
9990 }
9991 
9992 TEST_F(FormatTest, DisableRegions) {
9993   EXPECT_EQ("int i;\n"
9994             "// clang-format off\n"
9995             "  int j;\n"
9996             "// clang-format on\n"
9997             "int k;",
9998             format(" int  i;\n"
9999                    "   // clang-format off\n"
10000                    "  int j;\n"
10001                    " // clang-format on\n"
10002                    "   int   k;"));
10003   EXPECT_EQ("int i;\n"
10004             "/* clang-format off */\n"
10005             "  int j;\n"
10006             "/* clang-format on */\n"
10007             "int k;",
10008             format(" int  i;\n"
10009                    "   /* clang-format off */\n"
10010                    "  int j;\n"
10011                    " /* clang-format on */\n"
10012                    "   int   k;"));
10013 
10014   // Don't reflow comments within disabled regions.
10015   EXPECT_EQ(
10016       "// clang-format off\n"
10017       "// long long long long long long line\n"
10018       "/* clang-format on */\n"
10019       "/* long long long\n"
10020       " * long long long\n"
10021       " * line */\n"
10022       "int i;\n"
10023       "/* clang-format off */\n"
10024       "/* long long long long long long line */\n",
10025       format("// clang-format off\n"
10026              "// long long long long long long line\n"
10027              "/* clang-format on */\n"
10028              "/* long long long long long long line */\n"
10029              "int i;\n"
10030              "/* clang-format off */\n"
10031              "/* long long long long long long line */\n",
10032              getLLVMStyleWithColumns(20)));
10033 }
10034 
10035 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
10036   format("? ) =");
10037   verifyNoCrash("#define a\\\n /**/}");
10038 }
10039 
10040 TEST_F(FormatTest, FormatsTableGenCode) {
10041   FormatStyle Style = getLLVMStyle();
10042   Style.Language = FormatStyle::LK_TableGen;
10043   verifyFormat("include \"a.td\"\ninclude \"b.td\"", Style);
10044 }
10045 
10046 TEST_F(FormatTest, ArrayOfTemplates) {
10047   EXPECT_EQ("auto a = new unique_ptr<int>[10];",
10048             format("auto a = new unique_ptr<int > [ 10];"));
10049 
10050   FormatStyle Spaces = getLLVMStyle();
10051   Spaces.SpacesInSquareBrackets = true;
10052   EXPECT_EQ("auto a = new unique_ptr<int>[ 10 ];",
10053             format("auto a = new unique_ptr<int > [10];", Spaces));
10054 }
10055 
10056 TEST_F(FormatTest, ArrayAsTemplateType) {
10057   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[10]>;",
10058             format("auto a = unique_ptr < Foo < Bar>[ 10]> ;"));
10059 
10060   FormatStyle Spaces = getLLVMStyle();
10061   Spaces.SpacesInSquareBrackets = true;
10062   EXPECT_EQ("auto a = unique_ptr<Foo<Bar>[ 10 ]>;",
10063             format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces));
10064 }
10065 
10066 TEST_F(FormatTest, NoSpaceAfterSuper) {
10067     verifyFormat("__super::FooBar();");
10068 }
10069 
10070 TEST(FormatStyle, GetStyleOfFile) {
10071   vfs::InMemoryFileSystem FS;
10072   // Test 1: format file in the same directory.
10073   ASSERT_TRUE(
10074       FS.addFile("/a/.clang-format", 0,
10075                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM")));
10076   ASSERT_TRUE(
10077       FS.addFile("/a/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10078   auto Style1 = getStyle("file", "/a/.clang-format", "Google", "", &FS);
10079   ASSERT_TRUE((bool)Style1);
10080   ASSERT_EQ(*Style1, getLLVMStyle());
10081 
10082   // Test 2.1: fallback to default.
10083   ASSERT_TRUE(
10084       FS.addFile("/b/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10085   auto Style2 = getStyle("file", "/b/test.cpp", "Mozilla", "", &FS);
10086   ASSERT_TRUE((bool)Style2);
10087   ASSERT_EQ(*Style2, getMozillaStyle());
10088 
10089   // Test 2.2: no format on 'none' fallback style.
10090   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10091   ASSERT_TRUE((bool)Style2);
10092   ASSERT_EQ(*Style2, getNoStyle());
10093 
10094   // Test 2.3: format if config is found with no based style while fallback is
10095   // 'none'.
10096   ASSERT_TRUE(FS.addFile("/b/.clang-format", 0,
10097                          llvm::MemoryBuffer::getMemBuffer("IndentWidth: 2")));
10098   Style2 = getStyle("file", "/b/test.cpp", "none", "", &FS);
10099   ASSERT_TRUE((bool)Style2);
10100   ASSERT_EQ(*Style2, getLLVMStyle());
10101 
10102   // Test 2.4: format if yaml with no based style, while fallback is 'none'.
10103   Style2 = getStyle("{}", "a.h", "none", "", &FS);
10104   ASSERT_TRUE((bool)Style2);
10105   ASSERT_EQ(*Style2, getLLVMStyle());
10106 
10107   // Test 3: format file in parent directory.
10108   ASSERT_TRUE(
10109       FS.addFile("/c/.clang-format", 0,
10110                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: Google")));
10111   ASSERT_TRUE(FS.addFile("/c/sub/sub/sub/test.cpp", 0,
10112                          llvm::MemoryBuffer::getMemBuffer("int i;")));
10113   auto Style3 = getStyle("file", "/c/sub/sub/sub/test.cpp", "LLVM", "", &FS);
10114   ASSERT_TRUE((bool)Style3);
10115   ASSERT_EQ(*Style3, getGoogleStyle());
10116 
10117   // Test 4: error on invalid fallback style
10118   auto Style4 = getStyle("file", "a.h", "KungFu", "", &FS);
10119   ASSERT_FALSE((bool)Style4);
10120   llvm::consumeError(Style4.takeError());
10121 
10122   // Test 5: error on invalid yaml on command line
10123   auto Style5 = getStyle("{invalid_key=invalid_value}", "a.h", "LLVM", "", &FS);
10124   ASSERT_FALSE((bool)Style5);
10125   llvm::consumeError(Style5.takeError());
10126 
10127   // Test 6: error on invalid style
10128   auto Style6 = getStyle("KungFu", "a.h", "LLVM", "", &FS);
10129   ASSERT_FALSE((bool)Style6);
10130   llvm::consumeError(Style6.takeError());
10131 
10132   // Test 7: found config file, error on parsing it
10133   ASSERT_TRUE(
10134       FS.addFile("/d/.clang-format", 0,
10135                  llvm::MemoryBuffer::getMemBuffer("BasedOnStyle: LLVM\n"
10136                                                   "InvalidKey: InvalidValue")));
10137   ASSERT_TRUE(
10138       FS.addFile("/d/test.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int i;")));
10139   auto Style7 = getStyle("file", "/d/.clang-format", "LLVM", "", &FS);
10140   ASSERT_FALSE((bool)Style7);
10141   llvm::consumeError(Style7.takeError());
10142 }
10143 
10144 TEST_F(ReplacementTest, FormatCodeAfterReplacements) {
10145   // Column limit is 20.
10146   std::string Code = "Type *a =\n"
10147                      "    new Type();\n"
10148                      "g(iiiii, 0, jjjjj,\n"
10149                      "  0, kkkkk, 0, mm);\n"
10150                      "int  bad     = format   ;";
10151   std::string Expected = "auto a = new Type();\n"
10152                          "g(iiiii, nullptr,\n"
10153                          "  jjjjj, nullptr,\n"
10154                          "  kkkkk, nullptr,\n"
10155                          "  mm);\n"
10156                          "int  bad     = format   ;";
10157   FileID ID = Context.createInMemoryFile("format.cpp", Code);
10158   tooling::Replacements Replaces = toReplacements(
10159       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 6,
10160                             "auto "),
10161        tooling::Replacement(Context.Sources, Context.getLocation(ID, 3, 10), 1,
10162                             "nullptr"),
10163        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 3), 1,
10164                             "nullptr"),
10165        tooling::Replacement(Context.Sources, Context.getLocation(ID, 4, 13), 1,
10166                             "nullptr")});
10167 
10168   format::FormatStyle Style = format::getLLVMStyle();
10169   Style.ColumnLimit = 20; // Set column limit to 20 to increase readibility.
10170   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10171   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10172       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10173   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10174   EXPECT_TRUE(static_cast<bool>(Result));
10175   EXPECT_EQ(Expected, *Result);
10176 }
10177 
10178 TEST_F(ReplacementTest, SortIncludesAfterReplacement) {
10179   std::string Code = "#include \"a.h\"\n"
10180                      "#include \"c.h\"\n"
10181                      "\n"
10182                      "int main() {\n"
10183                      "  return 0;\n"
10184                      "}";
10185   std::string Expected = "#include \"a.h\"\n"
10186                          "#include \"b.h\"\n"
10187                          "#include \"c.h\"\n"
10188                          "\n"
10189                          "int main() {\n"
10190                          "  return 0;\n"
10191                          "}";
10192   FileID ID = Context.createInMemoryFile("fix.cpp", Code);
10193   tooling::Replacements Replaces = toReplacements(
10194       {tooling::Replacement(Context.Sources, Context.getLocation(ID, 1, 1), 0,
10195                             "#include \"b.h\"\n")});
10196 
10197   format::FormatStyle Style = format::getLLVMStyle();
10198   Style.SortIncludes = true;
10199   auto FormattedReplaces = formatReplacements(Code, Replaces, Style);
10200   EXPECT_TRUE(static_cast<bool>(FormattedReplaces))
10201       << llvm::toString(FormattedReplaces.takeError()) << "\n";
10202   auto Result = applyAllReplacements(Code, *FormattedReplaces);
10203   EXPECT_TRUE(static_cast<bool>(Result));
10204   EXPECT_EQ(Expected, *Result);
10205 }
10206 
10207 TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
10208   format::FormatStyle Style = format::getLLVMStyle();
10209   Style.Standard = FormatStyle::LS_Cpp03;
10210   // cpp03 recognize this string as identifier u8 and literal character 'a'
10211   EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style));
10212 }
10213 
10214 TEST_F(FormatTest, UTF8CharacterLiteralCpp11) {
10215   // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers
10216   // all modes, including C++11, C++14 and C++17
10217   EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';"));
10218 }
10219 
10220 } // end namespace
10221 } // end namespace format
10222 } // end namespace clang
10223