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