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