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