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