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() { return getGoogleStyle(FormatStyle::LK_Cpp); }
21 
22 class FormatTest : public ::testing::Test {
23 protected:
24   std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
25                      const FormatStyle &Style) {
26     DEBUG(llvm::errs() << "---\n");
27     DEBUG(llvm::errs() << Code << "\n\n");
28     std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
29     tooling::Replacements Replaces = reformat(Style, Code, Ranges);
30     ReplacementCount = Replaces.size();
31     std::string Result = applyAllReplacements(Code, Replaces);
32     EXPECT_NE("", Result);
33     DEBUG(llvm::errs() << "\n" << Result << "\n\n");
34     return Result;
35   }
36 
37   std::string format(llvm::StringRef Code,
38                      const FormatStyle &Style = getLLVMStyle()) {
39     return format(Code, 0, Code.size(), Style);
40   }
41 
42   FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
43     FormatStyle Style = getLLVMStyle();
44     Style.ColumnLimit = ColumnLimit;
45     return Style;
46   }
47 
48   FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) {
49     FormatStyle Style = getGoogleStyle();
50     Style.ColumnLimit = ColumnLimit;
51     return Style;
52   }
53 
54   void verifyFormat(llvm::StringRef Code,
55                     const FormatStyle &Style = getLLVMStyle()) {
56     EXPECT_EQ(Code.str(), format(test::messUp(Code), Style));
57   }
58 
59   void verifyGoogleFormat(llvm::StringRef Code) {
60     verifyFormat(Code, getGoogleStyle());
61   }
62 
63   void verifyIndependentOfContext(llvm::StringRef text) {
64     verifyFormat(text);
65     verifyFormat(llvm::Twine("void f() { " + text + " }").str());
66   }
67 
68   /// \brief Verify that clang-format does not crash on the given input.
69   void verifyNoCrash(llvm::StringRef Code,
70                      const FormatStyle &Style = getLLVMStyle()) {
71     format(Code, Style);
72   }
73 
74   int ReplacementCount;
75 };
76 
77 TEST_F(FormatTest, MessUp) {
78   EXPECT_EQ("1 2 3", test::messUp("1 2 3"));
79   EXPECT_EQ("1 2 3\n", test::messUp("1\n2\n3\n"));
80   EXPECT_EQ("a\n//b\nc", test::messUp("a\n//b\nc"));
81   EXPECT_EQ("a\n#b\nc", test::messUp("a\n#b\nc"));
82   EXPECT_EQ("a\n#b c d\ne", test::messUp("a\n#b\\\nc\\\nd\ne"));
83 }
84 
85 //===----------------------------------------------------------------------===//
86 // Basic function tests.
87 //===----------------------------------------------------------------------===//
88 
89 TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
90   EXPECT_EQ(";", format(";"));
91 }
92 
93 TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
94   EXPECT_EQ("int i;", format("  int i;"));
95   EXPECT_EQ("\nint i;", format(" \n\t \v \f  int i;"));
96   EXPECT_EQ("int i;\nint j;", format("    int i; int j;"));
97   EXPECT_EQ("int i;\nint j;", format("    int i;\n  int j;"));
98 }
99 
100 TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) {
101   EXPECT_EQ("int i;", format("int\ni;"));
102 }
103 
104 TEST_F(FormatTest, FormatsNestedBlockStatements) {
105   EXPECT_EQ("{\n  {\n    {}\n  }\n}", format("{{{}}}"));
106 }
107 
108 TEST_F(FormatTest, FormatsNestedCall) {
109   verifyFormat("Method(f1, f2(f3));");
110   verifyFormat("Method(f1(f2, f3()));");
111   verifyFormat("Method(f1(f2, (f3())));");
112 }
113 
114 TEST_F(FormatTest, NestedNameSpecifiers) {
115   verifyFormat("vector<::Type> v;");
116   verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())");
117   verifyFormat("static constexpr bool Bar = decltype(bar())::value;");
118   verifyFormat("bool a = 2 < ::SomeFunction();");
119 }
120 
121 TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
122   EXPECT_EQ("if (a) {\n"
123             "  f();\n"
124             "}",
125             format("if(a){f();}"));
126   EXPECT_EQ(4, ReplacementCount);
127   EXPECT_EQ("if (a) {\n"
128             "  f();\n"
129             "}",
130             format("if (a) {\n"
131                    "  f();\n"
132                    "}"));
133   EXPECT_EQ(0, ReplacementCount);
134 }
135 
136 TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
137   EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
138   EXPECT_EQ("int a;", format("int a;         "));
139   EXPECT_EQ("int a;\n", format("int a;  \n   \n   \n "));
140   EXPECT_EQ("int a;\nint b;    ",
141             format("int a;  \nint b;    ", 0, 0, getLLVMStyle()));
142 }
143 
144 TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
145   EXPECT_EQ("int b;\nint a;",
146             format("int b;\n   int a;", 7, 0, getLLVMStyle()));
147   EXPECT_EQ("int b;\n   int a;",
148             format("int b;\n   int a;", 6, 0, getLLVMStyle()));
149 
150   EXPECT_EQ("#define A  \\\n"
151             "  int a;   \\\n"
152             "  int b;",
153             format("#define A  \\\n"
154                    "  int a;   \\\n"
155                    "    int b;",
156                    26, 0, getLLVMStyleWithColumns(12)));
157   EXPECT_EQ("#define A  \\\n"
158             "  int a;   \\\n"
159             "  int b;",
160             format("#define A  \\\n"
161                    "  int a;   \\\n"
162                    "  int b;",
163                    25, 0, getLLVMStyleWithColumns(12)));
164 }
165 
166 TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
167   EXPECT_EQ("int  b;\n\nint a;",
168             format("int  b;\n\nint a;", 8, 0, getLLVMStyle()));
169   EXPECT_EQ("int b;\n\nint a;",
170             format("int  b;\n\nint a;", 7, 0, getLLVMStyle()));
171 
172   // This might not strictly be correct, but is likely good in all practical
173   // cases.
174   EXPECT_EQ("int b;\nint a;", format("int  b;int a;", 7, 0, getLLVMStyle()));
175 }
176 
177 TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
178   EXPECT_EQ("int  a;\n\n int b;",
179             format("int  a;\n  \n\n int b;", 8, 0, getLLVMStyle()));
180   EXPECT_EQ("int  a;\n\n int b;",
181             format("int  a;\n  \n\n int b;", 9, 0, getLLVMStyle()));
182 }
183 
184 TEST_F(FormatTest, RemovesEmptyLines) {
185   EXPECT_EQ("class C {\n"
186             "  int i;\n"
187             "};",
188             format("class C {\n"
189                    " int i;\n"
190                    "\n"
191                    "};"));
192 
193   // Don't remove empty lines at the start of namespaces or extern "C" blocks.
194   EXPECT_EQ("namespace N {\n"
195             "\n"
196             "int i;\n"
197             "}",
198             format("namespace N {\n"
199                    "\n"
200                    "int    i;\n"
201                    "}",
202                    getGoogleStyle()));
203   EXPECT_EQ("extern /**/ \"C\" /**/ {\n"
204             "\n"
205             "int i;\n"
206             "}",
207             format("extern /**/ \"C\" /**/ {\n"
208                    "\n"
209                    "int    i;\n"
210                    "}",
211                    getGoogleStyle()));
212 
213   // ...but do keep inlining and removing empty lines for non-block extern "C"
214   // functions.
215   verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle());
216   EXPECT_EQ("extern \"C\" int f() {\n"
217             "  int i = 42;\n"
218             "  return i;\n"
219             "}",
220             format("extern \"C\" int f() {\n"
221                    "\n"
222                    "  int i = 42;\n"
223                    "  return i;\n"
224                    "}",
225                    getGoogleStyle()));
226 
227   // Remove empty lines at the beginning and end of blocks.
228   EXPECT_EQ("void f() {\n"
229             "\n"
230             "  if (a) {\n"
231             "\n"
232             "    f();\n"
233             "  }\n"
234             "}",
235             format("void f() {\n"
236                    "\n"
237                    "  if (a) {\n"
238                    "\n"
239                    "    f();\n"
240                    "\n"
241                    "  }\n"
242                    "\n"
243                    "}",
244                    getLLVMStyle()));
245   EXPECT_EQ("void f() {\n"
246             "  if (a) {\n"
247             "    f();\n"
248             "  }\n"
249             "}",
250             format("void f() {\n"
251                    "\n"
252                    "  if (a) {\n"
253                    "\n"
254                    "    f();\n"
255                    "\n"
256                    "  }\n"
257                    "\n"
258                    "}",
259                    getGoogleStyle()));
260 
261   // Don't remove empty lines in more complex control statements.
262   EXPECT_EQ("void f() {\n"
263             "  if (a) {\n"
264             "    f();\n"
265             "\n"
266             "  } else if (b) {\n"
267             "    f();\n"
268             "  }\n"
269             "}",
270             format("void f() {\n"
271                    "  if (a) {\n"
272                    "    f();\n"
273                    "\n"
274                    "  } else if (b) {\n"
275                    "    f();\n"
276                    "\n"
277                    "  }\n"
278                    "\n"
279                    "}"));
280 
281   // FIXME: This is slightly inconsistent.
282   EXPECT_EQ("namespace {\n"
283             "int i;\n"
284             "}",
285             format("namespace {\n"
286                    "int i;\n"
287                    "\n"
288                    "}"));
289   EXPECT_EQ("namespace {\n"
290             "int i;\n"
291             "\n"
292             "} // namespace",
293             format("namespace {\n"
294                    "int i;\n"
295                    "\n"
296                    "}  // namespace"));
297 }
298 
299 TEST_F(FormatTest, ReformatsMovedLines) {
300   EXPECT_EQ(
301       "template <typename T> T *getFETokenInfo() const {\n"
302       "  return static_cast<T *>(FETokenInfo);\n"
303       "}\n"
304       "  int a; // <- Should not be formatted",
305       format(
306           "template<typename T>\n"
307           "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
308           "  int a; // <- Should not be formatted",
309           9, 5, getLLVMStyle()));
310 }
311 
312 TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
313   verifyFormat("x = (a) and (b);");
314   verifyFormat("x = (a) or (b);");
315   verifyFormat("x = (a) bitand (b);");
316   verifyFormat("x = (a) bitor (b);");
317   verifyFormat("x = (a) not_eq (b);");
318   verifyFormat("x = (a) and_eq (b);");
319   verifyFormat("x = (a) or_eq (b);");
320   verifyFormat("x = (a) xor (b);");
321 }
322 
323 //===----------------------------------------------------------------------===//
324 // Tests for control statements.
325 //===----------------------------------------------------------------------===//
326 
327 TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
328   verifyFormat("if (true)\n  f();\ng();");
329   verifyFormat("if (a)\n  if (b)\n    if (c)\n      g();\nh();");
330   verifyFormat("if (a)\n  if (b) {\n    f();\n  }\ng();");
331 
332   FormatStyle AllowsMergedIf = getLLVMStyle();
333   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
334   verifyFormat("if (a)\n"
335                "  // comment\n"
336                "  f();",
337                AllowsMergedIf);
338   verifyFormat("if (a)\n"
339                "  ;",
340                AllowsMergedIf);
341   verifyFormat("if (a)\n"
342                "  if (b) return;",
343                AllowsMergedIf);
344 
345   verifyFormat("if (a) // Can't merge this\n"
346                "  f();\n",
347                AllowsMergedIf);
348   verifyFormat("if (a) /* still don't merge */\n"
349                "  f();",
350                AllowsMergedIf);
351   verifyFormat("if (a) { // Never merge this\n"
352                "  f();\n"
353                "}",
354                AllowsMergedIf);
355   verifyFormat("if (a) {/* Never merge this */\n"
356                "  f();\n"
357                "}",
358                AllowsMergedIf);
359 
360   EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
361   EXPECT_EQ("if (a) return; // comment",
362             format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
363 
364   AllowsMergedIf.ColumnLimit = 14;
365   verifyFormat("if (a) return;", AllowsMergedIf);
366   verifyFormat("if (aaaaaaaaa)\n"
367                "  return;",
368                AllowsMergedIf);
369 
370   AllowsMergedIf.ColumnLimit = 13;
371   verifyFormat("if (a)\n  return;", AllowsMergedIf);
372 }
373 
374 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
375   FormatStyle AllowsMergedLoops = getLLVMStyle();
376   AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
377   verifyFormat("while (true) continue;", AllowsMergedLoops);
378   verifyFormat("for (;;) continue;", AllowsMergedLoops);
379   verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
380   verifyFormat("while (true)\n"
381                "  ;",
382                AllowsMergedLoops);
383   verifyFormat("for (;;)\n"
384                "  ;",
385                AllowsMergedLoops);
386   verifyFormat("for (;;)\n"
387                "  for (;;) continue;",
388                AllowsMergedLoops);
389   verifyFormat("for (;;) // Can't merge this\n"
390                "  continue;",
391                AllowsMergedLoops);
392   verifyFormat("for (;;) /* still don't merge */\n"
393                "  continue;",
394                AllowsMergedLoops);
395 }
396 
397 TEST_F(FormatTest, FormatShortBracedStatements) {
398   FormatStyle AllowSimpleBracedStatements = getLLVMStyle();
399   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine = true;
400 
401   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = true;
402   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
403 
404   verifyFormat("if (true) {}", AllowSimpleBracedStatements);
405   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
406   verifyFormat("for (;;) {}", AllowSimpleBracedStatements);
407   verifyFormat("if (true) { f(); }", AllowSimpleBracedStatements);
408   verifyFormat("while (true) { f(); }", AllowSimpleBracedStatements);
409   verifyFormat("for (;;) { f(); }", AllowSimpleBracedStatements);
410   verifyFormat("if (true) { //\n"
411                "  f();\n"
412                "}",
413                AllowSimpleBracedStatements);
414   verifyFormat("if (true) {\n"
415                "  f();\n"
416                "  f();\n"
417                "}",
418                AllowSimpleBracedStatements);
419   verifyFormat("if (true) {\n"
420                "  f();\n"
421                "} else {\n"
422                "  f();\n"
423                "}",
424                AllowSimpleBracedStatements);
425 
426   verifyFormat("template <int> struct A2 {\n"
427                "  struct B {};\n"
428                "};",
429                AllowSimpleBracedStatements);
430 
431   AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine = false;
432   verifyFormat("if (true) {\n"
433                "  f();\n"
434                "}",
435                AllowSimpleBracedStatements);
436   verifyFormat("if (true) {\n"
437                "  f();\n"
438                "} else {\n"
439                "  f();\n"
440                "}",
441                AllowSimpleBracedStatements);
442 
443   AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
444   verifyFormat("while (true) {\n"
445                "  f();\n"
446                "}",
447                AllowSimpleBracedStatements);
448   verifyFormat("for (;;) {\n"
449                "  f();\n"
450                "}",
451                AllowSimpleBracedStatements);
452 }
453 
454 TEST_F(FormatTest, ParseIfElse) {
455   verifyFormat("if (true)\n"
456                "  if (true)\n"
457                "    if (true)\n"
458                "      f();\n"
459                "    else\n"
460                "      g();\n"
461                "  else\n"
462                "    h();\n"
463                "else\n"
464                "  i();");
465   verifyFormat("if (true)\n"
466                "  if (true)\n"
467                "    if (true) {\n"
468                "      if (true)\n"
469                "        f();\n"
470                "    } else {\n"
471                "      g();\n"
472                "    }\n"
473                "  else\n"
474                "    h();\n"
475                "else {\n"
476                "  i();\n"
477                "}");
478   verifyFormat("void f() {\n"
479                "  if (a) {\n"
480                "  } else {\n"
481                "  }\n"
482                "}");
483 }
484 
485 TEST_F(FormatTest, ElseIf) {
486   verifyFormat("if (a) {\n} else if (b) {\n}");
487   verifyFormat("if (a)\n"
488                "  f();\n"
489                "else if (b)\n"
490                "  g();\n"
491                "else\n"
492                "  h();");
493   verifyFormat("if (a) {\n"
494                "  f();\n"
495                "}\n"
496                "// or else ..\n"
497                "else {\n"
498                "  g()\n"
499                "}");
500 
501   verifyFormat("if (a) {\n"
502                "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
503                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
504                "}");
505   verifyFormat("if (a) {\n"
506                "} else if (\n"
507                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
508                "}",
509                getLLVMStyleWithColumns(62));
510 }
511 
512 TEST_F(FormatTest, FormatsForLoop) {
513   verifyFormat(
514       "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n"
515       "     ++VeryVeryLongLoopVariable)\n"
516       "  ;");
517   verifyFormat("for (;;)\n"
518                "  f();");
519   verifyFormat("for (;;) {\n}");
520   verifyFormat("for (;;) {\n"
521                "  f();\n"
522                "}");
523   verifyFormat("for (int i = 0; (i < 10); ++i) {\n}");
524 
525   verifyFormat(
526       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
527       "                                          E = UnwrappedLines.end();\n"
528       "     I != E; ++I) {\n}");
529 
530   verifyFormat(
531       "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n"
532       "     ++IIIII) {\n}");
533   verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaa =\n"
534                "         aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n"
535                "     aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}");
536   verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n"
537                "         I = FD->getDeclsInPrototypeScope().begin(),\n"
538                "         E = FD->getDeclsInPrototypeScope().end();\n"
539                "     I != E; ++I) {\n}");
540 
541   verifyFormat(
542       "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
543       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n"
544       "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
545       "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
546       "     ++aaaaaaaaaaa) {\n}");
547   verifyFormat("for (int i = 0; i < aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
548                "                bbbbbbbbbbbbbbbbbbbb < ccccccccccccccc;\n"
549                "     ++i) {\n}");
550   verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n"
551                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
552                "}");
553   verifyFormat("for (some_namespace::SomeIterator iter( // force break\n"
554                "         aaaaaaaaaa);\n"
555                "     iter; ++iter) {\n"
556                "}");
557 
558   FormatStyle NoBinPacking = getLLVMStyle();
559   NoBinPacking.BinPackParameters = false;
560   verifyFormat("for (int aaaaaaaaaaa = 1;\n"
561                "     aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n"
562                "                                           aaaaaaaaaaaaaaaa,\n"
563                "                                           aaaaaaaaaaaaaaaa,\n"
564                "                                           aaaaaaaaaaaaaaaa);\n"
565                "     aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n"
566                "}",
567                NoBinPacking);
568   verifyFormat(
569       "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n"
570       "                                          E = UnwrappedLines.end();\n"
571       "     I != E;\n"
572       "     ++I) {\n}",
573       NoBinPacking);
574 }
575 
576 TEST_F(FormatTest, RangeBasedForLoops) {
577   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
578                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
579   verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n"
580                "     aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}");
581   verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n"
582                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
583   verifyFormat("for (aaaaaaaaa aaaaaaaaaaaaaaaaaaaaa :\n"
584                "     aaaaaaaaaaaa.aaaaaaaaaaaa().aaaaaaaaa().a()) {\n}");
585 }
586 
587 TEST_F(FormatTest, ForEachLoops) {
588   verifyFormat("void f() {\n"
589                "  foreach (Item *item, itemlist) {}\n"
590                "  Q_FOREACH (Item *item, itemlist) {}\n"
591                "  BOOST_FOREACH (Item *item, itemlist) {}\n"
592                "  UNKNOWN_FORACH(Item * item, itemlist) {}\n"
593                "}");
594 }
595 
596 TEST_F(FormatTest, FormatsWhileLoop) {
597   verifyFormat("while (true) {\n}");
598   verifyFormat("while (true)\n"
599                "  f();");
600   verifyFormat("while () {\n}");
601   verifyFormat("while () {\n"
602                "  f();\n"
603                "}");
604 }
605 
606 TEST_F(FormatTest, FormatsDoWhile) {
607   verifyFormat("do {\n"
608                "  do_something();\n"
609                "} while (something());");
610   verifyFormat("do\n"
611                "  do_something();\n"
612                "while (something());");
613 }
614 
615 TEST_F(FormatTest, FormatsSwitchStatement) {
616   verifyFormat("switch (x) {\n"
617                "case 1:\n"
618                "  f();\n"
619                "  break;\n"
620                "case kFoo:\n"
621                "case ns::kBar:\n"
622                "case kBaz:\n"
623                "  break;\n"
624                "default:\n"
625                "  g();\n"
626                "  break;\n"
627                "}");
628   verifyFormat("switch (x) {\n"
629                "case 1: {\n"
630                "  f();\n"
631                "  break;\n"
632                "}\n"
633                "case 2: {\n"
634                "  break;\n"
635                "}\n"
636                "}");
637   verifyFormat("switch (x) {\n"
638                "case 1: {\n"
639                "  f();\n"
640                "  {\n"
641                "    g();\n"
642                "    h();\n"
643                "  }\n"
644                "  break;\n"
645                "}\n"
646                "}");
647   verifyFormat("switch (x) {\n"
648                "case 1: {\n"
649                "  f();\n"
650                "  if (foo) {\n"
651                "    g();\n"
652                "    h();\n"
653                "  }\n"
654                "  break;\n"
655                "}\n"
656                "}");
657   verifyFormat("switch (x) {\n"
658                "case 1: {\n"
659                "  f();\n"
660                "  g();\n"
661                "} break;\n"
662                "}");
663   verifyFormat("switch (test)\n"
664                "  ;");
665   verifyFormat("switch (x) {\n"
666                "default: {\n"
667                "  // Do nothing.\n"
668                "}\n"
669                "}");
670   verifyFormat("switch (x) {\n"
671                "// comment\n"
672                "// if 1, do f()\n"
673                "case 1:\n"
674                "  f();\n"
675                "}");
676   verifyFormat("switch (x) {\n"
677                "case 1:\n"
678                "  // Do amazing stuff\n"
679                "  {\n"
680                "    f();\n"
681                "    g();\n"
682                "  }\n"
683                "  break;\n"
684                "}");
685   verifyFormat("#define A          \\\n"
686                "  switch (x) {     \\\n"
687                "  case a:          \\\n"
688                "    foo = b;       \\\n"
689                "  }",
690                getLLVMStyleWithColumns(20));
691   verifyFormat("#define OPERATION_CASE(name)           \\\n"
692                "  case OP_name:                        \\\n"
693                "    return operations::Operation##name\n",
694                getLLVMStyleWithColumns(40));
695 
696   verifyGoogleFormat("switch (x) {\n"
697                      "  case 1:\n"
698                      "    f();\n"
699                      "    break;\n"
700                      "  case kFoo:\n"
701                      "  case ns::kBar:\n"
702                      "  case kBaz:\n"
703                      "    break;\n"
704                      "  default:\n"
705                      "    g();\n"
706                      "    break;\n"
707                      "}");
708   verifyGoogleFormat("switch (x) {\n"
709                      "  case 1: {\n"
710                      "    f();\n"
711                      "    break;\n"
712                      "  }\n"
713                      "}");
714   verifyGoogleFormat("switch (test)\n"
715                      "  ;");
716 
717   verifyGoogleFormat("#define OPERATION_CASE(name) \\\n"
718                      "  case OP_name:              \\\n"
719                      "    return operations::Operation##name\n");
720   verifyGoogleFormat("Operation codeToOperation(OperationCode OpCode) {\n"
721                      "  // Get the correction operation class.\n"
722                      "  switch (OpCode) {\n"
723                      "    CASE(Add);\n"
724                      "    CASE(Subtract);\n"
725                      "    default:\n"
726                      "      return operations::Unknown;\n"
727                      "  }\n"
728                      "#undef OPERATION_CASE\n"
729                      "}");
730   verifyFormat("DEBUG({\n"
731                "  switch (x) {\n"
732                "  case A:\n"
733                "    f();\n"
734                "    break;\n"
735                "  // On B:\n"
736                "  case B:\n"
737                "    g();\n"
738                "    break;\n"
739                "  }\n"
740                "});");
741   verifyFormat("switch (a) {\n"
742                "case (b):\n"
743                "  return;\n"
744                "}");
745 
746   verifyFormat("switch (a) {\n"
747                "case some_namespace::\n"
748                "    some_constant:\n"
749                "  return;\n"
750                "}",
751                getLLVMStyleWithColumns(34));
752 }
753 
754 TEST_F(FormatTest, CaseRanges) {
755   verifyFormat("switch (x) {\n"
756                "case 'A' ... 'Z':\n"
757                "case 1 ... 5:\n"
758                "  break;\n"
759                "}");
760 }
761 
762 TEST_F(FormatTest, ShortCaseLabels) {
763   FormatStyle Style = getLLVMStyle();
764   Style.AllowShortCaseLabelsOnASingleLine = true;
765   verifyFormat("switch (a) {\n"
766                "case 1: x = 1; break;\n"
767                "case 2: return;\n"
768                "case 3:\n"
769                "case 4:\n"
770                "case 5: return;\n"
771                "case 6: // comment\n"
772                "  return;\n"
773                "case 7:\n"
774                "  // comment\n"
775                "  return;\n"
776                "default: y = 1; break;\n"
777                "}",
778                Style);
779   verifyFormat("switch (a) {\n"
780                "#if FOO\n"
781                "case 0: return 0;\n"
782                "#endif\n"
783                "}",
784                Style);
785   verifyFormat("switch (a) {\n"
786                "case 1: {\n"
787                "}\n"
788                "case 2: {\n"
789                "  return;\n"
790                "}\n"
791                "case 3: {\n"
792                "  x = 1;\n"
793                "  return;\n"
794                "}\n"
795                "case 4:\n"
796                "  if (x)\n"
797                "    return;\n"
798                "}",
799                Style);
800   Style.ColumnLimit = 21;
801   verifyFormat("switch (a) {\n"
802                "case 1: x = 1; break;\n"
803                "case 2: return;\n"
804                "case 3:\n"
805                "case 4:\n"
806                "case 5: return;\n"
807                "default:\n"
808                "  y = 1;\n"
809                "  break;\n"
810                "}",
811                Style);
812 }
813 
814 TEST_F(FormatTest, FormatsLabels) {
815   verifyFormat("void f() {\n"
816                "  some_code();\n"
817                "test_label:\n"
818                "  some_other_code();\n"
819                "  {\n"
820                "    some_more_code();\n"
821                "  another_label:\n"
822                "    some_more_code();\n"
823                "  }\n"
824                "}");
825   verifyFormat("{\n"
826                "  some_code();\n"
827                "test_label:\n"
828                "  some_other_code();\n"
829                "}");
830 }
831 
832 //===----------------------------------------------------------------------===//
833 // Tests for comments.
834 //===----------------------------------------------------------------------===//
835 
836 TEST_F(FormatTest, UnderstandsSingleLineComments) {
837   verifyFormat("//* */");
838   verifyFormat("// line 1\n"
839                "// line 2\n"
840                "void f() {}\n");
841 
842   verifyFormat("void f() {\n"
843                "  // Doesn't do anything\n"
844                "}");
845   verifyFormat("SomeObject\n"
846                "    // Calling someFunction on SomeObject\n"
847                "    .someFunction();");
848   verifyFormat("auto result = SomeObject\n"
849                "                  // Calling someFunction on SomeObject\n"
850                "                  .someFunction();");
851   verifyFormat("void f(int i,  // some comment (probably for i)\n"
852                "       int j,  // some comment (probably for j)\n"
853                "       int k); // some comment (probably for k)");
854   verifyFormat("void f(int i,\n"
855                "       // some comment (probably for j)\n"
856                "       int j,\n"
857                "       // some comment (probably for k)\n"
858                "       int k);");
859 
860   verifyFormat("int i    // This is a fancy variable\n"
861                "    = 5; // with nicely aligned comment.");
862 
863   verifyFormat("// Leading comment.\n"
864                "int a; // Trailing comment.");
865   verifyFormat("int a; // Trailing comment\n"
866                "       // on 2\n"
867                "       // or 3 lines.\n"
868                "int b;");
869   verifyFormat("int a; // Trailing comment\n"
870                "\n"
871                "// Leading comment.\n"
872                "int b;");
873   verifyFormat("int a;    // Comment.\n"
874                "          // More details.\n"
875                "int bbbb; // Another comment.");
876   verifyFormat(
877       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
878       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   // comment\n"
879       "int cccccccccccccccccccccccccccccc;       // comment\n"
880       "int ddd;                     // looooooooooooooooooooooooong comment\n"
881       "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n"
882       "int bbbbbbbbbbbbbbbbbbbbb;   // comment\n"
883       "int ccccccccccccccccccc;     // comment");
884 
885   verifyFormat("#include \"a\"     // comment\n"
886                "#include \"a/b/c\" // comment");
887   verifyFormat("#include <a>     // comment\n"
888                "#include <a/b/c> // comment");
889   EXPECT_EQ("#include \"a\"     // comment\n"
890             "#include \"a/b/c\" // comment",
891             format("#include \\\n"
892                    "  \"a\" // comment\n"
893                    "#include \"a/b/c\" // comment"));
894 
895   verifyFormat("enum E {\n"
896                "  // comment\n"
897                "  VAL_A, // comment\n"
898                "  VAL_B\n"
899                "};");
900 
901   verifyFormat(
902       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
903       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment");
904   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
905                "    // Comment inside a statement.\n"
906                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
907   verifyFormat("SomeFunction(a,\n"
908                "             // comment\n"
909                "             b + x);");
910   verifyFormat("SomeFunction(a, a,\n"
911                "             // comment\n"
912                "             b + x);");
913   verifyFormat(
914       "bool aaaaaaaaaaaaa = // comment\n"
915       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
916       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
917 
918   verifyFormat("int aaaa; // aaaaa\n"
919                "int aa;   // aaaaaaa",
920                getLLVMStyleWithColumns(20));
921 
922   EXPECT_EQ("void f() { // This does something ..\n"
923             "}\n"
924             "int a; // This is unrelated",
925             format("void f()    {     // This does something ..\n"
926                    "  }\n"
927                    "int   a;     // This is unrelated"));
928   EXPECT_EQ("class C {\n"
929             "  void f() { // This does something ..\n"
930             "  }          // awesome..\n"
931             "\n"
932             "  int a; // This is unrelated\n"
933             "};",
934             format("class C{void f()    { // This does something ..\n"
935                    "      } // awesome..\n"
936                    " \n"
937                    "int a;    // This is unrelated\n"
938                    "};"));
939 
940   EXPECT_EQ("int i; // single line trailing comment",
941             format("int i;\\\n// single line trailing comment"));
942 
943   verifyGoogleFormat("int a;  // Trailing comment.");
944 
945   verifyFormat("someFunction(anotherFunction( // Force break.\n"
946                "    parameter));");
947 
948   verifyGoogleFormat("#endif  // HEADER_GUARD");
949 
950   verifyFormat("const char *test[] = {\n"
951                "    // A\n"
952                "    \"aaaa\",\n"
953                "    // B\n"
954                "    \"aaaaa\"};");
955   verifyGoogleFormat(
956       "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
957       "    aaaaaaaaaaaaaaaaaaaaaa);  // 81_cols_with_this_comment");
958   EXPECT_EQ("D(a, {\n"
959             "  // test\n"
960             "  int a;\n"
961             "});",
962             format("D(a, {\n"
963                    "// test\n"
964                    "int a;\n"
965                    "});"));
966 
967   EXPECT_EQ("lineWith(); // comment\n"
968             "// at start\n"
969             "otherLine();",
970             format("lineWith();   // comment\n"
971                    "// at start\n"
972                    "otherLine();"));
973   EXPECT_EQ("lineWith(); // comment\n"
974             "            // at start\n"
975             "otherLine();",
976             format("lineWith();   // comment\n"
977                    " // at start\n"
978                    "otherLine();"));
979 
980   EXPECT_EQ("lineWith(); // comment\n"
981             "// at start\n"
982             "otherLine(); // comment",
983             format("lineWith();   // comment\n"
984                    "// at start\n"
985                    "otherLine();   // comment"));
986   EXPECT_EQ("lineWith();\n"
987             "// at start\n"
988             "otherLine(); // comment",
989             format("lineWith();\n"
990                    " // at start\n"
991                    "otherLine();   // comment"));
992   EXPECT_EQ("// first\n"
993             "// at start\n"
994             "otherLine(); // comment",
995             format("// first\n"
996                    " // at start\n"
997                    "otherLine();   // comment"));
998   EXPECT_EQ("f();\n"
999             "// first\n"
1000             "// at start\n"
1001             "otherLine(); // comment",
1002             format("f();\n"
1003                    "// first\n"
1004                    " // at start\n"
1005                    "otherLine();   // comment"));
1006   verifyFormat("f(); // comment\n"
1007                "// first\n"
1008                "// at start\n"
1009                "otherLine();");
1010   EXPECT_EQ("f(); // comment\n"
1011             "// first\n"
1012             "// at start\n"
1013             "otherLine();",
1014             format("f();   // comment\n"
1015                    "// first\n"
1016                    " // at start\n"
1017                    "otherLine();"));
1018   EXPECT_EQ("f(); // comment\n"
1019             "     // first\n"
1020             "// at start\n"
1021             "otherLine();",
1022             format("f();   // comment\n"
1023                    " // first\n"
1024                    "// at start\n"
1025                    "otherLine();"));
1026   EXPECT_EQ("void f() {\n"
1027             "  lineWith(); // comment\n"
1028             "  // at start\n"
1029             "}",
1030             format("void              f() {\n"
1031                    "  lineWith(); // comment\n"
1032                    "  // at start\n"
1033                    "}"));
1034 
1035   verifyFormat("#define A                                                  \\\n"
1036                "  int i; /* iiiiiiiiiiiiiiiiiiiii */                       \\\n"
1037                "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1038                getLLVMStyleWithColumns(60));
1039   verifyFormat(
1040       "#define A                                                   \\\n"
1041       "  int i;                        /* iiiiiiiiiiiiiiiiiiiii */ \\\n"
1042       "  int jjjjjjjjjjjjjjjjjjjjjjjj; /* */",
1043       getLLVMStyleWithColumns(61));
1044 
1045   verifyFormat("if ( // This is some comment\n"
1046                "    x + 3) {\n"
1047                "}");
1048   EXPECT_EQ("if ( // This is some comment\n"
1049             "     // spanning two lines\n"
1050             "    x + 3) {\n"
1051             "}",
1052             format("if( // This is some comment\n"
1053                    "     // spanning two lines\n"
1054                    " x + 3) {\n"
1055                    "}"));
1056 
1057   verifyNoCrash("/\\\n/");
1058   verifyNoCrash("/\\\n* */");
1059 }
1060 
1061 TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
1062   EXPECT_EQ("SomeFunction(a,\n"
1063             "             b, // comment\n"
1064             "             c);",
1065             format("SomeFunction(a,\n"
1066                    "          b, // comment\n"
1067                    "      c);"));
1068   EXPECT_EQ("SomeFunction(a, b,\n"
1069             "             // comment\n"
1070             "             c);",
1071             format("SomeFunction(a,\n"
1072                    "          b,\n"
1073                    "  // comment\n"
1074                    "      c);"));
1075   EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
1076             "             c);",
1077             format("SomeFunction(a, b, // comment (unclear relation)\n"
1078                    "      c);"));
1079   EXPECT_EQ("SomeFunction(a, // comment\n"
1080             "             b,\n"
1081             "             c); // comment",
1082             format("SomeFunction(a,     // comment\n"
1083                    "          b,\n"
1084                    "      c); // comment"));
1085 }
1086 
1087 TEST_F(FormatTest, CanFormatCommentsLocally) {
1088   EXPECT_EQ("int a;    // comment\n"
1089             "int    b; // comment",
1090             format("int   a; // comment\n"
1091                    "int    b; // comment",
1092                    0, 0, getLLVMStyle()));
1093   EXPECT_EQ("int   a; // comment\n"
1094             "         // line 2\n"
1095             "int b;",
1096             format("int   a; // comment\n"
1097                    "            // line 2\n"
1098                    "int b;",
1099                    28, 0, getLLVMStyle()));
1100   EXPECT_EQ("int aaaaaa; // comment\n"
1101             "int b;\n"
1102             "int c; // unrelated comment",
1103             format("int aaaaaa; // comment\n"
1104                    "int b;\n"
1105                    "int   c; // unrelated comment",
1106                    31, 0, getLLVMStyle()));
1107 
1108   EXPECT_EQ("int a; // This\n"
1109             "       // is\n"
1110             "       // a",
1111             format("int a;      // This\n"
1112                    "            // is\n"
1113                    "            // a",
1114                    0, 0, getLLVMStyle()));
1115   EXPECT_EQ("int a; // This\n"
1116             "       // is\n"
1117             "       // a\n"
1118             "// This is b\n"
1119             "int b;",
1120             format("int a; // This\n"
1121                    "     // is\n"
1122                    "     // a\n"
1123                    "// This is b\n"
1124                    "int b;",
1125                    0, 0, getLLVMStyle()));
1126   EXPECT_EQ("int a; // This\n"
1127             "       // is\n"
1128             "       // a\n"
1129             "\n"
1130             "  // This is unrelated",
1131             format("int a; // This\n"
1132                    "     // is\n"
1133                    "     // a\n"
1134                    "\n"
1135                    "  // This is unrelated",
1136                    0, 0, getLLVMStyle()));
1137   EXPECT_EQ("int a;\n"
1138             "// This is\n"
1139             "// not formatted.   ",
1140             format("int a;\n"
1141                    "// This is\n"
1142                    "// not formatted.   ",
1143                    0, 0, getLLVMStyle()));
1144 }
1145 
1146 TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
1147   EXPECT_EQ("// comment", format("// comment  "));
1148   EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
1149             format("int aaaaaaa, bbbbbbb; // comment                   ",
1150                    getLLVMStyleWithColumns(33)));
1151   EXPECT_EQ("// comment\\\n", format("// comment\\\n  \t \v   \f   "));
1152   EXPECT_EQ("// comment    \\\n", format("// comment    \\\n  \t \v   \f   "));
1153 }
1154 
1155 TEST_F(FormatTest, UnderstandsBlockComments) {
1156   verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);");
1157   verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }");
1158   EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n"
1159             "  bbbbbbbbbbbbbbbbbbbbbbbbb);",
1160             format("f(aaaaaaaaaaaaaaaaaaaaaaaaa ,   \\\n"
1161                    "/* Trailing comment for aa... */\n"
1162                    "  bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1163   EXPECT_EQ(
1164       "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
1165       "  /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
1166       format("f(aaaaaaaaaaaaaaaaaaaaaaaaa    ,   \n"
1167              "/* Leading comment for bb... */   bbbbbbbbbbbbbbbbbbbbbbbbb);"));
1168   EXPECT_EQ(
1169       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1170       "    aaaaaaaaaaaaaaaaaa,\n"
1171       "    aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1172       "}",
1173       format("void      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
1174              "                      aaaaaaaaaaaaaaaaaa  ,\n"
1175              "    aaaaaaaaaaaaaaaaaa) {   /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
1176              "}"));
1177 
1178   FormatStyle NoBinPacking = getLLVMStyle();
1179   NoBinPacking.BinPackParameters = false;
1180   verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n"
1181                "         /* parameter 2 */ aaaaaa,\n"
1182                "         /* parameter 3 */ aaaaaa,\n"
1183                "         /* parameter 4 */ aaaaaa);",
1184                NoBinPacking);
1185 
1186   // Aligning block comments in macros.
1187   verifyGoogleFormat("#define A        \\\n"
1188                      "  int i;   /*a*/ \\\n"
1189                      "  int jjj; /*b*/");
1190 }
1191 
1192 TEST_F(FormatTest, AlignsBlockComments) {
1193   EXPECT_EQ("/*\n"
1194             " * Really multi-line\n"
1195             " * comment.\n"
1196             " */\n"
1197             "void f() {}",
1198             format("  /*\n"
1199                    "   * Really multi-line\n"
1200                    "   * comment.\n"
1201                    "   */\n"
1202                    "  void f() {}"));
1203   EXPECT_EQ("class C {\n"
1204             "  /*\n"
1205             "   * Another multi-line\n"
1206             "   * comment.\n"
1207             "   */\n"
1208             "  void f() {}\n"
1209             "};",
1210             format("class C {\n"
1211                    "/*\n"
1212                    " * Another multi-line\n"
1213                    " * comment.\n"
1214                    " */\n"
1215                    "void f() {}\n"
1216                    "};"));
1217   EXPECT_EQ("/*\n"
1218             "  1. This is a comment with non-trivial formatting.\n"
1219             "     1.1. We have to indent/outdent all lines equally\n"
1220             "         1.1.1. to keep the formatting.\n"
1221             " */",
1222             format("  /*\n"
1223                    "    1. This is a comment with non-trivial formatting.\n"
1224                    "       1.1. We have to indent/outdent all lines equally\n"
1225                    "           1.1.1. to keep the formatting.\n"
1226                    "   */"));
1227   EXPECT_EQ("/*\n"
1228             "Don't try to outdent if there's not enough indentation.\n"
1229             "*/",
1230             format("  /*\n"
1231                    " Don't try to outdent if there's not enough indentation.\n"
1232                    " */"));
1233 
1234   EXPECT_EQ("int i; /* Comment with empty...\n"
1235             "        *\n"
1236             "        * line. */",
1237             format("int i; /* Comment with empty...\n"
1238                    "        *\n"
1239                    "        * line. */"));
1240   EXPECT_EQ("int foobar = 0; /* comment */\n"
1241             "int bar = 0;    /* multiline\n"
1242             "                   comment 1 */\n"
1243             "int baz = 0;    /* multiline\n"
1244             "                   comment 2 */\n"
1245             "int bzz = 0;    /* multiline\n"
1246             "                   comment 3 */",
1247             format("int foobar = 0; /* comment */\n"
1248                    "int bar = 0;    /* multiline\n"
1249                    "                   comment 1 */\n"
1250                    "int baz = 0; /* multiline\n"
1251                    "                comment 2 */\n"
1252                    "int bzz = 0;         /* multiline\n"
1253                    "                        comment 3 */"));
1254   EXPECT_EQ("int foobar = 0; /* comment */\n"
1255             "int bar = 0;    /* multiline\n"
1256             "   comment */\n"
1257             "int baz = 0;    /* multiline\n"
1258             "comment */",
1259             format("int foobar = 0; /* comment */\n"
1260                    "int bar = 0; /* multiline\n"
1261                    "comment */\n"
1262                    "int baz = 0;        /* multiline\n"
1263                    "comment */"));
1264 }
1265 
1266 TEST_F(FormatTest, CorrectlyHandlesLengthOfBlockComments) {
1267   EXPECT_EQ("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1268             "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */",
1269             format("double *x; /* aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
1270                    "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa */"));
1271   EXPECT_EQ(
1272       "void ffffffffffff(\n"
1273       "    int aaaaaaaa, int bbbbbbbb,\n"
1274       "    int cccccccccccc) { /*\n"
1275       "                           aaaaaaaaaa\n"
1276       "                           aaaaaaaaaaaaa\n"
1277       "                           bbbbbbbbbbbbbb\n"
1278       "                           bbbbbbbbbb\n"
1279       "                         */\n"
1280       "}",
1281       format("void ffffffffffff(int aaaaaaaa, int bbbbbbbb, int cccccccccccc)\n"
1282              "{ /*\n"
1283              "     aaaaaaaaaa aaaaaaaaaaaaa\n"
1284              "     bbbbbbbbbbbbbb bbbbbbbbbb\n"
1285              "   */\n"
1286              "}",
1287              getLLVMStyleWithColumns(40)));
1288 }
1289 
1290 TEST_F(FormatTest, DontBreakNonTrailingBlockComments) {
1291   EXPECT_EQ("void ffffffffff(\n"
1292             "    int aaaaa /* test */);",
1293             format("void ffffffffff(int aaaaa /* test */);",
1294                    getLLVMStyleWithColumns(35)));
1295 }
1296 
1297 TEST_F(FormatTest, SplitsLongCxxComments) {
1298   EXPECT_EQ("// A comment that\n"
1299             "// doesn't fit on\n"
1300             "// one line",
1301             format("// A comment that doesn't fit on one line",
1302                    getLLVMStyleWithColumns(20)));
1303   EXPECT_EQ("// a b c d\n"
1304             "// e f  g\n"
1305             "// h i j k",
1306             format("// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1307   EXPECT_EQ(
1308       "// a b c d\n"
1309       "// e f  g\n"
1310       "// h i j k",
1311       format("\\\n// a b c d e f  g h i j k", getLLVMStyleWithColumns(10)));
1312   EXPECT_EQ("if (true) // A comment that\n"
1313             "          // doesn't fit on\n"
1314             "          // one line",
1315             format("if (true) // A comment that doesn't fit on one line   ",
1316                    getLLVMStyleWithColumns(30)));
1317   EXPECT_EQ("//    Don't_touch_leading_whitespace",
1318             format("//    Don't_touch_leading_whitespace",
1319                    getLLVMStyleWithColumns(20)));
1320   EXPECT_EQ("// Add leading\n"
1321             "// whitespace",
1322             format("//Add leading whitespace", getLLVMStyleWithColumns(20)));
1323   EXPECT_EQ("// whitespace", format("//whitespace", getLLVMStyle()));
1324   EXPECT_EQ("// Even if it makes the line exceed the column\n"
1325             "// limit",
1326             format("//Even if it makes the line exceed the column limit",
1327                    getLLVMStyleWithColumns(51)));
1328   EXPECT_EQ("//--But not here", format("//--But not here", getLLVMStyle()));
1329 
1330   EXPECT_EQ("// aa bb cc dd",
1331             format("// aa bb             cc dd                   ",
1332                    getLLVMStyleWithColumns(15)));
1333 
1334   EXPECT_EQ("// A comment before\n"
1335             "// a macro\n"
1336             "// definition\n"
1337             "#define a b",
1338             format("// A comment before a macro definition\n"
1339                    "#define a b",
1340                    getLLVMStyleWithColumns(20)));
1341   EXPECT_EQ("void ffffff(\n"
1342             "    int aaaaaaaaa,  // wwww\n"
1343             "    int bbbbbbbbbb, // xxxxxxx\n"
1344             "                    // yyyyyyyyyy\n"
1345             "    int c, int d, int e) {}",
1346             format("void ffffff(\n"
1347                    "    int aaaaaaaaa, // wwww\n"
1348                    "    int bbbbbbbbbb, // xxxxxxx yyyyyyyyyy\n"
1349                    "    int c, int d, int e) {}",
1350                    getLLVMStyleWithColumns(40)));
1351   EXPECT_EQ("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1352             format("//\t aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1353                    getLLVMStyleWithColumns(20)));
1354   EXPECT_EQ(
1355       "#define XXX // a b c d\n"
1356       "            // e f g h",
1357       format("#define XXX // a b c d e f g h", getLLVMStyleWithColumns(22)));
1358   EXPECT_EQ(
1359       "#define XXX // q w e r\n"
1360       "            // t y u i",
1361       format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1362 }
1363 
1364 TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
1365   EXPECT_EQ("//     A comment\n"
1366             "//     that doesn't\n"
1367             "//     fit on one\n"
1368             "//     line",
1369             format("//     A comment that doesn't fit on one line",
1370                    getLLVMStyleWithColumns(20)));
1371   EXPECT_EQ("///     A comment\n"
1372             "///     that doesn't\n"
1373             "///     fit on one\n"
1374             "///     line",
1375             format("///     A comment that doesn't fit on one line",
1376                    getLLVMStyleWithColumns(20)));
1377 }
1378 
1379 TEST_F(FormatTest, DontSplitLineCommentsWithEscapedNewlines) {
1380   EXPECT_EQ("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1381             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1382             "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
1383             format("// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1384                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
1385                    "// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
1386   EXPECT_EQ("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1387             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1388             "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1389             format("int a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1390                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1391                    "       // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1392                    getLLVMStyleWithColumns(50)));
1393   // FIXME: One day we might want to implement adjustment of leading whitespace
1394   // of the consecutive lines in this kind of comment:
1395   EXPECT_EQ("double\n"
1396             "    a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1397             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1398             "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1399             format("double a; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1400                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\\\n"
1401                    "          // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
1402                    getLLVMStyleWithColumns(49)));
1403 }
1404 
1405 TEST_F(FormatTest, DontSplitLineCommentsWithPragmas) {
1406   FormatStyle Pragmas = getLLVMStyleWithColumns(30);
1407   Pragmas.CommentPragmas = "^ IWYU pragma:";
1408   EXPECT_EQ(
1409       "// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb",
1410       format("// IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb", Pragmas));
1411   EXPECT_EQ(
1412       "/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */",
1413       format("/* IWYU pragma: aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb */", Pragmas));
1414 }
1415 
1416 TEST_F(FormatTest, PriorityOfCommentBreaking) {
1417   EXPECT_EQ("if (xxx ==\n"
1418             "        yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1419             "    zzz)\n"
1420             "  q();",
1421             format("if (xxx == yyy && // aaaaaaaaaaaa bbbbbbbbb\n"
1422                    "    zzz) q();",
1423                    getLLVMStyleWithColumns(40)));
1424   EXPECT_EQ("if (xxxxxxxxxx ==\n"
1425             "        yyy && // aaaaaa bbbbbbbb cccc\n"
1426             "    zzz)\n"
1427             "  q();",
1428             format("if (xxxxxxxxxx == yyy && // aaaaaa bbbbbbbb cccc\n"
1429                    "    zzz) q();",
1430                    getLLVMStyleWithColumns(40)));
1431   EXPECT_EQ("if (xxxxxxxxxx &&\n"
1432             "        yyy || // aaaaaa bbbbbbbb cccc\n"
1433             "    zzz)\n"
1434             "  q();",
1435             format("if (xxxxxxxxxx && yyy || // aaaaaa bbbbbbbb cccc\n"
1436                    "    zzz) q();",
1437                    getLLVMStyleWithColumns(40)));
1438   EXPECT_EQ("fffffffff(\n"
1439             "    &xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1440             "    zzz);",
1441             format("fffffffff(&xxx, // aaaaaaaaaaaa bbbbbbbbbbb\n"
1442                    " zzz);",
1443                    getLLVMStyleWithColumns(40)));
1444 }
1445 
1446 TEST_F(FormatTest, MultiLineCommentsInDefines) {
1447   EXPECT_EQ("#define A(x) /* \\\n"
1448             "  a comment     \\\n"
1449             "  inside */     \\\n"
1450             "  f();",
1451             format("#define A(x) /* \\\n"
1452                    "  a comment     \\\n"
1453                    "  inside */     \\\n"
1454                    "  f();",
1455                    getLLVMStyleWithColumns(17)));
1456   EXPECT_EQ("#define A(      \\\n"
1457             "    x) /*       \\\n"
1458             "  a comment     \\\n"
1459             "  inside */     \\\n"
1460             "  f();",
1461             format("#define A(      \\\n"
1462                    "    x) /*       \\\n"
1463                    "  a comment     \\\n"
1464                    "  inside */     \\\n"
1465                    "  f();",
1466                    getLLVMStyleWithColumns(17)));
1467 }
1468 
1469 TEST_F(FormatTest, ParsesCommentsAdjacentToPPDirectives) {
1470   EXPECT_EQ("namespace {}\n// Test\n#define A",
1471             format("namespace {}\n   // Test\n#define A"));
1472   EXPECT_EQ("namespace {}\n/* Test */\n#define A",
1473             format("namespace {}\n   /* Test */\n#define A"));
1474   EXPECT_EQ("namespace {}\n/* Test */ #define A",
1475             format("namespace {}\n   /* Test */    #define A"));
1476 }
1477 
1478 TEST_F(FormatTest, SplitsLongLinesInComments) {
1479   EXPECT_EQ("/* This is a long\n"
1480             " * comment that\n"
1481             " * doesn't\n"
1482             " * fit on one line.\n"
1483             " */",
1484             format("/* "
1485                    "This is a long                                         "
1486                    "comment that "
1487                    "doesn't                                    "
1488                    "fit on one line.  */",
1489                    getLLVMStyleWithColumns(20)));
1490   EXPECT_EQ(
1491       "/* a b c d\n"
1492       " * e f  g\n"
1493       " * h i j k\n"
1494       " */",
1495       format("/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1496   EXPECT_EQ(
1497       "/* a b c d\n"
1498       " * e f  g\n"
1499       " * h i j k\n"
1500       " */",
1501       format("\\\n/* a b c d e f  g h i j k */", getLLVMStyleWithColumns(10)));
1502   EXPECT_EQ("/*\n"
1503             "This is a long\n"
1504             "comment that doesn't\n"
1505             "fit on one line.\n"
1506             "*/",
1507             format("/*\n"
1508                    "This is a long                                         "
1509                    "comment that doesn't                                    "
1510                    "fit on one line.                                      \n"
1511                    "*/",
1512                    getLLVMStyleWithColumns(20)));
1513   EXPECT_EQ("/*\n"
1514             " * This is a long\n"
1515             " * comment that\n"
1516             " * doesn't fit on\n"
1517             " * one line.\n"
1518             " */",
1519             format("/*      \n"
1520                    " * This is a long "
1521                    "   comment that     "
1522                    "   doesn't fit on   "
1523                    "   one line.                                            \n"
1524                    " */",
1525                    getLLVMStyleWithColumns(20)));
1526   EXPECT_EQ("/*\n"
1527             " * This_is_a_comment_with_words_that_dont_fit_on_one_line\n"
1528             " * so_it_should_be_broken\n"
1529             " * wherever_a_space_occurs\n"
1530             " */",
1531             format("/*\n"
1532                    " * This_is_a_comment_with_words_that_dont_fit_on_one_line "
1533                    "   so_it_should_be_broken "
1534                    "   wherever_a_space_occurs                             \n"
1535                    " */",
1536                    getLLVMStyleWithColumns(20)));
1537   EXPECT_EQ("/*\n"
1538             " *    This_comment_can_not_be_broken_into_lines\n"
1539             " */",
1540             format("/*\n"
1541                    " *    This_comment_can_not_be_broken_into_lines\n"
1542                    " */",
1543                    getLLVMStyleWithColumns(20)));
1544   EXPECT_EQ("{\n"
1545             "  /*\n"
1546             "  This is another\n"
1547             "  long comment that\n"
1548             "  doesn't fit on one\n"
1549             "  line    1234567890\n"
1550             "  */\n"
1551             "}",
1552             format("{\n"
1553                    "/*\n"
1554                    "This is another     "
1555                    "  long comment that "
1556                    "  doesn't fit on one"
1557                    "  line    1234567890\n"
1558                    "*/\n"
1559                    "}",
1560                    getLLVMStyleWithColumns(20)));
1561   EXPECT_EQ("{\n"
1562             "  /*\n"
1563             "   * This        i s\n"
1564             "   * another comment\n"
1565             "   * t hat  doesn' t\n"
1566             "   * fit on one l i\n"
1567             "   * n e\n"
1568             "   */\n"
1569             "}",
1570             format("{\n"
1571                    "/*\n"
1572                    " * This        i s"
1573                    "   another comment"
1574                    "   t hat  doesn' t"
1575                    "   fit on one l i"
1576                    "   n e\n"
1577                    " */\n"
1578                    "}",
1579                    getLLVMStyleWithColumns(20)));
1580   EXPECT_EQ("/*\n"
1581             " * This is a long\n"
1582             " * comment that\n"
1583             " * doesn't fit on\n"
1584             " * one line\n"
1585             " */",
1586             format("   /*\n"
1587                    "    * This is a long comment that doesn't fit on one line\n"
1588                    "    */",
1589                    getLLVMStyleWithColumns(20)));
1590   EXPECT_EQ("{\n"
1591             "  if (something) /* This is a\n"
1592             "                    long\n"
1593             "                    comment */\n"
1594             "    ;\n"
1595             "}",
1596             format("{\n"
1597                    "  if (something) /* This is a long comment */\n"
1598                    "    ;\n"
1599                    "}",
1600                    getLLVMStyleWithColumns(30)));
1601 
1602   EXPECT_EQ("/* A comment before\n"
1603             " * a macro\n"
1604             " * definition */\n"
1605             "#define a b",
1606             format("/* A comment before a macro definition */\n"
1607                    "#define a b",
1608                    getLLVMStyleWithColumns(20)));
1609 
1610   EXPECT_EQ("/* some comment\n"
1611             "     *   a comment\n"
1612             "* that we break\n"
1613             " * another comment\n"
1614             "* we have to break\n"
1615             "* a left comment\n"
1616             " */",
1617             format("  /* some comment\n"
1618                    "       *   a comment that we break\n"
1619                    "   * another comment we have to break\n"
1620                    "* a left comment\n"
1621                    "   */",
1622                    getLLVMStyleWithColumns(20)));
1623 
1624   EXPECT_EQ("/*\n"
1625             "\n"
1626             "\n"
1627             "    */\n",
1628             format("  /*       \n"
1629                    "      \n"
1630                    "               \n"
1631                    "      */\n"));
1632 
1633   EXPECT_EQ("/* a a */",
1634             format("/* a a            */", getLLVMStyleWithColumns(15)));
1635   EXPECT_EQ("/* a a bc  */",
1636             format("/* a a            bc  */", getLLVMStyleWithColumns(15)));
1637   EXPECT_EQ("/* aaa aaa\n"
1638             " * aaaaa */",
1639             format("/* aaa aaa aaaaa       */", getLLVMStyleWithColumns(15)));
1640   EXPECT_EQ("/* aaa aaa\n"
1641             " * aaaaa     */",
1642             format("/* aaa aaa aaaaa     */", getLLVMStyleWithColumns(15)));
1643 }
1644 
1645 TEST_F(FormatTest, SplitsLongLinesInCommentsInPreprocessor) {
1646   EXPECT_EQ("#define X          \\\n"
1647             "  /*               \\\n"
1648             "   Test            \\\n"
1649             "   Macro comment   \\\n"
1650             "   with a long     \\\n"
1651             "   line            \\\n"
1652             "   */              \\\n"
1653             "  A + B",
1654             format("#define X \\\n"
1655                    "  /*\n"
1656                    "   Test\n"
1657                    "   Macro comment with a long  line\n"
1658                    "   */ \\\n"
1659                    "  A + B",
1660                    getLLVMStyleWithColumns(20)));
1661   EXPECT_EQ("#define X          \\\n"
1662             "  /* Macro comment \\\n"
1663             "     with a long   \\\n"
1664             "     line */       \\\n"
1665             "  A + B",
1666             format("#define X \\\n"
1667                    "  /* Macro comment with a long\n"
1668                    "     line */ \\\n"
1669                    "  A + B",
1670                    getLLVMStyleWithColumns(20)));
1671   EXPECT_EQ("#define X          \\\n"
1672             "  /* Macro comment \\\n"
1673             "   * with a long   \\\n"
1674             "   * line */       \\\n"
1675             "  A + B",
1676             format("#define X \\\n"
1677                    "  /* Macro comment with a long  line */ \\\n"
1678                    "  A + B",
1679                    getLLVMStyleWithColumns(20)));
1680 }
1681 
1682 TEST_F(FormatTest, CommentsInStaticInitializers) {
1683   EXPECT_EQ(
1684       "static SomeType type = {aaaaaaaaaaaaaaaaaaaa, /* comment */\n"
1685       "                        aaaaaaaaaaaaaaaaaaaa /* comment */,\n"
1686       "                        /* comment */ aaaaaaaaaaaaaaaaaaaa,\n"
1687       "                        aaaaaaaaaaaaaaaaaaaa, // comment\n"
1688       "                        aaaaaaaaaaaaaaaaaaaa};",
1689       format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa  ,  /* comment */\n"
1690              "                   aaaaaaaaaaaaaaaaaaaa   /* comment */ ,\n"
1691              "                     /* comment */   aaaaaaaaaaaaaaaaaaaa ,\n"
1692              "              aaaaaaaaaaaaaaaaaaaa ,   // comment\n"
1693              "                  aaaaaaaaaaaaaaaaaaaa };"));
1694   verifyFormat("static SomeType type = {aaaaaaaaaaa, // comment for aa...\n"
1695                "                        bbbbbbbbbbb, ccccccccccc};");
1696   verifyFormat("static SomeType type = {aaaaaaaaaaa,\n"
1697                "                        // comment for bb....\n"
1698                "                        bbbbbbbbbbb, ccccccccccc};");
1699   verifyGoogleFormat(
1700       "static SomeType type = {aaaaaaaaaaa,  // comment for aa...\n"
1701       "                        bbbbbbbbbbb, ccccccccccc};");
1702   verifyGoogleFormat("static SomeType type = {aaaaaaaaaaa,\n"
1703                      "                        // comment for bb....\n"
1704                      "                        bbbbbbbbbbb, ccccccccccc};");
1705 
1706   verifyFormat("S s = {{a, b, c},  // Group #1\n"
1707                "       {d, e, f},  // Group #2\n"
1708                "       {g, h, i}}; // Group #3");
1709   verifyFormat("S s = {{// Group #1\n"
1710                "        a, b, c},\n"
1711                "       {// Group #2\n"
1712                "        d, e, f},\n"
1713                "       {// Group #3\n"
1714                "        g, h, i}};");
1715 
1716   EXPECT_EQ("S s = {\n"
1717             "    // Some comment\n"
1718             "    a,\n"
1719             "\n"
1720             "    // Comment after empty line\n"
1721             "    b}",
1722             format("S s =    {\n"
1723                    "      // Some comment\n"
1724                    "  a,\n"
1725                    "  \n"
1726                    "     // Comment after empty line\n"
1727                    "      b\n"
1728                    "}"));
1729   EXPECT_EQ("S s = {\n"
1730             "    /* Some comment */\n"
1731             "    a,\n"
1732             "\n"
1733             "    /* Comment after empty line */\n"
1734             "    b}",
1735             format("S s =    {\n"
1736                    "      /* Some comment */\n"
1737                    "  a,\n"
1738                    "  \n"
1739                    "     /* Comment after empty line */\n"
1740                    "      b\n"
1741                    "}"));
1742   verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n"
1743                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1744                "    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n"
1745                "    0x00, 0x00, 0x00, 0x00};            // comment\n");
1746 }
1747 
1748 TEST_F(FormatTest, IgnoresIf0Contents) {
1749   EXPECT_EQ("#if 0\n"
1750             "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1751             "#endif\n"
1752             "void f() {}",
1753             format("#if 0\n"
1754                    "}{)(&*(^%%#%@! fsadj f;ldjs ,:;| <<<>>>][)(][\n"
1755                    "#endif\n"
1756                    "void f(  ) {  }"));
1757   EXPECT_EQ("#if false\n"
1758             "void f(  ) {  }\n"
1759             "#endif\n"
1760             "void g() {}\n",
1761             format("#if false\n"
1762                    "void f(  ) {  }\n"
1763                    "#endif\n"
1764                    "void g(  ) {  }\n"));
1765   EXPECT_EQ("enum E {\n"
1766             "  One,\n"
1767             "  Two,\n"
1768             "#if 0\n"
1769             "Three,\n"
1770             "      Four,\n"
1771             "#endif\n"
1772             "  Five\n"
1773             "};",
1774             format("enum E {\n"
1775                    "  One,Two,\n"
1776                    "#if 0\n"
1777                    "Three,\n"
1778                    "      Four,\n"
1779                    "#endif\n"
1780                    "  Five};"));
1781   EXPECT_EQ("enum F {\n"
1782             "  One,\n"
1783             "#if 1\n"
1784             "  Two,\n"
1785             "#if 0\n"
1786             "Three,\n"
1787             "      Four,\n"
1788             "#endif\n"
1789             "  Five\n"
1790             "#endif\n"
1791             "};",
1792             format("enum F {\n"
1793                    "One,\n"
1794                    "#if 1\n"
1795                    "Two,\n"
1796                    "#if 0\n"
1797                    "Three,\n"
1798                    "      Four,\n"
1799                    "#endif\n"
1800                    "Five\n"
1801                    "#endif\n"
1802                    "};"));
1803   EXPECT_EQ("enum G {\n"
1804             "  One,\n"
1805             "#if 0\n"
1806             "Two,\n"
1807             "#else\n"
1808             "  Three,\n"
1809             "#endif\n"
1810             "  Four\n"
1811             "};",
1812             format("enum G {\n"
1813                    "One,\n"
1814                    "#if 0\n"
1815                    "Two,\n"
1816                    "#else\n"
1817                    "Three,\n"
1818                    "#endif\n"
1819                    "Four\n"
1820                    "};"));
1821   EXPECT_EQ("enum H {\n"
1822             "  One,\n"
1823             "#if 0\n"
1824             "#ifdef Q\n"
1825             "Two,\n"
1826             "#else\n"
1827             "Three,\n"
1828             "#endif\n"
1829             "#endif\n"
1830             "  Four\n"
1831             "};",
1832             format("enum H {\n"
1833                    "One,\n"
1834                    "#if 0\n"
1835                    "#ifdef Q\n"
1836                    "Two,\n"
1837                    "#else\n"
1838                    "Three,\n"
1839                    "#endif\n"
1840                    "#endif\n"
1841                    "Four\n"
1842                    "};"));
1843   EXPECT_EQ("enum I {\n"
1844             "  One,\n"
1845             "#if /* test */ 0 || 1\n"
1846             "Two,\n"
1847             "Three,\n"
1848             "#endif\n"
1849             "  Four\n"
1850             "};",
1851             format("enum I {\n"
1852                    "One,\n"
1853                    "#if /* test */ 0 || 1\n"
1854                    "Two,\n"
1855                    "Three,\n"
1856                    "#endif\n"
1857                    "Four\n"
1858                    "};"));
1859   EXPECT_EQ("enum J {\n"
1860             "  One,\n"
1861             "#if 0\n"
1862             "#if 0\n"
1863             "Two,\n"
1864             "#else\n"
1865             "Three,\n"
1866             "#endif\n"
1867             "Four,\n"
1868             "#endif\n"
1869             "  Five\n"
1870             "};",
1871             format("enum J {\n"
1872                    "One,\n"
1873                    "#if 0\n"
1874                    "#if 0\n"
1875                    "Two,\n"
1876                    "#else\n"
1877                    "Three,\n"
1878                    "#endif\n"
1879                    "Four,\n"
1880                    "#endif\n"
1881                    "Five\n"
1882                    "};"));
1883 }
1884 
1885 //===----------------------------------------------------------------------===//
1886 // Tests for classes, namespaces, etc.
1887 //===----------------------------------------------------------------------===//
1888 
1889 TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) {
1890   verifyFormat("class A {};");
1891 }
1892 
1893 TEST_F(FormatTest, UnderstandsAccessSpecifiers) {
1894   verifyFormat("class A {\n"
1895                "public:\n"
1896                "public: // comment\n"
1897                "protected:\n"
1898                "private:\n"
1899                "  void f() {}\n"
1900                "};");
1901   verifyGoogleFormat("class A {\n"
1902                      " public:\n"
1903                      " protected:\n"
1904                      " private:\n"
1905                      "  void f() {}\n"
1906                      "};");
1907   verifyFormat("class A {\n"
1908                "public slots:\n"
1909                "  void f() {}\n"
1910                "public Q_SLOTS:\n"
1911                "  void f() {}\n"
1912                "signals:\n"
1913                "  void g();\n"
1914                "};");
1915 
1916   // Don't interpret 'signals' the wrong way.
1917   verifyFormat("signals.set();");
1918   verifyFormat("for (Signals signals : f()) {\n}");
1919 }
1920 
1921 TEST_F(FormatTest, SeparatesLogicalBlocks) {
1922   EXPECT_EQ("class A {\n"
1923             "public:\n"
1924             "  void f();\n"
1925             "\n"
1926             "private:\n"
1927             "  void g() {}\n"
1928             "  // test\n"
1929             "protected:\n"
1930             "  int h;\n"
1931             "};",
1932             format("class A {\n"
1933                    "public:\n"
1934                    "void f();\n"
1935                    "private:\n"
1936                    "void g() {}\n"
1937                    "// test\n"
1938                    "protected:\n"
1939                    "int h;\n"
1940                    "};"));
1941   EXPECT_EQ("class A {\n"
1942             "protected:\n"
1943             "public:\n"
1944             "  void f();\n"
1945             "};",
1946             format("class A {\n"
1947                    "protected:\n"
1948                    "\n"
1949                    "public:\n"
1950                    "\n"
1951                    "  void f();\n"
1952                    "};"));
1953 
1954   // Even ensure proper spacing inside macros.
1955   EXPECT_EQ("#define B     \\\n"
1956             "  class A {   \\\n"
1957             "   protected: \\\n"
1958             "   public:    \\\n"
1959             "    void f(); \\\n"
1960             "  };",
1961             format("#define B     \\\n"
1962                    "  class A {   \\\n"
1963                    "   protected: \\\n"
1964                    "              \\\n"
1965                    "   public:    \\\n"
1966                    "              \\\n"
1967                    "    void f(); \\\n"
1968                    "  };",
1969                    getGoogleStyle()));
1970   // But don't remove empty lines after macros ending in access specifiers.
1971   EXPECT_EQ("#define A private:\n"
1972             "\n"
1973             "int i;",
1974             format("#define A         private:\n"
1975                    "\n"
1976                    "int              i;"));
1977 }
1978 
1979 TEST_F(FormatTest, FormatsClasses) {
1980   verifyFormat("class A : public B {};");
1981   verifyFormat("class A : public ::B {};");
1982 
1983   verifyFormat(
1984       "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1985       "                             public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1986   verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
1987                "    : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n"
1988                "      public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {};");
1989   verifyFormat(
1990       "class A : public B, public C, public D, public E, public F {};");
1991   verifyFormat("class AAAAAAAAAAAA : public B,\n"
1992                "                     public C,\n"
1993                "                     public D,\n"
1994                "                     public E,\n"
1995                "                     public F,\n"
1996                "                     public G {};");
1997 
1998   verifyFormat("class\n"
1999                "    ReallyReallyLongClassName {\n"
2000                "  int i;\n"
2001                "};",
2002                getLLVMStyleWithColumns(32));
2003   verifyFormat("struct aaaaaaaaaaaaa : public aaaaaaaaaaaaaaaaaaa< // break\n"
2004                "                           aaaaaaaaaaaaaaaa> {};");
2005   verifyFormat("struct aaaaaaaaaaaaaaaaaaaa\n"
2006                "    : public aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaa,\n"
2007                "                                 aaaaaaaaaaaaaaaaaaaaaa> {};");
2008   verifyFormat("template <class R, class C>\n"
2009                "struct Aaaaaaaaaaaaaaaaa<R (C::*)(int) const>\n"
2010                "    : Aaaaaaaaaaaaaaaaa<R (C::*)(int)> {};");
2011   verifyFormat("class ::A::B {};");
2012 }
2013 
2014 TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) {
2015   verifyFormat("class A {\n} a, b;");
2016   verifyFormat("struct A {\n} a, b;");
2017   verifyFormat("union A {\n} a;");
2018 }
2019 
2020 TEST_F(FormatTest, FormatsEnum) {
2021   verifyFormat("enum {\n"
2022                "  Zero,\n"
2023                "  One = 1,\n"
2024                "  Two = One + 1,\n"
2025                "  Three = (One + Two),\n"
2026                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2027                "  Five = (One, Two, Three, Four, 5)\n"
2028                "};");
2029   verifyGoogleFormat("enum {\n"
2030                      "  Zero,\n"
2031                      "  One = 1,\n"
2032                      "  Two = One + 1,\n"
2033                      "  Three = (One + Two),\n"
2034                      "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2035                      "  Five = (One, Two, Three, Four, 5)\n"
2036                      "};");
2037   verifyFormat("enum Enum {};");
2038   verifyFormat("enum {};");
2039   verifyFormat("enum X E {} d;");
2040   verifyFormat("enum __attribute__((...)) E {} d;");
2041   verifyFormat("enum __declspec__((...)) E {} d;");
2042   verifyFormat("enum X f() {\n  a();\n  return 42;\n}");
2043   verifyFormat("enum {\n"
2044                "  Bar = Foo<int, int>::value\n"
2045                "};",
2046                getLLVMStyleWithColumns(30));
2047 
2048   verifyFormat("enum ShortEnum { A, B, C };");
2049   verifyGoogleFormat("enum ShortEnum { A, B, C };");
2050 
2051   EXPECT_EQ("enum KeepEmptyLines {\n"
2052             "  ONE,\n"
2053             "\n"
2054             "  TWO,\n"
2055             "\n"
2056             "  THREE\n"
2057             "}",
2058             format("enum KeepEmptyLines {\n"
2059                    "  ONE,\n"
2060                    "\n"
2061                    "  TWO,\n"
2062                    "\n"
2063                    "\n"
2064                    "  THREE\n"
2065                    "}"));
2066   verifyFormat("enum E { // comment\n"
2067                "  ONE,\n"
2068                "  TWO\n"
2069                "};\n"
2070                "int i;");
2071 }
2072 
2073 TEST_F(FormatTest, FormatsEnumsWithErrors) {
2074   verifyFormat("enum Type {\n"
2075                "  One = 0; // These semicolons should be commas.\n"
2076                "  Two = 1;\n"
2077                "};");
2078   verifyFormat("namespace n {\n"
2079                "enum Type {\n"
2080                "  One,\n"
2081                "  Two, // missing };\n"
2082                "  int i;\n"
2083                "}\n"
2084                "void g() {}");
2085 }
2086 
2087 TEST_F(FormatTest, FormatsEnumStruct) {
2088   verifyFormat("enum struct {\n"
2089                "  Zero,\n"
2090                "  One = 1,\n"
2091                "  Two = One + 1,\n"
2092                "  Three = (One + Two),\n"
2093                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2094                "  Five = (One, Two, Three, Four, 5)\n"
2095                "};");
2096   verifyFormat("enum struct Enum {};");
2097   verifyFormat("enum struct {};");
2098   verifyFormat("enum struct X E {} d;");
2099   verifyFormat("enum struct __attribute__((...)) E {} d;");
2100   verifyFormat("enum struct __declspec__((...)) E {} d;");
2101   verifyFormat("enum struct X f() {\n  a();\n  return 42;\n}");
2102 }
2103 
2104 TEST_F(FormatTest, FormatsEnumClass) {
2105   verifyFormat("enum class {\n"
2106                "  Zero,\n"
2107                "  One = 1,\n"
2108                "  Two = One + 1,\n"
2109                "  Three = (One + Two),\n"
2110                "  Four = (Zero && (One ^ Two)) | (One << Two),\n"
2111                "  Five = (One, Two, Three, Four, 5)\n"
2112                "};");
2113   verifyFormat("enum class Enum {};");
2114   verifyFormat("enum class {};");
2115   verifyFormat("enum class X E {} d;");
2116   verifyFormat("enum class __attribute__((...)) E {} d;");
2117   verifyFormat("enum class __declspec__((...)) E {} d;");
2118   verifyFormat("enum class X f() {\n  a();\n  return 42;\n}");
2119 }
2120 
2121 TEST_F(FormatTest, FormatsEnumTypes) {
2122   verifyFormat("enum X : int {\n"
2123                "  A, // Force multiple lines.\n"
2124                "  B\n"
2125                "};");
2126   verifyFormat("enum X : int { A, B };");
2127   verifyFormat("enum X : std::uint32_t { A, B };");
2128 }
2129 
2130 TEST_F(FormatTest, FormatsNSEnums) {
2131   verifyGoogleFormat("typedef NS_ENUM(NSInteger, SomeName) { AAA, BBB }");
2132   verifyGoogleFormat("typedef NS_ENUM(NSInteger, MyType) {\n"
2133                      "  // Information about someDecentlyLongValue.\n"
2134                      "  someDecentlyLongValue,\n"
2135                      "  // Information about anotherDecentlyLongValue.\n"
2136                      "  anotherDecentlyLongValue,\n"
2137                      "  // Information about aThirdDecentlyLongValue.\n"
2138                      "  aThirdDecentlyLongValue\n"
2139                      "};");
2140   verifyGoogleFormat("typedef NS_OPTIONS(NSInteger, MyType) {\n"
2141                      "  a = 1,\n"
2142                      "  b = 2,\n"
2143                      "  c = 3,\n"
2144                      "};");
2145   verifyGoogleFormat("typedef CF_ENUM(NSInteger, MyType) {\n"
2146                      "  a = 1,\n"
2147                      "  b = 2,\n"
2148                      "  c = 3,\n"
2149                      "};");
2150   verifyGoogleFormat("typedef CF_OPTIONS(NSInteger, MyType) {\n"
2151                      "  a = 1,\n"
2152                      "  b = 2,\n"
2153                      "  c = 3,\n"
2154                      "};");
2155 }
2156 
2157 TEST_F(FormatTest, FormatsBitfields) {
2158   verifyFormat("struct Bitfields {\n"
2159                "  unsigned sClass : 8;\n"
2160                "  unsigned ValueKind : 2;\n"
2161                "};");
2162   verifyFormat("struct A {\n"
2163                "  int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n"
2164                "      bbbbbbbbbbbbbbbbbbbbbbbbb;\n"
2165                "};");
2166   verifyFormat("struct MyStruct {\n"
2167                "  uchar data;\n"
2168                "  uchar : 8;\n"
2169                "  uchar : 8;\n"
2170                "  uchar other;\n"
2171                "};");
2172 }
2173 
2174 TEST_F(FormatTest, FormatsNamespaces) {
2175   verifyFormat("namespace some_namespace {\n"
2176                "class A {};\n"
2177                "void f() { f(); }\n"
2178                "}");
2179   verifyFormat("namespace {\n"
2180                "class A {};\n"
2181                "void f() { f(); }\n"
2182                "}");
2183   verifyFormat("inline namespace X {\n"
2184                "class A {};\n"
2185                "void f() { f(); }\n"
2186                "}");
2187   verifyFormat("using namespace some_namespace;\n"
2188                "class A {};\n"
2189                "void f() { f(); }");
2190 
2191   // This code is more common than we thought; if we
2192   // layout this correctly the semicolon will go into
2193   // its own line, which is undesirable.
2194   verifyFormat("namespace {};");
2195   verifyFormat("namespace {\n"
2196                "class A {};\n"
2197                "};");
2198 
2199   verifyFormat("namespace {\n"
2200                "int SomeVariable = 0; // comment\n"
2201                "} // namespace");
2202   EXPECT_EQ("#ifndef HEADER_GUARD\n"
2203             "#define HEADER_GUARD\n"
2204             "namespace my_namespace {\n"
2205             "int i;\n"
2206             "} // my_namespace\n"
2207             "#endif // HEADER_GUARD",
2208             format("#ifndef HEADER_GUARD\n"
2209                    " #define HEADER_GUARD\n"
2210                    "   namespace my_namespace {\n"
2211                    "int i;\n"
2212                    "}    // my_namespace\n"
2213                    "#endif    // HEADER_GUARD"));
2214 
2215   FormatStyle Style = getLLVMStyle();
2216   Style.NamespaceIndentation = FormatStyle::NI_All;
2217   EXPECT_EQ("namespace out {\n"
2218             "  int i;\n"
2219             "  namespace in {\n"
2220             "    int i;\n"
2221             "  } // namespace\n"
2222             "} // namespace",
2223             format("namespace out {\n"
2224                    "int i;\n"
2225                    "namespace in {\n"
2226                    "int i;\n"
2227                    "} // namespace\n"
2228                    "} // namespace",
2229                    Style));
2230 
2231   Style.NamespaceIndentation = FormatStyle::NI_Inner;
2232   EXPECT_EQ("namespace out {\n"
2233             "int i;\n"
2234             "namespace in {\n"
2235             "  int i;\n"
2236             "} // namespace\n"
2237             "} // namespace",
2238             format("namespace out {\n"
2239                    "int i;\n"
2240                    "namespace in {\n"
2241                    "int i;\n"
2242                    "} // namespace\n"
2243                    "} // namespace",
2244                    Style));
2245 }
2246 
2247 TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); }
2248 
2249 TEST_F(FormatTest, FormatsInlineASM) {
2250   verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));");
2251   verifyFormat("asm(\"nop\" ::: \"memory\");");
2252   verifyFormat(
2253       "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n"
2254       "    \"cpuid\\n\\t\"\n"
2255       "    \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n"
2256       "    : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n"
2257       "    : \"a\"(value));");
2258   EXPECT_EQ(
2259       "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n"
2260       "  __asm {\n"
2261       "        mov     edx,[that] // vtable in edx\n"
2262       "        mov     eax,methodIndex\n"
2263       "        call    [edx][eax*4] // stdcall\n"
2264       "  }\n"
2265       "}",
2266       format("void NS_InvokeByIndex(void *that,   unsigned int methodIndex) {\n"
2267              "    __asm {\n"
2268              "        mov     edx,[that] // vtable in edx\n"
2269              "        mov     eax,methodIndex\n"
2270              "        call    [edx][eax*4] // stdcall\n"
2271              "    }\n"
2272              "}"));
2273   verifyFormat("void function() {\n"
2274                "  // comment\n"
2275                "  asm(\"\");\n"
2276                "}");
2277 }
2278 
2279 TEST_F(FormatTest, FormatTryCatch) {
2280   verifyFormat("try {\n"
2281                "  throw a * b;\n"
2282                "} catch (int a) {\n"
2283                "  // Do nothing.\n"
2284                "} catch (...) {\n"
2285                "  exit(42);\n"
2286                "}");
2287 
2288   // Function-level try statements.
2289   verifyFormat("int f() try { return 4; } catch (...) {\n"
2290                "  return 5;\n"
2291                "}");
2292   verifyFormat("class A {\n"
2293                "  int a;\n"
2294                "  A() try : a(0) {\n"
2295                "  } catch (...) {\n"
2296                "    throw;\n"
2297                "  }\n"
2298                "};\n");
2299 
2300   // Incomplete try-catch blocks.
2301   verifyFormat("try {} catch (");
2302 }
2303 
2304 TEST_F(FormatTest, FormatSEHTryCatch) {
2305   verifyFormat("__try {\n"
2306                "  int a = b * c;\n"
2307                "} __except (EXCEPTION_EXECUTE_HANDLER) {\n"
2308                "  // Do nothing.\n"
2309                "}");
2310 
2311   verifyFormat("__try {\n"
2312                "  int a = b * c;\n"
2313                "} __finally {\n"
2314                "  // Do nothing.\n"
2315                "}");
2316 
2317   verifyFormat("DEBUG({\n"
2318                "  __try {\n"
2319                "  } __finally {\n"
2320                "  }\n"
2321                "});\n");
2322 }
2323 
2324 TEST_F(FormatTest, IncompleteTryCatchBlocks) {
2325   verifyFormat("try {\n"
2326                "  f();\n"
2327                "} catch {\n"
2328                "  g();\n"
2329                "}");
2330   verifyFormat("try {\n"
2331                "  f();\n"
2332                "} catch (A a) MACRO(x) {\n"
2333                "  g();\n"
2334                "} catch (B b) MACRO(x) {\n"
2335                "  g();\n"
2336                "}");
2337 }
2338 
2339 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
2340   FormatStyle Style = getLLVMStyle();
2341   Style.BreakBeforeBraces = FormatStyle::BS_Attach;
2342   verifyFormat("try {\n"
2343                "  // something\n"
2344                "} catch (...) {\n"
2345                "  // something\n"
2346                "}",
2347                Style);
2348   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
2349   verifyFormat("try {\n"
2350                "  // something\n"
2351                "}\n"
2352                "catch (...) {\n"
2353                "  // something\n"
2354                "}",
2355                Style);
2356   verifyFormat("__try {\n"
2357                "  // something\n"
2358                "}\n"
2359                "__finally {\n"
2360                "  // something\n"
2361                "}",
2362                Style);
2363   verifyFormat("@try {\n"
2364                "  // something\n"
2365                "}\n"
2366                "@finally {\n"
2367                "  // something\n"
2368                "}",
2369                Style);
2370   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
2371   verifyFormat("try\n"
2372                "{\n"
2373                "  // something\n"
2374                "}\n"
2375                "catch (...)\n"
2376                "{\n"
2377                "  // something\n"
2378                "}",
2379                Style);
2380   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
2381   verifyFormat("try\n"
2382                "  {\n"
2383                "    // something\n"
2384                "  }\n"
2385                "catch (...)\n"
2386                "  {\n"
2387                "    // something\n"
2388                "  }",
2389                Style);
2390 }
2391 
2392 TEST_F(FormatTest, FormatObjCTryCatch) {
2393   verifyFormat("@try {\n"
2394                "  f();\n"
2395                "} @catch (NSException e) {\n"
2396                "  @throw;\n"
2397                "} @finally {\n"
2398                "  exit(42);\n"
2399                "}");
2400   verifyFormat("DEBUG({\n"
2401                "  @try {\n"
2402                "  } @finally {\n"
2403                "  }\n"
2404                "});\n");
2405 }
2406 
2407 TEST_F(FormatTest, StaticInitializers) {
2408   verifyFormat("static SomeClass SC = {1, 'a'};");
2409 
2410   verifyFormat("static SomeClass WithALoooooooooooooooooooongName = {\n"
2411                "    100000000, "
2412                "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"};");
2413 
2414   // Here, everything other than the "}" would fit on a line.
2415   verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n"
2416                "    10000000000000000000000000};");
2417   EXPECT_EQ("S s = {a,\n"
2418             "\n"
2419             "       b};",
2420             format("S s = {\n"
2421                    "  a,\n"
2422                    "\n"
2423                    "  b\n"
2424                    "};"));
2425 
2426   // FIXME: This would fit into the column limit if we'd fit "{ {" on the first
2427   // line. However, the formatting looks a bit off and this probably doesn't
2428   // happen often in practice.
2429   verifyFormat("static int Variable[1] = {\n"
2430                "    {1000000000000000000000000000000000000}};",
2431                getLLVMStyleWithColumns(40));
2432 }
2433 
2434 TEST_F(FormatTest, DesignatedInitializers) {
2435   verifyFormat("const struct A a = {.a = 1, .b = 2};");
2436   verifyFormat("const struct A a = {.aaaaaaaaaa = 1,\n"
2437                "                    .bbbbbbbbbb = 2,\n"
2438                "                    .cccccccccc = 3,\n"
2439                "                    .dddddddddd = 4,\n"
2440                "                    .eeeeeeeeee = 5};");
2441   verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n"
2442                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaa = 1,\n"
2443                "    .bbbbbbbbbbbbbbbbbbbbbbbbbbb = 2,\n"
2444                "    .ccccccccccccccccccccccccccc = 3,\n"
2445                "    .ddddddddddddddddddddddddddd = 4,\n"
2446                "    .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};");
2447 
2448   verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};");
2449 }
2450 
2451 TEST_F(FormatTest, NestedStaticInitializers) {
2452   verifyFormat("static A x = {{{}}};\n");
2453   verifyFormat("static A x = {{{init1, init2, init3, init4},\n"
2454                "               {init1, init2, init3, init4}}};",
2455                getLLVMStyleWithColumns(50));
2456 
2457   verifyFormat("somes Status::global_reps[3] = {\n"
2458                "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2459                "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2460                "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};",
2461                getLLVMStyleWithColumns(60));
2462   verifyGoogleFormat("SomeType Status::global_reps[3] = {\n"
2463                      "    {kGlobalRef, OK_CODE, NULL, NULL, NULL},\n"
2464                      "    {kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL},\n"
2465                      "    {kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL}};");
2466   verifyFormat("CGRect cg_rect = {{rect.fLeft, rect.fTop},\n"
2467                "                  {rect.fRight - rect.fLeft, rect.fBottom - "
2468                "rect.fTop}};");
2469 
2470   verifyFormat(
2471       "SomeArrayOfSomeType a = {\n"
2472       "    {{1, 2, 3},\n"
2473       "     {1, 2, 3},\n"
2474       "     {111111111111111111111111111111, 222222222222222222222222222222,\n"
2475       "      333333333333333333333333333333},\n"
2476       "     {1, 2, 3},\n"
2477       "     {1, 2, 3}}};");
2478   verifyFormat(
2479       "SomeArrayOfSomeType a = {\n"
2480       "    {{1, 2, 3}},\n"
2481       "    {{1, 2, 3}},\n"
2482       "    {{111111111111111111111111111111, 222222222222222222222222222222,\n"
2483       "      333333333333333333333333333333}},\n"
2484       "    {{1, 2, 3}},\n"
2485       "    {{1, 2, 3}}};");
2486 
2487   verifyFormat("struct {\n"
2488                "  unsigned bit;\n"
2489                "  const char *const name;\n"
2490                "} kBitsToOs[] = {{kOsMac, \"Mac\"},\n"
2491                "                 {kOsWin, \"Windows\"},\n"
2492                "                 {kOsLinux, \"Linux\"},\n"
2493                "                 {kOsCrOS, \"Chrome OS\"}};");
2494   verifyFormat("struct {\n"
2495                "  unsigned bit;\n"
2496                "  const char *const name;\n"
2497                "} kBitsToOs[] = {\n"
2498                "    {kOsMac, \"Mac\"},\n"
2499                "    {kOsWin, \"Windows\"},\n"
2500                "    {kOsLinux, \"Linux\"},\n"
2501                "    {kOsCrOS, \"Chrome OS\"},\n"
2502                "};");
2503 }
2504 
2505 TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) {
2506   verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2507                "                      \\\n"
2508                "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)");
2509 }
2510 
2511 TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) {
2512   verifyFormat("virtual void write(ELFWriter *writerrr,\n"
2513                "                   OwningPtr<FileOutputBuffer> &buffer) = 0;");
2514 
2515   // Do break defaulted and deleted functions.
2516   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2517                "    default;",
2518                getLLVMStyleWithColumns(40));
2519   verifyFormat("virtual void ~Deeeeeeeestructor() =\n"
2520                "    delete;",
2521                getLLVMStyleWithColumns(40));
2522 }
2523 
2524 TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) {
2525   verifyFormat("# 1111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\" 2 3",
2526                getLLVMStyleWithColumns(40));
2527   verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2528                getLLVMStyleWithColumns(40));
2529   EXPECT_EQ("#define Q                              \\\n"
2530             "  \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\"    \\\n"
2531             "  \"aaaaaaaa.cpp\"",
2532             format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"",
2533                    getLLVMStyleWithColumns(40)));
2534 }
2535 
2536 TEST_F(FormatTest, UnderstandsLinePPDirective) {
2537   EXPECT_EQ("# 123 \"A string literal\"",
2538             format("   #     123    \"A string literal\""));
2539 }
2540 
2541 TEST_F(FormatTest, LayoutUnknownPPDirective) {
2542   EXPECT_EQ("#;", format("#;"));
2543   verifyFormat("#\n;\n;\n;");
2544 }
2545 
2546 TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) {
2547   EXPECT_EQ("#line 42 \"test\"\n",
2548             format("#  \\\n  line  \\\n  42  \\\n  \"test\"\n"));
2549   EXPECT_EQ("#define A B\n", format("#  \\\n define  \\\n    A  \\\n       B\n",
2550                                     getLLVMStyleWithColumns(12)));
2551 }
2552 
2553 TEST_F(FormatTest, EndOfFileEndsPPDirective) {
2554   EXPECT_EQ("#line 42 \"test\"",
2555             format("#  \\\n  line  \\\n  42  \\\n  \"test\""));
2556   EXPECT_EQ("#define A B", format("#  \\\n define  \\\n    A  \\\n       B"));
2557 }
2558 
2559 TEST_F(FormatTest, DoesntRemoveUnknownTokens) {
2560   verifyFormat("#define A \\x20");
2561   verifyFormat("#define A \\ x20");
2562   EXPECT_EQ("#define A \\ x20", format("#define A \\   x20"));
2563   verifyFormat("#define A ''");
2564   verifyFormat("#define A ''qqq");
2565   verifyFormat("#define A `qqq");
2566   verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");");
2567   EXPECT_EQ("const char *c = STRINGIFY(\n"
2568             "\\na : b);",
2569             format("const char * c = STRINGIFY(\n"
2570                    "\\na : b);"));
2571 
2572   verifyFormat("a\r\\");
2573   verifyFormat("a\v\\");
2574   verifyFormat("a\f\\");
2575 }
2576 
2577 TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) {
2578   verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13));
2579   verifyFormat("#define A( \\\n    BB)", getLLVMStyleWithColumns(12));
2580   verifyFormat("#define A( \\\n    A, B)", getLLVMStyleWithColumns(12));
2581   // FIXME: We never break before the macro name.
2582   verifyFormat("#define AA( \\\n    B)", getLLVMStyleWithColumns(12));
2583 
2584   verifyFormat("#define A A\n#define A A");
2585   verifyFormat("#define A(X) A\n#define A A");
2586 
2587   verifyFormat("#define Something Other", getLLVMStyleWithColumns(23));
2588   verifyFormat("#define Something    \\\n  Other", getLLVMStyleWithColumns(22));
2589 }
2590 
2591 TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
2592   EXPECT_EQ("// somecomment\n"
2593             "#include \"a.h\"\n"
2594             "#define A(  \\\n"
2595             "    A, B)\n"
2596             "#include \"b.h\"\n"
2597             "// somecomment\n",
2598             format("  // somecomment\n"
2599                    "  #include \"a.h\"\n"
2600                    "#define A(A,\\\n"
2601                    "    B)\n"
2602                    "    #include \"b.h\"\n"
2603                    " // somecomment\n",
2604                    getLLVMStyleWithColumns(13)));
2605 }
2606 
2607 TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
2608 
2609 TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
2610   EXPECT_EQ("#define A    \\\n"
2611             "  c;         \\\n"
2612             "  e;\n"
2613             "f;",
2614             format("#define A c; e;\n"
2615                    "f;",
2616                    getLLVMStyleWithColumns(14)));
2617 }
2618 
2619 TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
2620 
2621 TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
2622   EXPECT_EQ("int  i;\n"
2623             "#define A \\\n"
2624             "  int i;  \\\n"
2625             "  int j\n"
2626             "int  k;",
2627             format("int  i;\n"
2628                    "#define A  \\\n"
2629                    " int   i    ;  \\\n"
2630                    " int   j\n"
2631                    "int  k;",
2632                    8, 0, getGoogleStyle())); // 8: position of "#define".
2633   EXPECT_EQ("int  i;\n"
2634             "#define A \\\n"
2635             "  int i;  \\\n"
2636             "  int j\n"
2637             "int  k;",
2638             format("int  i;\n"
2639                    "#define A  \\\n"
2640                    " int   i    ;  \\\n"
2641                    " int   j\n"
2642                    "int  k;",
2643                    45, 0, getGoogleStyle())); // 45: position of "j".
2644 }
2645 
2646 TEST_F(FormatTest, MacroDefinitionInsideStatement) {
2647   EXPECT_EQ("int x,\n"
2648             "#define A\n"
2649             "    y;",
2650             format("int x,\n#define A\ny;"));
2651 }
2652 
2653 TEST_F(FormatTest, HashInMacroDefinition) {
2654   EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
2655   verifyFormat("#define A \\\n  b #c;", getLLVMStyleWithColumns(11));
2656   verifyFormat("#define A  \\\n"
2657                "  {        \\\n"
2658                "    f(#c); \\\n"
2659                "  }",
2660                getLLVMStyleWithColumns(11));
2661 
2662   verifyFormat("#define A(X)         \\\n"
2663                "  void function##X()",
2664                getLLVMStyleWithColumns(22));
2665 
2666   verifyFormat("#define A(a, b, c)   \\\n"
2667                "  void a##b##c()",
2668                getLLVMStyleWithColumns(22));
2669 
2670   verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22));
2671 }
2672 
2673 TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) {
2674   EXPECT_EQ("#define A (x)", format("#define A (x)"));
2675   EXPECT_EQ("#define A(x)", format("#define A(x)"));
2676 }
2677 
2678 TEST_F(FormatTest, EmptyLinesInMacroDefinitions) {
2679   EXPECT_EQ("#define A b;", format("#define A \\\n"
2680                                    "          \\\n"
2681                                    "  b;",
2682                                    getLLVMStyleWithColumns(25)));
2683   EXPECT_EQ("#define A \\\n"
2684             "          \\\n"
2685             "  a;      \\\n"
2686             "  b;",
2687             format("#define A \\\n"
2688                    "          \\\n"
2689                    "  a;      \\\n"
2690                    "  b;",
2691                    getLLVMStyleWithColumns(11)));
2692   EXPECT_EQ("#define A \\\n"
2693             "  a;      \\\n"
2694             "          \\\n"
2695             "  b;",
2696             format("#define A \\\n"
2697                    "  a;      \\\n"
2698                    "          \\\n"
2699                    "  b;",
2700                    getLLVMStyleWithColumns(11)));
2701 }
2702 
2703 TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
2704   verifyFormat("#define A :");
2705   verifyFormat("#define SOMECASES  \\\n"
2706                "  case 1:          \\\n"
2707                "  case 2\n",
2708                getLLVMStyleWithColumns(20));
2709   verifyFormat("#define A template <typename T>");
2710   verifyFormat("#define STR(x) #x\n"
2711                "f(STR(this_is_a_string_literal{));");
2712   verifyFormat("#pragma omp threadprivate( \\\n"
2713                "    y)), // expected-warning",
2714                getLLVMStyleWithColumns(28));
2715   verifyFormat("#d, = };");
2716   verifyFormat("#if \"a");
2717   verifyFormat("({\n"
2718                "#define b }\\\n"
2719                "  a\n"
2720                "a");
2721   verifyFormat("#define A     \\\n"
2722                "  {           \\\n"
2723                "    {\n"
2724                "#define B     \\\n"
2725                "  }           \\\n"
2726                "  }",
2727                getLLVMStyleWithColumns(15));
2728 
2729   verifyNoCrash("#if a\na(\n#else\n#endif\n{a");
2730   verifyNoCrash("a={0,1\n#if a\n#else\n;\n#endif\n}");
2731   verifyNoCrash("#if a\na(\n#else\n#endif\n) a {a,b,c,d,f,g};");
2732   verifyNoCrash("#ifdef A\n a(\n #else\n #endif\n) = []() {      \n)}");
2733 }
2734 
2735 TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
2736   verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline.
2737   EXPECT_EQ("class A : public QObject {\n"
2738             "  Q_OBJECT\n"
2739             "\n"
2740             "  A() {}\n"
2741             "};",
2742             format("class A  :  public QObject {\n"
2743                    "     Q_OBJECT\n"
2744                    "\n"
2745                    "  A() {\n}\n"
2746                    "}  ;"));
2747   EXPECT_EQ("SOME_MACRO\n"
2748             "namespace {\n"
2749             "void f();\n"
2750             "}",
2751             format("SOME_MACRO\n"
2752                    "  namespace    {\n"
2753                    "void   f(  );\n"
2754                    "}"));
2755   // Only if the identifier contains at least 5 characters.
2756   EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
2757   EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
2758   // Only if everything is upper case.
2759   EXPECT_EQ("class A : public QObject {\n"
2760             "  Q_Object A() {}\n"
2761             "};",
2762             format("class A  :  public QObject {\n"
2763                    "     Q_Object\n"
2764                    "  A() {\n}\n"
2765                    "}  ;"));
2766 
2767   // Only if the next line can actually start an unwrapped line.
2768   EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;",
2769             format("SOME_WEIRD_LOG_MACRO\n"
2770                    "<< SomeThing;"));
2771 
2772   verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), "
2773                "(n, buffers))\n",
2774                getChromiumStyle(FormatStyle::LK_Cpp));
2775 }
2776 
2777 TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) {
2778   EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2779             "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2780             "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2781             "class X {};\n"
2782             "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2783             "int *createScopDetectionPass() { return 0; }",
2784             format("  INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n"
2785                    "  INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n"
2786                    "  INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n"
2787                    "  class X {};\n"
2788                    "  INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n"
2789                    "  int *createScopDetectionPass() { return 0; }"));
2790   // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as
2791   // braces, so that inner block is indented one level more.
2792   EXPECT_EQ("int q() {\n"
2793             "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2794             "  IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2795             "  IPC_END_MESSAGE_MAP()\n"
2796             "}",
2797             format("int q() {\n"
2798                    "  IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n"
2799                    "    IPC_MESSAGE_HANDLER(xxx, qqq)\n"
2800                    "  IPC_END_MESSAGE_MAP()\n"
2801                    "}"));
2802 
2803   // Same inside macros.
2804   EXPECT_EQ("#define LIST(L) \\\n"
2805             "  L(A)          \\\n"
2806             "  L(B)          \\\n"
2807             "  L(C)",
2808             format("#define LIST(L) \\\n"
2809                    "  L(A) \\\n"
2810                    "  L(B) \\\n"
2811                    "  L(C)",
2812                    getGoogleStyle()));
2813 
2814   // These must not be recognized as macros.
2815   EXPECT_EQ("int q() {\n"
2816             "  f(x);\n"
2817             "  f(x) {}\n"
2818             "  f(x)->g();\n"
2819             "  f(x)->*g();\n"
2820             "  f(x).g();\n"
2821             "  f(x) = x;\n"
2822             "  f(x) += x;\n"
2823             "  f(x) -= x;\n"
2824             "  f(x) *= x;\n"
2825             "  f(x) /= x;\n"
2826             "  f(x) %= x;\n"
2827             "  f(x) &= x;\n"
2828             "  f(x) |= x;\n"
2829             "  f(x) ^= x;\n"
2830             "  f(x) >>= x;\n"
2831             "  f(x) <<= x;\n"
2832             "  f(x)[y].z();\n"
2833             "  LOG(INFO) << x;\n"
2834             "  ifstream(x) >> x;\n"
2835             "}\n",
2836             format("int q() {\n"
2837                    "  f(x)\n;\n"
2838                    "  f(x)\n {}\n"
2839                    "  f(x)\n->g();\n"
2840                    "  f(x)\n->*g();\n"
2841                    "  f(x)\n.g();\n"
2842                    "  f(x)\n = x;\n"
2843                    "  f(x)\n += x;\n"
2844                    "  f(x)\n -= x;\n"
2845                    "  f(x)\n *= x;\n"
2846                    "  f(x)\n /= x;\n"
2847                    "  f(x)\n %= x;\n"
2848                    "  f(x)\n &= x;\n"
2849                    "  f(x)\n |= x;\n"
2850                    "  f(x)\n ^= x;\n"
2851                    "  f(x)\n >>= x;\n"
2852                    "  f(x)\n <<= x;\n"
2853                    "  f(x)\n[y].z();\n"
2854                    "  LOG(INFO)\n << x;\n"
2855                    "  ifstream(x)\n >> x;\n"
2856                    "}\n"));
2857   EXPECT_EQ("int q() {\n"
2858             "  F(x)\n"
2859             "  if (1) {\n"
2860             "  }\n"
2861             "  F(x)\n"
2862             "  while (1) {\n"
2863             "  }\n"
2864             "  F(x)\n"
2865             "  G(x);\n"
2866             "  F(x)\n"
2867             "  try {\n"
2868             "    Q();\n"
2869             "  } catch (...) {\n"
2870             "  }\n"
2871             "}\n",
2872             format("int q() {\n"
2873                    "F(x)\n"
2874                    "if (1) {}\n"
2875                    "F(x)\n"
2876                    "while (1) {}\n"
2877                    "F(x)\n"
2878                    "G(x);\n"
2879                    "F(x)\n"
2880                    "try { Q(); } catch (...) {}\n"
2881                    "}\n"));
2882   EXPECT_EQ("class A {\n"
2883             "  A() : t(0) {}\n"
2884             "  A(int i) noexcept() : {}\n"
2885             "  A(X x)\n" // FIXME: function-level try blocks are broken.
2886             "  try : t(0) {\n"
2887             "  } catch (...) {\n"
2888             "  }\n"
2889             "};",
2890             format("class A {\n"
2891                    "  A()\n : t(0) {}\n"
2892                    "  A(int i)\n noexcept() : {}\n"
2893                    "  A(X x)\n"
2894                    "  try : t(0) {} catch (...) {}\n"
2895                    "};"));
2896   EXPECT_EQ("class SomeClass {\n"
2897             "public:\n"
2898             "  SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2899             "};",
2900             format("class SomeClass {\n"
2901                    "public:\n"
2902                    "  SomeClass()\n"
2903                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2904                    "};"));
2905   EXPECT_EQ("class SomeClass {\n"
2906             "public:\n"
2907             "  SomeClass()\n"
2908             "      EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2909             "};",
2910             format("class SomeClass {\n"
2911                    "public:\n"
2912                    "  SomeClass()\n"
2913                    "  EXCLUSIVE_LOCK_FUNCTION(mu_);\n"
2914                    "};",
2915                    getLLVMStyleWithColumns(40)));
2916 }
2917 
2918 TEST_F(FormatTest, LayoutMacroDefinitionsStatementsSpanningBlocks) {
2919   verifyFormat("#define A \\\n"
2920                "  f({     \\\n"
2921                "    g();  \\\n"
2922                "  });",
2923                getLLVMStyleWithColumns(11));
2924 }
2925 
2926 TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) {
2927   EXPECT_EQ("{\n  {\n#define A\n  }\n}", format("{{\n#define A\n}}"));
2928 }
2929 
2930 TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) {
2931   verifyFormat("{\n  { a #c; }\n}");
2932 }
2933 
2934 TEST_F(FormatTest, FormatUnbalancedStructuralElements) {
2935   EXPECT_EQ("#define A \\\n  {       \\\n    {\nint i;",
2936             format("#define A { {\nint i;", getLLVMStyleWithColumns(11)));
2937   EXPECT_EQ("#define A \\\n  }       \\\n  }\nint i;",
2938             format("#define A } }\nint i;", getLLVMStyleWithColumns(11)));
2939 }
2940 
2941 TEST_F(FormatTest, EscapedNewlineAtStartOfToken) {
2942   EXPECT_EQ(
2943       "#define A \\\n  int i;  \\\n  int j;",
2944       format("#define A \\\nint i;\\\n  int j;", getLLVMStyleWithColumns(11)));
2945   EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
2946 }
2947 
2948 TEST_F(FormatTest, NoEscapedNewlineHandlingInBlockComments) {
2949   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
2950 }
2951 
2952 TEST_F(FormatTest, DontCrashOnBlockComments) {
2953   EXPECT_EQ(
2954       "int xxxxxxxxx; /* "
2955       "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
2956       "zzzzzz\n"
2957       "0*/",
2958       format("int xxxxxxxxx;                          /* "
2959              "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
2960              "0*/"));
2961 }
2962 
2963 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
2964   verifyFormat("#define A \\\n"
2965                "  int v(  \\\n"
2966                "      a); \\\n"
2967                "  int i;",
2968                getLLVMStyleWithColumns(11));
2969 }
2970 
2971 TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) {
2972   EXPECT_EQ(
2973       "#define ALooooooooooooooooooooooooooooooooooooooongMacro("
2974       "                      \\\n"
2975       "    aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2976       "\n"
2977       "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2978       "    aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n",
2979       format("  #define   ALooooooooooooooooooooooooooooooooooooooongMacro("
2980              "\\\n"
2981              "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n"
2982              "  \n"
2983              "   AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n"
2984              "  aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"));
2985 }
2986 
2987 TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) {
2988   EXPECT_EQ("int\n"
2989             "#define A\n"
2990             "    a;",
2991             format("int\n#define A\na;"));
2992   verifyFormat("functionCallTo(\n"
2993                "    someOtherFunction(\n"
2994                "        withSomeParameters, whichInSequence,\n"
2995                "        areLongerThanALine(andAnotherCall,\n"
2996                "#define A B\n"
2997                "                           withMoreParamters,\n"
2998                "                           whichStronglyInfluenceTheLayout),\n"
2999                "        andMoreParameters),\n"
3000                "    trailing);",
3001                getLLVMStyleWithColumns(69));
3002   verifyFormat("Foo::Foo()\n"
3003                "#ifdef BAR\n"
3004                "    : baz(0)\n"
3005                "#endif\n"
3006                "{\n"
3007                "}");
3008   verifyFormat("void f() {\n"
3009                "  if (true)\n"
3010                "#ifdef A\n"
3011                "    f(42);\n"
3012                "  x();\n"
3013                "#else\n"
3014                "    g();\n"
3015                "  x();\n"
3016                "#endif\n"
3017                "}");
3018   verifyFormat("void f(param1, param2,\n"
3019                "       param3,\n"
3020                "#ifdef A\n"
3021                "       param4(param5,\n"
3022                "#ifdef A1\n"
3023                "              param6,\n"
3024                "#ifdef A2\n"
3025                "              param7),\n"
3026                "#else\n"
3027                "              param8),\n"
3028                "       param9,\n"
3029                "#endif\n"
3030                "       param10,\n"
3031                "#endif\n"
3032                "       param11)\n"
3033                "#else\n"
3034                "       param12)\n"
3035                "#endif\n"
3036                "{\n"
3037                "  x();\n"
3038                "}",
3039                getLLVMStyleWithColumns(28));
3040   verifyFormat("#if 1\n"
3041                "int i;");
3042   verifyFormat("#if 1\n"
3043                "#endif\n"
3044                "#if 1\n"
3045                "#else\n"
3046                "#endif\n");
3047   verifyFormat("DEBUG({\n"
3048                "  return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3049                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"
3050                "});\n"
3051                "#if a\n"
3052                "#else\n"
3053                "#endif");
3054 
3055   verifyFormat("void f(\n"
3056                "#if A\n"
3057                "    );\n"
3058                "#else\n"
3059                "#endif");
3060 }
3061 
3062 TEST_F(FormatTest, GraciouslyHandleIncorrectPreprocessorConditions) {
3063   verifyFormat("#endif\n"
3064                "#if B");
3065 }
3066 
3067 TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) {
3068   FormatStyle SingleLine = getLLVMStyle();
3069   SingleLine.AllowShortIfStatementsOnASingleLine = true;
3070   verifyFormat("#if 0\n"
3071                "#elif 1\n"
3072                "#endif\n"
3073                "void foo() {\n"
3074                "  if (test) foo2();\n"
3075                "}",
3076                SingleLine);
3077 }
3078 
3079 TEST_F(FormatTest, LayoutBlockInsideParens) {
3080   verifyFormat("functionCall({ int i; });");
3081   verifyFormat("functionCall({\n"
3082                "  int i;\n"
3083                "  int j;\n"
3084                "});");
3085   verifyFormat("functionCall({\n"
3086                "  int i;\n"
3087                "  int j;\n"
3088                "}, aaaa, bbbb, cccc);");
3089   verifyFormat("functionA(functionB({\n"
3090                "            int i;\n"
3091                "            int j;\n"
3092                "          }),\n"
3093                "          aaaa, bbbb, cccc);");
3094   verifyFormat("functionCall(\n"
3095                "    {\n"
3096                "      int i;\n"
3097                "      int j;\n"
3098                "    },\n"
3099                "    aaaa, bbbb, // comment\n"
3100                "    cccc);");
3101   verifyFormat("functionA(functionB({\n"
3102                "            int i;\n"
3103                "            int j;\n"
3104                "          }),\n"
3105                "          aaaa, bbbb, // comment\n"
3106                "          cccc);");
3107   verifyFormat("functionCall(aaaa, bbbb, { int i; });");
3108   verifyFormat("functionCall(aaaa, bbbb, {\n"
3109                "  int i;\n"
3110                "  int j;\n"
3111                "});");
3112   verifyFormat(
3113       "Aaa(\n" // FIXME: There shouldn't be a linebreak here.
3114       "    {\n"
3115       "      int i; // break\n"
3116       "    },\n"
3117       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
3118       "                                     ccccccccccccccccc));");
3119   verifyFormat("DEBUG({\n"
3120                "  if (a)\n"
3121                "    f();\n"
3122                "});");
3123 }
3124 
3125 TEST_F(FormatTest, LayoutBlockInsideStatement) {
3126   EXPECT_EQ("SOME_MACRO { int i; }\n"
3127             "int i;",
3128             format("  SOME_MACRO  {int i;}  int i;"));
3129 }
3130 
3131 TEST_F(FormatTest, LayoutNestedBlocks) {
3132   verifyFormat("void AddOsStrings(unsigned bitmask) {\n"
3133                "  struct s {\n"
3134                "    int i;\n"
3135                "  };\n"
3136                "  s kBitsToOs[] = {{10}};\n"
3137                "  for (int i = 0; i < 10; ++i)\n"
3138                "    return;\n"
3139                "}");
3140   verifyFormat("call(parameter, {\n"
3141                "  something();\n"
3142                "  // Comment using all columns.\n"
3143                "  somethingelse();\n"
3144                "});",
3145                getLLVMStyleWithColumns(40));
3146   verifyFormat("DEBUG( //\n"
3147                "    { f(); }, a);");
3148   verifyFormat("DEBUG( //\n"
3149                "    {\n"
3150                "      f(); //\n"
3151                "    },\n"
3152                "    a);");
3153 
3154   EXPECT_EQ("call(parameter, {\n"
3155             "  something();\n"
3156             "  // Comment too\n"
3157             "  // looooooooooong.\n"
3158             "  somethingElse();\n"
3159             "});",
3160             format("call(parameter, {\n"
3161                    "  something();\n"
3162                    "  // Comment too looooooooooong.\n"
3163                    "  somethingElse();\n"
3164                    "});",
3165                    getLLVMStyleWithColumns(29)));
3166   EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int   i; });"));
3167   EXPECT_EQ("DEBUG({ // comment\n"
3168             "  int i;\n"
3169             "});",
3170             format("DEBUG({ // comment\n"
3171                    "int  i;\n"
3172                    "});"));
3173   EXPECT_EQ("DEBUG({\n"
3174             "  int i;\n"
3175             "\n"
3176             "  // comment\n"
3177             "  int j;\n"
3178             "});",
3179             format("DEBUG({\n"
3180                    "  int  i;\n"
3181                    "\n"
3182                    "  // comment\n"
3183                    "  int  j;\n"
3184                    "});"));
3185 
3186   verifyFormat("DEBUG({\n"
3187                "  if (a)\n"
3188                "    return;\n"
3189                "});");
3190   verifyGoogleFormat("DEBUG({\n"
3191                      "  if (a) return;\n"
3192                      "});");
3193   FormatStyle Style = getGoogleStyle();
3194   Style.ColumnLimit = 45;
3195   verifyFormat("Debug(aaaaa, {\n"
3196                "  if (aaaaaaaaaaaaaaaaaaaaaaaa) return;\n"
3197                "}, a);",
3198                Style);
3199 
3200   verifyNoCrash("^{v^{a}}");
3201 }
3202 
3203 TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
3204   EXPECT_EQ("DEBUG({\n"
3205             "  int i;\n"
3206             "  int        j;\n"
3207             "});",
3208             format("DEBUG(   {\n"
3209                    "  int        i;\n"
3210                    "  int        j;\n"
3211                    "}   )  ;",
3212                    20, 1, getLLVMStyle()));
3213   EXPECT_EQ("DEBUG(   {\n"
3214             "  int        i;\n"
3215             "  int j;\n"
3216             "}   )  ;",
3217             format("DEBUG(   {\n"
3218                    "  int        i;\n"
3219                    "  int        j;\n"
3220                    "}   )  ;",
3221                    41, 1, getLLVMStyle()));
3222   EXPECT_EQ("DEBUG(   {\n"
3223             "    int        i;\n"
3224             "    int j;\n"
3225             "}   )  ;",
3226             format("DEBUG(   {\n"
3227                    "    int        i;\n"
3228                    "    int        j;\n"
3229                    "}   )  ;",
3230                    41, 1, getLLVMStyle()));
3231   EXPECT_EQ("DEBUG({\n"
3232             "  int i;\n"
3233             "  int j;\n"
3234             "});",
3235             format("DEBUG(   {\n"
3236                    "    int        i;\n"
3237                    "    int        j;\n"
3238                    "}   )  ;",
3239                    20, 1, getLLVMStyle()));
3240 
3241   EXPECT_EQ("Debug({\n"
3242             "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
3243             "          return;\n"
3244             "      },\n"
3245             "      a);",
3246             format("Debug({\n"
3247                    "        if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
3248                    "             return;\n"
3249                    "      },\n"
3250                    "      a);",
3251                    50, 1, getLLVMStyle()));
3252   EXPECT_EQ("DEBUG({\n"
3253             "  DEBUG({\n"
3254             "    int a;\n"
3255             "    int b;\n"
3256             "  }) ;\n"
3257             "});",
3258             format("DEBUG({\n"
3259                    "  DEBUG({\n"
3260                    "    int a;\n"
3261                    "    int    b;\n" // Format this line only.
3262                    "  }) ;\n"        // Don't touch this line.
3263                    "});",
3264                    35, 0, getLLVMStyle()));
3265   EXPECT_EQ("DEBUG({\n"
3266             "  int a; //\n"
3267             "});",
3268             format("DEBUG({\n"
3269                    "    int a; //\n"
3270                    "});",
3271                    0, 0, getLLVMStyle()));
3272   EXPECT_EQ("someFunction(\n"
3273             "    [] {\n"
3274             "      // Only with this comment.\n"
3275             "      int i; // invoke formatting here.\n"
3276             "    }, // force line break\n"
3277             "    aaa);",
3278             format("someFunction(\n"
3279                    "    [] {\n"
3280                    "      // Only with this comment.\n"
3281                    "      int   i; // invoke formatting here.\n"
3282                    "    }, // force line break\n"
3283                    "    aaa);",
3284                    63, 1, getLLVMStyle()));
3285 }
3286 
3287 TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
3288   EXPECT_EQ("{}", format("{}"));
3289   verifyFormat("enum E {};");
3290   verifyFormat("enum E {}");
3291 }
3292 
3293 //===----------------------------------------------------------------------===//
3294 // Line break tests.
3295 //===----------------------------------------------------------------------===//
3296 
3297 TEST_F(FormatTest, PreventConfusingIndents) {
3298   verifyFormat(
3299       "void f() {\n"
3300       "  SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n"
3301       "                         parameter, parameter, parameter)),\n"
3302       "                     SecondLongCall(parameter));\n"
3303       "}");
3304   verifyFormat(
3305       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3306       "    aaaaaaaaaaaaaaaaaaaaaaaa(\n"
3307       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3308       "    aaaaaaaaaaaaaaaaaaaaaaaa);");
3309   verifyFormat(
3310       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3311       "    [aaaaaaaaaaaaaaaaaaaaaaaa\n"
3312       "         [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
3313       "         [aaaaaaaaaaaaaaaaaaaaaaaa]];");
3314   verifyFormat(
3315       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
3316       "    aaaaaaaaaaaaaaaaaaaaaaaa<\n"
3317       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n"
3318       "    aaaaaaaaaaaaaaaaaaaaaaaa>;");
3319   verifyFormat("int a = bbbb && ccc && fffff(\n"
3320                "#define A Just forcing a new line\n"
3321                "                           ddd);");
3322 }
3323 
3324 TEST_F(FormatTest, LineBreakingInBinaryExpressions) {
3325   verifyFormat(
3326       "bool aaaaaaa =\n"
3327       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() ||\n"
3328       "    bbbbbbbb();");
3329   verifyFormat(
3330       "bool aaaaaaa =\n"
3331       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaa).aaaaaaaaaaaaaaaaaaa() or\n"
3332       "    bbbbbbbb();");
3333 
3334   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3335                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb &&\n"
3336                "    ccccccccc == ddddddddddd;");
3337   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaa =\n"
3338                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbb and\n"
3339                "    ccccccccc == ddddddddddd;");
3340   verifyFormat(
3341       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
3342       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa not_eq bbbbbbbbbbbbbbbbbb and\n"
3343       "    ccccccccc == ddddddddddd;");
3344 
3345   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3346                "                 aaaaaa) &&\n"
3347                "         bbbbbb && cccccc;");
3348   verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n"
3349                "                 aaaaaa) >>\n"
3350                "         bbbbbb;");
3351   verifyFormat("Whitespaces.addUntouchableComment(\n"
3352                "    SourceMgr.getSpellingColumnNumber(\n"
3353                "        TheLine.Last->FormatTok.Tok.getLocation()) -\n"
3354                "    1);");
3355 
3356   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3357                "     bbbbbbbbbbbbbbbbbb) && // aaaaaaaaaaaaaaaa\n"
3358                "    cccccc) {\n}");
3359   verifyFormat("b = a &&\n"
3360                "    // Comment\n"
3361                "    b.c && d;");
3362 
3363   // If the LHS of a comparison is not a binary expression itself, the
3364   // additional linebreak confuses many people.
3365   verifyFormat(
3366       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3367       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) > 5) {\n"
3368       "}");
3369   verifyFormat(
3370       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3371       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3372       "}");
3373   verifyFormat(
3374       "if (aaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaa(\n"
3375       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3376       "}");
3377   // Even explicit parentheses stress the precedence enough to make the
3378   // additional break unnecessary.
3379   verifyFormat("if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3380                "     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) == 5) {\n"
3381                "}");
3382   // This cases is borderline, but with the indentation it is still readable.
3383   verifyFormat(
3384       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3385       "        aaaaaaaaaaaaaaa) > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3386       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
3387       "}",
3388       getLLVMStyleWithColumns(75));
3389 
3390   // If the LHS is a binary expression, we should still use the additional break
3391   // as otherwise the formatting hides the operator precedence.
3392   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3393                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3394                "    5) {\n"
3395                "}");
3396 
3397   FormatStyle OnePerLine = getLLVMStyle();
3398   OnePerLine.BinPackParameters = false;
3399   verifyFormat(
3400       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3401       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3402       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}",
3403       OnePerLine);
3404 }
3405 
3406 TEST_F(FormatTest, ExpressionIndentation) {
3407   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3408                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3409                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3410                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3411                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +\n"
3412                "                     bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb &&\n"
3413                "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3414                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >\n"
3415                "                 ccccccccccccccccccccccccccccccccccccccccc;");
3416   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3417                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3418                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3419                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3420   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3421                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3422                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3423                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3424   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ==\n"
3425                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n"
3426                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3427                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
3428   verifyFormat("if () {\n"
3429                "} else if (aaaaa &&\n"
3430                "           bbbbb > // break\n"
3431                "               ccccc) {\n"
3432                "}");
3433 
3434   // Presence of a trailing comment used to change indentation of b.
3435   verifyFormat("return aaaaaaaaaaaaaaaaaaa +\n"
3436                "       b;\n"
3437                "return aaaaaaaaaaaaaaaaaaa +\n"
3438                "       b; //",
3439                getLLVMStyleWithColumns(30));
3440 }
3441 
3442 TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) {
3443   // Not sure what the best system is here. Like this, the LHS can be found
3444   // immediately above an operator (everything with the same or a higher
3445   // indent). The RHS is aligned right of the operator and so compasses
3446   // everything until something with the same indent as the operator is found.
3447   // FIXME: Is this a good system?
3448   FormatStyle Style = getLLVMStyle();
3449   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
3450   verifyFormat(
3451       "bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3452       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3453       "                     + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3454       "                 == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3455       "                            * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3456       "                        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3457       "             && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3458       "                        * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3459       "                    > ccccccccccccccccccccccccccccccccccccccccc;",
3460       Style);
3461   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3462                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3463                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3464                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3465                Style);
3466   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3467                "        + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3468                "              * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3469                "    == bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3470                Style);
3471   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3472                "    == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3473                "               * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3474                "           + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}",
3475                Style);
3476   verifyFormat("if () {\n"
3477                "} else if (aaaaa\n"
3478                "           && bbbbb // break\n"
3479                "                  > ccccc) {\n"
3480                "}",
3481                Style);
3482   verifyFormat("return (a)\n"
3483                "       // comment\n"
3484                "       + b;",
3485                Style);
3486   verifyFormat(
3487       "int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3488       "                 * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3489       "             + cc;",
3490       Style);
3491 
3492   // Forced by comments.
3493   verifyFormat(
3494       "unsigned ContentSize =\n"
3495       "    sizeof(int16_t)   // DWARF ARange version number\n"
3496       "    + sizeof(int32_t) // Offset of CU in the .debug_info section\n"
3497       "    + sizeof(int8_t)  // Pointer Size (in bytes)\n"
3498       "    + sizeof(int8_t); // Segment Size (in bytes)");
3499 
3500   verifyFormat("return boost::fusion::at_c<0>(iiii).second\n"
3501                "       == boost::fusion::at_c<1>(iiii).second;",
3502                Style);
3503 
3504   Style.ColumnLimit = 60;
3505   verifyFormat("zzzzzzzzzz\n"
3506                "    = bbbbbbbbbbbbbbbbb\n"
3507                "      >> aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa);",
3508                Style);
3509 }
3510 
3511 TEST_F(FormatTest, NoOperandAlignment) {
3512   FormatStyle Style = getLLVMStyle();
3513   Style.AlignOperands = false;
3514   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3515   verifyFormat("bool value = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3516                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3517                "            + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3518                "        == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3519                "                * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3520                "            + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3521                "    && aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3522                "            * aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3523                "        > ccccccccccccccccccccccccccccccccccccccccc;",
3524                Style);
3525 
3526   verifyFormat("int aaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3527                "        * bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3528                "    + cc;",
3529                Style);
3530   verifyFormat("int a = aa\n"
3531                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n"
3532                "        * cccccccccccccccccccccccccccccccccccc;",
3533                Style);
3534 
3535   Style.AlignAfterOpenBracket = false;
3536   verifyFormat("return (a > b\n"
3537                "    // comment1\n"
3538                "    // comment2\n"
3539                "    || c);",
3540                Style);
3541 }
3542 
3543 TEST_F(FormatTest, BreakingBeforeNonAssigmentOperators) {
3544   FormatStyle Style = getLLVMStyle();
3545   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_NonAssignment;
3546   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
3547                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3548                "    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;",
3549                Style);
3550 }
3551 
3552 TEST_F(FormatTest, ConstructorInitializers) {
3553   verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}");
3554   verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}",
3555                getLLVMStyleWithColumns(45));
3556   verifyFormat("Constructor()\n"
3557                "    : Inttializer(FitsOnTheLine) {}",
3558                getLLVMStyleWithColumns(44));
3559   verifyFormat("Constructor()\n"
3560                "    : Inttializer(FitsOnTheLine) {}",
3561                getLLVMStyleWithColumns(43));
3562 
3563   verifyFormat(
3564       "SomeClass::Constructor()\n"
3565       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3566 
3567   verifyFormat(
3568       "SomeClass::Constructor()\n"
3569       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3570       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}");
3571   verifyFormat(
3572       "SomeClass::Constructor()\n"
3573       "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3574       "      aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}");
3575 
3576   verifyFormat("Constructor()\n"
3577                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3578                "      aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3579                "                               aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
3580                "      aaaaaaaaaaaaaaaaaaaaaaa() {}");
3581 
3582   verifyFormat("Constructor()\n"
3583                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3584                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3585 
3586   verifyFormat("Constructor(int Parameter = 0)\n"
3587                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n"
3588                "      aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}");
3589   verifyFormat("Constructor()\n"
3590                "    : aaaaaaaaaaaaaaaaaaaa(a), bbbbbbbbbbbbbbbbbbbbbbbb(b) {\n"
3591                "}",
3592                getLLVMStyleWithColumns(60));
3593   verifyFormat("Constructor()\n"
3594                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3595                "          aaaaaaaaaaaaaaaaaaaaaaaaa(aaaa, aaaa)) {}");
3596 
3597   // Here a line could be saved by splitting the second initializer onto two
3598   // lines, but that is not desirable.
3599   verifyFormat("Constructor()\n"
3600                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n"
3601                "      aaaaaaaaaaa(aaaaaaaaaaa),\n"
3602                "      aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3603 
3604   FormatStyle OnePerLine = getLLVMStyle();
3605   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3606   verifyFormat("SomeClass::Constructor()\n"
3607                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3608                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3609                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3610                OnePerLine);
3611   verifyFormat("SomeClass::Constructor()\n"
3612                "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n"
3613                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
3614                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
3615                OnePerLine);
3616   verifyFormat("MyClass::MyClass(int var)\n"
3617                "    : some_var_(var),            // 4 space indent\n"
3618                "      some_other_var_(var + 1) { // lined up\n"
3619                "}",
3620                OnePerLine);
3621   verifyFormat("Constructor()\n"
3622                "    : aaaaa(aaaaaa),\n"
3623                "      aaaaa(aaaaaa),\n"
3624                "      aaaaa(aaaaaa),\n"
3625                "      aaaaa(aaaaaa),\n"
3626                "      aaaaa(aaaaaa) {}",
3627                OnePerLine);
3628   verifyFormat("Constructor()\n"
3629                "    : aaaaa(aaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaa,\n"
3630                "            aaaaaaaaaaaaaaaaaaaaaa) {}",
3631                OnePerLine);
3632 
3633   EXPECT_EQ("Constructor()\n"
3634             "    : // Comment forcing unwanted break.\n"
3635             "      aaaa(aaaa) {}",
3636             format("Constructor() :\n"
3637                    "    // Comment forcing unwanted break.\n"
3638                    "    aaaa(aaaa) {}"));
3639 }
3640 
3641 TEST_F(FormatTest, MemoizationTests) {
3642   // This breaks if the memoization lookup does not take \c Indent and
3643   // \c LastSpace into account.
3644   verifyFormat(
3645       "extern CFRunLoopTimerRef\n"
3646       "CFRunLoopTimerCreate(CFAllocatorRef allocato, CFAbsoluteTime fireDate,\n"
3647       "                     CFTimeInterval interval, CFOptionFlags flags,\n"
3648       "                     CFIndex order, CFRunLoopTimerCallBack callout,\n"
3649       "                     CFRunLoopTimerContext *context) {}");
3650 
3651   // Deep nesting somewhat works around our memoization.
3652   verifyFormat(
3653       "aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3654       "    aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3655       "        aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3656       "            aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(aaaaa(\n"
3657       "                aaaaa())))))))))))))))))))))))))))))))))))))));",
3658       getLLVMStyleWithColumns(65));
3659   verifyFormat(
3660       "aaaaa(\n"
3661       "    aaaaa,\n"
3662       "    aaaaa(\n"
3663       "        aaaaa,\n"
3664       "        aaaaa(\n"
3665       "            aaaaa,\n"
3666       "            aaaaa(\n"
3667       "                aaaaa,\n"
3668       "                aaaaa(\n"
3669       "                    aaaaa,\n"
3670       "                    aaaaa(\n"
3671       "                        aaaaa,\n"
3672       "                        aaaaa(\n"
3673       "                            aaaaa,\n"
3674       "                            aaaaa(\n"
3675       "                                aaaaa,\n"
3676       "                                aaaaa(\n"
3677       "                                    aaaaa,\n"
3678       "                                    aaaaa(\n"
3679       "                                        aaaaa,\n"
3680       "                                        aaaaa(\n"
3681       "                                            aaaaa,\n"
3682       "                                            aaaaa(\n"
3683       "                                                aaaaa,\n"
3684       "                                                aaaaa))))))))))));",
3685       getLLVMStyleWithColumns(65));
3686   verifyFormat(
3687       "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"
3688       "                                  a),\n"
3689       "                                a),\n"
3690       "                              a),\n"
3691       "                            a),\n"
3692       "                          a),\n"
3693       "                        a),\n"
3694       "                      a),\n"
3695       "                    a),\n"
3696       "                  a),\n"
3697       "                a),\n"
3698       "              a),\n"
3699       "            a),\n"
3700       "          a),\n"
3701       "        a),\n"
3702       "      a),\n"
3703       "    a),\n"
3704       "  a)",
3705       getLLVMStyleWithColumns(65));
3706 
3707   // This test takes VERY long when memoization is broken.
3708   FormatStyle OnePerLine = getLLVMStyle();
3709   OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
3710   OnePerLine.BinPackParameters = false;
3711   std::string input = "Constructor()\n"
3712                       "    : aaaa(a,\n";
3713   for (unsigned i = 0, e = 80; i != e; ++i) {
3714     input += "           a,\n";
3715   }
3716   input += "           a) {}";
3717   verifyFormat(input, OnePerLine);
3718 }
3719 
3720 TEST_F(FormatTest, BreaksAsHighAsPossible) {
3721   verifyFormat(
3722       "void f() {\n"
3723       "  if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n"
3724       "      (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n"
3725       "    f();\n"
3726       "}");
3727   verifyFormat("if (Intervals[i].getRange().getFirst() <\n"
3728                "    Intervals[i - 1].getRange().getLast()) {\n}");
3729 }
3730 
3731 TEST_F(FormatTest, BreaksFunctionDeclarations) {
3732   // Principially, we break function declarations in a certain order:
3733   // 1) break amongst arguments.
3734   verifyFormat("Aaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccc,\n"
3735                "                              Cccccccccccccc cccccccccccccc);");
3736   verifyFormat("template <class TemplateIt>\n"
3737                "SomeReturnType SomeFunction(TemplateIt begin, TemplateIt end,\n"
3738                "                            TemplateIt *stop) {}");
3739 
3740   // 2) break after return type.
3741   verifyFormat(
3742       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3743       "bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);",
3744       getGoogleStyle());
3745 
3746   // 3) break after (.
3747   verifyFormat(
3748       "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n"
3749       "    Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);",
3750       getGoogleStyle());
3751 
3752   // 4) break before after nested name specifiers.
3753   verifyFormat(
3754       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3755       "SomeClasssssssssssssssssssssssssssssssssssssss::\n"
3756       "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);",
3757       getGoogleStyle());
3758 
3759   // However, there are exceptions, if a sufficient amount of lines can be
3760   // saved.
3761   // FIXME: The precise cut-offs wrt. the number of saved lines might need some
3762   // more adjusting.
3763   verifyFormat("Aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3764                "                                  Cccccccccccccc cccccccccc,\n"
3765                "                                  Cccccccccccccc cccccccccc,\n"
3766                "                                  Cccccccccccccc cccccccccc,\n"
3767                "                                  Cccccccccccccc cccccccccc);");
3768   verifyFormat(
3769       "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3770       "bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3771       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3772       "            Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);",
3773       getGoogleStyle());
3774   verifyFormat(
3775       "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n"
3776       "                                          Cccccccccccccc cccccccccc,\n"
3777       "                                          Cccccccccccccc cccccccccc,\n"
3778       "                                          Cccccccccccccc cccccccccc,\n"
3779       "                                          Cccccccccccccc cccccccccc,\n"
3780       "                                          Cccccccccccccc cccccccccc,\n"
3781       "                                          Cccccccccccccc cccccccccc);");
3782   verifyFormat("Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3783                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3784                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3785                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n"
3786                "    Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);");
3787 
3788   // Break after multi-line parameters.
3789   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3790                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3791                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3792                "    bbbb bbbb);");
3793   verifyFormat("void SomeLoooooooooooongFunction(\n"
3794                "    std::unique_ptr<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
3795                "        aaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3796                "    int bbbbbbbbbbbbb);");
3797 
3798   // Treat overloaded operators like other functions.
3799   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3800                "operator>(const SomeLoooooooooooooooooooooooooogType &other);");
3801   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3802                "operator>>(const SomeLooooooooooooooooooooooooogType &other);");
3803   verifyFormat("SomeLoooooooooooooooooooooooooogType\n"
3804                "operator<<(const SomeLooooooooooooooooooooooooogType &other);");
3805   verifyGoogleFormat(
3806       "SomeLoooooooooooooooooooooooooooooogType operator>>(\n"
3807       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3808   verifyGoogleFormat(
3809       "SomeLoooooooooooooooooooooooooooooogType operator<<(\n"
3810       "    const SomeLooooooooogType &a, const SomeLooooooooogType &b);");
3811   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3812                "    int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3813   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa\n"
3814                "aaaaaaaaaaaaaaaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaa = 1);");
3815   verifyGoogleFormat(
3816       "typename aaaaaaaaaa<aaaaaa>::aaaaaaaaaaa\n"
3817       "aaaaaaaaaa<aaaaaa>::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3818       "    bool *aaaaaaaaaaaaaaaaaa, bool *aa) {}");
3819 
3820   FormatStyle Style = getLLVMStyle();
3821   Style.PointerAlignment = FormatStyle::PAS_Left;
3822   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3823                "    aaaaaaaaaaaaaaaaaaaaaaaaa* const aaaaaaaaaaaa) {}",
3824                Style);
3825   verifyFormat("void aaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n"
3826                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
3827                Style);
3828 }
3829 
3830 TEST_F(FormatTest, TrailingReturnType) {
3831   verifyFormat("auto foo() -> int;\n");
3832   verifyFormat("struct S {\n"
3833                "  auto bar() const -> int;\n"
3834                "};");
3835   verifyFormat("template <size_t Order, typename T>\n"
3836                "auto load_img(const std::string &filename)\n"
3837                "    -> alias::tensor<Order, T, mem::tag::cpu> {}");
3838   verifyFormat("auto SomeFunction(A aaaaaaaaaaaaaaaaaaaaa) const\n"
3839                "    -> decltype(f(aaaaaaaaaaaaaaaaaaaaa)) {}");
3840   verifyFormat("auto doSomething(Aaaaaa *aaaaaa) -> decltype(aaaaaa->f()) {}");
3841 
3842   // Not trailing return types.
3843   verifyFormat("void f() { auto a = b->c(); }");
3844 }
3845 
3846 TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) {
3847   // Avoid breaking before trailing 'const' or other trailing annotations, if
3848   // they are not function-like.
3849   FormatStyle Style = getGoogleStyle();
3850   Style.ColumnLimit = 47;
3851   verifyFormat("void someLongFunction(\n"
3852                "    int someLoooooooooooooongParameter) const {\n}",
3853                getLLVMStyleWithColumns(47));
3854   verifyFormat("LoooooongReturnType\n"
3855                "someLoooooooongFunction() const {}",
3856                getLLVMStyleWithColumns(47));
3857   verifyFormat("LoooooongReturnType someLoooooooongFunction()\n"
3858                "    const {}",
3859                Style);
3860   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3861                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE;");
3862   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3863                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) OVERRIDE FINAL;");
3864   verifyFormat("void SomeFunction(aaaaa aaaaaaaaaaaaaaaaaaaa,\n"
3865                "                  aaaaa aaaaaaaaaaaaaaaaaaaa) override final;");
3866   verifyFormat("virtual void aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa,\n"
3867                "                   aaaaaaaaaaa aaaaa) const override;");
3868   verifyGoogleFormat(
3869       "virtual void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
3870       "    const override;");
3871 
3872   // Even if the first parameter has to be wrapped.
3873   verifyFormat("void someLongFunction(\n"
3874                "    int someLongParameter) const {}",
3875                getLLVMStyleWithColumns(46));
3876   verifyFormat("void someLongFunction(\n"
3877                "    int someLongParameter) const {}",
3878                Style);
3879   verifyFormat("void someLongFunction(\n"
3880                "    int someLongParameter) override {}",
3881                Style);
3882   verifyFormat("void someLongFunction(\n"
3883                "    int someLongParameter) OVERRIDE {}",
3884                Style);
3885   verifyFormat("void someLongFunction(\n"
3886                "    int someLongParameter) final {}",
3887                Style);
3888   verifyFormat("void someLongFunction(\n"
3889                "    int someLongParameter) FINAL {}",
3890                Style);
3891   verifyFormat("void someLongFunction(\n"
3892                "    int parameter) const override {}",
3893                Style);
3894 
3895   Style.BreakBeforeBraces = FormatStyle::BS_Allman;
3896   verifyFormat("void someLongFunction(\n"
3897                "    int someLongParameter) const\n"
3898                "{\n"
3899                "}",
3900                Style);
3901 
3902   // Unless these are unknown annotations.
3903   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
3904                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3905                "    LONG_AND_UGLY_ANNOTATION;");
3906 
3907   // Breaking before function-like trailing annotations is fine to keep them
3908   // close to their arguments.
3909   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
3910                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3911   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3912                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa);");
3913   verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n"
3914                "    LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}");
3915   verifyGoogleFormat("void aaaaaaaaaaaaaa(aaaaaaaa aaa) override\n"
3916                      "    AAAAAAAAAAAAAAAAAAAAAAAA(aaaaaaaaaaaaaaa);");
3917   verifyFormat("SomeFunction([](int i) LOCKS_EXCLUDED(a) {});");
3918 
3919   verifyFormat(
3920       "void aaaaaaaaaaaaaaaaaa()\n"
3921       "    __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n"
3922       "                   aaaaaaaaaaaaaaaaaaaaaaaaa));");
3923   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3924                "    __attribute__((unused));");
3925   verifyGoogleFormat(
3926       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3927       "    GUARDED_BY(aaaaaaaaaaaa);");
3928   verifyGoogleFormat(
3929       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
3930       "    GUARDED_BY(aaaaaaaaaaaa);");
3931   verifyGoogleFormat(
3932       "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa GUARDED_BY(aaaaaaaaaaaa) =\n"
3933       "    aaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
3934 }
3935 
3936 TEST_F(FormatTest, BreaksDesireably) {
3937   verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3938                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n"
3939                "    aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}");
3940   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3941                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n"
3942                "}");
3943 
3944   verifyFormat(
3945       "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3946       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}");
3947 
3948   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3949                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3950                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3951 
3952   verifyFormat(
3953       "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3954       "                            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
3955       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3956       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));");
3957 
3958   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
3959                "    (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3960 
3961   verifyFormat(
3962       "void f() {\n"
3963       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n"
3964       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
3965       "}");
3966   verifyFormat(
3967       "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3968       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3969   verifyFormat(
3970       "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
3971       "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));");
3972   verifyFormat("aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
3973                "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
3974                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3975 
3976   // Indent consistently independent of call expression.
3977   verifyFormat("aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccccc(\n"
3978                "    dddddddddddddddddddddddddddddd));\n"
3979                "aaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(\n"
3980                "    dddddddddddddddddddddddddddddd));");
3981 
3982   // This test case breaks on an incorrect memoization, i.e. an optimization not
3983   // taking into account the StopAt value.
3984   verifyFormat(
3985       "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3986       "       aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3987       "       aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n"
3988       "       (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
3989 
3990   verifyFormat("{\n  {\n    {\n"
3991                "      Annotation.SpaceRequiredBefore =\n"
3992                "          Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n"
3993                "          Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n"
3994                "    }\n  }\n}");
3995 
3996   // Break on an outer level if there was a break on an inner level.
3997   EXPECT_EQ("f(g(h(a, // comment\n"
3998             "      b, c),\n"
3999             "    d, e),\n"
4000             "  x, y);",
4001             format("f(g(h(a, // comment\n"
4002                    "    b, c), d, e), x, y);"));
4003 
4004   // Prefer breaking similar line breaks.
4005   verifyFormat(
4006       "const int kTrackingOptions = NSTrackingMouseMoved |\n"
4007       "                             NSTrackingMouseEnteredAndExited |\n"
4008       "                             NSTrackingActiveAlways;");
4009 }
4010 
4011 TEST_F(FormatTest, FormatsDeclarationsOnePerLine) {
4012   FormatStyle NoBinPacking = getGoogleStyle();
4013   NoBinPacking.BinPackParameters = false;
4014   NoBinPacking.BinPackArguments = true;
4015   verifyFormat("void f() {\n"
4016                "  f(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaa,\n"
4017                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4018                "}",
4019                NoBinPacking);
4020   verifyFormat("void f(int aaaaaaaaaaaaaaaaaaaa,\n"
4021                "       int aaaaaaaaaaaaaaaaaaaa,\n"
4022                "       int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4023                NoBinPacking);
4024 }
4025 
4026 TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) {
4027   FormatStyle NoBinPacking = getGoogleStyle();
4028   NoBinPacking.BinPackParameters = false;
4029   NoBinPacking.BinPackArguments = false;
4030   verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n"
4031                "  aaaaaaaaaaaaaaaaaaaa,\n"
4032                "  aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);",
4033                NoBinPacking);
4034   verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n"
4035                "        aaaaaaaaaaaaa,\n"
4036                "        aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));",
4037                NoBinPacking);
4038   verifyFormat(
4039       "aaaaaaaa(aaaaaaaaaaaaa,\n"
4040       "         aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4041       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n"
4042       "         aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4043       "             aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));",
4044       NoBinPacking);
4045   verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4046                "    .aaaaaaaaaaaaaaaaaa();",
4047                NoBinPacking);
4048   verifyFormat("void f() {\n"
4049                "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4050                "      aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);\n"
4051                "}",
4052                NoBinPacking);
4053 
4054   verifyFormat(
4055       "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4056       "             aaaaaaaaaaaa,\n"
4057       "             aaaaaaaaaaaa);",
4058       NoBinPacking);
4059   verifyFormat(
4060       "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n"
4061       "                               ddddddddddddddddddddddddddddd),\n"
4062       "             test);",
4063       NoBinPacking);
4064 
4065   verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n"
4066                "            aaaaaaaaaaaaaaaaaaaaaaa,\n"
4067                "            aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;",
4068                NoBinPacking);
4069   verifyFormat("a(\"a\"\n"
4070                "  \"a\",\n"
4071                "  a);");
4072 
4073   NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false;
4074   verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n"
4075                "                aaaaaaaaa,\n"
4076                "                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4077                NoBinPacking);
4078   verifyFormat(
4079       "void f() {\n"
4080       "  aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n"
4081       "      .aaaaaaa();\n"
4082       "}",
4083       NoBinPacking);
4084   verifyFormat(
4085       "template <class SomeType, class SomeOtherType>\n"
4086       "SomeType SomeFunction(SomeType Type, SomeOtherType OtherType) {}",
4087       NoBinPacking);
4088 }
4089 
4090 TEST_F(FormatTest, AdaptiveOnePerLineFormatting) {
4091   FormatStyle Style = getLLVMStyleWithColumns(15);
4092   Style.ExperimentalAutoDetectBinPacking = true;
4093   EXPECT_EQ("aaa(aaaa,\n"
4094             "    aaaa,\n"
4095             "    aaaa);\n"
4096             "aaa(aaaa,\n"
4097             "    aaaa,\n"
4098             "    aaaa);",
4099             format("aaa(aaaa,\n" // one-per-line
4100                    "  aaaa,\n"
4101                    "    aaaa  );\n"
4102                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4103                    Style));
4104   EXPECT_EQ("aaa(aaaa, aaaa,\n"
4105             "    aaaa);\n"
4106             "aaa(aaaa, aaaa,\n"
4107             "    aaaa);",
4108             format("aaa(aaaa,  aaaa,\n" // bin-packed
4109                    "    aaaa  );\n"
4110                    "aaa(aaaa,  aaaa,  aaaa);", // inconclusive
4111                    Style));
4112 }
4113 
4114 TEST_F(FormatTest, FormatsBuilderPattern) {
4115   verifyFormat("return llvm::StringSwitch<Reference::Kind>(name)\n"
4116                "    .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n"
4117                "    .StartsWith(\".eh_frame\", ORDER_EH_FRAME)\n"
4118                "    .StartsWith(\".init\", ORDER_INIT)\n"
4119                "    .StartsWith(\".fini\", ORDER_FINI)\n"
4120                "    .StartsWith(\".hash\", ORDER_HASH)\n"
4121                "    .Default(ORDER_TEXT);\n");
4122 
4123   verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n"
4124                "       aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();");
4125   verifyFormat(
4126       "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n"
4127       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4128       "    ->aaaaaaaa(aaaaaaaaaaaaaaa);");
4129   verifyFormat(
4130       "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
4131       "    aaaaaaaaaaaaaa);");
4132   verifyFormat(
4133       "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa =\n"
4134       "    aaaaaa->aaaaaaaaaaaa()\n"
4135       "        ->aaaaaaaaaaaaaaaa(\n"
4136       "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4137       "        ->aaaaaaaaaaaaaaaaa();");
4138   verifyGoogleFormat(
4139       "void f() {\n"
4140       "  someo->Add((new util::filetools::Handler(dir))\n"
4141       "                 ->OnEvent1(NewPermanentCallback(\n"
4142       "                     this, &HandlerHolderClass::EventHandlerCBA))\n"
4143       "                 ->OnEvent2(NewPermanentCallback(\n"
4144       "                     this, &HandlerHolderClass::EventHandlerCBB))\n"
4145       "                 ->OnEvent3(NewPermanentCallback(\n"
4146       "                     this, &HandlerHolderClass::EventHandlerCBC))\n"
4147       "                 ->OnEvent5(NewPermanentCallback(\n"
4148       "                     this, &HandlerHolderClass::EventHandlerCBD))\n"
4149       "                 ->OnEvent6(NewPermanentCallback(\n"
4150       "                     this, &HandlerHolderClass::EventHandlerCBE)));\n"
4151       "}");
4152 
4153   verifyFormat(
4154       "aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa().aaaaaaaaaaa();");
4155   verifyFormat("aaaaaaaaaaaaaaa()\n"
4156                "    .aaaaaaaaaaaaaaa()\n"
4157                "    .aaaaaaaaaaaaaaa()\n"
4158                "    .aaaaaaaaaaaaaaa()\n"
4159                "    .aaaaaaaaaaaaaaa();");
4160   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4161                "    .aaaaaaaaaaaaaaa()\n"
4162                "    .aaaaaaaaaaaaaaa()\n"
4163                "    .aaaaaaaaaaaaaaa();");
4164   verifyFormat("aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4165                "    .aaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa()\n"
4166                "    .aaaaaaaaaaaaaaa();");
4167   verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
4168                "    ->aaaaaaaaaaaaaae(0)\n"
4169                "    ->aaaaaaaaaaaaaaa();");
4170 
4171   // Don't linewrap after very short segments.
4172   verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4173                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4174                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4175   verifyFormat("aa().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4176                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4177                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4178   verifyFormat("aaa()\n"
4179                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4180                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4181                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4182 
4183   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4184                "    .aaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
4185                "    .has<bbbbbbbbbbbbbbbbbbbbb>();");
4186   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaa()\n"
4187                "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
4188                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>();");
4189 
4190   // Prefer not to break after empty parentheses.
4191   verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n"
4192                "    First->LastNewlineOffset);");
4193 }
4194 
4195 TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) {
4196   verifyFormat(
4197       "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
4198       "    bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}");
4199   verifyFormat(
4200       "if (aaaaaaaaaaaaaaaaaaaaaaaaa or\n"
4201       "    bbbbbbbbbbbbbbbbbbbbbbbbb and cccccccccccccccccccccccc) {\n}");
4202 
4203   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4204                "    ccccccccccccccccccccccccc) {\n}");
4205   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa and bbbbbbbbbbbbbbbbbbbbbbbb or\n"
4206                "    ccccccccccccccccccccccccc) {\n}");
4207 
4208   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n"
4209                "    ccccccccccccccccccccccccc) {\n}");
4210   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb or\n"
4211                "    ccccccccccccccccccccccccc) {\n}");
4212 
4213   verifyFormat(
4214       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n"
4215       "    ccccccccccccccccccccccccc) {\n}");
4216   verifyFormat(
4217       "if ((aaaaaaaaaaaaaaaaaaaaaaaaa or bbbbbbbbbbbbbbbbbbbbbbbbb) and\n"
4218       "    ccccccccccccccccccccccccc) {\n}");
4219 
4220   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n"
4221                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n"
4222                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n"
4223                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4224   verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA or\n"
4225                "       bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB or\n"
4226                "       cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC or\n"
4227                "       dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;");
4228 
4229   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n"
4230                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n"
4231                "    aaaaaaaaaaaaaaa != aa) {\n}");
4232   verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa or\n"
4233                "     aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) and\n"
4234                "    aaaaaaaaaaaaaaa != aa) {\n}");
4235 }
4236 
4237 TEST_F(FormatTest, BreaksAfterAssignments) {
4238   verifyFormat(
4239       "unsigned Cost =\n"
4240       "    TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n"
4241       "                        SI->getPointerAddressSpaceee());\n");
4242   verifyFormat(
4243       "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n"
4244       "    Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());");
4245 
4246   verifyFormat(
4247       "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa().aaaaaaaaa(\n"
4248       "    aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);");
4249   verifyFormat("unsigned OriginalStartColumn =\n"
4250                "    SourceMgr.getSpellingColumnNumber(\n"
4251                "        Current.FormatTok.getStartOfNonWhitespace()) -\n"
4252                "    1;");
4253 }
4254 
4255 TEST_F(FormatTest, AlignsAfterAssignments) {
4256   verifyFormat(
4257       "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4258       "             aaaaaaaaaaaaaaaaaaaaaaaaa;");
4259   verifyFormat(
4260       "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4261       "          aaaaaaaaaaaaaaaaaaaaaaaaa;");
4262   verifyFormat(
4263       "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4264       "           aaaaaaaaaaaaaaaaaaaaaaaaa;");
4265   verifyFormat(
4266       "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4267       "              aaaaaaaaaaaaaaaaaaaaaaaaa);");
4268   verifyFormat(
4269       "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4270       "                                            aaaaaaaaaaaaaaaaaaaaaaaa +\n"
4271       "                                            aaaaaaaaaaaaaaaaaaaaaaaa;");
4272 }
4273 
4274 TEST_F(FormatTest, AlignsAfterReturn) {
4275   verifyFormat(
4276       "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4277       "       aaaaaaaaaaaaaaaaaaaaaaaaa;");
4278   verifyFormat(
4279       "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4280       "        aaaaaaaaaaaaaaaaaaaaaaaaa);");
4281   verifyFormat(
4282       "return aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4283       "       aaaaaaaaaaaaaaaaaaaaaa();");
4284   verifyFormat(
4285       "return (aaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa >=\n"
4286       "        aaaaaaaaaaaaaaaaaaaaaa());");
4287   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4288                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4289   verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4290                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n"
4291                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4292   verifyFormat("return\n"
4293                "    // true if code is one of a or b.\n"
4294                "    code == a || code == b;");
4295 }
4296 
4297 TEST_F(FormatTest, AlignsAfterOpenBracket) {
4298   verifyFormat(
4299       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4300       "                                                aaaaaaaaa aaaaaaa) {}");
4301   verifyFormat(
4302       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4303       "                                               aaaaaaaaaaa aaaaaaaaa);");
4304   verifyFormat(
4305       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4306       "                                             aaaaaaaaaaaaaaaaaaaaa));");
4307   FormatStyle Style = getLLVMStyle();
4308   Style.AlignAfterOpenBracket = false;
4309   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4310                "    aaaaaaaaaaa aaaaaaaa, aaaaaaaaa aaaaaaa) {}",
4311                Style);
4312   verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n"
4313                "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaa aaaaaaaaa);",
4314                Style);
4315   verifyFormat("SomeLongVariableName->someFunction(\n"
4316                "    foooooooo(aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa));",
4317                Style);
4318   verifyFormat(
4319       "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaa aaaaaaaa,\n"
4320       "    aaaaaaaaa aaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}",
4321       Style);
4322   verifyFormat(
4323       "SomeLongVariableName->someVeryLongFunctionName(aaaaaaaaaaa aaaaaaaaa,\n"
4324       "    aaaaaaaaaaa aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4325       Style);
4326   verifyFormat(
4327       "SomeLongVariableName->someFunction(foooooooo(aaaaaaaaaaaaaaa,\n"
4328       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));",
4329       Style);
4330 }
4331 
4332 TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
4333   FormatStyle Style = getLLVMStyleWithColumns(40);
4334   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4335                "          bbbbbbbbbbbbbbbbbbbbbb);",
4336                Style);
4337   Style.AlignAfterOpenBracket = true;
4338   Style.AlignOperands = false;
4339   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4340                "          bbbbbbbbbbbbbbbbbbbbbb);",
4341                Style);
4342   Style.AlignAfterOpenBracket = false;
4343   Style.AlignOperands = true;
4344   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4345                "          bbbbbbbbbbbbbbbbbbbbbb);",
4346                Style);
4347   Style.AlignAfterOpenBracket = false;
4348   Style.AlignOperands = false;
4349   verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n"
4350                "    bbbbbbbbbbbbbbbbbbbbbb);",
4351                Style);
4352 }
4353 
4354 TEST_F(FormatTest, BreaksConditionalExpressions) {
4355   verifyFormat(
4356       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4357       "                               ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4358       "                               : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4359   verifyFormat(
4360       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4361       "                                   : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4362   verifyFormat(
4363       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n"
4364       "                                                    : aaaaaaaaaaaaa);");
4365   verifyFormat(
4366       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4367       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4368       "                                    : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4369       "                   aaaaaaaaaaaaa);");
4370   verifyFormat(
4371       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4372       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4373       "                   aaaaaaaaaaaaa);");
4374   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4375                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4376                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4377                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4378                "          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4379   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4380                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4381                "           ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4382                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4383                "           : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4384                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4385                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4386   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4387                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4388                "           ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4389                "                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4390                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4391   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4392                "    ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4393                "    : aaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4394   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4395                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4396                "        ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4397                "        : aaaaaaaaaaaaaaaa;");
4398   verifyFormat(
4399       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4400       "    ? aaaaaaaaaaaaaaa\n"
4401       "    : aaaaaaaaaaaaaaa;");
4402   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4403                "          aaaaaaaaa\n"
4404                "      ? b\n"
4405                "      : c);");
4406   verifyFormat("return aaaa == bbbb\n"
4407                "           // comment\n"
4408                "           ? aaaa\n"
4409                "           : bbbb;");
4410   verifyFormat("unsigned Indent =\n"
4411                "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n"
4412                "                              ? IndentForLevel[TheLine.Level]\n"
4413                "                              : TheLine * 2,\n"
4414                "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4415                getLLVMStyleWithColumns(70));
4416   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4417                "                  ? aaaaaaaaaaaaaaa\n"
4418                "                  : bbbbbbbbbbbbbbb //\n"
4419                "                        ? ccccccccccccccc\n"
4420                "                        : ddddddddddddddd;");
4421   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa //\n"
4422                "                  ? aaaaaaaaaaaaaaa\n"
4423                "                  : (bbbbbbbbbbbbbbb //\n"
4424                "                         ? ccccccccccccccc\n"
4425                "                         : ddddddddddddddd);");
4426   verifyFormat(
4427       "int aaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4428       "                                      ? aaaaaaaaaaaaaaaaaaaaaaaaa +\n"
4429       "                                            aaaaaaaaaaaaaaaaaaaaa +\n"
4430       "                                            aaaaaaaaaaaaaaaaaaaaa\n"
4431       "                                      : aaaaaaaaaa;");
4432   verifyFormat(
4433       "aaaaaa = aaaaaaaaaaaa\n"
4434       "             ? aaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4435       "                          : aaaaaaaaaaaaaaaaaaaaaa\n"
4436       "             : aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4437 
4438   FormatStyle NoBinPacking = getLLVMStyle();
4439   NoBinPacking.BinPackArguments = false;
4440   verifyFormat(
4441       "void f() {\n"
4442       "  g(aaa,\n"
4443       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4444       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4445       "        ? aaaaaaaaaaaaaaa\n"
4446       "        : aaaaaaaaaaaaaaa);\n"
4447       "}",
4448       NoBinPacking);
4449   verifyFormat(
4450       "void f() {\n"
4451       "  g(aaa,\n"
4452       "    aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n"
4453       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4454       "        ?: aaaaaaaaaaaaaaa);\n"
4455       "}",
4456       NoBinPacking);
4457 
4458   verifyFormat("SomeFunction(aaaaaaaaaaaaaaaaa,\n"
4459                "             // comment.\n"
4460                "             ccccccccccccccccccccccccccccccccccccccc\n"
4461                "                 ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4462                "                 : bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);");
4463 
4464   // Assignments in conditional expressions. Apparently not uncommon :-(.
4465   verifyFormat("return a != b\n"
4466                "           // comment\n"
4467                "           ? a = b\n"
4468                "           : a = b;");
4469   verifyFormat("return a != b\n"
4470                "           // comment\n"
4471                "           ? a = a != b\n"
4472                "                     // comment\n"
4473                "                     ? a = b\n"
4474                "                     : a\n"
4475                "           : a;\n");
4476   verifyFormat("return a != b\n"
4477                "           // comment\n"
4478                "           ? a\n"
4479                "           : a = a != b\n"
4480                "                     // comment\n"
4481                "                     ? a = b\n"
4482                "                     : a;");
4483 }
4484 
4485 TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) {
4486   FormatStyle Style = getLLVMStyle();
4487   Style.BreakBeforeTernaryOperators = false;
4488   Style.ColumnLimit = 70;
4489   verifyFormat(
4490       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4491       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4492       "                               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4493       Style);
4494   verifyFormat(
4495       "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4496       "                                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4497       Style);
4498   verifyFormat(
4499       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n"
4500       "                                                      aaaaaaaaaaaaa);",
4501       Style);
4502   verifyFormat(
4503       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4504       "                   aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4505       "                                      aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4506       "                   aaaaaaaaaaaaa);",
4507       Style);
4508   verifyFormat(
4509       "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4510       "                   aaaaaaaaaaaaaaaa ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4511       "                   aaaaaaaaaaaaa);",
4512       Style);
4513   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4514                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4515                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4516                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4517                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4518                Style);
4519   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4520                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4521                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4522                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) :\n"
4523                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4524                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4525                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4526                Style);
4527   verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4528                "       aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?:\n"
4529                "           aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4530                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n"
4531                "       aaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4532                Style);
4533   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4534                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4535                "    aaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4536                Style);
4537   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaa =\n"
4538                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4539                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n"
4540                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4541                Style);
4542   verifyFormat(
4543       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?\n"
4544       "    aaaaaaaaaaaaaaa :\n"
4545       "    aaaaaaaaaaaaaaa;",
4546       Style);
4547   verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n"
4548                "          aaaaaaaaa ?\n"
4549                "      b :\n"
4550                "      c);",
4551                Style);
4552   verifyFormat(
4553       "unsigned Indent =\n"
4554       "    format(TheLine.First, IndentForLevel[TheLine.Level] >= 0 ?\n"
4555       "                              IndentForLevel[TheLine.Level] :\n"
4556       "                              TheLine * 2,\n"
4557       "           TheLine.InPPDirective, PreviousEndOfLineColumn);",
4558       Style);
4559   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4560                "                  aaaaaaaaaaaaaaa :\n"
4561                "                  bbbbbbbbbbbbbbb ? //\n"
4562                "                      ccccccccccccccc :\n"
4563                "                      ddddddddddddddd;",
4564                Style);
4565   verifyFormat("bool aaaaaa = aaaaaaaaaaaaa ? //\n"
4566                "                  aaaaaaaaaaaaaaa :\n"
4567                "                  (bbbbbbbbbbbbbbb ? //\n"
4568                "                       ccccccccccccccc :\n"
4569                "                       ddddddddddddddd);",
4570                Style);
4571 }
4572 
4573 TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
4574   verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n"
4575                "     aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();");
4576   verifyFormat("bool a = true, b = false;");
4577 
4578   verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4579                "         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n"
4580                "     bbbbbbbbbbbbbbbbbbbbbbbbb =\n"
4581                "         bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);");
4582   verifyFormat(
4583       "bool aaaaaaaaaaaaaaaaaaaaa =\n"
4584       "         bbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n"
4585       "     d = e && f;");
4586   verifyFormat("aaaaaaaaa a = aaaaaaaaaaaaaaaaaaaa, b = bbbbbbbbbbbbbbbbbbbb,\n"
4587                "          c = cccccccccccccccccccc, d = dddddddddddddddddddd;");
4588   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4589                "          *c = ccccccccccccccccccc, *d = ddddddddddddddddddd;");
4590   verifyFormat("aaaaaaaaa ***a = aaaaaaaaaaaaaaaaaaa, ***b = bbbbbbbbbbbbbbb,\n"
4591                "          ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;");
4592 
4593   FormatStyle Style = getGoogleStyle();
4594   Style.PointerAlignment = FormatStyle::PAS_Left;
4595   Style.DerivePointerAlignment = false;
4596   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4597                "    *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n"
4598                "    *b = bbbbbbbbbbbbbbbbbbb;",
4599                Style);
4600   verifyFormat("aaaaaaaaa *a = aaaaaaaaaaaaaaaaaaa, *b = bbbbbbbbbbbbbbbbbbb,\n"
4601                "          *b = bbbbbbbbbbbbbbbbbbb, *d = ddddddddddddddddddd;",
4602                Style);
4603 }
4604 
4605 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {
4606   verifyFormat("arr[foo ? bar : baz];");
4607   verifyFormat("f()[foo ? bar : baz];");
4608   verifyFormat("(a + b)[foo ? bar : baz];");
4609   verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];");
4610 }
4611 
4612 TEST_F(FormatTest, AlignsStringLiterals) {
4613   verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n"
4614                "                                      \"short literal\");");
4615   verifyFormat(
4616       "looooooooooooooooooooooooongFunction(\n"
4617       "    \"short literal\"\n"
4618       "    \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");");
4619   verifyFormat("someFunction(\"Always break between multi-line\"\n"
4620                "             \" string literals\",\n"
4621                "             and, other, parameters);");
4622   EXPECT_EQ("fun + \"1243\" /* comment */\n"
4623             "      \"5678\";",
4624             format("fun + \"1243\" /* comment */\n"
4625                    "      \"5678\";",
4626                    getLLVMStyleWithColumns(28)));
4627   EXPECT_EQ(
4628       "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
4629       "         \"aaaaaaaaaaaaaaaaaaaaa\"\n"
4630       "         \"aaaaaaaaaaaaaaaa\";",
4631       format("aaaaaa ="
4632              "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa "
4633              "aaaaaaaaaaaaaaaaaaaaa\" "
4634              "\"aaaaaaaaaaaaaaaa\";"));
4635   verifyFormat("a = a + \"a\"\n"
4636                "        \"a\"\n"
4637                "        \"a\";");
4638   verifyFormat("f(\"a\", \"b\"\n"
4639                "       \"c\");");
4640 
4641   verifyFormat(
4642       "#define LL_FORMAT \"ll\"\n"
4643       "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n"
4644       "       \"d, ddddddddd: %\" LL_FORMAT \"d\");");
4645 
4646   verifyFormat("#define A(X)          \\\n"
4647                "  \"aaaaa\" #X \"bbbbbb\" \\\n"
4648                "  \"ccccc\"",
4649                getLLVMStyleWithColumns(23));
4650   verifyFormat("#define A \"def\"\n"
4651                "f(\"abc\" A \"ghi\"\n"
4652                "  \"jkl\");");
4653 
4654   verifyFormat("f(L\"a\"\n"
4655                "  L\"b\")");
4656   verifyFormat("#define A(X)            \\\n"
4657                "  L\"aaaaa\" #X L\"bbbbbb\" \\\n"
4658                "  L\"ccccc\"",
4659                getLLVMStyleWithColumns(25));
4660 }
4661 
4662 TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
4663   FormatStyle AfterType = getLLVMStyle();
4664   AfterType.AlwaysBreakAfterDefinitionReturnType = true;
4665   verifyFormat("const char *\n"
4666                "f(void) {\n" // Break here.
4667                "  return \"\";\n"
4668                "}\n"
4669                "const char *bar(void);\n", // No break here.
4670                AfterType);
4671   verifyFormat("template <class T>\n"
4672                "T *\n"
4673                "f(T &c) {\n" // Break here.
4674                "  return NULL;\n"
4675                "}\n"
4676                "template <class T> T *f(T &c);\n", // No break here.
4677                AfterType);
4678   AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
4679   verifyFormat("const char *\n"
4680                "f(void)\n" // Break here.
4681                "{\n"
4682                "  return \"\";\n"
4683                "}\n"
4684                "const char *bar(void);\n", // No break here.
4685                AfterType);
4686   verifyFormat("template <class T>\n"
4687                "T *\n"     // Problem here: no line break
4688                "f(T &c)\n" // Break here.
4689                "{\n"
4690                "  return NULL;\n"
4691                "}\n"
4692                "template <class T> T *f(T &c);\n", // No break here.
4693                AfterType);
4694 }
4695 
4696 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
4697   FormatStyle NoBreak = getLLVMStyle();
4698   NoBreak.AlwaysBreakBeforeMultilineStrings = false;
4699   FormatStyle Break = getLLVMStyle();
4700   Break.AlwaysBreakBeforeMultilineStrings = true;
4701   verifyFormat("aaaa = \"bbbb\"\n"
4702                "       \"cccc\";",
4703                NoBreak);
4704   verifyFormat("aaaa =\n"
4705                "    \"bbbb\"\n"
4706                "    \"cccc\";",
4707                Break);
4708   verifyFormat("aaaa(\"bbbb\"\n"
4709                "     \"cccc\");",
4710                NoBreak);
4711   verifyFormat("aaaa(\n"
4712                "    \"bbbb\"\n"
4713                "    \"cccc\");",
4714                Break);
4715   verifyFormat("aaaa(qqq, \"bbbb\"\n"
4716                "          \"cccc\");",
4717                NoBreak);
4718   verifyFormat("aaaa(qqq,\n"
4719                "     \"bbbb\"\n"
4720                "     \"cccc\");",
4721                Break);
4722   verifyFormat("aaaa(qqq,\n"
4723                "     L\"bbbb\"\n"
4724                "     L\"cccc\");",
4725                Break);
4726 
4727   // As we break before unary operators, breaking right after them is bad.
4728   verifyFormat("string foo = abc ? \"x\"\n"
4729                "                   \"blah blah blah blah blah blah\"\n"
4730                "                 : \"y\";",
4731                Break);
4732 
4733   // Don't break if there is no column gain.
4734   verifyFormat("f(\"aaaa\"\n"
4735                "  \"bbbb\");",
4736                Break);
4737 
4738   // Treat literals with escaped newlines like multi-line string literals.
4739   EXPECT_EQ("x = \"a\\\n"
4740             "b\\\n"
4741             "c\";",
4742             format("x = \"a\\\n"
4743                    "b\\\n"
4744                    "c\";",
4745                    NoBreak));
4746   EXPECT_EQ("x =\n"
4747             "    \"a\\\n"
4748             "b\\\n"
4749             "c\";",
4750             format("x = \"a\\\n"
4751                    "b\\\n"
4752                    "c\";",
4753                    Break));
4754 
4755   // Exempt ObjC strings for now.
4756   EXPECT_EQ("NSString *const kString = @\"aaaa\"\n"
4757             "                           \"bbbb\";",
4758             format("NSString *const kString = @\"aaaa\"\n"
4759                    "\"bbbb\";",
4760                    Break));
4761 
4762   Break.ColumnLimit = 0;
4763   verifyFormat("const char *hello = \"hello llvm\";", Break);
4764 }
4765 
4766 TEST_F(FormatTest, AlignsPipes) {
4767   verifyFormat(
4768       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4769       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4770       "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4771   verifyFormat(
4772       "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n"
4773       "                     << aaaaaaaaaaaaaaaaaaaa;");
4774   verifyFormat(
4775       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4776       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4777   verifyFormat(
4778       "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
4779       "                \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n"
4780       "             << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";");
4781   verifyFormat(
4782       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4783       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4784       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4785   verifyFormat(
4786       "llvm::errs() << \"a: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4787       "                             aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4788       "                             aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4789   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4790                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4791                "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4792                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4793   verifyFormat(
4794       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4795       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
4796 
4797   verifyFormat("return out << \"somepacket = {\\n\"\n"
4798                "           << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
4799                "           << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
4800                "           << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
4801                "           << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
4802                "           << \"}\";");
4803 
4804   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4805                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
4806                "             << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
4807   verifyFormat(
4808       "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
4809       "             << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
4810       "             << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
4811       "             << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
4812       "             << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
4813   verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
4814                "             << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
4815   verifyFormat(
4816       "void f() {\n"
4817       "  llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
4818       "               << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
4819       "}");
4820   verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
4821                "             << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
4822 
4823   // Breaking before the first "<<" is generally not desirable.
4824   verifyFormat(
4825       "llvm::errs()\n"
4826       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4827       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4828       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4829       "    << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4830       getLLVMStyleWithColumns(70));
4831   verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4832                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4833                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4834                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4835                "             << \"aaaaaaaaaaaaaaaaaaa: \"\n"
4836                "             << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
4837                getLLVMStyleWithColumns(70));
4838 
4839   // But sometimes, breaking before the first "<<" is desirable.
4840   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
4841                "    << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
4842   verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa, bbbbbbbbb)\n"
4843                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4844                "    << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4845   verifyFormat("SemaRef.Diag(Loc, diag::note_for_range_begin_end)\n"
4846                "    << BEF << IsTemplate << Description << E->getType();");
4847 
4848   verifyFormat(
4849       "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4850       "                    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4851 
4852   // Incomplete string literal.
4853   EXPECT_EQ("llvm::errs() << \"\n"
4854             "             << a;",
4855             format("llvm::errs() << \"\n<<a;"));
4856 
4857   verifyFormat("void f() {\n"
4858                "  CHECK_EQ(aaaa, (*bbbbbbbbb)->cccccc)\n"
4859                "      << \"qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\";\n"
4860                "}");
4861 
4862   // Handle 'endl'.
4863   verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaa << endl\n"
4864                "             << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4865   verifyFormat("llvm::errs() << endl << bbbbbbbbbbbbbbbbbbbbbb << endl;");
4866 }
4867 
4868 TEST_F(FormatTest, UnderstandsEquals) {
4869   verifyFormat(
4870       "aaaaaaaaaaaaaaaaa =\n"
4871       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
4872   verifyFormat(
4873       "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4874       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4875   verifyFormat(
4876       "if (a) {\n"
4877       "  f();\n"
4878       "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4879       "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n"
4880       "}");
4881 
4882   verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
4883                "        100000000 + 10000000) {\n}");
4884 }
4885 
4886 TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
4887   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4888                "    .looooooooooooooooooooooooooooooooooooooongFunction();");
4889 
4890   verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n"
4891                "    ->looooooooooooooooooooooooooooooooooooooongFunction();");
4892 
4893   verifyFormat(
4894       "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n"
4895       "                                                          Parameter2);");
4896 
4897   verifyFormat(
4898       "ShortObject->shortFunction(\n"
4899       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n"
4900       "    LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);");
4901 
4902   verifyFormat("loooooooooooooongFunction(\n"
4903                "    LoooooooooooooongObject->looooooooooooooooongFunction());");
4904 
4905   verifyFormat(
4906       "function(LoooooooooooooooooooooooooooooooooooongObject\n"
4907       "             ->loooooooooooooooooooooooooooooooooooooooongFunction());");
4908 
4909   verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4910                "    .WillRepeatedly(Return(SomeValue));");
4911   verifyFormat("void f() {\n"
4912                "  EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n"
4913                "      .Times(2)\n"
4914                "      .WillRepeatedly(Return(SomeValue));\n"
4915                "}");
4916   verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)].insert(\n"
4917                "    ccccccccccccccccccccccc);");
4918   verifyFormat("aaaaa(aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4919                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4920                "          .aaaaa(aaaaa),\n"
4921                "      aaaaaaaaaaaaaaaaaaaaa);");
4922   verifyFormat("void f() {\n"
4923                "  aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4924                "      aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa)->aaaaaaaaa());\n"
4925                "}");
4926   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4927                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4928                "    .aaaaaaaaaaaaaaa(aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4929                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4930                "                        aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
4931   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4932                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4933                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
4934                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()) {\n"
4935                "}");
4936 
4937   // Here, it is not necessary to wrap at "." or "->".
4938   verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n"
4939                "    aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}");
4940   verifyFormat(
4941       "aaaaaaaaaaa->aaaaaaaaa(\n"
4942       "    aaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4943       "    aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n");
4944 
4945   verifyFormat(
4946       "aaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4947       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());");
4948   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n"
4949                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4950   verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n"
4951                "                         aaaaaaaaa()->aaaaaa()->aaaaa());");
4952 
4953   verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
4954                "      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4955                "    .a();");
4956 
4957   FormatStyle NoBinPacking = getLLVMStyle();
4958   NoBinPacking.BinPackParameters = false;
4959   verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4960                "    .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
4961                "    .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n"
4962                "                         aaaaaaaaaaaaaaaaaaa,\n"
4963                "                         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);",
4964                NoBinPacking);
4965 
4966   // If there is a subsequent call, change to hanging indentation.
4967   verifyFormat(
4968       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4969       "                         aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n"
4970       "    .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4971   verifyFormat(
4972       "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4973       "    aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));");
4974   verifyFormat("aaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4975                "                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4976                "                 .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
4977   verifyFormat("aaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
4978                "               aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
4979                "               .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
4980 }
4981 
4982 TEST_F(FormatTest, WrapsTemplateDeclarations) {
4983   verifyFormat("template <typename T>\n"
4984                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4985   verifyFormat("template <typename T>\n"
4986                "// T should be one of {A, B}.\n"
4987                "virtual void loooooooooooongFunction(int Param1, int Param2);");
4988   verifyFormat(
4989       "template <typename T>\n"
4990       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");
4991   verifyFormat("template <typename T>\n"
4992                "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n"
4993                "       int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);");
4994   verifyFormat(
4995       "template <typename T>\n"
4996       "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n"
4997       "                                      int Paaaaaaaaaaaaaaaaaaaaram2);");
4998   verifyFormat(
4999       "template <typename T>\n"
5000       "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n"
5001       "                    aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n"
5002       "                    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5003   verifyFormat("template <typename T>\n"
5004                "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5005                "    int aaaaaaaaaaaaaaaaaaaaaa);");
5006   verifyFormat(
5007       "template <typename T1, typename T2 = char, typename T3 = char,\n"
5008       "          typename T4 = char>\n"
5009       "void f();");
5010   verifyFormat("template <typename aaaaaaaaaaa, typename bbbbbbbbbbbbb,\n"
5011                "          template <typename> class cccccccccccccccccccccc,\n"
5012                "          typename ddddddddddddd>\n"
5013                "class C {};");
5014   verifyFormat(
5015       "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n"
5016       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5017 
5018   verifyFormat("void f() {\n"
5019                "  a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n"
5020                "      a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));\n"
5021                "}");
5022 
5023   verifyFormat("template <typename T> class C {};");
5024   verifyFormat("template <typename T> void f();");
5025   verifyFormat("template <typename T> void f() {}");
5026   verifyFormat(
5027       "aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5028       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5029       "              aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> *aaaa =\n"
5030       "    new aaaaaaaaaaaaa<aaaaaaaaaa, aaaaaaaaaaa,\n"
5031       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5032       "                      aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n"
5033       "        bbbbbbbbbbbbbbbbbbbbbbbb);",
5034       getLLVMStyleWithColumns(72));
5035   EXPECT_EQ("static_cast<A< //\n"
5036             "    B> *>(\n"
5037             "\n"
5038             "    );",
5039             format("static_cast<A<//\n"
5040                    "    B>*>(\n"
5041                    "\n"
5042                    "    );"));
5043   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5044                "    const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);");
5045 
5046   FormatStyle AlwaysBreak = getLLVMStyle();
5047   AlwaysBreak.AlwaysBreakTemplateDeclarations = true;
5048   verifyFormat("template <typename T>\nclass C {};", AlwaysBreak);
5049   verifyFormat("template <typename T>\nvoid f();", AlwaysBreak);
5050   verifyFormat("template <typename T>\nvoid f() {}", AlwaysBreak);
5051   verifyFormat("void aaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5052                "                         bbbbbbbbbbbbbbbbbbbbbbbbbbbb>(\n"
5053                "    ccccccccccccccccccccccccccccccccccccccccccccccc);");
5054   verifyFormat("template <template <typename> class Fooooooo,\n"
5055                "          template <typename> class Baaaaaaar>\n"
5056                "struct C {};",
5057                AlwaysBreak);
5058   verifyFormat("template <typename T> // T can be A, B or C.\n"
5059                "struct C {};",
5060                AlwaysBreak);
5061 }
5062 
5063 TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) {
5064   verifyFormat(
5065       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5066       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5067   verifyFormat(
5068       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5069       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5070       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());");
5071 
5072   // FIXME: Should we have the extra indent after the second break?
5073   verifyFormat(
5074       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5075       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5076       "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5077 
5078   verifyFormat(
5079       "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n"
5080       "                    cccccccccccccccccccccccccccccccccccccccccccccc());");
5081 
5082   // Breaking at nested name specifiers is generally not desirable.
5083   verifyFormat(
5084       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5085       "    aaaaaaaaaaaaaaaaaaaaaaa);");
5086 
5087   verifyFormat(
5088       "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5089       "                                 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5090       "                   aaaaaaaaaaaaaaaaaaaaa);",
5091       getLLVMStyleWithColumns(74));
5092 
5093   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n"
5094                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5095                "        .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();");
5096 }
5097 
5098 TEST_F(FormatTest, UnderstandsTemplateParameters) {
5099   verifyFormat("A<int> a;");
5100   verifyFormat("A<A<A<int>>> a;");
5101   verifyFormat("A<A<A<int, 2>, 3>, 4> a;");
5102   verifyFormat("bool x = a < 1 || 2 > a;");
5103   verifyFormat("bool x = 5 < f<int>();");
5104   verifyFormat("bool x = f<int>() > 5;");
5105   verifyFormat("bool x = 5 < a<int>::x;");
5106   verifyFormat("bool x = a < 4 ? a > 2 : false;");
5107   verifyFormat("bool x = f() ? a < 2 : a > 2;");
5108 
5109   verifyGoogleFormat("A<A<int>> a;");
5110   verifyGoogleFormat("A<A<A<int>>> a;");
5111   verifyGoogleFormat("A<A<A<A<int>>>> a;");
5112   verifyGoogleFormat("A<A<int> > a;");
5113   verifyGoogleFormat("A<A<A<int> > > a;");
5114   verifyGoogleFormat("A<A<A<A<int> > > > a;");
5115   verifyGoogleFormat("A<::A<int>> a;");
5116   verifyGoogleFormat("A<::A> a;");
5117   verifyGoogleFormat("A< ::A> a;");
5118   verifyGoogleFormat("A< ::A<int> > a;");
5119   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle()));
5120   EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle()));
5121   EXPECT_EQ("A<::A<int>> a;", format("A< ::A<int>> a;", getGoogleStyle()));
5122   EXPECT_EQ("A<::A<int>> a;", format("A<::A<int> > a;", getGoogleStyle()));
5123 
5124   verifyFormat("A<A>> a;", getChromiumStyle(FormatStyle::LK_Cpp));
5125 
5126   verifyFormat("test >> a >> b;");
5127   verifyFormat("test << a >> b;");
5128 
5129   verifyFormat("f<int>();");
5130   verifyFormat("template <typename T> void f() {}");
5131   verifyFormat("struct A<std::enable_if<sizeof(T2) < sizeof(int32)>::type>;");
5132 
5133   // Not template parameters.
5134   verifyFormat("return a < b && c > d;");
5135   verifyFormat("void f() {\n"
5136                "  while (a < b && c > d) {\n"
5137                "  }\n"
5138                "}");
5139   verifyFormat("template <typename... Types>\n"
5140                "typename enable_if<0 < sizeof...(Types)>::type Foo() {}");
5141 
5142   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5143                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaa >> aaaaa);",
5144                getLLVMStyleWithColumns(60));
5145   verifyFormat("static_assert(is_convertible<A &&, B>::value, \"AAA\");");
5146   verifyFormat("Constructor(A... a) : a_(X<A>{std::forward<A>(a)}...) {}");
5147 }
5148 
5149 TEST_F(FormatTest, UnderstandsBinaryOperators) {
5150   verifyFormat("COMPARE(a, ==, b);");
5151 }
5152 
5153 TEST_F(FormatTest, UnderstandsPointersToMembers) {
5154   verifyFormat("int A::*x;");
5155   verifyFormat("int (S::*func)(void *);");
5156   verifyFormat("void f() { int (S::*func)(void *); }");
5157   verifyFormat("typedef bool *(Class::*Member)() const;");
5158   verifyFormat("void f() {\n"
5159                "  (a->*f)();\n"
5160                "  a->*x;\n"
5161                "  (a.*f)();\n"
5162                "  ((*a).*f)();\n"
5163                "  a.*x;\n"
5164                "}");
5165   verifyFormat("void f() {\n"
5166                "  (a->*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5167                "      aaaa, bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);\n"
5168                "}");
5169   verifyFormat(
5170       "(aaaaaaaaaa->*bbbbbbb)(\n"
5171       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa));");
5172   FormatStyle Style = getLLVMStyle();
5173   Style.PointerAlignment = FormatStyle::PAS_Left;
5174   verifyFormat("typedef bool* (Class::*Member)() const;", Style);
5175 }
5176 
5177 TEST_F(FormatTest, UnderstandsUnaryOperators) {
5178   verifyFormat("int a = -2;");
5179   verifyFormat("f(-1, -2, -3);");
5180   verifyFormat("a[-1] = 5;");
5181   verifyFormat("int a = 5 + -2;");
5182   verifyFormat("if (i == -1) {\n}");
5183   verifyFormat("if (i != -1) {\n}");
5184   verifyFormat("if (i > -1) {\n}");
5185   verifyFormat("if (i < -1) {\n}");
5186   verifyFormat("++(a->f());");
5187   verifyFormat("--(a->f());");
5188   verifyFormat("(a->f())++;");
5189   verifyFormat("a[42]++;");
5190   verifyFormat("if (!(a->f())) {\n}");
5191 
5192   verifyFormat("a-- > b;");
5193   verifyFormat("b ? -a : c;");
5194   verifyFormat("n * sizeof char16;");
5195   verifyFormat("n * alignof char16;", getGoogleStyle());
5196   verifyFormat("sizeof(char);");
5197   verifyFormat("alignof(char);", getGoogleStyle());
5198 
5199   verifyFormat("return -1;");
5200   verifyFormat("switch (a) {\n"
5201                "case -1:\n"
5202                "  break;\n"
5203                "}");
5204   verifyFormat("#define X -1");
5205   verifyFormat("#define X -kConstant");
5206 
5207   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {-5, +3};");
5208   verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = {+5, -3};");
5209 
5210   verifyFormat("int a = /* confusing comment */ -1;");
5211   // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case.
5212   verifyFormat("int a = i /* confusing comment */++;");
5213 }
5214 
5215 TEST_F(FormatTest, DoesNotIndentRelativeToUnaryOperators) {
5216   verifyFormat("if (!aaaaaaaaaa( // break\n"
5217                "        aaaaa)) {\n"
5218                "}");
5219   verifyFormat("aaaaaaaaaa(!aaaaaaaaaa( // break\n"
5220                "               aaaaa));");
5221   verifyFormat("*aaa = aaaaaaa( // break\n"
5222                "    bbbbbb);");
5223 }
5224 
5225 TEST_F(FormatTest, UnderstandsOverloadedOperators) {
5226   verifyFormat("bool operator<();");
5227   verifyFormat("bool operator>();");
5228   verifyFormat("bool operator=();");
5229   verifyFormat("bool operator==();");
5230   verifyFormat("bool operator!=();");
5231   verifyFormat("int operator+();");
5232   verifyFormat("int operator++();");
5233   verifyFormat("bool operator();");
5234   verifyFormat("bool operator()();");
5235   verifyFormat("bool operator[]();");
5236   verifyFormat("operator bool();");
5237   verifyFormat("operator int();");
5238   verifyFormat("operator void *();");
5239   verifyFormat("operator SomeType<int>();");
5240   verifyFormat("operator SomeType<int, int>();");
5241   verifyFormat("operator SomeType<SomeType<int>>();");
5242   verifyFormat("void *operator new(std::size_t size);");
5243   verifyFormat("void *operator new[](std::size_t size);");
5244   verifyFormat("void operator delete(void *ptr);");
5245   verifyFormat("void operator delete[](void *ptr);");
5246   verifyFormat("template <typename AAAAAAA, typename BBBBBBB>\n"
5247                "AAAAAAA operator/(const AAAAAAA &a, BBBBBBB &b);");
5248 
5249   verifyFormat(
5250       "ostream &operator<<(ostream &OutputStream,\n"
5251       "                    SomeReallyLongType WithSomeReallyLongValue);");
5252   verifyFormat("bool operator<(const aaaaaaaaaaaaaaaaaaaaa &left,\n"
5253                "               const aaaaaaaaaaaaaaaaaaaaa &right) {\n"
5254                "  return left.group < right.group;\n"
5255                "}");
5256   verifyFormat("SomeType &operator=(const SomeType &S);");
5257   verifyFormat("f.template operator()<int>();");
5258 
5259   verifyGoogleFormat("operator void*();");
5260   verifyGoogleFormat("operator SomeType<SomeType<int>>();");
5261   verifyGoogleFormat("operator ::A();");
5262 
5263   verifyFormat("using A::operator+;");
5264 
5265   verifyFormat("string // break\n"
5266                "operator()() & {}");
5267   verifyFormat("string // break\n"
5268                "operator()() && {}");
5269 }
5270 
5271 TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
5272   verifyFormat("Deleted &operator=(const Deleted &)& = default;");
5273   verifyFormat("Deleted &operator=(const Deleted &)&& = delete;");
5274   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;");
5275   verifyFormat("SomeType MemberFunction(const Deleted &)&& = delete;");
5276   verifyFormat("Deleted &operator=(const Deleted &)&;");
5277   verifyFormat("Deleted &operator=(const Deleted &)&&;");
5278   verifyFormat("SomeType MemberFunction(const Deleted &)&;");
5279   verifyFormat("SomeType MemberFunction(const Deleted &)&&;");
5280 
5281   verifyGoogleFormat("Deleted& operator=(const Deleted&)& = default;");
5282   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)& = delete;");
5283   verifyGoogleFormat("Deleted& operator=(const Deleted&)&;");
5284   verifyGoogleFormat("SomeType MemberFunction(const Deleted&)&;");
5285 
5286   FormatStyle Spaces = getLLVMStyle();
5287   Spaces.SpacesInCStyleCastParentheses = true;
5288   verifyFormat("Deleted &operator=(const Deleted &)& = default;", Spaces);
5289   verifyFormat("SomeType MemberFunction(const Deleted &)& = delete;", Spaces);
5290   verifyFormat("Deleted &operator=(const Deleted &)&;", Spaces);
5291   verifyFormat("SomeType MemberFunction(const Deleted &)&;", Spaces);
5292 
5293   Spaces.SpacesInCStyleCastParentheses = false;
5294   Spaces.SpacesInParentheses = true;
5295   verifyFormat("Deleted &operator=( const Deleted & )& = default;", Spaces);
5296   verifyFormat("SomeType MemberFunction( const Deleted & )& = delete;", Spaces);
5297   verifyFormat("Deleted &operator=( const Deleted & )&;", Spaces);
5298   verifyFormat("SomeType MemberFunction( const Deleted & )&;", Spaces);
5299 }
5300 
5301 TEST_F(FormatTest, UnderstandsNewAndDelete) {
5302   verifyFormat("void f() {\n"
5303                "  A *a = new A;\n"
5304                "  A *a = new (placement) A;\n"
5305                "  delete a;\n"
5306                "  delete (A *)a;\n"
5307                "}");
5308   verifyFormat("new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5309                "    typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5310   verifyFormat("auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5311                "    new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
5312                "        typename aaaaaaaaaaaaaaaaaaaaaaaa();");
5313   verifyFormat("delete[] h->p;");
5314 }
5315 
5316 TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
5317   verifyFormat("int *f(int *a) {}");
5318   verifyFormat("int main(int argc, char **argv) {}");
5319   verifyFormat("Test::Test(int b) : a(b * b) {}");
5320   verifyIndependentOfContext("f(a, *a);");
5321   verifyFormat("void g() { f(*a); }");
5322   verifyIndependentOfContext("int a = b * 10;");
5323   verifyIndependentOfContext("int a = 10 * b;");
5324   verifyIndependentOfContext("int a = b * c;");
5325   verifyIndependentOfContext("int a += b * c;");
5326   verifyIndependentOfContext("int a -= b * c;");
5327   verifyIndependentOfContext("int a *= b * c;");
5328   verifyIndependentOfContext("int a /= b * c;");
5329   verifyIndependentOfContext("int a = *b;");
5330   verifyIndependentOfContext("int a = *b * c;");
5331   verifyIndependentOfContext("int a = b * *c;");
5332   verifyIndependentOfContext("return 10 * b;");
5333   verifyIndependentOfContext("return *b * *c;");
5334   verifyIndependentOfContext("return a & ~b;");
5335   verifyIndependentOfContext("f(b ? *c : *d);");
5336   verifyIndependentOfContext("int a = b ? *c : *d;");
5337   verifyIndependentOfContext("*b = a;");
5338   verifyIndependentOfContext("a * ~b;");
5339   verifyIndependentOfContext("a * !b;");
5340   verifyIndependentOfContext("a * +b;");
5341   verifyIndependentOfContext("a * -b;");
5342   verifyIndependentOfContext("a * ++b;");
5343   verifyIndependentOfContext("a * --b;");
5344   verifyIndependentOfContext("a[4] * b;");
5345   verifyIndependentOfContext("a[a * a] = 1;");
5346   verifyIndependentOfContext("f() * b;");
5347   verifyIndependentOfContext("a * [self dostuff];");
5348   verifyIndependentOfContext("int x = a * (a + b);");
5349   verifyIndependentOfContext("(a *)(a + b);");
5350   verifyIndependentOfContext("*(int *)(p & ~3UL) = 0;");
5351   verifyIndependentOfContext("int *pa = (int *)&a;");
5352   verifyIndependentOfContext("return sizeof(int **);");
5353   verifyIndependentOfContext("return sizeof(int ******);");
5354   verifyIndependentOfContext("return (int **&)a;");
5355   verifyIndependentOfContext("f((*PointerToArray)[10]);");
5356   verifyFormat("void f(Type (*parameter)[10]) {}");
5357   verifyGoogleFormat("return sizeof(int**);");
5358   verifyIndependentOfContext("Type **A = static_cast<Type **>(P);");
5359   verifyGoogleFormat("Type** A = static_cast<Type**>(P);");
5360   verifyFormat("auto a = [](int **&, int ***) {};");
5361   verifyFormat("auto PointerBinding = [](const char *S) {};");
5362   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
5363   verifyFormat("[](const decltype(*a) &value) {}");
5364   verifyIndependentOfContext("typedef void (*f)(int *a);");
5365   verifyIndependentOfContext("int i{a * b};");
5366   verifyIndependentOfContext("aaa && aaa->f();");
5367   verifyIndependentOfContext("int x = ~*p;");
5368   verifyFormat("Constructor() : a(a), area(width * height) {}");
5369   verifyFormat("Constructor() : a(a), area(a, width * height) {}");
5370   verifyGoogleFormat("MACRO Constructor(const int& i) : a(a), b(b) {}");
5371   verifyFormat("void f() { f(a, c * d); }");
5372 
5373   verifyIndependentOfContext("InvalidRegions[*R] = 0;");
5374 
5375   verifyIndependentOfContext("A<int *> a;");
5376   verifyIndependentOfContext("A<int **> a;");
5377   verifyIndependentOfContext("A<int *, int *> a;");
5378   verifyIndependentOfContext("A<int *[]> a;");
5379   verifyIndependentOfContext(
5380       "const char *const p = reinterpret_cast<const char *const>(q);");
5381   verifyIndependentOfContext("A<int **, int **> a;");
5382   verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);");
5383   verifyFormat("for (char **a = b; *a; ++a) {\n}");
5384   verifyFormat("for (; a && b;) {\n}");
5385   verifyFormat("bool foo = true && [] { return false; }();");
5386 
5387   verifyFormat(
5388       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5389       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5390 
5391   verifyGoogleFormat("**outparam = 1;");
5392   verifyGoogleFormat("*outparam = a * b;");
5393   verifyGoogleFormat("int main(int argc, char** argv) {}");
5394   verifyGoogleFormat("A<int*> a;");
5395   verifyGoogleFormat("A<int**> a;");
5396   verifyGoogleFormat("A<int*, int*> a;");
5397   verifyGoogleFormat("A<int**, int**> a;");
5398   verifyGoogleFormat("f(b ? *c : *d);");
5399   verifyGoogleFormat("int a = b ? *c : *d;");
5400   verifyGoogleFormat("Type* t = **x;");
5401   verifyGoogleFormat("Type* t = *++*x;");
5402   verifyGoogleFormat("*++*x;");
5403   verifyGoogleFormat("Type* t = const_cast<T*>(&*x);");
5404   verifyGoogleFormat("Type* t = x++ * y;");
5405   verifyGoogleFormat(
5406       "const char* const p = reinterpret_cast<const char* const>(q);");
5407   verifyGoogleFormat("void f(int i = 0, SomeType** temps = NULL);");
5408   verifyGoogleFormat("void f(Bar* a = nullptr, Bar* b);");
5409   verifyGoogleFormat("template <typename T>\n"
5410                      "void f(int i = 0, SomeType** temps = NULL);");
5411 
5412   FormatStyle Left = getLLVMStyle();
5413   Left.PointerAlignment = FormatStyle::PAS_Left;
5414   verifyFormat("x = *a(x) = *a(y);", Left);
5415 
5416   verifyIndependentOfContext("a = *(x + y);");
5417   verifyIndependentOfContext("a = &(x + y);");
5418   verifyIndependentOfContext("*(x + y).call();");
5419   verifyIndependentOfContext("&(x + y)->call();");
5420   verifyFormat("void f() { &(*I).first; }");
5421 
5422   verifyIndependentOfContext("f(b * /* confusing comment */ ++c);");
5423   verifyFormat(
5424       "int *MyValues = {\n"
5425       "    *A, // Operator detection might be confused by the '{'\n"
5426       "    *BB // Operator detection might be confused by previous comment\n"
5427       "};");
5428 
5429   verifyIndependentOfContext("if (int *a = &b)");
5430   verifyIndependentOfContext("if (int &a = *b)");
5431   verifyIndependentOfContext("if (a & b[i])");
5432   verifyIndependentOfContext("if (a::b::c::d & b[i])");
5433   verifyIndependentOfContext("if (*b[i])");
5434   verifyIndependentOfContext("if (int *a = (&b))");
5435   verifyIndependentOfContext("while (int *a = &b)");
5436   verifyIndependentOfContext("size = sizeof *a;");
5437   verifyIndependentOfContext("if (a && (b = c))");
5438   verifyFormat("void f() {\n"
5439                "  for (const int &v : Values) {\n"
5440                "  }\n"
5441                "}");
5442   verifyFormat("for (int i = a * a; i < 10; ++i) {\n}");
5443   verifyFormat("for (int i = 0; i < a * a; ++i) {\n}");
5444   verifyGoogleFormat("for (int i = 0; i * 2 < z; i *= 2) {\n}");
5445 
5446   verifyFormat("#define A (!a * b)");
5447   verifyFormat("#define MACRO     \\\n"
5448                "  int *i = a * b; \\\n"
5449                "  void f(a *b);",
5450                getLLVMStyleWithColumns(19));
5451 
5452   verifyIndependentOfContext("A = new SomeType *[Length];");
5453   verifyIndependentOfContext("A = new SomeType *[Length]();");
5454   verifyIndependentOfContext("T **t = new T *;");
5455   verifyIndependentOfContext("T **t = new T *();");
5456   verifyGoogleFormat("A = new SomeType*[Length]();");
5457   verifyGoogleFormat("A = new SomeType*[Length];");
5458   verifyGoogleFormat("T** t = new T*;");
5459   verifyGoogleFormat("T** t = new T*();");
5460 
5461   FormatStyle PointerLeft = getLLVMStyle();
5462   PointerLeft.PointerAlignment = FormatStyle::PAS_Left;
5463   verifyFormat("delete *x;", PointerLeft);
5464   verifyFormat("STATIC_ASSERT((a & b) == 0);");
5465   verifyFormat("STATIC_ASSERT(0 == (a & b));");
5466   verifyFormat("template <bool a, bool b> "
5467                "typename t::if<x && y>::type f() {}");
5468   verifyFormat("template <int *y> f() {}");
5469   verifyFormat("vector<int *> v;");
5470   verifyFormat("vector<int *const> v;");
5471   verifyFormat("vector<int *const **const *> v;");
5472   verifyFormat("vector<int *volatile> v;");
5473   verifyFormat("vector<a * b> v;");
5474   verifyFormat("foo<b && false>();");
5475   verifyFormat("foo<b & 1>();");
5476   verifyFormat("decltype(*::std::declval<const T &>()) void F();");
5477   verifyFormat(
5478       "template <class T, class = typename std::enable_if<\n"
5479       "                       std::is_integral<T>::value &&\n"
5480       "                       (sizeof(T) > 1 || sizeof(T) < 8)>::type>\n"
5481       "void F();",
5482       getLLVMStyleWithColumns(76));
5483   verifyFormat(
5484       "template <class T,\n"
5485       "          class = typename ::std::enable_if<\n"
5486       "              ::std::is_array<T>{} && ::std::is_array<T>{}>::type>\n"
5487       "void F();",
5488       getGoogleStyleWithColumns(68));
5489 
5490   verifyIndependentOfContext("MACRO(int *i);");
5491   verifyIndependentOfContext("MACRO(auto *a);");
5492   verifyIndependentOfContext("MACRO(const A *a);");
5493   verifyIndependentOfContext("MACRO('0' <= c && c <= '9');");
5494   // FIXME: Is there a way to make this work?
5495   // verifyIndependentOfContext("MACRO(A *a);");
5496 
5497   verifyFormat("DatumHandle const *operator->() const { return input_; }");
5498 
5499   EXPECT_EQ("#define OP(x)                                    \\\n"
5500             "  ostream &operator<<(ostream &s, const A &a) {  \\\n"
5501             "    return s << a.DebugString();                 \\\n"
5502             "  }",
5503             format("#define OP(x) \\\n"
5504                    "  ostream &operator<<(ostream &s, const A &a) { \\\n"
5505                    "    return s << a.DebugString(); \\\n"
5506                    "  }",
5507                    getLLVMStyleWithColumns(50)));
5508 
5509   // FIXME: We cannot handle this case yet; we might be able to figure out that
5510   // foo<x> d > v; doesn't make sense.
5511   verifyFormat("foo<a<b && c> d> v;");
5512 
5513   FormatStyle PointerMiddle = getLLVMStyle();
5514   PointerMiddle.PointerAlignment = FormatStyle::PAS_Middle;
5515   verifyFormat("delete *x;", PointerMiddle);
5516   verifyFormat("int * x;", PointerMiddle);
5517   verifyFormat("template <int * y> f() {}", PointerMiddle);
5518   verifyFormat("int * f(int * a) {}", PointerMiddle);
5519   verifyFormat("int main(int argc, char ** argv) {}", PointerMiddle);
5520   verifyFormat("Test::Test(int b) : a(b * b) {}", PointerMiddle);
5521   verifyFormat("A<int *> a;", PointerMiddle);
5522   verifyFormat("A<int **> a;", PointerMiddle);
5523   verifyFormat("A<int *, int *> a;", PointerMiddle);
5524   verifyFormat("A<int * []> a;", PointerMiddle);
5525   verifyFormat("A = new SomeType *[Length]();", PointerMiddle);
5526   verifyFormat("A = new SomeType *[Length];", PointerMiddle);
5527   verifyFormat("T ** t = new T *;", PointerMiddle);
5528 }
5529 
5530 TEST_F(FormatTest, UnderstandsAttributes) {
5531   verifyFormat("SomeType s __attribute__((unused)) (InitValue);");
5532   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa __attribute__((unused))\n"
5533                "aaaaaaaaaaaaaaaaaaaaaaa(int i);");
5534 }
5535 
5536 TEST_F(FormatTest, UnderstandsEllipsis) {
5537   verifyFormat("int printf(const char *fmt, ...);");
5538   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
5539   verifyFormat("template <class... Ts> void Foo(Ts *... ts) {}");
5540 
5541   FormatStyle PointersLeft = getLLVMStyle();
5542   PointersLeft.PointerAlignment = FormatStyle::PAS_Left;
5543   verifyFormat("template <class... Ts> void Foo(Ts*... ts) {}", PointersLeft);
5544 }
5545 
5546 TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) {
5547   EXPECT_EQ("int *a;\n"
5548             "int *a;\n"
5549             "int *a;",
5550             format("int *a;\n"
5551                    "int* a;\n"
5552                    "int *a;",
5553                    getGoogleStyle()));
5554   EXPECT_EQ("int* a;\n"
5555             "int* a;\n"
5556             "int* a;",
5557             format("int* a;\n"
5558                    "int* a;\n"
5559                    "int *a;",
5560                    getGoogleStyle()));
5561   EXPECT_EQ("int *a;\n"
5562             "int *a;\n"
5563             "int *a;",
5564             format("int *a;\n"
5565                    "int * a;\n"
5566                    "int *  a;",
5567                    getGoogleStyle()));
5568 }
5569 
5570 TEST_F(FormatTest, UnderstandsRvalueReferences) {
5571   verifyFormat("int f(int &&a) {}");
5572   verifyFormat("int f(int a, char &&b) {}");
5573   verifyFormat("void f() { int &&a = b; }");
5574   verifyGoogleFormat("int f(int a, char&& b) {}");
5575   verifyGoogleFormat("void f() { int&& a = b; }");
5576 
5577   verifyIndependentOfContext("A<int &&> a;");
5578   verifyIndependentOfContext("A<int &&, int &&> a;");
5579   verifyGoogleFormat("A<int&&> a;");
5580   verifyGoogleFormat("A<int&&, int&&> a;");
5581 
5582   // Not rvalue references:
5583   verifyFormat("template <bool B, bool C> class A {\n"
5584                "  static_assert(B && C, \"Something is wrong\");\n"
5585                "};");
5586   verifyGoogleFormat("#define IF(a, b, c) if (a && (b == c))");
5587   verifyGoogleFormat("#define WHILE(a, b, c) while (a && (b == c))");
5588   verifyFormat("#define A(a, b) (a && b)");
5589 }
5590 
5591 TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) {
5592   verifyFormat("void f() {\n"
5593                "  x[aaaaaaaaa -\n"
5594                "    b] = 23;\n"
5595                "}",
5596                getLLVMStyleWithColumns(15));
5597 }
5598 
5599 TEST_F(FormatTest, FormatsCasts) {
5600   verifyFormat("Type *A = static_cast<Type *>(P);");
5601   verifyFormat("Type *A = (Type *)P;");
5602   verifyFormat("Type *A = (vector<Type *, int *>)P;");
5603   verifyFormat("int a = (int)(2.0f);");
5604   verifyFormat("int a = (int)2.0f;");
5605   verifyFormat("x[(int32)y];");
5606   verifyFormat("x = (int32)y;");
5607   verifyFormat("#define AA(X) sizeof(((X *)NULL)->a)");
5608   verifyFormat("int a = (int)*b;");
5609   verifyFormat("int a = (int)2.0f;");
5610   verifyFormat("int a = (int)~0;");
5611   verifyFormat("int a = (int)++a;");
5612   verifyFormat("int a = (int)sizeof(int);");
5613   verifyFormat("int a = (int)+2;");
5614   verifyFormat("my_int a = (my_int)2.0f;");
5615   verifyFormat("my_int a = (my_int)sizeof(int);");
5616   verifyFormat("return (my_int)aaa;");
5617   verifyFormat("#define x ((int)-1)");
5618   verifyFormat("#define LENGTH(x, y) (x) - (y) + 1");
5619   verifyFormat("#define p(q) ((int *)&q)");
5620   verifyFormat("fn(a)(b) + 1;");
5621 
5622   verifyFormat("void f() { my_int a = (my_int)*b; }");
5623   verifyFormat("void f() { return P ? (my_int)*P : (my_int)0; }");
5624   verifyFormat("my_int a = (my_int)~0;");
5625   verifyFormat("my_int a = (my_int)++a;");
5626   verifyFormat("my_int a = (my_int)-2;");
5627   verifyFormat("my_int a = (my_int)1;");
5628   verifyFormat("my_int a = (my_int *)1;");
5629   verifyFormat("my_int a = (const my_int)-1;");
5630   verifyFormat("my_int a = (const my_int *)-1;");
5631   verifyFormat("my_int a = (my_int)(my_int)-1;");
5632 
5633   // FIXME: single value wrapped with paren will be treated as cast.
5634   verifyFormat("void f(int i = (kValue)*kMask) {}");
5635 
5636   verifyFormat("{ (void)F; }");
5637 
5638   // Don't break after a cast's
5639   verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaa =\n"
5640                "    (aaaaaaaaaaaaaaaaaaaaaaaaaa *)(aaaaaaaaaaaaaaaaaaaaaa +\n"
5641                "                                   bbbbbbbbbbbbbbbbbbbbbb);");
5642 
5643   // These are not casts.
5644   verifyFormat("void f(int *) {}");
5645   verifyFormat("f(foo)->b;");
5646   verifyFormat("f(foo).b;");
5647   verifyFormat("f(foo)(b);");
5648   verifyFormat("f(foo)[b];");
5649   verifyFormat("[](foo) { return 4; }(bar);");
5650   verifyFormat("(*funptr)(foo)[4];");
5651   verifyFormat("funptrs[4](foo)[4];");
5652   verifyFormat("void f(int *);");
5653   verifyFormat("void f(int *) = 0;");
5654   verifyFormat("void f(SmallVector<int>) {}");
5655   verifyFormat("void f(SmallVector<int>);");
5656   verifyFormat("void f(SmallVector<int>) = 0;");
5657   verifyFormat("void f(int i = (kA * kB) & kMask) {}");
5658   verifyFormat("int a = sizeof(int) * b;");
5659   verifyFormat("int a = alignof(int) * b;", getGoogleStyle());
5660   verifyFormat("template <> void f<int>(int i) SOME_ANNOTATION;");
5661   verifyFormat("f(\"%\" SOME_MACRO(ll) \"d\");");
5662   verifyFormat("aaaaa &operator=(const aaaaa &) LLVM_DELETED_FUNCTION;");
5663 
5664   // These are not casts, but at some point were confused with casts.
5665   verifyFormat("virtual void foo(int *) override;");
5666   verifyFormat("virtual void foo(char &) const;");
5667   verifyFormat("virtual void foo(int *a, char *) const;");
5668   verifyFormat("int a = sizeof(int *) + b;");
5669   verifyFormat("int a = alignof(int *) + b;", getGoogleStyle());
5670 
5671   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *foo = (aaaaaaaaaaaaaaaaa *)\n"
5672                "    bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
5673   // FIXME: The indentation here is not ideal.
5674   verifyFormat(
5675       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5676       "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = (*cccccccccccccccc)\n"
5677       "        [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd];");
5678 }
5679 
5680 TEST_F(FormatTest, FormatsFunctionTypes) {
5681   verifyFormat("A<bool()> a;");
5682   verifyFormat("A<SomeType()> a;");
5683   verifyFormat("A<void (*)(int, std::string)> a;");
5684   verifyFormat("A<void *(int)>;");
5685   verifyFormat("void *(*a)(int *, SomeType *);");
5686   verifyFormat("int (*func)(void *);");
5687   verifyFormat("void f() { int (*func)(void *); }");
5688   verifyFormat("template <class CallbackClass>\n"
5689                "using MyCallback = void (CallbackClass::*)(SomeObject *Data);");
5690 
5691   verifyGoogleFormat("A<void*(int*, SomeType*)>;");
5692   verifyGoogleFormat("void* (*a)(int);");
5693   verifyGoogleFormat(
5694       "template <class CallbackClass>\n"
5695       "using MyCallback = void (CallbackClass::*)(SomeObject* Data);");
5696 
5697   // Other constructs can look somewhat like function types:
5698   verifyFormat("A<sizeof(*x)> a;");
5699   verifyFormat("#define DEREF_AND_CALL_F(x) f(*x)");
5700   verifyFormat("some_var = function(*some_pointer_var)[0];");
5701   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
5702 }
5703 
5704 TEST_F(FormatTest, FormatsPointersToArrayTypes) {
5705   verifyFormat("A (*foo_)[6];");
5706   verifyFormat("vector<int> (*foo_)[6];");
5707 }
5708 
5709 TEST_F(FormatTest, BreaksLongVariableDeclarations) {
5710   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5711                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5712   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType const\n"
5713                "    LoooooooooooooooooooooooooooooooooooooooongVariable;");
5714   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5715                "    *LoooooooooooooooooooooooooooooooooooooooongVariable;");
5716 
5717   // Different ways of ()-initializiation.
5718   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5719                "    LoooooooooooooooooooooooooooooooooooooooongVariable(1);");
5720   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5721                "    LoooooooooooooooooooooooooooooooooooooooongVariable(a);");
5722   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5723                "    LoooooooooooooooooooooooooooooooooooooooongVariable({});");
5724   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n"
5725                "    LoooooooooooooooooooooooooooooooooooooongVariable([A a]);");
5726 }
5727 
5728 TEST_F(FormatTest, BreaksLongDeclarations) {
5729   verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n"
5730                "    AnotherNameForTheLongType;");
5731   verifyFormat("typedef LongTemplateType<aaaaaaaaaaaaaaaaaaa()>\n"
5732                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
5733   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5734                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5735   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType *\n"
5736                "LoooooooooooooooooooooooooooooooongFunctionDeclaration();");
5737   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5738                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5739   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType MACRO\n"
5740                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5741   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5742                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5743   verifyFormat("decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5744                "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}");
5745   FormatStyle Indented = getLLVMStyle();
5746   Indented.IndentWrappedFunctionNames = true;
5747   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5748                "    LoooooooooooooooooooooooooooooooongFunctionDeclaration();",
5749                Indented);
5750   verifyFormat(
5751       "LoooooooooooooooooooooooooooooooooooooooongReturnType\n"
5752       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5753       Indented);
5754   verifyFormat(
5755       "LoooooooooooooooooooooooooooooooooooooooongReturnType const\n"
5756       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5757       Indented);
5758   verifyFormat(
5759       "decltype(LoooooooooooooooooooooooooooooooooooooooongName)\n"
5760       "    LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}",
5761       Indented);
5762 
5763   // FIXME: Without the comment, this breaks after "(".
5764   verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType  // break\n"
5765                "    (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();",
5766                getGoogleStyle());
5767 
5768   verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n"
5769                "                  int LoooooooooooooooooooongParam2) {}");
5770   verifyFormat(
5771       "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n"
5772       "                                   SourceLocation L, IdentifierIn *II,\n"
5773       "                                   Type *T) {}");
5774   verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n"
5775                "ReallyReaaallyLongFunctionName(\n"
5776                "    const std::string &SomeParameter,\n"
5777                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5778                "        &ReallyReallyLongParameterName,\n"
5779                "    const SomeType<string, SomeOtherTemplateParameter>\n"
5780                "        &AnotherLongParameterName) {}");
5781   verifyFormat("template <typename A>\n"
5782                "SomeLoooooooooooooooooooooongType<\n"
5783                "    typename some_namespace::SomeOtherType<A>::Type>\n"
5784                "Function() {}");
5785 
5786   verifyGoogleFormat(
5787       "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n"
5788       "    aaaaaaaaaaaaaaaaaaaaaaa;");
5789   verifyGoogleFormat(
5790       "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n"
5791       "                                   SourceLocation L) {}");
5792   verifyGoogleFormat(
5793       "some_namespace::LongReturnType\n"
5794       "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n"
5795       "    int first_long_parameter, int second_parameter) {}");
5796 
5797   verifyGoogleFormat("template <typename T>\n"
5798                      "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5799                      "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}");
5800   verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5801                      "                   int aaaaaaaaaaaaaaaaaaaaaaa);");
5802 
5803   verifyFormat("typedef size_t (*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)(\n"
5804                "    const aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5805                "        *aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5806   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5807                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>\n"
5808                "        aaaaaaaaaaaaaaaaaaaaaaaa);");
5809   verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
5810                "    vector<aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n"
5811                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>>\n"
5812                "        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5813 }
5814 
5815 TEST_F(FormatTest, FormatsArrays) {
5816   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5817                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
5818   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5819                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5820   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5821                "    [a][bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = cccccccc;");
5822   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
5823                "    [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5824                "    [bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb] = ccccccccccc;");
5825   verifyFormat(
5826       "llvm::outs() << \"aaaaaaaaaaaa: \"\n"
5827       "             << (*aaaaaaaiaaaaaaa)[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
5828       "                                  [aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
5829 
5830   verifyGoogleFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<int>\n"
5831                      "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaa];");
5832   verifyFormat(
5833       "aaaaaaaaaaa aaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaa[0]\n"
5834       "                                  .aaaaaaa[0]\n"
5835       "                                  .aaaaaaaaaaaaaaaaaaaaaa();");
5836 
5837   verifyNoCrash("a[,Y?)]", getLLVMStyleWithColumns(10));
5838 }
5839 
5840 TEST_F(FormatTest, LineStartsWithSpecialCharacter) {
5841   verifyFormat("(a)->b();");
5842   verifyFormat("--a;");
5843 }
5844 
5845 TEST_F(FormatTest, HandlesIncludeDirectives) {
5846   verifyFormat("#include <string>\n"
5847                "#include <a/b/c.h>\n"
5848                "#include \"a/b/string\"\n"
5849                "#include \"string.h\"\n"
5850                "#include \"string.h\"\n"
5851                "#include <a-a>\n"
5852                "#include < path with space >\n"
5853                "#include \"abc.h\" // this is included for ABC\n"
5854                "#include \"some long include\" // with a comment\n"
5855                "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"",
5856                getLLVMStyleWithColumns(35));
5857   EXPECT_EQ("#include \"a.h\"", format("#include  \"a.h\""));
5858   EXPECT_EQ("#include <a>", format("#include<a>"));
5859 
5860   verifyFormat("#import <string>");
5861   verifyFormat("#import <a/b/c.h>");
5862   verifyFormat("#import \"a/b/string\"");
5863   verifyFormat("#import \"string.h\"");
5864   verifyFormat("#import \"string.h\"");
5865   verifyFormat("#if __has_include(<strstream>)\n"
5866                "#include <strstream>\n"
5867                "#endif");
5868 
5869   verifyFormat("#define MY_IMPORT <a/b>");
5870 
5871   // Protocol buffer definition or missing "#".
5872   verifyFormat("import \"aaaaaaaaaaaaaaaaa/aaaaaaaaaaaaaaa\";",
5873                getLLVMStyleWithColumns(30));
5874 
5875   FormatStyle Style = getLLVMStyle();
5876   Style.AlwaysBreakBeforeMultilineStrings = true;
5877   Style.ColumnLimit = 0;
5878   verifyFormat("#import \"abc.h\"", Style);
5879 
5880   // But 'import' might also be a regular C++ namespace.
5881   verifyFormat("import::SomeFunction(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
5882                "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
5883 }
5884 
5885 //===----------------------------------------------------------------------===//
5886 // Error recovery tests.
5887 //===----------------------------------------------------------------------===//
5888 
5889 TEST_F(FormatTest, IncompleteParameterLists) {
5890   FormatStyle NoBinPacking = getLLVMStyle();
5891   NoBinPacking.BinPackParameters = false;
5892   verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n"
5893                "                        double *min_x,\n"
5894                "                        double *max_x,\n"
5895                "                        double *min_y,\n"
5896                "                        double *max_y,\n"
5897                "                        double *min_z,\n"
5898                "                        double *max_z, ) {}",
5899                NoBinPacking);
5900 }
5901 
5902 TEST_F(FormatTest, IncorrectCodeTrailingStuff) {
5903   verifyFormat("void f() { return; }\n42");
5904   verifyFormat("void f() {\n"
5905                "  if (0)\n"
5906                "    return;\n"
5907                "}\n"
5908                "42");
5909   verifyFormat("void f() { return }\n42");
5910   verifyFormat("void f() {\n"
5911                "  if (0)\n"
5912                "    return\n"
5913                "}\n"
5914                "42");
5915 }
5916 
5917 TEST_F(FormatTest, IncorrectCodeMissingSemicolon) {
5918   EXPECT_EQ("void f() { return }", format("void  f ( )  {  return  }"));
5919   EXPECT_EQ("void f() {\n"
5920             "  if (a)\n"
5921             "    return\n"
5922             "}",
5923             format("void  f  (  )  {  if  ( a )  return  }"));
5924   EXPECT_EQ("namespace N {\n"
5925             "void f()\n"
5926             "}",
5927             format("namespace  N  {  void f()  }"));
5928   EXPECT_EQ("namespace N {\n"
5929             "void f() {}\n"
5930             "void g()\n"
5931             "}",
5932             format("namespace N  { void f( ) { } void g( ) }"));
5933 }
5934 
5935 TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) {
5936   verifyFormat("int aaaaaaaa =\n"
5937                "    // Overlylongcomment\n"
5938                "    b;",
5939                getLLVMStyleWithColumns(20));
5940   verifyFormat("function(\n"
5941                "    ShortArgument,\n"
5942                "    LoooooooooooongArgument);\n",
5943                getLLVMStyleWithColumns(20));
5944 }
5945 
5946 TEST_F(FormatTest, IncorrectAccessSpecifier) {
5947   verifyFormat("public:");
5948   verifyFormat("class A {\n"
5949                "public\n"
5950                "  void f() {}\n"
5951                "};");
5952   verifyFormat("public\n"
5953                "int qwerty;");
5954   verifyFormat("public\n"
5955                "B {}");
5956   verifyFormat("public\n"
5957                "{}");
5958   verifyFormat("public\n"
5959                "B { int x; }");
5960 }
5961 
5962 TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) {
5963   verifyFormat("{");
5964   verifyFormat("#})");
5965   verifyNoCrash("(/**/[:!] ?[).");
5966 }
5967 
5968 TEST_F(FormatTest, IncorrectCodeDoNoWhile) {
5969   verifyFormat("do {\n}");
5970   verifyFormat("do {\n}\n"
5971                "f();");
5972   verifyFormat("do {\n}\n"
5973                "wheeee(fun);");
5974   verifyFormat("do {\n"
5975                "  f();\n"
5976                "}");
5977 }
5978 
5979 TEST_F(FormatTest, IncorrectCodeMissingParens) {
5980   verifyFormat("if {\n  foo;\n  foo();\n}");
5981   verifyFormat("switch {\n  foo;\n  foo();\n}");
5982   verifyFormat("for {\n  foo;\n  foo();\n}");
5983   verifyFormat("while {\n  foo;\n  foo();\n}");
5984   verifyFormat("do {\n  foo;\n  foo();\n} while;");
5985 }
5986 
5987 TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) {
5988   verifyFormat("namespace {\n"
5989                "class Foo { Foo (\n"
5990                "};\n"
5991                "} // comment");
5992 }
5993 
5994 TEST_F(FormatTest, IncorrectCodeErrorDetection) {
5995   EXPECT_EQ("{\n  {}\n", format("{\n{\n}\n"));
5996   EXPECT_EQ("{\n  {}\n", format("{\n  {\n}\n"));
5997   EXPECT_EQ("{\n  {}\n", format("{\n  {\n  }\n"));
5998   EXPECT_EQ("{\n  {}\n}\n}\n", format("{\n  {\n    }\n  }\n}\n"));
5999 
6000   EXPECT_EQ("{\n"
6001             "  {\n"
6002             "    breakme(\n"
6003             "        qwe);\n"
6004             "  }\n",
6005             format("{\n"
6006                    "    {\n"
6007                    " breakme(qwe);\n"
6008                    "}\n",
6009                    getLLVMStyleWithColumns(10)));
6010 }
6011 
6012 TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) {
6013   verifyFormat("int x = {\n"
6014                "    avariable,\n"
6015                "    b(alongervariable)};",
6016                getLLVMStyleWithColumns(25));
6017 }
6018 
6019 TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) {
6020   verifyFormat("return (a)(b){1, 2, 3};");
6021 }
6022 
6023 TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
6024   verifyFormat("vector<int> x{1, 2, 3, 4};");
6025   verifyFormat("vector<int> x{\n"
6026                "    1, 2, 3, 4,\n"
6027                "};");
6028   verifyFormat("vector<T> x{{}, {}, {}, {}};");
6029   verifyFormat("f({1, 2});");
6030   verifyFormat("auto v = Foo{-1};");
6031   verifyFormat("f({1, 2}, {{2, 3}, {4, 5}}, c, {d});");
6032   verifyFormat("Class::Class : member{1, 2, 3} {}");
6033   verifyFormat("new vector<int>{1, 2, 3};");
6034   verifyFormat("new int[3]{1, 2, 3};");
6035   verifyFormat("new int{1};");
6036   verifyFormat("return {arg1, arg2};");
6037   verifyFormat("return {arg1, SomeType{parameter}};");
6038   verifyFormat("int count = set<int>{f(), g(), h()}.size();");
6039   verifyFormat("new T{arg1, arg2};");
6040   verifyFormat("f(MyMap[{composite, key}]);");
6041   verifyFormat("class Class {\n"
6042                "  T member = {arg1, arg2};\n"
6043                "};");
6044   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
6045   verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
6046   verifyFormat("int a = std::is_integral<int>{} + 0;");
6047 
6048   verifyFormat("int foo(int i) { return fo1{}(i); }");
6049   verifyFormat("int foo(int i) { return fo1{}(i); }");
6050   verifyFormat("auto i = decltype(x){};");
6051   verifyFormat("std::vector<int> v = {1, 0 /* comment */};");
6052   verifyFormat("Node n{1, Node{1000}, //\n"
6053                "       2};");
6054   verifyFormat("Aaaa aaaaaaa{\n"
6055                "    {\n"
6056                "        aaaa,\n"
6057                "    },\n"
6058                "};");
6059 
6060   // In combination with BinPackParameters = false.
6061   FormatStyle NoBinPacking = getLLVMStyle();
6062   NoBinPacking.BinPackParameters = false;
6063   verifyFormat("const Aaaaaa aaaaa = {aaaaa,\n"
6064                "                      bbbbb,\n"
6065                "                      ccccc,\n"
6066                "                      ddddd,\n"
6067                "                      eeeee,\n"
6068                "                      ffffff,\n"
6069                "                      ggggg,\n"
6070                "                      hhhhhh,\n"
6071                "                      iiiiii,\n"
6072                "                      jjjjjj,\n"
6073                "                      kkkkkk};",
6074                NoBinPacking);
6075   verifyFormat("const Aaaaaa aaaaa = {\n"
6076                "    aaaaa,\n"
6077                "    bbbbb,\n"
6078                "    ccccc,\n"
6079                "    ddddd,\n"
6080                "    eeeee,\n"
6081                "    ffffff,\n"
6082                "    ggggg,\n"
6083                "    hhhhhh,\n"
6084                "    iiiiii,\n"
6085                "    jjjjjj,\n"
6086                "    kkkkkk,\n"
6087                "};",
6088                NoBinPacking);
6089   verifyFormat(
6090       "const Aaaaaa aaaaa = {\n"
6091       "    aaaaa,  bbbbb,  ccccc,  ddddd,  eeeee,  ffffff, ggggg, hhhhhh,\n"
6092       "    iiiiii, jjjjjj, kkkkkk, aaaaa,  bbbbb,  ccccc,  ddddd, eeeee,\n"
6093       "    ffffff, ggggg,  hhhhhh, iiiiii, jjjjjj, kkkkkk,\n"
6094       "};",
6095       NoBinPacking);
6096 
6097   // FIXME: The alignment of these trailing comments might be bad. Then again,
6098   // this might be utterly useless in real code.
6099   verifyFormat("Constructor::Constructor()\n"
6100                "    : some_value{         //\n"
6101                "                 aaaaaaa, //\n"
6102                "                 bbbbbbb} {}");
6103 
6104   // In braced lists, the first comment is always assumed to belong to the
6105   // first element. Thus, it can be moved to the next or previous line as
6106   // appropriate.
6107   EXPECT_EQ("function({// First element:\n"
6108             "          1,\n"
6109             "          // Second element:\n"
6110             "          2});",
6111             format("function({\n"
6112                    "    // First element:\n"
6113                    "    1,\n"
6114                    "    // Second element:\n"
6115                    "    2});"));
6116   EXPECT_EQ("std::vector<int> MyNumbers{\n"
6117             "    // First element:\n"
6118             "    1,\n"
6119             "    // Second element:\n"
6120             "    2};",
6121             format("std::vector<int> MyNumbers{// First element:\n"
6122                    "                           1,\n"
6123                    "                           // Second element:\n"
6124                    "                           2};",
6125                    getLLVMStyleWithColumns(30)));
6126   // A trailing comma should still lead to an enforced line break.
6127   EXPECT_EQ("vector<int> SomeVector = {\n"
6128             "    // aaa\n"
6129             "    1, 2,\n"
6130             "};",
6131             format("vector<int> SomeVector = { // aaa\n"
6132                    "    1, 2, };"));
6133 
6134   FormatStyle ExtraSpaces = getLLVMStyle();
6135   ExtraSpaces.Cpp11BracedListStyle = false;
6136   ExtraSpaces.ColumnLimit = 75;
6137   verifyFormat("vector<int> x{ 1, 2, 3, 4 };", ExtraSpaces);
6138   verifyFormat("vector<T> x{ {}, {}, {}, {} };", ExtraSpaces);
6139   verifyFormat("f({ 1, 2 });", ExtraSpaces);
6140   verifyFormat("auto v = Foo{ 1 };", ExtraSpaces);
6141   verifyFormat("f({ 1, 2 }, { { 2, 3 }, { 4, 5 } }, c, { d });", ExtraSpaces);
6142   verifyFormat("Class::Class : member{ 1, 2, 3 } {}", ExtraSpaces);
6143   verifyFormat("new vector<int>{ 1, 2, 3 };", ExtraSpaces);
6144   verifyFormat("new int[3]{ 1, 2, 3 };", ExtraSpaces);
6145   verifyFormat("return { arg1, arg2 };", ExtraSpaces);
6146   verifyFormat("return { arg1, SomeType{ parameter } };", ExtraSpaces);
6147   verifyFormat("int count = set<int>{ f(), g(), h() }.size();", ExtraSpaces);
6148   verifyFormat("new T{ arg1, arg2 };", ExtraSpaces);
6149   verifyFormat("f(MyMap[{ composite, key }]);", ExtraSpaces);
6150   verifyFormat("class Class {\n"
6151                "  T member = { arg1, arg2 };\n"
6152                "};",
6153                ExtraSpaces);
6154   verifyFormat(
6155       "foo = aaaaaaaaaaa ? vector<int>{ aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6156       "                                 aaaaaaaaaaaaaaaaaaaa, aaaaa }\n"
6157       "                  : vector<int>{ bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
6158       "                                 bbbbbbbbbbbbbbbbbbbb, bbbbb };",
6159       ExtraSpaces);
6160   verifyFormat("DoSomethingWithVector({} /* No data */);", ExtraSpaces);
6161   verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });",
6162                ExtraSpaces);
6163   verifyFormat(
6164       "someFunction(OtherParam,\n"
6165       "             BracedList{ // comment 1 (Forcing interesting break)\n"
6166       "                         param1, param2,\n"
6167       "                         // comment 2\n"
6168       "                         param3, param4 });",
6169       ExtraSpaces);
6170   verifyFormat(
6171       "std::this_thread::sleep_for(\n"
6172       "    std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
6173       ExtraSpaces);
6174   verifyFormat(
6175       "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
6176       "    aaaaaaa, aaaaaaaaaa, aaaaa, aaaaaaaaaaaaaaa, aaa, aaaaaaaaaa, a,\n"
6177       "    aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaa,\n"
6178       "    aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa, aaaaaaa, a};");
6179   verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
6180 }
6181 
6182 TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {
6183   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6184                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6185                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6186                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6187                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6188                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6189   verifyFormat("vector<int> x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6190                "                 // line comment\n"
6191                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6192                "                 1, 22, 333, 4444, 55555,\n"
6193                "                 // line comment\n"
6194                "                 1, 22, 333, 4444, 55555, 666666, 7777777,\n"
6195                "                 1, 22, 333, 4444, 55555, 666666, 7777777};");
6196   verifyFormat(
6197       "vector<int> x = {1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6198       "                 1,       22, 333, 4444, 55555, 666666, 7777777,\n"
6199       "                 1,       22, 333, 4444, 55555, 666666, // comment\n"
6200       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6201       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6202       "                 7777777, 1,  22,  333,  4444,  55555,  666666,\n"
6203       "                 7777777};");
6204   verifyFormat("static const uint16_t CallerSavedRegs64Bittttt[] = {\n"
6205                "    X86::RAX, X86::RDX, X86::RCX, X86::RSI, X86::RDI,\n"
6206                "    X86::R8,  X86::R9,  X86::R10, X86::R11, 0};");
6207   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6208                "                 1, 1, 1, 1};",
6209                getLLVMStyleWithColumns(39));
6210   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6211                "                 1, 1, 1, 1};",
6212                getLLVMStyleWithColumns(38));
6213   verifyFormat("vector<int> aaaaaaaaaaaaaaaaaaaaaa = {\n"
6214                "    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};",
6215                getLLVMStyleWithColumns(43));
6216 
6217   // Trailing commas.
6218   verifyFormat("vector<int> x = {\n"
6219                "    1, 1, 1, 1, 1, 1, 1, 1,\n"
6220                "};",
6221                getLLVMStyleWithColumns(39));
6222   verifyFormat("vector<int> x = {\n"
6223                "    1, 1, 1, 1, 1, 1, 1, 1, //\n"
6224                "};",
6225                getLLVMStyleWithColumns(39));
6226   verifyFormat("vector<int> x = {1, 1, 1, 1,\n"
6227                "                 1, 1, 1, 1,\n"
6228                "                 /**/ /**/};",
6229                getLLVMStyleWithColumns(39));
6230   verifyFormat("return {{aaaaaaaaaaaaaaaaaaaaa},\n"
6231                "        {aaaaaaaaaaaaaaaaaaa},\n"
6232                "        {aaaaaaaaaaaaaaaaaaaaa},\n"
6233                "        {aaaaaaaaaaaaaaaaa}};",
6234                getLLVMStyleWithColumns(60));
6235 
6236   // With nested lists, we should either format one item per line or all nested
6237   // lists one one line.
6238   // FIXME: For some nested lists, we can do better.
6239   verifyFormat(
6240       "SomeStruct my_struct_array = {\n"
6241       "    {aaaaaa, aaaaaaaa, aaaaaaaaaa, aaaaaaaaa, aaaaaaaaa, aaaaaaaaaa,\n"
6242       "     aaaaaaaaaaaaa, aaaaaaa, aaa},\n"
6243       "    {aaa, aaa},\n"
6244       "    {aaa, aaa},\n"
6245       "    {aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaaa, aaa},\n"
6246       "    {aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaa,\n"
6247       "     aaaaaaaaaaaa, a, aaaaaaaaaa, aaaaaaaaa, aaa}};");
6248 
6249   // No column layout should be used here.
6250   verifyFormat("aaaaaaaaaaaaaaa = {aaaaaaaaaaaaaaaaaaaaaaaaaaa, 0, 0,\n"
6251                "                   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb};");
6252 
6253   verifyNoCrash("a<,");
6254 }
6255 
6256 TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
6257   FormatStyle DoNotMerge = getLLVMStyle();
6258   DoNotMerge.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
6259 
6260   verifyFormat("void f() { return 42; }");
6261   verifyFormat("void f() {\n"
6262                "  return 42;\n"
6263                "}",
6264                DoNotMerge);
6265   verifyFormat("void f() {\n"
6266                "  // Comment\n"
6267                "}");
6268   verifyFormat("{\n"
6269                "#error {\n"
6270                "  int a;\n"
6271                "}");
6272   verifyFormat("{\n"
6273                "  int a;\n"
6274                "#error {\n"
6275                "}");
6276   verifyFormat("void f() {} // comment");
6277   verifyFormat("void f() { int a; } // comment");
6278   verifyFormat("void f() {\n"
6279                "} // comment",
6280                DoNotMerge);
6281   verifyFormat("void f() {\n"
6282                "  int a;\n"
6283                "} // comment",
6284                DoNotMerge);
6285   verifyFormat("void f() {\n"
6286                "} // comment",
6287                getLLVMStyleWithColumns(15));
6288 
6289   verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23));
6290   verifyFormat("void f() {\n  return 42;\n}", getLLVMStyleWithColumns(22));
6291 
6292   verifyFormat("void f() {}", getLLVMStyleWithColumns(11));
6293   verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10));
6294   verifyFormat("class C {\n"
6295                "  C()\n"
6296                "      : iiiiiiii(nullptr),\n"
6297                "        kkkkkkk(nullptr),\n"
6298                "        mmmmmmm(nullptr),\n"
6299                "        nnnnnnn(nullptr) {}\n"
6300                "};",
6301                getGoogleStyle());
6302 
6303   FormatStyle NoColumnLimit = getLLVMStyle();
6304   NoColumnLimit.ColumnLimit = 0;
6305   EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit));
6306   EXPECT_EQ("class C {\n"
6307             "  A() : b(0) {}\n"
6308             "};",
6309             format("class C{A():b(0){}};", NoColumnLimit));
6310   EXPECT_EQ("A()\n"
6311             "    : b(0) {\n"
6312             "}",
6313             format("A()\n:b(0)\n{\n}", NoColumnLimit));
6314 
6315   FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit;
6316   DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine =
6317       FormatStyle::SFS_None;
6318   EXPECT_EQ("A()\n"
6319             "    : b(0) {\n"
6320             "}",
6321             format("A():b(0){}", DoNotMergeNoColumnLimit));
6322   EXPECT_EQ("A()\n"
6323             "    : b(0) {\n"
6324             "}",
6325             format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit));
6326 
6327   verifyFormat("#define A          \\\n"
6328                "  void f() {       \\\n"
6329                "    int i;         \\\n"
6330                "  }",
6331                getLLVMStyleWithColumns(20));
6332   verifyFormat("#define A           \\\n"
6333                "  void f() { int i; }",
6334                getLLVMStyleWithColumns(21));
6335   verifyFormat("#define A            \\\n"
6336                "  void f() {         \\\n"
6337                "    int i;           \\\n"
6338                "  }                  \\\n"
6339                "  int j;",
6340                getLLVMStyleWithColumns(22));
6341   verifyFormat("#define A             \\\n"
6342                "  void f() { int i; } \\\n"
6343                "  int j;",
6344                getLLVMStyleWithColumns(23));
6345 }
6346 
6347 TEST_F(FormatTest, PullInlineFunctionDefinitionsIntoSingleLine) {
6348   FormatStyle MergeInlineOnly = getLLVMStyle();
6349   MergeInlineOnly.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
6350   verifyFormat("class C {\n"
6351                "  int f() { return 42; }\n"
6352                "};",
6353                MergeInlineOnly);
6354   verifyFormat("int f() {\n"
6355                "  return 42;\n"
6356                "}",
6357                MergeInlineOnly);
6358 }
6359 
6360 TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
6361   // Elaborate type variable declarations.
6362   verifyFormat("struct foo a = {bar};\nint n;");
6363   verifyFormat("class foo a = {bar};\nint n;");
6364   verifyFormat("union foo a = {bar};\nint n;");
6365 
6366   // Elaborate types inside function definitions.
6367   verifyFormat("struct foo f() {}\nint n;");
6368   verifyFormat("class foo f() {}\nint n;");
6369   verifyFormat("union foo f() {}\nint n;");
6370 
6371   // Templates.
6372   verifyFormat("template <class X> void f() {}\nint n;");
6373   verifyFormat("template <struct X> void f() {}\nint n;");
6374   verifyFormat("template <union X> void f() {}\nint n;");
6375 
6376   // Actual definitions...
6377   verifyFormat("struct {\n} n;");
6378   verifyFormat(
6379       "template <template <class T, class Y>, class Z> class X {\n} n;");
6380   verifyFormat("union Z {\n  int n;\n} x;");
6381   verifyFormat("class MACRO Z {\n} n;");
6382   verifyFormat("class MACRO(X) Z {\n} n;");
6383   verifyFormat("class __attribute__(X) Z {\n} n;");
6384   verifyFormat("class __declspec(X) Z {\n} n;");
6385   verifyFormat("class A##B##C {\n} n;");
6386   verifyFormat("class alignas(16) Z {\n} n;");
6387 
6388   // Redefinition from nested context:
6389   verifyFormat("class A::B::C {\n} n;");
6390 
6391   // Template definitions.
6392   verifyFormat(
6393       "template <typename F>\n"
6394       "Matcher(const Matcher<F> &Other,\n"
6395       "        typename enable_if_c<is_base_of<F, T>::value &&\n"
6396       "                             !is_same<F, T>::value>::type * = 0)\n"
6397       "    : Implementation(new ImplicitCastMatcher<F>(Other)) {}");
6398 
6399   // FIXME: This is still incorrectly handled at the formatter side.
6400   verifyFormat("template <> struct X < 15, i<3 && 42 < 50 && 33 < 28> {};");
6401 
6402   // FIXME:
6403   // This now gets parsed incorrectly as class definition.
6404   // verifyFormat("class A<int> f() {\n}\nint n;");
6405 
6406   // Elaborate types where incorrectly parsing the structural element would
6407   // break the indent.
6408   verifyFormat("if (true)\n"
6409                "  class X x;\n"
6410                "else\n"
6411                "  f();\n");
6412 
6413   // This is simply incomplete. Formatting is not important, but must not crash.
6414   verifyFormat("class A:");
6415 }
6416 
6417 TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
6418   EXPECT_EQ("#error Leave     all         white!!!!! space* alone!\n",
6419             format("#error Leave     all         white!!!!! space* alone!\n"));
6420   EXPECT_EQ(
6421       "#warning Leave     all         white!!!!! space* alone!\n",
6422       format("#warning Leave     all         white!!!!! space* alone!\n"));
6423   EXPECT_EQ("#error 1", format("  #  error   1"));
6424   EXPECT_EQ("#warning 1", format("  #  warning 1"));
6425 }
6426 
6427 TEST_F(FormatTest, FormatHashIfExpressions) {
6428   verifyFormat("#if AAAA && BBBB");
6429   // FIXME: Come up with a better indentation for #elif.
6430   verifyFormat(
6431       "#if !defined(AAAAAAA) && (defined CCCCCC || defined DDDDDD) &&  \\\n"
6432       "    defined(BBBBBBBB)\n"
6433       "#elif !defined(AAAAAA) && (defined CCCCC || defined DDDDDD) &&  \\\n"
6434       "    defined(BBBBBBBB)\n"
6435       "#endif",
6436       getLLVMStyleWithColumns(65));
6437 }
6438 
6439 TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) {
6440   FormatStyle AllowsMergedIf = getGoogleStyle();
6441   AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true;
6442   verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf);
6443   verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf);
6444   verifyFormat("if (true)\n#error E\n  return 42;", AllowsMergedIf);
6445   EXPECT_EQ("if (true) return 42;",
6446             format("if (true)\nreturn 42;", AllowsMergedIf));
6447   FormatStyle ShortMergedIf = AllowsMergedIf;
6448   ShortMergedIf.ColumnLimit = 25;
6449   verifyFormat("#define A \\\n"
6450                "  if (true) return 42;",
6451                ShortMergedIf);
6452   verifyFormat("#define A \\\n"
6453                "  f();    \\\n"
6454                "  if (true)\n"
6455                "#define B",
6456                ShortMergedIf);
6457   verifyFormat("#define A \\\n"
6458                "  f();    \\\n"
6459                "  if (true)\n"
6460                "g();",
6461                ShortMergedIf);
6462   verifyFormat("{\n"
6463                "#ifdef A\n"
6464                "  // Comment\n"
6465                "  if (true) continue;\n"
6466                "#endif\n"
6467                "  // Comment\n"
6468                "  if (true) continue;\n"
6469                "}",
6470                ShortMergedIf);
6471   ShortMergedIf.ColumnLimit = 29;
6472   verifyFormat("#define A                   \\\n"
6473                "  if (aaaaaaaaaa) return 1; \\\n"
6474                "  return 2;",
6475                ShortMergedIf);
6476   ShortMergedIf.ColumnLimit = 28;
6477   verifyFormat("#define A         \\\n"
6478                "  if (aaaaaaaaaa) \\\n"
6479                "    return 1;     \\\n"
6480                "  return 2;",
6481                ShortMergedIf);
6482 }
6483 
6484 TEST_F(FormatTest, BlockCommentsInControlLoops) {
6485   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6486                "  f();\n"
6487                "}");
6488   verifyFormat("if (0) /* a comment in a strange place */ {\n"
6489                "  f();\n"
6490                "} /* another comment */ else /* comment #3 */ {\n"
6491                "  g();\n"
6492                "}");
6493   verifyFormat("while (0) /* a comment in a strange place */ {\n"
6494                "  f();\n"
6495                "}");
6496   verifyFormat("for (;;) /* a comment in a strange place */ {\n"
6497                "  f();\n"
6498                "}");
6499   verifyFormat("do /* a comment in a strange place */ {\n"
6500                "  f();\n"
6501                "} /* another comment */ while (0);");
6502 }
6503 
6504 TEST_F(FormatTest, BlockComments) {
6505   EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */",
6506             format("/* *//* */  /* */\n/* *//* */  /* */"));
6507   EXPECT_EQ("/* */ a /* */ b;", format("  /* */  a/* */  b;"));
6508   EXPECT_EQ("#define A /*123*/ \\\n"
6509             "  b\n"
6510             "/* */\n"
6511             "someCall(\n"
6512             "    parameter);",
6513             format("#define A /*123*/ b\n"
6514                    "/* */\n"
6515                    "someCall(parameter);",
6516                    getLLVMStyleWithColumns(15)));
6517 
6518   EXPECT_EQ("#define A\n"
6519             "/* */ someCall(\n"
6520             "    parameter);",
6521             format("#define A\n"
6522                    "/* */someCall(parameter);",
6523                    getLLVMStyleWithColumns(15)));
6524   EXPECT_EQ("/*\n**\n*/", format("/*\n**\n*/"));
6525   EXPECT_EQ("/*\n"
6526             "*\n"
6527             " * aaaaaa\n"
6528             "*aaaaaa\n"
6529             "*/",
6530             format("/*\n"
6531                    "*\n"
6532                    " * aaaaaa aaaaaa\n"
6533                    "*/",
6534                    getLLVMStyleWithColumns(10)));
6535   EXPECT_EQ("/*\n"
6536             "**\n"
6537             "* aaaaaa\n"
6538             "*aaaaaa\n"
6539             "*/",
6540             format("/*\n"
6541                    "**\n"
6542                    "* aaaaaa aaaaaa\n"
6543                    "*/",
6544                    getLLVMStyleWithColumns(10)));
6545 
6546   FormatStyle NoBinPacking = getLLVMStyle();
6547   NoBinPacking.BinPackParameters = false;
6548   EXPECT_EQ("someFunction(1, /* comment 1 */\n"
6549             "             2, /* comment 2 */\n"
6550             "             3, /* comment 3 */\n"
6551             "             aaaa,\n"
6552             "             bbbb);",
6553             format("someFunction (1,   /* comment 1 */\n"
6554                    "                2, /* comment 2 */  \n"
6555                    "               3,   /* comment 3 */\n"
6556                    "aaaa, bbbb );",
6557                    NoBinPacking));
6558   verifyFormat(
6559       "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6560       "                     aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
6561   EXPECT_EQ(
6562       "bool aaaaaaaaaaaaa = /* trailing comment */\n"
6563       "    aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n"
6564       "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;",
6565       format(
6566           "bool       aaaaaaaaaaaaa =       /* trailing comment */\n"
6567           "    aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa    ||\n"
6568           "    aaaaaaaaaaaaaaaaaaaaaaaaaaaa   || aaaaaaaaaaaaaaaaaaaaaaaaaa;"));
6569   EXPECT_EQ(
6570       "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6571       "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;   /* comment */\n"
6572       "int cccccccccccccccccccccccccccccc;       /* comment */\n",
6573       format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n"
6574              "int      bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n"
6575              "int    cccccccccccccccccccccccccccccc;  /* comment */\n"));
6576 
6577   verifyFormat("void f(int * /* unused */) {}");
6578 
6579   EXPECT_EQ("/*\n"
6580             " **\n"
6581             " */",
6582             format("/*\n"
6583                    " **\n"
6584                    " */"));
6585   EXPECT_EQ("/*\n"
6586             " *q\n"
6587             " */",
6588             format("/*\n"
6589                    " *q\n"
6590                    " */"));
6591   EXPECT_EQ("/*\n"
6592             " * q\n"
6593             " */",
6594             format("/*\n"
6595                    " * q\n"
6596                    " */"));
6597   EXPECT_EQ("/*\n"
6598             " **/",
6599             format("/*\n"
6600                    " **/"));
6601   EXPECT_EQ("/*\n"
6602             " ***/",
6603             format("/*\n"
6604                    " ***/"));
6605 }
6606 
6607 TEST_F(FormatTest, BlockCommentsInMacros) {
6608   EXPECT_EQ("#define A          \\\n"
6609             "  {                \\\n"
6610             "    /* one line */ \\\n"
6611             "    someCall();",
6612             format("#define A {        \\\n"
6613                    "  /* one line */   \\\n"
6614                    "  someCall();",
6615                    getLLVMStyleWithColumns(20)));
6616   EXPECT_EQ("#define A          \\\n"
6617             "  {                \\\n"
6618             "    /* previous */ \\\n"
6619             "    /* one line */ \\\n"
6620             "    someCall();",
6621             format("#define A {        \\\n"
6622                    "  /* previous */   \\\n"
6623                    "  /* one line */   \\\n"
6624                    "  someCall();",
6625                    getLLVMStyleWithColumns(20)));
6626 }
6627 
6628 TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
6629   EXPECT_EQ("a = {\n"
6630             "    1111 /*    */\n"
6631             "};",
6632             format("a = {1111 /*    */\n"
6633                    "};",
6634                    getLLVMStyleWithColumns(15)));
6635   EXPECT_EQ("a = {\n"
6636             "    1111 /*      */\n"
6637             "};",
6638             format("a = {1111 /*      */\n"
6639                    "};",
6640                    getLLVMStyleWithColumns(15)));
6641 
6642   // FIXME: The formatting is still wrong here.
6643   EXPECT_EQ("a = {\n"
6644             "    1111 /*      a\n"
6645             "            */\n"
6646             "};",
6647             format("a = {1111 /*      a */\n"
6648                    "};",
6649                    getLLVMStyleWithColumns(15)));
6650 }
6651 
6652 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
6653   // FIXME: This is not what we want...
6654   verifyFormat("{\n"
6655                "// a"
6656                "// b");
6657 }
6658 
6659 TEST_F(FormatTest, FormatStarDependingOnContext) {
6660   verifyFormat("void f(int *a);");
6661   verifyFormat("void f() { f(fint * b); }");
6662   verifyFormat("class A {\n  void f(int *a);\n};");
6663   verifyFormat("class A {\n  int *a;\n};");
6664   verifyFormat("namespace a {\n"
6665                "namespace b {\n"
6666                "class A {\n"
6667                "  void f() {}\n"
6668                "  int *a;\n"
6669                "};\n"
6670                "}\n"
6671                "}");
6672 }
6673 
6674 TEST_F(FormatTest, SpecialTokensAtEndOfLine) {
6675   verifyFormat("while");
6676   verifyFormat("operator");
6677 }
6678 
6679 //===----------------------------------------------------------------------===//
6680 // Objective-C tests.
6681 //===----------------------------------------------------------------------===//
6682 
6683 TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
6684   verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;");
6685   EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;",
6686             format("-(NSUInteger)indexOfObject:(id)anObject;"));
6687   EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;"));
6688   EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;"));
6689   EXPECT_EQ("- (NSInteger)Method3:(id)anObject;",
6690             format("-(NSInteger)Method3:(id)anObject;"));
6691   EXPECT_EQ("- (NSInteger)Method4:(id)anObject;",
6692             format("-(NSInteger)Method4:(id)anObject;"));
6693   EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;",
6694             format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"));
6695   EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;",
6696             format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"));
6697   EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6698             "forAllCells:(BOOL)flag;",
6699             format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
6700                    "forAllCells:(BOOL)flag;"));
6701 
6702   // Very long objectiveC method declaration.
6703   verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n"
6704                "    (SoooooooooooooooooooooomeType *)bbbbbbbbbb;");
6705   verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n"
6706                "                    inRange:(NSRange)range\n"
6707                "                   outRange:(NSRange)out_range\n"
6708                "                  outRange1:(NSRange)out_range1\n"
6709                "                  outRange2:(NSRange)out_range2\n"
6710                "                  outRange3:(NSRange)out_range3\n"
6711                "                  outRange4:(NSRange)out_range4\n"
6712                "                  outRange5:(NSRange)out_range5\n"
6713                "                  outRange6:(NSRange)out_range6\n"
6714                "                  outRange7:(NSRange)out_range7\n"
6715                "                  outRange8:(NSRange)out_range8\n"
6716                "                  outRange9:(NSRange)out_range9;");
6717 
6718   verifyFormat("- (int)sum:(vector<int>)numbers;");
6719   verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;");
6720   // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC
6721   // protocol lists (but not for template classes):
6722   // verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;");
6723 
6724   verifyFormat("- (int (*)())foo:(int (*)())f;");
6725   verifyGoogleFormat("- (int (*)())foo:(int (*)())foo;");
6726 
6727   // If there's no return type (very rare in practice!), LLVM and Google style
6728   // agree.
6729   verifyFormat("- foo;");
6730   verifyFormat("- foo:(int)f;");
6731   verifyGoogleFormat("- foo:(int)foo;");
6732 }
6733 
6734 TEST_F(FormatTest, FormatObjCInterface) {
6735   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
6736                "@public\n"
6737                "  int field1;\n"
6738                "@protected\n"
6739                "  int field2;\n"
6740                "@private\n"
6741                "  int field3;\n"
6742                "@package\n"
6743                "  int field4;\n"
6744                "}\n"
6745                "+ (id)init;\n"
6746                "@end");
6747 
6748   verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
6749                      " @public\n"
6750                      "  int field1;\n"
6751                      " @protected\n"
6752                      "  int field2;\n"
6753                      " @private\n"
6754                      "  int field3;\n"
6755                      " @package\n"
6756                      "  int field4;\n"
6757                      "}\n"
6758                      "+ (id)init;\n"
6759                      "@end");
6760 
6761   verifyFormat("@interface /* wait for it */ Foo\n"
6762                "+ (id)init;\n"
6763                "// Look, a comment!\n"
6764                "- (int)answerWith:(int)i;\n"
6765                "@end");
6766 
6767   verifyFormat("@interface Foo\n"
6768                "@end\n"
6769                "@interface Bar\n"
6770                "@end");
6771 
6772   verifyFormat("@interface Foo : Bar\n"
6773                "+ (id)init;\n"
6774                "@end");
6775 
6776   verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
6777                "+ (id)init;\n"
6778                "@end");
6779 
6780   verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n"
6781                      "+ (id)init;\n"
6782                      "@end");
6783 
6784   verifyFormat("@interface Foo (HackStuff)\n"
6785                "+ (id)init;\n"
6786                "@end");
6787 
6788   verifyFormat("@interface Foo ()\n"
6789                "+ (id)init;\n"
6790                "@end");
6791 
6792   verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
6793                "+ (id)init;\n"
6794                "@end");
6795 
6796   verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n"
6797                      "+ (id)init;\n"
6798                      "@end");
6799 
6800   verifyFormat("@interface Foo {\n"
6801                "  int _i;\n"
6802                "}\n"
6803                "+ (id)init;\n"
6804                "@end");
6805 
6806   verifyFormat("@interface Foo : Bar {\n"
6807                "  int _i;\n"
6808                "}\n"
6809                "+ (id)init;\n"
6810                "@end");
6811 
6812   verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
6813                "  int _i;\n"
6814                "}\n"
6815                "+ (id)init;\n"
6816                "@end");
6817 
6818   verifyFormat("@interface Foo (HackStuff) {\n"
6819                "  int _i;\n"
6820                "}\n"
6821                "+ (id)init;\n"
6822                "@end");
6823 
6824   verifyFormat("@interface Foo () {\n"
6825                "  int _i;\n"
6826                "}\n"
6827                "+ (id)init;\n"
6828                "@end");
6829 
6830   verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
6831                "  int _i;\n"
6832                "}\n"
6833                "+ (id)init;\n"
6834                "@end");
6835 
6836   FormatStyle OnePerLine = getGoogleStyle();
6837   OnePerLine.BinPackParameters = false;
6838   verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
6839                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6840                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6841                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
6842                "    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
6843                "}",
6844                OnePerLine);
6845 }
6846 
6847 TEST_F(FormatTest, FormatObjCImplementation) {
6848   verifyFormat("@implementation Foo : NSObject {\n"
6849                "@public\n"
6850                "  int field1;\n"
6851                "@protected\n"
6852                "  int field2;\n"
6853                "@private\n"
6854                "  int field3;\n"
6855                "@package\n"
6856                "  int field4;\n"
6857                "}\n"
6858                "+ (id)init {\n}\n"
6859                "@end");
6860 
6861   verifyGoogleFormat("@implementation Foo : NSObject {\n"
6862                      " @public\n"
6863                      "  int field1;\n"
6864                      " @protected\n"
6865                      "  int field2;\n"
6866                      " @private\n"
6867                      "  int field3;\n"
6868                      " @package\n"
6869                      "  int field4;\n"
6870                      "}\n"
6871                      "+ (id)init {\n}\n"
6872                      "@end");
6873 
6874   verifyFormat("@implementation Foo\n"
6875                "+ (id)init {\n"
6876                "  if (true)\n"
6877                "    return nil;\n"
6878                "}\n"
6879                "// Look, a comment!\n"
6880                "- (int)answerWith:(int)i {\n"
6881                "  return i;\n"
6882                "}\n"
6883                "+ (int)answerWith:(int)i {\n"
6884                "  return i;\n"
6885                "}\n"
6886                "@end");
6887 
6888   verifyFormat("@implementation Foo\n"
6889                "@end\n"
6890                "@implementation Bar\n"
6891                "@end");
6892 
6893   EXPECT_EQ("@implementation Foo : Bar\n"
6894             "+ (id)init {\n}\n"
6895             "- (void)foo {\n}\n"
6896             "@end",
6897             format("@implementation Foo : Bar\n"
6898                    "+(id)init{}\n"
6899                    "-(void)foo{}\n"
6900                    "@end"));
6901 
6902   verifyFormat("@implementation Foo {\n"
6903                "  int _i;\n"
6904                "}\n"
6905                "+ (id)init {\n}\n"
6906                "@end");
6907 
6908   verifyFormat("@implementation Foo : Bar {\n"
6909                "  int _i;\n"
6910                "}\n"
6911                "+ (id)init {\n}\n"
6912                "@end");
6913 
6914   verifyFormat("@implementation Foo (HackStuff)\n"
6915                "+ (id)init {\n}\n"
6916                "@end");
6917   verifyFormat("@implementation ObjcClass\n"
6918                "- (void)method;\n"
6919                "{}\n"
6920                "@end");
6921 }
6922 
6923 TEST_F(FormatTest, FormatObjCProtocol) {
6924   verifyFormat("@protocol Foo\n"
6925                "@property(weak) id delegate;\n"
6926                "- (NSUInteger)numberOfThings;\n"
6927                "@end");
6928 
6929   verifyFormat("@protocol MyProtocol <NSObject>\n"
6930                "- (NSUInteger)numberOfThings;\n"
6931                "@end");
6932 
6933   verifyGoogleFormat("@protocol MyProtocol<NSObject>\n"
6934                      "- (NSUInteger)numberOfThings;\n"
6935                      "@end");
6936 
6937   verifyFormat("@protocol Foo;\n"
6938                "@protocol Bar;\n");
6939 
6940   verifyFormat("@protocol Foo\n"
6941                "@end\n"
6942                "@protocol Bar\n"
6943                "@end");
6944 
6945   verifyFormat("@protocol myProtocol\n"
6946                "- (void)mandatoryWithInt:(int)i;\n"
6947                "@optional\n"
6948                "- (void)optional;\n"
6949                "@required\n"
6950                "- (void)required;\n"
6951                "@optional\n"
6952                "@property(assign) int madProp;\n"
6953                "@end\n");
6954 
6955   verifyFormat("@property(nonatomic, assign, readonly)\n"
6956                "    int *looooooooooooooooooooooooooooongNumber;\n"
6957                "@property(nonatomic, assign, readonly)\n"
6958                "    NSString *looooooooooooooooooooooooooooongName;");
6959 
6960   verifyFormat("@implementation PR18406\n"
6961                "}\n"
6962                "@end");
6963 }
6964 
6965 TEST_F(FormatTest, FormatObjCMethodDeclarations) {
6966   verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
6967                "                   rect:(NSRect)theRect\n"
6968                "               interval:(float)theInterval {\n"
6969                "}");
6970   verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
6971                "          longKeyword:(NSRect)theRect\n"
6972                "    evenLongerKeyword:(float)theInterval\n"
6973                "                error:(NSError **)theError {\n"
6974                "}");
6975   verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
6976                "                         y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
6977                "    NS_DESIGNATED_INITIALIZER;",
6978                getLLVMStyleWithColumns(60));
6979 }
6980 
6981 TEST_F(FormatTest, FormatObjCMethodExpr) {
6982   verifyFormat("[foo bar:baz];");
6983   verifyFormat("return [foo bar:baz];");
6984   verifyFormat("return (a)[foo bar:baz];");
6985   verifyFormat("f([foo bar:baz]);");
6986   verifyFormat("f(2, [foo bar:baz]);");
6987   verifyFormat("f(2, a ? b : c);");
6988   verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
6989 
6990   // Unary operators.
6991   verifyFormat("int a = +[foo bar:baz];");
6992   verifyFormat("int a = -[foo bar:baz];");
6993   verifyFormat("int a = ![foo bar:baz];");
6994   verifyFormat("int a = ~[foo bar:baz];");
6995   verifyFormat("int a = ++[foo bar:baz];");
6996   verifyFormat("int a = --[foo bar:baz];");
6997   verifyFormat("int a = sizeof [foo bar:baz];");
6998   verifyFormat("int a = alignof [foo bar:baz];", getGoogleStyle());
6999   verifyFormat("int a = &[foo bar:baz];");
7000   verifyFormat("int a = *[foo bar:baz];");
7001   // FIXME: Make casts work, without breaking f()[4].
7002   // verifyFormat("int a = (int)[foo bar:baz];");
7003   // verifyFormat("return (int)[foo bar:baz];");
7004   // verifyFormat("(void)[foo bar:baz];");
7005   verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
7006 
7007   // Binary operators.
7008   verifyFormat("[foo bar:baz], [foo bar:baz];");
7009   verifyFormat("[foo bar:baz] = [foo bar:baz];");
7010   verifyFormat("[foo bar:baz] *= [foo bar:baz];");
7011   verifyFormat("[foo bar:baz] /= [foo bar:baz];");
7012   verifyFormat("[foo bar:baz] %= [foo bar:baz];");
7013   verifyFormat("[foo bar:baz] += [foo bar:baz];");
7014   verifyFormat("[foo bar:baz] -= [foo bar:baz];");
7015   verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
7016   verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
7017   verifyFormat("[foo bar:baz] &= [foo bar:baz];");
7018   verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
7019   verifyFormat("[foo bar:baz] |= [foo bar:baz];");
7020   verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
7021   verifyFormat("[foo bar:baz] || [foo bar:baz];");
7022   verifyFormat("[foo bar:baz] && [foo bar:baz];");
7023   verifyFormat("[foo bar:baz] | [foo bar:baz];");
7024   verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
7025   verifyFormat("[foo bar:baz] & [foo bar:baz];");
7026   verifyFormat("[foo bar:baz] == [foo bar:baz];");
7027   verifyFormat("[foo bar:baz] != [foo bar:baz];");
7028   verifyFormat("[foo bar:baz] >= [foo bar:baz];");
7029   verifyFormat("[foo bar:baz] <= [foo bar:baz];");
7030   verifyFormat("[foo bar:baz] > [foo bar:baz];");
7031   verifyFormat("[foo bar:baz] < [foo bar:baz];");
7032   verifyFormat("[foo bar:baz] >> [foo bar:baz];");
7033   verifyFormat("[foo bar:baz] << [foo bar:baz];");
7034   verifyFormat("[foo bar:baz] - [foo bar:baz];");
7035   verifyFormat("[foo bar:baz] + [foo bar:baz];");
7036   verifyFormat("[foo bar:baz] * [foo bar:baz];");
7037   verifyFormat("[foo bar:baz] / [foo bar:baz];");
7038   verifyFormat("[foo bar:baz] % [foo bar:baz];");
7039   // Whew!
7040 
7041   verifyFormat("return in[42];");
7042   verifyFormat("for (auto v : in[1]) {\n}");
7043   verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
7044   verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
7045   verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
7046   verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
7047   verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
7048   verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
7049                "}");
7050   verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
7051 
7052   verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
7053   verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
7054   verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
7055   verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
7056   verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
7057   verifyFormat("[button setAction:@selector(zoomOut:)];");
7058   verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
7059 
7060   verifyFormat("arr[[self indexForFoo:a]];");
7061   verifyFormat("throw [self errorFor:a];");
7062   verifyFormat("@throw [self errorFor:a];");
7063 
7064   verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
7065   verifyFormat("[(id)foo bar:(id) ? baz : quux];");
7066   verifyFormat("4 > 4 ? (id)a : (id)baz;");
7067 
7068   // This tests that the formatter doesn't break after "backing" but before ":",
7069   // which would be at 80 columns.
7070   verifyFormat(
7071       "void f() {\n"
7072       "  if ((self = [super initWithContentRect:contentRect\n"
7073       "                               styleMask:styleMask ?: otherMask\n"
7074       "                                 backing:NSBackingStoreBuffered\n"
7075       "                                   defer:YES]))");
7076 
7077   verifyFormat(
7078       "[foo checkThatBreakingAfterColonWorksOk:\n"
7079       "         [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
7080 
7081   verifyFormat("[myObj short:arg1 // Force line break\n"
7082                "          longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
7083                "    evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
7084                "                error:arg4];");
7085   verifyFormat(
7086       "void f() {\n"
7087       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7088       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7089       "                                     pos.width(), pos.height())\n"
7090       "                styleMask:NSBorderlessWindowMask\n"
7091       "                  backing:NSBackingStoreBuffered\n"
7092       "                    defer:NO]);\n"
7093       "}");
7094   verifyFormat(
7095       "void f() {\n"
7096       "  popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
7097       "      iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
7098       "                                 pos.width(), pos.height())\n"
7099       "                syeMask:NSBorderlessWindowMask\n"
7100       "                  bking:NSBackingStoreBuffered\n"
7101       "                    der:NO]);\n"
7102       "}",
7103       getLLVMStyleWithColumns(70));
7104   verifyFormat(
7105       "void f() {\n"
7106       "  popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
7107       "      initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
7108       "                                     pos.width(), pos.height())\n"
7109       "                styleMask:NSBorderlessWindowMask\n"
7110       "                  backing:NSBackingStoreBuffered\n"
7111       "                    defer:NO]);\n"
7112       "}",
7113       getChromiumStyle(FormatStyle::LK_Cpp));
7114   verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
7115                "                             with:contentsNativeView];");
7116 
7117   verifyFormat(
7118       "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
7119       "           owner:nillllll];");
7120 
7121   verifyFormat(
7122       "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
7123       "        forType:kBookmarkButtonDragType];");
7124 
7125   verifyFormat("[defaultCenter addObserver:self\n"
7126                "                  selector:@selector(willEnterFullscreen)\n"
7127                "                      name:kWillEnterFullscreenNotification\n"
7128                "                    object:nil];");
7129   verifyFormat("[image_rep drawInRect:drawRect\n"
7130                "             fromRect:NSZeroRect\n"
7131                "            operation:NSCompositeCopy\n"
7132                "             fraction:1.0\n"
7133                "       respectFlipped:NO\n"
7134                "                hints:nil];");
7135 
7136   verifyFormat(
7137       "scoped_nsobject<NSTextField> message(\n"
7138       "    // The frame will be fixed up when |-setMessageText:| is called.\n"
7139       "    [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
7140   verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
7141                "    aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
7142                "         aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
7143                "          aaaa:bbb];");
7144   verifyFormat("[self param:function( //\n"
7145                "                parameter)]");
7146   verifyFormat(
7147       "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7148       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
7149       "                 aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
7150 
7151   // Variadic parameters.
7152   verifyFormat(
7153       "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
7154   verifyFormat(
7155       "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7156       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
7157       "                    aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
7158   verifyFormat("[self // break\n"
7159                "      a:a\n"
7160                "    aaa:aaa];");
7161   verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
7162                "          [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
7163 }
7164 
7165 TEST_F(FormatTest, ObjCAt) {
7166   verifyFormat("@autoreleasepool");
7167   verifyFormat("@catch");
7168   verifyFormat("@class");
7169   verifyFormat("@compatibility_alias");
7170   verifyFormat("@defs");
7171   verifyFormat("@dynamic");
7172   verifyFormat("@encode");
7173   verifyFormat("@end");
7174   verifyFormat("@finally");
7175   verifyFormat("@implementation");
7176   verifyFormat("@import");
7177   verifyFormat("@interface");
7178   verifyFormat("@optional");
7179   verifyFormat("@package");
7180   verifyFormat("@private");
7181   verifyFormat("@property");
7182   verifyFormat("@protected");
7183   verifyFormat("@protocol");
7184   verifyFormat("@public");
7185   verifyFormat("@required");
7186   verifyFormat("@selector");
7187   verifyFormat("@synchronized");
7188   verifyFormat("@synthesize");
7189   verifyFormat("@throw");
7190   verifyFormat("@try");
7191 
7192   EXPECT_EQ("@interface", format("@ interface"));
7193 
7194   // The precise formatting of this doesn't matter, nobody writes code like
7195   // this.
7196   verifyFormat("@ /*foo*/ interface");
7197 }
7198 
7199 TEST_F(FormatTest, ObjCSnippets) {
7200   verifyFormat("@autoreleasepool {\n"
7201                "  foo();\n"
7202                "}");
7203   verifyFormat("@class Foo, Bar;");
7204   verifyFormat("@compatibility_alias AliasName ExistingClass;");
7205   verifyFormat("@dynamic textColor;");
7206   verifyFormat("char *buf1 = @encode(int *);");
7207   verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
7208   verifyFormat("char *buf1 = @encode(int **);");
7209   verifyFormat("Protocol *proto = @protocol(p1);");
7210   verifyFormat("SEL s = @selector(foo:);");
7211   verifyFormat("@synchronized(self) {\n"
7212                "  f();\n"
7213                "}");
7214 
7215   verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7216   verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
7217 
7218   verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
7219   verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
7220   verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;");
7221   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7222                getMozillaStyle());
7223   verifyFormat("@property BOOL editable;", getMozillaStyle());
7224   verifyFormat("@property (assign, getter=isEditable) BOOL editable;",
7225                getWebKitStyle());
7226   verifyFormat("@property BOOL editable;", getWebKitStyle());
7227 
7228   verifyFormat("@import foo.bar;\n"
7229                "@import baz;");
7230 }
7231 
7232 TEST_F(FormatTest, ObjCLiterals) {
7233   verifyFormat("@\"String\"");
7234   verifyFormat("@1");
7235   verifyFormat("@+4.8");
7236   verifyFormat("@-4");
7237   verifyFormat("@1LL");
7238   verifyFormat("@.5");
7239   verifyFormat("@'c'");
7240   verifyFormat("@true");
7241 
7242   verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
7243   verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
7244   verifyFormat("NSNumber *favoriteColor = @(Green);");
7245   verifyFormat("NSString *path = @(getenv(\"PATH\"));");
7246 
7247   verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
7248 }
7249 
7250 TEST_F(FormatTest, ObjCDictLiterals) {
7251   verifyFormat("@{");
7252   verifyFormat("@{}");
7253   verifyFormat("@{@\"one\" : @1}");
7254   verifyFormat("return @{@\"one\" : @1;");
7255   verifyFormat("@{@\"one\" : @1}");
7256 
7257   verifyFormat("@{@\"one\" : @{@2 : @1}}");
7258   verifyFormat("@{\n"
7259                "  @\"one\" : @{@2 : @1},\n"
7260                "}");
7261 
7262   verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
7263   verifyFormat("[self setDict:@{}");
7264   verifyFormat("[self setDict:@{@1 : @2}");
7265   verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
7266   verifyFormat(
7267       "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
7268   verifyFormat(
7269       "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
7270 
7271   verifyFormat("NSDictionary *d = @{\n"
7272                "  @\"nam\" : NSUserNam(),\n"
7273                "  @\"dte\" : [NSDate date],\n"
7274                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7275                "};");
7276   verifyFormat(
7277       "@{\n"
7278       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7279       "regularFont,\n"
7280       "};");
7281   verifyGoogleFormat(
7282       "@{\n"
7283       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
7284       "regularFont,\n"
7285       "};");
7286   verifyFormat(
7287       "@{\n"
7288       "  NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
7289       "      reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
7290       "};");
7291 
7292   // We should try to be robust in case someone forgets the "@".
7293   verifyFormat("NSDictionary *d = {\n"
7294                "  @\"nam\" : NSUserNam(),\n"
7295                "  @\"dte\" : [NSDate date],\n"
7296                "  @\"processInfo\" : [NSProcessInfo processInfo]\n"
7297                "};");
7298   verifyFormat("NSMutableDictionary *dictionary =\n"
7299                "    [NSMutableDictionary dictionaryWithDictionary:@{\n"
7300                "      aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
7301                "      bbbbbbbbbbbbbbbbbb : bbbbb,\n"
7302                "      cccccccccccccccc : ccccccccccccccc\n"
7303                "    }];");
7304 }
7305 
7306 TEST_F(FormatTest, ObjCArrayLiterals) {
7307   verifyFormat("@[");
7308   verifyFormat("@[]");
7309   verifyFormat(
7310       "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
7311   verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
7312   verifyFormat("NSArray *array = @[ [foo description] ];");
7313 
7314   verifyFormat(
7315       "NSArray *some_variable = @[\n"
7316       "  aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
7317       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7318       "  @\"aaaaaaaaaaaaaaaaa\",\n"
7319       "  @\"aaaaaaaaaaaaaaaaa\"\n"
7320       "];");
7321   verifyFormat("NSArray *some_variable = @[\n"
7322                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7323                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7324                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7325                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7326                "];");
7327   verifyGoogleFormat("NSArray *some_variable = @[\n"
7328                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7329                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7330                      "  @\"aaaaaaaaaaaaaaaaa\",\n"
7331                      "  @\"aaaaaaaaaaaaaaaaa\"\n"
7332                      "];");
7333   verifyFormat("NSArray *array = @[\n"
7334                "  @\"a\",\n"
7335                "  @\"a\",\n" // Trailing comma -> one per line.
7336                "];");
7337 
7338   // We should try to be robust in case someone forgets the "@".
7339   verifyFormat("NSArray *some_variable = [\n"
7340                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7341                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7342                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7343                "  @\"aaaaaaaaaaaaaaaaa\",\n"
7344                "];");
7345   verifyFormat(
7346       "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
7347       "                                             index:(NSUInteger)index\n"
7348       "                                nonDigitAttributes:\n"
7349       "                                    (NSDictionary *)noDigitAttributes;");
7350   verifyFormat(
7351       "[someFunction someLooooooooooooongParameter:\n"
7352       "                  @[ NSBundle.mainBundle.infoDictionary[@\"a\"] ]];");
7353 }
7354 
7355 TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
7356   EXPECT_EQ("{\n"
7357             "{\n"
7358             "a;\n"
7359             "b;\n"
7360             "}\n"
7361             "}",
7362             format("{\n"
7363                    "{\n"
7364                    "a;\n"
7365                    "     b;\n"
7366                    "}\n"
7367                    "}",
7368                    13, 2, getLLVMStyle()));
7369   EXPECT_EQ("{\n"
7370             "{\n"
7371             "  a;\n"
7372             "b;\n"
7373             "}\n"
7374             "}",
7375             format("{\n"
7376                    "{\n"
7377                    "     a;\n"
7378                    "b;\n"
7379                    "}\n"
7380                    "}",
7381                    9, 2, getLLVMStyle()));
7382   EXPECT_EQ("{\n"
7383             "{\n"
7384             "public:\n"
7385             "  b;\n"
7386             "}\n"
7387             "}",
7388             format("{\n"
7389                    "{\n"
7390                    "public:\n"
7391                    "     b;\n"
7392                    "}\n"
7393                    "}",
7394                    17, 2, getLLVMStyle()));
7395   EXPECT_EQ("{\n"
7396             "{\n"
7397             "a;\n"
7398             "}\n"
7399             "{\n"
7400             "  b; //\n"
7401             "}\n"
7402             "}",
7403             format("{\n"
7404                    "{\n"
7405                    "a;\n"
7406                    "}\n"
7407                    "{\n"
7408                    "           b; //\n"
7409                    "}\n"
7410                    "}",
7411                    22, 2, getLLVMStyle()));
7412   EXPECT_EQ("  {\n"
7413             "    a; //\n"
7414             "  }",
7415             format("  {\n"
7416                    "a; //\n"
7417                    "  }",
7418                    4, 2, getLLVMStyle()));
7419   EXPECT_EQ("void f() {}\n"
7420             "void g() {}",
7421             format("void f() {}\n"
7422                    "void g() {}",
7423                    13, 0, getLLVMStyle()));
7424   EXPECT_EQ("int a; // comment\n"
7425             "       // line 2\n"
7426             "int b;",
7427             format("int a; // comment\n"
7428                    "       // line 2\n"
7429                    "  int b;",
7430                    35, 0, getLLVMStyle()));
7431   EXPECT_EQ("  int a;\n"
7432             "  void\n"
7433             "  ffffff() {\n"
7434             "  }",
7435             format("  int a;\n"
7436                    "void ffffff() {}",
7437                    11, 0, getLLVMStyleWithColumns(11)));
7438 
7439   EXPECT_EQ(" void f() {\n"
7440             "#define A 1\n"
7441             " }",
7442             format(" void f() {\n"
7443                    "     #define A 1\n" // Format this line.
7444                    " }",
7445                    20, 0, getLLVMStyle()));
7446   EXPECT_EQ(" void f() {\n"
7447             "    int i;\n"
7448             "#define A \\\n"
7449             "    int i;  \\\n"
7450             "   int j;\n"
7451             "    int k;\n"
7452             " }",
7453             format(" void f() {\n"
7454                    "    int i;\n"
7455                    "#define A \\\n"
7456                    "    int i;  \\\n"
7457                    "   int j;\n"
7458                    "      int k;\n" // Format this line.
7459                    " }",
7460                    67, 0, getLLVMStyle()));
7461 }
7462 
7463 TEST_F(FormatTest, BreaksStringLiterals) {
7464   EXPECT_EQ("\"some text \"\n"
7465             "\"other\";",
7466             format("\"some text other\";", getLLVMStyleWithColumns(12)));
7467   EXPECT_EQ("\"some text \"\n"
7468             "\"other\";",
7469             format("\\\n\"some text other\";", getLLVMStyleWithColumns(12)));
7470   EXPECT_EQ(
7471       "#define A  \\\n"
7472       "  \"some \"  \\\n"
7473       "  \"text \"  \\\n"
7474       "  \"other\";",
7475       format("#define A \"some text other\";", getLLVMStyleWithColumns(12)));
7476   EXPECT_EQ(
7477       "#define A  \\\n"
7478       "  \"so \"    \\\n"
7479       "  \"text \"  \\\n"
7480       "  \"other\";",
7481       format("#define A \"so text other\";", getLLVMStyleWithColumns(12)));
7482 
7483   EXPECT_EQ("\"some text\"",
7484             format("\"some text\"", getLLVMStyleWithColumns(1)));
7485   EXPECT_EQ("\"some text\"",
7486             format("\"some text\"", getLLVMStyleWithColumns(11)));
7487   EXPECT_EQ("\"some \"\n"
7488             "\"text\"",
7489             format("\"some text\"", getLLVMStyleWithColumns(10)));
7490   EXPECT_EQ("\"some \"\n"
7491             "\"text\"",
7492             format("\"some text\"", getLLVMStyleWithColumns(7)));
7493   EXPECT_EQ("\"some\"\n"
7494             "\" tex\"\n"
7495             "\"t\"",
7496             format("\"some text\"", getLLVMStyleWithColumns(6)));
7497   EXPECT_EQ("\"some\"\n"
7498             "\" tex\"\n"
7499             "\" and\"",
7500             format("\"some tex and\"", getLLVMStyleWithColumns(6)));
7501   EXPECT_EQ("\"some\"\n"
7502             "\"/tex\"\n"
7503             "\"/and\"",
7504             format("\"some/tex/and\"", getLLVMStyleWithColumns(6)));
7505 
7506   EXPECT_EQ("variable =\n"
7507             "    \"long string \"\n"
7508             "    \"literal\";",
7509             format("variable = \"long string literal\";",
7510                    getLLVMStyleWithColumns(20)));
7511 
7512   EXPECT_EQ("variable = f(\n"
7513             "    \"long string \"\n"
7514             "    \"literal\",\n"
7515             "    short,\n"
7516             "    loooooooooooooooooooong);",
7517             format("variable = f(\"long string literal\", short, "
7518                    "loooooooooooooooooooong);",
7519                    getLLVMStyleWithColumns(20)));
7520 
7521   EXPECT_EQ(
7522       "f(g(\"long string \"\n"
7523       "    \"literal\"),\n"
7524       "  b);",
7525       format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20)));
7526   EXPECT_EQ("f(g(\"long string \"\n"
7527             "    \"literal\",\n"
7528             "    a),\n"
7529             "  b);",
7530             format("f(g(\"long string literal\", a), b);",
7531                    getLLVMStyleWithColumns(20)));
7532   EXPECT_EQ(
7533       "f(\"one two\".split(\n"
7534       "    variable));",
7535       format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)));
7536   EXPECT_EQ("f(\"one two three four five six \"\n"
7537             "  \"seven\".split(\n"
7538             "      really_looooong_variable));",
7539             format("f(\"one two three four five six seven\"."
7540                    "split(really_looooong_variable));",
7541                    getLLVMStyleWithColumns(33)));
7542 
7543   EXPECT_EQ("f(\"some \"\n"
7544             "  \"text\",\n"
7545             "  other);",
7546             format("f(\"some text\", other);", getLLVMStyleWithColumns(10)));
7547 
7548   // Only break as a last resort.
7549   verifyFormat(
7550       "aaaaaaaaaaaaaaaaaaaa(\n"
7551       "    aaaaaaaaaaaaaaaaaaaa,\n"
7552       "    aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));");
7553 
7554   EXPECT_EQ("\"splitmea\"\n"
7555             "\"trandomp\"\n"
7556             "\"oint\"",
7557             format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
7558 
7559   EXPECT_EQ("\"split/\"\n"
7560             "\"pathat/\"\n"
7561             "\"slashes\"",
7562             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7563 
7564   EXPECT_EQ("\"split/\"\n"
7565             "\"pathat/\"\n"
7566             "\"slashes\"",
7567             format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
7568   EXPECT_EQ("\"split at \"\n"
7569             "\"spaces/at/\"\n"
7570             "\"slashes.at.any$\"\n"
7571             "\"non-alphanumeric%\"\n"
7572             "\"1111111111characte\"\n"
7573             "\"rs\"",
7574             format("\"split at "
7575                    "spaces/at/"
7576                    "slashes.at."
7577                    "any$non-"
7578                    "alphanumeric%"
7579                    "1111111111characte"
7580                    "rs\"",
7581                    getLLVMStyleWithColumns(20)));
7582 
7583   // Verify that splitting the strings understands
7584   // Style::AlwaysBreakBeforeMultilineStrings.
7585   EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
7586             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
7587             "             \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
7588             format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
7589                    "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
7590                    "aaaaaaaaaaaaaaaaaaaaaa\");",
7591                    getGoogleStyle()));
7592   EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7593             "       \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";",
7594             format("return \"aaaaaaaaaaaaaaaaaaaaaa "
7595                    "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa "
7596                    "aaaaaaaaaaaaaaaaaaaaaa\";",
7597                    getGoogleStyle()));
7598   EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7599             "                \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7600             format("llvm::outs() << "
7601                    "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa"
7602                    "aaaaaaaaaaaaaaaaaaa\";"));
7603   EXPECT_EQ("ffff(\n"
7604             "    {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n"
7605             "     \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7606             format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
7607                    "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});",
7608                    getGoogleStyle()));
7609 
7610   FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
7611   AlignLeft.AlignEscapedNewlinesLeft = true;
7612   EXPECT_EQ("#define A \\\n"
7613             "  \"some \" \\\n"
7614             "  \"text \" \\\n"
7615             "  \"other\";",
7616             format("#define A \"some text other\";", AlignLeft));
7617 }
7618 
7619 TEST_F(FormatTest, BreaksStringLiteralsWithTabs) {
7620   EXPECT_EQ(
7621       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7622       "(\n"
7623       "    \"x\t\");",
7624       format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
7625              "aaaaaaa("
7626              "\"x\t\");"));
7627 }
7628 
7629 TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
7630   EXPECT_EQ(
7631       "u8\"utf8 string \"\n"
7632       "u8\"literal\";",
7633       format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)));
7634   EXPECT_EQ(
7635       "u\"utf16 string \"\n"
7636       "u\"literal\";",
7637       format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16)));
7638   EXPECT_EQ(
7639       "U\"utf32 string \"\n"
7640       "U\"literal\";",
7641       format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16)));
7642   EXPECT_EQ("L\"wide string \"\n"
7643             "L\"literal\";",
7644             format("L\"wide string literal\";", getGoogleStyleWithColumns(16)));
7645   EXPECT_EQ("@\"NSString \"\n"
7646             "@\"literal\";",
7647             format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
7648 
7649   // This input makes clang-format try to split the incomplete unicode escape
7650   // sequence, which used to lead to a crasher.
7651   verifyNoCrash(
7652       "aaaaaaaaaaaaaaaaaaaa = L\"\\udff\"'; // aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7653       getLLVMStyleWithColumns(60));
7654 }
7655 
7656 TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
7657   FormatStyle Style = getGoogleStyleWithColumns(15);
7658   EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
7659   EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
7660   EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
7661   EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
7662   EXPECT_EQ("u8R\"x(raw literal)x\";",
7663             format("u8R\"x(raw literal)x\";", Style));
7664 }
7665 
7666 TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
7667   FormatStyle Style = getLLVMStyleWithColumns(20);
7668   EXPECT_EQ(
7669       "_T(\"aaaaaaaaaaaaaa\")\n"
7670       "_T(\"aaaaaaaaaaaaaa\")\n"
7671       "_T(\"aaaaaaaaaaaa\")",
7672       format("  _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style));
7673   EXPECT_EQ("f(x, _T(\"aaaaaaaaa\")\n"
7674             "     _T(\"aaaaaa\"),\n"
7675             "  z);",
7676             format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style));
7677 
7678   // FIXME: Handle embedded spaces in one iteration.
7679   //  EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n"
7680   //            "_T(\"aaaaaaaaaaaaa\")\n"
7681   //            "_T(\"aaaaaaaaaaaaa\")\n"
7682   //            "_T(\"a\")",
7683   //            format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7684   //                   getLLVMStyleWithColumns(20)));
7685   EXPECT_EQ(
7686       "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )",
7687       format("  _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style));
7688   EXPECT_EQ("f(\n"
7689             "#if !TEST\n"
7690             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7691             "#endif\n"
7692             "    );",
7693             format("f(\n"
7694                    "#if !TEST\n"
7695                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n"
7696                    "#endif\n"
7697                    ");"));
7698   EXPECT_EQ("f(\n"
7699             "\n"
7700             "    _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));",
7701             format("f(\n"
7702                    "\n"
7703                    "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"));
7704 }
7705 
7706 TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) {
7707   EXPECT_EQ(
7708       "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7709       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7710       "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";",
7711       format("aaaaaaaaaaa  =  \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7712              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n"
7713              "  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"));
7714 }
7715 
7716 TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) {
7717   EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);",
7718             format("f(g(R\"x(raw literal)x\",   a), b);", getGoogleStyle()));
7719   EXPECT_EQ("fffffffffff(g(R\"x(\n"
7720             "multiline raw string literal xxxxxxxxxxxxxx\n"
7721             ")x\",\n"
7722             "              a),\n"
7723             "            b);",
7724             format("fffffffffff(g(R\"x(\n"
7725                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7726                    ")x\", a), b);",
7727                    getGoogleStyleWithColumns(20)));
7728   EXPECT_EQ("fffffffffff(\n"
7729             "    g(R\"x(qqq\n"
7730             "multiline raw string literal xxxxxxxxxxxxxx\n"
7731             ")x\",\n"
7732             "      a),\n"
7733             "    b);",
7734             format("fffffffffff(g(R\"x(qqq\n"
7735                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7736                    ")x\", a), b);",
7737                    getGoogleStyleWithColumns(20)));
7738 
7739   EXPECT_EQ("fffffffffff(R\"x(\n"
7740             "multiline raw string literal xxxxxxxxxxxxxx\n"
7741             ")x\");",
7742             format("fffffffffff(R\"x(\n"
7743                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7744                    ")x\");",
7745                    getGoogleStyleWithColumns(20)));
7746   EXPECT_EQ("fffffffffff(R\"x(\n"
7747             "multiline raw string literal xxxxxxxxxxxxxx\n"
7748             ")x\" + bbbbbb);",
7749             format("fffffffffff(R\"x(\n"
7750                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7751                    ")x\" +   bbbbbb);",
7752                    getGoogleStyleWithColumns(20)));
7753   EXPECT_EQ("fffffffffff(\n"
7754             "    R\"x(\n"
7755             "multiline raw string literal xxxxxxxxxxxxxx\n"
7756             ")x\" +\n"
7757             "    bbbbbb);",
7758             format("fffffffffff(\n"
7759                    " R\"x(\n"
7760                    "multiline raw string literal xxxxxxxxxxxxxx\n"
7761                    ")x\" + bbbbbb);",
7762                    getGoogleStyleWithColumns(20)));
7763 }
7764 
7765 TEST_F(FormatTest, SkipsUnknownStringLiterals) {
7766   verifyFormat("string a = \"unterminated;");
7767   EXPECT_EQ("function(\"unterminated,\n"
7768             "         OtherParameter);",
7769             format("function(  \"unterminated,\n"
7770                    "    OtherParameter);"));
7771 }
7772 
7773 TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) {
7774   FormatStyle Style = getLLVMStyle();
7775   Style.Standard = FormatStyle::LS_Cpp03;
7776   EXPECT_EQ("#define x(_a) printf(\"foo\" _a);",
7777             format("#define x(_a) printf(\"foo\"_a);", Style));
7778 }
7779 
7780 TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); }
7781 
7782 TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
7783   EXPECT_EQ("someFunction(\"aaabbbcccd\"\n"
7784             "             \"ddeeefff\");",
7785             format("someFunction(\"aaabbbcccdddeeefff\");",
7786                    getLLVMStyleWithColumns(25)));
7787   EXPECT_EQ("someFunction1234567890(\n"
7788             "    \"aaabbbcccdddeeefff\");",
7789             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7790                    getLLVMStyleWithColumns(26)));
7791   EXPECT_EQ("someFunction1234567890(\n"
7792             "    \"aaabbbcccdddeeeff\"\n"
7793             "    \"f\");",
7794             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7795                    getLLVMStyleWithColumns(25)));
7796   EXPECT_EQ("someFunction1234567890(\n"
7797             "    \"aaabbbcccdddeeeff\"\n"
7798             "    \"f\");",
7799             format("someFunction1234567890(\"aaabbbcccdddeeefff\");",
7800                    getLLVMStyleWithColumns(24)));
7801   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7802             "             \"ddde \"\n"
7803             "             \"efff\");",
7804             format("someFunction(\"aaabbbcc ddde efff\");",
7805                    getLLVMStyleWithColumns(25)));
7806   EXPECT_EQ("someFunction(\"aaabbbccc \"\n"
7807             "             \"ddeeefff\");",
7808             format("someFunction(\"aaabbbccc ddeeefff\");",
7809                    getLLVMStyleWithColumns(25)));
7810   EXPECT_EQ("someFunction1234567890(\n"
7811             "    \"aaabb \"\n"
7812             "    \"cccdddeeefff\");",
7813             format("someFunction1234567890(\"aaabb cccdddeeefff\");",
7814                    getLLVMStyleWithColumns(25)));
7815   EXPECT_EQ("#define A          \\\n"
7816             "  string s =       \\\n"
7817             "      \"123456789\"  \\\n"
7818             "      \"0\";         \\\n"
7819             "  int i;",
7820             format("#define A string s = \"1234567890\"; int i;",
7821                    getLLVMStyleWithColumns(20)));
7822   // FIXME: Put additional penalties on breaking at non-whitespace locations.
7823   EXPECT_EQ("someFunction(\"aaabbbcc \"\n"
7824             "             \"dddeeeff\"\n"
7825             "             \"f\");",
7826             format("someFunction(\"aaabbbcc dddeeefff\");",
7827                    getLLVMStyleWithColumns(25)));
7828 }
7829 
7830 TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
7831   EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
7832   EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
7833   EXPECT_EQ("\"test\"\n"
7834             "\"\\n\"",
7835             format("\"test\\n\"", getLLVMStyleWithColumns(7)));
7836   EXPECT_EQ("\"tes\\\\\"\n"
7837             "\"n\"",
7838             format("\"tes\\\\n\"", getLLVMStyleWithColumns(7)));
7839   EXPECT_EQ("\"\\\\\\\\\"\n"
7840             "\"\\n\"",
7841             format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
7842   EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
7843   EXPECT_EQ("\"\\uff01\"\n"
7844             "\"test\"",
7845             format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
7846   EXPECT_EQ("\"\\Uff01ff02\"",
7847             format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
7848   EXPECT_EQ("\"\\x000000000001\"\n"
7849             "\"next\"",
7850             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16)));
7851   EXPECT_EQ("\"\\x000000000001next\"",
7852             format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)));
7853   EXPECT_EQ("\"\\x000000000001\"",
7854             format("\"\\x000000000001\"", getLLVMStyleWithColumns(7)));
7855   EXPECT_EQ("\"test\"\n"
7856             "\"\\000000\"\n"
7857             "\"000001\"",
7858             format("\"test\\000000000001\"", getLLVMStyleWithColumns(9)));
7859   EXPECT_EQ("\"test\\000\"\n"
7860             "\"00000000\"\n"
7861             "\"1\"",
7862             format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
7863 }
7864 
7865 TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {
7866   verifyFormat("void f() {\n"
7867                "  return g() {}\n"
7868                "  void h() {}");
7869   verifyFormat("int a[] = {void forgot_closing_brace(){f();\n"
7870                "g();\n"
7871                "}");
7872 }
7873 
7874 TEST_F(FormatTest, DoNotPrematurelyEndUnwrappedLineForReturnStatements) {
7875   verifyFormat(
7876       "void f() { return C{param1, param2}.SomeCall(param1, param2); }");
7877 }
7878 
7879 TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) {
7880   verifyFormat("class X {\n"
7881                "  void f() {\n"
7882                "  }\n"
7883                "};",
7884                getLLVMStyleWithColumns(12));
7885 }
7886 
7887 TEST_F(FormatTest, ConfigurableIndentWidth) {
7888   FormatStyle EightIndent = getLLVMStyleWithColumns(18);
7889   EightIndent.IndentWidth = 8;
7890   EightIndent.ContinuationIndentWidth = 8;
7891   verifyFormat("void f() {\n"
7892                "        someFunction();\n"
7893                "        if (true) {\n"
7894                "                f();\n"
7895                "        }\n"
7896                "}",
7897                EightIndent);
7898   verifyFormat("class X {\n"
7899                "        void f() {\n"
7900                "        }\n"
7901                "};",
7902                EightIndent);
7903   verifyFormat("int x[] = {\n"
7904                "        call(),\n"
7905                "        call()};",
7906                EightIndent);
7907 }
7908 
7909 TEST_F(FormatTest, ConfigurableFunctionDeclarationIndentAfterType) {
7910   verifyFormat("double\n"
7911                "f();",
7912                getLLVMStyleWithColumns(8));
7913 }
7914 
7915 TEST_F(FormatTest, ConfigurableUseOfTab) {
7916   FormatStyle Tab = getLLVMStyleWithColumns(42);
7917   Tab.IndentWidth = 8;
7918   Tab.UseTab = FormatStyle::UT_Always;
7919   Tab.AlignEscapedNewlinesLeft = true;
7920 
7921   EXPECT_EQ("if (aaaaaaaa && // q\n"
7922             "    bb)\t\t// w\n"
7923             "\t;",
7924             format("if (aaaaaaaa &&// q\n"
7925                    "bb)// w\n"
7926                    ";",
7927                    Tab));
7928   EXPECT_EQ("if (aaa && bbb) // w\n"
7929             "\t;",
7930             format("if(aaa&&bbb)// w\n"
7931                    ";",
7932                    Tab));
7933 
7934   verifyFormat("class X {\n"
7935                "\tvoid f() {\n"
7936                "\t\tsomeFunction(parameter1,\n"
7937                "\t\t\t     parameter2);\n"
7938                "\t}\n"
7939                "};",
7940                Tab);
7941   verifyFormat("#define A                        \\\n"
7942                "\tvoid f() {               \\\n"
7943                "\t\tsomeFunction(    \\\n"
7944                "\t\t    parameter1,  \\\n"
7945                "\t\t    parameter2); \\\n"
7946                "\t}",
7947                Tab);
7948   EXPECT_EQ("void f() {\n"
7949             "\tf();\n"
7950             "\tg();\n"
7951             "}",
7952             format("void f() {\n"
7953                    "\tf();\n"
7954                    "\tg();\n"
7955                    "}",
7956                    0, 0, Tab));
7957   EXPECT_EQ("void f() {\n"
7958             "\tf();\n"
7959             "\tg();\n"
7960             "}",
7961             format("void f() {\n"
7962                    "\tf();\n"
7963                    "\tg();\n"
7964                    "}",
7965                    16, 0, Tab));
7966   EXPECT_EQ("void f() {\n"
7967             "  \tf();\n"
7968             "\tg();\n"
7969             "}",
7970             format("void f() {\n"
7971                    "  \tf();\n"
7972                    "  \tg();\n"
7973                    "}",
7974                    21, 0, Tab));
7975 
7976   Tab.TabWidth = 4;
7977   Tab.IndentWidth = 8;
7978   verifyFormat("class TabWidth4Indent8 {\n"
7979                "\t\tvoid f() {\n"
7980                "\t\t\t\tsomeFunction(parameter1,\n"
7981                "\t\t\t\t\t\t\t parameter2);\n"
7982                "\t\t}\n"
7983                "};",
7984                Tab);
7985 
7986   Tab.TabWidth = 4;
7987   Tab.IndentWidth = 4;
7988   verifyFormat("class TabWidth4Indent4 {\n"
7989                "\tvoid f() {\n"
7990                "\t\tsomeFunction(parameter1,\n"
7991                "\t\t\t\t\t parameter2);\n"
7992                "\t}\n"
7993                "};",
7994                Tab);
7995 
7996   Tab.TabWidth = 8;
7997   Tab.IndentWidth = 4;
7998   verifyFormat("class TabWidth8Indent4 {\n"
7999                "    void f() {\n"
8000                "\tsomeFunction(parameter1,\n"
8001                "\t\t     parameter2);\n"
8002                "    }\n"
8003                "};",
8004                Tab);
8005 
8006   Tab.TabWidth = 8;
8007   Tab.IndentWidth = 8;
8008   EXPECT_EQ("/*\n"
8009             "\t      a\t\tcomment\n"
8010             "\t      in multiple lines\n"
8011             "       */",
8012             format("   /*\t \t \n"
8013                    " \t \t a\t\tcomment\t \t\n"
8014                    " \t \t in multiple lines\t\n"
8015                    " \t  */",
8016                    Tab));
8017 
8018   Tab.UseTab = FormatStyle::UT_ForIndentation;
8019   verifyFormat("{\n"
8020                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8021                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8022                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8023                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8024                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8025                "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8026                "};",
8027                Tab);
8028   verifyFormat("enum A {\n"
8029                "\ta1, // Force multiple lines\n"
8030                "\ta2,\n"
8031                "\ta3\n"
8032                "};",
8033                Tab);
8034   EXPECT_EQ("if (aaaaaaaa && // q\n"
8035             "    bb)         // w\n"
8036             "\t;",
8037             format("if (aaaaaaaa &&// q\n"
8038                    "bb)// w\n"
8039                    ";",
8040                    Tab));
8041   verifyFormat("class X {\n"
8042                "\tvoid f() {\n"
8043                "\t\tsomeFunction(parameter1,\n"
8044                "\t\t             parameter2);\n"
8045                "\t}\n"
8046                "};",
8047                Tab);
8048   verifyFormat("{\n"
8049                "\tQ({\n"
8050                "\t\tint a;\n"
8051                "\t\tsomeFunction(aaaaaaaa,\n"
8052                "\t\t             bbbbbbb);\n"
8053                "\t}, p);\n"
8054                "}",
8055                Tab);
8056   EXPECT_EQ("{\n"
8057             "\t/* aaaa\n"
8058             "\t   bbbb */\n"
8059             "}",
8060             format("{\n"
8061                    "/* aaaa\n"
8062                    "   bbbb */\n"
8063                    "}",
8064                    Tab));
8065   EXPECT_EQ("{\n"
8066             "\t/*\n"
8067             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8068             "\t  bbbbbbbbbbbbb\n"
8069             "\t*/\n"
8070             "}",
8071             format("{\n"
8072                    "/*\n"
8073                    "  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8074                    "*/\n"
8075                    "}",
8076                    Tab));
8077   EXPECT_EQ("{\n"
8078             "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8079             "\t// bbbbbbbbbbbbb\n"
8080             "}",
8081             format("{\n"
8082                    "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8083                    "}",
8084                    Tab));
8085   EXPECT_EQ("{\n"
8086             "\t/*\n"
8087             "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8088             "\t  bbbbbbbbbbbbb\n"
8089             "\t*/\n"
8090             "}",
8091             format("{\n"
8092                    "\t/*\n"
8093                    "\t  aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8094                    "\t*/\n"
8095                    "}",
8096                    Tab));
8097   EXPECT_EQ("{\n"
8098             "\t/*\n"
8099             "\n"
8100             "\t*/\n"
8101             "}",
8102             format("{\n"
8103                    "\t/*\n"
8104                    "\n"
8105                    "\t*/\n"
8106                    "}",
8107                    Tab));
8108   EXPECT_EQ("{\n"
8109             "\t/*\n"
8110             " asdf\n"
8111             "\t*/\n"
8112             "}",
8113             format("{\n"
8114                    "\t/*\n"
8115                    " asdf\n"
8116                    "\t*/\n"
8117                    "}",
8118                    Tab));
8119 
8120   Tab.UseTab = FormatStyle::UT_Never;
8121   EXPECT_EQ("/*\n"
8122             "              a\t\tcomment\n"
8123             "              in multiple lines\n"
8124             "       */",
8125             format("   /*\t \t \n"
8126                    " \t \t a\t\tcomment\t \t\n"
8127                    " \t \t in multiple lines\t\n"
8128                    " \t  */",
8129                    Tab));
8130   EXPECT_EQ("/* some\n"
8131             "   comment */",
8132             format(" \t \t /* some\n"
8133                    " \t \t    comment */",
8134                    Tab));
8135   EXPECT_EQ("int a; /* some\n"
8136             "   comment */",
8137             format(" \t \t int a; /* some\n"
8138                    " \t \t    comment */",
8139                    Tab));
8140 
8141   EXPECT_EQ("int a; /* some\n"
8142             "comment */",
8143             format(" \t \t int\ta; /* some\n"
8144                    " \t \t    comment */",
8145                    Tab));
8146   EXPECT_EQ("f(\"\t\t\"); /* some\n"
8147             "    comment */",
8148             format(" \t \t f(\"\t\t\"); /* some\n"
8149                    " \t \t    comment */",
8150                    Tab));
8151   EXPECT_EQ("{\n"
8152             "  /*\n"
8153             "   * Comment\n"
8154             "   */\n"
8155             "  int i;\n"
8156             "}",
8157             format("{\n"
8158                    "\t/*\n"
8159                    "\t * Comment\n"
8160                    "\t */\n"
8161                    "\t int i;\n"
8162                    "}"));
8163 }
8164 
8165 TEST_F(FormatTest, CalculatesOriginalColumn) {
8166   EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8167             "q\"; /* some\n"
8168             "       comment */",
8169             format("  \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8170                    "q\"; /* some\n"
8171                    "       comment */",
8172                    getLLVMStyle()));
8173   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8174             "/* some\n"
8175             "   comment */",
8176             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n"
8177                    " /* some\n"
8178                    "    comment */",
8179                    getLLVMStyle()));
8180   EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8181             "qqq\n"
8182             "/* some\n"
8183             "   comment */",
8184             format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8185                    "qqq\n"
8186                    " /* some\n"
8187                    "    comment */",
8188                    getLLVMStyle()));
8189   EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8190             "wwww; /* some\n"
8191             "         comment */",
8192             format("  inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n"
8193                    "wwww; /* some\n"
8194                    "         comment */",
8195                    getLLVMStyle()));
8196 }
8197 
8198 TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
8199   FormatStyle NoSpace = getLLVMStyle();
8200   NoSpace.SpaceBeforeParens = FormatStyle::SBPO_Never;
8201 
8202   verifyFormat("while(true)\n"
8203                "  continue;",
8204                NoSpace);
8205   verifyFormat("for(;;)\n"
8206                "  continue;",
8207                NoSpace);
8208   verifyFormat("if(true)\n"
8209                "  f();\n"
8210                "else if(true)\n"
8211                "  f();",
8212                NoSpace);
8213   verifyFormat("do {\n"
8214                "  do_something();\n"
8215                "} while(something());",
8216                NoSpace);
8217   verifyFormat("switch(x) {\n"
8218                "default:\n"
8219                "  break;\n"
8220                "}",
8221                NoSpace);
8222   verifyFormat("auto i = std::make_unique<int>(5);", NoSpace);
8223   verifyFormat("size_t x = sizeof(x);", NoSpace);
8224   verifyFormat("auto f(int x) -> decltype(x);", NoSpace);
8225   verifyFormat("int f(T x) noexcept(x.create());", NoSpace);
8226   verifyFormat("alignas(128) char a[128];", NoSpace);
8227   verifyFormat("size_t x = alignof(MyType);", NoSpace);
8228   verifyFormat("static_assert(sizeof(char) == 1, \"Impossible!\");", NoSpace);
8229   verifyFormat("int f() throw(Deprecated);", NoSpace);
8230 
8231   FormatStyle Space = getLLVMStyle();
8232   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
8233 
8234   verifyFormat("int f ();", Space);
8235   verifyFormat("void f (int a, T b) {\n"
8236                "  while (true)\n"
8237                "    continue;\n"
8238                "}",
8239                Space);
8240   verifyFormat("if (true)\n"
8241                "  f ();\n"
8242                "else if (true)\n"
8243                "  f ();",
8244                Space);
8245   verifyFormat("do {\n"
8246                "  do_something ();\n"
8247                "} while (something ());",
8248                Space);
8249   verifyFormat("switch (x) {\n"
8250                "default:\n"
8251                "  break;\n"
8252                "}",
8253                Space);
8254   verifyFormat("A::A () : a (1) {}", Space);
8255   verifyFormat("void f () __attribute__ ((asdf));", Space);
8256   verifyFormat("*(&a + 1);\n"
8257                "&((&a)[1]);\n"
8258                "a[(b + c) * d];\n"
8259                "(((a + 1) * 2) + 3) * 4;",
8260                Space);
8261   verifyFormat("#define A(x) x", Space);
8262   verifyFormat("#define A (x) x", Space);
8263   verifyFormat("#if defined(x)\n"
8264                "#endif",
8265                Space);
8266   verifyFormat("auto i = std::make_unique<int> (5);", Space);
8267   verifyFormat("size_t x = sizeof (x);", Space);
8268   verifyFormat("auto f (int x) -> decltype (x);", Space);
8269   verifyFormat("int f (T x) noexcept (x.create ());", Space);
8270   verifyFormat("alignas (128) char a[128];", Space);
8271   verifyFormat("size_t x = alignof (MyType);", Space);
8272   verifyFormat("static_assert (sizeof (char) == 1, \"Impossible!\");", Space);
8273   verifyFormat("int f () throw (Deprecated);", Space);
8274 }
8275 
8276 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
8277   FormatStyle Spaces = getLLVMStyle();
8278 
8279   Spaces.SpacesInParentheses = true;
8280   verifyFormat("call( x, y, z );", Spaces);
8281   verifyFormat("call();", Spaces);
8282   verifyFormat("std::function<void( int, int )> callback;", Spaces);
8283   verifyFormat("while ( (bool)1 )\n"
8284                "  continue;",
8285                Spaces);
8286   verifyFormat("for ( ;; )\n"
8287                "  continue;",
8288                Spaces);
8289   verifyFormat("if ( true )\n"
8290                "  f();\n"
8291                "else if ( true )\n"
8292                "  f();",
8293                Spaces);
8294   verifyFormat("do {\n"
8295                "  do_something( (int)i );\n"
8296                "} while ( something() );",
8297                Spaces);
8298   verifyFormat("switch ( x ) {\n"
8299                "default:\n"
8300                "  break;\n"
8301                "}",
8302                Spaces);
8303 
8304   Spaces.SpacesInParentheses = false;
8305   Spaces.SpacesInCStyleCastParentheses = true;
8306   verifyFormat("Type *A = ( Type * )P;", Spaces);
8307   verifyFormat("Type *A = ( vector<Type *, int *> )P;", Spaces);
8308   verifyFormat("x = ( int32 )y;", Spaces);
8309   verifyFormat("int a = ( int )(2.0f);", Spaces);
8310   verifyFormat("#define AA(X) sizeof((( X * )NULL)->a)", Spaces);
8311   verifyFormat("my_int a = ( my_int )sizeof(int);", Spaces);
8312   verifyFormat("#define x (( int )-1)", Spaces);
8313 
8314   // Run the first set of tests again with:
8315   Spaces.SpacesInParentheses = false, Spaces.SpaceInEmptyParentheses = true;
8316   Spaces.SpacesInCStyleCastParentheses = true;
8317   verifyFormat("call(x, y, z);", Spaces);
8318   verifyFormat("call( );", Spaces);
8319   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8320   verifyFormat("while (( bool )1)\n"
8321                "  continue;",
8322                Spaces);
8323   verifyFormat("for (;;)\n"
8324                "  continue;",
8325                Spaces);
8326   verifyFormat("if (true)\n"
8327                "  f( );\n"
8328                "else if (true)\n"
8329                "  f( );",
8330                Spaces);
8331   verifyFormat("do {\n"
8332                "  do_something(( int )i);\n"
8333                "} while (something( ));",
8334                Spaces);
8335   verifyFormat("switch (x) {\n"
8336                "default:\n"
8337                "  break;\n"
8338                "}",
8339                Spaces);
8340 
8341   // Run the first set of tests again with:
8342   Spaces.SpaceAfterCStyleCast = true;
8343   verifyFormat("call(x, y, z);", Spaces);
8344   verifyFormat("call( );", Spaces);
8345   verifyFormat("std::function<void(int, int)> callback;", Spaces);
8346   verifyFormat("while (( bool ) 1)\n"
8347                "  continue;",
8348                Spaces);
8349   verifyFormat("for (;;)\n"
8350                "  continue;",
8351                Spaces);
8352   verifyFormat("if (true)\n"
8353                "  f( );\n"
8354                "else if (true)\n"
8355                "  f( );",
8356                Spaces);
8357   verifyFormat("do {\n"
8358                "  do_something(( int ) i);\n"
8359                "} while (something( ));",
8360                Spaces);
8361   verifyFormat("switch (x) {\n"
8362                "default:\n"
8363                "  break;\n"
8364                "}",
8365                Spaces);
8366 
8367   // Run subset of tests again with:
8368   Spaces.SpacesInCStyleCastParentheses = false;
8369   Spaces.SpaceAfterCStyleCast = true;
8370   verifyFormat("while ((bool) 1)\n"
8371                "  continue;",
8372                Spaces);
8373   verifyFormat("do {\n"
8374                "  do_something((int) i);\n"
8375                "} while (something( ));",
8376                Spaces);
8377 }
8378 
8379 TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {
8380   verifyFormat("int a[5];");
8381   verifyFormat("a[3] += 42;");
8382 
8383   FormatStyle Spaces = getLLVMStyle();
8384   Spaces.SpacesInSquareBrackets = true;
8385   // Lambdas unchanged.
8386   verifyFormat("int c = []() -> int { return 2; }();\n", Spaces);
8387   verifyFormat("return [i, args...] {};", Spaces);
8388 
8389   // Not lambdas.
8390   verifyFormat("int a[ 5 ];", Spaces);
8391   verifyFormat("a[ 3 ] += 42;", Spaces);
8392   verifyFormat("constexpr char hello[]{\"hello\"};", Spaces);
8393   verifyFormat("double &operator[](int i) { return 0; }\n"
8394                "int i;",
8395                Spaces);
8396   verifyFormat("std::unique_ptr<int[]> foo() {}", Spaces);
8397   verifyFormat("int i = a[ a ][ a ]->f();", Spaces);
8398   verifyFormat("int i = (*b)[ a ]->f();", Spaces);
8399 }
8400 
8401 TEST_F(FormatTest, ConfigurableSpaceBeforeAssignmentOperators) {
8402   verifyFormat("int a = 5;");
8403   verifyFormat("a += 42;");
8404   verifyFormat("a or_eq 8;");
8405 
8406   FormatStyle Spaces = getLLVMStyle();
8407   Spaces.SpaceBeforeAssignmentOperators = false;
8408   verifyFormat("int a= 5;", Spaces);
8409   verifyFormat("a+= 42;", Spaces);
8410   verifyFormat("a or_eq 8;", Spaces);
8411 }
8412 
8413 TEST_F(FormatTest, AlignConsecutiveAssignments) {
8414   FormatStyle Alignment = getLLVMStyle();
8415   Alignment.AlignConsecutiveAssignments = false;
8416   verifyFormat("int a = 5;\n"
8417                "int oneTwoThree = 123;",
8418                Alignment);
8419   verifyFormat("int a = 5;\n"
8420                "int oneTwoThree = 123;",
8421                Alignment);
8422 
8423   Alignment.AlignConsecutiveAssignments = true;
8424   verifyFormat("int a           = 5;\n"
8425                "int oneTwoThree = 123;",
8426                Alignment);
8427   verifyFormat("int a           = method();\n"
8428                "int oneTwoThree = 133;",
8429                Alignment);
8430   verifyFormat("a &= 5;\n"
8431                "bcd *= 5;\n"
8432                "ghtyf += 5;\n"
8433                "dvfvdb -= 5;\n"
8434                "a /= 5;\n"
8435                "vdsvsv %= 5;\n"
8436                "sfdbddfbdfbb ^= 5;\n"
8437                "dvsdsv |= 5;\n"
8438                "int dsvvdvsdvvv = 123;",
8439                Alignment);
8440   verifyFormat("int i = 1, j = 10;\n"
8441                "something = 2000;",
8442                Alignment);
8443   verifyFormat("something = 2000;\n"
8444                "int i = 1, j = 10;\n",
8445                Alignment);
8446   verifyFormat("something = 2000;\n"
8447                "another   = 911;\n"
8448                "int i = 1, j = 10;\n"
8449                "oneMore = 1;\n"
8450                "i       = 2;",
8451                Alignment);
8452   verifyFormat("int a   = 5;\n"
8453                "int one = 1;\n"
8454                "method();\n"
8455                "int oneTwoThree = 123;\n"
8456                "int oneTwo      = 12;",
8457                Alignment);
8458   verifyFormat("int oneTwoThree = 123; // comment\n"
8459                "int oneTwo      = 12;  // comment",
8460                Alignment);
8461   EXPECT_EQ("int a = 5;\n"
8462             "\n"
8463             "int oneTwoThree = 123;",
8464             format("int a       = 5;\n"
8465                    "\n"
8466                    "int oneTwoThree= 123;",
8467                    Alignment));
8468   EXPECT_EQ("int a   = 5;\n"
8469             "int one = 1;\n"
8470             "\n"
8471             "int oneTwoThree = 123;",
8472             format("int a = 5;\n"
8473                    "int one = 1;\n"
8474                    "\n"
8475                    "int oneTwoThree = 123;",
8476                    Alignment));
8477   EXPECT_EQ("int a   = 5;\n"
8478             "int one = 1;\n"
8479             "\n"
8480             "int oneTwoThree = 123;\n"
8481             "int oneTwo      = 12;",
8482             format("int a = 5;\n"
8483                    "int one = 1;\n"
8484                    "\n"
8485                    "int oneTwoThree = 123;\n"
8486                    "int oneTwo = 12;",
8487                    Alignment));
8488   Alignment.AlignEscapedNewlinesLeft = true;
8489   verifyFormat("#define A               \\\n"
8490                "  int aaaa       = 12;  \\\n"
8491                "  int b          = 23;  \\\n"
8492                "  int ccc        = 234; \\\n"
8493                "  int dddddddddd = 2345;",
8494                Alignment);
8495   Alignment.AlignEscapedNewlinesLeft = false;
8496   verifyFormat("#define A                                                      "
8497                "                \\\n"
8498                "  int aaaa       = 12;                                         "
8499                "                \\\n"
8500                "  int b          = 23;                                         "
8501                "                \\\n"
8502                "  int ccc        = 234;                                        "
8503                "                \\\n"
8504                "  int dddddddddd = 2345;",
8505                Alignment);
8506   verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int "
8507                "k = 4, int l = 5,\n"
8508                "                  int m = 6) {\n"
8509                "  int j      = 10;\n"
8510                "  otherThing = 1;\n"
8511                "}",
8512                Alignment);
8513   verifyFormat("void SomeFunction(int parameter = 0) {\n"
8514                "  int i   = 1;\n"
8515                "  int j   = 2;\n"
8516                "  int big = 10000;\n"
8517                "}",
8518                Alignment);
8519   verifyFormat("class C {\n"
8520                "public:\n"
8521                "  int i = 1;\n"
8522                "  virtual void f() = 0;\n"
8523                "};",
8524                Alignment);
8525   verifyFormat("int i = 1;\n"
8526                "if (SomeType t = getSomething()) {\n"
8527                "}\n"
8528                "int j   = 2;\n"
8529                "int big = 10000;",
8530                Alignment);
8531   verifyFormat("int j = 7;\n"
8532                "for (int k = 0; k < N; ++k) {\n"
8533                "}\n"
8534                "int j   = 2;\n"
8535                "int big = 10000;\n"
8536                "}",
8537                Alignment);
8538   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
8539   verifyFormat("int i = 1;\n"
8540                "LooooooooooongType loooooooooooooooooooooongVariable\n"
8541                "    = someLooooooooooooooooongFunction();\n"
8542                "int j = 2;",
8543                Alignment);
8544   Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
8545   verifyFormat("int i = 1;\n"
8546                "LooooooooooongType loooooooooooooooooooooongVariable =\n"
8547                "    someLooooooooooooooooongFunction();\n"
8548                "int j = 2;",
8549                Alignment);
8550   // FIXME: Should align all three assignments
8551   verifyFormat(
8552       "int i      = 1;\n"
8553       "SomeType a = SomeFunction(looooooooooooooooooooooongParameterA,\n"
8554       "                          loooooooooooooooooooooongParameterB);\n"
8555       "int j = 2;",
8556       Alignment);
8557 }
8558 
8559 TEST_F(FormatTest, LinuxBraceBreaking) {
8560   FormatStyle LinuxBraceStyle = getLLVMStyle();
8561   LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux;
8562   verifyFormat("namespace a\n"
8563                "{\n"
8564                "class A\n"
8565                "{\n"
8566                "  void f()\n"
8567                "  {\n"
8568                "    if (true) {\n"
8569                "      a();\n"
8570                "      b();\n"
8571                "    }\n"
8572                "  }\n"
8573                "  void g() { return; }\n"
8574                "};\n"
8575                "struct B {\n"
8576                "  int x;\n"
8577                "};\n"
8578                "}\n",
8579                LinuxBraceStyle);
8580   verifyFormat("enum X {\n"
8581                "  Y = 0,\n"
8582                "}\n",
8583                LinuxBraceStyle);
8584   verifyFormat("struct S {\n"
8585                "  int Type;\n"
8586                "  union {\n"
8587                "    int x;\n"
8588                "    double y;\n"
8589                "  } Value;\n"
8590                "  class C\n"
8591                "  {\n"
8592                "    MyFavoriteType Value;\n"
8593                "  } Class;\n"
8594                "}\n",
8595                LinuxBraceStyle);
8596 }
8597 
8598 TEST_F(FormatTest, StroustrupBraceBreaking) {
8599   FormatStyle StroustrupBraceStyle = getLLVMStyle();
8600   StroustrupBraceStyle.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
8601   verifyFormat("namespace a {\n"
8602                "class A {\n"
8603                "  void f()\n"
8604                "  {\n"
8605                "    if (true) {\n"
8606                "      a();\n"
8607                "      b();\n"
8608                "    }\n"
8609                "  }\n"
8610                "  void g() { return; }\n"
8611                "};\n"
8612                "struct B {\n"
8613                "  int x;\n"
8614                "};\n"
8615                "}\n",
8616                StroustrupBraceStyle);
8617 
8618   verifyFormat("void foo()\n"
8619                "{\n"
8620                "  if (a) {\n"
8621                "    a();\n"
8622                "  }\n"
8623                "  else {\n"
8624                "    b();\n"
8625                "  }\n"
8626                "}\n",
8627                StroustrupBraceStyle);
8628 
8629   verifyFormat("#ifdef _DEBUG\n"
8630                "int foo(int i = 0)\n"
8631                "#else\n"
8632                "int foo(int i = 5)\n"
8633                "#endif\n"
8634                "{\n"
8635                "  return i;\n"
8636                "}",
8637                StroustrupBraceStyle);
8638 
8639   verifyFormat("void foo() {}\n"
8640                "void bar()\n"
8641                "#ifdef _DEBUG\n"
8642                "{\n"
8643                "  foo();\n"
8644                "}\n"
8645                "#else\n"
8646                "{\n"
8647                "}\n"
8648                "#endif",
8649                StroustrupBraceStyle);
8650 
8651   verifyFormat("void foobar() { int i = 5; }\n"
8652                "#ifdef _DEBUG\n"
8653                "void bar() {}\n"
8654                "#else\n"
8655                "void bar() { foobar(); }\n"
8656                "#endif",
8657                StroustrupBraceStyle);
8658 }
8659 
8660 TEST_F(FormatTest, AllmanBraceBreaking) {
8661   FormatStyle AllmanBraceStyle = getLLVMStyle();
8662   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
8663   verifyFormat("namespace a\n"
8664                "{\n"
8665                "class A\n"
8666                "{\n"
8667                "  void f()\n"
8668                "  {\n"
8669                "    if (true)\n"
8670                "    {\n"
8671                "      a();\n"
8672                "      b();\n"
8673                "    }\n"
8674                "  }\n"
8675                "  void g() { return; }\n"
8676                "};\n"
8677                "struct B\n"
8678                "{\n"
8679                "  int x;\n"
8680                "};\n"
8681                "}",
8682                AllmanBraceStyle);
8683 
8684   verifyFormat("void f()\n"
8685                "{\n"
8686                "  if (true)\n"
8687                "  {\n"
8688                "    a();\n"
8689                "  }\n"
8690                "  else if (false)\n"
8691                "  {\n"
8692                "    b();\n"
8693                "  }\n"
8694                "  else\n"
8695                "  {\n"
8696                "    c();\n"
8697                "  }\n"
8698                "}\n",
8699                AllmanBraceStyle);
8700 
8701   verifyFormat("void f()\n"
8702                "{\n"
8703                "  for (int i = 0; i < 10; ++i)\n"
8704                "  {\n"
8705                "    a();\n"
8706                "  }\n"
8707                "  while (false)\n"
8708                "  {\n"
8709                "    b();\n"
8710                "  }\n"
8711                "  do\n"
8712                "  {\n"
8713                "    c();\n"
8714                "  } while (false)\n"
8715                "}\n",
8716                AllmanBraceStyle);
8717 
8718   verifyFormat("void f(int a)\n"
8719                "{\n"
8720                "  switch (a)\n"
8721                "  {\n"
8722                "  case 0:\n"
8723                "    break;\n"
8724                "  case 1:\n"
8725                "  {\n"
8726                "    break;\n"
8727                "  }\n"
8728                "  case 2:\n"
8729                "  {\n"
8730                "  }\n"
8731                "  break;\n"
8732                "  default:\n"
8733                "    break;\n"
8734                "  }\n"
8735                "}\n",
8736                AllmanBraceStyle);
8737 
8738   verifyFormat("enum X\n"
8739                "{\n"
8740                "  Y = 0,\n"
8741                "}\n",
8742                AllmanBraceStyle);
8743   verifyFormat("enum X\n"
8744                "{\n"
8745                "  Y = 0\n"
8746                "}\n",
8747                AllmanBraceStyle);
8748 
8749   verifyFormat("@interface BSApplicationController ()\n"
8750                "{\n"
8751                "@private\n"
8752                "  id _extraIvar;\n"
8753                "}\n"
8754                "@end\n",
8755                AllmanBraceStyle);
8756 
8757   verifyFormat("#ifdef _DEBUG\n"
8758                "int foo(int i = 0)\n"
8759                "#else\n"
8760                "int foo(int i = 5)\n"
8761                "#endif\n"
8762                "{\n"
8763                "  return i;\n"
8764                "}",
8765                AllmanBraceStyle);
8766 
8767   verifyFormat("void foo() {}\n"
8768                "void bar()\n"
8769                "#ifdef _DEBUG\n"
8770                "{\n"
8771                "  foo();\n"
8772                "}\n"
8773                "#else\n"
8774                "{\n"
8775                "}\n"
8776                "#endif",
8777                AllmanBraceStyle);
8778 
8779   verifyFormat("void foobar() { int i = 5; }\n"
8780                "#ifdef _DEBUG\n"
8781                "void bar() {}\n"
8782                "#else\n"
8783                "void bar() { foobar(); }\n"
8784                "#endif",
8785                AllmanBraceStyle);
8786 
8787   // This shouldn't affect ObjC blocks..
8788   verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
8789                "  // ...\n"
8790                "  int i;\n"
8791                "}];",
8792                AllmanBraceStyle);
8793   verifyFormat("void (^block)(void) = ^{\n"
8794                "  // ...\n"
8795                "  int i;\n"
8796                "};",
8797                AllmanBraceStyle);
8798   // .. or dict literals.
8799   verifyFormat("void f()\n"
8800                "{\n"
8801                "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
8802                "}",
8803                AllmanBraceStyle);
8804   verifyFormat("int f()\n"
8805                "{ // comment\n"
8806                "  return 42;\n"
8807                "}",
8808                AllmanBraceStyle);
8809 
8810   AllmanBraceStyle.ColumnLimit = 19;
8811   verifyFormat("void f() { int i; }", AllmanBraceStyle);
8812   AllmanBraceStyle.ColumnLimit = 18;
8813   verifyFormat("void f()\n"
8814                "{\n"
8815                "  int i;\n"
8816                "}",
8817                AllmanBraceStyle);
8818   AllmanBraceStyle.ColumnLimit = 80;
8819 
8820   FormatStyle BreakBeforeBraceShortIfs = AllmanBraceStyle;
8821   BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
8822   BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
8823   verifyFormat("void f(bool b)\n"
8824                "{\n"
8825                "  if (b)\n"
8826                "  {\n"
8827                "    return;\n"
8828                "  }\n"
8829                "}\n",
8830                BreakBeforeBraceShortIfs);
8831   verifyFormat("void f(bool b)\n"
8832                "{\n"
8833                "  if (b) return;\n"
8834                "}\n",
8835                BreakBeforeBraceShortIfs);
8836   verifyFormat("void f(bool b)\n"
8837                "{\n"
8838                "  while (b)\n"
8839                "  {\n"
8840                "    return;\n"
8841                "  }\n"
8842                "}\n",
8843                BreakBeforeBraceShortIfs);
8844 }
8845 
8846 TEST_F(FormatTest, GNUBraceBreaking) {
8847   FormatStyle GNUBraceStyle = getLLVMStyle();
8848   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
8849   verifyFormat("namespace a\n"
8850                "{\n"
8851                "class A\n"
8852                "{\n"
8853                "  void f()\n"
8854                "  {\n"
8855                "    int a;\n"
8856                "    {\n"
8857                "      int b;\n"
8858                "    }\n"
8859                "    if (true)\n"
8860                "      {\n"
8861                "        a();\n"
8862                "        b();\n"
8863                "      }\n"
8864                "  }\n"
8865                "  void g() { return; }\n"
8866                "}\n"
8867                "}",
8868                GNUBraceStyle);
8869 
8870   verifyFormat("void f()\n"
8871                "{\n"
8872                "  if (true)\n"
8873                "    {\n"
8874                "      a();\n"
8875                "    }\n"
8876                "  else if (false)\n"
8877                "    {\n"
8878                "      b();\n"
8879                "    }\n"
8880                "  else\n"
8881                "    {\n"
8882                "      c();\n"
8883                "    }\n"
8884                "}\n",
8885                GNUBraceStyle);
8886 
8887   verifyFormat("void f()\n"
8888                "{\n"
8889                "  for (int i = 0; i < 10; ++i)\n"
8890                "    {\n"
8891                "      a();\n"
8892                "    }\n"
8893                "  while (false)\n"
8894                "    {\n"
8895                "      b();\n"
8896                "    }\n"
8897                "  do\n"
8898                "    {\n"
8899                "      c();\n"
8900                "    }\n"
8901                "  while (false);\n"
8902                "}\n",
8903                GNUBraceStyle);
8904 
8905   verifyFormat("void f(int a)\n"
8906                "{\n"
8907                "  switch (a)\n"
8908                "    {\n"
8909                "    case 0:\n"
8910                "      break;\n"
8911                "    case 1:\n"
8912                "      {\n"
8913                "        break;\n"
8914                "      }\n"
8915                "    case 2:\n"
8916                "      {\n"
8917                "      }\n"
8918                "      break;\n"
8919                "    default:\n"
8920                "      break;\n"
8921                "    }\n"
8922                "}\n",
8923                GNUBraceStyle);
8924 
8925   verifyFormat("enum X\n"
8926                "{\n"
8927                "  Y = 0,\n"
8928                "}\n",
8929                GNUBraceStyle);
8930 
8931   verifyFormat("@interface BSApplicationController ()\n"
8932                "{\n"
8933                "@private\n"
8934                "  id _extraIvar;\n"
8935                "}\n"
8936                "@end\n",
8937                GNUBraceStyle);
8938 
8939   verifyFormat("#ifdef _DEBUG\n"
8940                "int foo(int i = 0)\n"
8941                "#else\n"
8942                "int foo(int i = 5)\n"
8943                "#endif\n"
8944                "{\n"
8945                "  return i;\n"
8946                "}",
8947                GNUBraceStyle);
8948 
8949   verifyFormat("void foo() {}\n"
8950                "void bar()\n"
8951                "#ifdef _DEBUG\n"
8952                "{\n"
8953                "  foo();\n"
8954                "}\n"
8955                "#else\n"
8956                "{\n"
8957                "}\n"
8958                "#endif",
8959                GNUBraceStyle);
8960 
8961   verifyFormat("void foobar() { int i = 5; }\n"
8962                "#ifdef _DEBUG\n"
8963                "void bar() {}\n"
8964                "#else\n"
8965                "void bar() { foobar(); }\n"
8966                "#endif",
8967                GNUBraceStyle);
8968 }
8969 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
8970   verifyFormat("void f() {\n"
8971                "  try {\n"
8972                "  } catch (const Exception &e) {\n"
8973                "  }\n"
8974                "}\n",
8975                getLLVMStyle());
8976 }
8977 
8978 TEST_F(FormatTest, UnderstandsPragmas) {
8979   verifyFormat("#pragma omp reduction(| : var)");
8980   verifyFormat("#pragma omp reduction(+ : var)");
8981 
8982   EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string "
8983             "(including parentheses).",
8984             format("#pragma    mark   Any non-hyphenated or hyphenated string "
8985                    "(including parentheses)."));
8986 }
8987 
8988 TEST_F(FormatTest, UnderstandPragmaOption) {
8989   verifyFormat("#pragma option -C -A");
8990 
8991   EXPECT_EQ("#pragma option -C -A", format("#pragma    option   -C   -A"));
8992 }
8993 
8994 #define EXPECT_ALL_STYLES_EQUAL(Styles)                                        \
8995   for (size_t i = 1; i < Styles.size(); ++i)                                   \
8996   EXPECT_EQ(Styles[0], Styles[i]) << "Style #" << i << " of " << Styles.size() \
8997                                   << " differs from Style #0"
8998 
8999 TEST_F(FormatTest, GetsPredefinedStyleByName) {
9000   SmallVector<FormatStyle, 3> Styles;
9001   Styles.resize(3);
9002 
9003   Styles[0] = getLLVMStyle();
9004   EXPECT_TRUE(getPredefinedStyle("LLVM", FormatStyle::LK_Cpp, &Styles[1]));
9005   EXPECT_TRUE(getPredefinedStyle("lLvM", FormatStyle::LK_Cpp, &Styles[2]));
9006   EXPECT_ALL_STYLES_EQUAL(Styles);
9007 
9008   Styles[0] = getGoogleStyle();
9009   EXPECT_TRUE(getPredefinedStyle("Google", FormatStyle::LK_Cpp, &Styles[1]));
9010   EXPECT_TRUE(getPredefinedStyle("gOOgle", FormatStyle::LK_Cpp, &Styles[2]));
9011   EXPECT_ALL_STYLES_EQUAL(Styles);
9012 
9013   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9014   EXPECT_TRUE(
9015       getPredefinedStyle("Google", FormatStyle::LK_JavaScript, &Styles[1]));
9016   EXPECT_TRUE(
9017       getPredefinedStyle("gOOgle", FormatStyle::LK_JavaScript, &Styles[2]));
9018   EXPECT_ALL_STYLES_EQUAL(Styles);
9019 
9020   Styles[0] = getChromiumStyle(FormatStyle::LK_Cpp);
9021   EXPECT_TRUE(getPredefinedStyle("Chromium", FormatStyle::LK_Cpp, &Styles[1]));
9022   EXPECT_TRUE(getPredefinedStyle("cHRoMiUM", FormatStyle::LK_Cpp, &Styles[2]));
9023   EXPECT_ALL_STYLES_EQUAL(Styles);
9024 
9025   Styles[0] = getMozillaStyle();
9026   EXPECT_TRUE(getPredefinedStyle("Mozilla", FormatStyle::LK_Cpp, &Styles[1]));
9027   EXPECT_TRUE(getPredefinedStyle("moZILla", FormatStyle::LK_Cpp, &Styles[2]));
9028   EXPECT_ALL_STYLES_EQUAL(Styles);
9029 
9030   Styles[0] = getWebKitStyle();
9031   EXPECT_TRUE(getPredefinedStyle("WebKit", FormatStyle::LK_Cpp, &Styles[1]));
9032   EXPECT_TRUE(getPredefinedStyle("wEbKit", FormatStyle::LK_Cpp, &Styles[2]));
9033   EXPECT_ALL_STYLES_EQUAL(Styles);
9034 
9035   Styles[0] = getGNUStyle();
9036   EXPECT_TRUE(getPredefinedStyle("GNU", FormatStyle::LK_Cpp, &Styles[1]));
9037   EXPECT_TRUE(getPredefinedStyle("gnU", FormatStyle::LK_Cpp, &Styles[2]));
9038   EXPECT_ALL_STYLES_EQUAL(Styles);
9039 
9040   EXPECT_FALSE(getPredefinedStyle("qwerty", FormatStyle::LK_Cpp, &Styles[0]));
9041 }
9042 
9043 TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
9044   SmallVector<FormatStyle, 8> Styles;
9045   Styles.resize(2);
9046 
9047   Styles[0] = getGoogleStyle();
9048   Styles[1] = getLLVMStyle();
9049   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9050   EXPECT_ALL_STYLES_EQUAL(Styles);
9051 
9052   Styles.resize(5);
9053   Styles[0] = getGoogleStyle(FormatStyle::LK_JavaScript);
9054   Styles[1] = getLLVMStyle();
9055   Styles[1].Language = FormatStyle::LK_JavaScript;
9056   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Styles[1]).value());
9057 
9058   Styles[2] = getLLVMStyle();
9059   Styles[2].Language = FormatStyle::LK_JavaScript;
9060   EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
9061                                   "BasedOnStyle: Google",
9062                                   &Styles[2]).value());
9063 
9064   Styles[3] = getLLVMStyle();
9065   Styles[3].Language = FormatStyle::LK_JavaScript;
9066   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
9067                                   "Language: JavaScript",
9068                                   &Styles[3]).value());
9069 
9070   Styles[4] = getLLVMStyle();
9071   Styles[4].Language = FormatStyle::LK_JavaScript;
9072   EXPECT_EQ(0, parseConfiguration("---\n"
9073                                   "BasedOnStyle: LLVM\n"
9074                                   "IndentWidth: 123\n"
9075                                   "---\n"
9076                                   "BasedOnStyle: Google\n"
9077                                   "Language: JavaScript",
9078                                   &Styles[4]).value());
9079   EXPECT_ALL_STYLES_EQUAL(Styles);
9080 }
9081 
9082 #define CHECK_PARSE_BOOL_FIELD(FIELD, CONFIG_NAME)                             \
9083   Style.FIELD = false;                                                         \
9084   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": true", &Style).value());      \
9085   EXPECT_TRUE(Style.FIELD);                                                    \
9086   EXPECT_EQ(0, parseConfiguration(CONFIG_NAME ": false", &Style).value());     \
9087   EXPECT_FALSE(Style.FIELD);
9088 
9089 #define CHECK_PARSE_BOOL(FIELD) CHECK_PARSE_BOOL_FIELD(FIELD, #FIELD)
9090 
9091 #define CHECK_PARSE(TEXT, FIELD, VALUE)                                        \
9092   EXPECT_NE(VALUE, Style.FIELD);                                               \
9093   EXPECT_EQ(0, parseConfiguration(TEXT, &Style).value());                      \
9094   EXPECT_EQ(VALUE, Style.FIELD)
9095 
9096 TEST_F(FormatTest, ParsesConfigurationBools) {
9097   FormatStyle Style = {};
9098   Style.Language = FormatStyle::LK_Cpp;
9099   CHECK_PARSE_BOOL(AlignAfterOpenBracket);
9100   CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft);
9101   CHECK_PARSE_BOOL(AlignOperands);
9102   CHECK_PARSE_BOOL(AlignTrailingComments);
9103   CHECK_PARSE_BOOL(AlignConsecutiveAssignments);
9104   CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
9105   CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine);
9106   CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
9107   CHECK_PARSE_BOOL(AllowShortIfStatementsOnASingleLine);
9108   CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
9109   CHECK_PARSE_BOOL(AlwaysBreakAfterDefinitionReturnType);
9110   CHECK_PARSE_BOOL(AlwaysBreakTemplateDeclarations);
9111   CHECK_PARSE_BOOL(BinPackParameters);
9112   CHECK_PARSE_BOOL(BinPackArguments);
9113   CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
9114   CHECK_PARSE_BOOL(BreakConstructorInitializersBeforeComma);
9115   CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine);
9116   CHECK_PARSE_BOOL(DerivePointerAlignment);
9117   CHECK_PARSE_BOOL_FIELD(DerivePointerAlignment, "DerivePointerBinding");
9118   CHECK_PARSE_BOOL(IndentCaseLabels);
9119   CHECK_PARSE_BOOL(IndentWrappedFunctionNames);
9120   CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks);
9121   CHECK_PARSE_BOOL(ObjCSpaceAfterProperty);
9122   CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
9123   CHECK_PARSE_BOOL(Cpp11BracedListStyle);
9124   CHECK_PARSE_BOOL(SpacesInParentheses);
9125   CHECK_PARSE_BOOL(SpacesInSquareBrackets);
9126   CHECK_PARSE_BOOL(SpacesInAngles);
9127   CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
9128   CHECK_PARSE_BOOL(SpacesInContainerLiterals);
9129   CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
9130   CHECK_PARSE_BOOL(SpaceAfterCStyleCast);
9131   CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators);
9132 }
9133 
9134 #undef CHECK_PARSE_BOOL
9135 
9136 TEST_F(FormatTest, ParsesConfiguration) {
9137   FormatStyle Style = {};
9138   Style.Language = FormatStyle::LK_Cpp;
9139   CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
9140   CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
9141               ConstructorInitializerIndentWidth, 1234u);
9142   CHECK_PARSE("ObjCBlockIndentWidth: 1234", ObjCBlockIndentWidth, 1234u);
9143   CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
9144   CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
9145   CHECK_PARSE("PenaltyBreakBeforeFirstCallParameter: 1234",
9146               PenaltyBreakBeforeFirstCallParameter, 1234u);
9147   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
9148   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
9149               PenaltyReturnTypeOnItsOwnLine, 1234u);
9150   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
9151               SpacesBeforeTrailingComments, 1234u);
9152   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
9153   CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
9154 
9155   Style.PointerAlignment = FormatStyle::PAS_Middle;
9156   CHECK_PARSE("PointerAlignment: Left", PointerAlignment,
9157               FormatStyle::PAS_Left);
9158   CHECK_PARSE("PointerAlignment: Right", PointerAlignment,
9159               FormatStyle::PAS_Right);
9160   CHECK_PARSE("PointerAlignment: Middle", PointerAlignment,
9161               FormatStyle::PAS_Middle);
9162   // For backward compatibility:
9163   CHECK_PARSE("PointerBindsToType: Left", PointerAlignment,
9164               FormatStyle::PAS_Left);
9165   CHECK_PARSE("PointerBindsToType: Right", PointerAlignment,
9166               FormatStyle::PAS_Right);
9167   CHECK_PARSE("PointerBindsToType: Middle", PointerAlignment,
9168               FormatStyle::PAS_Middle);
9169 
9170   Style.Standard = FormatStyle::LS_Auto;
9171   CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
9172   CHECK_PARSE("Standard: Cpp11", Standard, FormatStyle::LS_Cpp11);
9173   CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);
9174   CHECK_PARSE("Standard: C++11", Standard, FormatStyle::LS_Cpp11);
9175   CHECK_PARSE("Standard: Auto", Standard, FormatStyle::LS_Auto);
9176 
9177   Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
9178   CHECK_PARSE("BreakBeforeBinaryOperators: NonAssignment",
9179               BreakBeforeBinaryOperators, FormatStyle::BOS_NonAssignment);
9180   CHECK_PARSE("BreakBeforeBinaryOperators: None", BreakBeforeBinaryOperators,
9181               FormatStyle::BOS_None);
9182   CHECK_PARSE("BreakBeforeBinaryOperators: All", BreakBeforeBinaryOperators,
9183               FormatStyle::BOS_All);
9184   // For backward compatibility:
9185   CHECK_PARSE("BreakBeforeBinaryOperators: false", BreakBeforeBinaryOperators,
9186               FormatStyle::BOS_None);
9187   CHECK_PARSE("BreakBeforeBinaryOperators: true", BreakBeforeBinaryOperators,
9188               FormatStyle::BOS_All);
9189 
9190   Style.UseTab = FormatStyle::UT_ForIndentation;
9191   CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
9192   CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
9193   CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
9194   // For backward compatibility:
9195   CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
9196   CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);
9197 
9198   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
9199   CHECK_PARSE("AllowShortFunctionsOnASingleLine: None",
9200               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9201   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Inline",
9202               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Inline);
9203   CHECK_PARSE("AllowShortFunctionsOnASingleLine: Empty",
9204               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_Empty);
9205   CHECK_PARSE("AllowShortFunctionsOnASingleLine: All",
9206               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9207   // For backward compatibility:
9208   CHECK_PARSE("AllowShortFunctionsOnASingleLine: false",
9209               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_None);
9210   CHECK_PARSE("AllowShortFunctionsOnASingleLine: true",
9211               AllowShortFunctionsOnASingleLine, FormatStyle::SFS_All);
9212 
9213   Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
9214   CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
9215               FormatStyle::SBPO_Never);
9216   CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
9217               FormatStyle::SBPO_Always);
9218   CHECK_PARSE("SpaceBeforeParens: ControlStatements", SpaceBeforeParens,
9219               FormatStyle::SBPO_ControlStatements);
9220   // For backward compatibility:
9221   CHECK_PARSE("SpaceAfterControlStatementKeyword: false", SpaceBeforeParens,
9222               FormatStyle::SBPO_Never);
9223   CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
9224               FormatStyle::SBPO_ControlStatements);
9225 
9226   Style.ColumnLimit = 123;
9227   FormatStyle BaseStyle = getLLVMStyle();
9228   CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
9229   CHECK_PARSE("BasedOnStyle: LLVM\nColumnLimit: 1234", ColumnLimit, 1234u);
9230 
9231   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
9232   CHECK_PARSE("BreakBeforeBraces: Attach", BreakBeforeBraces,
9233               FormatStyle::BS_Attach);
9234   CHECK_PARSE("BreakBeforeBraces: Linux", BreakBeforeBraces,
9235               FormatStyle::BS_Linux);
9236   CHECK_PARSE("BreakBeforeBraces: Stroustrup", BreakBeforeBraces,
9237               FormatStyle::BS_Stroustrup);
9238   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
9239               FormatStyle::BS_Allman);
9240   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
9241 
9242   Style.NamespaceIndentation = FormatStyle::NI_All;
9243   CHECK_PARSE("NamespaceIndentation: None", NamespaceIndentation,
9244               FormatStyle::NI_None);
9245   CHECK_PARSE("NamespaceIndentation: Inner", NamespaceIndentation,
9246               FormatStyle::NI_Inner);
9247   CHECK_PARSE("NamespaceIndentation: All", NamespaceIndentation,
9248               FormatStyle::NI_All);
9249 
9250   Style.ForEachMacros.clear();
9251   std::vector<std::string> BoostForeach;
9252   BoostForeach.push_back("BOOST_FOREACH");
9253   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH]", ForEachMacros, BoostForeach);
9254   std::vector<std::string> BoostAndQForeach;
9255   BoostAndQForeach.push_back("BOOST_FOREACH");
9256   BoostAndQForeach.push_back("Q_FOREACH");
9257   CHECK_PARSE("ForEachMacros: [BOOST_FOREACH, Q_FOREACH]", ForEachMacros,
9258               BoostAndQForeach);
9259 }
9260 
9261 TEST_F(FormatTest, ParsesConfigurationWithLanguages) {
9262   FormatStyle Style = {};
9263   Style.Language = FormatStyle::LK_Cpp;
9264   CHECK_PARSE("Language: Cpp\n"
9265               "IndentWidth: 12",
9266               IndentWidth, 12u);
9267   EXPECT_EQ(parseConfiguration("Language: JavaScript\n"
9268                                "IndentWidth: 34",
9269                                &Style),
9270             ParseError::Unsuitable);
9271   EXPECT_EQ(12u, Style.IndentWidth);
9272   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9273   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9274 
9275   Style.Language = FormatStyle::LK_JavaScript;
9276   CHECK_PARSE("Language: JavaScript\n"
9277               "IndentWidth: 12",
9278               IndentWidth, 12u);
9279   CHECK_PARSE("IndentWidth: 23", IndentWidth, 23u);
9280   EXPECT_EQ(parseConfiguration("Language: Cpp\n"
9281                                "IndentWidth: 34",
9282                                &Style),
9283             ParseError::Unsuitable);
9284   EXPECT_EQ(23u, Style.IndentWidth);
9285   CHECK_PARSE("IndentWidth: 56", IndentWidth, 56u);
9286   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9287 
9288   CHECK_PARSE("BasedOnStyle: LLVM\n"
9289               "IndentWidth: 67",
9290               IndentWidth, 67u);
9291 
9292   CHECK_PARSE("---\n"
9293               "Language: JavaScript\n"
9294               "IndentWidth: 12\n"
9295               "---\n"
9296               "Language: Cpp\n"
9297               "IndentWidth: 34\n"
9298               "...\n",
9299               IndentWidth, 12u);
9300 
9301   Style.Language = FormatStyle::LK_Cpp;
9302   CHECK_PARSE("---\n"
9303               "Language: JavaScript\n"
9304               "IndentWidth: 12\n"
9305               "---\n"
9306               "Language: Cpp\n"
9307               "IndentWidth: 34\n"
9308               "...\n",
9309               IndentWidth, 34u);
9310   CHECK_PARSE("---\n"
9311               "IndentWidth: 78\n"
9312               "---\n"
9313               "Language: JavaScript\n"
9314               "IndentWidth: 56\n"
9315               "...\n",
9316               IndentWidth, 78u);
9317 
9318   Style.ColumnLimit = 123;
9319   Style.IndentWidth = 234;
9320   Style.BreakBeforeBraces = FormatStyle::BS_Linux;
9321   Style.TabWidth = 345;
9322   EXPECT_FALSE(parseConfiguration("---\n"
9323                                   "IndentWidth: 456\n"
9324                                   "BreakBeforeBraces: Allman\n"
9325                                   "---\n"
9326                                   "Language: JavaScript\n"
9327                                   "IndentWidth: 111\n"
9328                                   "TabWidth: 111\n"
9329                                   "---\n"
9330                                   "Language: Cpp\n"
9331                                   "BreakBeforeBraces: Stroustrup\n"
9332                                   "TabWidth: 789\n"
9333                                   "...\n",
9334                                   &Style));
9335   EXPECT_EQ(123u, Style.ColumnLimit);
9336   EXPECT_EQ(456u, Style.IndentWidth);
9337   EXPECT_EQ(FormatStyle::BS_Stroustrup, Style.BreakBeforeBraces);
9338   EXPECT_EQ(789u, Style.TabWidth);
9339 
9340   EXPECT_EQ(parseConfiguration("---\n"
9341                                "Language: JavaScript\n"
9342                                "IndentWidth: 56\n"
9343                                "---\n"
9344                                "IndentWidth: 78\n"
9345                                "...\n",
9346                                &Style),
9347             ParseError::Error);
9348   EXPECT_EQ(parseConfiguration("---\n"
9349                                "Language: JavaScript\n"
9350                                "IndentWidth: 56\n"
9351                                "---\n"
9352                                "Language: JavaScript\n"
9353                                "IndentWidth: 78\n"
9354                                "...\n",
9355                                &Style),
9356             ParseError::Error);
9357 
9358   EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
9359 }
9360 
9361 #undef CHECK_PARSE
9362 
9363 TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
9364   FormatStyle Style = {};
9365   Style.Language = FormatStyle::LK_JavaScript;
9366   Style.BreakBeforeTernaryOperators = true;
9367   EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google", &Style).value());
9368   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9369 
9370   Style.BreakBeforeTernaryOperators = true;
9371   EXPECT_EQ(0, parseConfiguration("---\n"
9372                                   "BasedOnStyle: Google\n"
9373                                   "---\n"
9374                                   "Language: JavaScript\n"
9375                                   "IndentWidth: 76\n"
9376                                   "...\n",
9377                                   &Style).value());
9378   EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
9379   EXPECT_EQ(76u, Style.IndentWidth);
9380   EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
9381 }
9382 
9383 TEST_F(FormatTest, ConfigurationRoundTripTest) {
9384   FormatStyle Style = getLLVMStyle();
9385   std::string YAML = configurationAsText(Style);
9386   FormatStyle ParsedStyle = {};
9387   ParsedStyle.Language = FormatStyle::LK_Cpp;
9388   EXPECT_EQ(0, parseConfiguration(YAML, &ParsedStyle).value());
9389   EXPECT_EQ(Style, ParsedStyle);
9390 }
9391 
9392 TEST_F(FormatTest, WorksFor8bitEncodings) {
9393   EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n"
9394             "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n"
9395             "\"\xe7\xe8\xec\xed\xfe\xfe \"\n"
9396             "\"\xef\xee\xf0\xf3...\"",
9397             format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 "
9398                    "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe "
9399                    "\xef\xee\xf0\xf3...\"",
9400                    getLLVMStyleWithColumns(12)));
9401 }
9402 
9403 TEST_F(FormatTest, HandlesUTF8BOM) {
9404   EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
9405   EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
9406             format("\xef\xbb\xbf#include <iostream>"));
9407   EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
9408             format("\xef\xbb\xbf\n#include <iostream>"));
9409 }
9410 
9411 // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
9412 #if !defined(_MSC_VER)
9413 
9414 TEST_F(FormatTest, CountsUTF8CharactersProperly) {
9415   verifyFormat("\"Однажды в студёную зимнюю пору...\"",
9416                getLLVMStyleWithColumns(35));
9417   verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"",
9418                getLLVMStyleWithColumns(31));
9419   verifyFormat("// Однажды в студёную зимнюю пору...",
9420                getLLVMStyleWithColumns(36));
9421   verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32));
9422   verifyFormat("/* Однажды в студёную зимнюю пору... */",
9423                getLLVMStyleWithColumns(39));
9424   verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */",
9425                getLLVMStyleWithColumns(35));
9426 }
9427 
9428 TEST_F(FormatTest, SplitsUTF8Strings) {
9429   // Non-printable characters' width is currently considered to be the length in
9430   // bytes in UTF8. The characters can be displayed in very different manner
9431   // (zero-width, single width with a substitution glyph, expanded to their code
9432   // (e.g. "<8d>"), so there's no single correct way to handle them.
9433   EXPECT_EQ("\"aaaaÄ\"\n"
9434             "\"\xc2\x8d\";",
9435             format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9436   EXPECT_EQ("\"aaaaaaaÄ\"\n"
9437             "\"\xc2\x8d\";",
9438             format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)));
9439   EXPECT_EQ("\"Однажды, в \"\n"
9440             "\"студёную \"\n"
9441             "\"зимнюю \"\n"
9442             "\"пору,\"",
9443             format("\"Однажды, в студёную зимнюю пору,\"",
9444                    getLLVMStyleWithColumns(13)));
9445   EXPECT_EQ(
9446       "\"一 二 三 \"\n"
9447       "\"四 五六 \"\n"
9448       "\"七 八 九 \"\n"
9449       "\"十\"",
9450       format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)));
9451   EXPECT_EQ("\"一\t二 \"\n"
9452             "\"\t三 \"\n"
9453             "\"四 五\t六 \"\n"
9454             "\"\t七 \"\n"
9455             "\"八九十\tqq\"",
9456             format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"",
9457                    getLLVMStyleWithColumns(11)));
9458 }
9459 
9460 TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
9461   EXPECT_EQ("const char *sssss =\n"
9462             "    \"一二三四五六七八\\\n"
9463             " 九 十\";",
9464             format("const char *sssss = \"一二三四五六七八\\\n"
9465                    " 九 十\";",
9466                    getLLVMStyleWithColumns(30)));
9467 }
9468 
9469 TEST_F(FormatTest, SplitsUTF8LineComments) {
9470   EXPECT_EQ("// aaaaÄ\xc2\x8d",
9471             format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
9472   EXPECT_EQ("// Я из лесу\n"
9473             "// вышел; был\n"
9474             "// сильный\n"
9475             "// мороз.",
9476             format("// Я из лесу вышел; был сильный мороз.",
9477                    getLLVMStyleWithColumns(13)));
9478   EXPECT_EQ("// 一二三\n"
9479             "// 四五六七\n"
9480             "// 八  九\n"
9481             "// 十",
9482             format("// 一二三 四五六七 八  九 十", getLLVMStyleWithColumns(9)));
9483 }
9484 
9485 TEST_F(FormatTest, SplitsUTF8BlockComments) {
9486   EXPECT_EQ("/* Гляжу,\n"
9487             " * поднимается\n"
9488             " * медленно в\n"
9489             " * гору\n"
9490             " * Лошадка,\n"
9491             " * везущая\n"
9492             " * хворосту\n"
9493             " * воз. */",
9494             format("/* Гляжу, поднимается медленно в гору\n"
9495                    " * Лошадка, везущая хворосту воз. */",
9496                    getLLVMStyleWithColumns(13)));
9497   EXPECT_EQ(
9498       "/* 一二三\n"
9499       " * 四五六七\n"
9500       " * 八  九\n"
9501       " * 十  */",
9502       format("/* 一二三 四五六七 八  九 十  */", getLLVMStyleWithColumns(9)));
9503   EXPECT_EQ("/* �������� ��������\n"
9504             " * ��������\n"
9505             " * ������-�� */",
9506             format("/* �������� �������� �������� ������-�� */", getLLVMStyleWithColumns(12)));
9507 }
9508 
9509 #endif // _MSC_VER
9510 
9511 TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
9512   FormatStyle Style = getLLVMStyle();
9513 
9514   Style.ConstructorInitializerIndentWidth = 4;
9515   verifyFormat(
9516       "SomeClass::Constructor()\n"
9517       "    : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9518       "      aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9519       Style);
9520 
9521   Style.ConstructorInitializerIndentWidth = 2;
9522   verifyFormat(
9523       "SomeClass::Constructor()\n"
9524       "  : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9525       "    aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9526       Style);
9527 
9528   Style.ConstructorInitializerIndentWidth = 0;
9529   verifyFormat(
9530       "SomeClass::Constructor()\n"
9531       ": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
9532       "  aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
9533       Style);
9534 }
9535 
9536 TEST_F(FormatTest, BreakConstructorInitializersBeforeComma) {
9537   FormatStyle Style = getLLVMStyle();
9538   Style.BreakConstructorInitializersBeforeComma = true;
9539   Style.ConstructorInitializerIndentWidth = 4;
9540   verifyFormat("SomeClass::Constructor()\n"
9541                "    : a(a)\n"
9542                "    , b(b)\n"
9543                "    , c(c) {}",
9544                Style);
9545   verifyFormat("SomeClass::Constructor()\n"
9546                "    : a(a) {}",
9547                Style);
9548 
9549   Style.ColumnLimit = 0;
9550   verifyFormat("SomeClass::Constructor()\n"
9551                "    : a(a) {}",
9552                Style);
9553   verifyFormat("SomeClass::Constructor()\n"
9554                "    : a(a)\n"
9555                "    , b(b)\n"
9556                "    , c(c) {}",
9557                Style);
9558   verifyFormat("SomeClass::Constructor()\n"
9559                "    : a(a) {\n"
9560                "  foo();\n"
9561                "  bar();\n"
9562                "}",
9563                Style);
9564 
9565   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
9566   verifyFormat("SomeClass::Constructor()\n"
9567                "    : a(a)\n"
9568                "    , b(b)\n"
9569                "    , c(c) {\n}",
9570                Style);
9571   verifyFormat("SomeClass::Constructor()\n"
9572                "    : a(a) {\n}",
9573                Style);
9574 
9575   Style.ColumnLimit = 80;
9576   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
9577   Style.ConstructorInitializerIndentWidth = 2;
9578   verifyFormat("SomeClass::Constructor()\n"
9579                "  : a(a)\n"
9580                "  , b(b)\n"
9581                "  , c(c) {}",
9582                Style);
9583 
9584   Style.ConstructorInitializerIndentWidth = 0;
9585   verifyFormat("SomeClass::Constructor()\n"
9586                ": a(a)\n"
9587                ", b(b)\n"
9588                ", c(c) {}",
9589                Style);
9590 
9591   Style.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
9592   Style.ConstructorInitializerIndentWidth = 4;
9593   verifyFormat("SomeClass::Constructor() : aaaaaaaa(aaaaaaaa) {}", Style);
9594   verifyFormat(
9595       "SomeClass::Constructor() : aaaaa(aaaaa), aaaaa(aaaaa), aaaaa(aaaaa)\n",
9596       Style);
9597   verifyFormat(
9598       "SomeClass::Constructor()\n"
9599       "    : aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa), aaaaaaaa(aaaaaaaa) {}",
9600       Style);
9601   Style.ConstructorInitializerIndentWidth = 4;
9602   Style.ColumnLimit = 60;
9603   verifyFormat("SomeClass::Constructor()\n"
9604                "    : aaaaaaaa(aaaaaaaa)\n"
9605                "    , aaaaaaaa(aaaaaaaa)\n"
9606                "    , aaaaaaaa(aaaaaaaa) {}",
9607                Style);
9608 }
9609 
9610 TEST_F(FormatTest, Destructors) {
9611   verifyFormat("void F(int &i) { i.~int(); }");
9612   verifyFormat("void F(int &i) { i->~int(); }");
9613 }
9614 
9615 TEST_F(FormatTest, FormatsWithWebKitStyle) {
9616   FormatStyle Style = getWebKitStyle();
9617 
9618   // Don't indent in outer namespaces.
9619   verifyFormat("namespace outer {\n"
9620                "int i;\n"
9621                "namespace inner {\n"
9622                "    int i;\n"
9623                "} // namespace inner\n"
9624                "} // namespace outer\n"
9625                "namespace other_outer {\n"
9626                "int i;\n"
9627                "}",
9628                Style);
9629 
9630   // Don't indent case labels.
9631   verifyFormat("switch (variable) {\n"
9632                "case 1:\n"
9633                "case 2:\n"
9634                "    doSomething();\n"
9635                "    break;\n"
9636                "default:\n"
9637                "    ++variable;\n"
9638                "}",
9639                Style);
9640 
9641   // Wrap before binary operators.
9642   EXPECT_EQ("void f()\n"
9643             "{\n"
9644             "    if (aaaaaaaaaaaaaaaa\n"
9645             "        && bbbbbbbbbbbbbbbbbbbbbbbb\n"
9646             "        && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9647             "        return;\n"
9648             "}",
9649             format("void f() {\n"
9650                    "if (aaaaaaaaaaaaaaaa\n"
9651                    "&& bbbbbbbbbbbbbbbbbbbbbbbb\n"
9652                    "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n"
9653                    "return;\n"
9654                    "}",
9655                    Style));
9656 
9657   // Allow functions on a single line.
9658   verifyFormat("void f() { return; }", Style);
9659 
9660   // Constructor initializers are formatted one per line with the "," on the
9661   // new line.
9662   verifyFormat("Constructor()\n"
9663                "    : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
9664                "    , aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaa, // break\n"
9665                "          aaaaaaaaaaaaaa)\n"
9666                "    , aaaaaaaaaaaaaaaaaaaaaaa()\n"
9667                "{\n"
9668                "}",
9669                Style);
9670   verifyFormat("SomeClass::Constructor()\n"
9671                "    : a(a)\n"
9672                "{\n"
9673                "}",
9674                Style);
9675   EXPECT_EQ("SomeClass::Constructor()\n"
9676             "    : a(a)\n"
9677             "{\n"
9678             "}",
9679             format("SomeClass::Constructor():a(a){}", Style));
9680   verifyFormat("SomeClass::Constructor()\n"
9681                "    : a(a)\n"
9682                "    , b(b)\n"
9683                "    , c(c)\n"
9684                "{\n"
9685                "}",
9686                Style);
9687   verifyFormat("SomeClass::Constructor()\n"
9688                "    : a(a)\n"
9689                "{\n"
9690                "    foo();\n"
9691                "    bar();\n"
9692                "}",
9693                Style);
9694 
9695   // Access specifiers should be aligned left.
9696   verifyFormat("class C {\n"
9697                "public:\n"
9698                "    int i;\n"
9699                "};",
9700                Style);
9701 
9702   // Do not align comments.
9703   verifyFormat("int a; // Do not\n"
9704                "double b; // align comments.",
9705                Style);
9706 
9707   // Do not align operands.
9708   EXPECT_EQ("ASSERT(aaaa\n"
9709             "    || bbbb);",
9710             format("ASSERT ( aaaa\n||bbbb);", Style));
9711 
9712   // Accept input's line breaks.
9713   EXPECT_EQ("if (aaaaaaaaaaaaaaa\n"
9714             "    || bbbbbbbbbbbbbbb) {\n"
9715             "    i++;\n"
9716             "}",
9717             format("if (aaaaaaaaaaaaaaa\n"
9718                    "|| bbbbbbbbbbbbbbb) { i++; }",
9719                    Style));
9720   EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n"
9721             "    i++;\n"
9722             "}",
9723             format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style));
9724 
9725   // Don't automatically break all macro definitions (llvm.org/PR17842).
9726   verifyFormat("#define aNumber 10", Style);
9727   // However, generally keep the line breaks that the user authored.
9728   EXPECT_EQ("#define aNumber \\\n"
9729             "    10",
9730             format("#define aNumber \\\n"
9731                    " 10",
9732                    Style));
9733 
9734   // Keep empty and one-element array literals on a single line.
9735   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n"
9736             "                                  copyItems:YES];",
9737             format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n"
9738                    "copyItems:YES];",
9739                    Style));
9740   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n"
9741             "                                  copyItems:YES];",
9742             format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n"
9743                    "             copyItems:YES];",
9744                    Style));
9745   // FIXME: This does not seem right, there should be more indentation before
9746   // the array literal's entries. Nested blocks have the same problem.
9747   EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9748             "    @\"a\",\n"
9749             "    @\"a\"\n"
9750             "]\n"
9751             "                                  copyItems:YES];",
9752             format("NSArray* a = [[NSArray alloc] initWithArray:@[\n"
9753                    "     @\"a\",\n"
9754                    "     @\"a\"\n"
9755                    "     ]\n"
9756                    "       copyItems:YES];",
9757                    Style));
9758   EXPECT_EQ(
9759       "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9760       "                                  copyItems:YES];",
9761       format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n"
9762              "   copyItems:YES];",
9763              Style));
9764 
9765   verifyFormat("[self.a b:c c:d];", Style);
9766   EXPECT_EQ("[self.a b:c\n"
9767             "        c:d];",
9768             format("[self.a b:c\n"
9769                    "c:d];",
9770                    Style));
9771 }
9772 
9773 TEST_F(FormatTest, FormatsLambdas) {
9774   verifyFormat("int c = [b]() mutable { return [&b] { return b++; }(); }();\n");
9775   verifyFormat("int c = [&] { [=] { return b++; }(); }();\n");
9776   verifyFormat("int c = [&, &a, a] { [=, c, &d] { return b++; }(); }();\n");
9777   verifyFormat("int c = [&a, &a, a] { [=, a, b, &c] { return b++; }(); }();\n");
9778   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] { return b++; }(); }}\n");
9779   verifyFormat("auto c = {[&a, &a, a] { [=, a, b, &c] {}(); }}\n");
9780   verifyFormat("void f() {\n"
9781                "  other(x.begin(), x.end(), [&](int, int) { return 1; });\n"
9782                "}\n");
9783   verifyFormat("void f() {\n"
9784                "  other(x.begin(), //\n"
9785                "        x.end(),   //\n"
9786                "        [&](int, int) { return 1; });\n"
9787                "}\n");
9788   verifyFormat("SomeFunction([]() { // A cool function...\n"
9789                "  return 43;\n"
9790                "});");
9791   EXPECT_EQ("SomeFunction([]() {\n"
9792             "#define A a\n"
9793             "  return 43;\n"
9794             "});",
9795             format("SomeFunction([](){\n"
9796                    "#define A a\n"
9797                    "return 43;\n"
9798                    "});"));
9799   verifyFormat("void f() {\n"
9800                "  SomeFunction([](decltype(x), A *a) {});\n"
9801                "}");
9802   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
9803                "    [](const aaaaaaaaaa &a) { return a; });");
9804   verifyFormat("string abc = SomeFunction(aaaaaaaaaaaaa, aaaaa, []() {\n"
9805                "  SomeOtherFunctioooooooooooooooooooooooooon();\n"
9806                "});");
9807   verifyFormat("Constructor()\n"
9808                "    : Field([] { // comment\n"
9809                "        int i;\n"
9810                "      }) {}");
9811 
9812   // Lambdas with return types.
9813   verifyFormat("int c = []() -> int { return 2; }();\n");
9814   verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
9815   verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
9816   verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
9817   verifyGoogleFormat("auto a = [&b, c](D* d) -> pair<D*, D*> {};");
9818   verifyGoogleFormat("auto a = [&b, c](D* d) -> D& {};");
9819   verifyGoogleFormat("auto a = [&b, c](D* d) -> const D* {};");
9820   verifyFormat("auto aaaaaaaa = [](int i, // break for some reason\n"
9821                "                   int j) -> int {\n"
9822                "  return ffffffffffffffffffffffffffffffffffffffffffff(i * j);\n"
9823                "};");
9824   verifyFormat(
9825       "aaaaaaaaaaaaaaaaaaaaaa(\n"
9826       "    [](aaaaaaaaaaaaaaaaaaaaaaaaaaa &aaa) -> aaaaaaaaaaaaaaaa {\n"
9827       "      return aaaaaaaaaaaaaaaaa;\n"
9828       "    });",
9829       getLLVMStyleWithColumns(70));
9830 
9831   // Multiple lambdas in the same parentheses change indentation rules.
9832   verifyFormat("SomeFunction(\n"
9833                "    []() {\n"
9834                "      int i = 42;\n"
9835                "      return i;\n"
9836                "    },\n"
9837                "    []() {\n"
9838                "      int j = 43;\n"
9839                "      return j;\n"
9840                "    });");
9841 
9842   // More complex introducers.
9843   verifyFormat("return [i, args...] {};");
9844 
9845   // Not lambdas.
9846   verifyFormat("constexpr char hello[]{\"hello\"};");
9847   verifyFormat("double &operator[](int i) { return 0; }\n"
9848                "int i;");
9849   verifyFormat("std::unique_ptr<int[]> foo() {}");
9850   verifyFormat("int i = a[a][a]->f();");
9851   verifyFormat("int i = (*b)[a]->f();");
9852 
9853   // Other corner cases.
9854   verifyFormat("void f() {\n"
9855                "  bar([]() {} // Did not respect SpacesBeforeTrailingComments\n"
9856                "      );\n"
9857                "}");
9858 
9859   // Lambdas created through weird macros.
9860   verifyFormat("void f() {\n"
9861                "  MACRO((const AA &a) { return 1; });\n"
9862                "}");
9863 
9864   verifyFormat("if (blah_blah(whatever, whatever, [] {\n"
9865                "      doo_dah();\n"
9866                "      doo_dah();\n"
9867                "    })) {\n"
9868                "}");
9869   verifyFormat("auto lambda = []() {\n"
9870                "  int a = 2\n"
9871                "#if A\n"
9872                "          + 2\n"
9873                "#endif\n"
9874                "      ;\n"
9875                "};");
9876 }
9877 
9878 TEST_F(FormatTest, FormatsBlocks) {
9879   FormatStyle ShortBlocks = getLLVMStyle();
9880   ShortBlocks.AllowShortBlocksOnASingleLine = true;
9881   verifyFormat("int (^Block)(int, int);", ShortBlocks);
9882   verifyFormat("int (^Block1)(int, int) = ^(int i, int j)", ShortBlocks);
9883   verifyFormat("void (^block)(int) = ^(id test) { int i; };", ShortBlocks);
9884   verifyFormat("void (^block)(int) = ^(int test) { int i; };", ShortBlocks);
9885   verifyFormat("void (^block)(int) = ^id(int test) { int i; };", ShortBlocks);
9886   verifyFormat("void (^block)(int) = ^int(int test) { int i; };", ShortBlocks);
9887 
9888   verifyFormat("foo(^{ bar(); });", ShortBlocks);
9889   verifyFormat("foo(a, ^{ bar(); });", ShortBlocks);
9890   verifyFormat("{ void (^block)(Object *x); }", ShortBlocks);
9891 
9892   verifyFormat("[operation setCompletionBlock:^{\n"
9893                "  [self onOperationDone];\n"
9894                "}];");
9895   verifyFormat("int i = {[operation setCompletionBlock:^{\n"
9896                "  [self onOperationDone];\n"
9897                "}]};");
9898   verifyFormat("[operation setCompletionBlock:^(int *i) {\n"
9899                "  f();\n"
9900                "}];");
9901   verifyFormat("int a = [operation block:^int(int *i) {\n"
9902                "  return 1;\n"
9903                "}];");
9904   verifyFormat("[myObject doSomethingWith:arg1\n"
9905                "                      aaa:^int(int *a) {\n"
9906                "                        return 1;\n"
9907                "                      }\n"
9908                "                      bbb:f(a * bbbbbbbb)];");
9909 
9910   verifyFormat("[operation setCompletionBlock:^{\n"
9911                "  [self.delegate newDataAvailable];\n"
9912                "}];",
9913                getLLVMStyleWithColumns(60));
9914   verifyFormat("dispatch_async(_fileIOQueue, ^{\n"
9915                "  NSString *path = [self sessionFilePath];\n"
9916                "  if (path) {\n"
9917                "    // ...\n"
9918                "  }\n"
9919                "});");
9920   verifyFormat("[[SessionService sharedService]\n"
9921                "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9922                "      if (window) {\n"
9923                "        [self windowDidLoad:window];\n"
9924                "      } else {\n"
9925                "        [self errorLoadingWindow];\n"
9926                "      }\n"
9927                "    }];");
9928   verifyFormat("void (^largeBlock)(void) = ^{\n"
9929                "  // ...\n"
9930                "};\n",
9931                getLLVMStyleWithColumns(40));
9932   verifyFormat("[[SessionService sharedService]\n"
9933                "    loadWindowWithCompletionBlock: //\n"
9934                "        ^(SessionWindow *window) {\n"
9935                "          if (window) {\n"
9936                "            [self windowDidLoad:window];\n"
9937                "          } else {\n"
9938                "            [self errorLoadingWindow];\n"
9939                "          }\n"
9940                "        }];",
9941                getLLVMStyleWithColumns(60));
9942   verifyFormat("[myObject doSomethingWith:arg1\n"
9943                "    firstBlock:^(Foo *a) {\n"
9944                "      // ...\n"
9945                "      int i;\n"
9946                "    }\n"
9947                "    secondBlock:^(Bar *b) {\n"
9948                "      // ...\n"
9949                "      int i;\n"
9950                "    }\n"
9951                "    thirdBlock:^Foo(Bar *b) {\n"
9952                "      // ...\n"
9953                "      int i;\n"
9954                "    }];");
9955   verifyFormat("[myObject doSomethingWith:arg1\n"
9956                "               firstBlock:-1\n"
9957                "              secondBlock:^(Bar *b) {\n"
9958                "                // ...\n"
9959                "                int i;\n"
9960                "              }];");
9961 
9962   verifyFormat("f(^{\n"
9963                "  @autoreleasepool {\n"
9964                "    if (a) {\n"
9965                "      g();\n"
9966                "    }\n"
9967                "  }\n"
9968                "});");
9969   verifyFormat("Block b = ^int *(A *a, B *b) {}");
9970 
9971   FormatStyle FourIndent = getLLVMStyle();
9972   FourIndent.ObjCBlockIndentWidth = 4;
9973   verifyFormat("[operation setCompletionBlock:^{\n"
9974                "    [self onOperationDone];\n"
9975                "}];",
9976                FourIndent);
9977 }
9978 
9979 TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
9980   FormatStyle ZeroColumn = getLLVMStyle();
9981   ZeroColumn.ColumnLimit = 0;
9982 
9983   verifyFormat("[[SessionService sharedService] "
9984                "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9985                "  if (window) {\n"
9986                "    [self windowDidLoad:window];\n"
9987                "  } else {\n"
9988                "    [self errorLoadingWindow];\n"
9989                "  }\n"
9990                "}];",
9991                ZeroColumn);
9992   EXPECT_EQ("[[SessionService sharedService]\n"
9993             "    loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
9994             "      if (window) {\n"
9995             "        [self windowDidLoad:window];\n"
9996             "      } else {\n"
9997             "        [self errorLoadingWindow];\n"
9998             "      }\n"
9999             "    }];",
10000             format("[[SessionService sharedService]\n"
10001                    "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
10002                    "                if (window) {\n"
10003                    "    [self windowDidLoad:window];\n"
10004                    "  } else {\n"
10005                    "    [self errorLoadingWindow];\n"
10006                    "  }\n"
10007                    "}];",
10008                    ZeroColumn));
10009   verifyFormat("[myObject doSomethingWith:arg1\n"
10010                "    firstBlock:^(Foo *a) {\n"
10011                "      // ...\n"
10012                "      int i;\n"
10013                "    }\n"
10014                "    secondBlock:^(Bar *b) {\n"
10015                "      // ...\n"
10016                "      int i;\n"
10017                "    }\n"
10018                "    thirdBlock:^Foo(Bar *b) {\n"
10019                "      // ...\n"
10020                "      int i;\n"
10021                "    }];",
10022                ZeroColumn);
10023   verifyFormat("f(^{\n"
10024                "  @autoreleasepool {\n"
10025                "    if (a) {\n"
10026                "      g();\n"
10027                "    }\n"
10028                "  }\n"
10029                "});",
10030                ZeroColumn);
10031   verifyFormat("void (^largeBlock)(void) = ^{\n"
10032                "  // ...\n"
10033                "};",
10034                ZeroColumn);
10035 
10036   ZeroColumn.AllowShortBlocksOnASingleLine = true;
10037   EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
10038             format("void   (^largeBlock)(void) = ^{ int   i; };",
10039                    ZeroColumn));
10040   ZeroColumn.AllowShortBlocksOnASingleLine = false;
10041   EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
10042             "  int i;\n"
10043             "};",
10044             format("void   (^largeBlock)(void) = ^{ int   i; };", ZeroColumn));
10045 }
10046 
10047 TEST_F(FormatTest, SupportsCRLF) {
10048   EXPECT_EQ("int a;\r\n"
10049             "int b;\r\n"
10050             "int c;\r\n",
10051             format("int a;\r\n"
10052                    "  int b;\r\n"
10053                    "    int c;\r\n",
10054                    getLLVMStyle()));
10055   EXPECT_EQ("int a;\r\n"
10056             "int b;\r\n"
10057             "int c;\r\n",
10058             format("int a;\r\n"
10059                    "  int b;\n"
10060                    "    int c;\r\n",
10061                    getLLVMStyle()));
10062   EXPECT_EQ("int a;\n"
10063             "int b;\n"
10064             "int c;\n",
10065             format("int a;\r\n"
10066                    "  int b;\n"
10067                    "    int c;\n",
10068                    getLLVMStyle()));
10069   EXPECT_EQ("\"aaaaaaa \"\r\n"
10070             "\"bbbbbbb\";\r\n",
10071             format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)));
10072   EXPECT_EQ("#define A \\\r\n"
10073             "  b;      \\\r\n"
10074             "  c;      \\\r\n"
10075             "  d;\r\n",
10076             format("#define A \\\r\n"
10077                    "  b; \\\r\n"
10078                    "  c; d; \r\n",
10079                    getGoogleStyle()));
10080 
10081   EXPECT_EQ("/*\r\n"
10082             "multi line block comments\r\n"
10083             "should not introduce\r\n"
10084             "an extra carriage return\r\n"
10085             "*/\r\n",
10086             format("/*\r\n"
10087                    "multi line block comments\r\n"
10088                    "should not introduce\r\n"
10089                    "an extra carriage return\r\n"
10090                    "*/\r\n"));
10091 }
10092 
10093 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
10094   verifyFormat("MY_CLASS(C) {\n"
10095                "  int i;\n"
10096                "  int j;\n"
10097                "};");
10098 }
10099 
10100 TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
10101   FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
10102   TwoIndent.ContinuationIndentWidth = 2;
10103 
10104   EXPECT_EQ("int i =\n"
10105             "  longFunction(\n"
10106             "    arg);",
10107             format("int i = longFunction(arg);", TwoIndent));
10108 
10109   FormatStyle SixIndent = getLLVMStyleWithColumns(20);
10110   SixIndent.ContinuationIndentWidth = 6;
10111 
10112   EXPECT_EQ("int i =\n"
10113             "      longFunction(\n"
10114             "            arg);",
10115             format("int i = longFunction(arg);", SixIndent));
10116 }
10117 
10118 TEST_F(FormatTest, SpacesInAngles) {
10119   FormatStyle Spaces = getLLVMStyle();
10120   Spaces.SpacesInAngles = true;
10121 
10122   verifyFormat("static_cast< int >(arg);", Spaces);
10123   verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
10124   verifyFormat("f< int, float >();", Spaces);
10125   verifyFormat("template <> g() {}", Spaces);
10126   verifyFormat("template < std::vector< int > > f() {}", Spaces);
10127 
10128   Spaces.Standard = FormatStyle::LS_Cpp03;
10129   Spaces.SpacesInAngles = true;
10130   verifyFormat("A< A< int > >();", Spaces);
10131 
10132   Spaces.SpacesInAngles = false;
10133   verifyFormat("A<A<int> >();", Spaces);
10134 
10135   Spaces.Standard = FormatStyle::LS_Cpp11;
10136   Spaces.SpacesInAngles = true;
10137   verifyFormat("A< A< int > >();", Spaces);
10138 
10139   Spaces.SpacesInAngles = false;
10140   verifyFormat("A<A<int>>();", Spaces);
10141 }
10142 
10143 TEST_F(FormatTest, TripleAngleBrackets) {
10144   verifyFormat("f<<<1, 1>>>();");
10145   verifyFormat("f<<<1, 1, 1, s>>>();");
10146   verifyFormat("f<<<a, b, c, d>>>();");
10147   EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();"));
10148   verifyFormat("f<param><<<1, 1>>>();");
10149   verifyFormat("f<1><<<1, 1>>>();");
10150   EXPECT_EQ("f<param><<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();"));
10151   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10152                "aaaaaaaaaaa<<<\n    1, 1>>>();");
10153 }
10154 
10155 TEST_F(FormatTest, MergeLessLessAtEnd) {
10156   verifyFormat("<<");
10157   EXPECT_EQ("< < <", format("\\\n<<<"));
10158   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10159                "aaallvm::outs() <<");
10160   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10161                "aaaallvm::outs()\n    <<");
10162 }
10163 
10164 TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
10165   std::string code = "#if A\n"
10166                      "#if B\n"
10167                      "a.\n"
10168                      "#endif\n"
10169                      "    a = 1;\n"
10170                      "#else\n"
10171                      "#endif\n"
10172                      "#if C\n"
10173                      "#else\n"
10174                      "#endif\n";
10175   EXPECT_EQ(code, format(code));
10176 }
10177 
10178 TEST_F(FormatTest, HandleConflictMarkers) {
10179   // Git/SVN conflict markers.
10180   EXPECT_EQ("int a;\n"
10181             "void f() {\n"
10182             "  callme(some(parameter1,\n"
10183             "<<<<<<< text by the vcs\n"
10184             "              parameter2),\n"
10185             "||||||| text by the vcs\n"
10186             "              parameter2),\n"
10187             "         parameter3,\n"
10188             "======= text by the vcs\n"
10189             "              parameter2, parameter3),\n"
10190             ">>>>>>> text by the vcs\n"
10191             "         otherparameter);\n",
10192             format("int a;\n"
10193                    "void f() {\n"
10194                    "  callme(some(parameter1,\n"
10195                    "<<<<<<< text by the vcs\n"
10196                    "  parameter2),\n"
10197                    "||||||| text by the vcs\n"
10198                    "  parameter2),\n"
10199                    "  parameter3,\n"
10200                    "======= text by the vcs\n"
10201                    "  parameter2,\n"
10202                    "  parameter3),\n"
10203                    ">>>>>>> text by the vcs\n"
10204                    "  otherparameter);\n"));
10205 
10206   // Perforce markers.
10207   EXPECT_EQ("void f() {\n"
10208             "  function(\n"
10209             ">>>> text by the vcs\n"
10210             "      parameter,\n"
10211             "==== text by the vcs\n"
10212             "      parameter,\n"
10213             "==== text by the vcs\n"
10214             "      parameter,\n"
10215             "<<<< text by the vcs\n"
10216             "      parameter);\n",
10217             format("void f() {\n"
10218                    "  function(\n"
10219                    ">>>> text by the vcs\n"
10220                    "  parameter,\n"
10221                    "==== text by the vcs\n"
10222                    "  parameter,\n"
10223                    "==== text by the vcs\n"
10224                    "  parameter,\n"
10225                    "<<<< text by the vcs\n"
10226                    "  parameter);\n"));
10227 
10228   EXPECT_EQ("<<<<<<<\n"
10229             "|||||||\n"
10230             "=======\n"
10231             ">>>>>>>",
10232             format("<<<<<<<\n"
10233                    "|||||||\n"
10234                    "=======\n"
10235                    ">>>>>>>"));
10236 
10237   EXPECT_EQ("<<<<<<<\n"
10238             "|||||||\n"
10239             "int i;\n"
10240             "=======\n"
10241             ">>>>>>>",
10242             format("<<<<<<<\n"
10243                    "|||||||\n"
10244                    "int i;\n"
10245                    "=======\n"
10246                    ">>>>>>>"));
10247 
10248   // FIXME: Handle parsing of macros around conflict markers correctly:
10249   EXPECT_EQ("#define Macro \\\n"
10250             "<<<<<<<\n"
10251             "Something \\\n"
10252             "|||||||\n"
10253             "Else \\\n"
10254             "=======\n"
10255             "Other \\\n"
10256             ">>>>>>>\n"
10257             "    End int i;\n",
10258             format("#define Macro \\\n"
10259                    "<<<<<<<\n"
10260                    "  Something \\\n"
10261                    "|||||||\n"
10262                    "  Else \\\n"
10263                    "=======\n"
10264                    "  Other \\\n"
10265                    ">>>>>>>\n"
10266                    "  End\n"
10267                    "int i;\n"));
10268 }
10269 
10270 TEST_F(FormatTest, DisableRegions) {
10271   EXPECT_EQ("int i;\n"
10272             "// clang-format off\n"
10273             "  int j;\n"
10274             "// clang-format on\n"
10275             "int k;",
10276             format(" int  i;\n"
10277                    "   // clang-format off\n"
10278                    "  int j;\n"
10279                    " // clang-format on\n"
10280                    "   int   k;"));
10281   EXPECT_EQ("int i;\n"
10282             "/* clang-format off */\n"
10283             "  int j;\n"
10284             "/* clang-format on */\n"
10285             "int k;",
10286             format(" int  i;\n"
10287                    "   /* clang-format off */\n"
10288                    "  int j;\n"
10289                    " /* clang-format on */\n"
10290                    "   int   k;"));
10291 }
10292 
10293 TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); }
10294 
10295 } // end namespace tooling
10296 } // end namespace clang
10297