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 && bbbbb // break\n"
2944                "                    > ccccc) {\n"
2945                "}",
2946                Style);
2947 
2948   // Forced by comments.
2949   verifyFormat(
2950       "unsigned ContentSize =\n"
2951       "    sizeof(int16_t)   // DWARF ARange version number\n"
2952       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
2953       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
2954       "    + sizeof(int8_t); // Segment Size (in bytes)");
2955 
2956   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
2957                "       == boost::fusion::at_c<1>(iiii).second;",
2958                Style);
2959 }
2960 
2961 TEST_F(FormatTest, ConstructorInitializers) {
2962   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
2963   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
2964                getLLVMStyleWithColumns(45));
2965   verifyFormat("Constructor()\n"
2966                "    : Inttializer(FitsOnTheLine) {}",
2967                getLLVMStyleWithColumns(44));
2968   verifyFormat("Constructor()\n"
2969                "    : Inttializer(FitsOnTheLine) {}",
2970                getLLVMStyleWithColumns(43));
2971 
2972   verifyFormat(
2973       "SomeClass::Constructor()\n"
2974       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2975 
2976   verifyFormat(
2977       "SomeClass::Constructor()\n"
2978       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
2979       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
2980   verifyFormat(
2981       "SomeClass::Constructor()\n"
2982       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2983       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
2984 
2985   verifyFormat("Constructor()\n"
2986                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2987                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
2988                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
2989                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
2990 
2991   verifyFormat("Constructor()\n"
2992                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
2993                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
2994 
2995   verifyFormat("Constructor(int Parameter = 0)\n"
2996                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
2997                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
2998   verifyFormat("Constructor()\n"
2999                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3000                "}",
3001                getLLVMStyleWithColumns(60));
3002   verifyFormat("Constructor()\n"
3003                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3004                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3005 
3006   // Here a line could be saved by splitting the second initializer onto two
3007   // lines, but that is not desirable.
3008   verifyFormat("Constructor()\n"
3009                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3010                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3011                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3012 
3013   FormatStyle OnePerLine = getLLVMStyle();
3014   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3015   verifyFormat("SomeClass::Constructor()\n"
3016                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3017                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3018                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3019                OnePerLine);
3020   verifyFormat("SomeClass::Constructor()\n"
3021                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3022                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3023                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3024                OnePerLine);
3025   verifyFormat("MyClass::MyClass(int var)\n"
3026                "    : some_var_(var),            // 4 space indent\n"
3027                "      some_other_var_(var + 1) { // lined up\n"
3028                "}",
3029                OnePerLine);
3030   verifyFormat("Constructor()\n"
3031                "    : aaaaa(aaaaaa),\n"
3032                "      aaaaa(aaaaaa),\n"
3033                "      aaaaa(aaaaaa),\n"
3034                "      aaaaa(aaaaaa),\n"
3035                "      aaaaa(aaaaaa) {}",
3036                OnePerLine);
3037   verifyFormat("Constructor()\n"
3038                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3039                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3040                OnePerLine);
3041 
3042   EXPECT_EQ("Constructor()\n"
3043             "    : // Comment forcing unwanted break.\n"
3044             "      aaaa(aaaa) {}",
3045             format("Constructor() :\n"
3046                    "    // Comment forcing unwanted break.\n"
3047                    "    aaaa(aaaa) {}"));
3048 }
3049 
3050 TEST_F(FormatTest, MemoizationTests) {
3051   // This breaks if the memoization lookup does not take \c Indent and
3052   // \c LastSpace into account.
3053   verifyFormat(
3054       "extern CFRunLoopTimerRef\n"
3055       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3056       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3057       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3058       "                     CFRunLoopTimerContext *context) {}");
3059 
3060   // Deep nesting somewhat works around our memoization.
3061   verifyFormat(
3062       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3063       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3064       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3065       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3066       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3067       getLLVMStyleWithColumns(65));
3068   verifyFormat(
3069       "aaaaa(\n"
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))))))))))));",
3094       getLLVMStyleWithColumns(65));
3095   verifyFormat(
3096       "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"
3097       "                                  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)",
3114       getLLVMStyleWithColumns(65));
3115 
3116   // This test takes VERY long when memoization is broken.
3117   FormatStyle OnePerLine = getLLVMStyle();
3118   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3119   OnePerLine.BinPackParameters = false;
3120   std::string input = "Constructor()\n"
3121                       "    : aaaa(a,\n";
3122   for (unsigned i = 0, e = 80; i != e; ++i) {
3123     input += "           a,\n";
3124   }
3125   input += "           a) {}";
3126   verifyFormat(input, OnePerLine);
3127 }
3128 
3129 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3130   verifyFormat(
3131       "void f() {\n"
3132       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3133       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3134       "    f();\n"
3135       "}");
3136   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3137                "    Intervals[i - 1].getRange().getLast()) {\n}");
3138 }
3139 
3140 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3141   // Principially, we break function declarations in a certain order:
3142   // 1) break amongst arguments.
3143   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3144                "                              Cccccccccccccc cccccccccccccc);");
3145   verifyFormat(
3146       "template <class TemplateIt>\n"
3147       "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3148       "                            TemplateIt *stop) {}");
3149 
3150   // 2) break after return type.
3151   verifyFormat(
3152       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3153       "    bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3154       getGoogleStyle());
3155 
3156   // 3) break after (.
3157   verifyFormat(
3158       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3159       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3160       getGoogleStyle());
3161 
3162   // 4) break before after nested name specifiers.
3163   verifyFormat(
3164       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3165       "    SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3166       "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3167       getGoogleStyle());
3168 
3169   // However, there are exceptions, if a sufficient amount of lines can be
3170   // saved.
3171   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3172   // more adjusting.
3173   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3174                "                                  Cccccccccccccc cccccccccc,\n"
3175                "                                  Cccccccccccccc cccccccccc,\n"
3176                "                                  Cccccccccccccc cccccccccc,\n"
3177                "                                  Cccccccccccccc cccccccccc);");
3178   verifyFormat(
3179       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3180       "    bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3181       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3182       "                Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3183       getGoogleStyle());
3184   verifyFormat(
3185       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3186       "                                          Cccccccccccccc cccccccccc,\n"
3187       "                                          Cccccccccccccc cccccccccc,\n"
3188       "                                          Cccccccccccccc cccccccccc,\n"
3189       "                                          Cccccccccccccc cccccccccc,\n"
3190       "                                          Cccccccccccccc cccccccccc,\n"
3191       "                                          Cccccccccccccc cccccccccc);");
3192   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3193                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3194                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3195                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3196                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3197 
3198   // Break after multi-line parameters.
3199   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3200                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3201                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3202                "    bbbb bbbb);");
3203 
3204   // Treat overloaded operators like other functions.
3205   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3206                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3207   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3208                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3209   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3210                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3211   verifyGoogleFormat(
3212       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3213       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3214   verifyGoogleFormat(
3215       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3216       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3217   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3218                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3219   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3220                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3221 }
3222 
3223 TEST_F(FormatTest, TrailingReturnType) {
3224   verifyFormat("auto foo() -> int;\n");
3225   verifyFormat("struct S {\n"
3226                "  auto bar() const -> int;\n"
3227                "};");
3228   verifyFormat("template <size_t Order, typename T>\n"
3229                "auto load_img(const std::string &filename)\n"
3230                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3231 
3232   // Not trailing return types.
3233   verifyFormat("void f() { auto a = b->c(); }");
3234 }
3235 
3236 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3237   // Avoid breaking before trailing 'const' or other trailing annotations, if
3238   // they are not function-like.
3239   FormatStyle Style = getGoogleStyle();
3240   Style.ColumnLimit = 47;
3241   verifyFormat("void\n"
3242                "someLongFunction(int someLongParameter) const {\n}",
3243                getLLVMStyleWithColumns(47));
3244   verifyFormat("LoooooongReturnType\n"
3245                "someLoooooooongFunction() const {}",
3246                getLLVMStyleWithColumns(47));
3247   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3248                "    const {}",
3249                Style);
3250   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3251                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3252   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3253                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3254   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3255                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3256   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3257                "                   aaaaaaaaaaa aaaaa) const override;");
3258   verifyGoogleFormat(
3259       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3260       "    const override;");
3261 
3262   // Even if the first parameter has to be wrapped.
3263   verifyFormat("void someLongFunction(\n"
3264                "    int someLongParameter) const {}",
3265                getLLVMStyleWithColumns(46));
3266   verifyFormat("void someLongFunction(\n"
3267                "    int someLongParameter) const {}",
3268                Style);
3269   verifyFormat("void someLongFunction(\n"
3270                "    int someLongParameter) override {}",
3271                Style);
3272   verifyFormat("void someLongFunction(\n"
3273                "    int someLongParameter) final {}",
3274                Style);
3275   verifyFormat("void someLongFunction(\n"
3276                "    int parameter) const override {}",
3277                Style);
3278 
3279   // Unless these are unknown annotations.
3280   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3281                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3282                "    LONG_AND_UGLY_ANNOTATION;");
3283 
3284   // Breaking before function-like trailing annotations is fine to keep them
3285   // close to their arguments.
3286   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3287                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3288   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3289                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3290   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3291                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3292 
3293   verifyFormat(
3294       "void aaaaaaaaaaaaaaaaaa()\n"
3295       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3296       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3297   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3298                "    __attribute__((unused));");
3299   verifyFormat(
3300       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3301       "    GUARDED_BY(aaaaaaaaaaaa);",
3302       getGoogleStyle());
3303   verifyFormat(
3304       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3305       "    GUARDED_BY(aaaaaaaaaaaa);",
3306       getGoogleStyle());
3307 }
3308 
3309 TEST_F(FormatTest, BreaksDesireably) {
3310   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3311                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3312                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3313   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3314                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3315                "}");
3316 
3317   verifyFormat(
3318       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3319       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3320 
3321   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3322                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3323                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3324 
3325   verifyFormat(
3326       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3327       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3328       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3329       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3330 
3331   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3332                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3333 
3334   verifyFormat(
3335       "void f() {\n"
3336       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3337       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3338       "}");
3339   verifyFormat(
3340       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3341       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3342   verifyFormat(
3343       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3344       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3345   verifyFormat(
3346       "aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3347       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3348       "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3349 
3350   // Indent consistently independent of call expression.
3351   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3352                "    dddddddddddddddddddddddddddddd));\n"
3353                "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3354                "    dddddddddddddddddddddddddddddd));");
3355 
3356   // This test case breaks on an incorrect memoization, i.e. an optimization not
3357   // taking into account the StopAt value.
3358   verifyFormat(
3359       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3360       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3361       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3362       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3363 
3364   verifyFormat("{\n  {\n    {\n"
3365                "      Annotation.SpaceRequiredBefore =\n"
3366                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3367                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3368                "    }\n  }\n}");
3369 
3370   // Break on an outer level if there was a break on an inner level.
3371   EXPECT_EQ("f(g(h(a, // comment\n"
3372             "      b, c),\n"
3373             "    d, e),\n"
3374             "  x, y);",
3375             format("f(g(h(a, // comment\n"
3376                    "    b, c), d, e), x, y);"));
3377 
3378   // Prefer breaking similar line breaks.
3379   verifyFormat(
3380       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
3381       "                             NSTrackingMouseEnteredAndExited |\n"
3382       "                             NSTrackingActiveAlways;");
3383 }
3384 
3385 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
3386   FormatStyle NoBinPacking = getGoogleStyle();
3387   NoBinPacking.BinPackParameters = false;
3388   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
3389                "  aaaaaaaaaaaaaaaaaaaa,\n"
3390                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
3391                NoBinPacking);
3392   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
3393                "        aaaaaaaaaaaaa,\n"
3394                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
3395                NoBinPacking);
3396   verifyFormat(
3397       "aaaaaaaa(aaaaaaaaaaaaa,\n"
3398       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3399       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3400       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3401       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
3402       NoBinPacking);
3403   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3404                "    .aaaaaaaaaaaaaaaaaa();",
3405                NoBinPacking);
3406   verifyFormat("void f() {\n"
3407                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3408                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
3409                "}",
3410                NoBinPacking);
3411 
3412   verifyFormat(
3413       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3414       "             aaaaaaaaaaaa,\n"
3415       "             aaaaaaaaaaaa);",
3416       NoBinPacking);
3417   verifyFormat(
3418       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
3419       "                               ddddddddddddddddddddddddddddd),\n"
3420       "             test);",
3421       NoBinPacking);
3422 
3423   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
3424                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
3425                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
3426                NoBinPacking);
3427   verifyFormat("a(\"a\"\n"
3428                "  \"a\",\n"
3429                "  a);");
3430 
3431   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
3432   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
3433                "                aaaaaaaaa,\n"
3434                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3435                NoBinPacking);
3436   verifyFormat(
3437       "void f() {\n"
3438       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
3439       "      .aaaaaaa();\n"
3440       "}",
3441       NoBinPacking);
3442   verifyFormat(
3443       "template <class SomeType, class SomeOtherType>\n"
3444       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
3445       NoBinPacking);
3446 }
3447 
3448 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
3449   FormatStyle Style = getLLVMStyleWithColumns(15);
3450   Style.ExperimentalAutoDetectBinPacking = true;
3451   EXPECT_EQ("aaa(aaaa,\n"
3452             "    aaaa,\n"
3453             "    aaaa);\n"
3454             "aaa(aaaa,\n"
3455             "    aaaa,\n"
3456             "    aaaa);",
3457             format("aaa(aaaa,\n" // one-per-line
3458                    "  aaaa,\n"
3459                    "    aaaa  );\n"
3460                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3461                    Style));
3462   EXPECT_EQ("aaa(aaaa, aaaa,\n"
3463             "    aaaa);\n"
3464             "aaa(aaaa, aaaa,\n"
3465             "    aaaa);",
3466             format("aaa(aaaa,  aaaa,\n" // bin-packed
3467                    "    aaaa  );\n"
3468                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
3469                    Style));
3470 }
3471 
3472 TEST_F(FormatTest, FormatsBuilderPattern) {
3473   verifyFormat(
3474       "return llvm::StringSwitch<Reference::Kind>(name)\n"
3475       "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
3476       "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
3477       "    .StartsWith(\".init\", ORDER_INIT)\n"
3478       "    .StartsWith(\".fini\", ORDER_FINI)\n"
3479       "    .StartsWith(\".hash\", ORDER_HASH)\n"
3480       "    .Default(ORDER_TEXT);\n");
3481 
3482   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
3483                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
3484   verifyFormat(
3485       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
3486       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3487       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
3488   verifyFormat(
3489       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
3490       "    aaaaaaaaaaaaaa);");
3491   verifyFormat(
3492       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
3493       "    aaaaaa->aaaaaaaaaaaa()\n"
3494       "        ->aaaaaaaaaaaaaaaa(\n"
3495       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3496       "        ->aaaaaaaaaaaaaaaaa();");
3497   verifyGoogleFormat(
3498       "void f() {\n"
3499       "  someo->Add((new util::filetools::Handler(dir))\n"
3500       "                 ->OnEvent1(NewPermanentCallback(\n"
3501       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
3502       "                 ->OnEvent2(NewPermanentCallback(\n"
3503       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
3504       "                 ->OnEvent3(NewPermanentCallback(\n"
3505       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
3506       "                 ->OnEvent5(NewPermanentCallback(\n"
3507       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
3508       "                 ->OnEvent6(NewPermanentCallback(\n"
3509       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
3510       "}");
3511 
3512   verifyFormat(
3513       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
3514   verifyFormat("aaaaaaaaaaaaaaa()\n"
3515                "    .aaaaaaaaaaaaaaa()\n"
3516                "    .aaaaaaaaaaaaaaa()\n"
3517                "    .aaaaaaaaaaaaaaa()\n"
3518                "    .aaaaaaaaaaaaaaa();");
3519   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3520                "    .aaaaaaaaaaaaaaa()\n"
3521                "    .aaaaaaaaaaaaaaa()\n"
3522                "    .aaaaaaaaaaaaaaa();");
3523   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3524                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
3525                "    .aaaaaaaaaaaaaaa();");
3526   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
3527                "    ->aaaaaaaaaaaaaae(0)\n"
3528                "    ->aaaaaaaaaaaaaaa();");
3529 
3530   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3531                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3532                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
3533   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
3534                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3535                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
3536 
3537   // Prefer not to break after empty parentheses.
3538   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
3539                "    First->LastNewlineOffset);");
3540 }
3541 
3542 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
3543   verifyFormat(
3544       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3545       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
3546   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3547                "    ccccccccccccccccccccccccc) {\n}");
3548   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
3549                "    ccccccccccccccccccccccccc) {\n}");
3550   verifyFormat(
3551       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
3552       "    ccccccccccccccccccccccccc) {\n}");
3553   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
3554                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
3555                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
3556                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
3557   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
3558                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
3559                "    aaaaaaaaaaaaaaa != aa) {\n}");
3560 }
3561 
3562 TEST_F(FormatTest, BreaksAfterAssignments) {
3563   verifyFormat(
3564       "unsigned Cost =\n"
3565       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
3566       "                        SI->getPointerAddressSpaceee());\n");
3567   verifyFormat(
3568       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
3569       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
3570 
3571   verifyFormat(
3572       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
3573       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
3574   verifyFormat("unsigned OriginalStartColumn =\n"
3575                "    SourceMgr.getSpellingColumnNumber(\n"
3576                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
3577                "    1;");
3578 }
3579 
3580 TEST_F(FormatTest, AlignsAfterAssignments) {
3581   verifyFormat(
3582       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3583       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
3584   verifyFormat(
3585       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3586       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
3587   verifyFormat(
3588       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3589       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
3590   verifyFormat(
3591       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3592       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
3593   verifyFormat(
3594       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3595       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
3596       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
3597 }
3598 
3599 TEST_F(FormatTest, AlignsAfterReturn) {
3600   verifyFormat(
3601       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3602       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
3603   verifyFormat(
3604       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3605       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
3606   verifyFormat(
3607       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3608       "       aaaaaaaaaaaaaaaaaaaaaa();");
3609   verifyFormat(
3610       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
3611       "        aaaaaaaaaaaaaaaaaaaaaa());");
3612   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3613                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3614   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3615                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
3616                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3617   verifyFormat("return\n"
3618                "    // true if code is one of a or b.\n"
3619                "    code == a || code == b;");
3620 }
3621 
3622 TEST_F(FormatTest, BreaksConditionalExpressions) {
3623   verifyFormat(
3624       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3625       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3626       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3627   verifyFormat(
3628       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3629       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3630   verifyFormat(
3631       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
3632       "                                                    : aaaaaaaaaaaaa);");
3633   verifyFormat(
3634       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3635       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3636       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3637       "                   aaaaaaaaaaaaa);");
3638   verifyFormat(
3639       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3640       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3641       "                   aaaaaaaaaaaaa);");
3642   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3643                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3644                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3645                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3646                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3647   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3648                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3649                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3650                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3651                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3652                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3653                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3654   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3655                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3656                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3657                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3658                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3659   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3660                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3661                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3662   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3663                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3664                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3665                "        : aaaaaaaaaaaaaaaa;");
3666   verifyFormat(
3667       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3668       "    ? aaaaaaaaaaaaaaa\n"
3669       "    : aaaaaaaaaaaaaaa;");
3670   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3671                "          aaaaaaaaa\n"
3672                "      ? b\n"
3673                "      : c);");
3674   verifyFormat(
3675       "unsigned Indent =\n"
3676       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
3677       "                              ? IndentForLevel[TheLine.Level]\n"
3678       "                              : TheLine * 2,\n"
3679       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3680       getLLVMStyleWithColumns(70));
3681   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3682                "                  ? aaaaaaaaaaaaaaa\n"
3683                "                  : bbbbbbbbbbbbbbb //\n"
3684                "                        ? ccccccccccccccc\n"
3685                "                        : ddddddddddddddd;");
3686   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
3687                "                  ? aaaaaaaaaaaaaaa\n"
3688                "                  : (bbbbbbbbbbbbbbb //\n"
3689                "                         ? ccccccccccccccc\n"
3690                "                         : ddddddddddddddd);");
3691 
3692   FormatStyle NoBinPacking = getLLVMStyle();
3693   NoBinPacking.BinPackParameters = false;
3694   verifyFormat(
3695       "void f() {\n"
3696       "  g(aaa,\n"
3697       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3698       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3699       "        ? aaaaaaaaaaaaaaa\n"
3700       "        : aaaaaaaaaaaaaaa);\n"
3701       "}",
3702       NoBinPacking);
3703   verifyFormat(
3704       "void f() {\n"
3705       "  g(aaa,\n"
3706       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
3707       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3708       "        ?: aaaaaaaaaaaaaaa);\n"
3709       "}",
3710       NoBinPacking);
3711 }
3712 
3713 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
3714   FormatStyle Style = getLLVMStyle();
3715   Style.BreakBeforeTernaryOperators = false;
3716   Style.ColumnLimit = 70;
3717   verifyFormat(
3718       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3719       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3720       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3721       Style);
3722   verifyFormat(
3723       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3724       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3725       Style);
3726   verifyFormat(
3727       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
3728       "                                                      aaaaaaaaaaaaa);",
3729       Style);
3730   verifyFormat(
3731       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3732       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3733       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3734       "                   aaaaaaaaaaaaa);",
3735       Style);
3736   verifyFormat(
3737       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3738       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3739       "                   aaaaaaaaaaaaa);",
3740       Style);
3741   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3742                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3743                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3744                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3745                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3746                Style);
3747   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3748                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3749                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3750                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
3751                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3752                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3753                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3754                Style);
3755   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3756                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
3757                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3758                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3759                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
3760                Style);
3761   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3762                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3763                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3764                Style);
3765   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
3766                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3767                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
3768                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
3769                Style);
3770   verifyFormat(
3771       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
3772       "    aaaaaaaaaaaaaaa :\n"
3773       "    aaaaaaaaaaaaaaa;",
3774       Style);
3775   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
3776                "          aaaaaaaaa ?\n"
3777                "      b :\n"
3778                "      c);",
3779                Style);
3780   verifyFormat(
3781       "unsigned Indent =\n"
3782       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
3783       "                              IndentForLevel[TheLine.Level] :\n"
3784       "                              TheLine * 2,\n"
3785       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
3786       Style);
3787   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3788                "                  aaaaaaaaaaaaaaa :\n"
3789                "                  bbbbbbbbbbbbbbb ? //\n"
3790                "                      ccccccccccccccc :\n"
3791                "                      ddddddddddddddd;",
3792                Style);
3793   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
3794                "                  aaaaaaaaaaaaaaa :\n"
3795                "                  (bbbbbbbbbbbbbbb ? //\n"
3796                "                       ccccccccccccccc :\n"
3797                "                       ddddddddddddddd);",
3798                Style);
3799 }
3800 
3801 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
3802   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
3803                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
3804   verifyFormat("bool a = true, b = false;");
3805 
3806   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3807                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
3808                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
3809                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
3810   verifyFormat(
3811       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3812       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
3813       "     d = e && f;");
3814   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
3815                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
3816   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3817                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
3818   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
3819                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
3820   // FIXME: If multiple variables are defined, the "*" needs to move to the new
3821   // line. Also fix indent for breaking after the type, this looks bad.
3822   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3823                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
3824                "    *b = bbbbbbbbbbbbbbbbbbb;",
3825                getGoogleStyle());
3826 
3827   // Not ideal, but pointer-with-type does not allow much here.
3828   verifyGoogleFormat(
3829       "aaaaaaaaa* a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
3830       "           *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;");
3831 }
3832 
3833 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
3834   verifyFormat("arr[foo ? bar : baz];");
3835   verifyFormat("f()[foo ? bar : baz];");
3836   verifyFormat("(a + b)[foo ? bar : baz];");
3837   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
3838 }
3839 
3840 TEST_F(FormatTest, AlignsStringLiterals) {
3841   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
3842                "                                      \"short literal\");");
3843   verifyFormat(
3844       "looooooooooooooooooooooooongFunction(\n"
3845       "    \"short literal\"\n"
3846       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
3847   verifyFormat("someFunction(\"Always break between multi-line\"\n"
3848                "             \" string literals\",\n"
3849                "             and, other, parameters);");
3850   EXPECT_EQ("fun + \"1243\" /* comment */\n"
3851             "      \"5678\";",
3852             format("fun + \"1243\" /* comment */\n"
3853                    "      \"5678\";",
3854                    getLLVMStyleWithColumns(28)));
3855   EXPECT_EQ(
3856       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
3857       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
3858       "         \"aaaaaaaaaaaaaaaa\";",
3859       format("aaaaaa ="
3860              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
3861              "aaaaaaaaaaaaaaaaaaaaa\" "
3862              "\"aaaaaaaaaaaaaaaa\";"));
3863   verifyFormat("a = a + \"a\"\n"
3864                "        \"a\"\n"
3865                "        \"a\";");
3866   verifyFormat("f(\"a\", \"b\"\n"
3867                "       \"c\");");
3868 
3869   verifyFormat(
3870       "#define LL_FORMAT \"ll\"\n"
3871       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
3872       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
3873 
3874   verifyFormat("#define A(X)          \\\n"
3875                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
3876                "  \"ccccc\"",
3877                getLLVMStyleWithColumns(23));
3878   verifyFormat("#define A \"def\"\n"
3879                "f(\"abc\" A \"ghi\"\n"
3880                "  \"jkl\");");
3881 
3882   verifyFormat("f(L\"a\"\n"
3883                "  L\"b\")");
3884   verifyFormat("#define A(X)            \\\n"
3885                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
3886                "  L\"ccccc\"",
3887                getLLVMStyleWithColumns(25));
3888 }
3889 
3890 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
3891   FormatStyle NoBreak = getLLVMStyle();
3892   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
3893   FormatStyle Break = getLLVMStyle();
3894   Break.AlwaysBreakBeforeMultilineStrings = true;
3895   verifyFormat("aaaa = \"bbbb\"\n"
3896                "       \"cccc\";",
3897                NoBreak);
3898   verifyFormat("aaaa =\n"
3899                "    \"bbbb\"\n"
3900                "    \"cccc\";",
3901                Break);
3902   verifyFormat("aaaa(\"bbbb\"\n"
3903                "     \"cccc\");",
3904                NoBreak);
3905   verifyFormat("aaaa(\n"
3906                "    \"bbbb\"\n"
3907                "    \"cccc\");",
3908                Break);
3909   verifyFormat("aaaa(qqq, \"bbbb\"\n"
3910                "          \"cccc\");",
3911                NoBreak);
3912   verifyFormat("aaaa(qqq,\n"
3913                "     \"bbbb\"\n"
3914                "     \"cccc\");",
3915                Break);
3916   verifyFormat("aaaa(qqq,\n"
3917                "     L\"bbbb\"\n"
3918                "     L\"cccc\");",
3919                Break);
3920 
3921   // Don't break if there is no column gain.
3922   verifyFormat("f(\"aaaa\"\n"
3923                "  \"bbbb\");",
3924                Break);
3925 
3926   // Treat literals with escaped newlines like multi-line string literals.
3927   EXPECT_EQ("x = \"a\\\n"
3928             "b\\\n"
3929             "c\";",
3930             format("x = \"a\\\n"
3931                    "b\\\n"
3932                    "c\";",
3933                    NoBreak));
3934   EXPECT_EQ("x =\n"
3935             "    \"a\\\n"
3936             "b\\\n"
3937             "c\";",
3938             format("x = \"a\\\n"
3939                    "b\\\n"
3940                    "c\";",
3941                    Break));
3942 
3943   // Exempt ObjC strings for now.
3944   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
3945             "                           \"bbbb\";",
3946             format("NSString *const kString = @\"aaaa\"\n"
3947                    "\"bbbb\";",
3948                    Break));
3949 }
3950 
3951 TEST_F(FormatTest, AlignsPipes) {
3952   verifyFormat(
3953       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3954       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3955       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3956   verifyFormat(
3957       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
3958       "                     << aaaaaaaaaaaaaaaaaaaa;");
3959   verifyFormat(
3960       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3961       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3962   verifyFormat(
3963       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
3964       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
3965       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
3966   verifyFormat(
3967       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3968       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3969       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3970 
3971   verifyFormat("return out << \"somepacket = {\\n\"\n"
3972                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
3973                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
3974                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
3975                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
3976                "           << \"}\";");
3977 
3978   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
3979                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
3980                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
3981   verifyFormat(
3982       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
3983       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
3984       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
3985       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
3986       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
3987   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
3988                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
3989   verifyFormat(
3990       "void f() {\n"
3991       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
3992       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3993       "}");
3994 
3995   // Breaking before the first "<<" is generally not desirable.
3996   verifyFormat(
3997       "llvm::errs()\n"
3998       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3999       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4000       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4001       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4002       getLLVMStyleWithColumns(70));
4003   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4004                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4005                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4006                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4007                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4008                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4009                getLLVMStyleWithColumns(70));
4010 
4011   // But sometimes, breaking before the first "<<" is desirable.
4012   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4013                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4014   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4015                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4016                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4017   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4018                "    << BEF << IsTemplate << Description << E->getType();");
4019 
4020   verifyFormat(
4021       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4022       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4023 
4024   // Incomplete string literal.
4025   EXPECT_EQ("llvm::errs() << \"\n"
4026             "             << a;",
4027             format("llvm::errs() << \"\n<<a;"));
4028 
4029   verifyFormat("void f() {\n"
4030                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4031                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4032                "}");
4033 }
4034 
4035 TEST_F(FormatTest, UnderstandsEquals) {
4036   verifyFormat(
4037       "aaaaaaaaaaaaaaaaa =\n"
4038       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4039   verifyFormat(
4040       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4041       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4042   verifyFormat(
4043       "if (a) {\n"
4044       "  f();\n"
4045       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4046       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4047       "}");
4048 
4049   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4050                "        100000000 + 10000000) {\n}");
4051 }
4052 
4053 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4054   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4055                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4056 
4057   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4058                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4059 
4060   verifyFormat(
4061       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4062       "                                                          Parameter2);");
4063 
4064   verifyFormat(
4065       "ShortObject->shortFunction(\n"
4066       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4067       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4068 
4069   verifyFormat("loooooooooooooongFunction(\n"
4070                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4071 
4072   verifyFormat(
4073       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4074       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4075 
4076   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4077                "    .WillRepeatedly(Return(SomeValue));");
4078   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4079                "    ccccccccccccccccccccccc);");
4080   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4081                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).aaaaa(aaaaa),\n"
4082                "      aaaaaaaaaaaaaaaaaaaaa);");
4083   verifyFormat("void f() {\n"
4084                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4085                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4086                "}");
4087   verifyFormat(
4088       "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4089       "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4090       "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4091       "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4092       "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4093   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4094                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4095                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4096                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4097                "}");
4098 
4099   // Here, it is not necessary to wrap at "." or "->".
4100   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4101                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4102   verifyFormat(
4103       "aaaaaaaaaaa->aaaaaaaaa(\n"
4104       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4105       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4106 
4107   verifyFormat(
4108       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4109       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4110   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4111                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4112   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4113                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4114 
4115   // FIXME: Should we break before .a()?
4116   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4117                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();");
4118 
4119   FormatStyle NoBinPacking = getLLVMStyle();
4120   NoBinPacking.BinPackParameters = false;
4121   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4122                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4123                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4124                "                         aaaaaaaaaaaaaaaaaaa,\n"
4125                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4126                NoBinPacking);
4127 
4128   // If there is a subsequent call, change to hanging indentation.
4129   verifyFormat(
4130       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4131       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4132       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4133   verifyFormat(
4134       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4135       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4136   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4137                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4138                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4139   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4140                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4141                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4142 }
4143 
4144 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4145   verifyFormat("template <typename T>\n"
4146                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4147   verifyFormat("template <typename T>\n"
4148                "// T should be one of {A, B}.\n"
4149                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4150   verifyFormat(
4151       "template <typename T>\n"
4152       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4153   verifyFormat("template <typename T>\n"
4154                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4155                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4156   verifyFormat(
4157       "template <typename T>\n"
4158       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4159       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4160   verifyFormat(
4161       "template <typename T>\n"
4162       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
4163       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
4164       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4165   verifyFormat("template <typename T>\n"
4166                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4167                "    int aaaaaaaaaaaaaaaaaaaaaa);");
4168   verifyFormat(
4169       "template <typename T1, typename T2 = char, typename T3 = char,\n"
4170       "          typename T4 = char>\n"
4171       "void f();");
4172   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
4173                "          template <typename> class cccccccccccccccccccccc,\n"
4174                "          typename ddddddddddddd>\n"
4175                "class C {};");
4176   verifyFormat(
4177       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
4178       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4179 
4180   verifyFormat("void f() {\n"
4181                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
4182                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
4183                "}");
4184 
4185   verifyFormat("template <typename T> class C {};");
4186   verifyFormat("template <typename T> void f();");
4187   verifyFormat("template <typename T> void f() {}");
4188   verifyFormat(
4189       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4190       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4191       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
4192       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
4193       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4194       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
4195       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
4196       getLLVMStyleWithColumns(72));
4197 
4198   FormatStyle AlwaysBreak = getLLVMStyle();
4199   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
4200   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
4201   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
4202   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
4203   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4204                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
4205                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
4206   verifyFormat("template <template <typename> class Fooooooo,\n"
4207                "          template <typename> class Baaaaaaar>\n"
4208                "struct C {};",
4209                AlwaysBreak);
4210   verifyFormat("template <typename T> // T can be A, B or C.\n"
4211                "struct C {};",
4212                AlwaysBreak);
4213 }
4214 
4215 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
4216   verifyFormat(
4217       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4218       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4219   verifyFormat(
4220       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4221       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4222       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4223 
4224   // FIXME: Should we have the extra indent after the second break?
4225   verifyFormat(
4226       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4227       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4228       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4229 
4230   verifyFormat(
4231       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
4232       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
4233 
4234   // Breaking at nested name specifiers is generally not desirable.
4235   verifyFormat(
4236       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4237       "    aaaaaaaaaaaaaaaaaaaaaaa);");
4238 
4239   verifyFormat(
4240       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4241       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4242       "                   aaaaaaaaaaaaaaaaaaaaa);",
4243       getLLVMStyleWithColumns(74));
4244 
4245   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
4246                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4247                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4248 }
4249 
4250 TEST_F(FormatTest, UnderstandsTemplateParameters) {
4251   verifyFormat("A<int> a;");
4252   verifyFormat("A<A<A<int>>> a;");
4253   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
4254   verifyFormat("bool x = a < 1 || 2 > a;");
4255   verifyFormat("bool x = 5 < f<int>();");
4256   verifyFormat("bool x = f<int>() > 5;");
4257   verifyFormat("bool x = 5 < a<int>::x;");
4258   verifyFormat("bool x = a < 4 ? a > 2 : false;");
4259   verifyFormat("bool x = f() ? a < 2 : a > 2;");
4260 
4261   verifyGoogleFormat("A<A<int>> a;");
4262   verifyGoogleFormat("A<A<A<int>>> a;");
4263   verifyGoogleFormat("A<A<A<A<int>>>> a;");
4264   verifyGoogleFormat("A<A<int> > a;");
4265   verifyGoogleFormat("A<A<A<int> > > a;");
4266   verifyGoogleFormat("A<A<A<A<int> > > > a;");
4267   verifyGoogleFormat("A<::A<int>> a;");
4268   verifyGoogleFormat("A<::A> a;");
4269   verifyGoogleFormat("A< ::A> a;");
4270   verifyGoogleFormat("A< ::A<int> > a;");
4271   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
4272   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
4273   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
4274   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
4275 
4276   verifyFormat("test >> a >> b;");
4277   verifyFormat("test << a >> b;");
4278 
4279   verifyFormat("f<int>();");
4280   verifyFormat("template <typename T> void f() {}");
4281 
4282   // Not template parameters.
4283   verifyFormat("return a < b && c > d;");
4284   verifyFormat("void f() {\n"
4285                "  while (a < b && c > d) {\n"
4286                "  }\n"
4287                "}");
4288   verifyFormat("template <typename... Types>\n"
4289                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
4290 
4291   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4292                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
4293                getLLVMStyleWithColumns(60));
4294 }
4295 
4296 TEST_F(FormatTest, UnderstandsBinaryOperators) {
4297   verifyFormat("COMPARE(a, ==, b);");
4298 }
4299 
4300 TEST_F(FormatTest, UnderstandsPointersToMembers) {
4301   verifyFormat("int A::*x;");
4302   verifyFormat("int (S::*func)(void *);");
4303   verifyFormat("void f() { int (S::*func)(void *); }");
4304   verifyFormat("typedef bool *(Class::*Member)() const;");
4305   verifyFormat("void f() {\n"
4306                "  (a->*f)();\n"
4307                "  a->*x;\n"
4308                "  (a.*f)();\n"
4309                "  ((*a).*f)();\n"
4310                "  a.*x;\n"
4311                "}");
4312   verifyFormat("void f() {\n"
4313                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
4314                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
4315                "}");
4316   FormatStyle Style = getLLVMStyle();
4317   Style.PointerBindsToType = true;
4318   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
4319 }
4320 
4321 TEST_F(FormatTest, UnderstandsUnaryOperators) {
4322   verifyFormat("int a = -2;");
4323   verifyFormat("f(-1, -2, -3);");
4324   verifyFormat("a[-1] = 5;");
4325   verifyFormat("int a = 5 + -2;");
4326   verifyFormat("if (i == -1) {\n}");
4327   verifyFormat("if (i != -1) {\n}");
4328   verifyFormat("if (i > -1) {\n}");
4329   verifyFormat("if (i < -1) {\n}");
4330   verifyFormat("++(a->f());");
4331   verifyFormat("--(a->f());");
4332   verifyFormat("(a->f())++;");
4333   verifyFormat("a[42]++;");
4334   verifyFormat("if (!(a->f())) {\n}");
4335 
4336   verifyFormat("a-- > b;");
4337   verifyFormat("b ? -a : c;");
4338   verifyFormat("n * sizeof char16;");
4339   verifyFormat("n * alignof char16;", getGoogleStyle());
4340   verifyFormat("sizeof(char);");
4341   verifyFormat("alignof(char);", getGoogleStyle());
4342 
4343   verifyFormat("return -1;");
4344   verifyFormat("switch (a) {\n"
4345                "case -1:\n"
4346                "  break;\n"
4347                "}");
4348   verifyFormat("#define X -1");
4349   verifyFormat("#define X -kConstant");
4350 
4351   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
4352   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
4353 
4354   verifyFormat("int a = /* confusing comment */ -1;");
4355   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
4356   verifyFormat("int a = i /* confusing comment */++;");
4357 }
4358 
4359 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
4360   verifyFormat("if (!aaaaaaaaaa( // break\n"
4361                "        aaaaa)) {\n"
4362                "}");
4363   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
4364                "               aaaaa));");
4365   verifyFormat("*aaa = aaaaaaa( // break\n"
4366                "    bbbbbb);");
4367 }
4368 
4369 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
4370   verifyFormat("bool operator<();");
4371   verifyFormat("bool operator>();");
4372   verifyFormat("bool operator=();");
4373   verifyFormat("bool operator==();");
4374   verifyFormat("bool operator!=();");
4375   verifyFormat("int operator+();");
4376   verifyFormat("int operator++();");
4377   verifyFormat("bool operator();");
4378   verifyFormat("bool operator()();");
4379   verifyFormat("bool operator[]();");
4380   verifyFormat("operator bool();");
4381   verifyFormat("operator int();");
4382   verifyFormat("operator void *();");
4383   verifyFormat("operator SomeType<int>();");
4384   verifyFormat("operator SomeType<int, int>();");
4385   verifyFormat("operator SomeType<SomeType<int>>();");
4386   verifyFormat("void *operator new(std::size_t size);");
4387   verifyFormat("void *operator new[](std::size_t size);");
4388   verifyFormat("void operator delete(void *ptr);");
4389   verifyFormat("void operator delete[](void *ptr);");
4390   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
4391                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
4392 
4393   verifyFormat(
4394       "ostream &operator<<(ostream &OutputStream,\n"
4395       "                    SomeReallyLongType WithSomeReallyLongValue);");
4396   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
4397                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
4398                "  return left.group < right.group;\n"
4399                "}");
4400   verifyFormat("SomeType &operator=(const SomeType &S);");
4401 
4402   verifyGoogleFormat("operator void*();");
4403   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
4404   verifyGoogleFormat("operator ::A();");
4405 
4406   verifyFormat("using A::operator+;");
4407 
4408   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
4409   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
4410   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
4411   verifyGoogleFormat("Deleted& operator=(const Deleted&)&& = delete;");
4412 }
4413 
4414 TEST_F(FormatTest, UnderstandsNewAndDelete) {
4415   verifyFormat("void f() {\n"
4416                "  A *a = new A;\n"
4417                "  A *a = new (placement) A;\n"
4418                "  delete a;\n"
4419                "  delete (A *)a;\n"
4420                "}");
4421 }
4422 
4423 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
4424   verifyFormat("int *f(int *a) {}");
4425   verifyFormat("int main(int argc, char **argv) {}");
4426   verifyFormat("Test::Test(int b) : a(b * b) {}");
4427   verifyIndependentOfContext("f(a, *a);");
4428   verifyFormat("void g() { f(*a); }");
4429   verifyIndependentOfContext("int a = b * 10;");
4430   verifyIndependentOfContext("int a = 10 * b;");
4431   verifyIndependentOfContext("int a = b * c;");
4432   verifyIndependentOfContext("int a += b * c;");
4433   verifyIndependentOfContext("int a -= b * c;");
4434   verifyIndependentOfContext("int a *= b * c;");
4435   verifyIndependentOfContext("int a /= b * c;");
4436   verifyIndependentOfContext("int a = *b;");
4437   verifyIndependentOfContext("int a = *b * c;");
4438   verifyIndependentOfContext("int a = b * *c;");
4439   verifyIndependentOfContext("return 10 * b;");
4440   verifyIndependentOfContext("return *b * *c;");
4441   verifyIndependentOfContext("return a & ~b;");
4442   verifyIndependentOfContext("f(b ? *c : *d);");
4443   verifyIndependentOfContext("int a = b ? *c : *d;");
4444   verifyIndependentOfContext("*b = a;");
4445   verifyIndependentOfContext("a * ~b;");
4446   verifyIndependentOfContext("a * !b;");
4447   verifyIndependentOfContext("a * +b;");
4448   verifyIndependentOfContext("a * -b;");
4449   verifyIndependentOfContext("a * ++b;");
4450   verifyIndependentOfContext("a * --b;");
4451   verifyIndependentOfContext("a[4] * b;");
4452   verifyIndependentOfContext("a[a * a] = 1;");
4453   verifyIndependentOfContext("f() * b;");
4454   verifyIndependentOfContext("a * [self dostuff];");
4455   verifyIndependentOfContext("int x = a * (a + b);");
4456   verifyIndependentOfContext("(a *)(a + b);");
4457   verifyIndependentOfContext("int *pa = (int *)&a;");
4458   verifyIndependentOfContext("return sizeof(int **);");
4459   verifyIndependentOfContext("return sizeof(int ******);");
4460   verifyIndependentOfContext("return (int **&)a;");
4461   verifyIndependentOfContext("f((*PointerToArray)[10]);");
4462   verifyFormat("void f(Type (*parameter)[10]) {}");
4463   verifyGoogleFormat("return sizeof(int**);");
4464   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
4465   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
4466   verifyFormat("auto a = [](int **&, int ***) {};");
4467   verifyFormat("auto PointerBinding = [](const char *S) {};");
4468   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
4469   verifyIndependentOfContext("typedef void (*f)(int *a);");
4470 
4471   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
4472 
4473   verifyIndependentOfContext("A<int *> a;");
4474   verifyIndependentOfContext("A<int **> a;");
4475   verifyIndependentOfContext("A<int *, int *> a;");
4476   verifyIndependentOfContext("A<int *[]> a;");
4477   verifyIndependentOfContext(
4478       "const char *const p = reinterpret_cast<const char *const>(q);");
4479   verifyIndependentOfContext("A<int **, int **> a;");
4480   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
4481   verifyFormat("for (char **a = b; *a; ++a) {\n}");
4482   verifyFormat("for (; a && b;) {\n}");
4483 
4484   verifyFormat(
4485       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4486       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4487 
4488   verifyGoogleFormat("int main(int argc, char** argv) {}");
4489   verifyGoogleFormat("A<int*> a;");
4490   verifyGoogleFormat("A<int**> a;");
4491   verifyGoogleFormat("A<int*, int*> a;");
4492   verifyGoogleFormat("A<int**, int**> a;");
4493   verifyGoogleFormat("f(b ? *c : *d);");
4494   verifyGoogleFormat("int a = b ? *c : *d;");
4495   verifyGoogleFormat("Type* t = **x;");
4496   verifyGoogleFormat("Type* t = *++*x;");
4497   verifyGoogleFormat("*++*x;");
4498   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
4499   verifyGoogleFormat("Type* t = x++ * y;");
4500   verifyGoogleFormat(
4501       "const char* const p = reinterpret_cast<const char* const>(q);");
4502 
4503   verifyIndependentOfContext("a = *(x + y);");
4504   verifyIndependentOfContext("a = &(x + y);");
4505   verifyIndependentOfContext("*(x + y).call();");
4506   verifyIndependentOfContext("&(x + y)->call();");
4507   verifyFormat("void f() { &(*I).first; }");
4508 
4509   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
4510   verifyFormat(
4511       "int *MyValues = {*A, // Operator detection might be confused by the '{'\n"
4512       "                 *BB // Operator detection might be confused by previous comment\n"
4513       "};");
4514 
4515   verifyIndependentOfContext("if (int *a = &b)");
4516   verifyIndependentOfContext("if (int &a = *b)");
4517   verifyIndependentOfContext("if (a & b[i])");
4518   verifyIndependentOfContext("if (a::b::c::d & b[i])");
4519   verifyIndependentOfContext("if (*b[i])");
4520   verifyIndependentOfContext("if (int *a = (&b))");
4521   verifyIndependentOfContext("while (int *a = &b)");
4522   verifyIndependentOfContext("size = sizeof *a;");
4523   verifyFormat("void f() {\n"
4524                "  for (const int &v : Values) {\n"
4525                "  }\n"
4526                "}");
4527   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
4528   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
4529   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
4530 
4531   verifyFormat("#define A (!a * b)");
4532   verifyFormat("#define MACRO     \\\n"
4533                "  int *i = a * b; \\\n"
4534                "  void f(a *b);",
4535                getLLVMStyleWithColumns(19));
4536 
4537   verifyIndependentOfContext("A = new SomeType *[Length];");
4538   verifyIndependentOfContext("A = new SomeType *[Length]();");
4539   verifyIndependentOfContext("T **t = new T *;");
4540   verifyIndependentOfContext("T **t = new T *();");
4541   verifyGoogleFormat("A = new SomeType* [Length]();");
4542   verifyGoogleFormat("A = new SomeType* [Length];");
4543   verifyGoogleFormat("T** t = new T*;");
4544   verifyGoogleFormat("T** t = new T*();");
4545 
4546   FormatStyle PointerLeft = getLLVMStyle();
4547   PointerLeft.PointerBindsToType = true;
4548   verifyFormat("delete *x;", PointerLeft);
4549   verifyFormat("STATIC_ASSERT((a & b) == 0);");
4550   verifyFormat("STATIC_ASSERT(0 == (a & b));");
4551   verifyFormat("template <bool a, bool b> "
4552                "typename t::if<x && y>::type f() {};");
4553   verifyFormat("template <int *y> f() {};");
4554   verifyFormat("vector<int *> v;");
4555   verifyFormat("vector<int *const> v;");
4556   verifyFormat("vector<int *const **const *> v;");
4557   verifyFormat("vector<int *volatile> v;");
4558   verifyFormat("vector<a * b> v;");
4559   verifyFormat("foo<b && false>();");
4560   verifyFormat("foo<b & 1>();");
4561 
4562   verifyIndependentOfContext("MACRO(int *i);");
4563   verifyIndependentOfContext("MACRO(auto *a);");
4564   verifyIndependentOfContext("MACRO(const A *a);");
4565   // FIXME: Is there a way to make this work?
4566   // verifyIndependentOfContext("MACRO(A *a);");
4567 
4568   // FIXME: We cannot handle this case yet; we might be able to figure out that
4569   // foo<x> d > v; doesn't make sense.
4570   verifyFormat("foo<a < b && c> d > v;");
4571 }
4572 
4573 TEST_F(FormatTest, UnderstandsAttributes) {
4574   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
4575   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
4576                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
4577 }
4578 
4579 TEST_F(FormatTest, UnderstandsEllipsis) {
4580   verifyFormat("int printf(const char *fmt, ...);");
4581   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
4582   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
4583 
4584   FormatStyle PointersLeft = getLLVMStyle();
4585   PointersLeft.PointerBindsToType = true;
4586   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
4587 }
4588 
4589 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
4590   EXPECT_EQ("int *a;\n"
4591             "int *a;\n"
4592             "int *a;",
4593             format("int *a;\n"
4594                    "int* a;\n"
4595                    "int *a;",
4596                    getGoogleStyle()));
4597   EXPECT_EQ("int* a;\n"
4598             "int* a;\n"
4599             "int* a;",
4600             format("int* a;\n"
4601                    "int* a;\n"
4602                    "int *a;",
4603                    getGoogleStyle()));
4604   EXPECT_EQ("int *a;\n"
4605             "int *a;\n"
4606             "int *a;",
4607             format("int *a;\n"
4608                    "int * a;\n"
4609                    "int *  a;",
4610                    getGoogleStyle()));
4611 }
4612 
4613 TEST_F(FormatTest, UnderstandsRvalueReferences) {
4614   verifyFormat("int f(int &&a) {}");
4615   verifyFormat("int f(int a, char &&b) {}");
4616   verifyFormat("void f() { int &&a = b; }");
4617   verifyGoogleFormat("int f(int a, char&& b) {}");
4618   verifyGoogleFormat("void f() { int&& a = b; }");
4619 
4620   verifyIndependentOfContext("A<int &&> a;");
4621   verifyIndependentOfContext("A<int &&, int &&> a;");
4622   verifyGoogleFormat("A<int&&> a;");
4623   verifyGoogleFormat("A<int&&, int&&> a;");
4624 
4625   // Not rvalue references:
4626   verifyFormat("template <bool B, bool C> class A {\n"
4627                "  static_assert(B && C, \"Something is wrong\");\n"
4628                "};");
4629   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
4630   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
4631 }
4632 
4633 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
4634   verifyFormat("void f() {\n"
4635                "  x[aaaaaaaaa -\n"
4636                "    b] = 23;\n"
4637                "}",
4638                getLLVMStyleWithColumns(15));
4639 }
4640 
4641 TEST_F(FormatTest, FormatsCasts) {
4642   verifyFormat("Type *A = static_cast<Type *>(P);");
4643   verifyFormat("Type *A = (Type *)P;");
4644   verifyFormat("Type *A = (vector<Type *, int *>)P;");
4645   verifyFormat("int a = (int)(2.0f);");
4646   verifyFormat("int a = (int)2.0f;");
4647   verifyFormat("x[(int32)y];");
4648   verifyFormat("x = (int32)y;");
4649   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
4650   verifyFormat("int a = (int)*b;");
4651   verifyFormat("int a = (int)2.0f;");
4652   verifyFormat("int a = (int)~0;");
4653   verifyFormat("int a = (int)++a;");
4654   verifyFormat("int a = (int)sizeof(int);");
4655   verifyFormat("int a = (int)+2;");
4656   verifyFormat("my_int a = (my_int)2.0f;");
4657   verifyFormat("my_int a = (my_int)sizeof(int);");
4658   verifyFormat("return (my_int)aaa;");
4659   verifyFormat("#define x ((int)-1)");
4660   verifyFormat("#define p(q) ((int *)&q)");
4661 
4662   // FIXME: Without type knowledge, this can still fall apart miserably.
4663   verifyFormat("void f() { my_int a = (my_int) * b; }");
4664   verifyFormat("void f() { return P ? (my_int) * P : (my_int)0; }");
4665   verifyFormat("my_int a = (my_int) ~0;");
4666   verifyFormat("my_int a = (my_int)++ a;");
4667   verifyFormat("my_int a = (my_int) + 2;");
4668 
4669   // Don't break after a cast's
4670   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4671                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
4672                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
4673 
4674   // These are not casts.
4675   verifyFormat("void f(int *) {}");
4676   verifyFormat("f(foo)->b;");
4677   verifyFormat("f(foo).b;");
4678   verifyFormat("f(foo)(b);");
4679   verifyFormat("f(foo)[b];");
4680   verifyFormat("[](foo) { return 4; }(bar);");
4681   verifyFormat("(*funptr)(foo)[4];");
4682   verifyFormat("funptrs[4](foo)[4];");
4683   verifyFormat("void f(int *);");
4684   verifyFormat("void f(int *) = 0;");
4685   verifyFormat("void f(SmallVector<int>) {}");
4686   verifyFormat("void f(SmallVector<int>);");
4687   verifyFormat("void f(SmallVector<int>) = 0;");
4688   verifyFormat("void f(int i = (kValue) * kMask) {}");
4689   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
4690   verifyFormat("int a = sizeof(int) * b;");
4691   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
4692   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
4693   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
4694   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
4695 
4696   // These are not casts, but at some point were confused with casts.
4697   verifyFormat("virtual void foo(int *) override;");
4698   verifyFormat("virtual void foo(char &) const;");
4699   verifyFormat("virtual void foo(int *a, char *) const;");
4700   verifyFormat("int a = sizeof(int *) + b;");
4701   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
4702 
4703   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
4704                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4705   // FIXME: The indentation here is not ideal.
4706   verifyFormat(
4707       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4708       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
4709       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
4710 }
4711 
4712 TEST_F(FormatTest, FormatsFunctionTypes) {
4713   verifyFormat("A<bool()> a;");
4714   verifyFormat("A<SomeType()> a;");
4715   verifyFormat("A<void (*)(int, std::string)> a;");
4716   verifyFormat("A<void *(int)>;");
4717   verifyFormat("void *(*a)(int *, SomeType *);");
4718   verifyFormat("int (*func)(void *);");
4719   verifyFormat("void f() { int (*func)(void *); }");
4720   verifyFormat("template <class CallbackClass>\n"
4721                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
4722 
4723   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
4724   verifyGoogleFormat("void* (*a)(int);");
4725   verifyGoogleFormat(
4726       "template <class CallbackClass>\n"
4727       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
4728 
4729   // Other constructs can look somewhat like function types:
4730   verifyFormat("A<sizeof(*x)> a;");
4731   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
4732   verifyFormat("some_var = function(*some_pointer_var)[0];");
4733   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
4734 }
4735 
4736 TEST_F(FormatTest, BreaksLongDeclarations) {
4737   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
4738                "    AnotherNameForTheLongType;",
4739                getGoogleStyle());
4740   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
4741                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4742                getGoogleStyle());
4743   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
4744                "    LoooooooooooooooooooooooooooooooooooooooongVariable;",
4745                getGoogleStyle());
4746   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
4747                "    LoooooooooooooooooooooooooooooooooooooooongVariable;",
4748                getGoogleStyle());
4749   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
4750                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
4751                getGoogleStyle());
4752   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
4753                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
4754   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
4755                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
4756 
4757   // FIXME: Without the comment, this breaks after "(".
4758   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
4759                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
4760                getGoogleStyle());
4761 
4762   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
4763                "                  int LoooooooooooooooooooongParam2) {}");
4764   verifyFormat(
4765       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
4766       "                                   SourceLocation L, IdentifierIn *II,\n"
4767       "                                   Type *T) {}");
4768   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
4769                "ReallyReallyLongFunctionName(\n"
4770                "    const std::string &SomeParameter,\n"
4771                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
4772                "        ReallyReallyLongParameterName,\n"
4773                "    const SomeType<string, SomeOtherTemplateParameter> &\n"
4774                "        AnotherLongParameterName) {}");
4775   verifyFormat("template <typename A>\n"
4776                "SomeLoooooooooooooooooooooongType<\n"
4777                "    typename some_namespace::SomeOtherType<A>::Type>\n"
4778                "Function() {}");
4779 
4780   verifyGoogleFormat(
4781       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
4782       "    aaaaaaaaaaaaaaaaaaaaaaa;");
4783   verifyGoogleFormat(
4784       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
4785       "                                   SourceLocation L) {}");
4786   verifyGoogleFormat(
4787       "some_namespace::LongReturnType\n"
4788       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
4789       "    int first_long_parameter, int second_parameter) {}");
4790 
4791   verifyGoogleFormat("template <typename T>\n"
4792                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
4793                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
4794   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4795                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
4796 }
4797 
4798 TEST_F(FormatTest, FormatsArrays) {
4799   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4800                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
4801   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4802                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
4803   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4804                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
4805   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4806                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4807                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
4808   verifyFormat(
4809       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
4810       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
4811       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
4812 
4813   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
4814                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
4815 }
4816 
4817 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
4818   verifyFormat("(a)->b();");
4819   verifyFormat("--a;");
4820 }
4821 
4822 TEST_F(FormatTest, HandlesIncludeDirectives) {
4823   verifyFormat("#include <string>\n"
4824                "#include <a/b/c.h>\n"
4825                "#include \"a/b/string\"\n"
4826                "#include \"string.h\"\n"
4827                "#include \"string.h\"\n"
4828                "#include <a-a>\n"
4829                "#include < path with space >\n"
4830                "#include \"abc.h\" // this is included for ABC\n"
4831                "#include \"some long include\" // with a comment\n"
4832                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
4833                getLLVMStyleWithColumns(35));
4834   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
4835   EXPECT_EQ("#include <a>", format("#include<a>"));
4836 
4837   verifyFormat("#import <string>");
4838   verifyFormat("#import <a/b/c.h>");
4839   verifyFormat("#import \"a/b/string\"");
4840   verifyFormat("#import \"string.h\"");
4841   verifyFormat("#import \"string.h\"");
4842   verifyFormat("#if __has_include(<strstream>)\n"
4843                "#include <strstream>\n"
4844                "#endif");
4845 
4846   // Protocol buffer definition or missing "#".
4847   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
4848                getLLVMStyleWithColumns(30));
4849 }
4850 
4851 //===----------------------------------------------------------------------===//
4852 // Error recovery tests.
4853 //===----------------------------------------------------------------------===//
4854 
4855 TEST_F(FormatTest, IncompleteParameterLists) {
4856   FormatStyle NoBinPacking = getLLVMStyle();
4857   NoBinPacking.BinPackParameters = false;
4858   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
4859                "                        double *min_x,\n"
4860                "                        double *max_x,\n"
4861                "                        double *min_y,\n"
4862                "                        double *max_y,\n"
4863                "                        double *min_z,\n"
4864                "                        double *max_z, ) {}",
4865                NoBinPacking);
4866 }
4867 
4868 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
4869   verifyFormat("void f() { return; }\n42");
4870   verifyFormat("void f() {\n"
4871                "  if (0)\n"
4872                "    return;\n"
4873                "}\n"
4874                "42");
4875   verifyFormat("void f() { return }\n42");
4876   verifyFormat("void f() {\n"
4877                "  if (0)\n"
4878                "    return\n"
4879                "}\n"
4880                "42");
4881 }
4882 
4883 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
4884   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
4885   EXPECT_EQ("void f() {\n"
4886             "  if (a)\n"
4887             "    return\n"
4888             "}",
4889             format("void  f  (  )  {  if  ( a )  return  }"));
4890   EXPECT_EQ("namespace N {\n"
4891             "void f()\n"
4892             "}",
4893             format("namespace  N  {  void f()  }"));
4894   EXPECT_EQ("namespace N {\n"
4895             "void f() {}\n"
4896             "void g()\n"
4897             "}",
4898             format("namespace N  { void f( ) { } void g( ) }"));
4899 }
4900 
4901 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
4902   verifyFormat("int aaaaaaaa =\n"
4903                "    // Overlylongcomment\n"
4904                "    b;",
4905                getLLVMStyleWithColumns(20));
4906   verifyFormat("function(\n"
4907                "    ShortArgument,\n"
4908                "    LoooooooooooongArgument);\n",
4909                getLLVMStyleWithColumns(20));
4910 }
4911 
4912 TEST_F(FormatTest, IncorrectAccessSpecifier) {
4913   verifyFormat("public:");
4914   verifyFormat("class A {\n"
4915                "public\n"
4916                "  void f() {}\n"
4917                "};");
4918   verifyFormat("public\n"
4919                "int qwerty;");
4920   verifyFormat("public\n"
4921                "B {}");
4922   verifyFormat("public\n"
4923                "{}");
4924   verifyFormat("public\n"
4925                "B { int x; }");
4926 }
4927 
4928 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
4929   verifyFormat("{");
4930   verifyFormat("#})");
4931 }
4932 
4933 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
4934   verifyFormat("do {\n}");
4935   verifyFormat("do {\n}\n"
4936                "f();");
4937   verifyFormat("do {\n}\n"
4938                "wheeee(fun);");
4939   verifyFormat("do {\n"
4940                "  f();\n"
4941                "}");
4942 }
4943 
4944 TEST_F(FormatTest, IncorrectCodeMissingParens) {
4945   verifyFormat("if {\n  foo;\n  foo();\n}");
4946   verifyFormat("switch {\n  foo;\n  foo();\n}");
4947   verifyFormat("for {\n  foo;\n  foo();\n}");
4948   verifyFormat("while {\n  foo;\n  foo();\n}");
4949   verifyFormat("do {\n  foo;\n  foo();\n} while;");
4950 }
4951 
4952 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
4953   verifyFormat("namespace {\n"
4954                "class Foo { Foo (\n"
4955                "};\n"
4956                "} // comment");
4957 }
4958 
4959 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
4960   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
4961   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
4962   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
4963   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
4964 
4965   EXPECT_EQ("{\n"
4966             "  {\n"
4967             "    breakme(\n"
4968             "        qwe);\n"
4969             "  }\n",
4970             format("{\n"
4971                    "    {\n"
4972                    " breakme(qwe);\n"
4973                    "}\n",
4974                    getLLVMStyleWithColumns(10)));
4975 }
4976 
4977 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
4978   verifyFormat("int x = {\n"
4979                "    avariable,\n"
4980                "    b(alongervariable)};",
4981                getLLVMStyleWithColumns(25));
4982 }
4983 
4984 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
4985   verifyFormat("return (a)(b) {1, 2, 3};");
4986 }
4987 
4988 TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) {
4989   verifyFormat("vector<int> x{1, 2, 3, 4};");
4990   verifyFormat("vector<int> x{\n"
4991                "    1, 2, 3, 4,\n"
4992                "};");
4993   verifyFormat("vector<T> x{{}, {}, {}, {}};");
4994   verifyFormat("f({1, 2});");
4995   verifyFormat("auto v = Foo{-1};");
4996   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
4997   verifyFormat("Class::Class : member{1, 2, 3} {}");
4998   verifyFormat("new vector<int>{1, 2, 3};");
4999   verifyFormat("new int[3]{1, 2, 3};");
5000   verifyFormat("return {arg1, arg2};");
5001   verifyFormat("return {arg1, SomeType{parameter}};");
5002   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
5003   verifyFormat("new T{arg1, arg2};");
5004   verifyFormat("f(MyMap[{composite, key}]);");
5005   verifyFormat("class Class {\n"
5006                "  T member = {arg1, arg2};\n"
5007                "};");
5008   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
5009 
5010   // FIXME: The alignment of these trailing comments might be bad. Then again,
5011   // this might be utterly useless in real code.
5012   verifyFormat("Constructor::Constructor()\n"
5013                "    : some_value{        //\n"
5014                "                 aaaaaaa //\n"
5015                "      } {}");
5016 
5017   // In braced lists, the first comment is always assumed to belong to the
5018   // first element. Thus, it can be moved to the next or previous line as
5019   // appropriate.
5020   EXPECT_EQ("function({// First element:\n"
5021             "          1,\n"
5022             "          // Second element:\n"
5023             "          2});",
5024             format("function({\n"
5025                    "    // First element:\n"
5026                    "    1,\n"
5027                    "    // Second element:\n"
5028                    "    2});"));
5029   EXPECT_EQ("std::vector<int> MyNumbers{\n"
5030             "    // First element:\n"
5031             "    1,\n"
5032             "    // Second element:\n"
5033             "    2};",
5034             format("std::vector<int> MyNumbers{// First element:\n"
5035                    "                           1,\n"
5036                    "                           // Second element:\n"
5037                    "                           2};",
5038                    getLLVMStyleWithColumns(30)));
5039 
5040   FormatStyle ExtraSpaces = getLLVMStyle();
5041   ExtraSpaces.Cpp11BracedListStyle = false;
5042   ExtraSpaces.ColumnLimit = 75;
5043   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
5044   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
5045   verifyFormat("f({ 1, 2 });", ExtraSpaces);
5046   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
5047   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
5048   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
5049   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
5050   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
5051   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
5052   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
5053   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
5054   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
5055   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
5056   verifyFormat("class Class {\n"
5057                "  T member = { arg1, arg2 };\n"
5058                "};",
5059                ExtraSpaces);
5060   verifyFormat(
5061       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5062       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
5063       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
5064       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
5065       ExtraSpaces);
5066   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
5067   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
5068                ExtraSpaces);
5069   verifyFormat(
5070       "someFunction(OtherParam,\n"
5071       "             BracedList{ // comment 1 (Forcing interesting break)\n"
5072       "                         param1, param2,\n"
5073       "                         // comment 2\n"
5074       "                         param3, param4 });",
5075       ExtraSpaces);
5076   verifyFormat(
5077       "std::this_thread::sleep_for(\n"
5078       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
5079       ExtraSpaces);
5080   verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
5081                "  aaaaaaa,      aaaaaaaaaa,\n"
5082                "  aaaaa,        aaaaaaaaaaaaaaa,\n"
5083                "  aaa,          aaaaaaaaaa,\n"
5084                "  a,            aaaaaaaaaaaaaaaaaaaaa,\n"
5085                "  aaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
5086                "  aaaaaaa,      a\n"
5087                "};",
5088                ExtraSpaces);
5089   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
5090 }
5091 
5092 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
5093   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5094                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5095                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5096                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5097                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5098                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5099   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5100                "                 // line comment\n"
5101                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5102                "                 1, 22, 333, 4444, 55555,\n"
5103                "                 // line comment\n"
5104                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
5105                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
5106   verifyFormat(
5107       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5108       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
5109       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
5110       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5111       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5112       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
5113       "                 7777777};");
5114   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
5115                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
5116                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
5117   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5118                "                 1, 1, 1, 1};",
5119                getLLVMStyleWithColumns(39));
5120   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5121                "                 1, 1, 1, 1};",
5122                getLLVMStyleWithColumns(38));
5123   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
5124                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
5125                getLLVMStyleWithColumns(43));
5126 
5127   // Trailing commas.
5128   verifyFormat("vector<int> x = {\n"
5129                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
5130                "};",
5131                getLLVMStyleWithColumns(39));
5132   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5133                "                 1, 1, 1, 1, //\n"
5134                "};",
5135                getLLVMStyleWithColumns(39));
5136   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
5137                "                 1, 1, 1, 1,\n"
5138                "                 /**/ /**/};",
5139                getLLVMStyleWithColumns(39));
5140   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
5141                "        {aaaaaaaaaaaaaaaaaaa},\n"
5142                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
5143                "        {aaaaaaaaaaaaaaaaa}};",
5144                getLLVMStyleWithColumns(60));
5145 
5146   // With nested lists, we should either format one item per line or all nested
5147   // lists one one line.
5148   // FIXME: For some nested lists, we can do better.
5149   verifyFormat(
5150       "SomeStruct my_struct_array = {\n"
5151       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
5152       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
5153       "    {aaa, aaa},\n"
5154       "    {aaa, aaa},\n"
5155       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
5156       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
5157       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
5158 
5159   // No column layout should be used here.
5160   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
5161                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
5162 }
5163 
5164 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
5165   FormatStyle DoNotMerge = getLLVMStyle();
5166   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
5167 
5168   verifyFormat("void f() { return 42; }");
5169   verifyFormat("void f() {\n"
5170                "  return 42;\n"
5171                "}",
5172                DoNotMerge);
5173   verifyFormat("void f() {\n"
5174                "  // Comment\n"
5175                "}");
5176   verifyFormat("{\n"
5177                "#error {\n"
5178                "  int a;\n"
5179                "}");
5180   verifyFormat("{\n"
5181                "  int a;\n"
5182                "#error {\n"
5183                "}");
5184   verifyFormat("void f() {} // comment");
5185   verifyFormat("void f() { int a; } // comment");
5186   verifyFormat("void f() {\n"
5187                "} // comment",
5188                DoNotMerge);
5189   verifyFormat("void f() {\n"
5190                "  int a;\n"
5191                "} // comment",
5192                DoNotMerge);
5193   verifyFormat("void f() {\n"
5194                "} // comment",
5195                getLLVMStyleWithColumns(15));
5196 
5197   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
5198   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
5199 
5200   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
5201   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
5202   verifyFormat("class C {\n"
5203                "  C()\n"
5204                "      : iiiiiiii(nullptr),\n"
5205                "        kkkkkkk(nullptr),\n"
5206                "        mmmmmmm(nullptr),\n"
5207                "        nnnnnnn(nullptr) {}\n"
5208                "};",
5209                getGoogleStyle());
5210 
5211   FormatStyle NoColumnLimit = getLLVMStyle();
5212   NoColumnLimit.ColumnLimit = 0;
5213   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
5214   EXPECT_EQ("class C {\n"
5215             "  A() : b(0) {}\n"
5216             "};", format("class C{A():b(0){}};", NoColumnLimit));
5217   EXPECT_EQ("A()\n"
5218             "    : b(0) {\n"
5219             "}",
5220             format("A()\n:b(0)\n{\n}", NoColumnLimit));
5221 
5222   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
5223   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
5224       FormatStyle::SFS_None;
5225   EXPECT_EQ("A()\n"
5226             "    : b(0) {\n"
5227             "}",
5228             format("A():b(0){}", DoNotMergeNoColumnLimit));
5229   EXPECT_EQ("A()\n"
5230             "    : b(0) {\n"
5231             "}",
5232             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
5233 
5234   verifyFormat("#define A          \\\n"
5235                "  void f() {       \\\n"
5236                "    int i;         \\\n"
5237                "  }",
5238                getLLVMStyleWithColumns(20));
5239   verifyFormat("#define A           \\\n"
5240                "  void f() { int i; }",
5241                getLLVMStyleWithColumns(21));
5242   verifyFormat("#define A            \\\n"
5243                "  void f() {         \\\n"
5244                "    int i;           \\\n"
5245                "  }                  \\\n"
5246                "  int j;",
5247                getLLVMStyleWithColumns(22));
5248   verifyFormat("#define A             \\\n"
5249                "  void f() { int i; } \\\n"
5250                "  int j;",
5251                getLLVMStyleWithColumns(23));
5252 }
5253 
5254 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
5255   FormatStyle MergeInlineOnly = getLLVMStyle();
5256   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
5257   verifyFormat("class C {\n"
5258                "  int f() { return 42; }\n"
5259                "};",
5260                MergeInlineOnly);
5261   verifyFormat("int f() {\n"
5262                "  return 42;\n"
5263                "}",
5264                MergeInlineOnly);
5265 }
5266 
5267 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
5268   // Elaborate type variable declarations.
5269   verifyFormat("struct foo a = {bar};\nint n;");
5270   verifyFormat("class foo a = {bar};\nint n;");
5271   verifyFormat("union foo a = {bar};\nint n;");
5272 
5273   // Elaborate types inside function definitions.
5274   verifyFormat("struct foo f() {}\nint n;");
5275   verifyFormat("class foo f() {}\nint n;");
5276   verifyFormat("union foo f() {}\nint n;");
5277 
5278   // Templates.
5279   verifyFormat("template <class X> void f() {}\nint n;");
5280   verifyFormat("template <struct X> void f() {}\nint n;");
5281   verifyFormat("template <union X> void f() {}\nint n;");
5282 
5283   // Actual definitions...
5284   verifyFormat("struct {\n} n;");
5285   verifyFormat(
5286       "template <template <class T, class Y>, class Z> class X {\n} n;");
5287   verifyFormat("union Z {\n  int n;\n} x;");
5288   verifyFormat("class MACRO Z {\n} n;");
5289   verifyFormat("class MACRO(X) Z {\n} n;");
5290   verifyFormat("class __attribute__(X) Z {\n} n;");
5291   verifyFormat("class __declspec(X) Z {\n} n;");
5292   verifyFormat("class A##B##C {\n} n;");
5293   verifyFormat("class alignas(16) Z {\n} n;");
5294 
5295   // Redefinition from nested context:
5296   verifyFormat("class A::B::C {\n} n;");
5297 
5298   // Template definitions.
5299   verifyFormat(
5300       "template <typename F>\n"
5301       "Matcher(const Matcher<F> &Other,\n"
5302       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
5303       "                             !is_same<F, T>::value>::type * = 0)\n"
5304       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
5305 
5306   // FIXME: This is still incorrectly handled at the formatter side.
5307   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
5308 
5309   // FIXME:
5310   // This now gets parsed incorrectly as class definition.
5311   // verifyFormat("class A<int> f() {\n}\nint n;");
5312 
5313   // Elaborate types where incorrectly parsing the structural element would
5314   // break the indent.
5315   verifyFormat("if (true)\n"
5316                "  class X x;\n"
5317                "else\n"
5318                "  f();\n");
5319 
5320   // This is simply incomplete. Formatting is not important, but must not crash.
5321   verifyFormat("class A:");
5322 }
5323 
5324 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
5325   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
5326             format("#error Leave     all         white!!!!! space* alone!\n"));
5327   EXPECT_EQ(
5328       "#warning Leave     all         white!!!!! space* alone!\n",
5329       format("#warning Leave     all         white!!!!! space* alone!\n"));
5330   EXPECT_EQ("#error 1", format("  #  error   1"));
5331   EXPECT_EQ("#warning 1", format("  #  warning 1"));
5332 }
5333 
5334 TEST_F(FormatTest, FormatHashIfExpressions) {
5335   verifyFormat("#if AAAA && BBBB");
5336   // FIXME: Come up with a better indentation for #elif.
5337   verifyFormat(
5338       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
5339       "    defined(BBBBBBBB)\n"
5340       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
5341       "    defined(BBBBBBBB)\n"
5342       "#endif",
5343       getLLVMStyleWithColumns(65));
5344 }
5345 
5346 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
5347   FormatStyle AllowsMergedIf = getGoogleStyle();
5348   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
5349   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
5350   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
5351   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
5352   EXPECT_EQ("if (true) return 42;",
5353             format("if (true)\nreturn 42;", AllowsMergedIf));
5354   FormatStyle ShortMergedIf = AllowsMergedIf;
5355   ShortMergedIf.ColumnLimit = 25;
5356   verifyFormat("#define A \\\n"
5357                "  if (true) return 42;",
5358                ShortMergedIf);
5359   verifyFormat("#define A \\\n"
5360                "  f();    \\\n"
5361                "  if (true)\n"
5362                "#define B",
5363                ShortMergedIf);
5364   verifyFormat("#define A \\\n"
5365                "  f();    \\\n"
5366                "  if (true)\n"
5367                "g();",
5368                ShortMergedIf);
5369   verifyFormat("{\n"
5370                "#ifdef A\n"
5371                "  // Comment\n"
5372                "  if (true) continue;\n"
5373                "#endif\n"
5374                "  // Comment\n"
5375                "  if (true) continue;\n"
5376                "}",
5377                ShortMergedIf);
5378   ShortMergedIf.ColumnLimit = 29;
5379   verifyFormat("#define A                   \\\n"
5380                "  if (aaaaaaaaaa) return 1; \\\n"
5381                "  return 2;",
5382                ShortMergedIf);
5383   ShortMergedIf.ColumnLimit = 28;
5384   verifyFormat("#define A         \\\n"
5385                "  if (aaaaaaaaaa) \\\n"
5386                "    return 1;     \\\n"
5387                "  return 2;",
5388                ShortMergedIf);
5389 }
5390 
5391 TEST_F(FormatTest, BlockCommentsInControlLoops) {
5392   verifyFormat("if (0) /* a comment in a strange place */ {\n"
5393                "  f();\n"
5394                "}");
5395   verifyFormat("if (0) /* a comment in a strange place */ {\n"
5396                "  f();\n"
5397                "} /* another comment */ else /* comment #3 */ {\n"
5398                "  g();\n"
5399                "}");
5400   verifyFormat("while (0) /* a comment in a strange place */ {\n"
5401                "  f();\n"
5402                "}");
5403   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
5404                "  f();\n"
5405                "}");
5406   verifyFormat("do /* a comment in a strange place */ {\n"
5407                "  f();\n"
5408                "} /* another comment */ while (0);");
5409 }
5410 
5411 TEST_F(FormatTest, BlockComments) {
5412   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
5413             format("/* *//* */  /* */\n/* *//* */  /* */"));
5414   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
5415   EXPECT_EQ("#define A /*123*/ \\\n"
5416             "  b\n"
5417             "/* */\n"
5418             "someCall(\n"
5419             "    parameter);",
5420             format("#define A /*123*/ b\n"
5421                    "/* */\n"
5422                    "someCall(parameter);",
5423                    getLLVMStyleWithColumns(15)));
5424 
5425   EXPECT_EQ("#define A\n"
5426             "/* */ someCall(\n"
5427             "    parameter);",
5428             format("#define A\n"
5429                    "/* */someCall(parameter);",
5430                    getLLVMStyleWithColumns(15)));
5431   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
5432   EXPECT_EQ("/*\n"
5433             "*\n"
5434             " * aaaaaa\n"
5435             "*aaaaaa\n"
5436             "*/",
5437             format("/*\n"
5438                    "*\n"
5439                    " * aaaaaa aaaaaa\n"
5440                    "*/",
5441                    getLLVMStyleWithColumns(10)));
5442   EXPECT_EQ("/*\n"
5443             "**\n"
5444             "* aaaaaa\n"
5445             "*aaaaaa\n"
5446             "*/",
5447             format("/*\n"
5448                    "**\n"
5449                    "* aaaaaa aaaaaa\n"
5450                    "*/",
5451                    getLLVMStyleWithColumns(10)));
5452 
5453   FormatStyle NoBinPacking = getLLVMStyle();
5454   NoBinPacking.BinPackParameters = false;
5455   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
5456             "             2, /* comment 2 */\n"
5457             "             3, /* comment 3 */\n"
5458             "             aaaa,\n"
5459             "             bbbb);",
5460             format("someFunction (1,   /* comment 1 */\n"
5461                    "                2, /* comment 2 */  \n"
5462                    "               3,   /* comment 3 */\n"
5463                    "aaaa, bbbb );",
5464                    NoBinPacking));
5465   verifyFormat(
5466       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5467       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5468   EXPECT_EQ(
5469       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
5470       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
5471       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
5472       format(
5473           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
5474           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
5475           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
5476   EXPECT_EQ(
5477       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
5478       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
5479       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
5480       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
5481              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
5482              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
5483 
5484   verifyFormat("void f(int * /* unused */) {}");
5485 
5486   EXPECT_EQ("/*\n"
5487             " **\n"
5488             " */",
5489             format("/*\n"
5490                    " **\n"
5491                    " */"));
5492   EXPECT_EQ("/*\n"
5493             " *q\n"
5494             " */",
5495             format("/*\n"
5496                    " *q\n"
5497                    " */"));
5498   EXPECT_EQ("/*\n"
5499             " * q\n"
5500             " */",
5501             format("/*\n"
5502                    " * q\n"
5503                    " */"));
5504   EXPECT_EQ("/*\n"
5505             " **/",
5506             format("/*\n"
5507                    " **/"));
5508   EXPECT_EQ("/*\n"
5509             " ***/",
5510             format("/*\n"
5511                    " ***/"));
5512 }
5513 
5514 TEST_F(FormatTest, BlockCommentsInMacros) {
5515   EXPECT_EQ("#define A          \\\n"
5516             "  {                \\\n"
5517             "    /* one line */ \\\n"
5518             "    someCall();",
5519             format("#define A {        \\\n"
5520                    "  /* one line */   \\\n"
5521                    "  someCall();",
5522                    getLLVMStyleWithColumns(20)));
5523   EXPECT_EQ("#define A          \\\n"
5524             "  {                \\\n"
5525             "    /* previous */ \\\n"
5526             "    /* one line */ \\\n"
5527             "    someCall();",
5528             format("#define A {        \\\n"
5529                    "  /* previous */   \\\n"
5530                    "  /* one line */   \\\n"
5531                    "  someCall();",
5532                    getLLVMStyleWithColumns(20)));
5533 }
5534 
5535 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
5536   EXPECT_EQ("a = {\n"
5537             "    1111 /*    */\n"
5538             "};",
5539             format("a = {1111 /*    */\n"
5540                    "};",
5541                    getLLVMStyleWithColumns(15)));
5542   EXPECT_EQ("a = {\n"
5543             "    1111 /*      */\n"
5544             "};",
5545             format("a = {1111 /*      */\n"
5546                    "};",
5547                    getLLVMStyleWithColumns(15)));
5548 
5549   // FIXME: The formatting is still wrong here.
5550   EXPECT_EQ("a = {\n"
5551             "    1111 /*      a\n"
5552             "            */\n"
5553             "};",
5554             format("a = {1111 /*      a */\n"
5555                    "};",
5556                    getLLVMStyleWithColumns(15)));
5557 }
5558 
5559 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
5560   // FIXME: This is not what we want...
5561   verifyFormat("{\n"
5562                "// a"
5563                "// b");
5564 }
5565 
5566 TEST_F(FormatTest, FormatStarDependingOnContext) {
5567   verifyFormat("void f(int *a);");
5568   verifyFormat("void f() { f(fint * b); }");
5569   verifyFormat("class A {\n  void f(int *a);\n};");
5570   verifyFormat("class A {\n  int *a;\n};");
5571   verifyFormat("namespace a {\n"
5572                "namespace b {\n"
5573                "class A {\n"
5574                "  void f() {}\n"
5575                "  int *a;\n"
5576                "};\n"
5577                "}\n"
5578                "}");
5579 }
5580 
5581 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
5582   verifyFormat("while");
5583   verifyFormat("operator");
5584 }
5585 
5586 //===----------------------------------------------------------------------===//
5587 // Objective-C tests.
5588 //===----------------------------------------------------------------------===//
5589 
5590 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
5591   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
5592   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
5593             format("-(NSUInteger)indexOfObject:(id)anObject;"));
5594   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
5595   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
5596   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
5597             format("-(NSInteger)Method3:(id)anObject;"));
5598   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
5599             format("-(NSInteger)Method4:(id)anObject;"));
5600   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
5601             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
5602   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
5603             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
5604   EXPECT_EQ(
5605       "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;",
5606       format(
5607           "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;"));
5608 
5609   // Very long objectiveC method declaration.
5610   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
5611                "                    inRange:(NSRange)range\n"
5612                "                   outRange:(NSRange)out_range\n"
5613                "                  outRange1:(NSRange)out_range1\n"
5614                "                  outRange2:(NSRange)out_range2\n"
5615                "                  outRange3:(NSRange)out_range3\n"
5616                "                  outRange4:(NSRange)out_range4\n"
5617                "                  outRange5:(NSRange)out_range5\n"
5618                "                  outRange6:(NSRange)out_range6\n"
5619                "                  outRange7:(NSRange)out_range7\n"
5620                "                  outRange8:(NSRange)out_range8\n"
5621                "                  outRange9:(NSRange)out_range9;");
5622 
5623   verifyFormat("- (int)sum:(vector<int>)numbers;");
5624   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
5625   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
5626   // protocol lists (but not for template classes):
5627   //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
5628 
5629   verifyFormat("- (int (*)())foo:(int (*)())f;");
5630   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
5631 
5632   // If there's no return type (very rare in practice!), LLVM and Google style
5633   // agree.
5634   verifyFormat("- foo;");
5635   verifyFormat("- foo:(int)f;");
5636   verifyGoogleFormat("- foo:(int)foo;");
5637 }
5638 
5639 TEST_F(FormatTest, FormatObjCInterface) {
5640   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
5641                "@public\n"
5642                "  int field1;\n"
5643                "@protected\n"
5644                "  int field2;\n"
5645                "@private\n"
5646                "  int field3;\n"
5647                "@package\n"
5648                "  int field4;\n"
5649                "}\n"
5650                "+ (id)init;\n"
5651                "@end");
5652 
5653   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
5654                      " @public\n"
5655                      "  int field1;\n"
5656                      " @protected\n"
5657                      "  int field2;\n"
5658                      " @private\n"
5659                      "  int field3;\n"
5660                      " @package\n"
5661                      "  int field4;\n"
5662                      "}\n"
5663                      "+ (id)init;\n"
5664                      "@end");
5665 
5666   verifyFormat("@interface /* wait for it */ Foo\n"
5667                "+ (id)init;\n"
5668                "// Look, a comment!\n"
5669                "- (int)answerWith:(int)i;\n"
5670                "@end");
5671 
5672   verifyFormat("@interface Foo\n"
5673                "@end\n"
5674                "@interface Bar\n"
5675                "@end");
5676 
5677   verifyFormat("@interface Foo : Bar\n"
5678                "+ (id)init;\n"
5679                "@end");
5680 
5681   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
5682                "+ (id)init;\n"
5683                "@end");
5684 
5685   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
5686                      "+ (id)init;\n"
5687                      "@end");
5688 
5689   verifyFormat("@interface Foo (HackStuff)\n"
5690                "+ (id)init;\n"
5691                "@end");
5692 
5693   verifyFormat("@interface Foo ()\n"
5694                "+ (id)init;\n"
5695                "@end");
5696 
5697   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
5698                "+ (id)init;\n"
5699                "@end");
5700 
5701   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
5702                      "+ (id)init;\n"
5703                      "@end");
5704 
5705   verifyFormat("@interface Foo {\n"
5706                "  int _i;\n"
5707                "}\n"
5708                "+ (id)init;\n"
5709                "@end");
5710 
5711   verifyFormat("@interface Foo : Bar {\n"
5712                "  int _i;\n"
5713                "}\n"
5714                "+ (id)init;\n"
5715                "@end");
5716 
5717   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
5718                "  int _i;\n"
5719                "}\n"
5720                "+ (id)init;\n"
5721                "@end");
5722 
5723   verifyFormat("@interface Foo (HackStuff) {\n"
5724                "  int _i;\n"
5725                "}\n"
5726                "+ (id)init;\n"
5727                "@end");
5728 
5729   verifyFormat("@interface Foo () {\n"
5730                "  int _i;\n"
5731                "}\n"
5732                "+ (id)init;\n"
5733                "@end");
5734 
5735   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
5736                "  int _i;\n"
5737                "}\n"
5738                "+ (id)init;\n"
5739                "@end");
5740 }
5741 
5742 TEST_F(FormatTest, FormatObjCImplementation) {
5743   verifyFormat("@implementation Foo : NSObject {\n"
5744                "@public\n"
5745                "  int field1;\n"
5746                "@protected\n"
5747                "  int field2;\n"
5748                "@private\n"
5749                "  int field3;\n"
5750                "@package\n"
5751                "  int field4;\n"
5752                "}\n"
5753                "+ (id)init {\n}\n"
5754                "@end");
5755 
5756   verifyGoogleFormat("@implementation Foo : NSObject {\n"
5757                      " @public\n"
5758                      "  int field1;\n"
5759                      " @protected\n"
5760                      "  int field2;\n"
5761                      " @private\n"
5762                      "  int field3;\n"
5763                      " @package\n"
5764                      "  int field4;\n"
5765                      "}\n"
5766                      "+ (id)init {\n}\n"
5767                      "@end");
5768 
5769   verifyFormat("@implementation Foo\n"
5770                "+ (id)init {\n"
5771                "  if (true)\n"
5772                "    return nil;\n"
5773                "}\n"
5774                "// Look, a comment!\n"
5775                "- (int)answerWith:(int)i {\n"
5776                "  return i;\n"
5777                "}\n"
5778                "+ (int)answerWith:(int)i {\n"
5779                "  return i;\n"
5780                "}\n"
5781                "@end");
5782 
5783   verifyFormat("@implementation Foo\n"
5784                "@end\n"
5785                "@implementation Bar\n"
5786                "@end");
5787 
5788   verifyFormat("@implementation Foo : Bar\n"
5789                "+ (id)init {\n}\n"
5790                "- (void)foo {\n}\n"
5791                "@end");
5792 
5793   verifyFormat("@implementation Foo {\n"
5794                "  int _i;\n"
5795                "}\n"
5796                "+ (id)init {\n}\n"
5797                "@end");
5798 
5799   verifyFormat("@implementation Foo : Bar {\n"
5800                "  int _i;\n"
5801                "}\n"
5802                "+ (id)init {\n}\n"
5803                "@end");
5804 
5805   verifyFormat("@implementation Foo (HackStuff)\n"
5806                "+ (id)init {\n}\n"
5807                "@end");
5808   verifyFormat("@implementation ObjcClass\n"
5809                "- (void)method;\n"
5810                "{}\n"
5811                "@end");
5812 }
5813 
5814 TEST_F(FormatTest, FormatObjCProtocol) {
5815   verifyFormat("@protocol Foo\n"
5816                "@property(weak) id delegate;\n"
5817                "- (NSUInteger)numberOfThings;\n"
5818                "@end");
5819 
5820   verifyFormat("@protocol MyProtocol <NSObject>\n"
5821                "- (NSUInteger)numberOfThings;\n"
5822                "@end");
5823 
5824   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
5825                      "- (NSUInteger)numberOfThings;\n"
5826                      "@end");
5827 
5828   verifyFormat("@protocol Foo;\n"
5829                "@protocol Bar;\n");
5830 
5831   verifyFormat("@protocol Foo\n"
5832                "@end\n"
5833                "@protocol Bar\n"
5834                "@end");
5835 
5836   verifyFormat("@protocol myProtocol\n"
5837                "- (void)mandatoryWithInt:(int)i;\n"
5838                "@optional\n"
5839                "- (void)optional;\n"
5840                "@required\n"
5841                "- (void)required;\n"
5842                "@optional\n"
5843                "@property(assign) int madProp;\n"
5844                "@end\n");
5845 
5846   verifyFormat("@property(nonatomic, assign, readonly)\n"
5847                "    int *looooooooooooooooooooooooooooongNumber;\n"
5848                "@property(nonatomic, assign, readonly)\n"
5849                "    NSString *looooooooooooooooooooooooooooongName;");
5850 
5851   verifyFormat("@implementation PR18406\n"
5852                "}\n"
5853                "@end");
5854 }
5855 
5856 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
5857   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
5858                "                   rect:(NSRect)theRect\n"
5859                "               interval:(float)theInterval {\n"
5860                "}");
5861   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
5862                "          longKeyword:(NSRect)theRect\n"
5863                "    evenLongerKeyword:(float)theInterval\n"
5864                "                error:(NSError **)theError {\n"
5865                "}");
5866 }
5867 
5868 TEST_F(FormatTest, FormatObjCMethodExpr) {
5869   verifyFormat("[foo bar:baz];");
5870   verifyFormat("return [foo bar:baz];");
5871   verifyFormat("f([foo bar:baz]);");
5872   verifyFormat("f(2, [foo bar:baz]);");
5873   verifyFormat("f(2, a ? b : c);");
5874   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
5875 
5876   // Unary operators.
5877   verifyFormat("int a = +[foo bar:baz];");
5878   verifyFormat("int a = -[foo bar:baz];");
5879   verifyFormat("int a = ![foo bar:baz];");
5880   verifyFormat("int a = ~[foo bar:baz];");
5881   verifyFormat("int a = ++[foo bar:baz];");
5882   verifyFormat("int a = --[foo bar:baz];");
5883   verifyFormat("int a = sizeof [foo bar:baz];");
5884   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
5885   verifyFormat("int a = &[foo bar:baz];");
5886   verifyFormat("int a = *[foo bar:baz];");
5887   // FIXME: Make casts work, without breaking f()[4].
5888   //verifyFormat("int a = (int)[foo bar:baz];");
5889   //verifyFormat("return (int)[foo bar:baz];");
5890   //verifyFormat("(void)[foo bar:baz];");
5891   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
5892 
5893   // Binary operators.
5894   verifyFormat("[foo bar:baz], [foo bar:baz];");
5895   verifyFormat("[foo bar:baz] = [foo bar:baz];");
5896   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
5897   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
5898   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
5899   verifyFormat("[foo bar:baz] += [foo bar:baz];");
5900   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
5901   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
5902   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
5903   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
5904   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
5905   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
5906   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
5907   verifyFormat("[foo bar:baz] || [foo bar:baz];");
5908   verifyFormat("[foo bar:baz] && [foo bar:baz];");
5909   verifyFormat("[foo bar:baz] | [foo bar:baz];");
5910   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
5911   verifyFormat("[foo bar:baz] & [foo bar:baz];");
5912   verifyFormat("[foo bar:baz] == [foo bar:baz];");
5913   verifyFormat("[foo bar:baz] != [foo bar:baz];");
5914   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
5915   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
5916   verifyFormat("[foo bar:baz] > [foo bar:baz];");
5917   verifyFormat("[foo bar:baz] < [foo bar:baz];");
5918   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
5919   verifyFormat("[foo bar:baz] << [foo bar:baz];");
5920   verifyFormat("[foo bar:baz] - [foo bar:baz];");
5921   verifyFormat("[foo bar:baz] + [foo bar:baz];");
5922   verifyFormat("[foo bar:baz] * [foo bar:baz];");
5923   verifyFormat("[foo bar:baz] / [foo bar:baz];");
5924   verifyFormat("[foo bar:baz] % [foo bar:baz];");
5925   // Whew!
5926 
5927   verifyFormat("return in[42];");
5928   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
5929                "}");
5930 
5931   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
5932   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
5933   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
5934   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
5935   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
5936   verifyFormat("[button setAction:@selector(zoomOut:)];");
5937   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
5938 
5939   verifyFormat("arr[[self indexForFoo:a]];");
5940   verifyFormat("throw [self errorFor:a];");
5941   verifyFormat("@throw [self errorFor:a];");
5942 
5943   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
5944   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
5945   verifyFormat("4 > 4 ? (id)a : (id)baz;");
5946 
5947   // This tests that the formatter doesn't break after "backing" but before ":",
5948   // which would be at 80 columns.
5949   verifyFormat(
5950       "void f() {\n"
5951       "  if ((self = [super initWithContentRect:contentRect\n"
5952       "                               styleMask:styleMask ?: otherMask\n"
5953       "                                 backing:NSBackingStoreBuffered\n"
5954       "                                   defer:YES]))");
5955 
5956   verifyFormat(
5957       "[foo checkThatBreakingAfterColonWorksOk:\n"
5958       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
5959 
5960   verifyFormat("[myObj short:arg1 // Force line break\n"
5961                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
5962                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
5963                "                error:arg4];");
5964   verifyFormat(
5965       "void f() {\n"
5966       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
5967       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
5968       "                                     pos.width(), pos.height())\n"
5969       "                styleMask:NSBorderlessWindowMask\n"
5970       "                  backing:NSBackingStoreBuffered\n"
5971       "                    defer:NO]);\n"
5972       "}");
5973   verifyFormat(
5974       "void f() {\n"
5975       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
5976       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
5977       "                                 pos.width(), pos.height())\n"
5978       "                syeMask:NSBorderlessWindowMask\n"
5979       "                  bking:NSBackingStoreBuffered\n"
5980       "                    der:NO]);\n"
5981       "}",
5982       getLLVMStyleWithColumns(70));
5983   verifyFormat("{\n"
5984                "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
5985                "      initWithContentRect:NSMakeRect(origin_global.x,\n"
5986                "                                     origin_global.y,\n"
5987                "                                     pos.width(),\n"
5988                "                                     pos.height())\n"
5989                "                styleMask:NSBorderlessWindowMask\n"
5990                "                  backing:NSBackingStoreBuffered\n"
5991                "                    defer:NO]);\n"
5992                "}",
5993                getChromiumStyle(FormatStyle::LK_Cpp));
5994   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
5995                "                             with:contentsNativeView];");
5996 
5997   verifyFormat(
5998       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
5999       "           owner:nillllll];");
6000 
6001   verifyFormat(
6002       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
6003       "        forType:kBookmarkButtonDragType];");
6004 
6005   verifyFormat("[defaultCenter addObserver:self\n"
6006                "                  selector:@selector(willEnterFullscreen)\n"
6007                "                      name:kWillEnterFullscreenNotification\n"
6008                "                    object:nil];");
6009   verifyFormat("[image_rep drawInRect:drawRect\n"
6010                "             fromRect:NSZeroRect\n"
6011                "            operation:NSCompositeCopy\n"
6012                "             fraction:1.0\n"
6013                "       respectFlipped:NO\n"
6014                "                hints:nil];");
6015 
6016   verifyFormat(
6017       "scoped_nsobject<NSTextField> message(\n"
6018       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
6019       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
6020   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
6021                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
6022                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
6023                "          aaaa:bbb];");
6024   verifyFormat("[self param:function( //\n"
6025                "                parameter)]");
6026   verifyFormat(
6027       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
6028       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
6029       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
6030 
6031   // Variadic parameters.
6032   verifyFormat(
6033       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
6034   verifyFormat(
6035       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
6036       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
6037       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
6038   verifyFormat("[self // break\n"
6039                "      a:a\n"
6040                "    aaa:aaa];");
6041   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
6042                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
6043 }
6044 
6045 TEST_F(FormatTest, ObjCAt) {
6046   verifyFormat("@autoreleasepool");
6047   verifyFormat("@catch");
6048   verifyFormat("@class");
6049   verifyFormat("@compatibility_alias");
6050   verifyFormat("@defs");
6051   verifyFormat("@dynamic");
6052   verifyFormat("@encode");
6053   verifyFormat("@end");
6054   verifyFormat("@finally");
6055   verifyFormat("@implementation");
6056   verifyFormat("@import");
6057   verifyFormat("@interface");
6058   verifyFormat("@optional");
6059   verifyFormat("@package");
6060   verifyFormat("@private");
6061   verifyFormat("@property");
6062   verifyFormat("@protected");
6063   verifyFormat("@protocol");
6064   verifyFormat("@public");
6065   verifyFormat("@required");
6066   verifyFormat("@selector");
6067   verifyFormat("@synchronized");
6068   verifyFormat("@synthesize");
6069   verifyFormat("@throw");
6070   verifyFormat("@try");
6071 
6072   EXPECT_EQ("@interface", format("@ interface"));
6073 
6074   // The precise formatting of this doesn't matter, nobody writes code like
6075   // this.
6076   verifyFormat("@ /*foo*/ interface");
6077 }
6078 
6079 TEST_F(FormatTest, ObjCSnippets) {
6080   verifyFormat("@autoreleasepool {\n"
6081                "  foo();\n"
6082                "}");
6083   verifyFormat("@class Foo, Bar;");
6084   verifyFormat("@compatibility_alias AliasName ExistingClass;");
6085   verifyFormat("@dynamic textColor;");
6086   verifyFormat("char *buf1 = @encode(int *);");
6087   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
6088   verifyFormat("char *buf1 = @encode(int **);");
6089   verifyFormat("Protocol *proto = @protocol(p1);");
6090   verifyFormat("SEL s = @selector(foo:);");
6091   verifyFormat("@synchronized(self) {\n"
6092                "  f();\n"
6093                "}");
6094 
6095   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
6096   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
6097 
6098   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
6099   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
6100   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
6101   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
6102                getMozillaStyle());
6103   verifyFormat("@property BOOL editable;", getMozillaStyle());
6104   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
6105                getWebKitStyle());
6106   verifyFormat("@property BOOL editable;", getWebKitStyle());
6107 
6108   verifyFormat("@import foo.bar;\n"
6109                "@import baz;");
6110 }
6111 
6112 TEST_F(FormatTest, ObjCLiterals) {
6113   verifyFormat("@\"String\"");
6114   verifyFormat("@1");
6115   verifyFormat("@+4.8");
6116   verifyFormat("@-4");
6117   verifyFormat("@1LL");
6118   verifyFormat("@.5");
6119   verifyFormat("@'c'");
6120   verifyFormat("@true");
6121 
6122   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
6123   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
6124   verifyFormat("NSNumber *favoriteColor = @(Green);");
6125   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
6126 
6127   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
6128 }
6129 
6130 TEST_F(FormatTest, ObjCDictLiterals) {
6131   verifyFormat("@{");
6132   verifyFormat("@{}");
6133   verifyFormat("@{@\"one\" : @1}");
6134   verifyFormat("return @{@\"one\" : @1;");
6135   verifyFormat("@{@\"one\" : @1}");
6136 
6137   verifyFormat("@{@\"one\" : @{@2 : @1}}");
6138   verifyFormat("@{\n"
6139                "  @\"one\" : @{@2 : @1},\n"
6140                "}");
6141 
6142   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
6143   verifyFormat("[self setDict:@{}");
6144   verifyFormat("[self setDict:@{@1 : @2}");
6145   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
6146   verifyFormat(
6147       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
6148   verifyFormat(
6149       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
6150 
6151   verifyFormat(
6152       "NSDictionary *d = @{\n"
6153       "  @\"nam\" : NSUserNam(),\n"
6154       "  @\"dte\" : [NSDate date],\n"
6155       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
6156       "};");
6157   verifyFormat(
6158       "@{\n"
6159       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
6160       "regularFont,\n"
6161       "};");
6162   verifyGoogleFormat(
6163       "@{\n"
6164       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
6165       "regularFont,\n"
6166       "};");
6167   verifyFormat(
6168       "@{\n"
6169       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
6170       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
6171       "};");
6172 
6173   // We should try to be robust in case someone forgets the "@".
6174   verifyFormat(
6175       "NSDictionary *d = {\n"
6176       "  @\"nam\" : NSUserNam(),\n"
6177       "  @\"dte\" : [NSDate date],\n"
6178       "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
6179       "};");
6180 }
6181 
6182 TEST_F(FormatTest, ObjCArrayLiterals) {
6183   verifyFormat("@[");
6184   verifyFormat("@[]");
6185   verifyFormat(
6186       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
6187   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
6188   verifyFormat("NSArray *array = @[ [foo description] ];");
6189 
6190   verifyFormat(
6191       "NSArray *some_variable = @[\n"
6192       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
6193       "  @\"aaaaaaaaaaaaaaaaa\",\n"
6194       "  @\"aaaaaaaaaaaaaaaaa\",\n"
6195       "  @\"aaaaaaaaaaaaaaaaa\"\n"
6196       "];");
6197   verifyFormat("NSArray *some_variable = @[\n"
6198                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6199                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6200                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6201                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6202                "];");
6203   verifyGoogleFormat("NSArray *some_variable = @[\n"
6204                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
6205                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
6206                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
6207                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
6208                      "];");
6209 
6210   // We should try to be robust in case someone forgets the "@".
6211   verifyFormat("NSArray *some_variable = [\n"
6212                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6213                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6214                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6215                "  @\"aaaaaaaaaaaaaaaaa\",\n"
6216                "];");
6217   verifyFormat(
6218       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
6219       "                                             index:(NSUInteger)index\n"
6220       "                                nonDigitAttributes:\n"
6221       "                                    (NSDictionary *)noDigitAttributes;");
6222   verifyFormat(
6223       "[someFunction someLooooooooooooongParameter:\n"
6224       "                  @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
6225 }
6226 
6227 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
6228   EXPECT_EQ("{\n"
6229             "{\n"
6230             "a;\n"
6231             "b;\n"
6232             "}\n"
6233             "}",
6234             format("{\n"
6235                    "{\n"
6236                    "a;\n"
6237                    "     b;\n"
6238                    "}\n"
6239                    "}",
6240                    13, 2, getLLVMStyle()));
6241   EXPECT_EQ("{\n"
6242             "{\n"
6243             "  a;\n"
6244             "b;\n"
6245             "}\n"
6246             "}",
6247             format("{\n"
6248                    "{\n"
6249                    "     a;\n"
6250                    "b;\n"
6251                    "}\n"
6252                    "}",
6253                    9, 2, getLLVMStyle()));
6254   EXPECT_EQ("{\n"
6255             "{\n"
6256             "public:\n"
6257             "  b;\n"
6258             "}\n"
6259             "}",
6260             format("{\n"
6261                    "{\n"
6262                    "public:\n"
6263                    "     b;\n"
6264                    "}\n"
6265                    "}",
6266                    17, 2, getLLVMStyle()));
6267   EXPECT_EQ("{\n"
6268             "{\n"
6269             "a;\n"
6270             "}\n"
6271             "{\n"
6272             "  b; //\n"
6273             "}\n"
6274             "}",
6275             format("{\n"
6276                    "{\n"
6277                    "a;\n"
6278                    "}\n"
6279                    "{\n"
6280                    "           b; //\n"
6281                    "}\n"
6282                    "}",
6283                    22, 2, getLLVMStyle()));
6284   EXPECT_EQ("  {\n"
6285             "    a; //\n"
6286             "  }",
6287             format("  {\n"
6288                    "a; //\n"
6289                    "  }",
6290                    4, 2, getLLVMStyle()));
6291   EXPECT_EQ("void f() {}\n"
6292             "void g() {}",
6293             format("void f() {}\n"
6294                    "void g() {}",
6295                    13, 0, getLLVMStyle()));
6296   EXPECT_EQ("int a; // comment\n"
6297             "       // line 2\n"
6298             "int b;",
6299             format("int a; // comment\n"
6300                    "       // line 2\n"
6301                    "  int b;",
6302                    35, 0, getLLVMStyle()));
6303   EXPECT_EQ("  int a;\n"
6304             "  void\n"
6305             "  ffffff() {\n"
6306             "  }",
6307             format("  int a;\n"
6308                    "void ffffff() {}",
6309                    11, 0, getLLVMStyleWithColumns(11)));
6310 
6311   EXPECT_EQ(" void f() {\n"
6312             "#define A 1\n"
6313             " }",
6314             format(" void f() {\n"
6315                    "     #define A 1\n" // Format this line.
6316                    " }",
6317                    20, 0, getLLVMStyle()));
6318   EXPECT_EQ(" void f() {\n"
6319             "    int i;\n"
6320             "#define A \\\n"
6321             "    int i;  \\\n"
6322             "   int j;\n"
6323             "    int k;\n"
6324             " }",
6325             format(" void f() {\n"
6326                    "    int i;\n"
6327                    "#define A \\\n"
6328                    "    int i;  \\\n"
6329                    "   int j;\n"
6330                    "      int k;\n" // Format this line.
6331                    " }",
6332                    67, 0, getLLVMStyle()));
6333 }
6334 
6335 TEST_F(FormatTest, BreakStringLiterals) {
6336   EXPECT_EQ("\"some text \"\n"
6337             "\"other\";",
6338             format("\"some text other\";", getLLVMStyleWithColumns(12)));
6339   EXPECT_EQ("\"some text \"\n"
6340             "\"other\";",
6341             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
6342   EXPECT_EQ(
6343       "#define A  \\\n"
6344       "  \"some \"  \\\n"
6345       "  \"text \"  \\\n"
6346       "  \"other\";",
6347       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
6348   EXPECT_EQ(
6349       "#define A  \\\n"
6350       "  \"so \"    \\\n"
6351       "  \"text \"  \\\n"
6352       "  \"other\";",
6353       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
6354 
6355   EXPECT_EQ("\"some text\"",
6356             format("\"some text\"", getLLVMStyleWithColumns(1)));
6357   EXPECT_EQ("\"some text\"",
6358             format("\"some text\"", getLLVMStyleWithColumns(11)));
6359   EXPECT_EQ("\"some \"\n"
6360             "\"text\"",
6361             format("\"some text\"", getLLVMStyleWithColumns(10)));
6362   EXPECT_EQ("\"some \"\n"
6363             "\"text\"",
6364             format("\"some text\"", getLLVMStyleWithColumns(7)));
6365   EXPECT_EQ("\"some\"\n"
6366             "\" tex\"\n"
6367             "\"t\"",
6368             format("\"some text\"", getLLVMStyleWithColumns(6)));
6369   EXPECT_EQ("\"some\"\n"
6370             "\" tex\"\n"
6371             "\" and\"",
6372             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
6373   EXPECT_EQ("\"some\"\n"
6374             "\"/tex\"\n"
6375             "\"/and\"",
6376             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
6377 
6378   EXPECT_EQ("variable =\n"
6379             "    \"long string \"\n"
6380             "    \"literal\";",
6381             format("variable = \"long string literal\";",
6382                    getLLVMStyleWithColumns(20)));
6383 
6384   EXPECT_EQ("variable = f(\n"
6385             "    \"long string \"\n"
6386             "    \"literal\",\n"
6387             "    short,\n"
6388             "    loooooooooooooooooooong);",
6389             format("variable = f(\"long string literal\", short, "
6390                    "loooooooooooooooooooong);",
6391                    getLLVMStyleWithColumns(20)));
6392 
6393   EXPECT_EQ("f(g(\"long string \"\n"
6394             "    \"literal\"),\n"
6395             "  b);",
6396             format("f(g(\"long string literal\"), b);",
6397                    getLLVMStyleWithColumns(20)));
6398   EXPECT_EQ("f(g(\"long string \"\n"
6399             "    \"literal\",\n"
6400             "    a),\n"
6401             "  b);",
6402             format("f(g(\"long string literal\", a), b);",
6403                    getLLVMStyleWithColumns(20)));
6404   EXPECT_EQ(
6405       "f(\"one two\".split(\n"
6406       "    variable));",
6407       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
6408   EXPECT_EQ("f(\"one two three four five six \"\n"
6409             "  \"seven\".split(\n"
6410             "      really_looooong_variable));",
6411             format("f(\"one two three four five six seven\"."
6412                    "split(really_looooong_variable));",
6413                    getLLVMStyleWithColumns(33)));
6414 
6415   EXPECT_EQ("f(\"some \"\n"
6416             "  \"text\",\n"
6417             "  other);",
6418             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
6419 
6420   // Only break as a last resort.
6421   verifyFormat(
6422       "aaaaaaaaaaaaaaaaaaaa(\n"
6423       "    aaaaaaaaaaaaaaaaaaaa,\n"
6424       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
6425 
6426   EXPECT_EQ(
6427       "\"splitmea\"\n"
6428       "\"trandomp\"\n"
6429       "\"oint\"",
6430       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
6431 
6432   EXPECT_EQ(
6433       "\"split/\"\n"
6434       "\"pathat/\"\n"
6435       "\"slashes\"",
6436       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6437 
6438   EXPECT_EQ(
6439       "\"split/\"\n"
6440       "\"pathat/\"\n"
6441       "\"slashes\"",
6442       format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
6443   EXPECT_EQ("\"split at \"\n"
6444             "\"spaces/at/\"\n"
6445             "\"slashes.at.any$\"\n"
6446             "\"non-alphanumeric%\"\n"
6447             "\"1111111111characte\"\n"
6448             "\"rs\"",
6449             format("\"split at "
6450                    "spaces/at/"
6451                    "slashes.at."
6452                    "any$non-"
6453                    "alphanumeric%"
6454                    "1111111111characte"
6455                    "rs\"",
6456                    getLLVMStyleWithColumns(20)));
6457 
6458   // Verify that splitting the strings understands
6459   // Style::AlwaysBreakBeforeMultilineStrings.
6460   EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
6461             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
6462             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
6463             format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
6464                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
6465                    "aaaaaaaaaaaaaaaaaaaaaa\");",
6466                    getGoogleStyle()));
6467   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6468             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
6469             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
6470                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
6471                    "aaaaaaaaaaaaaaaaaaaaaa\";",
6472                    getGoogleStyle()));
6473   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6474             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6475             format("llvm::outs() << "
6476                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
6477                    "aaaaaaaaaaaaaaaaaaa\";"));
6478   EXPECT_EQ("ffff(\n"
6479             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
6480             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6481             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
6482                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
6483                    getGoogleStyle()));
6484 
6485   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
6486   AlignLeft.AlignEscapedNewlinesLeft = true;
6487   EXPECT_EQ(
6488       "#define A \\\n"
6489       "  \"some \" \\\n"
6490       "  \"text \" \\\n"
6491       "  \"other\";",
6492       format("#define A \"some text other\";", AlignLeft));
6493 }
6494 
6495 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
6496   EXPECT_EQ(
6497       "u8\"utf8 string \"\n"
6498       "u8\"literal\";",
6499       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
6500   EXPECT_EQ(
6501       "u\"utf16 string \"\n"
6502       "u\"literal\";",
6503       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
6504   EXPECT_EQ(
6505       "U\"utf32 string \"\n"
6506       "U\"literal\";",
6507       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
6508   EXPECT_EQ("L\"wide string \"\n"
6509             "L\"literal\";",
6510             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
6511   EXPECT_EQ("@\"NSString \"\n"
6512             "@\"literal\";",
6513             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
6514 }
6515 
6516 TEST_F(FormatTest, BreaksRawStringLiterals) {
6517   EXPECT_EQ("R\"x(raw )x\"\n"
6518             "R\"x(literal)x\";",
6519             format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
6520   EXPECT_EQ("uR\"x(raw )x\"\n"
6521             "uR\"x(literal)x\";",
6522             format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6523   EXPECT_EQ("u8R\"x(raw )x\"\n"
6524             "u8R\"x(literal)x\";",
6525             format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
6526   EXPECT_EQ("LR\"x(raw )x\"\n"
6527             "LR\"x(literal)x\";",
6528             format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6529   EXPECT_EQ("UR\"x(raw )x\"\n"
6530             "UR\"x(literal)x\";",
6531             format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
6532 }
6533 
6534 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
6535   FormatStyle Style = getLLVMStyleWithColumns(20);
6536   EXPECT_EQ(
6537       "_T(\"aaaaaaaaaaaaaa\")\n"
6538       "_T(\"aaaaaaaaaaaaaa\")\n"
6539       "_T(\"aaaaaaaaaaaa\")",
6540       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
6541   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
6542             "     _T(\"aaaaaa\"),\n"
6543             "  z);",
6544             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
6545 
6546   // FIXME: Handle embedded spaces in one iteration.
6547   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
6548   //            "_T(\"aaaaaaaaaaaaa\")\n"
6549   //            "_T(\"aaaaaaaaaaaaa\")\n"
6550   //            "_T(\"a\")",
6551   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6552   //                   getLLVMStyleWithColumns(20)));
6553   EXPECT_EQ(
6554       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
6555       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
6556 }
6557 
6558 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
6559   EXPECT_EQ(
6560       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6561       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6562       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
6563       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6564              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
6565              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
6566 }
6567 
6568 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
6569   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
6570             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
6571   EXPECT_EQ("fffffffffff(g(R\"x(\n"
6572             "multiline raw string literal xxxxxxxxxxxxxx\n"
6573             ")x\",\n"
6574             "              a),\n"
6575             "            b);",
6576             format("fffffffffff(g(R\"x(\n"
6577                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6578                    ")x\", a), b);",
6579                    getGoogleStyleWithColumns(20)));
6580   EXPECT_EQ("fffffffffff(\n"
6581             "    g(R\"x(qqq\n"
6582             "multiline raw string literal xxxxxxxxxxxxxx\n"
6583             ")x\",\n"
6584             "      a),\n"
6585             "    b);",
6586             format("fffffffffff(g(R\"x(qqq\n"
6587                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6588                    ")x\", a), b);",
6589                    getGoogleStyleWithColumns(20)));
6590 
6591   EXPECT_EQ("fffffffffff(R\"x(\n"
6592             "multiline raw string literal xxxxxxxxxxxxxx\n"
6593             ")x\");",
6594             format("fffffffffff(R\"x(\n"
6595                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6596                    ")x\");",
6597                    getGoogleStyleWithColumns(20)));
6598   EXPECT_EQ("fffffffffff(R\"x(\n"
6599             "multiline raw string literal xxxxxxxxxxxxxx\n"
6600             ")x\" + bbbbbb);",
6601             format("fffffffffff(R\"x(\n"
6602                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6603                    ")x\" +   bbbbbb);",
6604                    getGoogleStyleWithColumns(20)));
6605   EXPECT_EQ("fffffffffff(\n"
6606             "    R\"x(\n"
6607             "multiline raw string literal xxxxxxxxxxxxxx\n"
6608             ")x\" +\n"
6609             "    bbbbbb);",
6610             format("fffffffffff(\n"
6611                    " R\"x(\n"
6612                    "multiline raw string literal xxxxxxxxxxxxxx\n"
6613                    ")x\" + bbbbbb);",
6614                    getGoogleStyleWithColumns(20)));
6615 }
6616 
6617 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
6618   verifyFormat("string a = \"unterminated;");
6619   EXPECT_EQ("function(\"unterminated,\n"
6620             "         OtherParameter);",
6621             format("function(  \"unterminated,\n"
6622                    "    OtherParameter);"));
6623 }
6624 
6625 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
6626   FormatStyle Style = getLLVMStyle();
6627   Style.Standard = FormatStyle::LS_Cpp03;
6628   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
6629             format("#define x(_a) printf(\"foo\"_a);", Style));
6630 }
6631 
6632 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
6633   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
6634             "             \"ddeeefff\");",
6635             format("someFunction(\"aaabbbcccdddeeefff\");",
6636                    getLLVMStyleWithColumns(25)));
6637   EXPECT_EQ("someFunction1234567890(\n"
6638             "    \"aaabbbcccdddeeefff\");",
6639             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6640                    getLLVMStyleWithColumns(26)));
6641   EXPECT_EQ("someFunction1234567890(\n"
6642             "    \"aaabbbcccdddeeeff\"\n"
6643             "    \"f\");",
6644             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6645                    getLLVMStyleWithColumns(25)));
6646   EXPECT_EQ("someFunction1234567890(\n"
6647             "    \"aaabbbcccdddeeeff\"\n"
6648             "    \"f\");",
6649             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
6650                    getLLVMStyleWithColumns(24)));
6651   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6652             "             \"ddde \"\n"
6653             "             \"efff\");",
6654             format("someFunction(\"aaabbbcc ddde efff\");",
6655                    getLLVMStyleWithColumns(25)));
6656   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
6657             "             \"ddeeefff\");",
6658             format("someFunction(\"aaabbbccc ddeeefff\");",
6659                    getLLVMStyleWithColumns(25)));
6660   EXPECT_EQ("someFunction1234567890(\n"
6661             "    \"aaabb \"\n"
6662             "    \"cccdddeeefff\");",
6663             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
6664                    getLLVMStyleWithColumns(25)));
6665   EXPECT_EQ("#define A          \\\n"
6666             "  string s =       \\\n"
6667             "      \"123456789\"  \\\n"
6668             "      \"0\";         \\\n"
6669             "  int i;",
6670             format("#define A string s = \"1234567890\"; int i;",
6671                    getLLVMStyleWithColumns(20)));
6672   // FIXME: Put additional penalties on breaking at non-whitespace locations.
6673   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
6674             "             \"dddeeeff\"\n"
6675             "             \"f\");",
6676             format("someFunction(\"aaabbbcc dddeeefff\");",
6677                    getLLVMStyleWithColumns(25)));
6678 }
6679 
6680 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
6681   EXPECT_EQ("\"\\a\"",
6682             format("\"\\a\"", getLLVMStyleWithColumns(3)));
6683   EXPECT_EQ("\"\\\"",
6684             format("\"\\\"", getLLVMStyleWithColumns(2)));
6685   EXPECT_EQ("\"test\"\n"
6686             "\"\\n\"",
6687             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
6688   EXPECT_EQ("\"tes\\\\\"\n"
6689             "\"n\"",
6690             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
6691   EXPECT_EQ("\"\\\\\\\\\"\n"
6692             "\"\\n\"",
6693             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
6694   EXPECT_EQ("\"\\uff01\"",
6695             format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
6696   EXPECT_EQ("\"\\uff01\"\n"
6697             "\"test\"",
6698             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
6699   EXPECT_EQ("\"\\Uff01ff02\"",
6700             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
6701   EXPECT_EQ("\"\\x000000000001\"\n"
6702             "\"next\"",
6703             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
6704   EXPECT_EQ("\"\\x000000000001next\"",
6705             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
6706   EXPECT_EQ("\"\\x000000000001\"",
6707             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
6708   EXPECT_EQ("\"test\"\n"
6709             "\"\\000000\"\n"
6710             "\"000001\"",
6711             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
6712   EXPECT_EQ("\"test\\000\"\n"
6713             "\"00000000\"\n"
6714             "\"1\"",
6715             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
6716   // FIXME: We probably don't need to care about escape sequences in raw
6717   // literals.
6718   EXPECT_EQ("R\"(\\x)\"\n"
6719             "R\"(\\x00)\"\n",
6720             format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
6721 }
6722 
6723 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
6724   verifyFormat("void f() {\n"
6725                "  return g() {}\n"
6726                "  void h() {}");
6727   verifyFormat("int a[] = {void forgot_closing_brace() {f();\n"
6728                "g();\n"
6729                "}");
6730 }
6731 
6732 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
6733   verifyFormat(
6734       "void f() {\n"
6735       "  return C{param1, param2}.SomeCall(param1, param2);\n"
6736       "}\n");
6737 }
6738 
6739 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
6740   verifyFormat("class X {\n"
6741                "  void f() {\n"
6742                "  }\n"
6743                "};",
6744                getLLVMStyleWithColumns(12));
6745 }
6746 
6747 TEST_F(FormatTest, ConfigurableIndentWidth) {
6748   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
6749   EightIndent.IndentWidth = 8;
6750   EightIndent.ContinuationIndentWidth = 8;
6751   verifyFormat("void f() {\n"
6752                "        someFunction();\n"
6753                "        if (true) {\n"
6754                "                f();\n"
6755                "        }\n"
6756                "}",
6757                EightIndent);
6758   verifyFormat("class X {\n"
6759                "        void f() {\n"
6760                "        }\n"
6761                "};",
6762                EightIndent);
6763   verifyFormat("int x[] = {\n"
6764                "        call(),\n"
6765                "        call()};",
6766                EightIndent);
6767 }
6768 
6769 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
6770   verifyFormat("void\n"
6771                "f();",
6772                getLLVMStyleWithColumns(8));
6773 }
6774 
6775 TEST_F(FormatTest, ConfigurableUseOfTab) {
6776   FormatStyle Tab = getLLVMStyleWithColumns(42);
6777   Tab.IndentWidth = 8;
6778   Tab.UseTab = FormatStyle::UT_Always;
6779   Tab.AlignEscapedNewlinesLeft = true;
6780 
6781   EXPECT_EQ("if (aaaaaaaa && // q\n"
6782             "    bb)\t\t// w\n"
6783             "\t;",
6784             format("if (aaaaaaaa &&// q\n"
6785                    "bb)// w\n"
6786                    ";",
6787                    Tab));
6788   EXPECT_EQ("if (aaa && bbb) // w\n"
6789             "\t;",
6790             format("if(aaa&&bbb)// w\n"
6791                    ";",
6792                    Tab));
6793 
6794   verifyFormat("class X {\n"
6795                "\tvoid f() {\n"
6796                "\t\tsomeFunction(parameter1,\n"
6797                "\t\t\t     parameter2);\n"
6798                "\t}\n"
6799                "};",
6800                Tab);
6801   verifyFormat("#define A                        \\\n"
6802                "\tvoid f() {               \\\n"
6803                "\t\tsomeFunction(    \\\n"
6804                "\t\t    parameter1,  \\\n"
6805                "\t\t    parameter2); \\\n"
6806                "\t}",
6807                Tab);
6808   EXPECT_EQ("void f() {\n"
6809             "\tf();\n"
6810             "\tg();\n"
6811             "}",
6812             format("void f() {\n"
6813                    "\tf();\n"
6814                    "\tg();\n"
6815                    "}",
6816                    0, 0, Tab));
6817   EXPECT_EQ("void f() {\n"
6818             "\tf();\n"
6819             "\tg();\n"
6820             "}",
6821             format("void f() {\n"
6822                    "\tf();\n"
6823                    "\tg();\n"
6824                    "}",
6825                    16, 0, Tab));
6826   EXPECT_EQ("void f() {\n"
6827             "  \tf();\n"
6828             "\tg();\n"
6829             "}",
6830             format("void f() {\n"
6831                    "  \tf();\n"
6832                    "  \tg();\n"
6833                    "}",
6834                    21, 0, Tab));
6835 
6836   Tab.TabWidth = 4;
6837   Tab.IndentWidth = 8;
6838   verifyFormat("class TabWidth4Indent8 {\n"
6839                "\t\tvoid f() {\n"
6840                "\t\t\t\tsomeFunction(parameter1,\n"
6841                "\t\t\t\t\t\t\t parameter2);\n"
6842                "\t\t}\n"
6843                "};",
6844                Tab);
6845 
6846   Tab.TabWidth = 4;
6847   Tab.IndentWidth = 4;
6848   verifyFormat("class TabWidth4Indent4 {\n"
6849                "\tvoid f() {\n"
6850                "\t\tsomeFunction(parameter1,\n"
6851                "\t\t\t\t\t parameter2);\n"
6852                "\t}\n"
6853                "};",
6854                Tab);
6855 
6856   Tab.TabWidth = 8;
6857   Tab.IndentWidth = 4;
6858   verifyFormat("class TabWidth8Indent4 {\n"
6859                "    void f() {\n"
6860                "\tsomeFunction(parameter1,\n"
6861                "\t\t     parameter2);\n"
6862                "    }\n"
6863                "};",
6864                Tab);
6865 
6866   Tab.TabWidth = 8;
6867   Tab.IndentWidth = 8;
6868   EXPECT_EQ("/*\n"
6869             "\t      a\t\tcomment\n"
6870             "\t      in multiple lines\n"
6871             "       */",
6872             format("   /*\t \t \n"
6873                    " \t \t a\t\tcomment\t \t\n"
6874                    " \t \t in multiple lines\t\n"
6875                    " \t  */",
6876                    Tab));
6877 
6878   Tab.UseTab = FormatStyle::UT_ForIndentation;
6879   verifyFormat("{\n"
6880                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6881                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6882                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6883                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6884                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6885                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
6886                "};",
6887                Tab);
6888   verifyFormat("enum A {\n"
6889                "\ta1, // Force multiple lines\n"
6890                "\ta2,\n"
6891                "\ta3\n"
6892                "};",
6893                Tab);
6894   EXPECT_EQ("if (aaaaaaaa && // q\n"
6895             "    bb)         // w\n"
6896             "\t;",
6897             format("if (aaaaaaaa &&// q\n"
6898                    "bb)// w\n"
6899                    ";",
6900                    Tab));
6901   verifyFormat("class X {\n"
6902                "\tvoid f() {\n"
6903                "\t\tsomeFunction(parameter1,\n"
6904                "\t\t             parameter2);\n"
6905                "\t}\n"
6906                "};",
6907                Tab);
6908   verifyFormat("{\n"
6909                "\tQ({\n"
6910                "\t\t  int a;\n"
6911                "\t\t  someFunction(aaaaaaaaaa,\n"
6912                "\t\t               bbbbbbbbb);\n"
6913                "\t  },\n"
6914                "\t  p);\n"
6915                "}",
6916                Tab);
6917   EXPECT_EQ("{\n"
6918             "\t/* aaaa\n"
6919             "\t   bbbb */\n"
6920             "}",
6921             format("{\n"
6922                    "/* aaaa\n"
6923                    "   bbbb */\n"
6924                    "}",
6925                    Tab));
6926   EXPECT_EQ("{\n"
6927             "\t/*\n"
6928             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6929             "\t  bbbbbbbbbbbbb\n"
6930             "\t*/\n"
6931             "}",
6932             format("{\n"
6933                    "/*\n"
6934                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6935                    "*/\n"
6936                    "}",
6937                    Tab));
6938   EXPECT_EQ("{\n"
6939             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6940             "\t// bbbbbbbbbbbbb\n"
6941             "}",
6942             format("{\n"
6943                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6944                    "}",
6945                    Tab));
6946   EXPECT_EQ("{\n"
6947             "\t/*\n"
6948             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
6949             "\t  bbbbbbbbbbbbb\n"
6950             "\t*/\n"
6951             "}",
6952             format("{\n"
6953                    "\t/*\n"
6954                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
6955                    "\t*/\n"
6956                    "}",
6957                    Tab));
6958   EXPECT_EQ("{\n"
6959             "\t/*\n"
6960             "\n"
6961             "\t*/\n"
6962             "}",
6963             format("{\n"
6964                    "\t/*\n"
6965                    "\n"
6966                    "\t*/\n"
6967                    "}",
6968                    Tab));
6969   EXPECT_EQ("{\n"
6970             "\t/*\n"
6971             " asdf\n"
6972             "\t*/\n"
6973             "}",
6974             format("{\n"
6975                    "\t/*\n"
6976                    " asdf\n"
6977                    "\t*/\n"
6978                    "}",
6979                    Tab));
6980 
6981   Tab.UseTab = FormatStyle::UT_Never;
6982   EXPECT_EQ("/*\n"
6983             "              a\t\tcomment\n"
6984             "              in multiple lines\n"
6985             "       */",
6986             format("   /*\t \t \n"
6987                    " \t \t a\t\tcomment\t \t\n"
6988                    " \t \t in multiple lines\t\n"
6989                    " \t  */",
6990                    Tab));
6991   EXPECT_EQ("/* some\n"
6992             "   comment */",
6993            format(" \t \t /* some\n"
6994                   " \t \t    comment */",
6995                   Tab));
6996   EXPECT_EQ("int a; /* some\n"
6997             "   comment */",
6998            format(" \t \t int a; /* some\n"
6999                   " \t \t    comment */",
7000                   Tab));
7001 
7002   EXPECT_EQ("int a; /* some\n"
7003             "comment */",
7004            format(" \t \t int\ta; /* some\n"
7005                   " \t \t    comment */",
7006                   Tab));
7007   EXPECT_EQ("f(\"\t\t\"); /* some\n"
7008             "    comment */",
7009            format(" \t \t f(\"\t\t\"); /* some\n"
7010                   " \t \t    comment */",
7011                   Tab));
7012   EXPECT_EQ("{\n"
7013             "  /*\n"
7014             "   * Comment\n"
7015             "   */\n"
7016             "  int i;\n"
7017             "}",
7018             format("{\n"
7019                    "\t/*\n"
7020                    "\t * Comment\n"
7021                    "\t */\n"
7022                    "\t int i;\n"
7023                    "}"));
7024 }
7025 
7026 TEST_F(FormatTest, CalculatesOriginalColumn) {
7027   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7028             "q\"; /* some\n"
7029             "       comment */",
7030             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7031                    "q\"; /* some\n"
7032                    "       comment */",
7033                    getLLVMStyle()));
7034   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7035             "/* some\n"
7036             "   comment */",
7037             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
7038                    " /* some\n"
7039                    "    comment */",
7040                    getLLVMStyle()));
7041   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7042             "qqq\n"
7043             "/* some\n"
7044             "   comment */",
7045             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7046                    "qqq\n"
7047                    " /* some\n"
7048                    "    comment */",
7049                    getLLVMStyle()));
7050   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7051             "wwww; /* some\n"
7052             "         comment */",
7053             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
7054                    "wwww; /* some\n"
7055                    "         comment */",
7056                    getLLVMStyle()));
7057 }
7058 
7059 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
7060   FormatStyle NoSpace = getLLVMStyle();
7061   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
7062 
7063   verifyFormat("while(true)\n"
7064                "  continue;", NoSpace);
7065   verifyFormat("for(;;)\n"
7066                "  continue;", NoSpace);
7067   verifyFormat("if(true)\n"
7068                "  f();\n"
7069                "else if(true)\n"
7070                "  f();", NoSpace);
7071   verifyFormat("do {\n"
7072                "  do_something();\n"
7073                "} while(something());", NoSpace);
7074   verifyFormat("switch(x) {\n"
7075                "default:\n"
7076                "  break;\n"
7077                "}", NoSpace);
7078 
7079   FormatStyle Space = getLLVMStyle();
7080   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
7081 
7082   verifyFormat("int f ();", Space);
7083   verifyFormat("void f (int a, T b) {\n"
7084                "  while (true)\n"
7085                "    continue;\n"
7086                "}",
7087                Space);
7088   verifyFormat("if (true)\n"
7089                "  f ();\n"
7090                "else if (true)\n"
7091                "  f ();",
7092                Space);
7093   verifyFormat("do {\n"
7094                "  do_something ();\n"
7095                "} while (something ());",
7096                Space);
7097   verifyFormat("switch (x) {\n"
7098                "default:\n"
7099                "  break;\n"
7100                "}",
7101                Space);
7102   verifyFormat("A::A () : a (1) {}", Space);
7103   verifyFormat("void f () __attribute__ ((asdf));", Space);
7104   verifyFormat("*(&a + 1);\n"
7105                "&((&a)[1]);\n"
7106                "a[(b + c) * d];\n"
7107                "(((a + 1) * 2) + 3) * 4;",
7108                Space);
7109   verifyFormat("#define A(x) x", Space);
7110   verifyFormat("#define A (x) x", Space);
7111   verifyFormat("#if defined(x)\n"
7112                "#endif",
7113                Space);
7114 }
7115 
7116 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
7117   FormatStyle Spaces = getLLVMStyle();
7118 
7119   Spaces.SpacesInParentheses = true;
7120   verifyFormat("call( x, y, z );", Spaces);
7121   verifyFormat("while ( (bool)1 )\n"
7122                "  continue;", Spaces);
7123   verifyFormat("for ( ;; )\n"
7124                "  continue;", Spaces);
7125   verifyFormat("if ( true )\n"
7126                "  f();\n"
7127                "else if ( true )\n"
7128                "  f();", Spaces);
7129   verifyFormat("do {\n"
7130                "  do_something( (int)i );\n"
7131                "} while ( something() );", Spaces);
7132   verifyFormat("switch ( x ) {\n"
7133                "default:\n"
7134                "  break;\n"
7135                "}", Spaces);
7136 
7137   Spaces.SpacesInParentheses = false;
7138   Spaces.SpacesInCStyleCastParentheses = true;
7139   verifyFormat("Type *A = ( Type * )P;", Spaces);
7140   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
7141   verifyFormat("x = ( int32 )y;", Spaces);
7142   verifyFormat("int a = ( int )(2.0f);", Spaces);
7143   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
7144   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
7145   verifyFormat("#define x (( int )-1)", Spaces);
7146 
7147   Spaces.SpacesInParentheses = false;
7148   Spaces.SpaceInEmptyParentheses = true;
7149   verifyFormat("call(x, y, z);", Spaces);
7150   verifyFormat("call( )", Spaces);
7151 
7152   // Run the first set of tests again with
7153   // Spaces.SpacesInParentheses = false,
7154   // Spaces.SpaceInEmptyParentheses = true and
7155   // Spaces.SpacesInCStyleCastParentheses = true
7156   Spaces.SpacesInParentheses = false,
7157   Spaces.SpaceInEmptyParentheses = true;
7158   Spaces.SpacesInCStyleCastParentheses = true;
7159   verifyFormat("call(x, y, z);", Spaces);
7160   verifyFormat("while (( bool )1)\n"
7161                "  continue;", Spaces);
7162   verifyFormat("for (;;)\n"
7163                "  continue;", Spaces);
7164   verifyFormat("if (true)\n"
7165                "  f( );\n"
7166                "else if (true)\n"
7167                "  f( );", Spaces);
7168   verifyFormat("do {\n"
7169                "  do_something(( int )i);\n"
7170                "} while (something( ));", Spaces);
7171   verifyFormat("switch (x) {\n"
7172                "default:\n"
7173                "  break;\n"
7174                "}", Spaces);
7175 }
7176 
7177 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
7178   verifyFormat("int a = 5;");
7179   verifyFormat("a += 42;");
7180   verifyFormat("a or_eq 8;");
7181 
7182   FormatStyle Spaces = getLLVMStyle();
7183   Spaces.SpaceBeforeAssignmentOperators = false;
7184   verifyFormat("int a= 5;", Spaces);
7185   verifyFormat("a+= 42;", Spaces);
7186   verifyFormat("a or_eq 8;", Spaces);
7187 }
7188 
7189 TEST_F(FormatTest, LinuxBraceBreaking) {
7190   FormatStyle BreakBeforeBrace = getLLVMStyle();
7191   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Linux;
7192   verifyFormat("namespace a\n"
7193                "{\n"
7194                "class A\n"
7195                "{\n"
7196                "  void f()\n"
7197                "  {\n"
7198                "    if (true) {\n"
7199                "      a();\n"
7200                "      b();\n"
7201                "    }\n"
7202                "  }\n"
7203                "  void g() { return; }\n"
7204                "}\n"
7205                "}",
7206                BreakBeforeBrace);
7207 }
7208 
7209 TEST_F(FormatTest, StroustrupBraceBreaking) {
7210   FormatStyle BreakBeforeBrace = getLLVMStyle();
7211   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7212   verifyFormat("namespace a {\n"
7213                "class A {\n"
7214                "  void f()\n"
7215                "  {\n"
7216                "    if (true) {\n"
7217                "      a();\n"
7218                "      b();\n"
7219                "    }\n"
7220                "  }\n"
7221                "  void g() { return; }\n"
7222                "}\n"
7223                "}",
7224                BreakBeforeBrace);
7225 }
7226 
7227 TEST_F(FormatTest, AllmanBraceBreaking) {
7228   FormatStyle BreakBeforeBrace = getLLVMStyle();
7229   BreakBeforeBrace.BreakBeforeBraces = FormatStyle::BS_Allman;
7230   verifyFormat("namespace a\n"
7231                "{\n"
7232                "class A\n"
7233                "{\n"
7234                "  void f()\n"
7235                "  {\n"
7236                "    if (true)\n"
7237                "    {\n"
7238                "      a();\n"
7239                "      b();\n"
7240                "    }\n"
7241                "  }\n"
7242                "  void g() { return; }\n"
7243                "}\n"
7244                "}",
7245                BreakBeforeBrace);
7246 
7247   verifyFormat("void f()\n"
7248                "{\n"
7249                "  if (true)\n"
7250                "  {\n"
7251                "    a();\n"
7252                "  }\n"
7253                "  else if (false)\n"
7254                "  {\n"
7255                "    b();\n"
7256                "  }\n"
7257                "  else\n"
7258                "  {\n"
7259                "    c();\n"
7260                "  }\n"
7261                "}\n",
7262                BreakBeforeBrace);
7263 
7264   verifyFormat("void f()\n"
7265                "{\n"
7266                "  for (int i = 0; i < 10; ++i)\n"
7267                "  {\n"
7268                "    a();\n"
7269                "  }\n"
7270                "  while (false)\n"
7271                "  {\n"
7272                "    b();\n"
7273                "  }\n"
7274                "  do\n"
7275                "  {\n"
7276                "    c();\n"
7277                "  } while (false)\n"
7278                "}\n",
7279                BreakBeforeBrace);
7280 
7281   verifyFormat("void f(int a)\n"
7282                "{\n"
7283                "  switch (a)\n"
7284                "  {\n"
7285                "  case 0:\n"
7286                "    break;\n"
7287                "  case 1:\n"
7288                "  {\n"
7289                "    break;\n"
7290                "  }\n"
7291                "  case 2:\n"
7292                "  {\n"
7293                "  }\n"
7294                "  break;\n"
7295                "  default:\n"
7296                "    break;\n"
7297                "  }\n"
7298                "}\n",
7299                BreakBeforeBrace);
7300 
7301   verifyFormat("enum X\n"
7302                "{\n"
7303                "  Y = 0,\n"
7304                "}\n",
7305                BreakBeforeBrace);
7306 
7307   BreakBeforeBrace.ColumnLimit = 19;
7308   verifyFormat("void f() { int i; }", BreakBeforeBrace);
7309   BreakBeforeBrace.ColumnLimit = 18;
7310   verifyFormat("void f()\n"
7311                "{\n"
7312                "  int i;\n"
7313                "}",
7314                BreakBeforeBrace);
7315   BreakBeforeBrace.ColumnLimit = 80;
7316 
7317   FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
7318   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
7319   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
7320   verifyFormat("void f(bool b)\n"
7321                "{\n"
7322                "  if (b)\n"
7323                "  {\n"
7324                "    return;\n"
7325                "  }\n"
7326                "}\n",
7327                BreakBeforeBraceShortIfs);
7328   verifyFormat("void f(bool b)\n"
7329                "{\n"
7330                "  if (b) return;\n"
7331                "}\n",
7332                BreakBeforeBraceShortIfs);
7333   verifyFormat("void f(bool b)\n"
7334                "{\n"
7335                "  while (b)\n"
7336                "  {\n"
7337                "    return;\n"
7338                "  }\n"
7339                "}\n",
7340                BreakBeforeBraceShortIfs);
7341 }
7342 
7343 TEST_F(FormatTest, GNUBraceBreaking) {
7344   FormatStyle GNUBraceStyle = getLLVMStyle();
7345   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
7346   verifyFormat("namespace a\n"
7347                "{\n"
7348                "class A\n"
7349                "{\n"
7350                "  void f()\n"
7351                "  {\n"
7352                "    int a;\n"
7353                "    {\n"
7354                "      int b;\n"
7355                "    }\n"
7356                "    if (true)\n"
7357                "      {\n"
7358                "        a();\n"
7359                "        b();\n"
7360                "      }\n"
7361                "  }\n"
7362                "  void g() { return; }\n"
7363                "}\n"
7364                "}",
7365                GNUBraceStyle);
7366 
7367   verifyFormat("void f()\n"
7368                "{\n"
7369                "  if (true)\n"
7370                "    {\n"
7371                "      a();\n"
7372                "    }\n"
7373                "  else if (false)\n"
7374                "    {\n"
7375                "      b();\n"
7376                "    }\n"
7377                "  else\n"
7378                "    {\n"
7379                "      c();\n"
7380                "    }\n"
7381                "}\n",
7382                GNUBraceStyle);
7383 
7384   verifyFormat("void f()\n"
7385                "{\n"
7386                "  for (int i = 0; i < 10; ++i)\n"
7387                "    {\n"
7388                "      a();\n"
7389                "    }\n"
7390                "  while (false)\n"
7391                "    {\n"
7392                "      b();\n"
7393                "    }\n"
7394                "  do\n"
7395                "    {\n"
7396                "      c();\n"
7397                "    }\n"
7398                "  while (false);\n"
7399                "}\n",
7400                GNUBraceStyle);
7401 
7402   verifyFormat("void f(int a)\n"
7403                "{\n"
7404                "  switch (a)\n"
7405                "    {\n"
7406                "    case 0:\n"
7407                "      break;\n"
7408                "    case 1:\n"
7409                "      {\n"
7410                "        break;\n"
7411                "      }\n"
7412                "    case 2:\n"
7413                "      {\n"
7414                "      }\n"
7415                "      break;\n"
7416                "    default:\n"
7417                "      break;\n"
7418                "    }\n"
7419                "}\n",
7420                GNUBraceStyle);
7421 
7422   verifyFormat("enum X\n"
7423                "{\n"
7424                "  Y = 0,\n"
7425                "}\n",
7426                GNUBraceStyle);
7427 }
7428 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
7429   verifyFormat("void f() {\n"
7430                "  try {\n"
7431                "  }\n"
7432                "  catch (const Exception &e) {\n"
7433                "  }\n"
7434                "}\n",
7435                getLLVMStyle());
7436 }
7437 
7438 TEST_F(FormatTest, UnderstandsPragmas) {
7439   verifyFormat("#pragma omp reduction(| : var)");
7440   verifyFormat("#pragma omp reduction(+ : var)");
7441 
7442   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
7443             "(including parentheses).",
7444             format("#pragma    mark   Any non-hyphenated or hyphenated string "
7445                    "(including parentheses)."));
7446 }
7447 
7448 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
7449   for (size_t i = 1; i < Styles.size(); ++i)                                   \
7450     EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of "                \
7451                                     << Styles.size()                           \
7452                                     << " differs from Style #0"
7453 
7454 TEST_F(FormatTest, GetsPredefinedStyleByName) {
7455   SmallVector<FormatStyle, 3> Styles;
7456   Styles.resize(3);
7457 
7458   Styles[0] = getLLVMStyle();
7459   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
7460   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
7461   EXPECT_ALL_STYLES_EQUAL(Styles);
7462 
7463   Styles[0] = getGoogleStyle();
7464   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
7465   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
7466   EXPECT_ALL_STYLES_EQUAL(Styles);
7467 
7468   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
7469   EXPECT_TRUE(
7470       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
7471   EXPECT_TRUE(
7472       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
7473   EXPECT_ALL_STYLES_EQUAL(Styles);
7474 
7475   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
7476   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
7477   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
7478   EXPECT_ALL_STYLES_EQUAL(Styles);
7479 
7480   Styles[0] = getMozillaStyle();
7481   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
7482   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
7483   EXPECT_ALL_STYLES_EQUAL(Styles);
7484 
7485   Styles[0] = getWebKitStyle();
7486   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
7487   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
7488   EXPECT_ALL_STYLES_EQUAL(Styles);
7489 
7490   Styles[0] = getGNUStyle();
7491   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
7492   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
7493   EXPECT_ALL_STYLES_EQUAL(Styles);
7494 
7495   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
7496 }
7497 
7498 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
7499   SmallVector<FormatStyle, 8> Styles;
7500   Styles.resize(2);
7501 
7502   Styles[0] = getGoogleStyle();
7503   Styles[1] = getLLVMStyle();
7504   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
7505   EXPECT_ALL_STYLES_EQUAL(Styles);
7506 
7507   Styles.resize(5);
7508   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
7509   Styles[1] = getLLVMStyle();
7510   Styles[1].Language = FormatStyle::LK_JavaScript;
7511   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
7512 
7513   Styles[2] = getLLVMStyle();
7514   Styles[2].Language = FormatStyle::LK_JavaScript;
7515   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
7516                                   "BasedOnStyle: Google",
7517                                   &Styles[2]).value());
7518 
7519   Styles[3] = getLLVMStyle();
7520   Styles[3].Language = FormatStyle::LK_JavaScript;
7521   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
7522                                   "Language: JavaScript",
7523                                   &Styles[3]).value());
7524 
7525   Styles[4] = getLLVMStyle();
7526   Styles[4].Language = FormatStyle::LK_JavaScript;
7527   EXPECT_EQ(0, parseConfiguration("---\n"
7528                                   "BasedOnStyle: LLVM\n"
7529                                   "IndentWidth: 123\n"
7530                                   "---\n"
7531                                   "BasedOnStyle: Google\n"
7532                                   "Language: JavaScript",
7533                                   &Styles[4]).value());
7534   EXPECT_ALL_STYLES_EQUAL(Styles);
7535 }
7536 
7537 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
7538   EXPECT_NE(VALUE, Style.FIELD);                                               \
7539   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
7540   EXPECT_EQ(VALUE, Style.FIELD)
7541 
7542 #define CHECK_PARSE_BOOL(FIELD)                                                \
7543   Style.FIELD = false;                                                         \
7544   EXPECT_EQ(0, parseConfiguration(#FIELD ": true", &Style).value());           \
7545   EXPECT_TRUE(Style.FIELD);                                                    \
7546   EXPECT_EQ(0, parseConfiguration(#FIELD ": false", &Style).value());          \
7547   EXPECT_FALSE(Style.FIELD);
7548 
7549 TEST_F(FormatTest, ParsesConfiguration) {
7550   FormatStyle Style = {};
7551   Style.Language = FormatStyle::LK_Cpp;
7552   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
7553   CHECK_PARSE_BOOL(AlignTrailingComments);
7554   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
7555   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
7556   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
7557   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
7558   CHECK_PARSE_BOOL(BinPackParameters);
7559   CHECK_PARSE_BOOL(BreakBeforeBinaryOperators);
7560   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
7561   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
7562   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
7563   CHECK_PARSE_BOOL(DerivePointerBinding);
7564   CHECK_PARSE_BOOL(IndentCaseLabels);
7565   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
7566   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
7567   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
7568   CHECK_PARSE_BOOL(PointerBindsToType);
7569   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
7570   CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
7571   CHECK_PARSE_BOOL(SpacesInParentheses);
7572   CHECK_PARSE_BOOL(SpacesInAngles);
7573   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
7574   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
7575   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
7576   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
7577 
7578   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
7579   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
7580               ConstructorInitializerIndentWidth, 1234u);
7581   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
7582   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
7583   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
7584               PenaltyBreakBeforeFirstCallParameter, 1234u);
7585   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
7586   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
7587               PenaltyReturnTypeOnItsOwnLine, 1234u);
7588   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
7589               SpacesBeforeTrailingComments, 1234u);
7590   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
7591   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
7592 
7593   Style.Standard = FormatStyle::LS_Auto;
7594   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
7595   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
7596   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
7597   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
7598   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
7599 
7600   Style.UseTab = FormatStyle::UT_ForIndentation;
7601   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
7602   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
7603   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
7604   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
7605   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
7606 
7607   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
7608   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
7609               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
7610   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
7611               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
7612   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
7613               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
7614   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
7615               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
7616   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
7617               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
7618 
7619   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
7620   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
7621               FormatStyle::SBPO_Never);
7622   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
7623               FormatStyle::SBPO_Always);
7624   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
7625               FormatStyle::SBPO_ControlStatements);
7626   // For backward compatibility:
7627   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
7628               FormatStyle::SBPO_Never);
7629   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
7630               FormatStyle::SBPO_ControlStatements);
7631 
7632   Style.ColumnLimit = 123;
7633   FormatStyle BaseStyle = getLLVMStyle();
7634   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
7635   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
7636 
7637   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
7638   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
7639               FormatStyle::BS_Attach);
7640   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
7641               FormatStyle::BS_Linux);
7642   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
7643               FormatStyle::BS_Stroustrup);
7644   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
7645               FormatStyle::BS_Allman);
7646   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
7647 
7648   Style.NamespaceIndentation = FormatStyle::NI_All;
7649   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
7650               FormatStyle::NI_None);
7651   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
7652               FormatStyle::NI_Inner);
7653   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
7654               FormatStyle::NI_All);
7655 
7656   Style.ForEachMacros.clear();
7657   std::vector<std::string> BoostForeach;
7658   BoostForeach.push_back("BOOST_FOREACH");
7659   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
7660   std::vector<std::string> BoostAndQForeach;
7661   BoostAndQForeach.push_back("BOOST_FOREACH");
7662   BoostAndQForeach.push_back("Q_FOREACH");
7663   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
7664               BoostAndQForeach);
7665 }
7666 
7667 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
7668   FormatStyle Style = {};
7669   Style.Language = FormatStyle::LK_Cpp;
7670   CHECK_PARSE("Language: Cpp\n"
7671               "IndentWidth: 12",
7672               IndentWidth, 12u);
7673   EXPECT_EQ(llvm::errc::not_supported,
7674             parseConfiguration("Language: JavaScript\n"
7675                                "IndentWidth: 34",
7676                                &Style));
7677   EXPECT_EQ(12u, Style.IndentWidth);
7678   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
7679   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
7680 
7681   Style.Language = FormatStyle::LK_JavaScript;
7682   CHECK_PARSE("Language: JavaScript\n"
7683               "IndentWidth: 12",
7684               IndentWidth, 12u);
7685   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
7686   EXPECT_EQ(llvm::errc::not_supported, parseConfiguration("Language: Cpp\n"
7687                                                           "IndentWidth: 34",
7688                                                           &Style));
7689   EXPECT_EQ(23u, Style.IndentWidth);
7690   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
7691   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
7692 
7693   CHECK_PARSE("BasedOnStyle: LLVM\n"
7694               "IndentWidth: 67",
7695               IndentWidth, 67u);
7696 
7697   CHECK_PARSE("---\n"
7698               "Language: JavaScript\n"
7699               "IndentWidth: 12\n"
7700               "---\n"
7701               "Language: Cpp\n"
7702               "IndentWidth: 34\n"
7703               "...\n",
7704               IndentWidth, 12u);
7705 
7706   Style.Language = FormatStyle::LK_Cpp;
7707   CHECK_PARSE("---\n"
7708               "Language: JavaScript\n"
7709               "IndentWidth: 12\n"
7710               "---\n"
7711               "Language: Cpp\n"
7712               "IndentWidth: 34\n"
7713               "...\n",
7714               IndentWidth, 34u);
7715   CHECK_PARSE("---\n"
7716               "IndentWidth: 78\n"
7717               "---\n"
7718               "Language: JavaScript\n"
7719               "IndentWidth: 56\n"
7720               "...\n",
7721               IndentWidth, 78u);
7722 
7723   Style.ColumnLimit = 123;
7724   Style.IndentWidth = 234;
7725   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
7726   Style.TabWidth = 345;
7727   EXPECT_EQ(llvm::errc::success,
7728             parseConfiguration("---\n"
7729                                "IndentWidth: 456\n"
7730                                "BreakBeforeBraces: Allman\n"
7731                                "---\n"
7732                                "Language: JavaScript\n"
7733                                "IndentWidth: 111\n"
7734                                "TabWidth: 111\n"
7735                                "---\n"
7736                                "Language: Cpp\n"
7737                                "BreakBeforeBraces: Stroustrup\n"
7738                                "TabWidth: 789\n"
7739                                "...\n",
7740                                &Style));
7741   EXPECT_EQ(123u, Style.ColumnLimit);
7742   EXPECT_EQ(456u, Style.IndentWidth);
7743   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
7744   EXPECT_EQ(789u, Style.TabWidth);
7745 
7746 
7747   EXPECT_EQ(llvm::errc::invalid_argument,
7748             parseConfiguration("---\n"
7749                                "Language: JavaScript\n"
7750                                "IndentWidth: 56\n"
7751                                "---\n"
7752                                "IndentWidth: 78\n"
7753                                "...\n",
7754                                &Style));
7755   EXPECT_EQ(llvm::errc::invalid_argument,
7756             parseConfiguration("---\n"
7757                                "Language: JavaScript\n"
7758                                "IndentWidth: 56\n"
7759                                "---\n"
7760                                "Language: JavaScript\n"
7761                                "IndentWidth: 78\n"
7762                                "...\n",
7763                                &Style));
7764 
7765   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
7766 }
7767 
7768 #undef CHECK_PARSE
7769 #undef CHECK_PARSE_BOOL
7770 
7771 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
7772   FormatStyle Style = {};
7773   Style.Language = FormatStyle::LK_JavaScript;
7774   Style.BreakBeforeTernaryOperators = true;
7775   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
7776   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
7777 
7778   Style.BreakBeforeTernaryOperators = true;
7779   EXPECT_EQ(0, parseConfiguration("---\n"
7780               "BasedOnStyle: Google\n"
7781               "---\n"
7782               "Language: JavaScript\n"
7783               "IndentWidth: 76\n"
7784               "...\n", &Style).value());
7785   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
7786   EXPECT_EQ(76u, Style.IndentWidth);
7787   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
7788 }
7789 
7790 TEST_F(FormatTest, ConfigurationRoundTripTest) {
7791   FormatStyle Style = getLLVMStyle();
7792   std::string YAML = configurationAsText(Style);
7793   FormatStyle ParsedStyle = {};
7794   ParsedStyle.Language = FormatStyle::LK_Cpp;
7795   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
7796   EXPECT_EQ(Style, ParsedStyle);
7797 }
7798 
7799 TEST_F(FormatTest, WorksFor8bitEncodings) {
7800   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
7801             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
7802             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
7803             "\"\xef\xee\xf0\xf3...\"",
7804             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
7805                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
7806                    "\xef\xee\xf0\xf3...\"",
7807                    getLLVMStyleWithColumns(12)));
7808 }
7809 
7810 TEST_F(FormatTest, HandlesUTF8BOM) {
7811   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
7812   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
7813             format("\xef\xbb\xbf#include <iostream>"));
7814   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
7815             format("\xef\xbb\xbf\n#include <iostream>"));
7816 }
7817 
7818 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
7819 #if !defined(_MSC_VER)
7820 
7821 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
7822   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
7823                getLLVMStyleWithColumns(35));
7824   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
7825                getLLVMStyleWithColumns(31));
7826   verifyFormat("// Однажды в студёную зимнюю пору...",
7827                getLLVMStyleWithColumns(36));
7828   verifyFormat("// 一 二 三 四 五 六 七 八 九 十",
7829                getLLVMStyleWithColumns(32));
7830   verifyFormat("/* Однажды в студёную зимнюю пору... */",
7831                getLLVMStyleWithColumns(39));
7832   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
7833                getLLVMStyleWithColumns(35));
7834 }
7835 
7836 TEST_F(FormatTest, SplitsUTF8Strings) {
7837   // Non-printable characters' width is currently considered to be the length in
7838   // bytes in UTF8. The characters can be displayed in very different manner
7839   // (zero-width, single width with a substitution glyph, expanded to their code
7840   // (e.g. "<8d>"), so there's no single correct way to handle them.
7841   EXPECT_EQ("\"aaaaÄ\"\n"
7842             "\"\xc2\x8d\";",
7843             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
7844   EXPECT_EQ("\"aaaaaaaÄ\"\n"
7845             "\"\xc2\x8d\";",
7846             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
7847   EXPECT_EQ(
7848       "\"Однажды, в \"\n"
7849       "\"студёную \"\n"
7850       "\"зимнюю \"\n"
7851       "\"пору,\"",
7852       format("\"Однажды, в студёную зимнюю пору,\"",
7853              getLLVMStyleWithColumns(13)));
7854   EXPECT_EQ("\"一 二 三 \"\n"
7855             "\"四 五六 \"\n"
7856             "\"七 八 九 \"\n"
7857             "\"十\"",
7858             format("\"一 二 三 四 五六 七 八 九 十\"",
7859                    getLLVMStyleWithColumns(11)));
7860   EXPECT_EQ("\"一\t二 \"\n"
7861             "\"\t三 \"\n"
7862             "\"四 五\t六 \"\n"
7863             "\"\t七 \"\n"
7864             "\"八九十\tqq\"",
7865             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
7866                    getLLVMStyleWithColumns(11)));
7867 }
7868 
7869 
7870 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
7871   EXPECT_EQ("const char *sssss =\n"
7872             "    \"一二三四五六七八\\\n"
7873             " 九 十\";",
7874             format("const char *sssss = \"一二三四五六七八\\\n"
7875                    " 九 十\";",
7876                    getLLVMStyleWithColumns(30)));
7877 }
7878 
7879 TEST_F(FormatTest, SplitsUTF8LineComments) {
7880   EXPECT_EQ("// aaaaÄ\xc2\x8d",
7881             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
7882   EXPECT_EQ("// Я из лесу\n"
7883             "// вышел; был\n"
7884             "// сильный\n"
7885             "// мороз.",
7886             format("// Я из лесу вышел; был сильный мороз.",
7887                    getLLVMStyleWithColumns(13)));
7888   EXPECT_EQ("// 一二三\n"
7889             "// 四五六七\n"
7890             "// 八  九\n"
7891             "// 十",
7892             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
7893 }
7894 
7895 TEST_F(FormatTest, SplitsUTF8BlockComments) {
7896   EXPECT_EQ("/* Гляжу,\n"
7897             " * поднимается\n"
7898             " * медленно в\n"
7899             " * гору\n"
7900             " * Лошадка,\n"
7901             " * везущая\n"
7902             " * хворосту\n"
7903             " * воз. */",
7904             format("/* Гляжу, поднимается медленно в гору\n"
7905                    " * Лошадка, везущая хворосту воз. */",
7906                    getLLVMStyleWithColumns(13)));
7907   EXPECT_EQ(
7908       "/* 一二三\n"
7909       " * 四五六七\n"
7910       " * 八  九\n"
7911       " * 十  */",
7912       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
7913   EXPECT_EQ("/* �������� ��������\n"
7914             " * ��������\n"
7915             " * ������-�� */",
7916             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
7917 }
7918 
7919 #endif // _MSC_VER
7920 
7921 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
7922   FormatStyle Style = getLLVMStyle();
7923 
7924   Style.ConstructorInitializerIndentWidth = 4;
7925   verifyFormat(
7926       "SomeClass::Constructor()\n"
7927       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7928       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7929       Style);
7930 
7931   Style.ConstructorInitializerIndentWidth = 2;
7932   verifyFormat(
7933       "SomeClass::Constructor()\n"
7934       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7935       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7936       Style);
7937 
7938   Style.ConstructorInitializerIndentWidth = 0;
7939   verifyFormat(
7940       "SomeClass::Constructor()\n"
7941       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
7942       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
7943       Style);
7944 
7945   Style.BreakConstructorInitializersBeforeComma = true;
7946   Style.ConstructorInitializerIndentWidth = 4;
7947   verifyFormat("SomeClass::Constructor()\n"
7948                "    : a(a)\n"
7949                "    , b(b)\n"
7950                "    , c(c) {}",
7951                Style);
7952   verifyFormat("SomeClass::Constructor()\n"
7953                "    : a(a) {}",
7954                Style);
7955 
7956   Style.ColumnLimit = 0;
7957   verifyFormat("SomeClass::Constructor()\n"
7958                "    : a(a) {}",
7959                Style);
7960   verifyFormat("SomeClass::Constructor()\n"
7961                "    : a(a)\n"
7962                "    , b(b)\n"
7963                "    , c(c) {}",
7964                Style);
7965   verifyFormat("SomeClass::Constructor()\n"
7966                "    : a(a) {\n"
7967                "  foo();\n"
7968                "  bar();\n"
7969                "}",
7970                Style);
7971 
7972   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
7973   verifyFormat("SomeClass::Constructor()\n"
7974                "    : a(a)\n"
7975                "    , b(b)\n"
7976                "    , c(c) {\n}",
7977                Style);
7978   verifyFormat("SomeClass::Constructor()\n"
7979                "    : a(a) {\n}",
7980                Style);
7981 
7982   Style.ColumnLimit = 80;
7983   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
7984   Style.ConstructorInitializerIndentWidth = 2;
7985   verifyFormat("SomeClass::Constructor()\n"
7986                "  : a(a)\n"
7987                "  , b(b)\n"
7988                "  , c(c) {}",
7989                Style);
7990 
7991   Style.ConstructorInitializerIndentWidth = 0;
7992   verifyFormat("SomeClass::Constructor()\n"
7993                ": a(a)\n"
7994                ", b(b)\n"
7995                ", c(c) {}",
7996                Style);
7997 
7998   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
7999   Style.ConstructorInitializerIndentWidth = 4;
8000   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
8001   verifyFormat(
8002       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
8003       Style);
8004   verifyFormat(
8005       "SomeClass::Constructor()\n"
8006       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
8007       Style);
8008   Style.ConstructorInitializerIndentWidth = 4;
8009   Style.ColumnLimit = 60;
8010   verifyFormat("SomeClass::Constructor()\n"
8011                "    : aaaaaaaa(aaaaaaaa)\n"
8012                "    , aaaaaaaa(aaaaaaaa)\n"
8013                "    , aaaaaaaa(aaaaaaaa) {}",
8014                Style);
8015 }
8016 
8017 TEST_F(FormatTest, FormatsWithWebKitStyle) {
8018   FormatStyle Style = getWebKitStyle();
8019 
8020   // Don't indent in outer namespaces.
8021   verifyFormat("namespace outer {\n"
8022                "int i;\n"
8023                "namespace inner {\n"
8024                "    int i;\n"
8025                "} // namespace inner\n"
8026                "} // namespace outer\n"
8027                "namespace other_outer {\n"
8028                "int i;\n"
8029                "}",
8030                Style);
8031 
8032   // Don't indent case labels.
8033   verifyFormat("switch (variable) {\n"
8034                "case 1:\n"
8035                "case 2:\n"
8036                "    doSomething();\n"
8037                "    break;\n"
8038                "default:\n"
8039                "    ++variable;\n"
8040                "}",
8041                Style);
8042 
8043   // Wrap before binary operators.
8044   EXPECT_EQ(
8045       "void f()\n"
8046       "{\n"
8047       "    if (aaaaaaaaaaaaaaaa\n"
8048       "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
8049       "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
8050       "        return;\n"
8051       "}",
8052       format(
8053           "void f() {\n"
8054           "if (aaaaaaaaaaaaaaaa\n"
8055           "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
8056           "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
8057           "return;\n"
8058           "}",
8059           Style));
8060 
8061   // Constructor initializers are formatted one per line with the "," on the
8062   // new line.
8063   verifyFormat("Constructor()\n"
8064                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
8065                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
8066                "                               aaaaaaaaaaaaaa)\n"
8067                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
8068                "{\n"
8069                "}",
8070                Style);
8071   verifyFormat("SomeClass::Constructor()\n"
8072                "    : a(a)\n"
8073                "{\n"
8074                "}",
8075                Style);
8076   EXPECT_EQ("SomeClass::Constructor()\n"
8077             "    : a(a)\n"
8078             "{\n"
8079             "}",
8080             format("SomeClass::Constructor():a(a){}", Style));
8081   verifyFormat("SomeClass::Constructor()\n"
8082                "    : a(a)\n"
8083                "    , b(b)\n"
8084                "    , c(c)\n"
8085                "{\n"
8086                "}", Style);
8087   verifyFormat("SomeClass::Constructor()\n"
8088                "    : a(a)\n"
8089                "{\n"
8090                "    foo();\n"
8091                "    bar();\n"
8092                "}",
8093                Style);
8094 
8095   // Access specifiers should be aligned left.
8096   verifyFormat("class C {\n"
8097                "public:\n"
8098                "    int i;\n"
8099                "};",
8100                Style);
8101 
8102   // Do not align comments.
8103   verifyFormat("int a; // Do not\n"
8104                "double b; // align comments.",
8105                Style);
8106 
8107   // Accept input's line breaks.
8108   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
8109             "    || bbbbbbbbbbbbbbb) {\n"
8110             "    i++;\n"
8111             "}",
8112             format("if (aaaaaaaaaaaaaaa\n"
8113                    "|| bbbbbbbbbbbbbbb) { i++; }",
8114                    Style));
8115   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
8116             "    i++;\n"
8117             "}",
8118             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
8119 
8120   // Don't automatically break all macro definitions (llvm.org/PR17842).
8121   verifyFormat("#define aNumber 10", Style);
8122   // However, generally keep the line breaks that the user authored.
8123   EXPECT_EQ("#define aNumber \\\n"
8124             "    10",
8125             format("#define aNumber \\\n"
8126                    " 10",
8127                    Style));
8128 }
8129 
8130 TEST_F(FormatTest, FormatsLambdas) {
8131   verifyFormat("int c = [b]() mutable {\n"
8132                "  return [&b] { return b++; }();\n"
8133                "}();\n");
8134   verifyFormat("int c = [&] {\n"
8135                "  [=] { return b++; }();\n"
8136                "}();\n");
8137   verifyFormat("int c = [&, &a, a] {\n"
8138                "  [=, c, &d] { return b++; }();\n"
8139                "}();\n");
8140   verifyFormat("int c = [&a, &a, a] {\n"
8141                "  [=, a, b, &c] { return b++; }();\n"
8142                "}();\n");
8143   verifyFormat("auto c = {[&a, &a, a] {\n"
8144                "  [=, a, b, &c] { return b++; }();\n"
8145                "}}\n");
8146   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
8147   verifyFormat("void f() {\n"
8148                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
8149                "}\n");
8150   verifyFormat("void f() {\n"
8151                "  other(x.begin(), //\n"
8152                "        x.end(),   //\n"
8153                "        [&](int, int) { return 1; });\n"
8154                "}\n");
8155 
8156   // Lambdas with return types.
8157   verifyFormat("int c = []() -> int { return 2; }();\n");
8158   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
8159   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
8160   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
8161                "                   int j) -> int {\n"
8162                "  return fffffffffffffffffffffffffffffffffffffff(i * j);\n"
8163                "};");
8164 
8165   // Not lambdas.
8166   verifyFormat("constexpr char hello[]{\"hello\"};");
8167   verifyFormat("double &operator[](int i) { return 0; }\n"
8168                "int i;");
8169   verifyFormat("std::unique_ptr<int[]> foo() {}");
8170   verifyFormat("int i = a[a][a]->f();");
8171   verifyFormat("int i = (*b)[a]->f();");
8172 
8173   // Other corner cases.
8174   verifyFormat("void f() {\n"
8175                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
8176                "      );\n"
8177                "}");
8178 
8179   // Lambdas created through weird macros.
8180   verifyFormat("void f() {\n"
8181                "  MACRO((const AA &a) { return 1; });\n"
8182                "}");
8183 }
8184 
8185 TEST_F(FormatTest, FormatsBlocks) {
8186   verifyFormat("int (^Block)(int, int);");
8187   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)");
8188   verifyFormat("void (^block)(int) = ^(id test) { int i; };");
8189   verifyFormat("void (^block)(int) = ^(int test) { int i; };");
8190   verifyFormat("void (^block)(int) = ^id(int test) { int i; };");
8191   verifyFormat("void (^block)(int) = ^int(int test) { int i; };");
8192 
8193   verifyFormat("foo(^{ bar(); });");
8194   verifyFormat("foo(a, ^{ bar(); });");
8195 
8196   // FIXME: Make whitespace formatting consistent. Ask a ObjC dev how
8197   // it would ideally look.
8198   verifyFormat("[operation setCompletionBlock:^{ [self onOperationDone]; }];");
8199   verifyFormat("int i = {[operation setCompletionBlock : ^{ [self "
8200                "onOperationDone]; }]};");
8201   verifyFormat("[operation setCompletionBlock:^(int *i) { f(); }];");
8202   verifyFormat("int a = [operation block:^int(int *i) { return 1; }];");
8203   verifyFormat("[myObject doSomethingWith:arg1\n"
8204                "                      aaa:^int(int *a) { return 1; }\n"
8205                "                      bbb:f(a * b)];");
8206 
8207   verifyFormat("[operation setCompletionBlock:^{\n"
8208                "    [self.delegate newDataAvailable];\n"
8209                "}];",
8210                getLLVMStyleWithColumns(60));
8211   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
8212                "    NSString *path = [self sessionFilePath];\n"
8213                "    if (path) {\n"
8214                "      // ...\n"
8215                "    }\n"
8216                "});");
8217   verifyFormat("[[SessionService sharedService]\n"
8218                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
8219                "        if (window) {\n"
8220                "          [self windowDidLoad:window];\n"
8221                "        } else {\n"
8222                "          [self errorLoadingWindow];\n"
8223                "        }\n"
8224                "    }];");
8225   verifyFormat("void (^largeBlock)(void) = ^{\n"
8226                "    // ...\n"
8227                "};\n",
8228                getLLVMStyleWithColumns(40));
8229   verifyFormat("[[SessionService sharedService]\n"
8230                "    loadWindowWithCompletionBlock: //\n"
8231                "        ^(SessionWindow *window) {\n"
8232                "            if (window) {\n"
8233                "              [self windowDidLoad:window];\n"
8234                "            } else {\n"
8235                "              [self errorLoadingWindow];\n"
8236                "            }\n"
8237                "        }];",
8238                getLLVMStyleWithColumns(60));
8239   verifyFormat("[myObject doSomethingWith:arg1\n"
8240                "    firstBlock:^(Foo *a) {\n"
8241                "        // ...\n"
8242                "        int i;\n"
8243                "    }\n"
8244                "    secondBlock:^(Bar *b) {\n"
8245                "        // ...\n"
8246                "        int i;\n"
8247                "    }\n"
8248                "    thirdBlock:^Foo(Bar *b) {\n"
8249                "        // ...\n"
8250                "        int i;\n"
8251                "    }];");
8252 
8253   verifyFormat("f(^{\n"
8254                "    @autoreleasepool {\n"
8255                "      if (a) {\n"
8256                "        g();\n"
8257                "      }\n"
8258                "    }\n"
8259                "});");
8260 }
8261 
8262 TEST_F(FormatTest, SupportsCRLF) {
8263   EXPECT_EQ("int a;\r\n"
8264             "int b;\r\n"
8265             "int c;\r\n",
8266             format("int a;\r\n"
8267                    "  int b;\r\n"
8268                    "    int c;\r\n",
8269                    getLLVMStyle()));
8270   EXPECT_EQ("int a;\r\n"
8271             "int b;\r\n"
8272             "int c;\r\n",
8273             format("int a;\r\n"
8274                    "  int b;\n"
8275                    "    int c;\r\n",
8276                    getLLVMStyle()));
8277   EXPECT_EQ("int a;\n"
8278             "int b;\n"
8279             "int c;\n",
8280             format("int a;\r\n"
8281                    "  int b;\n"
8282                    "    int c;\n",
8283                    getLLVMStyle()));
8284   EXPECT_EQ("\"aaaaaaa \"\r\n"
8285             "\"bbbbbbb\";\r\n",
8286             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
8287   EXPECT_EQ("#define A \\\r\n"
8288             "  b;      \\\r\n"
8289             "  c;      \\\r\n"
8290             "  d;\r\n",
8291             format("#define A \\\r\n"
8292                    "  b; \\\r\n"
8293                    "  c; d; \r\n",
8294                    getGoogleStyle()));
8295 
8296   EXPECT_EQ("/*\r\n"
8297             "multi line block comments\r\n"
8298             "should not introduce\r\n"
8299             "an extra carriage return\r\n"
8300             "*/\r\n",
8301             format("/*\r\n"
8302                    "multi line block comments\r\n"
8303                    "should not introduce\r\n"
8304                    "an extra carriage return\r\n"
8305                    "*/\r\n"));
8306 }
8307 
8308 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
8309   verifyFormat("MY_CLASS(C) {\n"
8310                "  int i;\n"
8311                "  int j;\n"
8312                "};");
8313 }
8314 
8315 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
8316   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
8317   TwoIndent.ContinuationIndentWidth = 2;
8318 
8319   EXPECT_EQ("int i =\n"
8320             "  longFunction(\n"
8321             "    arg);",
8322             format("int i = longFunction(arg);", TwoIndent));
8323 
8324   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
8325   SixIndent.ContinuationIndentWidth = 6;
8326 
8327   EXPECT_EQ("int i =\n"
8328             "      longFunction(\n"
8329             "            arg);",
8330             format("int i = longFunction(arg);", SixIndent));
8331 }
8332 
8333 TEST_F(FormatTest, SpacesInAngles) {
8334   FormatStyle Spaces = getLLVMStyle();
8335   Spaces.SpacesInAngles = true;
8336 
8337   verifyFormat("static_cast< int >(arg);", Spaces);
8338   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
8339   verifyFormat("f< int, float >();", Spaces);
8340   verifyFormat("template <> g() {}", Spaces);
8341   verifyFormat("template < std::vector< int > > f() {}", Spaces);
8342 
8343   Spaces.Standard = FormatStyle::LS_Cpp03;
8344   Spaces.SpacesInAngles = true;
8345   verifyFormat("A< A< int > >();", Spaces);
8346 
8347   Spaces.SpacesInAngles = false;
8348   verifyFormat("A<A<int> >();", Spaces);
8349 
8350   Spaces.Standard = FormatStyle::LS_Cpp11;
8351   Spaces.SpacesInAngles = true;
8352   verifyFormat("A< A< int > >();", Spaces);
8353 
8354   Spaces.SpacesInAngles = false;
8355   verifyFormat("A<A<int>>();", Spaces);
8356 }
8357 
8358 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
8359   std::string code = "#if A\n"
8360                      "#if B\n"
8361                      "a.\n"
8362                      "#endif\n"
8363                      "    a = 1;\n"
8364                      "#else\n"
8365                      "#endif\n"
8366                      "#if C\n"
8367                      "#else\n"
8368                      "#endif\n";
8369   EXPECT_EQ(code, format(code));
8370 }
8371 
8372 } // end namespace tooling
8373 } // end namespace clang
8374