xref: /llvm-project/clang/unittests/Format/FormatTest.cpp (revision beaa322c36e3cc00652bac081f91feaad29b80f3)
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   // FIXME: If multiple variables are defined, the "*" needs to move to the new
4499   // line. Also fix indent for breaking after the type, this looks bad.
4500   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
4501                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4502                "    * b = bbbbbbbbbbbbbbbbbbb;",
4503                getGoogleStyle());
4504 
4505   // Not ideal, but pointer-with-type does not allow much here.
4506   verifyGoogleFormat(
4507       "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, * b = bbbbbbbbbbbbbbbbbbb,\n"
4508       "           * b = bbbbbbbbbbbbbbbbbbb, * d = ddddddddddddddddddd;");
4509 }
4510 
4511 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4512   verifyFormat("arr[foo ? bar : baz];");
4513   verifyFormat("f()[foo ? bar : baz];");
4514   verifyFormat("(a + b)[foo ? bar : baz];");
4515   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4516 }
4517 
4518 TEST_F(FormatTest, AlignsStringLiterals) {
4519   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4520                "                                      \"short literal\");");
4521   verifyFormat(
4522       "looooooooooooooooooooooooongFunction(\n"
4523       "    \"short literal\"\n"
4524       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4525   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4526                "             \" string literals\",\n"
4527                "             and, other, parameters);");
4528   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4529             "      \"5678\";",
4530             format("fun + \"1243\" /* comment */\n"
4531                    "      \"5678\";",
4532                    getLLVMStyleWithColumns(28)));
4533   EXPECT_EQ(
4534       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4535       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4536       "         \"aaaaaaaaaaaaaaaa\";",
4537       format("aaaaaa ="
4538              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4539              "aaaaaaaaaaaaaaaaaaaaa\" "
4540              "\"aaaaaaaaaaaaaaaa\";"));
4541   verifyFormat("a = a + \"a\"\n"
4542                "        \"a\"\n"
4543                "        \"a\";");
4544   verifyFormat("f(\"a\", \"b\"\n"
4545                "       \"c\");");
4546 
4547   verifyFormat(
4548       "#define LL_FORMAT \"ll\"\n"
4549       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4550       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4551 
4552   verifyFormat("#define A(X)          \\\n"
4553                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4554                "  \"ccccc\"",
4555                getLLVMStyleWithColumns(23));
4556   verifyFormat("#define A \"def\"\n"
4557                "f(\"abc\" A \"ghi\"\n"
4558                "  \"jkl\");");
4559 
4560   verifyFormat("f(L\"a\"\n"
4561                "  L\"b\")");
4562   verifyFormat("#define A(X)            \\\n"
4563                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4564                "  L\"ccccc\"",
4565                getLLVMStyleWithColumns(25));
4566 }
4567 
4568 TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
4569   FormatStyle AfterType = getLLVMStyle();
4570   AfterType.AlwaysBreakAfterDefinitionReturnType = true;
4571   verifyFormat("const char *\n"
4572                "f(void) {\n"  // Break here.
4573                "  return \"\";\n"
4574                "}\n"
4575                "const char *bar(void);\n",  // No break here.
4576                AfterType);
4577   verifyFormat("template <class T>\n"
4578                "T *\n"
4579                "f(T &c) {\n"  // Break here.
4580                "  return NULL;\n"
4581                "}\n"
4582                "template <class T> T *f(T &c);\n",  // No break here.
4583                AfterType);
4584   AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4585   verifyFormat("const char *\n"
4586                "f(void)\n"  // Break here.
4587                "{\n"
4588                "  return \"\";\n"
4589                "}\n"
4590                "const char *bar(void);\n",  // No break here.
4591                AfterType);
4592   verifyFormat("template <class T>\n"
4593                "T *\n"  // Problem here: no line break
4594                "f(T &c)\n"  // Break here.
4595                "{\n"
4596                "  return NULL;\n"
4597                "}\n"
4598                "template <class T> T *f(T &c);\n",  // No break here.
4599                AfterType);
4600 }
4601 
4602 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4603   FormatStyle NoBreak = getLLVMStyle();
4604   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4605   FormatStyle Break = getLLVMStyle();
4606   Break.AlwaysBreakBeforeMultilineStrings = true;
4607   verifyFormat("aaaa = \"bbbb\"\n"
4608                "       \"cccc\";",
4609                NoBreak);
4610   verifyFormat("aaaa =\n"
4611                "    \"bbbb\"\n"
4612                "    \"cccc\";",
4613                Break);
4614   verifyFormat("aaaa(\"bbbb\"\n"
4615                "     \"cccc\");",
4616                NoBreak);
4617   verifyFormat("aaaa(\n"
4618                "    \"bbbb\"\n"
4619                "    \"cccc\");",
4620                Break);
4621   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4622                "          \"cccc\");",
4623                NoBreak);
4624   verifyFormat("aaaa(qqq,\n"
4625                "     \"bbbb\"\n"
4626                "     \"cccc\");",
4627                Break);
4628   verifyFormat("aaaa(qqq,\n"
4629                "     L\"bbbb\"\n"
4630                "     L\"cccc\");",
4631                Break);
4632 
4633   // As we break before unary operators, breaking right after them is bad.
4634   verifyFormat("string foo = abc ? \"x\"\n"
4635                "                   \"blah blah blah blah blah blah\"\n"
4636                "                 : \"y\";",
4637                Break);
4638 
4639   // Don't break if there is no column gain.
4640   verifyFormat("f(\"aaaa\"\n"
4641                "  \"bbbb\");",
4642                Break);
4643 
4644   // Treat literals with escaped newlines like multi-line string literals.
4645   EXPECT_EQ("x = \"a\\\n"
4646             "b\\\n"
4647             "c\";",
4648             format("x = \"a\\\n"
4649                    "b\\\n"
4650                    "c\";",
4651                    NoBreak));
4652   EXPECT_EQ("x =\n"
4653             "    \"a\\\n"
4654             "b\\\n"
4655             "c\";",
4656             format("x = \"a\\\n"
4657                    "b\\\n"
4658                    "c\";",
4659                    Break));
4660 
4661   // Exempt ObjC strings for now.
4662   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4663             "                           \"bbbb\";",
4664             format("NSString *const kString = @\"aaaa\"\n"
4665                    "\"bbbb\";",
4666                    Break));
4667 
4668   Break.ColumnLimit = 0;
4669   verifyFormat("const char *hello = \"hello llvm\";", Break);
4670 }
4671 
4672 TEST_F(FormatTest, AlignsPipes) {
4673   verifyFormat(
4674       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4675       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4676       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4677   verifyFormat(
4678       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4679       "                     << aaaaaaaaaaaaaaaaaaaa;");
4680   verifyFormat(
4681       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4682       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4683   verifyFormat(
4684       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4685       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4686       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4687   verifyFormat(
4688       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4689       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4690       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4691   verifyFormat(
4692       "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4693       "                             aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4694       "                             aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4695   verifyFormat(
4696       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4697       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4698       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4699       "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4700   verifyFormat(
4701       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4702       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4703 
4704   verifyFormat("return out << \"somepacket = {\\n\"\n"
4705                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4706                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4707                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4708                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4709                "           << \"}\";");
4710 
4711   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4712                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4713                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4714   verifyFormat(
4715       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4716       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4717       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4718       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4719       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4720   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4721                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4722   verifyFormat(
4723       "void f() {\n"
4724       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4725       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4726       "}");
4727   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4728                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4729 
4730   // Breaking before the first "<<" is generally not desirable.
4731   verifyFormat(
4732       "llvm::errs()\n"
4733       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4734       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4735       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4736       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4737       getLLVMStyleWithColumns(70));
4738   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4739                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4740                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4741                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4742                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4743                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4744                getLLVMStyleWithColumns(70));
4745 
4746   // But sometimes, breaking before the first "<<" is desirable.
4747   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4748                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4749   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4750                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4751                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4752   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4753                "    << BEF << IsTemplate << Description << E->getType();");
4754 
4755   verifyFormat(
4756       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4757       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4758 
4759   // Incomplete string literal.
4760   EXPECT_EQ("llvm::errs() << \"\n"
4761             "             << a;",
4762             format("llvm::errs() << \"\n<<a;"));
4763 
4764   verifyFormat("void f() {\n"
4765                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4766                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4767                "}");
4768 
4769   // Handle 'endl'.
4770   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4771                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4772   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4773 }
4774 
4775 TEST_F(FormatTest, UnderstandsEquals) {
4776   verifyFormat(
4777       "aaaaaaaaaaaaaaaaa =\n"
4778       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4779   verifyFormat(
4780       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4781       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4782   verifyFormat(
4783       "if (a) {\n"
4784       "  f();\n"
4785       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4786       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4787       "}");
4788 
4789   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4790                "        100000000 + 10000000) {\n}");
4791 }
4792 
4793 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4794   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4795                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4796 
4797   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4798                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4799 
4800   verifyFormat(
4801       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4802       "                                                          Parameter2);");
4803 
4804   verifyFormat(
4805       "ShortObject->shortFunction(\n"
4806       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4807       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4808 
4809   verifyFormat("loooooooooooooongFunction(\n"
4810                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4811 
4812   verifyFormat(
4813       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4814       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4815 
4816   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4817                "    .WillRepeatedly(Return(SomeValue));");
4818   verifyFormat("void f() {\n"
4819                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4820                "      .Times(2)\n"
4821                "      .WillRepeatedly(Return(SomeValue));\n"
4822                "}");
4823   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4824                "    ccccccccccccccccccccccc);");
4825   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4826                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
4827                "      aaaaaaaaaaaaaaaaaaaaa);");
4828   verifyFormat("void f() {\n"
4829                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4830                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4831                "}");
4832   verifyFormat(
4833       "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4834       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4835       "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4836       "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4837       "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4838   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4839                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4840                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4841                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4842                "}");
4843 
4844   // Here, it is not necessary to wrap at "." or "->".
4845   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4846                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4847   verifyFormat(
4848       "aaaaaaaaaaa->aaaaaaaaa(\n"
4849       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4850       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4851 
4852   verifyFormat(
4853       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4854       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4855   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4856                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4857   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4858                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4859 
4860   // FIXME: Should we break before .a()?
4861   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4862                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
4863 
4864   FormatStyle NoBinPacking = getLLVMStyle();
4865   NoBinPacking.BinPackParameters = false;
4866   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4867                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4868                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4869                "                         aaaaaaaaaaaaaaaaaaa,\n"
4870                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4871                NoBinPacking);
4872 
4873   // If there is a subsequent call, change to hanging indentation.
4874   verifyFormat(
4875       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4876       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4877       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4878   verifyFormat(
4879       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4880       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4881   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4882                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4883                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4884   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4885                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4886                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4887 }
4888 
4889 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4890   verifyFormat("template <typename T>\n"
4891                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4892   verifyFormat("template <typename T>\n"
4893                "// T should be one of {A, B}.\n"
4894                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4895   verifyFormat(
4896       "template <typename T>\n"
4897       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4898   verifyFormat("template <typename T>\n"
4899                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4900                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4901   verifyFormat(
4902       "template <typename T>\n"
4903       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4904       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4905   verifyFormat(
4906       "template <typename T>\n"
4907       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4908       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4909       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4910   verifyFormat("template <typename T>\n"
4911                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4912                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4913   verifyFormat(
4914       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4915       "          typename T4 = char>\n"
4916       "void f();");
4917   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4918                "          template <typename> class cccccccccccccccccccccc,\n"
4919                "          typename ddddddddddddd>\n"
4920                "class C {};");
4921   verifyFormat(
4922       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4923       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4924 
4925   verifyFormat("void f() {\n"
4926                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4927                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4928                "}");
4929 
4930   verifyFormat("template <typename T> class C {};");
4931   verifyFormat("template <typename T> void f();");
4932   verifyFormat("template <typename T> void f() {}");
4933   verifyFormat(
4934       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4935       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4936       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4937       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4938       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4939       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4940       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4941       getLLVMStyleWithColumns(72));
4942   EXPECT_EQ("static_cast<A< //\n"
4943             "    B> *>(\n"
4944             "\n"
4945             "    );",
4946             format("static_cast<A<//\n"
4947                    "    B>*>(\n"
4948                    "\n"
4949                    "    );"));
4950   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4951                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
4952 
4953   FormatStyle AlwaysBreak = getLLVMStyle();
4954   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4955   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4956   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4957   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4958   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4959                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4960                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4961   verifyFormat("template <template <typename> class Fooooooo,\n"
4962                "          template <typename> class Baaaaaaar>\n"
4963                "struct C {};",
4964                AlwaysBreak);
4965   verifyFormat("template <typename T> // T can be A, B or C.\n"
4966                "struct C {};",
4967                AlwaysBreak);
4968 }
4969 
4970 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4971   verifyFormat(
4972       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4973       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4974   verifyFormat(
4975       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4976       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4977       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4978 
4979   // FIXME: Should we have the extra indent after the second break?
4980   verifyFormat(
4981       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4982       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4983       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4984 
4985   verifyFormat(
4986       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4987       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4988 
4989   // Breaking at nested name specifiers is generally not desirable.
4990   verifyFormat(
4991       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4992       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4993 
4994   verifyFormat(
4995       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4996       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4997       "                   aaaaaaaaaaaaaaaaaaaaa);",
4998       getLLVMStyleWithColumns(74));
4999 
5000   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5001                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5002                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5003 }
5004 
5005 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5006   verifyFormat("A<int> a;");
5007   verifyFormat("A<A<A<int>>> a;");
5008   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5009   verifyFormat("bool x = a < 1 || 2 > a;");
5010   verifyFormat("bool x = 5 < f<int>();");
5011   verifyFormat("bool x = f<int>() > 5;");
5012   verifyFormat("bool x = 5 < a<int>::x;");
5013   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5014   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5015 
5016   verifyGoogleFormat("A<A<int>> a;");
5017   verifyGoogleFormat("A<A<A<int>>> a;");
5018   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5019   verifyGoogleFormat("A<A<int> > a;");
5020   verifyGoogleFormat("A<A<A<int> > > a;");
5021   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5022   verifyGoogleFormat("A<::A<int>> a;");
5023   verifyGoogleFormat("A<::A> a;");
5024   verifyGoogleFormat("A< ::A> a;");
5025   verifyGoogleFormat("A< ::A<int> > a;");
5026   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5027   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5028   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5029   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5030 
5031   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5032 
5033   verifyFormat("test >> a >> b;");
5034   verifyFormat("test << a >> b;");
5035 
5036   verifyFormat("f<int>();");
5037   verifyFormat("template <typename T> void f() {}");
5038   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5039 
5040   // Not template parameters.
5041   verifyFormat("return a < b && c > d;");
5042   verifyFormat("void f() {\n"
5043                "  while (a < b && c > d) {\n"
5044                "  }\n"
5045                "}");
5046   verifyFormat("template <typename... Types>\n"
5047                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5048 
5049   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5050                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5051                getLLVMStyleWithColumns(60));
5052   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5053   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5054 }
5055 
5056 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5057   verifyFormat("COMPARE(a, ==, b);");
5058 }
5059 
5060 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5061   verifyFormat("int A::*x;");
5062   verifyFormat("int (S::*func)(void *);");
5063   verifyFormat("void f() { int (S::*func)(void *); }");
5064   verifyFormat("typedef bool *(Class::*Member)() const;");
5065   verifyFormat("void f() {\n"
5066                "  (a->*f)();\n"
5067                "  a->*x;\n"
5068                "  (a.*f)();\n"
5069                "  ((*a).*f)();\n"
5070                "  a.*x;\n"
5071                "}");
5072   verifyFormat("void f() {\n"
5073                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5074                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5075                "}");
5076   verifyFormat(
5077       "(aaaaaaaaaa->*bbbbbbb)(\n"
5078       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5079   FormatStyle Style = getLLVMStyle();
5080   Style.PointerAlignment = FormatStyle::PAS_Left;
5081   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5082 }
5083 
5084 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5085   verifyFormat("int a = -2;");
5086   verifyFormat("f(-1, -2, -3);");
5087   verifyFormat("a[-1] = 5;");
5088   verifyFormat("int a = 5 + -2;");
5089   verifyFormat("if (i == -1) {\n}");
5090   verifyFormat("if (i != -1) {\n}");
5091   verifyFormat("if (i > -1) {\n}");
5092   verifyFormat("if (i < -1) {\n}");
5093   verifyFormat("++(a->f());");
5094   verifyFormat("--(a->f());");
5095   verifyFormat("(a->f())++;");
5096   verifyFormat("a[42]++;");
5097   verifyFormat("if (!(a->f())) {\n}");
5098 
5099   verifyFormat("a-- > b;");
5100   verifyFormat("b ? -a : c;");
5101   verifyFormat("n * sizeof char16;");
5102   verifyFormat("n * alignof char16;", getGoogleStyle());
5103   verifyFormat("sizeof(char);");
5104   verifyFormat("alignof(char);", getGoogleStyle());
5105 
5106   verifyFormat("return -1;");
5107   verifyFormat("switch (a) {\n"
5108                "case -1:\n"
5109                "  break;\n"
5110                "}");
5111   verifyFormat("#define X -1");
5112   verifyFormat("#define X -kConstant");
5113 
5114   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5115   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5116 
5117   verifyFormat("int a = /* confusing comment */ -1;");
5118   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5119   verifyFormat("int a = i /* confusing comment */++;");
5120 }
5121 
5122 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5123   verifyFormat("if (!aaaaaaaaaa( // break\n"
5124                "        aaaaa)) {\n"
5125                "}");
5126   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5127                "               aaaaa));");
5128   verifyFormat("*aaa = aaaaaaa( // break\n"
5129                "    bbbbbb);");
5130 }
5131 
5132 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5133   verifyFormat("bool operator<();");
5134   verifyFormat("bool operator>();");
5135   verifyFormat("bool operator=();");
5136   verifyFormat("bool operator==();");
5137   verifyFormat("bool operator!=();");
5138   verifyFormat("int operator+();");
5139   verifyFormat("int operator++();");
5140   verifyFormat("bool operator();");
5141   verifyFormat("bool operator()();");
5142   verifyFormat("bool operator[]();");
5143   verifyFormat("operator bool();");
5144   verifyFormat("operator int();");
5145   verifyFormat("operator void *();");
5146   verifyFormat("operator SomeType<int>();");
5147   verifyFormat("operator SomeType<int, int>();");
5148   verifyFormat("operator SomeType<SomeType<int>>();");
5149   verifyFormat("void *operator new(std::size_t size);");
5150   verifyFormat("void *operator new[](std::size_t size);");
5151   verifyFormat("void operator delete(void *ptr);");
5152   verifyFormat("void operator delete[](void *ptr);");
5153   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5154                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5155 
5156   verifyFormat(
5157       "ostream &operator<<(ostream &OutputStream,\n"
5158       "                    SomeReallyLongType WithSomeReallyLongValue);");
5159   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5160                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5161                "  return left.group < right.group;\n"
5162                "}");
5163   verifyFormat("SomeType &operator=(const SomeType &S);");
5164   verifyFormat("f.template operator()<int>();");
5165 
5166   verifyGoogleFormat("operator void*();");
5167   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5168   verifyGoogleFormat("operator ::A();");
5169 
5170   verifyFormat("using A::operator+;");
5171 
5172   verifyFormat("string // break\n"
5173                "operator()() & {}");
5174   verifyFormat("string // break\n"
5175                "operator()() && {}");
5176 }
5177 
5178 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5179   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5180   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5181   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5182   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5183   verifyFormat("Deleted &operator=(const Deleted &)&;");
5184   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5185   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5186   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5187 
5188   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5189   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5190   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5191   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5192 
5193   FormatStyle Spaces = getLLVMStyle();
5194   Spaces.SpacesInCStyleCastParentheses = true;
5195   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5196   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5197   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5198   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5199 
5200   Spaces.SpacesInCStyleCastParentheses = false;
5201   Spaces.SpacesInParentheses = true;
5202   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5203   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5204   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5205   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5206 }
5207 
5208 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5209   verifyFormat("void f() {\n"
5210                "  A *a = new A;\n"
5211                "  A *a = new (placement) A;\n"
5212                "  delete a;\n"
5213                "  delete (A *)a;\n"
5214                "}");
5215   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5216                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5217   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5218                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5219                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5220   verifyFormat("delete[] h->p;");
5221 }
5222 
5223 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5224   verifyFormat("int *f(int *a) {}");
5225   verifyFormat("int main(int argc, char **argv) {}");
5226   verifyFormat("Test::Test(int b) : a(b * b) {}");
5227   verifyIndependentOfContext("f(a, *a);");
5228   verifyFormat("void g() { f(*a); }");
5229   verifyIndependentOfContext("int a = b * 10;");
5230   verifyIndependentOfContext("int a = 10 * b;");
5231   verifyIndependentOfContext("int a = b * c;");
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;");
5237   verifyIndependentOfContext("int a = *b * c;");
5238   verifyIndependentOfContext("int a = b * *c;");
5239   verifyIndependentOfContext("return 10 * b;");
5240   verifyIndependentOfContext("return *b * *c;");
5241   verifyIndependentOfContext("return a & ~b;");
5242   verifyIndependentOfContext("f(b ? *c : *d);");
5243   verifyIndependentOfContext("int a = b ? *c : *d;");
5244   verifyIndependentOfContext("*b = a;");
5245   verifyIndependentOfContext("a * ~b;");
5246   verifyIndependentOfContext("a * !b;");
5247   verifyIndependentOfContext("a * +b;");
5248   verifyIndependentOfContext("a * -b;");
5249   verifyIndependentOfContext("a * ++b;");
5250   verifyIndependentOfContext("a * --b;");
5251   verifyIndependentOfContext("a[4] * b;");
5252   verifyIndependentOfContext("a[a * a] = 1;");
5253   verifyIndependentOfContext("f() * b;");
5254   verifyIndependentOfContext("a * [self dostuff];");
5255   verifyIndependentOfContext("int x = a * (a + b);");
5256   verifyIndependentOfContext("(a *)(a + b);");
5257   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5258   verifyIndependentOfContext("int *pa = (int *)&a;");
5259   verifyIndependentOfContext("return sizeof(int **);");
5260   verifyIndependentOfContext("return sizeof(int ******);");
5261   verifyIndependentOfContext("return (int **&)a;");
5262   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5263   verifyFormat("void f(Type (*parameter)[10]) {}");
5264   verifyGoogleFormat("return sizeof(int**);");
5265   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5266   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5267   verifyFormat("auto a = [](int **&, int ***) {};");
5268   verifyFormat("auto PointerBinding = [](const char *S) {};");
5269   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5270   verifyFormat("[](const decltype(*a) &value) {}");
5271   verifyIndependentOfContext("typedef void (*f)(int *a);");
5272   verifyIndependentOfContext("int i{a * b};");
5273   verifyIndependentOfContext("aaa && aaa->f();");
5274   verifyIndependentOfContext("int x = ~*p;");
5275   verifyFormat("Constructor() : a(a), area(width * height) {}");
5276   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5277   verifyFormat("void f() { f(a, c * d); }");
5278 
5279   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5280 
5281   verifyIndependentOfContext("A<int *> a;");
5282   verifyIndependentOfContext("A<int **> a;");
5283   verifyIndependentOfContext("A<int *, int *> a;");
5284   verifyIndependentOfContext("A<int *[]> a;");
5285   verifyIndependentOfContext(
5286       "const char *const p = reinterpret_cast<const char *const>(q);");
5287   verifyIndependentOfContext("A<int **, int **> a;");
5288   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5289   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5290   verifyFormat("for (; a && b;) {\n}");
5291   verifyFormat("bool foo = true && [] { return false; }();");
5292 
5293   verifyFormat(
5294       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5295       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5296 
5297   verifyGoogleFormat("**outparam = 1;");
5298   verifyGoogleFormat("*outparam = a * b;");
5299   verifyGoogleFormat("int main(int argc, char** argv) {}");
5300   verifyGoogleFormat("A<int*> a;");
5301   verifyGoogleFormat("A<int**> a;");
5302   verifyGoogleFormat("A<int*, int*> a;");
5303   verifyGoogleFormat("A<int**, int**> a;");
5304   verifyGoogleFormat("f(b ? *c : *d);");
5305   verifyGoogleFormat("int a = b ? *c : *d;");
5306   verifyGoogleFormat("Type* t = **x;");
5307   verifyGoogleFormat("Type* t = *++*x;");
5308   verifyGoogleFormat("*++*x;");
5309   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5310   verifyGoogleFormat("Type* t = x++ * y;");
5311   verifyGoogleFormat(
5312       "const char* const p = reinterpret_cast<const char* const>(q);");
5313   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5314   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5315   verifyGoogleFormat("template <typename T>\n"
5316                      "void f(int i = 0, SomeType** temps = NULL);");
5317 
5318   FormatStyle Left = getLLVMStyle();
5319   Left.PointerAlignment = FormatStyle::PAS_Left;
5320   verifyFormat("x = *a(x) = *a(y);", Left);
5321 
5322   verifyIndependentOfContext("a = *(x + y);");
5323   verifyIndependentOfContext("a = &(x + y);");
5324   verifyIndependentOfContext("*(x + y).call();");
5325   verifyIndependentOfContext("&(x + y)->call();");
5326   verifyFormat("void f() { &(*I).first; }");
5327 
5328   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5329   verifyFormat(
5330       "int *MyValues = {\n"
5331       "    *A, // Operator detection might be confused by the '{'\n"
5332       "    *BB // Operator detection might be confused by previous comment\n"
5333       "};");
5334 
5335   verifyIndependentOfContext("if (int *a = &b)");
5336   verifyIndependentOfContext("if (int &a = *b)");
5337   verifyIndependentOfContext("if (a & b[i])");
5338   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5339   verifyIndependentOfContext("if (*b[i])");
5340   verifyIndependentOfContext("if (int *a = (&b))");
5341   verifyIndependentOfContext("while (int *a = &b)");
5342   verifyIndependentOfContext("size = sizeof *a;");
5343   verifyFormat("void f() {\n"
5344                "  for (const int &v : Values) {\n"
5345                "  }\n"
5346                "}");
5347   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5348   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5349   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5350 
5351   verifyFormat("#define A (!a * b)");
5352   verifyFormat("#define MACRO     \\\n"
5353                "  int *i = a * b; \\\n"
5354                "  void f(a *b);",
5355                getLLVMStyleWithColumns(19));
5356 
5357   verifyIndependentOfContext("A = new SomeType *[Length];");
5358   verifyIndependentOfContext("A = new SomeType *[Length]();");
5359   verifyIndependentOfContext("T **t = new T *;");
5360   verifyIndependentOfContext("T **t = new T *();");
5361   verifyGoogleFormat("A = new SomeType* [Length]();");
5362   verifyGoogleFormat("A = new SomeType* [Length];");
5363   verifyGoogleFormat("T** t = new T*;");
5364   verifyGoogleFormat("T** t = new T*();");
5365 
5366   FormatStyle PointerLeft = getLLVMStyle();
5367   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5368   verifyFormat("delete *x;", PointerLeft);
5369   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5370   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5371   verifyFormat("template <bool a, bool b> "
5372                "typename t::if<x && y>::type f() {}");
5373   verifyFormat("template <int *y> f() {}");
5374   verifyFormat("vector<int *> v;");
5375   verifyFormat("vector<int *const> v;");
5376   verifyFormat("vector<int *const **const *> v;");
5377   verifyFormat("vector<int *volatile> v;");
5378   verifyFormat("vector<a * b> v;");
5379   verifyFormat("foo<b && false>();");
5380   verifyFormat("foo<b & 1>();");
5381   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5382   verifyFormat(
5383       "template <class T, class = typename std::enable_if<\n"
5384       "                       std::is_integral<T>::value &&\n"
5385       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5386       "void F();",
5387       getLLVMStyleWithColumns(76));
5388   verifyFormat(
5389       "template <class T,\n"
5390       "          class = typename ::std::enable_if<\n"
5391       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5392       "void F();",
5393       getGoogleStyleWithColumns(68));
5394 
5395   verifyIndependentOfContext("MACRO(int *i);");
5396   verifyIndependentOfContext("MACRO(auto *a);");
5397   verifyIndependentOfContext("MACRO(const A *a);");
5398   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5399   // FIXME: Is there a way to make this work?
5400   // verifyIndependentOfContext("MACRO(A *a);");
5401 
5402   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5403 
5404   EXPECT_EQ("#define OP(x)                                    \\\n"
5405             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5406             "    return s << a.DebugString();                 \\\n"
5407             "  }",
5408             format("#define OP(x) \\\n"
5409                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5410                    "    return s << a.DebugString(); \\\n"
5411                    "  }",
5412                    getLLVMStyleWithColumns(50)));
5413 
5414   // FIXME: We cannot handle this case yet; we might be able to figure out that
5415   // foo<x> d > v; doesn't make sense.
5416   verifyFormat("foo<a<b && c> d> v;");
5417 
5418   FormatStyle PointerMiddle = getLLVMStyle();
5419   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5420   verifyFormat("delete *x;", PointerMiddle);
5421   verifyFormat("int * x;", PointerMiddle);
5422   verifyFormat("template <int * y> f() {}", PointerMiddle);
5423   verifyFormat("int * f(int * a) {}", PointerMiddle);
5424   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5425   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5426   verifyFormat("A<int *> a;", PointerMiddle);
5427   verifyFormat("A<int **> a;", PointerMiddle);
5428   verifyFormat("A<int *, int *> a;", PointerMiddle);
5429   verifyFormat("A<int * []> a;", PointerMiddle);
5430   verifyFormat("A = new SomeType * [Length]();", PointerMiddle);
5431   verifyFormat("A = new SomeType * [Length];", PointerMiddle);
5432   verifyFormat("T ** t = new T *;", PointerMiddle);
5433 }
5434 
5435 TEST_F(FormatTest, UnderstandsAttributes) {
5436   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5437   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5438                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5439 }
5440 
5441 TEST_F(FormatTest, UnderstandsEllipsis) {
5442   verifyFormat("int printf(const char *fmt, ...);");
5443   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5444   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5445 
5446   FormatStyle PointersLeft = getLLVMStyle();
5447   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5448   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5449 }
5450 
5451 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5452   EXPECT_EQ("int *a;\n"
5453             "int *a;\n"
5454             "int *a;",
5455             format("int *a;\n"
5456                    "int* a;\n"
5457                    "int *a;",
5458                    getGoogleStyle()));
5459   EXPECT_EQ("int* a;\n"
5460             "int* a;\n"
5461             "int* a;",
5462             format("int* a;\n"
5463                    "int* a;\n"
5464                    "int *a;",
5465                    getGoogleStyle()));
5466   EXPECT_EQ("int *a;\n"
5467             "int *a;\n"
5468             "int *a;",
5469             format("int *a;\n"
5470                    "int * a;\n"
5471                    "int *  a;",
5472                    getGoogleStyle()));
5473 }
5474 
5475 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5476   verifyFormat("int f(int &&a) {}");
5477   verifyFormat("int f(int a, char &&b) {}");
5478   verifyFormat("void f() { int &&a = b; }");
5479   verifyGoogleFormat("int f(int a, char&& b) {}");
5480   verifyGoogleFormat("void f() { int&& a = b; }");
5481 
5482   verifyIndependentOfContext("A<int &&> a;");
5483   verifyIndependentOfContext("A<int &&, int &&> a;");
5484   verifyGoogleFormat("A<int&&> a;");
5485   verifyGoogleFormat("A<int&&, int&&> a;");
5486 
5487   // Not rvalue references:
5488   verifyFormat("template <bool B, bool C> class A {\n"
5489                "  static_assert(B && C, \"Something is wrong\");\n"
5490                "};");
5491   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5492   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5493   verifyFormat("#define A(a, b) (a && b)");
5494 }
5495 
5496 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5497   verifyFormat("void f() {\n"
5498                "  x[aaaaaaaaa -\n"
5499                "    b] = 23;\n"
5500                "}",
5501                getLLVMStyleWithColumns(15));
5502 }
5503 
5504 TEST_F(FormatTest, FormatsCasts) {
5505   verifyFormat("Type *A = static_cast<Type *>(P);");
5506   verifyFormat("Type *A = (Type *)P;");
5507   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5508   verifyFormat("int a = (int)(2.0f);");
5509   verifyFormat("int a = (int)2.0f;");
5510   verifyFormat("x[(int32)y];");
5511   verifyFormat("x = (int32)y;");
5512   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5513   verifyFormat("int a = (int)*b;");
5514   verifyFormat("int a = (int)2.0f;");
5515   verifyFormat("int a = (int)~0;");
5516   verifyFormat("int a = (int)++a;");
5517   verifyFormat("int a = (int)sizeof(int);");
5518   verifyFormat("int a = (int)+2;");
5519   verifyFormat("my_int a = (my_int)2.0f;");
5520   verifyFormat("my_int a = (my_int)sizeof(int);");
5521   verifyFormat("return (my_int)aaa;");
5522   verifyFormat("#define x ((int)-1)");
5523   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5524   verifyFormat("#define p(q) ((int *)&q)");
5525   verifyFormat("fn(a)(b) + 1;");
5526 
5527   verifyFormat("void f() { my_int a = (my_int)*b; }");
5528   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5529   verifyFormat("my_int a = (my_int)~0;");
5530   verifyFormat("my_int a = (my_int)++a;");
5531   verifyFormat("my_int a = (my_int)-2;");
5532   verifyFormat("my_int a = (my_int)1;");
5533   verifyFormat("my_int a = (my_int *)1;");
5534   verifyFormat("my_int a = (const my_int)-1;");
5535   verifyFormat("my_int a = (const my_int *)-1;");
5536   verifyFormat("my_int a = (my_int)(my_int)-1;");
5537 
5538   // FIXME: single value wrapped with paren will be treated as cast.
5539   verifyFormat("void f(int i = (kValue)*kMask) {}");
5540 
5541   verifyFormat("{ (void)F; }");
5542 
5543   // Don't break after a cast's
5544   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5545                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5546                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5547 
5548   // These are not casts.
5549   verifyFormat("void f(int *) {}");
5550   verifyFormat("f(foo)->b;");
5551   verifyFormat("f(foo).b;");
5552   verifyFormat("f(foo)(b);");
5553   verifyFormat("f(foo)[b];");
5554   verifyFormat("[](foo) { return 4; }(bar);");
5555   verifyFormat("(*funptr)(foo)[4];");
5556   verifyFormat("funptrs[4](foo)[4];");
5557   verifyFormat("void f(int *);");
5558   verifyFormat("void f(int *) = 0;");
5559   verifyFormat("void f(SmallVector<int>) {}");
5560   verifyFormat("void f(SmallVector<int>);");
5561   verifyFormat("void f(SmallVector<int>) = 0;");
5562   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5563   verifyFormat("int a = sizeof(int) * b;");
5564   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5565   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5566   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5567   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5568 
5569   // These are not casts, but at some point were confused with casts.
5570   verifyFormat("virtual void foo(int *) override;");
5571   verifyFormat("virtual void foo(char &) const;");
5572   verifyFormat("virtual void foo(int *a, char *) const;");
5573   verifyFormat("int a = sizeof(int *) + b;");
5574   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5575 
5576   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5577                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5578   // FIXME: The indentation here is not ideal.
5579   verifyFormat(
5580       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5581       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5582       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5583 }
5584 
5585 TEST_F(FormatTest, FormatsFunctionTypes) {
5586   verifyFormat("A<bool()> a;");
5587   verifyFormat("A<SomeType()> a;");
5588   verifyFormat("A<void (*)(int, std::string)> a;");
5589   verifyFormat("A<void *(int)>;");
5590   verifyFormat("void *(*a)(int *, SomeType *);");
5591   verifyFormat("int (*func)(void *);");
5592   verifyFormat("void f() { int (*func)(void *); }");
5593   verifyFormat("template <class CallbackClass>\n"
5594                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5595 
5596   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5597   verifyGoogleFormat("void* (*a)(int);");
5598   verifyGoogleFormat(
5599       "template <class CallbackClass>\n"
5600       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5601 
5602   // Other constructs can look somewhat like function types:
5603   verifyFormat("A<sizeof(*x)> a;");
5604   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5605   verifyFormat("some_var = function(*some_pointer_var)[0];");
5606   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5607 }
5608 
5609 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5610   verifyFormat("A (*foo_)[6];");
5611   verifyFormat("vector<int> (*foo_)[6];");
5612 }
5613 
5614 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5615   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5616                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5617   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5618                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5619 
5620   // Different ways of ()-initializiation.
5621   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5622                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5623   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5624                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5625   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5626                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5627 }
5628 
5629 TEST_F(FormatTest, BreaksLongDeclarations) {
5630   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5631                "    AnotherNameForTheLongType;");
5632   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5633                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5634   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5635                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5636   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5637                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5638   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5639                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5640   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5641                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5642   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5643                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5644   FormatStyle Indented = getLLVMStyle();
5645   Indented.IndentWrappedFunctionNames = true;
5646   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5647                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5648                Indented);
5649   verifyFormat(
5650       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5651       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5652       Indented);
5653   verifyFormat(
5654       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5655       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5656       Indented);
5657   verifyFormat(
5658       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5659       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5660       Indented);
5661 
5662   // FIXME: Without the comment, this breaks after "(".
5663   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5664                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5665                getGoogleStyle());
5666 
5667   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5668                "                  int LoooooooooooooooooooongParam2) {}");
5669   verifyFormat(
5670       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5671       "                                   SourceLocation L, IdentifierIn *II,\n"
5672       "                                   Type *T) {}");
5673   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5674                "ReallyReallyLongFunctionName(\n"
5675                "    const std::string &SomeParameter,\n"
5676                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
5677                "        ReallyReallyLongParameterName,\n"
5678                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
5679                "        AnotherLongParameterName) {}");
5680   verifyFormat("template <typename A>\n"
5681                "SomeLoooooooooooooooooooooongType<\n"
5682                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5683                "Function() {}");
5684 
5685   verifyGoogleFormat(
5686       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5687       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5688   verifyGoogleFormat(
5689       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5690       "                                   SourceLocation L) {}");
5691   verifyGoogleFormat(
5692       "some_namespace::LongReturnType\n"
5693       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5694       "    int first_long_parameter, int second_parameter) {}");
5695 
5696   verifyGoogleFormat("template <typename T>\n"
5697                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5698                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5699   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5700                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5701 
5702   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5703                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
5704                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5705 }
5706 
5707 TEST_F(FormatTest, FormatsArrays) {
5708   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5709                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5710   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5711                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5712   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5713                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5714   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5715                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5716                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5717   verifyFormat(
5718       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5719       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5720       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5721 
5722   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5723                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5724   verifyFormat(
5725       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5726       "                                  .aaaaaaa[0]\n"
5727       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5728 
5729   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5730 }
5731 
5732 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5733   verifyFormat("(a)->b();");
5734   verifyFormat("--a;");
5735 }
5736 
5737 TEST_F(FormatTest, HandlesIncludeDirectives) {
5738   verifyFormat("#include <string>\n"
5739                "#include <a/b/c.h>\n"
5740                "#include \"a/b/string\"\n"
5741                "#include \"string.h\"\n"
5742                "#include \"string.h\"\n"
5743                "#include <a-a>\n"
5744                "#include < path with space >\n"
5745                "#include \"abc.h\" // this is included for ABC\n"
5746                "#include \"some long include\" // with a comment\n"
5747                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5748                getLLVMStyleWithColumns(35));
5749   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5750   EXPECT_EQ("#include <a>", format("#include<a>"));
5751 
5752   verifyFormat("#import <string>");
5753   verifyFormat("#import <a/b/c.h>");
5754   verifyFormat("#import \"a/b/string\"");
5755   verifyFormat("#import \"string.h\"");
5756   verifyFormat("#import \"string.h\"");
5757   verifyFormat("#if __has_include(<strstream>)\n"
5758                "#include <strstream>\n"
5759                "#endif");
5760 
5761   verifyFormat("#define MY_IMPORT <a/b>");
5762 
5763   // Protocol buffer definition or missing "#".
5764   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5765                getLLVMStyleWithColumns(30));
5766 
5767   FormatStyle Style = getLLVMStyle();
5768   Style.AlwaysBreakBeforeMultilineStrings = true;
5769   Style.ColumnLimit = 0;
5770   verifyFormat("#import \"abc.h\"", Style);
5771 
5772   // But 'import' might also be a regular C++ namespace.
5773   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5774                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5775 }
5776 
5777 //===----------------------------------------------------------------------===//
5778 // Error recovery tests.
5779 //===----------------------------------------------------------------------===//
5780 
5781 TEST_F(FormatTest, IncompleteParameterLists) {
5782   FormatStyle NoBinPacking = getLLVMStyle();
5783   NoBinPacking.BinPackParameters = false;
5784   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5785                "                        double *min_x,\n"
5786                "                        double *max_x,\n"
5787                "                        double *min_y,\n"
5788                "                        double *max_y,\n"
5789                "                        double *min_z,\n"
5790                "                        double *max_z, ) {}",
5791                NoBinPacking);
5792 }
5793 
5794 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5795   verifyFormat("void f() { return; }\n42");
5796   verifyFormat("void f() {\n"
5797                "  if (0)\n"
5798                "    return;\n"
5799                "}\n"
5800                "42");
5801   verifyFormat("void f() { return }\n42");
5802   verifyFormat("void f() {\n"
5803                "  if (0)\n"
5804                "    return\n"
5805                "}\n"
5806                "42");
5807 }
5808 
5809 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5810   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5811   EXPECT_EQ("void f() {\n"
5812             "  if (a)\n"
5813             "    return\n"
5814             "}",
5815             format("void  f  (  )  {  if  ( a )  return  }"));
5816   EXPECT_EQ("namespace N {\n"
5817             "void f()\n"
5818             "}",
5819             format("namespace  N  {  void f()  }"));
5820   EXPECT_EQ("namespace N {\n"
5821             "void f() {}\n"
5822             "void g()\n"
5823             "}",
5824             format("namespace N  { void f( ) { } void g( ) }"));
5825 }
5826 
5827 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5828   verifyFormat("int aaaaaaaa =\n"
5829                "    // Overlylongcomment\n"
5830                "    b;",
5831                getLLVMStyleWithColumns(20));
5832   verifyFormat("function(\n"
5833                "    ShortArgument,\n"
5834                "    LoooooooooooongArgument);\n",
5835                getLLVMStyleWithColumns(20));
5836 }
5837 
5838 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5839   verifyFormat("public:");
5840   verifyFormat("class A {\n"
5841                "public\n"
5842                "  void f() {}\n"
5843                "};");
5844   verifyFormat("public\n"
5845                "int qwerty;");
5846   verifyFormat("public\n"
5847                "B {}");
5848   verifyFormat("public\n"
5849                "{}");
5850   verifyFormat("public\n"
5851                "B { int x; }");
5852 }
5853 
5854 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5855   verifyFormat("{");
5856   verifyFormat("#})");
5857   verifyNoCrash("(/**/[:!] ?[).");
5858 }
5859 
5860 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5861   verifyFormat("do {\n}");
5862   verifyFormat("do {\n}\n"
5863                "f();");
5864   verifyFormat("do {\n}\n"
5865                "wheeee(fun);");
5866   verifyFormat("do {\n"
5867                "  f();\n"
5868                "}");
5869 }
5870 
5871 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5872   verifyFormat("if {\n  foo;\n  foo();\n}");
5873   verifyFormat("switch {\n  foo;\n  foo();\n}");
5874   verifyFormat("for {\n  foo;\n  foo();\n}");
5875   verifyFormat("while {\n  foo;\n  foo();\n}");
5876   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5877 }
5878 
5879 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5880   verifyFormat("namespace {\n"
5881                "class Foo { Foo (\n"
5882                "};\n"
5883                "} // comment");
5884 }
5885 
5886 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5887   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5888   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5889   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5890   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5891 
5892   EXPECT_EQ("{\n"
5893             "  {\n"
5894             "    breakme(\n"
5895             "        qwe);\n"
5896             "  }\n",
5897             format("{\n"
5898                    "    {\n"
5899                    " breakme(qwe);\n"
5900                    "}\n",
5901                    getLLVMStyleWithColumns(10)));
5902 }
5903 
5904 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
5905   verifyFormat("int x = {\n"
5906                "    avariable,\n"
5907                "    b(alongervariable)};",
5908                getLLVMStyleWithColumns(25));
5909 }
5910 
5911 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
5912   verifyFormat("return (a)(b){1, 2, 3};");
5913 }
5914 
5915 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
5916   verifyFormat("vector<int> x{1, 2, 3, 4};");
5917   verifyFormat("vector<int> x{\n"
5918                "    1, 2, 3, 4,\n"
5919                "};");
5920   verifyFormat("vector<T> x{{}, {}, {}, {}};");
5921   verifyFormat("f({1, 2});");
5922   verifyFormat("auto v = Foo{-1};");
5923   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
5924   verifyFormat("Class::Class : member{1, 2, 3} {}");
5925   verifyFormat("new vector<int>{1, 2, 3};");
5926   verifyFormat("new int[3]{1, 2, 3};");
5927   verifyFormat("new int{1};");
5928   verifyFormat("return {arg1, arg2};");
5929   verifyFormat("return {arg1, SomeType{parameter}};");
5930   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5931   verifyFormat("new T{arg1, arg2};");
5932   verifyFormat("f(MyMap[{composite, key}]);");
5933   verifyFormat("class Class {\n"
5934                "  T member = {arg1, arg2};\n"
5935                "};");
5936   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5937   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
5938   verifyFormat("int a = std::is_integral<int>{} + 0;");
5939 
5940   verifyFormat("int foo(int i) { return fo1{}(i); }");
5941   verifyFormat("int foo(int i) { return fo1{}(i); }");
5942   verifyFormat("auto i = decltype(x){};");
5943   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
5944   verifyFormat("Node n{1, Node{1000}, //\n"
5945                "       2};");
5946 
5947   // In combination with BinPackParameters = false.
5948   FormatStyle NoBinPacking = getLLVMStyle();
5949   NoBinPacking.BinPackParameters = false;
5950   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
5951                "                      bbbbb,\n"
5952                "                      ccccc,\n"
5953                "                      ddddd,\n"
5954                "                      eeeee,\n"
5955                "                      ffffff,\n"
5956                "                      ggggg,\n"
5957                "                      hhhhhh,\n"
5958                "                      iiiiii,\n"
5959                "                      jjjjjj,\n"
5960                "                      kkkkkk};",
5961                NoBinPacking);
5962   verifyFormat("const Aaaaaa aaaaa = {\n"
5963                "    aaaaa,\n"
5964                "    bbbbb,\n"
5965                "    ccccc,\n"
5966                "    ddddd,\n"
5967                "    eeeee,\n"
5968                "    ffffff,\n"
5969                "    ggggg,\n"
5970                "    hhhhhh,\n"
5971                "    iiiiii,\n"
5972                "    jjjjjj,\n"
5973                "    kkkkkk,\n"
5974                "};",
5975                NoBinPacking);
5976   verifyFormat(
5977       "const Aaaaaa aaaaa = {\n"
5978       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
5979       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
5980       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
5981       "};",
5982       NoBinPacking);
5983 
5984   // FIXME: The alignment of these trailing comments might be bad. Then again,
5985   // this might be utterly useless in real code.
5986   verifyFormat("Constructor::Constructor()\n"
5987                "    : some_value{         //\n"
5988                "                 aaaaaaa, //\n"
5989                "                 bbbbbbb} {}");
5990 
5991   // In braced lists, the first comment is always assumed to belong to the
5992   // first element. Thus, it can be moved to the next or previous line as
5993   // appropriate.
5994   EXPECT_EQ("function({// First element:\n"
5995             "          1,\n"
5996             "          // Second element:\n"
5997             "          2});",
5998             format("function({\n"
5999                    "    // First element:\n"
6000                    "    1,\n"
6001                    "    // Second element:\n"
6002                    "    2});"));
6003   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6004             "    // First element:\n"
6005             "    1,\n"
6006             "    // Second element:\n"
6007             "    2};",
6008             format("std::vector<int> MyNumbers{// First element:\n"
6009                    "                           1,\n"
6010                    "                           // Second element:\n"
6011                    "                           2};",
6012                    getLLVMStyleWithColumns(30)));
6013   // A trailing comma should still lead to an enforced line break.
6014   EXPECT_EQ("vector<int> SomeVector = {\n"
6015             "    // aaa\n"
6016             "    1, 2,\n"
6017             "};",
6018             format("vector<int> SomeVector = { // aaa\n"
6019                    "    1, 2, };"));
6020 
6021   FormatStyle ExtraSpaces = getLLVMStyle();
6022   ExtraSpaces.Cpp11BracedListStyle = false;
6023   ExtraSpaces.ColumnLimit = 75;
6024   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6025   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6026   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6027   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6028   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6029   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6030   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6031   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6032   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6033   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6034   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6035   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6036   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6037   verifyFormat("class Class {\n"
6038                "  T member = { arg1, arg2 };\n"
6039                "};",
6040                ExtraSpaces);
6041   verifyFormat(
6042       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6043       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6044       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6045       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6046       ExtraSpaces);
6047   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6048   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6049                ExtraSpaces);
6050   verifyFormat(
6051       "someFunction(OtherParam,\n"
6052       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6053       "                         param1, param2,\n"
6054       "                         // comment 2\n"
6055       "                         param3, param4 });",
6056       ExtraSpaces);
6057   verifyFormat(
6058       "std::this_thread::sleep_for(\n"
6059       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6060       ExtraSpaces);
6061   verifyFormat(
6062       "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6063       "    aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
6064       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
6065       "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
6066   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6067 }
6068 
6069 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6070   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6071                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6072                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6073                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6074                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6075                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6076   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6077                "                 // line comment\n"
6078                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6079                "                 1, 22, 333, 4444, 55555,\n"
6080                "                 // line comment\n"
6081                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6082                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6083   verifyFormat(
6084       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6085       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6086       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6087       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6088       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6089       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6090       "                 7777777};");
6091   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6092                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6093                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6094   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6095                "                 1, 1, 1, 1};",
6096                getLLVMStyleWithColumns(39));
6097   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6098                "                 1, 1, 1, 1};",
6099                getLLVMStyleWithColumns(38));
6100   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6101                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6102                getLLVMStyleWithColumns(43));
6103 
6104   // Trailing commas.
6105   verifyFormat("vector<int> x = {\n"
6106                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6107                "};",
6108                getLLVMStyleWithColumns(39));
6109   verifyFormat("vector<int> x = {\n"
6110                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6111                "};",
6112                getLLVMStyleWithColumns(39));
6113   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6114                "                 1, 1, 1, 1,\n"
6115                "                 /**/ /**/};",
6116                getLLVMStyleWithColumns(39));
6117   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6118                "        {aaaaaaaaaaaaaaaaaaa},\n"
6119                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6120                "        {aaaaaaaaaaaaaaaaa}};",
6121                getLLVMStyleWithColumns(60));
6122 
6123   // With nested lists, we should either format one item per line or all nested
6124   // lists one one line.
6125   // FIXME: For some nested lists, we can do better.
6126   verifyFormat(
6127       "SomeStruct my_struct_array = {\n"
6128       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6129       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6130       "    {aaa, aaa},\n"
6131       "    {aaa, aaa},\n"
6132       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6133       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6134       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6135 
6136   // No column layout should be used here.
6137   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6138                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6139 
6140   verifyNoCrash("a<,");
6141 }
6142 
6143 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6144   FormatStyle DoNotMerge = getLLVMStyle();
6145   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6146 
6147   verifyFormat("void f() { return 42; }");
6148   verifyFormat("void f() {\n"
6149                "  return 42;\n"
6150                "}",
6151                DoNotMerge);
6152   verifyFormat("void f() {\n"
6153                "  // Comment\n"
6154                "}");
6155   verifyFormat("{\n"
6156                "#error {\n"
6157                "  int a;\n"
6158                "}");
6159   verifyFormat("{\n"
6160                "  int a;\n"
6161                "#error {\n"
6162                "}");
6163   verifyFormat("void f() {} // comment");
6164   verifyFormat("void f() { int a; } // comment");
6165   verifyFormat("void f() {\n"
6166                "} // comment",
6167                DoNotMerge);
6168   verifyFormat("void f() {\n"
6169                "  int a;\n"
6170                "} // comment",
6171                DoNotMerge);
6172   verifyFormat("void f() {\n"
6173                "} // comment",
6174                getLLVMStyleWithColumns(15));
6175 
6176   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6177   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6178 
6179   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6180   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6181   verifyFormat("class C {\n"
6182                "  C()\n"
6183                "      : iiiiiiii(nullptr),\n"
6184                "        kkkkkkk(nullptr),\n"
6185                "        mmmmmmm(nullptr),\n"
6186                "        nnnnnnn(nullptr) {}\n"
6187                "};",
6188                getGoogleStyle());
6189 
6190   FormatStyle NoColumnLimit = getLLVMStyle();
6191   NoColumnLimit.ColumnLimit = 0;
6192   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6193   EXPECT_EQ("class C {\n"
6194             "  A() : b(0) {}\n"
6195             "};", format("class C{A():b(0){}};", NoColumnLimit));
6196   EXPECT_EQ("A()\n"
6197             "    : b(0) {\n"
6198             "}",
6199             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6200 
6201   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6202   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6203       FormatStyle::SFS_None;
6204   EXPECT_EQ("A()\n"
6205             "    : b(0) {\n"
6206             "}",
6207             format("A():b(0){}", DoNotMergeNoColumnLimit));
6208   EXPECT_EQ("A()\n"
6209             "    : b(0) {\n"
6210             "}",
6211             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6212 
6213   verifyFormat("#define A          \\\n"
6214                "  void f() {       \\\n"
6215                "    int i;         \\\n"
6216                "  }",
6217                getLLVMStyleWithColumns(20));
6218   verifyFormat("#define A           \\\n"
6219                "  void f() { int i; }",
6220                getLLVMStyleWithColumns(21));
6221   verifyFormat("#define A            \\\n"
6222                "  void f() {         \\\n"
6223                "    int i;           \\\n"
6224                "  }                  \\\n"
6225                "  int j;",
6226                getLLVMStyleWithColumns(22));
6227   verifyFormat("#define A             \\\n"
6228                "  void f() { int i; } \\\n"
6229                "  int j;",
6230                getLLVMStyleWithColumns(23));
6231 }
6232 
6233 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6234   FormatStyle MergeInlineOnly = getLLVMStyle();
6235   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6236   verifyFormat("class C {\n"
6237                "  int f() { return 42; }\n"
6238                "};",
6239                MergeInlineOnly);
6240   verifyFormat("int f() {\n"
6241                "  return 42;\n"
6242                "}",
6243                MergeInlineOnly);
6244 }
6245 
6246 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6247   // Elaborate type variable declarations.
6248   verifyFormat("struct foo a = {bar};\nint n;");
6249   verifyFormat("class foo a = {bar};\nint n;");
6250   verifyFormat("union foo a = {bar};\nint n;");
6251 
6252   // Elaborate types inside function definitions.
6253   verifyFormat("struct foo f() {}\nint n;");
6254   verifyFormat("class foo f() {}\nint n;");
6255   verifyFormat("union foo f() {}\nint n;");
6256 
6257   // Templates.
6258   verifyFormat("template <class X> void f() {}\nint n;");
6259   verifyFormat("template <struct X> void f() {}\nint n;");
6260   verifyFormat("template <union X> void f() {}\nint n;");
6261 
6262   // Actual definitions...
6263   verifyFormat("struct {\n} n;");
6264   verifyFormat(
6265       "template <template <class T, class Y>, class Z> class X {\n} n;");
6266   verifyFormat("union Z {\n  int n;\n} x;");
6267   verifyFormat("class MACRO Z {\n} n;");
6268   verifyFormat("class MACRO(X) Z {\n} n;");
6269   verifyFormat("class __attribute__(X) Z {\n} n;");
6270   verifyFormat("class __declspec(X) Z {\n} n;");
6271   verifyFormat("class A##B##C {\n} n;");
6272   verifyFormat("class alignas(16) Z {\n} n;");
6273 
6274   // Redefinition from nested context:
6275   verifyFormat("class A::B::C {\n} n;");
6276 
6277   // Template definitions.
6278   verifyFormat(
6279       "template <typename F>\n"
6280       "Matcher(const Matcher<F> &Other,\n"
6281       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6282       "                             !is_same<F, T>::value>::type * = 0)\n"
6283       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6284 
6285   // FIXME: This is still incorrectly handled at the formatter side.
6286   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6287 
6288   // FIXME:
6289   // This now gets parsed incorrectly as class definition.
6290   // verifyFormat("class A<int> f() {\n}\nint n;");
6291 
6292   // Elaborate types where incorrectly parsing the structural element would
6293   // break the indent.
6294   verifyFormat("if (true)\n"
6295                "  class X x;\n"
6296                "else\n"
6297                "  f();\n");
6298 
6299   // This is simply incomplete. Formatting is not important, but must not crash.
6300   verifyFormat("class A:");
6301 }
6302 
6303 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6304   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6305             format("#error Leave     all         white!!!!! space* alone!\n"));
6306   EXPECT_EQ(
6307       "#warning Leave     all         white!!!!! space* alone!\n",
6308       format("#warning Leave     all         white!!!!! space* alone!\n"));
6309   EXPECT_EQ("#error 1", format("  #  error   1"));
6310   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6311 }
6312 
6313 TEST_F(FormatTest, FormatHashIfExpressions) {
6314   verifyFormat("#if AAAA && BBBB");
6315   // FIXME: Come up with a better indentation for #elif.
6316   verifyFormat(
6317       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6318       "    defined(BBBBBBBB)\n"
6319       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6320       "    defined(BBBBBBBB)\n"
6321       "#endif",
6322       getLLVMStyleWithColumns(65));
6323 }
6324 
6325 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6326   FormatStyle AllowsMergedIf = getGoogleStyle();
6327   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6328   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6329   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6330   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6331   EXPECT_EQ("if (true) return 42;",
6332             format("if (true)\nreturn 42;", AllowsMergedIf));
6333   FormatStyle ShortMergedIf = AllowsMergedIf;
6334   ShortMergedIf.ColumnLimit = 25;
6335   verifyFormat("#define A \\\n"
6336                "  if (true) return 42;",
6337                ShortMergedIf);
6338   verifyFormat("#define A \\\n"
6339                "  f();    \\\n"
6340                "  if (true)\n"
6341                "#define B",
6342                ShortMergedIf);
6343   verifyFormat("#define A \\\n"
6344                "  f();    \\\n"
6345                "  if (true)\n"
6346                "g();",
6347                ShortMergedIf);
6348   verifyFormat("{\n"
6349                "#ifdef A\n"
6350                "  // Comment\n"
6351                "  if (true) continue;\n"
6352                "#endif\n"
6353                "  // Comment\n"
6354                "  if (true) continue;\n"
6355                "}",
6356                ShortMergedIf);
6357   ShortMergedIf.ColumnLimit = 29;
6358   verifyFormat("#define A                   \\\n"
6359                "  if (aaaaaaaaaa) return 1; \\\n"
6360                "  return 2;",
6361                ShortMergedIf);
6362   ShortMergedIf.ColumnLimit = 28;
6363   verifyFormat("#define A         \\\n"
6364                "  if (aaaaaaaaaa) \\\n"
6365                "    return 1;     \\\n"
6366                "  return 2;",
6367                ShortMergedIf);
6368 }
6369 
6370 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6371   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6372                "  f();\n"
6373                "}");
6374   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6375                "  f();\n"
6376                "} /* another comment */ else /* comment #3 */ {\n"
6377                "  g();\n"
6378                "}");
6379   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6380                "  f();\n"
6381                "}");
6382   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6383                "  f();\n"
6384                "}");
6385   verifyFormat("do /* a comment in a strange place */ {\n"
6386                "  f();\n"
6387                "} /* another comment */ while (0);");
6388 }
6389 
6390 TEST_F(FormatTest, BlockComments) {
6391   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6392             format("/* *//* */  /* */\n/* *//* */  /* */"));
6393   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6394   EXPECT_EQ("#define A /*123*/ \\\n"
6395             "  b\n"
6396             "/* */\n"
6397             "someCall(\n"
6398             "    parameter);",
6399             format("#define A /*123*/ b\n"
6400                    "/* */\n"
6401                    "someCall(parameter);",
6402                    getLLVMStyleWithColumns(15)));
6403 
6404   EXPECT_EQ("#define A\n"
6405             "/* */ someCall(\n"
6406             "    parameter);",
6407             format("#define A\n"
6408                    "/* */someCall(parameter);",
6409                    getLLVMStyleWithColumns(15)));
6410   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6411   EXPECT_EQ("/*\n"
6412             "*\n"
6413             " * aaaaaa\n"
6414             "*aaaaaa\n"
6415             "*/",
6416             format("/*\n"
6417                    "*\n"
6418                    " * aaaaaa aaaaaa\n"
6419                    "*/",
6420                    getLLVMStyleWithColumns(10)));
6421   EXPECT_EQ("/*\n"
6422             "**\n"
6423             "* aaaaaa\n"
6424             "*aaaaaa\n"
6425             "*/",
6426             format("/*\n"
6427                    "**\n"
6428                    "* aaaaaa aaaaaa\n"
6429                    "*/",
6430                    getLLVMStyleWithColumns(10)));
6431 
6432   FormatStyle NoBinPacking = getLLVMStyle();
6433   NoBinPacking.BinPackParameters = false;
6434   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6435             "             2, /* comment 2 */\n"
6436             "             3, /* comment 3 */\n"
6437             "             aaaa,\n"
6438             "             bbbb);",
6439             format("someFunction (1,   /* comment 1 */\n"
6440                    "                2, /* comment 2 */  \n"
6441                    "               3,   /* comment 3 */\n"
6442                    "aaaa, bbbb );",
6443                    NoBinPacking));
6444   verifyFormat(
6445       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6446       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6447   EXPECT_EQ(
6448       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6449       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6450       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6451       format(
6452           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6453           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6454           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6455   EXPECT_EQ(
6456       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6457       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6458       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6459       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6460              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6461              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6462 
6463   verifyFormat("void f(int * /* unused */) {}");
6464 
6465   EXPECT_EQ("/*\n"
6466             " **\n"
6467             " */",
6468             format("/*\n"
6469                    " **\n"
6470                    " */"));
6471   EXPECT_EQ("/*\n"
6472             " *q\n"
6473             " */",
6474             format("/*\n"
6475                    " *q\n"
6476                    " */"));
6477   EXPECT_EQ("/*\n"
6478             " * q\n"
6479             " */",
6480             format("/*\n"
6481                    " * q\n"
6482                    " */"));
6483   EXPECT_EQ("/*\n"
6484             " **/",
6485             format("/*\n"
6486                    " **/"));
6487   EXPECT_EQ("/*\n"
6488             " ***/",
6489             format("/*\n"
6490                    " ***/"));
6491 }
6492 
6493 TEST_F(FormatTest, BlockCommentsInMacros) {
6494   EXPECT_EQ("#define A          \\\n"
6495             "  {                \\\n"
6496             "    /* one line */ \\\n"
6497             "    someCall();",
6498             format("#define A {        \\\n"
6499                    "  /* one line */   \\\n"
6500                    "  someCall();",
6501                    getLLVMStyleWithColumns(20)));
6502   EXPECT_EQ("#define A          \\\n"
6503             "  {                \\\n"
6504             "    /* previous */ \\\n"
6505             "    /* one line */ \\\n"
6506             "    someCall();",
6507             format("#define A {        \\\n"
6508                    "  /* previous */   \\\n"
6509                    "  /* one line */   \\\n"
6510                    "  someCall();",
6511                    getLLVMStyleWithColumns(20)));
6512 }
6513 
6514 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6515   EXPECT_EQ("a = {\n"
6516             "    1111 /*    */\n"
6517             "};",
6518             format("a = {1111 /*    */\n"
6519                    "};",
6520                    getLLVMStyleWithColumns(15)));
6521   EXPECT_EQ("a = {\n"
6522             "    1111 /*      */\n"
6523             "};",
6524             format("a = {1111 /*      */\n"
6525                    "};",
6526                    getLLVMStyleWithColumns(15)));
6527 
6528   // FIXME: The formatting is still wrong here.
6529   EXPECT_EQ("a = {\n"
6530             "    1111 /*      a\n"
6531             "            */\n"
6532             "};",
6533             format("a = {1111 /*      a */\n"
6534                    "};",
6535                    getLLVMStyleWithColumns(15)));
6536 }
6537 
6538 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6539   // FIXME: This is not what we want...
6540   verifyFormat("{\n"
6541                "// a"
6542                "// b");
6543 }
6544 
6545 TEST_F(FormatTest, FormatStarDependingOnContext) {
6546   verifyFormat("void f(int *a);");
6547   verifyFormat("void f() { f(fint * b); }");
6548   verifyFormat("class A {\n  void f(int *a);\n};");
6549   verifyFormat("class A {\n  int *a;\n};");
6550   verifyFormat("namespace a {\n"
6551                "namespace b {\n"
6552                "class A {\n"
6553                "  void f() {}\n"
6554                "  int *a;\n"
6555                "};\n"
6556                "}\n"
6557                "}");
6558 }
6559 
6560 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6561   verifyFormat("while");
6562   verifyFormat("operator");
6563 }
6564 
6565 //===----------------------------------------------------------------------===//
6566 // Objective-C tests.
6567 //===----------------------------------------------------------------------===//
6568 
6569 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6570   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6571   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6572             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6573   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6574   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6575   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6576             format("-(NSInteger)Method3:(id)anObject;"));
6577   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6578             format("-(NSInteger)Method4:(id)anObject;"));
6579   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6580             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6581   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6582             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6583   EXPECT_EQ(
6584       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
6585       format(
6586           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
6587 
6588   // Very long objectiveC method declaration.
6589   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6590                "                    inRange:(NSRange)range\n"
6591                "                   outRange:(NSRange)out_range\n"
6592                "                  outRange1:(NSRange)out_range1\n"
6593                "                  outRange2:(NSRange)out_range2\n"
6594                "                  outRange3:(NSRange)out_range3\n"
6595                "                  outRange4:(NSRange)out_range4\n"
6596                "                  outRange5:(NSRange)out_range5\n"
6597                "                  outRange6:(NSRange)out_range6\n"
6598                "                  outRange7:(NSRange)out_range7\n"
6599                "                  outRange8:(NSRange)out_range8\n"
6600                "                  outRange9:(NSRange)out_range9;");
6601 
6602   verifyFormat("- (int)sum:(vector<int>)numbers;");
6603   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6604   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6605   // protocol lists (but not for template classes):
6606   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6607 
6608   verifyFormat("- (int (*)())foo:(int (*)())f;");
6609   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6610 
6611   // If there's no return type (very rare in practice!), LLVM and Google style
6612   // agree.
6613   verifyFormat("- foo;");
6614   verifyFormat("- foo:(int)f;");
6615   verifyGoogleFormat("- foo:(int)foo;");
6616 }
6617 
6618 TEST_F(FormatTest, FormatObjCInterface) {
6619   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6620                "@public\n"
6621                "  int field1;\n"
6622                "@protected\n"
6623                "  int field2;\n"
6624                "@private\n"
6625                "  int field3;\n"
6626                "@package\n"
6627                "  int field4;\n"
6628                "}\n"
6629                "+ (id)init;\n"
6630                "@end");
6631 
6632   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6633                      " @public\n"
6634                      "  int field1;\n"
6635                      " @protected\n"
6636                      "  int field2;\n"
6637                      " @private\n"
6638                      "  int field3;\n"
6639                      " @package\n"
6640                      "  int field4;\n"
6641                      "}\n"
6642                      "+ (id)init;\n"
6643                      "@end");
6644 
6645   verifyFormat("@interface /* wait for it */ Foo\n"
6646                "+ (id)init;\n"
6647                "// Look, a comment!\n"
6648                "- (int)answerWith:(int)i;\n"
6649                "@end");
6650 
6651   verifyFormat("@interface Foo\n"
6652                "@end\n"
6653                "@interface Bar\n"
6654                "@end");
6655 
6656   verifyFormat("@interface Foo : Bar\n"
6657                "+ (id)init;\n"
6658                "@end");
6659 
6660   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6661                "+ (id)init;\n"
6662                "@end");
6663 
6664   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6665                      "+ (id)init;\n"
6666                      "@end");
6667 
6668   verifyFormat("@interface Foo (HackStuff)\n"
6669                "+ (id)init;\n"
6670                "@end");
6671 
6672   verifyFormat("@interface Foo ()\n"
6673                "+ (id)init;\n"
6674                "@end");
6675 
6676   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6677                "+ (id)init;\n"
6678                "@end");
6679 
6680   verifyGoogleFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6681                      "+ (id)init;\n"
6682                      "@end");
6683 
6684   verifyFormat("@interface Foo {\n"
6685                "  int _i;\n"
6686                "}\n"
6687                "+ (id)init;\n"
6688                "@end");
6689 
6690   verifyFormat("@interface Foo : Bar {\n"
6691                "  int _i;\n"
6692                "}\n"
6693                "+ (id)init;\n"
6694                "@end");
6695 
6696   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6697                "  int _i;\n"
6698                "}\n"
6699                "+ (id)init;\n"
6700                "@end");
6701 
6702   verifyFormat("@interface Foo (HackStuff) {\n"
6703                "  int _i;\n"
6704                "}\n"
6705                "+ (id)init;\n"
6706                "@end");
6707 
6708   verifyFormat("@interface Foo () {\n"
6709                "  int _i;\n"
6710                "}\n"
6711                "+ (id)init;\n"
6712                "@end");
6713 
6714   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6715                "  int _i;\n"
6716                "}\n"
6717                "+ (id)init;\n"
6718                "@end");
6719 
6720   FormatStyle OnePerLine = getGoogleStyle();
6721   OnePerLine.BinPackParameters = false;
6722   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
6723                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6724                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6725                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6726                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6727                "}",
6728                OnePerLine);
6729 }
6730 
6731 TEST_F(FormatTest, FormatObjCImplementation) {
6732   verifyFormat("@implementation Foo : NSObject {\n"
6733                "@public\n"
6734                "  int field1;\n"
6735                "@protected\n"
6736                "  int field2;\n"
6737                "@private\n"
6738                "  int field3;\n"
6739                "@package\n"
6740                "  int field4;\n"
6741                "}\n"
6742                "+ (id)init {\n}\n"
6743                "@end");
6744 
6745   verifyGoogleFormat("@implementation Foo : NSObject {\n"
6746                      " @public\n"
6747                      "  int field1;\n"
6748                      " @protected\n"
6749                      "  int field2;\n"
6750                      " @private\n"
6751                      "  int field3;\n"
6752                      " @package\n"
6753                      "  int field4;\n"
6754                      "}\n"
6755                      "+ (id)init {\n}\n"
6756                      "@end");
6757 
6758   verifyFormat("@implementation Foo\n"
6759                "+ (id)init {\n"
6760                "  if (true)\n"
6761                "    return nil;\n"
6762                "}\n"
6763                "// Look, a comment!\n"
6764                "- (int)answerWith:(int)i {\n"
6765                "  return i;\n"
6766                "}\n"
6767                "+ (int)answerWith:(int)i {\n"
6768                "  return i;\n"
6769                "}\n"
6770                "@end");
6771 
6772   verifyFormat("@implementation Foo\n"
6773                "@end\n"
6774                "@implementation Bar\n"
6775                "@end");
6776 
6777   EXPECT_EQ("@implementation Foo : Bar\n"
6778             "+ (id)init {\n}\n"
6779             "- (void)foo {\n}\n"
6780             "@end",
6781             format("@implementation Foo : Bar\n"
6782                    "+(id)init{}\n"
6783                    "-(void)foo{}\n"
6784                    "@end"));
6785 
6786   verifyFormat("@implementation Foo {\n"
6787                "  int _i;\n"
6788                "}\n"
6789                "+ (id)init {\n}\n"
6790                "@end");
6791 
6792   verifyFormat("@implementation Foo : Bar {\n"
6793                "  int _i;\n"
6794                "}\n"
6795                "+ (id)init {\n}\n"
6796                "@end");
6797 
6798   verifyFormat("@implementation Foo (HackStuff)\n"
6799                "+ (id)init {\n}\n"
6800                "@end");
6801   verifyFormat("@implementation ObjcClass\n"
6802                "- (void)method;\n"
6803                "{}\n"
6804                "@end");
6805 }
6806 
6807 TEST_F(FormatTest, FormatObjCProtocol) {
6808   verifyFormat("@protocol Foo\n"
6809                "@property(weak) id delegate;\n"
6810                "- (NSUInteger)numberOfThings;\n"
6811                "@end");
6812 
6813   verifyFormat("@protocol MyProtocol <NSObject>\n"
6814                "- (NSUInteger)numberOfThings;\n"
6815                "@end");
6816 
6817   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
6818                      "- (NSUInteger)numberOfThings;\n"
6819                      "@end");
6820 
6821   verifyFormat("@protocol Foo;\n"
6822                "@protocol Bar;\n");
6823 
6824   verifyFormat("@protocol Foo\n"
6825                "@end\n"
6826                "@protocol Bar\n"
6827                "@end");
6828 
6829   verifyFormat("@protocol myProtocol\n"
6830                "- (void)mandatoryWithInt:(int)i;\n"
6831                "@optional\n"
6832                "- (void)optional;\n"
6833                "@required\n"
6834                "- (void)required;\n"
6835                "@optional\n"
6836                "@property(assign) int madProp;\n"
6837                "@end\n");
6838 
6839   verifyFormat("@property(nonatomic, assign, readonly)\n"
6840                "    int *looooooooooooooooooooooooooooongNumber;\n"
6841                "@property(nonatomic, assign, readonly)\n"
6842                "    NSString *looooooooooooooooooooooooooooongName;");
6843 
6844   verifyFormat("@implementation PR18406\n"
6845                "}\n"
6846                "@end");
6847 }
6848 
6849 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
6850   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
6851                "                   rect:(NSRect)theRect\n"
6852                "               interval:(float)theInterval {\n"
6853                "}");
6854   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
6855                "          longKeyword:(NSRect)theRect\n"
6856                "    evenLongerKeyword:(float)theInterval\n"
6857                "                error:(NSError **)theError {\n"
6858                "}");
6859   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
6860                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
6861                "    NS_DESIGNATED_INITIALIZER;",
6862                getLLVMStyleWithColumns(60));
6863 }
6864 
6865 TEST_F(FormatTest, FormatObjCMethodExpr) {
6866   verifyFormat("[foo bar:baz];");
6867   verifyFormat("return [foo bar:baz];");
6868   verifyFormat("return (a)[foo bar:baz];");
6869   verifyFormat("f([foo bar:baz]);");
6870   verifyFormat("f(2, [foo bar:baz]);");
6871   verifyFormat("f(2, a ? b : c);");
6872   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
6873 
6874   // Unary operators.
6875   verifyFormat("int a = +[foo bar:baz];");
6876   verifyFormat("int a = -[foo bar:baz];");
6877   verifyFormat("int a = ![foo bar:baz];");
6878   verifyFormat("int a = ~[foo bar:baz];");
6879   verifyFormat("int a = ++[foo bar:baz];");
6880   verifyFormat("int a = --[foo bar:baz];");
6881   verifyFormat("int a = sizeof [foo bar:baz];");
6882   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
6883   verifyFormat("int a = &[foo bar:baz];");
6884   verifyFormat("int a = *[foo bar:baz];");
6885   // FIXME: Make casts work, without breaking f()[4].
6886   //verifyFormat("int a = (int)[foo bar:baz];");
6887   //verifyFormat("return (int)[foo bar:baz];");
6888   //verifyFormat("(void)[foo bar:baz];");
6889   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
6890 
6891   // Binary operators.
6892   verifyFormat("[foo bar:baz], [foo bar:baz];");
6893   verifyFormat("[foo bar:baz] = [foo bar:baz];");
6894   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
6895   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
6896   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
6897   verifyFormat("[foo bar:baz] += [foo bar:baz];");
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] : [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];");
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   // Whew!
6924 
6925   verifyFormat("return in[42];");
6926   verifyFormat("for (auto v : in[1]) {\n}");
6927   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
6928                "}");
6929   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
6930 
6931   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
6932   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
6933   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
6934   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
6935   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
6936   verifyFormat("[button setAction:@selector(zoomOut:)];");
6937   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
6938 
6939   verifyFormat("arr[[self indexForFoo:a]];");
6940   verifyFormat("throw [self errorFor:a];");
6941   verifyFormat("@throw [self errorFor:a];");
6942 
6943   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
6944   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
6945   verifyFormat("4 > 4 ? (id)a : (id)baz;");
6946 
6947   // This tests that the formatter doesn't break after "backing" but before ":",
6948   // which would be at 80 columns.
6949   verifyFormat(
6950       "void f() {\n"
6951       "  if ((self = [super initWithContentRect:contentRect\n"
6952       "                               styleMask:styleMask ?: otherMask\n"
6953       "                                 backing:NSBackingStoreBuffered\n"
6954       "                                   defer:YES]))");
6955 
6956   verifyFormat(
6957       "[foo checkThatBreakingAfterColonWorksOk:\n"
6958       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
6959 
6960   verifyFormat("[myObj short:arg1 // Force line break\n"
6961                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
6962                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
6963                "                error:arg4];");
6964   verifyFormat(
6965       "void f() {\n"
6966       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6967       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
6968       "                                     pos.width(), pos.height())\n"
6969       "                styleMask:NSBorderlessWindowMask\n"
6970       "                  backing:NSBackingStoreBuffered\n"
6971       "                    defer:NO]);\n"
6972       "}");
6973   verifyFormat(
6974       "void f() {\n"
6975       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
6976       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
6977       "                                 pos.width(), pos.height())\n"
6978       "                syeMask:NSBorderlessWindowMask\n"
6979       "                  bking:NSBackingStoreBuffered\n"
6980       "                    der:NO]);\n"
6981       "}",
6982       getLLVMStyleWithColumns(70));
6983   verifyFormat(
6984       "void f() {\n"
6985       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
6986       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
6987       "                                     pos.width(), pos.height())\n"
6988       "                styleMask:NSBorderlessWindowMask\n"
6989       "                  backing:NSBackingStoreBuffered\n"
6990       "                    defer:NO]);\n"
6991       "}",
6992       getChromiumStyle(FormatStyle::LK_Cpp));
6993   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
6994                "                             with:contentsNativeView];");
6995 
6996   verifyFormat(
6997       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
6998       "           owner:nillllll];");
6999 
7000   verifyFormat(
7001       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7002       "        forType:kBookmarkButtonDragType];");
7003 
7004   verifyFormat("[defaultCenter addObserver:self\n"
7005                "                  selector:@selector(willEnterFullscreen)\n"
7006                "                      name:kWillEnterFullscreenNotification\n"
7007                "                    object:nil];");
7008   verifyFormat("[image_rep drawInRect:drawRect\n"
7009                "             fromRect:NSZeroRect\n"
7010                "            operation:NSCompositeCopy\n"
7011                "             fraction:1.0\n"
7012                "       respectFlipped:NO\n"
7013                "                hints:nil];");
7014 
7015   verifyFormat(
7016       "scoped_nsobject<NSTextField> message(\n"
7017       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7018       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7019   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7020                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7021                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7022                "          aaaa:bbb];");
7023   verifyFormat("[self param:function( //\n"
7024                "                parameter)]");
7025   verifyFormat(
7026       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7027       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7028       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7029 
7030   // Variadic parameters.
7031   verifyFormat(
7032       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7033   verifyFormat(
7034       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7035       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7036       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7037   verifyFormat("[self // break\n"
7038                "      a:a\n"
7039                "    aaa:aaa];");
7040   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7041                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7042 }
7043 
7044 TEST_F(FormatTest, ObjCAt) {
7045   verifyFormat("@autoreleasepool");
7046   verifyFormat("@catch");
7047   verifyFormat("@class");
7048   verifyFormat("@compatibility_alias");
7049   verifyFormat("@defs");
7050   verifyFormat("@dynamic");
7051   verifyFormat("@encode");
7052   verifyFormat("@end");
7053   verifyFormat("@finally");
7054   verifyFormat("@implementation");
7055   verifyFormat("@import");
7056   verifyFormat("@interface");
7057   verifyFormat("@optional");
7058   verifyFormat("@package");
7059   verifyFormat("@private");
7060   verifyFormat("@property");
7061   verifyFormat("@protected");
7062   verifyFormat("@protocol");
7063   verifyFormat("@public");
7064   verifyFormat("@required");
7065   verifyFormat("@selector");
7066   verifyFormat("@synchronized");
7067   verifyFormat("@synthesize");
7068   verifyFormat("@throw");
7069   verifyFormat("@try");
7070 
7071   EXPECT_EQ("@interface", format("@ interface"));
7072 
7073   // The precise formatting of this doesn't matter, nobody writes code like
7074   // this.
7075   verifyFormat("@ /*foo*/ interface");
7076 }
7077 
7078 TEST_F(FormatTest, ObjCSnippets) {
7079   verifyFormat("@autoreleasepool {\n"
7080                "  foo();\n"
7081                "}");
7082   verifyFormat("@class Foo, Bar;");
7083   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7084   verifyFormat("@dynamic textColor;");
7085   verifyFormat("char *buf1 = @encode(int *);");
7086   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7087   verifyFormat("char *buf1 = @encode(int **);");
7088   verifyFormat("Protocol *proto = @protocol(p1);");
7089   verifyFormat("SEL s = @selector(foo:);");
7090   verifyFormat("@synchronized(self) {\n"
7091                "  f();\n"
7092                "}");
7093 
7094   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7095   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7096 
7097   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7098   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7099   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7100   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7101                getMozillaStyle());
7102   verifyFormat("@property BOOL editable;", getMozillaStyle());
7103   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7104                getWebKitStyle());
7105   verifyFormat("@property BOOL editable;", getWebKitStyle());
7106 
7107   verifyFormat("@import foo.bar;\n"
7108                "@import baz;");
7109 }
7110 
7111 TEST_F(FormatTest, ObjCLiterals) {
7112   verifyFormat("@\"String\"");
7113   verifyFormat("@1");
7114   verifyFormat("@+4.8");
7115   verifyFormat("@-4");
7116   verifyFormat("@1LL");
7117   verifyFormat("@.5");
7118   verifyFormat("@'c'");
7119   verifyFormat("@true");
7120 
7121   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7122   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7123   verifyFormat("NSNumber *favoriteColor = @(Green);");
7124   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7125 
7126   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7127 }
7128 
7129 TEST_F(FormatTest, ObjCDictLiterals) {
7130   verifyFormat("@{");
7131   verifyFormat("@{}");
7132   verifyFormat("@{@\"one\" : @1}");
7133   verifyFormat("return @{@\"one\" : @1;");
7134   verifyFormat("@{@\"one\" : @1}");
7135 
7136   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7137   verifyFormat("@{\n"
7138                "  @\"one\" : @{@2 : @1},\n"
7139                "}");
7140 
7141   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7142   verifyFormat("[self setDict:@{}");
7143   verifyFormat("[self setDict:@{@1 : @2}");
7144   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7145   verifyFormat(
7146       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7147   verifyFormat(
7148       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7149 
7150   verifyFormat(
7151       "NSDictionary *d = @{\n"
7152       "  @\"nam\" : NSUserNam(),\n"
7153       "  @\"dte\" : [NSDate date],\n"
7154       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7155       "};");
7156   verifyFormat(
7157       "@{\n"
7158       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7159       "regularFont,\n"
7160       "};");
7161   verifyGoogleFormat(
7162       "@{\n"
7163       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7164       "regularFont,\n"
7165       "};");
7166   verifyFormat(
7167       "@{\n"
7168       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7169       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7170       "};");
7171 
7172   // We should try to be robust in case someone forgets the "@".
7173   verifyFormat(
7174       "NSDictionary *d = {\n"
7175       "  @\"nam\" : NSUserNam(),\n"
7176       "  @\"dte\" : [NSDate date],\n"
7177       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7178       "};");
7179   verifyFormat("NSMutableDictionary *dictionary =\n"
7180                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7181                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7182                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7183                "      cccccccccccccccc : ccccccccccccccc\n"
7184                "    }];");
7185 }
7186 
7187 TEST_F(FormatTest, ObjCArrayLiterals) {
7188   verifyFormat("@[");
7189   verifyFormat("@[]");
7190   verifyFormat(
7191       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7192   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7193   verifyFormat("NSArray *array = @[ [foo description] ];");
7194 
7195   verifyFormat(
7196       "NSArray *some_variable = @[\n"
7197       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7198       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7199       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7200       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7201       "];");
7202   verifyFormat("NSArray *some_variable = @[\n"
7203                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7204                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7205                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7206                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7207                "];");
7208   verifyGoogleFormat("NSArray *some_variable = @[\n"
7209                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7210                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7211                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7212                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7213                      "];");
7214 
7215   // We should try to be robust in case someone forgets the "@".
7216   verifyFormat("NSArray *some_variable = [\n"
7217                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7218                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7219                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7220                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7221                "];");
7222   verifyFormat(
7223       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7224       "                                             index:(NSUInteger)index\n"
7225       "                                nonDigitAttributes:\n"
7226       "                                    (NSDictionary *)noDigitAttributes;");
7227   verifyFormat(
7228       "[someFunction someLooooooooooooongParameter:\n"
7229       "                  @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
7230 }
7231 
7232 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
7233   EXPECT_EQ("{\n"
7234             "{\n"
7235             "a;\n"
7236             "b;\n"
7237             "}\n"
7238             "}",
7239             format("{\n"
7240                    "{\n"
7241                    "a;\n"
7242                    "     b;\n"
7243                    "}\n"
7244                    "}",
7245                    13, 2, getLLVMStyle()));
7246   EXPECT_EQ("{\n"
7247             "{\n"
7248             "  a;\n"
7249             "b;\n"
7250             "}\n"
7251             "}",
7252             format("{\n"
7253                    "{\n"
7254                    "     a;\n"
7255                    "b;\n"
7256                    "}\n"
7257                    "}",
7258                    9, 2, getLLVMStyle()));
7259   EXPECT_EQ("{\n"
7260             "{\n"
7261             "public:\n"
7262             "  b;\n"
7263             "}\n"
7264             "}",
7265             format("{\n"
7266                    "{\n"
7267                    "public:\n"
7268                    "     b;\n"
7269                    "}\n"
7270                    "}",
7271                    17, 2, getLLVMStyle()));
7272   EXPECT_EQ("{\n"
7273             "{\n"
7274             "a;\n"
7275             "}\n"
7276             "{\n"
7277             "  b; //\n"
7278             "}\n"
7279             "}",
7280             format("{\n"
7281                    "{\n"
7282                    "a;\n"
7283                    "}\n"
7284                    "{\n"
7285                    "           b; //\n"
7286                    "}\n"
7287                    "}",
7288                    22, 2, getLLVMStyle()));
7289   EXPECT_EQ("  {\n"
7290             "    a; //\n"
7291             "  }",
7292             format("  {\n"
7293                    "a; //\n"
7294                    "  }",
7295                    4, 2, getLLVMStyle()));
7296   EXPECT_EQ("void f() {}\n"
7297             "void g() {}",
7298             format("void f() {}\n"
7299                    "void g() {}",
7300                    13, 0, getLLVMStyle()));
7301   EXPECT_EQ("int a; // comment\n"
7302             "       // line 2\n"
7303             "int b;",
7304             format("int a; // comment\n"
7305                    "       // line 2\n"
7306                    "  int b;",
7307                    35, 0, getLLVMStyle()));
7308   EXPECT_EQ("  int a;\n"
7309             "  void\n"
7310             "  ffffff() {\n"
7311             "  }",
7312             format("  int a;\n"
7313                    "void ffffff() {}",
7314                    11, 0, getLLVMStyleWithColumns(11)));
7315 
7316   EXPECT_EQ(" void f() {\n"
7317             "#define A 1\n"
7318             " }",
7319             format(" void f() {\n"
7320                    "     #define A 1\n" // Format this line.
7321                    " }",
7322                    20, 0, getLLVMStyle()));
7323   EXPECT_EQ(" void f() {\n"
7324             "    int i;\n"
7325             "#define A \\\n"
7326             "    int i;  \\\n"
7327             "   int j;\n"
7328             "    int k;\n"
7329             " }",
7330             format(" void f() {\n"
7331                    "    int i;\n"
7332                    "#define A \\\n"
7333                    "    int i;  \\\n"
7334                    "   int j;\n"
7335                    "      int k;\n" // Format this line.
7336                    " }",
7337                    67, 0, getLLVMStyle()));
7338 }
7339 
7340 TEST_F(FormatTest, BreaksStringLiterals) {
7341   EXPECT_EQ("\"some text \"\n"
7342             "\"other\";",
7343             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7344   EXPECT_EQ("\"some text \"\n"
7345             "\"other\";",
7346             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7347   EXPECT_EQ(
7348       "#define A  \\\n"
7349       "  \"some \"  \\\n"
7350       "  \"text \"  \\\n"
7351       "  \"other\";",
7352       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7353   EXPECT_EQ(
7354       "#define A  \\\n"
7355       "  \"so \"    \\\n"
7356       "  \"text \"  \\\n"
7357       "  \"other\";",
7358       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7359 
7360   EXPECT_EQ("\"some text\"",
7361             format("\"some text\"", getLLVMStyleWithColumns(1)));
7362   EXPECT_EQ("\"some text\"",
7363             format("\"some text\"", getLLVMStyleWithColumns(11)));
7364   EXPECT_EQ("\"some \"\n"
7365             "\"text\"",
7366             format("\"some text\"", getLLVMStyleWithColumns(10)));
7367   EXPECT_EQ("\"some \"\n"
7368             "\"text\"",
7369             format("\"some text\"", getLLVMStyleWithColumns(7)));
7370   EXPECT_EQ("\"some\"\n"
7371             "\" tex\"\n"
7372             "\"t\"",
7373             format("\"some text\"", getLLVMStyleWithColumns(6)));
7374   EXPECT_EQ("\"some\"\n"
7375             "\" tex\"\n"
7376             "\" and\"",
7377             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7378   EXPECT_EQ("\"some\"\n"
7379             "\"/tex\"\n"
7380             "\"/and\"",
7381             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7382 
7383   EXPECT_EQ("variable =\n"
7384             "    \"long string \"\n"
7385             "    \"literal\";",
7386             format("variable = \"long string literal\";",
7387                    getLLVMStyleWithColumns(20)));
7388 
7389   EXPECT_EQ("variable = f(\n"
7390             "    \"long string \"\n"
7391             "    \"literal\",\n"
7392             "    short,\n"
7393             "    loooooooooooooooooooong);",
7394             format("variable = f(\"long string literal\", short, "
7395                    "loooooooooooooooooooong);",
7396                    getLLVMStyleWithColumns(20)));
7397 
7398   EXPECT_EQ("f(g(\"long string \"\n"
7399             "    \"literal\"),\n"
7400             "  b);",
7401             format("f(g(\"long string literal\"), b);",
7402                    getLLVMStyleWithColumns(20)));
7403   EXPECT_EQ("f(g(\"long string \"\n"
7404             "    \"literal\",\n"
7405             "    a),\n"
7406             "  b);",
7407             format("f(g(\"long string literal\", a), b);",
7408                    getLLVMStyleWithColumns(20)));
7409   EXPECT_EQ(
7410       "f(\"one two\".split(\n"
7411       "    variable));",
7412       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7413   EXPECT_EQ("f(\"one two three four five six \"\n"
7414             "  \"seven\".split(\n"
7415             "      really_looooong_variable));",
7416             format("f(\"one two three four five six seven\"."
7417                    "split(really_looooong_variable));",
7418                    getLLVMStyleWithColumns(33)));
7419 
7420   EXPECT_EQ("f(\"some \"\n"
7421             "  \"text\",\n"
7422             "  other);",
7423             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7424 
7425   // Only break as a last resort.
7426   verifyFormat(
7427       "aaaaaaaaaaaaaaaaaaaa(\n"
7428       "    aaaaaaaaaaaaaaaaaaaa,\n"
7429       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7430 
7431   EXPECT_EQ(
7432       "\"splitmea\"\n"
7433       "\"trandomp\"\n"
7434       "\"oint\"",
7435       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7436 
7437   EXPECT_EQ(
7438       "\"split/\"\n"
7439       "\"pathat/\"\n"
7440       "\"slashes\"",
7441       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7442 
7443   EXPECT_EQ(
7444       "\"split/\"\n"
7445       "\"pathat/\"\n"
7446       "\"slashes\"",
7447       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7448   EXPECT_EQ("\"split at \"\n"
7449             "\"spaces/at/\"\n"
7450             "\"slashes.at.any$\"\n"
7451             "\"non-alphanumeric%\"\n"
7452             "\"1111111111characte\"\n"
7453             "\"rs\"",
7454             format("\"split at "
7455                    "spaces/at/"
7456                    "slashes.at."
7457                    "any$non-"
7458                    "alphanumeric%"
7459                    "1111111111characte"
7460                    "rs\"",
7461                    getLLVMStyleWithColumns(20)));
7462 
7463   // Verify that splitting the strings understands
7464   // Style::AlwaysBreakBeforeMultilineStrings.
7465   EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
7466             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7467             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7468             format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
7469                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
7470                    "aaaaaaaaaaaaaaaaaaaaaa\");",
7471                    getGoogleStyle()));
7472   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7473             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7474             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7475                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7476                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7477                    getGoogleStyle()));
7478   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7479             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7480             format("llvm::outs() << "
7481                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7482                    "aaaaaaaaaaaaaaaaaaa\";"));
7483   EXPECT_EQ("ffff(\n"
7484             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7485             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7486             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7487                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7488                    getGoogleStyle()));
7489 
7490   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7491   AlignLeft.AlignEscapedNewlinesLeft = true;
7492   EXPECT_EQ(
7493       "#define A \\\n"
7494       "  \"some \" \\\n"
7495       "  \"text \" \\\n"
7496       "  \"other\";",
7497       format("#define A \"some text other\";", AlignLeft));
7498 }
7499 
7500 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7501   EXPECT_EQ(
7502       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7503       "(\n"
7504       "    \"x\t\");",
7505       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7506              "aaaaaaa("
7507              "\"x\t\");"));
7508 }
7509 
7510 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7511   EXPECT_EQ(
7512       "u8\"utf8 string \"\n"
7513       "u8\"literal\";",
7514       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7515   EXPECT_EQ(
7516       "u\"utf16 string \"\n"
7517       "u\"literal\";",
7518       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7519   EXPECT_EQ(
7520       "U\"utf32 string \"\n"
7521       "U\"literal\";",
7522       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7523   EXPECT_EQ("L\"wide string \"\n"
7524             "L\"literal\";",
7525             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7526   EXPECT_EQ("@\"NSString \"\n"
7527             "@\"literal\";",
7528             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7529 
7530   // This input makes clang-format try to split the incomplete unicode escape
7531   // sequence, which used to lead to a crasher.
7532   verifyNoCrash(
7533       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7534       getLLVMStyleWithColumns(60));
7535 }
7536 
7537 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7538   FormatStyle Style = getGoogleStyleWithColumns(15);
7539   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7540   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7541   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7542   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7543   EXPECT_EQ("u8R\"x(raw literal)x\";",
7544             format("u8R\"x(raw literal)x\";", Style));
7545 }
7546 
7547 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7548   FormatStyle Style = getLLVMStyleWithColumns(20);
7549   EXPECT_EQ(
7550       "_T(\"aaaaaaaaaaaaaa\")\n"
7551       "_T(\"aaaaaaaaaaaaaa\")\n"
7552       "_T(\"aaaaaaaaaaaa\")",
7553       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7554   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7555             "     _T(\"aaaaaa\"),\n"
7556             "  z);",
7557             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7558 
7559   // FIXME: Handle embedded spaces in one iteration.
7560   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7561   //            "_T(\"aaaaaaaaaaaaa\")\n"
7562   //            "_T(\"aaaaaaaaaaaaa\")\n"
7563   //            "_T(\"a\")",
7564   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7565   //                   getLLVMStyleWithColumns(20)));
7566   EXPECT_EQ(
7567       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7568       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7569 }
7570 
7571 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7572   EXPECT_EQ(
7573       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7574       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7575       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7576       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7577              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7578              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7579 }
7580 
7581 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7582   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7583             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7584   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7585             "multiline raw string literal xxxxxxxxxxxxxx\n"
7586             ")x\",\n"
7587             "              a),\n"
7588             "            b);",
7589             format("fffffffffff(g(R\"x(\n"
7590                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7591                    ")x\", a), b);",
7592                    getGoogleStyleWithColumns(20)));
7593   EXPECT_EQ("fffffffffff(\n"
7594             "    g(R\"x(qqq\n"
7595             "multiline raw string literal xxxxxxxxxxxxxx\n"
7596             ")x\",\n"
7597             "      a),\n"
7598             "    b);",
7599             format("fffffffffff(g(R\"x(qqq\n"
7600                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7601                    ")x\", a), b);",
7602                    getGoogleStyleWithColumns(20)));
7603 
7604   EXPECT_EQ("fffffffffff(R\"x(\n"
7605             "multiline raw string literal xxxxxxxxxxxxxx\n"
7606             ")x\");",
7607             format("fffffffffff(R\"x(\n"
7608                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7609                    ")x\");",
7610                    getGoogleStyleWithColumns(20)));
7611   EXPECT_EQ("fffffffffff(R\"x(\n"
7612             "multiline raw string literal xxxxxxxxxxxxxx\n"
7613             ")x\" + bbbbbb);",
7614             format("fffffffffff(R\"x(\n"
7615                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7616                    ")x\" +   bbbbbb);",
7617                    getGoogleStyleWithColumns(20)));
7618   EXPECT_EQ("fffffffffff(\n"
7619             "    R\"x(\n"
7620             "multiline raw string literal xxxxxxxxxxxxxx\n"
7621             ")x\" +\n"
7622             "    bbbbbb);",
7623             format("fffffffffff(\n"
7624                    " R\"x(\n"
7625                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7626                    ")x\" + bbbbbb);",
7627                    getGoogleStyleWithColumns(20)));
7628 }
7629 
7630 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7631   verifyFormat("string a = \"unterminated;");
7632   EXPECT_EQ("function(\"unterminated,\n"
7633             "         OtherParameter);",
7634             format("function(  \"unterminated,\n"
7635                    "    OtherParameter);"));
7636 }
7637 
7638 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7639   FormatStyle Style = getLLVMStyle();
7640   Style.Standard = FormatStyle::LS_Cpp03;
7641   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7642             format("#define x(_a) printf(\"foo\"_a);", Style));
7643 }
7644 
7645 TEST_F(FormatTest, UnderstandsCpp1y) {
7646   verifyFormat("int bi{1'000'000};");
7647 }
7648 
7649 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7650   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7651             "             \"ddeeefff\");",
7652             format("someFunction(\"aaabbbcccdddeeefff\");",
7653                    getLLVMStyleWithColumns(25)));
7654   EXPECT_EQ("someFunction1234567890(\n"
7655             "    \"aaabbbcccdddeeefff\");",
7656             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7657                    getLLVMStyleWithColumns(26)));
7658   EXPECT_EQ("someFunction1234567890(\n"
7659             "    \"aaabbbcccdddeeeff\"\n"
7660             "    \"f\");",
7661             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7662                    getLLVMStyleWithColumns(25)));
7663   EXPECT_EQ("someFunction1234567890(\n"
7664             "    \"aaabbbcccdddeeeff\"\n"
7665             "    \"f\");",
7666             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7667                    getLLVMStyleWithColumns(24)));
7668   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7669             "             \"ddde \"\n"
7670             "             \"efff\");",
7671             format("someFunction(\"aaabbbcc ddde efff\");",
7672                    getLLVMStyleWithColumns(25)));
7673   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7674             "             \"ddeeefff\");",
7675             format("someFunction(\"aaabbbccc ddeeefff\");",
7676                    getLLVMStyleWithColumns(25)));
7677   EXPECT_EQ("someFunction1234567890(\n"
7678             "    \"aaabb \"\n"
7679             "    \"cccdddeeefff\");",
7680             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7681                    getLLVMStyleWithColumns(25)));
7682   EXPECT_EQ("#define A          \\\n"
7683             "  string s =       \\\n"
7684             "      \"123456789\"  \\\n"
7685             "      \"0\";         \\\n"
7686             "  int i;",
7687             format("#define A string s = \"1234567890\"; int i;",
7688                    getLLVMStyleWithColumns(20)));
7689   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7690   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7691             "             \"dddeeeff\"\n"
7692             "             \"f\");",
7693             format("someFunction(\"aaabbbcc dddeeefff\");",
7694                    getLLVMStyleWithColumns(25)));
7695 }
7696 
7697 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7698   EXPECT_EQ("\"\\a\"",
7699             format("\"\\a\"", getLLVMStyleWithColumns(3)));
7700   EXPECT_EQ("\"\\\"",
7701             format("\"\\\"", getLLVMStyleWithColumns(2)));
7702   EXPECT_EQ("\"test\"\n"
7703             "\"\\n\"",
7704             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7705   EXPECT_EQ("\"tes\\\\\"\n"
7706             "\"n\"",
7707             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7708   EXPECT_EQ("\"\\\\\\\\\"\n"
7709             "\"\\n\"",
7710             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7711   EXPECT_EQ("\"\\uff01\"",
7712             format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7713   EXPECT_EQ("\"\\uff01\"\n"
7714             "\"test\"",
7715             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7716   EXPECT_EQ("\"\\Uff01ff02\"",
7717             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7718   EXPECT_EQ("\"\\x000000000001\"\n"
7719             "\"next\"",
7720             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7721   EXPECT_EQ("\"\\x000000000001next\"",
7722             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7723   EXPECT_EQ("\"\\x000000000001\"",
7724             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7725   EXPECT_EQ("\"test\"\n"
7726             "\"\\000000\"\n"
7727             "\"000001\"",
7728             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7729   EXPECT_EQ("\"test\\000\"\n"
7730             "\"00000000\"\n"
7731             "\"1\"",
7732             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7733 }
7734 
7735 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7736   verifyFormat("void f() {\n"
7737                "  return g() {}\n"
7738                "  void h() {}");
7739   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7740                "g();\n"
7741                "}");
7742 }
7743 
7744 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7745   verifyFormat(
7746       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7747 }
7748 
7749 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7750   verifyFormat("class X {\n"
7751                "  void f() {\n"
7752                "  }\n"
7753                "};",
7754                getLLVMStyleWithColumns(12));
7755 }
7756 
7757 TEST_F(FormatTest, ConfigurableIndentWidth) {
7758   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7759   EightIndent.IndentWidth = 8;
7760   EightIndent.ContinuationIndentWidth = 8;
7761   verifyFormat("void f() {\n"
7762                "        someFunction();\n"
7763                "        if (true) {\n"
7764                "                f();\n"
7765                "        }\n"
7766                "}",
7767                EightIndent);
7768   verifyFormat("class X {\n"
7769                "        void f() {\n"
7770                "        }\n"
7771                "};",
7772                EightIndent);
7773   verifyFormat("int x[] = {\n"
7774                "        call(),\n"
7775                "        call()};",
7776                EightIndent);
7777 }
7778 
7779 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7780   verifyFormat("double\n"
7781                "f();",
7782                getLLVMStyleWithColumns(8));
7783 }
7784 
7785 TEST_F(FormatTest, ConfigurableUseOfTab) {
7786   FormatStyle Tab = getLLVMStyleWithColumns(42);
7787   Tab.IndentWidth = 8;
7788   Tab.UseTab = FormatStyle::UT_Always;
7789   Tab.AlignEscapedNewlinesLeft = true;
7790 
7791   EXPECT_EQ("if (aaaaaaaa && // q\n"
7792             "    bb)\t\t// w\n"
7793             "\t;",
7794             format("if (aaaaaaaa &&// q\n"
7795                    "bb)// w\n"
7796                    ";",
7797                    Tab));
7798   EXPECT_EQ("if (aaa && bbb) // w\n"
7799             "\t;",
7800             format("if(aaa&&bbb)// w\n"
7801                    ";",
7802                    Tab));
7803 
7804   verifyFormat("class X {\n"
7805                "\tvoid f() {\n"
7806                "\t\tsomeFunction(parameter1,\n"
7807                "\t\t\t     parameter2);\n"
7808                "\t}\n"
7809                "};",
7810                Tab);
7811   verifyFormat("#define A                        \\\n"
7812                "\tvoid f() {               \\\n"
7813                "\t\tsomeFunction(    \\\n"
7814                "\t\t    parameter1,  \\\n"
7815                "\t\t    parameter2); \\\n"
7816                "\t}",
7817                Tab);
7818   EXPECT_EQ("void f() {\n"
7819             "\tf();\n"
7820             "\tg();\n"
7821             "}",
7822             format("void f() {\n"
7823                    "\tf();\n"
7824                    "\tg();\n"
7825                    "}",
7826                    0, 0, Tab));
7827   EXPECT_EQ("void f() {\n"
7828             "\tf();\n"
7829             "\tg();\n"
7830             "}",
7831             format("void f() {\n"
7832                    "\tf();\n"
7833                    "\tg();\n"
7834                    "}",
7835                    16, 0, Tab));
7836   EXPECT_EQ("void f() {\n"
7837             "  \tf();\n"
7838             "\tg();\n"
7839             "}",
7840             format("void f() {\n"
7841                    "  \tf();\n"
7842                    "  \tg();\n"
7843                    "}",
7844                    21, 0, Tab));
7845 
7846   Tab.TabWidth = 4;
7847   Tab.IndentWidth = 8;
7848   verifyFormat("class TabWidth4Indent8 {\n"
7849                "\t\tvoid f() {\n"
7850                "\t\t\t\tsomeFunction(parameter1,\n"
7851                "\t\t\t\t\t\t\t parameter2);\n"
7852                "\t\t}\n"
7853                "};",
7854                Tab);
7855 
7856   Tab.TabWidth = 4;
7857   Tab.IndentWidth = 4;
7858   verifyFormat("class TabWidth4Indent4 {\n"
7859                "\tvoid f() {\n"
7860                "\t\tsomeFunction(parameter1,\n"
7861                "\t\t\t\t\t parameter2);\n"
7862                "\t}\n"
7863                "};",
7864                Tab);
7865 
7866   Tab.TabWidth = 8;
7867   Tab.IndentWidth = 4;
7868   verifyFormat("class TabWidth8Indent4 {\n"
7869                "    void f() {\n"
7870                "\tsomeFunction(parameter1,\n"
7871                "\t\t     parameter2);\n"
7872                "    }\n"
7873                "};",
7874                Tab);
7875 
7876   Tab.TabWidth = 8;
7877   Tab.IndentWidth = 8;
7878   EXPECT_EQ("/*\n"
7879             "\t      a\t\tcomment\n"
7880             "\t      in multiple lines\n"
7881             "       */",
7882             format("   /*\t \t \n"
7883                    " \t \t a\t\tcomment\t \t\n"
7884                    " \t \t in multiple lines\t\n"
7885                    " \t  */",
7886                    Tab));
7887 
7888   Tab.UseTab = FormatStyle::UT_ForIndentation;
7889   verifyFormat("{\n"
7890                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7891                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7892                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7893                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7894                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7895                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
7896                "};",
7897                Tab);
7898   verifyFormat("enum A {\n"
7899                "\ta1, // Force multiple lines\n"
7900                "\ta2,\n"
7901                "\ta3\n"
7902                "};",
7903                Tab);
7904   EXPECT_EQ("if (aaaaaaaa && // q\n"
7905             "    bb)         // w\n"
7906             "\t;",
7907             format("if (aaaaaaaa &&// q\n"
7908                    "bb)// w\n"
7909                    ";",
7910                    Tab));
7911   verifyFormat("class X {\n"
7912                "\tvoid f() {\n"
7913                "\t\tsomeFunction(parameter1,\n"
7914                "\t\t             parameter2);\n"
7915                "\t}\n"
7916                "};",
7917                Tab);
7918   verifyFormat("{\n"
7919                "\tQ({\n"
7920                "\t\tint a;\n"
7921                "\t\tsomeFunction(aaaaaaaa,\n"
7922                "\t\t             bbbbbbb);\n"
7923                "\t}, p);\n"
7924                "}",
7925                Tab);
7926   EXPECT_EQ("{\n"
7927             "\t/* aaaa\n"
7928             "\t   bbbb */\n"
7929             "}",
7930             format("{\n"
7931                    "/* aaaa\n"
7932                    "   bbbb */\n"
7933                    "}",
7934                    Tab));
7935   EXPECT_EQ("{\n"
7936             "\t/*\n"
7937             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7938             "\t  bbbbbbbbbbbbb\n"
7939             "\t*/\n"
7940             "}",
7941             format("{\n"
7942                    "/*\n"
7943                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7944                    "*/\n"
7945                    "}",
7946                    Tab));
7947   EXPECT_EQ("{\n"
7948             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7949             "\t// bbbbbbbbbbbbb\n"
7950             "}",
7951             format("{\n"
7952                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7953                    "}",
7954                    Tab));
7955   EXPECT_EQ("{\n"
7956             "\t/*\n"
7957             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
7958             "\t  bbbbbbbbbbbbb\n"
7959             "\t*/\n"
7960             "}",
7961             format("{\n"
7962                    "\t/*\n"
7963                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
7964                    "\t*/\n"
7965                    "}",
7966                    Tab));
7967   EXPECT_EQ("{\n"
7968             "\t/*\n"
7969             "\n"
7970             "\t*/\n"
7971             "}",
7972             format("{\n"
7973                    "\t/*\n"
7974                    "\n"
7975                    "\t*/\n"
7976                    "}",
7977                    Tab));
7978   EXPECT_EQ("{\n"
7979             "\t/*\n"
7980             " asdf\n"
7981             "\t*/\n"
7982             "}",
7983             format("{\n"
7984                    "\t/*\n"
7985                    " asdf\n"
7986                    "\t*/\n"
7987                    "}",
7988                    Tab));
7989 
7990   Tab.UseTab = FormatStyle::UT_Never;
7991   EXPECT_EQ("/*\n"
7992             "              a\t\tcomment\n"
7993             "              in multiple lines\n"
7994             "       */",
7995             format("   /*\t \t \n"
7996                    " \t \t a\t\tcomment\t \t\n"
7997                    " \t \t in multiple lines\t\n"
7998                    " \t  */",
7999                    Tab));
8000   EXPECT_EQ("/* some\n"
8001             "   comment */",
8002            format(" \t \t /* some\n"
8003                   " \t \t    comment */",
8004                   Tab));
8005   EXPECT_EQ("int a; /* some\n"
8006             "   comment */",
8007            format(" \t \t int a; /* some\n"
8008                   " \t \t    comment */",
8009                   Tab));
8010 
8011   EXPECT_EQ("int a; /* some\n"
8012             "comment */",
8013            format(" \t \t int\ta; /* some\n"
8014                   " \t \t    comment */",
8015                   Tab));
8016   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8017             "    comment */",
8018            format(" \t \t f(\"\t\t\"); /* some\n"
8019                   " \t \t    comment */",
8020                   Tab));
8021   EXPECT_EQ("{\n"
8022             "  /*\n"
8023             "   * Comment\n"
8024             "   */\n"
8025             "  int i;\n"
8026             "}",
8027             format("{\n"
8028                    "\t/*\n"
8029                    "\t * Comment\n"
8030                    "\t */\n"
8031                    "\t int i;\n"
8032                    "}"));
8033 }
8034 
8035 TEST_F(FormatTest, CalculatesOriginalColumn) {
8036   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8037             "q\"; /* some\n"
8038             "       comment */",
8039             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8040                    "q\"; /* some\n"
8041                    "       comment */",
8042                    getLLVMStyle()));
8043   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8044             "/* some\n"
8045             "   comment */",
8046             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8047                    " /* some\n"
8048                    "    comment */",
8049                    getLLVMStyle()));
8050   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8051             "qqq\n"
8052             "/* some\n"
8053             "   comment */",
8054             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8055                    "qqq\n"
8056                    " /* some\n"
8057                    "    comment */",
8058                    getLLVMStyle()));
8059   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8060             "wwww; /* some\n"
8061             "         comment */",
8062             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8063                    "wwww; /* some\n"
8064                    "         comment */",
8065                    getLLVMStyle()));
8066 }
8067 
8068 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8069   FormatStyle NoSpace = getLLVMStyle();
8070   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8071 
8072   verifyFormat("while(true)\n"
8073                "  continue;", NoSpace);
8074   verifyFormat("for(;;)\n"
8075                "  continue;", NoSpace);
8076   verifyFormat("if(true)\n"
8077                "  f();\n"
8078                "else if(true)\n"
8079                "  f();", NoSpace);
8080   verifyFormat("do {\n"
8081                "  do_something();\n"
8082                "} while(something());", NoSpace);
8083   verifyFormat("switch(x) {\n"
8084                "default:\n"
8085                "  break;\n"
8086                "}", NoSpace);
8087   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8088   verifyFormat("size_t x = sizeof(x);", NoSpace);
8089   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8090   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8091   verifyFormat("alignas(128) char a[128];", NoSpace);
8092   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8093   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8094   verifyFormat("int f() throw(Deprecated);", NoSpace);
8095 
8096   FormatStyle Space = getLLVMStyle();
8097   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8098 
8099   verifyFormat("int f ();", Space);
8100   verifyFormat("void f (int a, T b) {\n"
8101                "  while (true)\n"
8102                "    continue;\n"
8103                "}",
8104                Space);
8105   verifyFormat("if (true)\n"
8106                "  f ();\n"
8107                "else if (true)\n"
8108                "  f ();",
8109                Space);
8110   verifyFormat("do {\n"
8111                "  do_something ();\n"
8112                "} while (something ());",
8113                Space);
8114   verifyFormat("switch (x) {\n"
8115                "default:\n"
8116                "  break;\n"
8117                "}",
8118                Space);
8119   verifyFormat("A::A () : a (1) {}", Space);
8120   verifyFormat("void f () __attribute__ ((asdf));", Space);
8121   verifyFormat("*(&a + 1);\n"
8122                "&((&a)[1]);\n"
8123                "a[(b + c) * d];\n"
8124                "(((a + 1) * 2) + 3) * 4;",
8125                Space);
8126   verifyFormat("#define A(x) x", Space);
8127   verifyFormat("#define A (x) x", Space);
8128   verifyFormat("#if defined(x)\n"
8129                "#endif",
8130                Space);
8131   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8132   verifyFormat("size_t x = sizeof (x);", Space);
8133   verifyFormat("auto f (int x) -> decltype (x);", Space);
8134   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8135   verifyFormat("alignas (128) char a[128];", Space);
8136   verifyFormat("size_t x = alignof (MyType);", Space);
8137   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8138   verifyFormat("int f () throw (Deprecated);", Space);
8139 }
8140 
8141 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8142   FormatStyle Spaces = getLLVMStyle();
8143 
8144   Spaces.SpacesInParentheses = true;
8145   verifyFormat("call( x, y, z );", Spaces);
8146   verifyFormat("while ( (bool)1 )\n"
8147                "  continue;", Spaces);
8148   verifyFormat("for ( ;; )\n"
8149                "  continue;", Spaces);
8150   verifyFormat("if ( true )\n"
8151                "  f();\n"
8152                "else if ( true )\n"
8153                "  f();", Spaces);
8154   verifyFormat("do {\n"
8155                "  do_something( (int)i );\n"
8156                "} while ( something() );", Spaces);
8157   verifyFormat("switch ( x ) {\n"
8158                "default:\n"
8159                "  break;\n"
8160                "}", Spaces);
8161 
8162   Spaces.SpacesInParentheses = false;
8163   Spaces.SpacesInCStyleCastParentheses = true;
8164   verifyFormat("Type *A = ( Type * )P;", Spaces);
8165   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8166   verifyFormat("x = ( int32 )y;", Spaces);
8167   verifyFormat("int a = ( int )(2.0f);", Spaces);
8168   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8169   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8170   verifyFormat("#define x (( int )-1)", Spaces);
8171 
8172   Spaces.SpacesInParentheses = false;
8173   Spaces.SpaceInEmptyParentheses = true;
8174   verifyFormat("call(x, y, z);", Spaces);
8175   verifyFormat("call( )", Spaces);
8176 
8177   // Run the first set of tests again with
8178   // Spaces.SpacesInParentheses = false,
8179   // Spaces.SpaceInEmptyParentheses = true and
8180   // Spaces.SpacesInCStyleCastParentheses = true
8181   Spaces.SpacesInParentheses = false,
8182   Spaces.SpaceInEmptyParentheses = true;
8183   Spaces.SpacesInCStyleCastParentheses = true;
8184   verifyFormat("call(x, y, z);", Spaces);
8185   verifyFormat("while (( bool )1)\n"
8186                "  continue;", Spaces);
8187   verifyFormat("for (;;)\n"
8188                "  continue;", Spaces);
8189   verifyFormat("if (true)\n"
8190                "  f( );\n"
8191                "else if (true)\n"
8192                "  f( );", Spaces);
8193   verifyFormat("do {\n"
8194                "  do_something(( int )i);\n"
8195                "} while (something( ));", Spaces);
8196   verifyFormat("switch (x) {\n"
8197                "default:\n"
8198                "  break;\n"
8199                "}", Spaces);
8200 
8201   Spaces.SpaceAfterCStyleCast = true;
8202   verifyFormat("call(x, y, z);", Spaces);
8203   verifyFormat("while (( bool ) 1)\n"
8204                "  continue;",
8205                Spaces);
8206   verifyFormat("for (;;)\n"
8207                "  continue;",
8208                Spaces);
8209   verifyFormat("if (true)\n"
8210                "  f( );\n"
8211                "else if (true)\n"
8212                "  f( );",
8213                Spaces);
8214   verifyFormat("do {\n"
8215                "  do_something(( int ) i);\n"
8216                "} while (something( ));",
8217                Spaces);
8218   verifyFormat("switch (x) {\n"
8219                "default:\n"
8220                "  break;\n"
8221                "}",
8222                Spaces);
8223   Spaces.SpacesInCStyleCastParentheses = false;
8224   Spaces.SpaceAfterCStyleCast = true;
8225   verifyFormat("while ((bool) 1)\n"
8226                "  continue;",
8227                Spaces);
8228   verifyFormat("do {\n"
8229                "  do_something((int) i);\n"
8230                "} while (something( ));",
8231                Spaces);
8232 }
8233 
8234 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8235   verifyFormat("int a[5];");
8236   verifyFormat("a[3] += 42;");
8237 
8238   FormatStyle Spaces = getLLVMStyle();
8239   Spaces.SpacesInSquareBrackets = true;
8240   // Lambdas unchanged.
8241   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8242   verifyFormat("return [i, args...] {};", Spaces);
8243 
8244   // Not lambdas.
8245   verifyFormat("int a[ 5 ];", Spaces);
8246   verifyFormat("a[ 3 ] += 42;", Spaces);
8247   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8248   verifyFormat("double &operator[](int i) { return 0; }\n"
8249                "int i;",
8250                Spaces);
8251   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8252   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8253   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8254 }
8255 
8256 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8257   verifyFormat("int a = 5;");
8258   verifyFormat("a += 42;");
8259   verifyFormat("a or_eq 8;");
8260 
8261   FormatStyle Spaces = getLLVMStyle();
8262   Spaces.SpaceBeforeAssignmentOperators = false;
8263   verifyFormat("int a= 5;", Spaces);
8264   verifyFormat("a+= 42;", Spaces);
8265   verifyFormat("a or_eq 8;", Spaces);
8266 }
8267 
8268 TEST_F(FormatTest, LinuxBraceBreaking) {
8269   FormatStyle LinuxBraceStyle = getLLVMStyle();
8270   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8271   verifyFormat("namespace a\n"
8272                "{\n"
8273                "class A\n"
8274                "{\n"
8275                "  void f()\n"
8276                "  {\n"
8277                "    if (true) {\n"
8278                "      a();\n"
8279                "      b();\n"
8280                "    }\n"
8281                "  }\n"
8282                "  void g() { return; }\n"
8283                "};\n"
8284                "struct B {\n"
8285                "  int x;\n"
8286                "};\n"
8287                "}\n",
8288                LinuxBraceStyle);
8289   verifyFormat("enum X {\n"
8290                "  Y = 0,\n"
8291                "}\n",
8292                LinuxBraceStyle);
8293   verifyFormat("struct S {\n"
8294                "  int Type;\n"
8295                "  union {\n"
8296                "    int x;\n"
8297                "    double y;\n"
8298                "  } Value;\n"
8299                "  class C\n"
8300                "  {\n"
8301                "    MyFavoriteType Value;\n"
8302                "  } Class;\n"
8303                "}\n",
8304                LinuxBraceStyle);
8305 }
8306 
8307 TEST_F(FormatTest, StroustrupBraceBreaking) {
8308   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8309   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8310   verifyFormat("namespace a {\n"
8311                "class A {\n"
8312                "  void f()\n"
8313                "  {\n"
8314                "    if (true) {\n"
8315                "      a();\n"
8316                "      b();\n"
8317                "    }\n"
8318                "  }\n"
8319                "  void g() { return; }\n"
8320                "};\n"
8321                "struct B {\n"
8322                "  int x;\n"
8323                "};\n"
8324                "}\n",
8325                StroustrupBraceStyle);
8326 
8327   verifyFormat("void foo()\n"
8328                "{\n"
8329                "  if (a) {\n"
8330                "    a();\n"
8331                "  }\n"
8332                "  else {\n"
8333                "    b();\n"
8334                "  }\n"
8335                "}\n",
8336                StroustrupBraceStyle);
8337 
8338   verifyFormat("#ifdef _DEBUG\n"
8339                "int foo(int i = 0)\n"
8340                "#else\n"
8341                "int foo(int i = 5)\n"
8342                "#endif\n"
8343                "{\n"
8344                "  return i;\n"
8345                "}",
8346                StroustrupBraceStyle);
8347 
8348   verifyFormat("void foo() {}\n"
8349                "void bar()\n"
8350                "#ifdef _DEBUG\n"
8351                "{\n"
8352                "  foo();\n"
8353                "}\n"
8354                "#else\n"
8355                "{\n"
8356                "}\n"
8357                "#endif",
8358                StroustrupBraceStyle);
8359 
8360   verifyFormat("void foobar() { int i = 5; }\n"
8361                "#ifdef _DEBUG\n"
8362                "void bar() {}\n"
8363                "#else\n"
8364                "void bar() { foobar(); }\n"
8365                "#endif",
8366                StroustrupBraceStyle);
8367 }
8368 
8369 TEST_F(FormatTest, AllmanBraceBreaking) {
8370   FormatStyle AllmanBraceStyle = getLLVMStyle();
8371   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8372   verifyFormat("namespace a\n"
8373                "{\n"
8374                "class A\n"
8375                "{\n"
8376                "  void f()\n"
8377                "  {\n"
8378                "    if (true)\n"
8379                "    {\n"
8380                "      a();\n"
8381                "      b();\n"
8382                "    }\n"
8383                "  }\n"
8384                "  void g() { return; }\n"
8385                "};\n"
8386                "struct B\n"
8387                "{\n"
8388                "  int x;\n"
8389                "};\n"
8390                "}",
8391                AllmanBraceStyle);
8392 
8393   verifyFormat("void f()\n"
8394                "{\n"
8395                "  if (true)\n"
8396                "  {\n"
8397                "    a();\n"
8398                "  }\n"
8399                "  else if (false)\n"
8400                "  {\n"
8401                "    b();\n"
8402                "  }\n"
8403                "  else\n"
8404                "  {\n"
8405                "    c();\n"
8406                "  }\n"
8407                "}\n",
8408                AllmanBraceStyle);
8409 
8410   verifyFormat("void f()\n"
8411                "{\n"
8412                "  for (int i = 0; i < 10; ++i)\n"
8413                "  {\n"
8414                "    a();\n"
8415                "  }\n"
8416                "  while (false)\n"
8417                "  {\n"
8418                "    b();\n"
8419                "  }\n"
8420                "  do\n"
8421                "  {\n"
8422                "    c();\n"
8423                "  } while (false)\n"
8424                "}\n",
8425                AllmanBraceStyle);
8426 
8427   verifyFormat("void f(int a)\n"
8428                "{\n"
8429                "  switch (a)\n"
8430                "  {\n"
8431                "  case 0:\n"
8432                "    break;\n"
8433                "  case 1:\n"
8434                "  {\n"
8435                "    break;\n"
8436                "  }\n"
8437                "  case 2:\n"
8438                "  {\n"
8439                "  }\n"
8440                "  break;\n"
8441                "  default:\n"
8442                "    break;\n"
8443                "  }\n"
8444                "}\n",
8445                AllmanBraceStyle);
8446 
8447   verifyFormat("enum X\n"
8448                "{\n"
8449                "  Y = 0,\n"
8450                "}\n",
8451                AllmanBraceStyle);
8452   verifyFormat("enum X\n"
8453                "{\n"
8454                "  Y = 0\n"
8455                "}\n",
8456                AllmanBraceStyle);
8457 
8458   verifyFormat("@interface BSApplicationController ()\n"
8459                "{\n"
8460                "@private\n"
8461                "  id _extraIvar;\n"
8462                "}\n"
8463                "@end\n",
8464                AllmanBraceStyle);
8465 
8466   verifyFormat("#ifdef _DEBUG\n"
8467                "int foo(int i = 0)\n"
8468                "#else\n"
8469                "int foo(int i = 5)\n"
8470                "#endif\n"
8471                "{\n"
8472                "  return i;\n"
8473                "}",
8474                AllmanBraceStyle);
8475 
8476   verifyFormat("void foo() {}\n"
8477                "void bar()\n"
8478                "#ifdef _DEBUG\n"
8479                "{\n"
8480                "  foo();\n"
8481                "}\n"
8482                "#else\n"
8483                "{\n"
8484                "}\n"
8485                "#endif",
8486                AllmanBraceStyle);
8487 
8488   verifyFormat("void foobar() { int i = 5; }\n"
8489                "#ifdef _DEBUG\n"
8490                "void bar() {}\n"
8491                "#else\n"
8492                "void bar() { foobar(); }\n"
8493                "#endif",
8494                AllmanBraceStyle);
8495 
8496   // This shouldn't affect ObjC blocks..
8497   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8498                "  // ...\n"
8499                "  int i;\n"
8500                "}];",
8501                AllmanBraceStyle);
8502   verifyFormat("void (^block)(void) = ^{\n"
8503                "  // ...\n"
8504                "  int i;\n"
8505                "};",
8506                AllmanBraceStyle);
8507   // .. or dict literals.
8508   verifyFormat("void f()\n"
8509                "{\n"
8510                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8511                "}",
8512                AllmanBraceStyle);
8513   verifyFormat("int f()\n"
8514                "{ // comment\n"
8515                "  return 42;\n"
8516                "}",
8517                AllmanBraceStyle);
8518 
8519   AllmanBraceStyle.ColumnLimit = 19;
8520   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8521   AllmanBraceStyle.ColumnLimit = 18;
8522   verifyFormat("void f()\n"
8523                "{\n"
8524                "  int i;\n"
8525                "}",
8526                AllmanBraceStyle);
8527   AllmanBraceStyle.ColumnLimit = 80;
8528 
8529   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8530   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8531   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8532   verifyFormat("void f(bool b)\n"
8533                "{\n"
8534                "  if (b)\n"
8535                "  {\n"
8536                "    return;\n"
8537                "  }\n"
8538                "}\n",
8539                BreakBeforeBraceShortIfs);
8540   verifyFormat("void f(bool b)\n"
8541                "{\n"
8542                "  if (b) return;\n"
8543                "}\n",
8544                BreakBeforeBraceShortIfs);
8545   verifyFormat("void f(bool b)\n"
8546                "{\n"
8547                "  while (b)\n"
8548                "  {\n"
8549                "    return;\n"
8550                "  }\n"
8551                "}\n",
8552                BreakBeforeBraceShortIfs);
8553 }
8554 
8555 TEST_F(FormatTest, GNUBraceBreaking) {
8556   FormatStyle GNUBraceStyle = getLLVMStyle();
8557   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8558   verifyFormat("namespace a\n"
8559                "{\n"
8560                "class A\n"
8561                "{\n"
8562                "  void f()\n"
8563                "  {\n"
8564                "    int a;\n"
8565                "    {\n"
8566                "      int b;\n"
8567                "    }\n"
8568                "    if (true)\n"
8569                "      {\n"
8570                "        a();\n"
8571                "        b();\n"
8572                "      }\n"
8573                "  }\n"
8574                "  void g() { return; }\n"
8575                "}\n"
8576                "}",
8577                GNUBraceStyle);
8578 
8579   verifyFormat("void f()\n"
8580                "{\n"
8581                "  if (true)\n"
8582                "    {\n"
8583                "      a();\n"
8584                "    }\n"
8585                "  else if (false)\n"
8586                "    {\n"
8587                "      b();\n"
8588                "    }\n"
8589                "  else\n"
8590                "    {\n"
8591                "      c();\n"
8592                "    }\n"
8593                "}\n",
8594                GNUBraceStyle);
8595 
8596   verifyFormat("void f()\n"
8597                "{\n"
8598                "  for (int i = 0; i < 10; ++i)\n"
8599                "    {\n"
8600                "      a();\n"
8601                "    }\n"
8602                "  while (false)\n"
8603                "    {\n"
8604                "      b();\n"
8605                "    }\n"
8606                "  do\n"
8607                "    {\n"
8608                "      c();\n"
8609                "    }\n"
8610                "  while (false);\n"
8611                "}\n",
8612                GNUBraceStyle);
8613 
8614   verifyFormat("void f(int a)\n"
8615                "{\n"
8616                "  switch (a)\n"
8617                "    {\n"
8618                "    case 0:\n"
8619                "      break;\n"
8620                "    case 1:\n"
8621                "      {\n"
8622                "        break;\n"
8623                "      }\n"
8624                "    case 2:\n"
8625                "      {\n"
8626                "      }\n"
8627                "      break;\n"
8628                "    default:\n"
8629                "      break;\n"
8630                "    }\n"
8631                "}\n",
8632                GNUBraceStyle);
8633 
8634   verifyFormat("enum X\n"
8635                "{\n"
8636                "  Y = 0,\n"
8637                "}\n",
8638                GNUBraceStyle);
8639 
8640   verifyFormat("@interface BSApplicationController ()\n"
8641                "{\n"
8642                "@private\n"
8643                "  id _extraIvar;\n"
8644                "}\n"
8645                "@end\n",
8646                GNUBraceStyle);
8647 
8648   verifyFormat("#ifdef _DEBUG\n"
8649                "int foo(int i = 0)\n"
8650                "#else\n"
8651                "int foo(int i = 5)\n"
8652                "#endif\n"
8653                "{\n"
8654                "  return i;\n"
8655                "}",
8656                GNUBraceStyle);
8657 
8658   verifyFormat("void foo() {}\n"
8659                "void bar()\n"
8660                "#ifdef _DEBUG\n"
8661                "{\n"
8662                "  foo();\n"
8663                "}\n"
8664                "#else\n"
8665                "{\n"
8666                "}\n"
8667                "#endif",
8668                GNUBraceStyle);
8669 
8670   verifyFormat("void foobar() { int i = 5; }\n"
8671                "#ifdef _DEBUG\n"
8672                "void bar() {}\n"
8673                "#else\n"
8674                "void bar() { foobar(); }\n"
8675                "#endif",
8676                GNUBraceStyle);
8677 }
8678 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8679   verifyFormat("void f() {\n"
8680                "  try {\n"
8681                "  } catch (const Exception &e) {\n"
8682                "  }\n"
8683                "}\n",
8684                getLLVMStyle());
8685 }
8686 
8687 TEST_F(FormatTest, UnderstandsPragmas) {
8688   verifyFormat("#pragma omp reduction(| : var)");
8689   verifyFormat("#pragma omp reduction(+ : var)");
8690 
8691   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8692             "(including parentheses).",
8693             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8694                    "(including parentheses)."));
8695 }
8696 
8697 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8698   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8699     EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of "                \
8700                                     << Styles.size()                           \
8701                                     << " differs from Style #0"
8702 
8703 TEST_F(FormatTest, GetsPredefinedStyleByName) {
8704   SmallVector<FormatStyle, 3> Styles;
8705   Styles.resize(3);
8706 
8707   Styles[0] = getLLVMStyle();
8708   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
8709   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
8710   EXPECT_ALL_STYLES_EQUAL(Styles);
8711 
8712   Styles[0] = getGoogleStyle();
8713   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
8714   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
8715   EXPECT_ALL_STYLES_EQUAL(Styles);
8716 
8717   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8718   EXPECT_TRUE(
8719       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
8720   EXPECT_TRUE(
8721       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
8722   EXPECT_ALL_STYLES_EQUAL(Styles);
8723 
8724   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
8725   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
8726   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
8727   EXPECT_ALL_STYLES_EQUAL(Styles);
8728 
8729   Styles[0] = getMozillaStyle();
8730   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
8731   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
8732   EXPECT_ALL_STYLES_EQUAL(Styles);
8733 
8734   Styles[0] = getWebKitStyle();
8735   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
8736   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
8737   EXPECT_ALL_STYLES_EQUAL(Styles);
8738 
8739   Styles[0] = getGNUStyle();
8740   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
8741   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
8742   EXPECT_ALL_STYLES_EQUAL(Styles);
8743 
8744   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
8745 }
8746 
8747 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
8748   SmallVector<FormatStyle, 8> Styles;
8749   Styles.resize(2);
8750 
8751   Styles[0] = getGoogleStyle();
8752   Styles[1] = getLLVMStyle();
8753   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8754   EXPECT_ALL_STYLES_EQUAL(Styles);
8755 
8756   Styles.resize(5);
8757   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
8758   Styles[1] = getLLVMStyle();
8759   Styles[1].Language = FormatStyle::LK_JavaScript;
8760   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
8761 
8762   Styles[2] = getLLVMStyle();
8763   Styles[2].Language = FormatStyle::LK_JavaScript;
8764   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
8765                                   "BasedOnStyle: Google",
8766                                   &Styles[2]).value());
8767 
8768   Styles[3] = getLLVMStyle();
8769   Styles[3].Language = FormatStyle::LK_JavaScript;
8770   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
8771                                   "Language: JavaScript",
8772                                   &Styles[3]).value());
8773 
8774   Styles[4] = getLLVMStyle();
8775   Styles[4].Language = FormatStyle::LK_JavaScript;
8776   EXPECT_EQ(0, parseConfiguration("---\n"
8777                                   "BasedOnStyle: LLVM\n"
8778                                   "IndentWidth: 123\n"
8779                                   "---\n"
8780                                   "BasedOnStyle: Google\n"
8781                                   "Language: JavaScript",
8782                                   &Styles[4]).value());
8783   EXPECT_ALL_STYLES_EQUAL(Styles);
8784 }
8785 
8786 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
8787   Style.FIELD = false;                                                         \
8788   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
8789   EXPECT_TRUE(Style.FIELD);                                                    \
8790   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
8791   EXPECT_FALSE(Style.FIELD);
8792 
8793 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
8794 
8795 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
8796   EXPECT_NE(VALUE, Style.FIELD);                                               \
8797   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
8798   EXPECT_EQ(VALUE, Style.FIELD)
8799 
8800 TEST_F(FormatTest, ParsesConfigurationBools) {
8801   FormatStyle Style = {};
8802   Style.Language = FormatStyle::LK_Cpp;
8803   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
8804   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
8805   CHECK_PARSE_BOOL(AlignOperands);
8806   CHECK_PARSE_BOOL(AlignTrailingComments);
8807   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
8808   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
8809   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
8810   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
8811   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
8812   CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
8813   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
8814   CHECK_PARSE_BOOL(BinPackParameters);
8815   CHECK_PARSE_BOOL(BinPackArguments);
8816   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
8817   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
8818   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
8819   CHECK_PARSE_BOOL(DerivePointerAlignment);
8820   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
8821   CHECK_PARSE_BOOL(IndentCaseLabels);
8822   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
8823   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
8824   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
8825   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
8826   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
8827   CHECK_PARSE_BOOL(SpacesInParentheses);
8828   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
8829   CHECK_PARSE_BOOL(SpacesInAngles);
8830   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
8831   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
8832   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
8833   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
8834   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
8835 }
8836 
8837 #undef CHECK_PARSE_BOOL
8838 
8839 TEST_F(FormatTest, ParsesConfiguration) {
8840   FormatStyle Style = {};
8841   Style.Language = FormatStyle::LK_Cpp;
8842   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
8843   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
8844               ConstructorInitializerIndentWidth, 1234u);
8845   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
8846   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
8847   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
8848   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
8849               PenaltyBreakBeforeFirstCallParameter, 1234u);
8850   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
8851   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
8852               PenaltyReturnTypeOnItsOwnLine, 1234u);
8853   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
8854               SpacesBeforeTrailingComments, 1234u);
8855   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
8856   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
8857 
8858   Style.PointerAlignment = FormatStyle::PAS_Middle;
8859   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
8860               FormatStyle::PAS_Left);
8861   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
8862               FormatStyle::PAS_Right);
8863   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
8864               FormatStyle::PAS_Middle);
8865   // For backward compatibility:
8866   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
8867               FormatStyle::PAS_Left);
8868   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
8869               FormatStyle::PAS_Right);
8870   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
8871               FormatStyle::PAS_Middle);
8872 
8873   Style.Standard = FormatStyle::LS_Auto;
8874   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
8875   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
8876   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
8877   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
8878   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
8879 
8880   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8881   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
8882               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
8883   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
8884               FormatStyle::BOS_None);
8885   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
8886               FormatStyle::BOS_All);
8887   // For backward compatibility:
8888   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
8889               FormatStyle::BOS_None);
8890   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
8891               FormatStyle::BOS_All);
8892 
8893   Style.UseTab = FormatStyle::UT_ForIndentation;
8894   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
8895   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
8896   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
8897   // For backward compatibility:
8898   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
8899   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
8900 
8901   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
8902   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
8903               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8904   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
8905               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
8906   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
8907               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
8908   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
8909               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8910   // For backward compatibility:
8911   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
8912               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
8913   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
8914               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
8915 
8916   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
8917   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
8918               FormatStyle::SBPO_Never);
8919   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
8920               FormatStyle::SBPO_Always);
8921   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
8922               FormatStyle::SBPO_ControlStatements);
8923   // For backward compatibility:
8924   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
8925               FormatStyle::SBPO_Never);
8926   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
8927               FormatStyle::SBPO_ControlStatements);
8928 
8929   Style.ColumnLimit = 123;
8930   FormatStyle BaseStyle = getLLVMStyle();
8931   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
8932   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
8933 
8934   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8935   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
8936               FormatStyle::BS_Attach);
8937   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
8938               FormatStyle::BS_Linux);
8939   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
8940               FormatStyle::BS_Stroustrup);
8941   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
8942               FormatStyle::BS_Allman);
8943   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
8944 
8945   Style.NamespaceIndentation = FormatStyle::NI_All;
8946   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
8947               FormatStyle::NI_None);
8948   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
8949               FormatStyle::NI_Inner);
8950   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
8951               FormatStyle::NI_All);
8952 
8953   Style.ForEachMacros.clear();
8954   std::vector<std::string> BoostForeach;
8955   BoostForeach.push_back("BOOST_FOREACH");
8956   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
8957   std::vector<std::string> BoostAndQForeach;
8958   BoostAndQForeach.push_back("BOOST_FOREACH");
8959   BoostAndQForeach.push_back("Q_FOREACH");
8960   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
8961               BoostAndQForeach);
8962 }
8963 
8964 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
8965   FormatStyle Style = {};
8966   Style.Language = FormatStyle::LK_Cpp;
8967   CHECK_PARSE("Language: Cpp\n"
8968               "IndentWidth: 12",
8969               IndentWidth, 12u);
8970   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
8971                                "IndentWidth: 34",
8972                                &Style),
8973             ParseError::Unsuitable);
8974   EXPECT_EQ(12u, Style.IndentWidth);
8975   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8976   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
8977 
8978   Style.Language = FormatStyle::LK_JavaScript;
8979   CHECK_PARSE("Language: JavaScript\n"
8980               "IndentWidth: 12",
8981               IndentWidth, 12u);
8982   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
8983   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
8984                                "IndentWidth: 34",
8985                                &Style),
8986             ParseError::Unsuitable);
8987   EXPECT_EQ(23u, Style.IndentWidth);
8988   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
8989   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
8990 
8991   CHECK_PARSE("BasedOnStyle: LLVM\n"
8992               "IndentWidth: 67",
8993               IndentWidth, 67u);
8994 
8995   CHECK_PARSE("---\n"
8996               "Language: JavaScript\n"
8997               "IndentWidth: 12\n"
8998               "---\n"
8999               "Language: Cpp\n"
9000               "IndentWidth: 34\n"
9001               "...\n",
9002               IndentWidth, 12u);
9003 
9004   Style.Language = FormatStyle::LK_Cpp;
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, 34u);
9013   CHECK_PARSE("---\n"
9014               "IndentWidth: 78\n"
9015               "---\n"
9016               "Language: JavaScript\n"
9017               "IndentWidth: 56\n"
9018               "...\n",
9019               IndentWidth, 78u);
9020 
9021   Style.ColumnLimit = 123;
9022   Style.IndentWidth = 234;
9023   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9024   Style.TabWidth = 345;
9025   EXPECT_FALSE(parseConfiguration("---\n"
9026                                   "IndentWidth: 456\n"
9027                                   "BreakBeforeBraces: Allman\n"
9028                                   "---\n"
9029                                   "Language: JavaScript\n"
9030                                   "IndentWidth: 111\n"
9031                                   "TabWidth: 111\n"
9032                                   "---\n"
9033                                   "Language: Cpp\n"
9034                                   "BreakBeforeBraces: Stroustrup\n"
9035                                   "TabWidth: 789\n"
9036                                   "...\n",
9037                                   &Style));
9038   EXPECT_EQ(123u, Style.ColumnLimit);
9039   EXPECT_EQ(456u, Style.IndentWidth);
9040   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9041   EXPECT_EQ(789u, Style.TabWidth);
9042 
9043   EXPECT_EQ(parseConfiguration("---\n"
9044                                "Language: JavaScript\n"
9045                                "IndentWidth: 56\n"
9046                                "---\n"
9047                                "IndentWidth: 78\n"
9048                                "...\n",
9049                                &Style),
9050             ParseError::Error);
9051   EXPECT_EQ(parseConfiguration("---\n"
9052                                "Language: JavaScript\n"
9053                                "IndentWidth: 56\n"
9054                                "---\n"
9055                                "Language: JavaScript\n"
9056                                "IndentWidth: 78\n"
9057                                "...\n",
9058                                &Style),
9059             ParseError::Error);
9060 
9061   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9062 }
9063 
9064 #undef CHECK_PARSE
9065 
9066 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9067   FormatStyle Style = {};
9068   Style.Language = FormatStyle::LK_JavaScript;
9069   Style.BreakBeforeTernaryOperators = true;
9070   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9071   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9072 
9073   Style.BreakBeforeTernaryOperators = true;
9074   EXPECT_EQ(0, parseConfiguration("---\n"
9075               "BasedOnStyle: Google\n"
9076               "---\n"
9077               "Language: JavaScript\n"
9078               "IndentWidth: 76\n"
9079               "...\n", &Style).value());
9080   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9081   EXPECT_EQ(76u, Style.IndentWidth);
9082   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9083 }
9084 
9085 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9086   FormatStyle Style = getLLVMStyle();
9087   std::string YAML = configurationAsText(Style);
9088   FormatStyle ParsedStyle = {};
9089   ParsedStyle.Language = FormatStyle::LK_Cpp;
9090   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9091   EXPECT_EQ(Style, ParsedStyle);
9092 }
9093 
9094 TEST_F(FormatTest, WorksFor8bitEncodings) {
9095   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9096             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9097             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9098             "\"\xef\xee\xf0\xf3...\"",
9099             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9100                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9101                    "\xef\xee\xf0\xf3...\"",
9102                    getLLVMStyleWithColumns(12)));
9103 }
9104 
9105 TEST_F(FormatTest, HandlesUTF8BOM) {
9106   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9107   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9108             format("\xef\xbb\xbf#include <iostream>"));
9109   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9110             format("\xef\xbb\xbf\n#include <iostream>"));
9111 }
9112 
9113 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9114 #if !defined(_MSC_VER)
9115 
9116 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9117   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9118                getLLVMStyleWithColumns(35));
9119   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9120                getLLVMStyleWithColumns(31));
9121   verifyFormat("// Однажды в студёную зимнюю пору...",
9122                getLLVMStyleWithColumns(36));
9123   verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
9124                getLLVMStyleWithColumns(32));
9125   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9126                getLLVMStyleWithColumns(39));
9127   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9128                getLLVMStyleWithColumns(35));
9129 }
9130 
9131 TEST_F(FormatTest, SplitsUTF8Strings) {
9132   // Non-printable characters' width is currently considered to be the length in
9133   // bytes in UTF8. The characters can be displayed in very different manner
9134   // (zero-width, single width with a substitution glyph, expanded to their code
9135   // (e.g. "<8d>"), so there's no single correct way to handle them.
9136   EXPECT_EQ("\"aaaaÄ\"\n"
9137             "\"\xc2\x8d\";",
9138             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9139   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9140             "\"\xc2\x8d\";",
9141             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9142   EXPECT_EQ(
9143       "\"Однажды, в \"\n"
9144       "\"студёную \"\n"
9145       "\"зимнюю \"\n"
9146       "\"пору,\"",
9147       format("\"Однажды, в студёную зимнюю пору,\"",
9148              getLLVMStyleWithColumns(13)));
9149   EXPECT_EQ("\"一 二 三 \"\n"
9150             "\"四 五六 \"\n"
9151             "\"七 八 九 \"\n"
9152             "\"十\"",
9153             format("\"一 二 三 四 五六 七 八 九 十\"",
9154                    getLLVMStyleWithColumns(11)));
9155   EXPECT_EQ("\"一\t二 \"\n"
9156             "\"\t三 \"\n"
9157             "\"四 五\t六 \"\n"
9158             "\"\t七 \"\n"
9159             "\"八九十\tqq\"",
9160             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9161                    getLLVMStyleWithColumns(11)));
9162 }
9163 
9164 
9165 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9166   EXPECT_EQ("const char *sssss =\n"
9167             "    \"一二三四五六七八\\\n"
9168             " 九 十\";",
9169             format("const char *sssss = \"一二三四五六七八\\\n"
9170                    " 九 十\";",
9171                    getLLVMStyleWithColumns(30)));
9172 }
9173 
9174 TEST_F(FormatTest, SplitsUTF8LineComments) {
9175   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9176             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9177   EXPECT_EQ("// Я из лесу\n"
9178             "// вышел; был\n"
9179             "// сильный\n"
9180             "// мороз.",
9181             format("// Я из лесу вышел; был сильный мороз.",
9182                    getLLVMStyleWithColumns(13)));
9183   EXPECT_EQ("// 一二三\n"
9184             "// 四五六七\n"
9185             "// 八  九\n"
9186             "// 十",
9187             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9188 }
9189 
9190 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9191   EXPECT_EQ("/* Гляжу,\n"
9192             " * поднимается\n"
9193             " * медленно в\n"
9194             " * гору\n"
9195             " * Лошадка,\n"
9196             " * везущая\n"
9197             " * хворосту\n"
9198             " * воз. */",
9199             format("/* Гляжу, поднимается медленно в гору\n"
9200                    " * Лошадка, везущая хворосту воз. */",
9201                    getLLVMStyleWithColumns(13)));
9202   EXPECT_EQ(
9203       "/* 一二三\n"
9204       " * 四五六七\n"
9205       " * 八  九\n"
9206       " * 十  */",
9207       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9208   EXPECT_EQ("/* �������� ��������\n"
9209             " * ��������\n"
9210             " * ������-�� */",
9211             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9212 }
9213 
9214 #endif // _MSC_VER
9215 
9216 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9217   FormatStyle Style = getLLVMStyle();
9218 
9219   Style.ConstructorInitializerIndentWidth = 4;
9220   verifyFormat(
9221       "SomeClass::Constructor()\n"
9222       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9223       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9224       Style);
9225 
9226   Style.ConstructorInitializerIndentWidth = 2;
9227   verifyFormat(
9228       "SomeClass::Constructor()\n"
9229       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9230       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9231       Style);
9232 
9233   Style.ConstructorInitializerIndentWidth = 0;
9234   verifyFormat(
9235       "SomeClass::Constructor()\n"
9236       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9237       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9238       Style);
9239 }
9240 
9241 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9242   FormatStyle Style = getLLVMStyle();
9243   Style.BreakConstructorInitializersBeforeComma = true;
9244   Style.ConstructorInitializerIndentWidth = 4;
9245   verifyFormat("SomeClass::Constructor()\n"
9246                "    : a(a)\n"
9247                "    , b(b)\n"
9248                "    , c(c) {}",
9249                Style);
9250   verifyFormat("SomeClass::Constructor()\n"
9251                "    : a(a) {}",
9252                Style);
9253 
9254   Style.ColumnLimit = 0;
9255   verifyFormat("SomeClass::Constructor()\n"
9256                "    : a(a) {}",
9257                Style);
9258   verifyFormat("SomeClass::Constructor()\n"
9259                "    : a(a)\n"
9260                "    , b(b)\n"
9261                "    , c(c) {}",
9262                Style);
9263   verifyFormat("SomeClass::Constructor()\n"
9264                "    : a(a) {\n"
9265                "  foo();\n"
9266                "  bar();\n"
9267                "}",
9268                Style);
9269 
9270   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9271   verifyFormat("SomeClass::Constructor()\n"
9272                "    : a(a)\n"
9273                "    , b(b)\n"
9274                "    , c(c) {\n}",
9275                Style);
9276   verifyFormat("SomeClass::Constructor()\n"
9277                "    : a(a) {\n}",
9278                Style);
9279 
9280   Style.ColumnLimit = 80;
9281   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9282   Style.ConstructorInitializerIndentWidth = 2;
9283   verifyFormat("SomeClass::Constructor()\n"
9284                "  : a(a)\n"
9285                "  , b(b)\n"
9286                "  , c(c) {}",
9287                Style);
9288 
9289   Style.ConstructorInitializerIndentWidth = 0;
9290   verifyFormat("SomeClass::Constructor()\n"
9291                ": a(a)\n"
9292                ", b(b)\n"
9293                ", c(c) {}",
9294                Style);
9295 
9296   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9297   Style.ConstructorInitializerIndentWidth = 4;
9298   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9299   verifyFormat(
9300       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9301       Style);
9302   verifyFormat(
9303       "SomeClass::Constructor()\n"
9304       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9305       Style);
9306   Style.ConstructorInitializerIndentWidth = 4;
9307   Style.ColumnLimit = 60;
9308   verifyFormat("SomeClass::Constructor()\n"
9309                "    : aaaaaaaa(aaaaaaaa)\n"
9310                "    , aaaaaaaa(aaaaaaaa)\n"
9311                "    , aaaaaaaa(aaaaaaaa) {}",
9312                Style);
9313 }
9314 
9315 TEST_F(FormatTest, Destructors) {
9316   verifyFormat("void F(int &i) { i.~int(); }");
9317   verifyFormat("void F(int &i) { i->~int(); }");
9318 }
9319 
9320 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9321   FormatStyle Style = getWebKitStyle();
9322 
9323   // Don't indent in outer namespaces.
9324   verifyFormat("namespace outer {\n"
9325                "int i;\n"
9326                "namespace inner {\n"
9327                "    int i;\n"
9328                "} // namespace inner\n"
9329                "} // namespace outer\n"
9330                "namespace other_outer {\n"
9331                "int i;\n"
9332                "}",
9333                Style);
9334 
9335   // Don't indent case labels.
9336   verifyFormat("switch (variable) {\n"
9337                "case 1:\n"
9338                "case 2:\n"
9339                "    doSomething();\n"
9340                "    break;\n"
9341                "default:\n"
9342                "    ++variable;\n"
9343                "}",
9344                Style);
9345 
9346   // Wrap before binary operators.
9347   EXPECT_EQ(
9348       "void f()\n"
9349       "{\n"
9350       "    if (aaaaaaaaaaaaaaaa\n"
9351       "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9352       "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9353       "        return;\n"
9354       "}",
9355       format(
9356           "void f() {\n"
9357           "if (aaaaaaaaaaaaaaaa\n"
9358           "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9359           "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9360           "return;\n"
9361           "}",
9362           Style));
9363 
9364   // Allow functions on a single line.
9365   verifyFormat("void f() { return; }", Style);
9366 
9367   // Constructor initializers are formatted one per line with the "," on the
9368   // new line.
9369   verifyFormat("Constructor()\n"
9370                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9371                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9372                "          aaaaaaaaaaaaaa)\n"
9373                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9374                "{\n"
9375                "}",
9376                Style);
9377   verifyFormat("SomeClass::Constructor()\n"
9378                "    : a(a)\n"
9379                "{\n"
9380                "}",
9381                Style);
9382   EXPECT_EQ("SomeClass::Constructor()\n"
9383             "    : a(a)\n"
9384             "{\n"
9385             "}",
9386             format("SomeClass::Constructor():a(a){}", Style));
9387   verifyFormat("SomeClass::Constructor()\n"
9388                "    : a(a)\n"
9389                "    , b(b)\n"
9390                "    , c(c)\n"
9391                "{\n"
9392                "}", Style);
9393   verifyFormat("SomeClass::Constructor()\n"
9394                "    : a(a)\n"
9395                "{\n"
9396                "    foo();\n"
9397                "    bar();\n"
9398                "}",
9399                Style);
9400 
9401   // Access specifiers should be aligned left.
9402   verifyFormat("class C {\n"
9403                "public:\n"
9404                "    int i;\n"
9405                "};",
9406                Style);
9407 
9408   // Do not align comments.
9409   verifyFormat("int a; // Do not\n"
9410                "double b; // align comments.",
9411                Style);
9412 
9413   // Do not align operands.
9414   EXPECT_EQ("ASSERT(aaaa\n"
9415             "    || bbbb);",
9416             format("ASSERT ( aaaa\n||bbbb);", Style));
9417 
9418   // Accept input's line breaks.
9419   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9420             "    || bbbbbbbbbbbbbbb) {\n"
9421             "    i++;\n"
9422             "}",
9423             format("if (aaaaaaaaaaaaaaa\n"
9424                    "|| bbbbbbbbbbbbbbb) { i++; }",
9425                    Style));
9426   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9427             "    i++;\n"
9428             "}",
9429             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9430 
9431   // Don't automatically break all macro definitions (llvm.org/PR17842).
9432   verifyFormat("#define aNumber 10", Style);
9433   // However, generally keep the line breaks that the user authored.
9434   EXPECT_EQ("#define aNumber \\\n"
9435             "    10",
9436             format("#define aNumber \\\n"
9437                    " 10",
9438                    Style));
9439 
9440   // Keep empty and one-element array literals on a single line.
9441   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9442             "                                  copyItems:YES];",
9443             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9444                    "copyItems:YES];",
9445                    Style));
9446   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9447             "                                  copyItems:YES];",
9448             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9449                    "             copyItems:YES];",
9450                    Style));
9451   // FIXME: This does not seem right, there should be more indentation before
9452   // the array literal's entries. Nested blocks have the same problem.
9453   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9454             "    @\"a\",\n"
9455             "    @\"a\"\n"
9456             "]\n"
9457             "                                  copyItems:YES];",
9458             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9459                    "     @\"a\",\n"
9460                    "     @\"a\"\n"
9461                    "     ]\n"
9462                    "       copyItems:YES];",
9463                    Style));
9464   EXPECT_EQ(
9465       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9466       "                                  copyItems:YES];",
9467       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9468              "   copyItems:YES];",
9469              Style));
9470 
9471   verifyFormat("[self.a b:c c:d];", Style);
9472   EXPECT_EQ("[self.a b:c\n"
9473             "        c:d];",
9474             format("[self.a b:c\n"
9475                    "c:d];",
9476                    Style));
9477 }
9478 
9479 TEST_F(FormatTest, FormatsLambdas) {
9480   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9481   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9482   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9483   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9484   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9485   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9486   verifyFormat("void f() {\n"
9487                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9488                "}\n");
9489   verifyFormat("void f() {\n"
9490                "  other(x.begin(), //\n"
9491                "        x.end(),   //\n"
9492                "        [&](int, int) { return 1; });\n"
9493                "}\n");
9494   verifyFormat("SomeFunction([]() { // A cool function...\n"
9495                "  return 43;\n"
9496                "});");
9497   EXPECT_EQ("SomeFunction([]() {\n"
9498             "#define A a\n"
9499             "  return 43;\n"
9500             "});",
9501             format("SomeFunction([](){\n"
9502                    "#define A a\n"
9503                    "return 43;\n"
9504                    "});"));
9505   verifyFormat("void f() {\n"
9506                "  SomeFunction([](decltype(x), A *a) {});\n"
9507                "}");
9508   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9509                "    [](const aaaaaaaaaa &a) { return a; });");
9510   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9511                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9512                "});");
9513   verifyFormat("Constructor()\n"
9514                "    : Field([] { // comment\n"
9515                "        int i;\n"
9516                "      }) {}");
9517 
9518   // Lambdas with return types.
9519   verifyFormat("int c = []() -> int { return 2; }();\n");
9520   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9521   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9522   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9523   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9524   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9525   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9526   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9527                "                   int j) -> int {\n"
9528                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9529                "};");
9530 
9531   // Multiple lambdas in the same parentheses change indentation rules.
9532   verifyFormat("SomeFunction(\n"
9533                "    []() {\n"
9534                "      int i = 42;\n"
9535                "      return i;\n"
9536                "    },\n"
9537                "    []() {\n"
9538                "      int j = 43;\n"
9539                "      return j;\n"
9540                "    });");
9541 
9542   // More complex introducers.
9543   verifyFormat("return [i, args...] {};");
9544 
9545   // Not lambdas.
9546   verifyFormat("constexpr char hello[]{\"hello\"};");
9547   verifyFormat("double &operator[](int i) { return 0; }\n"
9548                "int i;");
9549   verifyFormat("std::unique_ptr<int[]> foo() {}");
9550   verifyFormat("int i = a[a][a]->f();");
9551   verifyFormat("int i = (*b)[a]->f();");
9552 
9553   // Other corner cases.
9554   verifyFormat("void f() {\n"
9555                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9556                "      );\n"
9557                "}");
9558 
9559   // Lambdas created through weird macros.
9560   verifyFormat("void f() {\n"
9561                "  MACRO((const AA &a) { return 1; });\n"
9562                "}");
9563 
9564   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9565                "      doo_dah();\n"
9566                "      doo_dah();\n"
9567                "    })) {\n"
9568                "}");
9569   verifyFormat("auto lambda = []() {\n"
9570                "  int a = 2\n"
9571                "#if A\n"
9572                "          + 2\n"
9573                "#endif\n"
9574                "      ;\n"
9575                "};");
9576 }
9577 
9578 TEST_F(FormatTest, FormatsBlocks) {
9579   FormatStyle ShortBlocks = getLLVMStyle();
9580   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9581   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9582   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9583   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9584   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9585   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9586   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9587 
9588   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9589   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9590   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9591 
9592   verifyFormat("[operation setCompletionBlock:^{\n"
9593                "  [self onOperationDone];\n"
9594                "}];");
9595   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9596                "  [self onOperationDone];\n"
9597                "}]};");
9598   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9599                "  f();\n"
9600                "}];");
9601   verifyFormat("int a = [operation block:^int(int *i) {\n"
9602                "  return 1;\n"
9603                "}];");
9604   verifyFormat("[myObject doSomethingWith:arg1\n"
9605                "                      aaa:^int(int *a) {\n"
9606                "                        return 1;\n"
9607                "                      }\n"
9608                "                      bbb:f(a * bbbbbbbb)];");
9609 
9610   verifyFormat("[operation setCompletionBlock:^{\n"
9611                "  [self.delegate newDataAvailable];\n"
9612                "}];",
9613                getLLVMStyleWithColumns(60));
9614   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9615                "  NSString *path = [self sessionFilePath];\n"
9616                "  if (path) {\n"
9617                "    // ...\n"
9618                "  }\n"
9619                "});");
9620   verifyFormat("[[SessionService sharedService]\n"
9621                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9622                "      if (window) {\n"
9623                "        [self windowDidLoad:window];\n"
9624                "      } else {\n"
9625                "        [self errorLoadingWindow];\n"
9626                "      }\n"
9627                "    }];");
9628   verifyFormat("void (^largeBlock)(void) = ^{\n"
9629                "  // ...\n"
9630                "};\n",
9631                getLLVMStyleWithColumns(40));
9632   verifyFormat("[[SessionService sharedService]\n"
9633                "    loadWindowWithCompletionBlock: //\n"
9634                "        ^(SessionWindow *window) {\n"
9635                "          if (window) {\n"
9636                "            [self windowDidLoad:window];\n"
9637                "          } else {\n"
9638                "            [self errorLoadingWindow];\n"
9639                "          }\n"
9640                "        }];",
9641                getLLVMStyleWithColumns(60));
9642   verifyFormat("[myObject doSomethingWith:arg1\n"
9643                "    firstBlock:^(Foo *a) {\n"
9644                "      // ...\n"
9645                "      int i;\n"
9646                "    }\n"
9647                "    secondBlock:^(Bar *b) {\n"
9648                "      // ...\n"
9649                "      int i;\n"
9650                "    }\n"
9651                "    thirdBlock:^Foo(Bar *b) {\n"
9652                "      // ...\n"
9653                "      int i;\n"
9654                "    }];");
9655   verifyFormat("[myObject doSomethingWith:arg1\n"
9656                "               firstBlock:-1\n"
9657                "              secondBlock:^(Bar *b) {\n"
9658                "                // ...\n"
9659                "                int i;\n"
9660                "              }];");
9661 
9662   verifyFormat("f(^{\n"
9663                "  @autoreleasepool {\n"
9664                "    if (a) {\n"
9665                "      g();\n"
9666                "    }\n"
9667                "  }\n"
9668                "});");
9669   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9670 
9671   FormatStyle FourIndent = getLLVMStyle();
9672   FourIndent.ObjCBlockIndentWidth = 4;
9673   verifyFormat("[operation setCompletionBlock:^{\n"
9674                "    [self onOperationDone];\n"
9675                "}];",
9676                FourIndent);
9677 }
9678 
9679 TEST_F(FormatTest, SupportsCRLF) {
9680   EXPECT_EQ("int a;\r\n"
9681             "int b;\r\n"
9682             "int c;\r\n",
9683             format("int a;\r\n"
9684                    "  int b;\r\n"
9685                    "    int c;\r\n",
9686                    getLLVMStyle()));
9687   EXPECT_EQ("int a;\r\n"
9688             "int b;\r\n"
9689             "int c;\r\n",
9690             format("int a;\r\n"
9691                    "  int b;\n"
9692                    "    int c;\r\n",
9693                    getLLVMStyle()));
9694   EXPECT_EQ("int a;\n"
9695             "int b;\n"
9696             "int c;\n",
9697             format("int a;\r\n"
9698                    "  int b;\n"
9699                    "    int c;\n",
9700                    getLLVMStyle()));
9701   EXPECT_EQ("\"aaaaaaa \"\r\n"
9702             "\"bbbbbbb\";\r\n",
9703             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
9704   EXPECT_EQ("#define A \\\r\n"
9705             "  b;      \\\r\n"
9706             "  c;      \\\r\n"
9707             "  d;\r\n",
9708             format("#define A \\\r\n"
9709                    "  b; \\\r\n"
9710                    "  c; d; \r\n",
9711                    getGoogleStyle()));
9712 
9713   EXPECT_EQ("/*\r\n"
9714             "multi line block comments\r\n"
9715             "should not introduce\r\n"
9716             "an extra carriage return\r\n"
9717             "*/\r\n",
9718             format("/*\r\n"
9719                    "multi line block comments\r\n"
9720                    "should not introduce\r\n"
9721                    "an extra carriage return\r\n"
9722                    "*/\r\n"));
9723 }
9724 
9725 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
9726   verifyFormat("MY_CLASS(C) {\n"
9727                "  int i;\n"
9728                "  int j;\n"
9729                "};");
9730 }
9731 
9732 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
9733   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
9734   TwoIndent.ContinuationIndentWidth = 2;
9735 
9736   EXPECT_EQ("int i =\n"
9737             "  longFunction(\n"
9738             "    arg);",
9739             format("int i = longFunction(arg);", TwoIndent));
9740 
9741   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
9742   SixIndent.ContinuationIndentWidth = 6;
9743 
9744   EXPECT_EQ("int i =\n"
9745             "      longFunction(\n"
9746             "            arg);",
9747             format("int i = longFunction(arg);", SixIndent));
9748 }
9749 
9750 TEST_F(FormatTest, SpacesInAngles) {
9751   FormatStyle Spaces = getLLVMStyle();
9752   Spaces.SpacesInAngles = true;
9753 
9754   verifyFormat("static_cast< int >(arg);", Spaces);
9755   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
9756   verifyFormat("f< int, float >();", Spaces);
9757   verifyFormat("template <> g() {}", Spaces);
9758   verifyFormat("template < std::vector< int > > f() {}", Spaces);
9759 
9760   Spaces.Standard = FormatStyle::LS_Cpp03;
9761   Spaces.SpacesInAngles = true;
9762   verifyFormat("A< A< int > >();", Spaces);
9763 
9764   Spaces.SpacesInAngles = false;
9765   verifyFormat("A<A<int> >();", Spaces);
9766 
9767   Spaces.Standard = FormatStyle::LS_Cpp11;
9768   Spaces.SpacesInAngles = true;
9769   verifyFormat("A< A< int > >();", Spaces);
9770 
9771   Spaces.SpacesInAngles = false;
9772   verifyFormat("A<A<int>>();", Spaces);
9773 }
9774 
9775 TEST_F(FormatTest, TripleAngleBrackets) {
9776   verifyFormat("f<<<1, 1>>>();");
9777   verifyFormat("f<<<1, 1, 1, s>>>();");
9778   verifyFormat("f<<<a, b, c, d>>>();");
9779   EXPECT_EQ("f<<<1, 1>>>();",
9780             format("f <<< 1, 1 >>> ();"));
9781   verifyFormat("f<param><<<1, 1>>>();");
9782   verifyFormat("f<1><<<1, 1>>>();");
9783   EXPECT_EQ("f<param><<<1, 1>>>();",
9784             format("f< param > <<< 1, 1 >>> ();"));
9785   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9786                "aaaaaaaaaaa<<<\n    1, 1>>>();");
9787 }
9788 
9789 TEST_F(FormatTest, MergeLessLessAtEnd) {
9790   verifyFormat("<<");
9791   EXPECT_EQ("< < <", format("\\\n<<<"));
9792   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9793                "aaallvm::outs() <<");
9794   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
9795                "aaaallvm::outs()\n    <<");
9796 }
9797 
9798 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
9799   std::string code = "#if A\n"
9800                      "#if B\n"
9801                      "a.\n"
9802                      "#endif\n"
9803                      "    a = 1;\n"
9804                      "#else\n"
9805                      "#endif\n"
9806                      "#if C\n"
9807                      "#else\n"
9808                      "#endif\n";
9809   EXPECT_EQ(code, format(code));
9810 }
9811 
9812 TEST_F(FormatTest, HandleConflictMarkers) {
9813   // Git/SVN conflict markers.
9814   EXPECT_EQ("int a;\n"
9815             "void f() {\n"
9816             "  callme(some(parameter1,\n"
9817             "<<<<<<< text by the vcs\n"
9818             "              parameter2),\n"
9819             "||||||| text by the vcs\n"
9820             "              parameter2),\n"
9821             "         parameter3,\n"
9822             "======= text by the vcs\n"
9823             "              parameter2, parameter3),\n"
9824             ">>>>>>> text by the vcs\n"
9825             "         otherparameter);\n",
9826             format("int a;\n"
9827                    "void f() {\n"
9828                    "  callme(some(parameter1,\n"
9829                    "<<<<<<< text by the vcs\n"
9830                    "  parameter2),\n"
9831                    "||||||| text by the vcs\n"
9832                    "  parameter2),\n"
9833                    "  parameter3,\n"
9834                    "======= text by the vcs\n"
9835                    "  parameter2,\n"
9836                    "  parameter3),\n"
9837                    ">>>>>>> text by the vcs\n"
9838                    "  otherparameter);\n"));
9839 
9840   // Perforce markers.
9841   EXPECT_EQ("void f() {\n"
9842             "  function(\n"
9843             ">>>> text by the vcs\n"
9844             "      parameter,\n"
9845             "==== text by the vcs\n"
9846             "      parameter,\n"
9847             "==== text by the vcs\n"
9848             "      parameter,\n"
9849             "<<<< text by the vcs\n"
9850             "      parameter);\n",
9851             format("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 
9862   EXPECT_EQ("<<<<<<<\n"
9863             "|||||||\n"
9864             "=======\n"
9865             ">>>>>>>",
9866             format("<<<<<<<\n"
9867                    "|||||||\n"
9868                    "=======\n"
9869                    ">>>>>>>"));
9870 
9871   EXPECT_EQ("<<<<<<<\n"
9872             "|||||||\n"
9873             "int i;\n"
9874             "=======\n"
9875             ">>>>>>>",
9876             format("<<<<<<<\n"
9877                    "|||||||\n"
9878                    "int i;\n"
9879                    "=======\n"
9880                    ">>>>>>>"));
9881 
9882   // FIXME: Handle parsing of macros around conflict markers correctly:
9883   EXPECT_EQ("#define Macro \\\n"
9884             "<<<<<<<\n"
9885             "Something \\\n"
9886             "|||||||\n"
9887             "Else \\\n"
9888             "=======\n"
9889             "Other \\\n"
9890             ">>>>>>>\n"
9891             "    End int i;\n",
9892             format("#define Macro \\\n"
9893                    "<<<<<<<\n"
9894                    "  Something \\\n"
9895                    "|||||||\n"
9896                    "  Else \\\n"
9897                    "=======\n"
9898                    "  Other \\\n"
9899                    ">>>>>>>\n"
9900                    "  End\n"
9901                    "int i;\n"));
9902 }
9903 
9904 TEST_F(FormatTest, DisableRegions) {
9905   EXPECT_EQ("int i;\n"
9906             "// clang-format off\n"
9907             "  int j;\n"
9908             "// clang-format on\n"
9909             "int k;",
9910             format(" int  i;\n"
9911                    "   // clang-format off\n"
9912                    "  int j;\n"
9913                    " // clang-format on\n"
9914                    "   int   k;"));
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 }
9926 
9927 TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
9928   format("? ) =");
9929 }
9930 
9931 } // end namespace tooling
9932 } // end namespace clang
9933