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