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