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