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